Completed
Push — master ( a5d74c...946f06 )
by Jakub
06:04
created

CombatBase::setActionSelector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Numbers;
7
8
/**
9
 * Handles combat
10
 * 
11
 * @author Jakub Konečný
12
 * @property-read CombatLogger $log Log from the combat
13
 * @property-read int $winner Team which won the combat/0 if there is no winner yet
14
 * @property-read int $round Number of current round
15
 * @property-read int $roundLimit
16
 * @property-read Team $team1
17
 * @property-read Team $team2
18
 * @property-read int $team1Damage
19
 * @property-read int $team2Damage
20
 * @property ISuccessCalculator $successCalculator
21
 * @property ICombatActionSelector $actionSelector
22
 * @property callable $victoryCondition To evaluate the winner of combat. Gets combat as parameter, should return winning team (1/2) or 0 if there is not winner (yet)
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 166 characters
Loading history...
23
 * @property callable $healers To determine characters that are supposed to heal their team. Gets team1 and team2 as parameters, should return Team
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 147 characters
Loading history...
24
 * @method void onCombatStart(CombatBase $combat)
25
 * @method void onCombatEnd(CombatBase $combat)
26
 * @method void onRoundStart(CombatBase $combat)
27
 * @method void onRound(CombatBase $combat)
28
 * @method void onRoundEnd(CombatBase $combat)
29
 * @method void onAttack(Character $attacker, Character $defender)
30
 * @method void onSkillAttack(Character $attacker, Character $defender, CharacterAttackSkill $skill)
31
 * @method void onSkillSpecial(Character $character1, Character $target, CharacterSpecialSkill $skill)
32
 * @method void onHeal(Character $healer, Character $patient)
33
 */
