Issues (30)

src/Character.php (4 issues)

Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Numbers;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Structure for single character
11
 *
12
 * @author Jakub Konečný
13
 * @property-read int|string $id
14
 * @property-read string $name
15
 * @property-read string $gender
16
 * @property-read string $race
17
 * @property-read string $occupation
18
 * @property-read string $specialization
19
 * @property-read int $level
20
 * @property-read int $strength
21
 * @property-read int $strengthBase
22
 * @property-read int $dexterity
23
 * @property-read int $dexterityBase
24
 * @property-read int $constitution
25
 * @property-read int $constitutionBase
26
 * @property-read int $intelligence
27
 * @property-read int $intelligenceBase
28
 * @property-read int $charisma
29
 * @property-read int $charismaBase
30
 * @property-read int $maxHitpoints
31
 * @property-read int $maxHitpointsBase
32
 * @property-read int $hitpoints
33
 * @property-read int $damage
34
 * @property-read int $damageBase
35
 * @property-read int $hit
36
 * @property-read int $hitBase
37
 * @property-read int $dodge
38
 * @property-read int $dodgeBase
39
 * @property-read int $initiative
40
 * @property-read int $initiativeBase
41
 * @property-read string $initiativeFormula
42
 * @property-read int $defense
43
 * @property-read int $defenseBase
44
 * @property-read int|null $activePet
45
 * @property-read bool $stunned
46
 * @property-read bool $poisoned
47
 * @property-read bool $hidden
48
 * @property-read BaseCharacterSkill[] $usableSkills
49
 * @property IInitiativeFormulaParser $initiativeFormulaParser
50
 * @property int $positionRow
51
 * @property int $positionColumn
52
 */
