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) |
|
|
|
|
23
|
|
|
* @property callable $healers To determine characters that are supposed to heal their team. Gets team1 and team2 as parameters, should return Team |
|
|
|
|
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) { |
|
|
|
|
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): void { |
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): void { |
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 |
|
$character->applyEffectProviders(); |
211
|
|
|
} |
212
|
1 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set skills' cooldowns |
216
|
|
|
*/ |
217
|
|
|
public function setSkillsCooldowns(self $combat): void { |
218
|
|
|
/** @var Character[] $characters */ |
219
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
220
|
1 |
|
foreach($characters as $character) { |
221
|
1 |
|
foreach($character->skills as $skill) { |
222
|
1 |
|
$skill->resetCooldown(); |
223
|
|
|
} |
224
|
|
|
} |
225
|
1 |
|
} |
226
|
|
|
|
227
|
|
|
public function assignPositions(self $combat): void { |
228
|
1 |
|
$assignPositions = function(Team $team) { |
229
|
1 |
|
$row = 1; |
230
|
1 |
|
$column = 0; |
231
|
|
|
/** @var Character $character */ |
232
|
1 |
|
foreach($team as $character) { |
233
|
|
|
try { |
234
|
1 |
|
$column++; |
235
|
1 |
|
if($character->positionRow > 0 AND $character->positionColumn > 0) { |
236
|
1 |
|
continue; |
237
|
|
|
} |
238
|
|
|
setPosition: |
|
|
|
|
239
|
1 |
|
$team->setCharacterPosition($character->id, $row, $column); |
240
|
1 |
|
} catch(InvalidCharacterPositionException $e) { |
241
|
1 |
|
if($e->getCode() === InvalidCharacterPositionException::ROW_FULL) { |
242
|
1 |
|
$row++; |
243
|
1 |
|
$column = 1; |
244
|
|
|
} elseif($e->getCode() === InvalidCharacterPositionException::POSITION_OCCUPIED) { |
245
|
|
|
$column++; |
246
|
|
|
} else { |
247
|
|
|
throw $e; |
248
|
|
|
} |
249
|
1 |
|
goto setPosition; |
|
|
|
|
250
|
|
|
} |
251
|
|
|
} |
252
|
1 |
|
}; |
253
|
1 |
|
$assignPositions($combat->team1); |
254
|
1 |
|
$assignPositions($combat->team2); |
255
|
1 |
|
} |
256
|
|
|
|
257
|
|
|
public function decreaseEffectsDuration(self $combat): void { |
258
|
|
|
/** @var Character[] $characters */ |
259
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
260
|
1 |
|
foreach($characters as $character) { |
261
|
1 |
|
foreach($character->effects as $effect) { |
262
|
1 |
|
if(is_int($effect->duration)) { |
263
|
1 |
|
$effect->duration--; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
1 |
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Decrease skills' cooldowns |
271
|
|
|
*/ |
272
|
|
|
public function decreaseSkillsCooldowns(self $combat): void { |
273
|
|
|
/** @var Character[] $characters */ |
274
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
275
|
1 |
|
foreach($characters as $character) { |
276
|
1 |
|
foreach($character->skills as $skill) { |
277
|
1 |
|
$skill->decreaseCooldown(); |
278
|
|
|
} |
279
|
|
|
} |
280
|
1 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Remove combat effects from character at the end of the combat |
284
|
|
|
*/ |
285
|
|
|
public function removeCombatEffects(self $combat): void { |
286
|
|
|
/** @var Character[] $characters */ |
287
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
288
|
1 |
|
foreach($characters as $character) { |
289
|
1 |
|
foreach($character->effects as $effect) { |
290
|
1 |
|
if($effect->duration === CharacterEffect::DURATION_COMBAT OR is_int($effect->duration)) { |
291
|
1 |
|
$character->removeEffect($effect->id); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
1 |
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Add winner to the log |
299
|
|
|
*/ |
300
|
|
|
public function logCombatResult(self $combat): void { |
301
|
1 |
|
$combat->log->round = 5000; |
302
|
|
|
$params = [ |
303
|
1 |
|
"team1name" => $combat->team1->name, "team1damage" => $combat->damage[1], |
304
|
1 |
|
"team2name" => $combat->team2->name, "team2damage" => $combat->damage[2], |
305
|
|
|
]; |
306
|
1 |
|
if($combat->winner === 1) { |
307
|
|
|
$params["winner"] = $combat->team1->name; |
308
|
|
|
} else { |
309
|
1 |
|
$params["winner"] = $combat->team2->name; |
310
|
|
|
} |
311
|
1 |
|
$combat->log->logText("combat.log.combatEnd", $params); |
312
|
1 |
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Log start of a round |
316
|
|
|
*/ |
317
|
|
|
public function logRoundNumber(self $combat): void { |
318
|
1 |
|
$combat->log->round = ++$this->round; |
319
|
1 |
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Decrease duration of effects and recalculate stats |
323
|
|
|
*/ |
324
|
|
|
public function recalculateStats(self $combat): void { |
325
|
|
|
/** @var Character[] $characters */ |
326
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
327
|
1 |
|
foreach($characters as $character) { |
328
|
1 |
|
$character->recalculateStats(); |
329
|
|
|
} |
330
|
1 |
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Reset characters' initiative |
334
|
|
|
*/ |
335
|
|
|
public function resetInitiative(self $combat): void { |
336
|
|
|
/** @var Character[] $characters */ |
337
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
338
|
1 |
|
foreach($characters as $character) { |
339
|
1 |
|
$character->resetInitiative(); |
340
|
|
|
} |
341
|
1 |
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Select target for attack |
345
|
|
|
* |
346
|
|
|
* @internal |
347
|
|
|
*/ |
348
|
|
|
public function selectAttackTarget(Character $attacker): ?Character { |
349
|
1 |
|
$enemyTeam = $this->getEnemyTeam($attacker); |
350
|
1 |
|
$rangedWeapon = false; |
351
|
1 |
|
foreach($attacker->equipment as $equipment) { |
352
|
1 |
|
if($equipment instanceof Weapon AND $equipment->isWorn() AND $equipment->ranged) { |
353
|
|
|
$rangedWeapon = true; |
354
|
1 |
|
break; |
355
|
|
|
} |
356
|
|
|
} |
357
|
1 |
|
if(!$rangedWeapon) { |
358
|
1 |
|
$rowToAttack = $enemyTeam->rowToAttack; |
359
|
1 |
|
if(is_null($rowToAttack)) { |
360
|
|
|
return null; |
361
|
|
|
} |
362
|
|
|
/** @var Team $enemies */ |
363
|
1 |
|
$enemies = Team::fromArray($enemyTeam->getItems(["positionRow" => $rowToAttack, "hitpoints>" => 0,]), $enemyTeam->name); |
|
|
|
|
364
|
|
|
} else { |
365
|
|
|
$enemies = $enemyTeam; |
366
|
|
|
} |
367
|
1 |
|
$target = $enemies->getLowestHpCharacter(); |
368
|
1 |
|
if(!is_null($target)) { |
369
|
1 |
|
return $target; |
370
|
|
|
} |
371
|
1 |
|
return $enemies->getRandomCharacter(); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Select target for healing |
376
|
|
|
* |
377
|
|
|
* @internal |
378
|
|
|
*/ |
379
|
|
|
public function selectHealingTarget(Character $healer): ?Character { |
380
|
1 |
|
return $this->getTeam($healer)->getLowestHpCharacter(); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @internal |
385
|
|
|
*/ |
386
|
|
|
public function findHealers(): Team { |
387
|
1 |
|
$healers = call_user_func($this->healers, $this->team1, $this->team2); |
388
|
1 |
|
if($healers instanceof Team) { |
389
|
1 |
|
return $healers; |
390
|
|
|
} |
391
|
|
|
return new Team("healers"); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @throws NotImplementedException |
396
|
|
|
*/ |
397
|
|
|
protected function doAttackSkill(Character $character, CharacterAttackSkill $skill): void { |
398
|
1 |
|
$targets = []; |
399
|
|
|
/** @var Character $primaryTarget */ |
400
|
1 |
|
$primaryTarget = $this->selectAttackTarget($character); |
401
|
1 |
|
switch($skill->skill->target) { |
402
|
1 |
|
case SkillAttack::TARGET_SINGLE: |
403
|
1 |
|
$targets[] = $primaryTarget; |
404
|
1 |
|
break; |
405
|
|
|
case SkillAttack::TARGET_ROW: |
406
|
|
|
$targets = $this->getTeam($primaryTarget)->getItems(["positionRow" => $primaryTarget->positionRow]); |
407
|
|
|
break; |
408
|
|
|
case SkillAttack::TARGET_COLUMN: |
409
|
|
|
$targets = $this->getTeam($primaryTarget)->getItems(["positionColumn" => $primaryTarget->positionColumn]); |
410
|
|
|
break; |
411
|
|
|
default: |
412
|
|
|
throw new NotImplementedException("Target {$skill->skill->target} for attack skills is not implemented."); |
413
|
|
|
} |
414
|
1 |
|
foreach($targets as $target) { |
415
|
1 |
|
for($i = 1; $i <= $skill->skill->strikes; $i++) { |
416
|
1 |
|
$this->onSkillAttack($character, $target, $skill); |
417
|
|
|
} |
418
|
|
|
} |
419
|
1 |
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @throws NotImplementedException |
423
|
|
|
*/ |
424
|
|
|
protected function doSpecialSkill(Character $character, CharacterSpecialSkill $skill): void { |
425
|
1 |
|
$targets = []; |
426
|
1 |
|
switch($skill->skill->target) { |
427
|
1 |
|
case SkillSpecial::TARGET_ENEMY: |
428
|
|
|
$targets[] = $this->selectAttackTarget($character); |
429
|
|
|
break; |
430
|
1 |
|
case SkillSpecial::TARGET_SELF: |
431
|
1 |
|
$targets[] = $character; |
432
|
1 |
|
break; |
433
|
|
|
case SkillSpecial::TARGET_PARTY: |
434
|
|
|
$targets = $this->getTeam($character)->toArray(); |
435
|
|
|
break; |
436
|
|
|
case SkillSpecial::TARGET_ENEMY_PARTY: |
437
|
|
|
$targets = $this->getEnemyTeam($character)->toArray(); |
438
|
|
|
break; |
439
|
|
|
default: |
440
|
|
|
throw new NotImplementedException("Target {$skill->skill->target} for special skills is not implemented."); |
441
|
|
|
} |
442
|
1 |
|
foreach($targets as $target) { |
443
|
1 |
|
$this->onSkillSpecial($character, $target, $skill); |
444
|
|
|
} |
445
|
1 |
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Main stage of a round |
449
|
|
|
* |
450
|
|
|
* @throws NotImplementedException |
451
|
|
|
*/ |
452
|
|
|
public function mainStage(self $combat): void { |
453
|
|
|
/** @var Character[] $characters */ |
454
|
1 |
|
$characters = array_merge($combat->team1->usableMembers, $combat->team2->usableMembers); |
455
|
1 |
|
usort($characters, function(Character $a, Character $b) { |
456
|
1 |
|
return -1 * strcmp((string) $a->initiative, (string) $b->initiative); |
457
|
1 |
|
}); |
458
|
1 |
|
foreach($characters as $character) { |
459
|
1 |
|
$action = $combat->actionSelector->chooseAction($combat, $character); |
460
|
|
|
switch($action) { |
461
|
1 |
|
case CombatAction::ACTION_ATTACK: |
462
|
1 |
|
$combat->onAttack($character, $combat->selectAttackTarget($character)); |
463
|
1 |
|
break; |
464
|
1 |
|
case CombatAction::ACTION_SKILL_ATTACK: |
465
|
|
|
/** @var CharacterAttackSkill $skill */ |
466
|
1 |
|
$skill = $character->usableSkills[0]; |
467
|
1 |
|
$combat->doAttackSkill($character, $skill); |
468
|
1 |
|
break; |
469
|
1 |
|
case CombatAction::ACTION_SKILL_SPECIAL: |
470
|
|
|
/** @var CharacterSpecialSkill $skill */ |
471
|
1 |
|
$skill = $character->usableSkills[0]; |
472
|
1 |
|
$combat->doSpecialSkill($character, $skill); |
473
|
1 |
|
break; |
474
|
1 |
|
case CombatAction::ACTION_HEALING: |
475
|
1 |
|
$combat->onHeal($character, $combat->selectHealingTarget($character)); |
476
|
1 |
|
break; |
477
|
|
|
case null: |
|
|
|
|
478
|
|
|
break; |
479
|
|
|
default: |
480
|
1 |
|
throw new NotImplementedException("Action $action is not implemented."); |
481
|
|
|
} |
482
|
|
|
} |
483
|
1 |
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Start next round |
487
|
|
|
* |
488
|
|
|
* @return int Winning team/0 |
489
|
|
|
*/ |
490
|
|
|
protected function startRound(): int { |
491
|
1 |
|
$this->onRoundStart($this); |
492
|
1 |
|
return $this->getWinner(); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Do a round |
497
|
|
|
*/ |
498
|
|
|
protected function doRound(): void { |
499
|
1 |
|
$this->onRound($this); |
500
|
1 |
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* End round |
504
|
|
|
* |
505
|
|
|
* @return int Winning team/0 |
506
|
|
|
*/ |
507
|
|
|
protected function endRound(): int { |
508
|
1 |
|
$this->onRoundEnd($this); |
509
|
1 |
|
return $this->getWinner(); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Executes the combat |
514
|
|
|
* |
515
|
|
|
* @return int Winning team |
516
|
|
|
*/ |
517
|
|
|
public function execute(): int { |
518
|
1 |
|
if(!isset($this->team1)) { |
519
|
1 |
|
throw new InvalidStateException("Teams are not set."); |
520
|
|
|
} |
521
|
1 |
|
$this->onCombatStart($this); |
522
|
1 |
|
while($this->round <= $this->roundLimit) { |
523
|
1 |
|
if($this->startRound() > 0) { |
524
|
1 |
|
break; |
525
|
|
|
} |
526
|
1 |
|
$this->doRound(); |
527
|
1 |
|
if($this->endRound() > 0) { |
528
|
|
|
break; |
529
|
|
|
} |
530
|
|
|
} |
531
|
1 |
|
$this->onCombatEnd($this); |
532
|
1 |
|
return $this->getWinner(); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* Do an attack |
537
|
|
|
* Hit chance = Attacker's hit - Defender's dodge, but at least 15% |
538
|
|
|
* Damage = Attacker's damage - defender's defense |
539
|
|
|
*/ |
540
|
|
|
public function attackHarm(Character $attacker, Character $defender): void { |
541
|
1 |
|
$result = []; |
542
|
1 |
|
$result["result"] = $this->successCalculator->hasHit($attacker, $defender); |
543
|
1 |
|
$result["amount"] = 0; |
544
|
1 |
|
if($result["result"]) { |
545
|
1 |
|
$amount = $attacker->damage - $defender->defense; |
546
|
1 |
|
$result["amount"] = Numbers::range($amount, 0, $defender->hitpoints); |
547
|
|
|
} |
548
|
1 |
|
if($result["amount"] > 0) { |
549
|
1 |
|
$defender->harm($result["amount"]); |
550
|
|
|
} |
551
|
1 |
|
$result["action"] = CombatAction::ACTION_ATTACK; |
552
|
1 |
|
$result["name"] = ""; |
553
|
1 |
|
$result["character1"] = $attacker; |
554
|
1 |
|
$result["character2"] = $defender; |
555
|
1 |
|
$this->logDamage($attacker, $result["amount"]); |
556
|
1 |
|
$this->log->log($result); |
557
|
1 |
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Use an attack skill |
561
|
|
|
*/ |
562
|
|
|
public function useAttackSkill(Character $attacker, Character $defender, CharacterAttackSkill $skill): void { |
563
|
1 |
|
$result = []; |
564
|
1 |
|
$result["result"] = $this->successCalculator->hasHit($attacker, $defender, $skill); |
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"] > 0) { |
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 |
|
$result["result"] = $this->successCalculator->hasHealed($healer); |
609
|
1 |
|
$amount = ($result["result"]) ? (int) ($healer->intelligence / 2) : 0; |
610
|
1 |
|
$result["amount"] = Numbers::range($amount, 0, $patient->maxHitpoints - $patient->hitpoints); |
611
|
1 |
|
if($result["amount"] > 0) { |
612
|
1 |
|
$patient->heal($result["amount"]); |
613
|
|
|
} |
614
|
1 |
|
$result["action"] = CombatAction::ACTION_HEALING; |
615
|
1 |
|
$result["name"] = ""; |
616
|
1 |
|
$result["character1"] = $healer; |
617
|
1 |
|
$result["character2"] = $patient; |
618
|
1 |
|
$this->log->log($result); |
619
|
1 |
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Harm poisoned characters at start of round |
623
|
|
|
*/ |
624
|
|
|
public function applyPoison(self $combat): void { |
625
|
|
|
/** @var Character[] $characters */ |
626
|
1 |
|
$characters = array_merge($combat->team1->aliveMembers, $combat->team2->aliveMembers); |
627
|
1 |
|
foreach($characters as $character) { |
628
|
1 |
|
foreach($character->effects as $effect) { |
629
|
1 |
|
if($effect->type === SkillSpecial::TYPE_POISON) { |
630
|
1 |
|
$character->harm($effect->value); |
631
|
|
|
$action = [ |
632
|
1 |
|
"action" => CombatAction::ACTION_POISON, "result" => true, "amount" => $effect->value, |
633
|
1 |
|
"character1" => $character, "character2" => $character, |
634
|
|
|
]; |
635
|
1 |
|
$combat->log->log($action); |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
} |
639
|
1 |
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Log dealt damage |
643
|
|
|
*/ |
644
|
|
|
public function logDamage(Character $attacker, int $amount): void { |
645
|
1 |
|
$team = $this->team1->hasItems(["id" => $attacker->id]) ? 1 : 2; |
646
|
1 |
|
$this->damage[$team] += $amount; |
647
|
1 |
|
} |
648
|
|
|
|
649
|
|
|
public function getLog(): CombatLogger { |
650
|
1 |
|
return $this->log; |
651
|
|
|
} |
652
|
|
|
} |
653
|
|
|
?> |