34 1
class CombatBase {
35 1
  use \Nette\SmartObject;
36
  
37
  /** @var Team First team */
38
  protected $team1;
39
  /** @var Team Second team */
40
  protected $team2;
41
  /** @var CombatLogger */
42
  protected $log;
43
  /** @var int Number of current round */
44
  protected $round = 0;
45
  /** @var int Round limit */
46
  protected $roundLimit = 30;
47
  /** @var array Dealt damage by team */
48
  protected $damage = [1 => 0, 2 => 0];
49
  /** @var callable[] */
50
  public $onCombatStart = [];
51
  /** @var callable[] */
52
  public $onCombatEnd = [];
53
  /** @var callable[] */
54
  public $onRoundStart = [];
55
  /** @var callable[] */
56
  public $onRound = [];
57
  /** @var callable[] */
58
  public $onRoundEnd = [];
59
  /** @var callable[] */
60
  public $onAttack = [];
61
  /** @var callable[] */
62
  public $onSkillAttack = [];
63
  /** @var callable[] */
64
  public $onSkillSpecial = [];
65
  /** @var callable[] */
66
  public $onHeal = [];
67
  /** @var callable */
68
  protected $victoryCondition;
69
  /** @var callable */
70
  protected $healers;
71
  /** @var ISuccessCalculator */
72
  protected $successCalculator;
73
  /** @var ICombatActionSelector */
74
  protected $actionSelector;
75
  
76
  public function __construct(CombatLogger $logger, ?ISuccessCalculator $successCalculator = NULL, ?ICombatActionSelector $actionSelector = NULL) {
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 147 characters
Loading history...
77 1
    $this->log = $logger;
78 1
    $this->onCombatStart[] = [$this, "applyEffectProviders"];
79 1
    $this->onCombatStart[] = [$this, "setSkillsCooldowns"];
80 1
    $this->onCombatStart[] = [$this, "assignPositions"];
81 1
    $this->onCombatEnd[] = [$this, "removeCombatEffects"];
82 1
    $this->onCombatEnd[] = [$this, "logCombatResult"];
83 1
    $this->onCombatEnd[] = [$this, "resetInitiative"];
84 1
    $this->onRoundStart[] = [$this, "decreaseEffectsDuration"];
85 1
    $this->onRoundStart[] = [$this ,"recalculateStats"];
86 1
    $this->onRoundStart[] = [$this, "logRoundNumber"];
87 1
    $this->onRoundStart[] = [$this, "applyPoison"];
88 1
    $this->onRound[] = [$this, "mainStage"];
89 1
    $this->onRoundEnd[] = [$this, "decreaseSkillsCooldowns"];
90 1
    $this->onRoundEnd[] = [$this, "resetInitiative"];
91 1
    $this->onAttack[] = [$this, "attackHarm"];
92 1
    $this->onSkillAttack[] = [$this, "useAttackSkill"];
93 1
    $this->onSkillSpecial[] = [$this, "useSpecialSkill"];
94 1
    $this->onHeal[] = [$this, "heal"];
95 1
    $this->victoryCondition = [VictoryConditions::class, "moreDamage"];
96 1
    $this->successCalculator = $successCalculator ?? new RandomSuccessCalculator();
97 1
    $this->actionSelector = $actionSelector ?? new CombatActionSelector();
98 1
    $this->healers = function(): Team {
99
      return new Team("healers");
100
    };
101 1
  }
102
  
103
  public function getRound(): int {
104 1
    return $this->round;
105
  }
106
  
107
  public function getRoundLimit(): int {
108 1
    return $this->roundLimit;
109
  }
110
  
111
  /**
112
   * Set teams
113
   */
114
  public function setTeams(Team $team1, Team $team2): void {
115 1
    if(isset($this->team1)) {
116 1
      throw new ImmutableException("Teams has already been set.");
117
    }
118 1
    $this->team1 = & $team1;
119 1
    $this->team2 = & $team2;
120 1
    $this->log->setTeams($team1, $team2);
121 1
  }
122
  
123
  /**
124
   * Set participants for duel
125
   * Creates teams named after the member
126
   */
127
  public function setDuelParticipants(Character $player, Character $opponent): void {
128 1
    $team1 = new Team($player->name);
129 1
    $team1[] = $player;
130 1
    $team2 = new Team($opponent->name);
131 1
    $team2[] = $opponent;
132 1
    $this->setTeams($team1, $team2);
133 1
  }
134
  
135
  public function getTeam1(): Team {
136 1
    return $this->team1;
137
  }
138
  
139
  public function getTeam2(): Team {
140 1
    return $this->team2;
141
  }
142
  
143
  public function getVictoryCondition(): callable {
144 1
    return $this->victoryCondition;
145
  }
146
  
147
  public function setVictoryCondition(callable $victoryCondition) {
148 1
    $this->victoryCondition = $victoryCondition;
149 1
  }
150
  
151
  public function getHealers(): callable {
152
    return $this->healers;
153
  }
154
  
155
  public function setHealers(callable $healers) {
156 1
    $this->healers = $healers;
157 1
  }
158
  
159
  public function getTeam1Damage(): int {
160 1
    return $this->damage[1];
161
  }
162
  
163
  public function getTeam2Damage(): int {
164 1
    return $this->damage[2];
165
  }
166
  
167
  public function getSuccessCalculator(): ISuccessCalculator {
168 1
    return $this->successCalculator;
169
  }
170
  
171
  public function setSuccessCalculator(ISuccessCalculator $successCalculator): void {
172 1
    $this->successCalculator = $successCalculator;
173 1
  }
174
  
175
  public function getActionSelector(): ICombatActionSelector {
176 1
    return $this->actionSelector;
177
  }
178
  
179
  public function setActionSelector(ICombatActionSelector $actionSelector): void {
180 1
    $this->actionSelector = $actionSelector;
181 1
  }
182
  
183
  /**
184
   * Get winner of combat
185
   * 
186
   * @staticvar int $result
187
   * @return int Winning team/0
188
   */
189
  public function getWinner(): int {
190 1
    static $result = 0;
191 1
    if($result === 0) {
192 1
      $result = call_user_func($this->victoryCondition, $this);
193 1
      $result = Numbers::range($result, 0, 2);
194
    }
195 1
    return $result;
196
  }
197
  
198
  protected function getTeam(Character $character): Team {
199 1
    return $this->team1->hasItems(["id" => $character->id]) ? $this->team1 : $this->team2;
200
  }
201
  
202
  protected function getEnemyTeam(Character $character): Team {
203 1
    return $this->team1->hasItems(["id" => $character->id]) ? $this->team2 : $this->team1;
204
  }
205
  
206
  public function applyEffectProviders(self $combat): void {
207
    /** @var Character[] $characters */
208 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
209 1
    foreach($characters as $character) {
210 1
      foreach($character->effectProviders as $item) {
211 1
        $effects = $item->getCombatEffects();
212 1
        array_walk($effects, function(CharacterEffect $effect) use($character) {
213 1
          $character->addEffect($effect);
214 1
        });
215
      }
216
    }
217 1
  }
218
  
219
  /**
220
   * Set skills' cooldowns
221
   */
222
  public function setSkillsCooldowns(self $combat): void {
223
    /** @var Character[] $characters */
224 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
225 1
    foreach($characters as $character) {
226 1
      foreach($character->skills as $skill) {
227 1
        $skill->resetCooldown();
228
      }
229
    }
230 1
  }
231
  
232
  public function assignPositions(self $combat): void {
233 1
    $assignPositions = function(Team $team) {
234 1
      $row = 1;
235 1
      $column = 0;
236
      /** @var Character $character */
237 1
      foreach($team as $character) {
238
        try {
239 1
          $column++;
240 1
          if($character->positionRow > 0 AND $character->positionColumn > 0) {
241 1
            continue;
242
          }
243
          setPosition:
0 ignored issues
show
introduced by
Use of the GOTO language construct is discouraged
Loading history...
244 1
          $team->setCharacterPosition($character->id, $row, $column);
245 1
        } catch(InvalidCharacterPositionException $e) {
246 1
          if($e->getCode() === InvalidCharacterPositionException::ROW_FULL) {
247 1
            $row++;
248 1
            $column = 1;
249
          } elseif($e->getCode() === InvalidCharacterPositionException::POSITION_OCCUPIED) {
250
            $column++;
251
          } else {
252
            throw $e;
253
          }
254 1
          goto setPosition;
0 ignored issues
show
introduced by
Use of the GOTO language construct is discouraged
Loading history...
255
        }
256
      }
257 1
    };
258 1
    $assignPositions($combat->team1);
259 1
    $assignPositions($combat->team2);
260 1
  }
261
  
262
  public function decreaseEffectsDuration(self $combat): void {
263
    /** @var Character[] $characters */
264 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
265 1
    foreach($characters as $character) {
266 1
      foreach($character->effects as $effect) {
267 1
        $effect->duration--;
268
      }
269
    }
270 1
  }
271
  
272
  /**
273
   * Decrease skills' cooldowns
274
   */
275
  public function decreaseSkillsCooldowns(self $combat): void {
276
    /** @var Character[] $characters */
277 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
278 1
    foreach($characters as $character) {
279 1
      foreach($character->skills as $skill) {
280 1
        $skill->decreaseCooldown();
281
      }
282
    }
283 1
  }
284
  
285
  /**
286
   * Remove combat effects from character at the end of the combat
287
   */
288
  public function removeCombatEffects(self $combat): void {
289
    /** @var Character[] $characters */
290 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
291 1
    foreach($characters as $character) {
292 1
      foreach($character->effects as $effect) {
293 1
        if($effect->duration === CharacterEffect::DURATION_COMBAT OR is_int($effect->duration)) {
294 1
          $character->removeEffect($effect->id);
295
        }
296
      }
297
    }
298 1
  }
299
  
300
  /**
301
   * Add winner to the log
302
   */
303
  public function logCombatResult(self $combat): void {
304 1
    $combat->log->round = 5000;
305
    $params = [
306 1
      "team1name" => $combat->team1->name, "team1damage" => $combat->damage[1],
307 1
      "team2name" => $combat->team2->name, "team2damage" => $combat->damage[2],
308
    ];
309 1
    if($combat->winner === 1) {
310 1
      $params["winner"] = $combat->team1->name;
311
    } else {
312 1
      $params["winner"] = $combat->team2->name;
313
    }
314 1
    $combat->log->logText("combat.log.combatEnd", $params);
315 1
  }
316
  
317
  /**
318
   * Log start of a round
319
   */
320
  public function logRoundNumber(self $combat): void {
321 1
    $combat->log->round = ++$this->round;
322 1
  }
323
  
324
  /**
325
   * Decrease duration of effects and recalculate stats
326
   */
327
  public function recalculateStats(self $combat): void {
328
    /** @var Character[] $characters */
329 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
330 1
    foreach($characters as $character) {
331 1
      $character->recalculateStats();
332
    }
333 1
  }
334
  
335
  /**
336
   * Reset characters' initiative
337
   */
338
  public function resetInitiative(self $combat): void {
339
    /** @var Character[] $characters */
340 1
    $characters = array_merge($combat->team1->toArray(), $combat->team2->toArray());
341 1
    foreach($characters as $character) {
342 1
      $character->resetInitiative();
343
    }
344 1
  }
345
  
346
  /**
347
   * Select target for attack
348
   *
349
   * @internal
350
   */
351
  public function selectAttackTarget(Character $attacker): ?Character {
352 1
    $enemyTeam = $this->getEnemyTeam($attacker);
353 1
    $rowToAttack = $enemyTeam->rowToAttack;
354 1
    if(is_null($rowToAttack)) {
355
      return NULL;
356
    }
357
    /** @var Team $enemies */
358 1
    $enemies = Team::fromArray($enemyTeam->getItems(["positionRow" => $rowToAttack, "hitpoints>" => 0,]), $enemyTeam->name);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 124 characters
Loading history...
359 1
    $target = $enemies->getLowestHpCharacter();
360 1
    if(!is_null($target)) {
361 1
      return $target;
362
    }
363 1
    return $enemies->getRandomCharacter();
364
  }
365
  
366
  /**
367
   * Select target for healing
368
   *
369
   * @internal
370
   */
371
  public function selectHealingTarget(Character $healer): ?Character {
372 1
    return $this->getTeam($healer)->getLowestHpCharacter();
373
  }
374
  
375
  /**
376
   * @internal
377
   */
378
  public function findHealers(): Team {
379 1
    $healers = call_user_func($this->healers, $this->team1, $this->team2);
380 1
    if($healers instanceof Team) {
381 1
      return $healers;
382
    }
383
    return new Team("healers");
384
  }
385
  
386
  protected function doAttackSkill(Character $character, CharacterAttackSkill $skill): void {
387 1
    $targets = [];
388
    /** @var Character $primaryTarget */
389 1
    $primaryTarget = $this->selectAttackTarget($character);
390 1
    switch($skill->skill->target) {
391 1
      case SkillAttack::TARGET_SINGLE:
392 1
        $targets[] = $primaryTarget;
393 1
        break;
394
      case SkillAttack::TARGET_ROW:
395
        $targets = $this->getTeam($primaryTarget)->getItems(["positionRow" => $primaryTarget->positionRow]);
396
        break;
397
      case SkillAttack::TARGET_COLUMN:
398
        $targets = $this->getTeam($primaryTarget)->getItems(["positionColumn" => $primaryTarget->positionColumn]);
399
        break;
400
      default:
401
        throw new NotImplementedException("Target $skill->skill->target for attack skills is not implemented.");
402
    }
403 1
    foreach($targets as $target) {
404 1
      for($i = 1; $i <= $skill->skill->strikes; $i++) {
405 1
        $this->onSkillAttack($character, $target, $skill);
406
      }
407
    }
408 1
  }
409
  
410
  protected function doSpecialSkill(Character $character, CharacterSpecialSkill $skill): void {
411 1
    $targets = [];
412 1
    switch($skill->skill->target) {
413 1
      case SkillSpecial::TARGET_ENEMY:
414
        $targets[] = $this->selectAttackTarget($character);
415
        break;
416 1
      case SkillSpecial::TARGET_SELF:
417 1
        $targets[] = $character;
418 1
        break;
419
      case SkillSpecial::TARGET_PARTY:
420
        $targets = $this->getTeam($character)->toArray();
421
        break;
422
      case SkillSpecial::TARGET_ENEMY_PARTY:
423
        $targets = $this->getEnemyTeam($character)->toArray();
424
        break;
425
      default:
426
        throw new NotImplementedException("Target $skill->skill->target for special skills is not implemented.");
427
    }
428 1
    foreach($targets as $target) {
429 1
      $this->onSkillSpecial($character, $target, $skill);
430
    }
431 1
  }
432
  
433
  /**
434
   * Main stage of a round
435
   */
436
  public function mainStage(self $combat): void {
437
    /** @var Character[] $characters */
438 1
    $characters = array_merge($combat->team1->usableMembers, $combat->team2->usableMembers);
439 1
    usort($characters, function(Character $a, Character $b) {
440 1
      return -1 * strcmp((string) $a->initiative, (string) $b->initiative);
441 1
    });
442 1
    foreach($characters as $character) {
443 1
      $action = $combat->actionSelector->chooseAction($combat, $character);
444 1
      if(!in_array($action, $this->actionSelector->getAllowedActions(), true)) {
445
        continue;
446
      }
447
      switch($action) {
448 1
        case CombatAction::ACTION_ATTACK:
449 1
          $combat->onAttack($character, $combat->selectAttackTarget($character));
450 1
          break;
451 1
        case CombatAction::ACTION_SKILL_ATTACK:
452
          /** @var CharacterAttackSkill $skill */
453 1
          $skill = $character->usableSkills[0];
454 1
          $combat->doAttackSkill($character, $skill);
455 1
          break;
456 1
        case CombatAction::ACTION_SKILL_SPECIAL:
457
          /** @var CharacterSpecialSkill $skill */
458 1
          $skill = $character->usableSkills[0];
459 1
          $combat->doSpecialSkill($character, $skill);
460 1
          break;
461 1
        case CombatAction::ACTION_HEALING:
462 1
          $combat->onHeal($character, $combat->selectHealingTarget($character));
463 1
          break;
464
      }
465
    }
466 1
  }
467
  
468
  /**
469
   * Start next round
470
   * 
471
   * @return int Winning team/0
472
   */
473
  protected function startRound(): int {
474 1
    $this->onRoundStart($this);
475 1
    return $this->getWinner();
476
  }
477
  
478
  /**
479
   * Do a round
480
   */
481
  protected function doRound(): void {
482 1
    $this->onRound($this);
483 1
  }
484
  
485
  /**
486
   * End round
487
   * 
488
   * @return int Winning team/0
489
   */
490
  protected function endRound(): int {
491 1
    $this->onRoundEnd($this);
492 1
    return $this->getWinner();
493
  }
494
  
495
  /**
496
   * Executes the combat
497
   * 
498
   * @return int Winning team
499
   */
500
  public function execute(): int {
501 1
    if(!isset($this->team1)) {
502 1
      throw new InvalidStateException("Teams are not set.");
503
    }
504 1
    $this->onCombatStart($this);
505 1
    while($this->round <= $this->roundLimit) {
506 1
      if($this->startRound() > 0) {
507 1
        break;
508
      }
509 1
      $this->doRound();
510 1
      if($this->endRound() > 0) {
511
        break;
512
      }
513
    }
514 1
    $this->onCombatEnd($this);
515 1
    return $this->getWinner();
516
  }
517
  
518
  /**
519
   * Calculate hit chance for attack/skill attack
520
   */
521
  protected function calculateHitChance(Character $character1, Character $character2, CharacterAttackSkill $skill = NULL): int {
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 128 characters
Loading history...
522 1
    $hitChance = $this->successCalculator->calculateHitChance($character1, $character2, $skill);
523 1
    return Numbers::range($hitChance, ISuccessCalculator::MIN_HIT_CHANCE, ISuccessCalculator::MAX_HIT_CHANCE);
524
  }
525
  
526
  /**
527
   * Check whether action succeeded
528
   */
529
  protected function hasHit(int $hitChance): bool {
530 1
    return $this->successCalculator->hasHit($hitChance);
531
  }
532
  
533
  /**
534
   * Do an attack
535
   * Hit chance = Attacker's hit - Defender's dodge, but at least 15%
536
   * Damage = Attacker's damage - defender's defense
537
   */
538
  public function attackHarm(Character $attacker, Character $defender): void {
539 1
    $result = [];
540 1
    $hitChance = $this->calculateHitChance($attacker, $defender);
541 1
    $result["result"] = $this->hasHit($hitChance);
542 1
    $result["amount"] = 0;
543 1
    if($result["result"]) {
544 1
      $amount = $attacker->damage - $defender->defense;
545 1
      $result["amount"] = Numbers::range($amount, 0, $defender->hitpoints);
546
    }
547 1
    if($result["amount"] > 0) {
548 1
      $defender->harm($result["amount"]);
549
    }
550 1
    $result["action"] = CombatAction::ACTION_ATTACK;
551 1
    $result["name"] = "";
552 1
    $result["character1"] = $attacker;
553 1
    $result["character2"] = $defender;
554 1
    $this->logDamage($attacker, $result["amount"]);
555 1
    $this->log->log($result);
556 1
  }
557
  
558
  /**
559
   * Use an attack skill
560
   */
561
  public function useAttackSkill(Character $attacker, Character $defender, CharacterAttackSkill $skill): void {
562 1
    $result = [];
563 1
    $hitChance = $this->calculateHitChance($attacker, $defender, $skill);
564 1
    $result["result"] = $this->hasHit($hitChance);
565 1
    $result["amount"] = 0;
566 1
    if($result["result"]) {
567 1
      $amount = (int) ($attacker->damage - $defender->defense / 100 * $skill->damage);
568 1
      $result["amount"] = Numbers::range($amount, 0, $defender->hitpoints);
569
    }
570 1
    if($result["amount"]) {
571 1
      $defender->harm($result["amount"]);
572
    }
573 1
    $result["action"] = CombatAction::ACTION_SKILL_ATTACK;
574 1
    $result["name"] = $skill->skill->name;
575 1
    $result["character1"] = $attacker;
576 1
    $result["character2"] = $defender;
577 1
    $this->logDamage($attacker, $result["amount"]);
578 1
    $this->log->log($result);
579 1
    $skill->resetCooldown();
580 1
  }
581
  
582
  /**
583
   * Use a special skill
584
   */
585
  public function useSpecialSkill(Character $character1, Character $target, CharacterSpecialSkill $skill): void {
586
    $result = [
587 1
      "result" => true, "amount" => 0, "action" => CombatAction::ACTION_SKILL_SPECIAL, "name" => $skill->skill->name,
588 1
      "character1" => $character1, "character2" => $target,
589
    ];
590 1
    $effect = new CharacterEffect([
591 1
      "id" => "skill{$skill->skill->id}Effect",
592 1
      "type" => $skill->skill->type,
593 1
      "stat" => ((in_array($skill->skill->type, SkillSpecial::NO_STAT_TYPES, true)) ? NULL : $skill->skill->stat),
594 1
      "value" => $skill->value,
595 1
      "source" => CharacterEffect::SOURCE_SKILL,
596 1
      "duration" => $skill->skill->duration,
597
    ]);
598 1
    $target->addEffect($effect);
599 1
    $this->log->log($result);
600 1
    $skill->resetCooldown();
601 1
  }
602
  
603
  /**
604
   * Heal a character
605
   */
606
  public function heal(Character $healer, Character $patient): void {
607 1
    $result = [];
608 1
    $hitChance = $this->successCalculator->calculateHealingSuccessChance($healer);
609 1
    $hitChance = Numbers::range($hitChance, 0, 100);
610 1
    $result["result"] = $this->successCalculator->hasHit($hitChance);
611 1
    $amount = ($result["result"]) ? (int) ($healer->intelligence / 2) : 0;
612 1
    $result["amount"] = Numbers::range($amount, 0, $patient->maxHitpoints - $patient->hitpoints);
613 1
    if($result["amount"]) {
614 1
      $patient->heal($result["amount"]);
615
    }
616 1
    $result["action"] = CombatAction::ACTION_HEALING;
617 1
    $result["name"] = "";
618 1
    $result["character1"] = $healer;
619 1
    $result["character2"] = $patient;
620 1
    $this->log->log($result);
621 1
  }
622
  
623
  /**
624
   * Harm poisoned characters at start of round
625
   */
626
  public function applyPoison(self $combat): void {
627
    /** @var Character[] $characters */
628 1
    $characters = array_merge($combat->team1->aliveMembers, $combat->team2->aliveMembers);
629 1
    foreach($characters as $character) {
630 1
      foreach($character->effects as $effect) {
631 1
        if($effect->type === SkillSpecial::TYPE_POISON) {
632 1
          $character->harm($effect->value);
633
          $action = [
634 1
            "action" => CombatAction::ACTION_POISON, "result" => true, "amount" => $effect->value,
635 1
            "character1" => $character, "character2" => $character,
636
          ];
637 1
          $combat->log->log($action);
638
        }
639
      }
640
    }
641 1
  }
642
  
643
  /**
644
   * Log dealt damage
645
   */
646
  public function logDamage(Character $attacker, int $amount): void {
647 1
    $team = $this->team1->hasItems(["id" => $attacker->id]) ? 1 : 2;
648 1
    $this->damage[$team] += $amount;
649 1
  }
650
  
651
  public function getLog(): CombatLogger {
652 1
    return $this->log;
653
  }
654
}
655
?>