53 1
class Character {
54
  use \Nette\SmartObject;
55
  
56
  public const HITPOINTS_PER_CONSTITUTION = 5;
57
  public const STAT_STRENGTH = "strength";
58
  public const STAT_DEXTERITY = "dexterity";
59
  public const STAT_CONSTITUTION = "constitution";
60
  public const STAT_INTELLIGENCE = "intelligence";
61
  public const STAT_CHARISMA = "charisma";
62
  public const STAT_MAX_HITPOINTS = "maxHitpoints";
63
  public const STAT_DAMAGE = "damage";
64
  public const STAT_DEFENSE = "defense";
65
  public const STAT_HIT = "hit";
66
  public const STAT_DODGE = "dodge";
67
  public const STAT_INITIATIVE = "initiative";
68
  public const BASE_STATS = [
69
    self::STAT_STRENGTH, self::STAT_DEXTERITY, self::STAT_CONSTITUTION, self::STAT_INTELLIGENCE, self::STAT_CHARISMA,
70
  ];
71
  public const SECONDARY_STATS = [
72
    self::STAT_MAX_HITPOINTS, self::STAT_DAMAGE, self::STAT_DEFENSE, self::STAT_HIT, self::STAT_DODGE, self::STAT_INITIATIVE,
0 ignored issues
show
Line exceeds 120 characters; contains 125 characters
Loading history...
73
  ];
74
  public const STATUS_STUNNED = "stunned";
75
  public const STATUS_POISONED = "poisoned";
76
  public const STATUS_HIDDEN = "hidden";
77
78
  protected int|string $id;
79
  protected string $name;
80
  protected string $gender = "male";
81
  protected string $race;
82
  protected string $occupation;
83
  protected string $specialization;
84
  protected int $level;
85
  protected int $strength;
86
  protected int $strengthBase;
87
  protected int $dexterity;
88
  protected int $dexterityBase;
89
  protected int $constitution;
90
  protected int $constitutionBase;
91
  protected int $intelligence;
92
  protected int $intelligenceBase;
93
  protected int $charisma;
94
  protected int $charismaBase;
95
  protected int $maxHitpoints;
96
  protected int $maxHitpointsBase;
97
  protected int $hitpoints;
98
  protected int $damage = 0;
99
  protected int $damageBase = 0;
100
  protected int $hit = 0;
101
  protected int $hitBase = 0;
102
  protected int $dodge = 0;
103
  protected int $dodgeBase = 0;
104
  protected int $initiative = 0;
105
  protected int $initiativeBase = 0;
106
  protected string $initiativeFormula;
107
  protected IInitiativeFormulaParser $initiativeFormulaParser;
108
  protected float $defense = 0;
109
  protected float $defenseBase = 0;
110
  /** @var Equipment[]|EquipmentCollection Character's equipment */
111
  public EquipmentCollection $equipment;
112
  /** @var Pet[]|PetsCollection Character's pets */
113
  public PetsCollection $pets;
114
  /** @var BaseCharacterSkill[]|CharacterSkillsCollection Character's skills */
115
  public CharacterSkillsCollection $skills;
116
  protected ?int $activePet = null;
117
  /** @var CharacterEffect[]|CharacterEffectsCollection Active effects */
118
  public CharacterEffectsCollection $effects;
119
  /** @var ICharacterEffectsProvider[]|CharacterEffectsProvidersCollection */
120
  public CharacterEffectsProvidersCollection $effectProviders;
121
  protected int $positionRow = 0;
122
  protected int $positionColumn = 0;
123
  /** @var callable[] */
124
  protected array $statuses = [];
125
  
126
  /**
127
   *
128
   * @param array $stats Stats of the character
129
   * @param Equipment[] $equipment Equipment of the character
130
   * @param Pet[] $pets Pets owned by the character
131
   * @param BaseCharacterSkill[] $skills Skills acquired by the character
132
   */
133
  public function __construct(array $stats, array $equipment = [], array $pets = [], array $skills = [], IInitiativeFormulaParser $initiativeFormulaParser = new InitiativeFormulaParser()) {
0 ignored issues
show
Line exceeds 120 characters; contains 189 characters
Loading history...
134 1
    $this->initiativeFormulaParser = $initiativeFormulaParser;
135 1
    $this->equipment = EquipmentCollection::fromArray($equipment);
136 1
    $this->pets = PetsCollection::fromArray($pets);
137 1
    $this->effectProviders = CharacterEffectsProvidersCollection::fromArray(array_merge($equipment, $pets));
138 1
    $this->skills = CharacterSkillsCollection::fromArray($skills);
139 1
    $this->equipment->lock();
140 1
    $this->pets->lock();
141 1
    $this->skills->lock();
142 1
    $this->setStats($stats);
143 1
    $this->effects = new CharacterEffectsCollection($this);
144 1
    $this->registerDefaultStatuses();
145 1
  }
146
147
  protected function registerDefaultStatuses(): void {
148 1
    $this->registerStatus(static::STATUS_STUNNED, function(Character $character): bool {
149 1
      return $character->effects->hasItems(["type" => SkillSpecial::TYPE_STUN]);
150 1
    });
151 1
    $this->registerStatus(static::STATUS_POISONED, function(Character $character): int {
152 1
      $poisons = $character->effects->getItems(["type" => SkillSpecial::TYPE_POISON]);
153 1
      $poisonValue = 0;
154
      /** @var CharacterEffect $poison */
155 1
      foreach($poisons as $poison) {
156 1
        $poisonValue += $poison->value;
157
      }
158 1
      return $poisonValue;
159 1
    });
160 1
    $this->registerStatus(static::STATUS_HIDDEN, function(Character $character): bool {
161 1
      return $character->effects->hasItems(["type" => SkillSpecial::TYPE_HIDE]);
162 1
    });
163 1
  }
164
  
165
  protected function setStats(array $stats): void {
166 1
    $requiredStats = array_merge(["id", "name", "level", "initiativeFormula", ], static::BASE_STATS);
167 1
    $allStats = array_merge($requiredStats, ["occupation", "race", "specialization", "gender", ]);
168 1
    $numberStats = static::BASE_STATS;
169 1
    $textStats = ["name", "race", "occupation", "specialization", "initiativeFormula", ];
170 1
    $resolver = new OptionsResolver();
171 1
    $resolver->setDefined($allStats);
172 1
    $resolver->setAllowedTypes("id", ["integer", "string", ]);
173 1
    foreach($numberStats as $stat) {
174 1
      $resolver->setAllowedTypes($stat, ["integer", "float"]);
175 1
      $resolver->setNormalizer($stat, function(OptionsResolver $resolver, $value): int {
176 1
        return (int) $value;
177 1
      });
178
    }
179 1
    foreach($textStats as $stat) {
180 1
      $resolver->setNormalizer($stat, function(OptionsResolver $resolver, $value): string {
181 1
        return (string) $value;
182 1
      });
183
    }
184 1
    $resolver->setRequired($requiredStats);
185 1
    $stats = array_filter($stats, function($key) use($allStats): bool {
186 1
      return in_array($key, $allStats, true);
187 1
    }, ARRAY_FILTER_USE_KEY);
188 1
    $stats = $resolver->resolve($stats);
189 1
    foreach($stats as $key => $value) {
190 1
      $this->$key = $value;
191 1
      if(in_array($key, $numberStats, true)) {
192 1
        $this->{$key . "Base"} = $value;
193
      }
194
    }
195 1
    $this->hitpoints = $this->maxHitpoints = $this->maxHitpointsBase = $this->constitution * static::HITPOINTS_PER_CONSTITUTION;
0 ignored issues
show
Line exceeds 120 characters; contains 128 characters
Loading history...
196 1
    $this->recalculateSecondaryStats();
197 1
    $this->hitBase = $this->hit;
198 1
    $this->dodgeBase = $this->dodge;
199 1
  }
200
201
  protected function getId(): int|string {
202 1
    return $this->id;
203
  }
204
205
  protected function getName(): string {
206 1
    return $this->name;
207
  }
208
  
209
  protected function getGender(): string {
210
    return $this->gender;
211
  }
212
  
213
  protected function getRace(): string {
214
    return $this->race;
215
  }
216
  
217
  protected function getOccupation(): string {
218
    return $this->occupation;
219
  }
220
  
221
  protected function getLevel(): int {
222 1
    return $this->level;
223
  }
224
  
225
  protected function getStrength(): int {
226 1
    return $this->strength;
227
  }
228
  
229
  protected function getStrengthBase(): int {
230
    return $this->strengthBase;
231
  }
232
  
233
  protected function getDexterity(): int {
234 1
    return $this->dexterity;
235
  }
236
  
237
  protected function getDexterityBase(): int {
238
    return $this->dexterityBase;
239
  }
240
  
241
  protected function getConstitution(): int {
242 1
    return $this->constitution;
243
  }
244
  
245
  protected function getConstitutionBase(): int {
246
    return $this->constitutionBase;
247
  }
248
  
249
  protected function getCharisma(): int {
250 1
    return $this->charisma;
251
  }
252
  
253
  protected function getCharismaBase(): int {
254
    return $this->charismaBase;
255
  }
256
  
257
  protected function getMaxHitpoints(): int {
258 1
    return $this->maxHitpoints;
259
  }
260
  
261
  protected function getMaxHitpointsBase(): int {
262 1
    return $this->maxHitpointsBase;
263
  }
264
  
265
  protected function getHitpoints(): int {
266 1
    return $this->hitpoints;
267
  }
268
  
269
  protected function getDamage(): int {
270 1
    return $this->damage;
271
  }
272
  
273
  protected function getDamageBase(): int {
274
    return $this->damageBase;
275
  }
276
  
277
  protected function getHit(): int {
278 1
    return $this->hit;
279
  }
280
  
281
  protected function getHitBase(): int {
282
    return $this->hitBase;
283
  }
284
  
285
  protected function getDodge(): int {
286 1
    return $this->dodge;
287
  }
288
  
289
  protected function getDodgeBase(): int {
290
    return $this->dodgeBase;
291
  }
292
  
293
  protected function getInitiative(): int {
294 1
    return $this->initiative;
295
  }
296
  
297
  protected function getInitiativeBase(): int {
298 1
    return $this->initiativeBase;
299
  }
300
  
301
  protected function getInitiativeFormula(): string {
302 1
    return $this->initiativeFormula;
303
  }
304
  
305
  protected function getDefense(): int {
306 1
    return (int) $this->defense;
307
  }
308
  
309
  protected function getDefenseBase(): int {
310
    return (int) $this->defenseBase;
311
  }
312
313
  protected function getActivePet(): ?int {
314
    /** @var Pet|null $pet */
315 1
    $pet = $this->pets->getItem(["deployed" => true]);
316 1
    if($pet === null) {
317 1
      return null;
318
    }
319 1
    return $pet->id;
320
  }
321
  
322
  protected function isStunned(): bool {
323
    return $this->hasStatus(static::STATUS_STUNNED);
324
  }
325
326
  protected function isPoisoned(): bool {
327 1
    return $this->hasStatus(static::STATUS_POISONED);
328
  }
329
330
  protected function isHidden(): bool {
331 1
    return $this->hasStatus(static::STATUS_HIDDEN);
332
  }
333
  
334
  protected function getSpecialization(): string {
335
    return $this->specialization;
336
  }
337
  
338
  protected function getIntelligence(): int {
339 1
    return $this->intelligence;
340
  }
341
  
342
  protected function getIntelligenceBase(): int {
343
    return $this->intelligenceBase;
344
  }
345
  
346
  protected function getInitiativeFormulaParser(): IInitiativeFormulaParser {
347 1
    return $this->initiativeFormulaParser;
348
  }
349
  
350
  protected function setInitiativeFormulaParser(IInitiativeFormulaParser $initiativeFormulaParser): void {
351 1
    $oldParser = $this->initiativeFormulaParser;
352 1
    $this->initiativeFormulaParser = $initiativeFormulaParser;
353 1
    if($oldParser !== $initiativeFormulaParser) {
354 1
      $this->recalculateStats();
355
    }
356 1
  }
357
  
358
  protected function getPositionRow(): int {
359 1
    return $this->positionRow;
360
  }
361
  
362
  protected function setPositionRow(int $positionRow): void {
363 1
    $this->positionRow = Numbers::range($positionRow, 1, PHP_INT_MAX);
364 1
  }
365
  
366
  protected function getPositionColumn(): int {
367 1
    return $this->positionColumn;
368
  }
369
  
370
  protected function setPositionColumn(int $positionColumn): void {
371 1
    $this->positionColumn = Numbers::range($positionColumn, 1, PHP_INT_MAX);
372 1
  }
373
374
  /**
375
   * Register a new possible character status
376
   *
377
   * @param string $name
378
   * @param callable $callback Used to determine whether the status is active/what value does it have. Is called with Character instance as parameter
0 ignored issues
show
Line exceeds 120 characters; contains 149 characters
Loading history...
379
   */
380
  public function registerStatus(string $name, callable $callback): void {
381 1
    $this->statuses[$name] = $callback;
382 1
  }
383
384
  public function getStatus(string $name): mixed {
385 1
    if(!array_key_exists($name, $this->statuses)) {
386 1
      return null;
387
    }
388 1
    return (call_user_func($this->statuses[$name], $this));
389
  }
390
391
  public function hasStatus(string $status): bool {
392 1
    if(!array_key_exists($status, $this->statuses)) {
393 1
      return false;
394
    }
395 1
    return (bool) (call_user_func($this->statuses[$status], $this));
396
  }
397
  
398
  /**
399
   * @internal
400
   */
401
  public function applyEffectProviders(): void {
402 1
    foreach($this->effectProviders as $item) {
403 1
      $effects = $item->getCombatEffects();
404 1
      array_walk($effects, function(CharacterEffect $effect): void {
405 1
        $this->effects->removeByFilter(["id" => $effect->id]);
406 1
        $this->effects[] = $effect;
407 1
      });
408
    }
409 1
  }
410
  
411
  /**
412
   * @return BaseCharacterSkill[]
413
   */
414
  protected function getUsableSkills(): array {
415 1
    return $this->skills->getItems(["usable" => true]);
416
  }
417
  
418
  /**
419
   * Harm the character
420
   */
421
  public function harm(int $amount): void {
422 1
    $this->hitpoints -= Numbers::range($amount, 0, $this->hitpoints);
423 1
  }
424
  
425
  /**
426
   * Heal the character
427
   */
428
  public function heal(int $amount): void {
429 1
    $this->hitpoints += Numbers::range($amount, 0, $this->maxHitpoints - $this->hitpoints);
430 1
  }
431
  
432
  /**
433
   * Determine which (primary) stat should be used to calculate damage
434
   */
435
  public function damageStat(): string {
436
    /** @var Weapon|null $item */
437 1
    $item = $this->equipment->getItem(["%class%" => Weapon::class, "worn" => true, ]);
438 1
    if($item === null) {
439 1
      return static::STAT_STRENGTH;
440
    }
441 1
    return $item->damageStat;
442
  }
443
  
444
  /**
445
   * Recalculate secondary stats from the the primary ones
446
   */
447
  public function recalculateSecondaryStats(): void {
448 1
    $stats = [
449 1
      static::STAT_DAMAGE => $this->damageStat(), static::STAT_HIT => static::STAT_DEXTERITY,
450 1
      static::STAT_DODGE => static::STAT_DEXTERITY, static::STAT_MAX_HITPOINTS => static::STAT_CONSTITUTION,
451 1
      static::STAT_INITIATIVE => "",
452
    ];
453 1
    foreach($stats as $secondary => $primary) {
454 1
      $gain = $this->$secondary - $this->{$secondary . "Base"};
455 1
      if($secondary === static::STAT_DAMAGE) {
456 1
        $base = (int) round($this->$primary / 2);
457 1
      } elseif($secondary === static::STAT_MAX_HITPOINTS) {
458 1
        $base = $this->$primary * static::HITPOINTS_PER_CONSTITUTION;
459 1
      } elseif($secondary === static::STAT_INITIATIVE) {
460 1
        $base = $this->initiativeFormulaParser->calculateInitiative($this);
461
      } else {
462 1
        $base = $this->$primary * 3;
463
      }
464 1
      $this->{$secondary . "Base"} = $base;
465 1
      $this->$secondary = $base + $gain;
466
    }
467 1
  }
468
  
469
  /**
470
   * Recalculates stats of the character (mostly used during combat)
471
   */
472
  public function recalculateStats(): void {
473 1
    $stats = array_merge(static::BASE_STATS, static::SECONDARY_STATS);
474 1
    $debuffs = [];
475 1
    foreach($stats as $stat) {
476 1
      $$stat = $this->{$stat . "Base"};
477 1
      $debuffs[$stat] = 0;
478
    }
479 1
    $this->effects->removeByFilter(["duration<" => 1]);
480 1
    foreach($this->effects as $effect) {
481 1
      $stat = $effect->stat;
482 1
      $type = $effect->type;
483 1
      $bonus_value = 0;
484 1
      if(!in_array($type, SkillSpecial::NO_STAT_TYPES, true)) {
485 1
        $bonus_value = ($effect->valueAbsolute) ? $effect->value : $$stat / 100 * $effect->value;
486
      }
487 1
      if($type === SkillSpecial::TYPE_BUFF) {
488 1
        $$stat += $bonus_value;
489 1
      } elseif($type === SkillSpecial::TYPE_DEBUFF) {
490 1
        $debuffs[$stat] += $bonus_value;
491
      }
492 1
      unset($stat, $type, $bonus_value);
493
    }
494 1
    foreach($debuffs as $stat => $value) {
495 1
      $value = min($value, 80);
496 1
      $bonus_value = $$stat / 100 * $value;
497 1
      $$stat -= $bonus_value;
498
    }
499 1
    foreach($stats as $stat) {
500 1
      $this->$stat = (int) round($$stat);
501
    }
502 1
    $this->recalculateSecondaryStats();
503 1
  }
504
  
505
  /**
506
   * Reset character's initiative
507
   */
508
  public function resetInitiative(): void {
509 1
    $this->initiative = $this->initiativeBase = 0;
510 1
  }
511
512
  public function canAct(): bool {
513 1
    return !$this->hasStatus(static::STATUS_STUNNED) && $this->hitpoints > 0;
514
  }
515
516
  public function canDefend(): bool {
517 1
    return !$this->hasStatus(static::STATUS_STUNNED);
518
  }
519
}
520
?>