1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace HeroesofAbenez\Combat; |
5
|
|
|
|
6
|
|
|
use Nexendrie\Utils\Numbers; |
7
|
|
|
use HeroesofAbenez\Combat\CombatActions\ICombatAction; |
8
|
|
|
use Nexendrie\Utils\Collection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Handles combat |
12
|
|
|
* |
13
|
|
|
* @author Jakub Konečný |
14
|
|
|
* @property-read CombatLogger $log Log from the combat |
15
|
|
|
* @property-read int $winner Team which won the combat/0 if there is no winner yet |
16
|
|
|
* @property-read int $round Number of current round |
17
|
|
|
* @property int $roundLimit |
18
|
|
|
* @property Team $team1 |
19
|
|
|
* @property Team $team2 |
20
|
|
|
* @property-read int $team1Damage |
21
|
|
|
* @property-read int $team2Damage |
22
|
|
|
* @property ISuccessCalculator $successCalculator |
23
|
|
|
* @property ICombatActionSelector $actionSelector |
24
|
|
|
* @property Collection|ICombatAction[] $combatActions |
25
|
|
|
* @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) |
|
|
|
|
26
|
|
|
* @property callable $healers To determine characters that are supposed to heal their team. Gets team1 and team2 as parameters, should return Team |
|
|
|
|
27
|
|
|
* @method void onCombatStart(CombatBase $combat) |
28
|
|
|
* @method void onCombatEnd(CombatBase $combat) |
29
|
|
|
* @method void onRoundStart(CombatBase $combat) |
30
|
|
|
* @method void onRound(CombatBase $combat) |
31
|
|
|
* @method void onRoundEnd(CombatBase $combat) |
32
|
|
|
*/ |
33
|
1 |
|
class CombatBase { |
34
|
1 |
|
use \Nette\SmartObject; |
35
|
|
|
|
36
|
|
|
/** @var Team First team */ |
37
|
|
|
protected $team1; |
38
|
|
|
/** @var Team Second team */ |
39
|
|
|
protected $team2; |
40
|
|
|
/** @var CombatLogger */ |
41
|
|
|
protected $log; |
42
|
|
|
/** @var int Number of current round */ |
43
|
|
|
protected $round = 0; |
44
|
|
|
/** @var int Round limit */ |
45
|
|
|
protected $roundLimit = 30; |
46
|
|
|
/** @var array Dealt damage by team */ |
47
|
|
|
protected $damage = [1 => 0, 2 => 0]; |
48
|
|
|
/** @var callable[] */ |
49
|
|
|
public $onCombatStart = []; |
50
|
|
|
/** @var callable[] */ |
51
|
|
|
public $onCombatEnd = []; |
52
|
|
|
/** @var callable[] */ |
53
|
|
|
public $onRoundStart = []; |
54
|
|
|
/** @var callable[] */ |
55
|
|
|
public $onRound = []; |
56
|
|
|
/** @var callable[] */ |
57
|
|
|
public $onRoundEnd = []; |
58
|
|
|
/** @var callable */ |
59
|
|
|
protected $victoryCondition; |
60
|
|
|
/** @var callable */ |
61
|
|
|
protected $healers; |
62
|
|
|
/** @var ISuccessCalculator */ |
63
|
|
|
protected $successCalculator; |
64
|
|
|
/** @var ICombatActionSelector */ |
65
|
|
|
protected $actionSelector; |
66
|
|
|
/** @var Collection|ICombatAction[] */ |
67
|
|
|
protected $combatActions; |
68
|
|
|
|
69
|
|
|
public function __construct(CombatLogger $logger, ?ISuccessCalculator $successCalculator = null, ?ICombatActionSelector $actionSelector = null) { |
|
|
|
|
70
|
1 |
|
$this->log = $logger; |
71
|
1 |
|
$this->onCombatStart[] = [$this, "assignPositions"]; |
72
|
1 |
|
$this->onCombatEnd[] = [$this, "removeCombatEffects"]; |
73
|
1 |
|
$this->onCombatEnd[] = [$this, "logCombatResult"]; |
74
|
1 |
|
$this->onCombatEnd[] = [$this, "resetInitiative"]; |
75
|
1 |
|
$this->onRoundStart[] = [$this, "applyEffectProviders"]; |
76
|
1 |
|
$this->onRoundStart[] = [$this, "decreaseEffectsDuration"]; |
77
|
1 |
|
$this->onRoundStart[] = [$this ,"recalculateStats"]; |
78
|
1 |
|
$this->onRoundStart[] = [$this, "logRoundNumber"]; |
79
|
1 |
|
$this->onRoundStart[] = [$this, "applyPoison"]; |
80
|
1 |
|
$this->onRound[] = [$this, "mainStage"]; |
81
|
1 |
|
$this->onRoundEnd[] = [$this, "decreaseSkillsCooldowns"]; |
82
|
1 |
|
$this->onRoundEnd[] = [$this, "resetInitiative"]; |
83
|
1 |
|
$this->victoryCondition = [VictoryConditions::class, "moreDamage"]; |
84
|
1 |
|
$this->successCalculator = $successCalculator ?? new RandomSuccessCalculator(); |
85
|
1 |
|
$this->actionSelector = $actionSelector ?? new CombatActionSelector(); |
86
|
1 |
|
$this->healers = function(): Team { |
87
|
|
|
return new Team("healers"); |
88
|
|
|
}; |
89
|
1 |
|
$this->combatActions = new class extends Collection { |
90
|
|
|
/** @var string */ |
91
|
|
|
protected $class = ICombatAction::class; |
92
|
|
|
}; |
93
|
1 |
|
$this->combatActions[] = new CombatActions\Attack(); |
94
|
1 |
|
$this->combatActions[] = new CombatActions\Heal(); |
95
|
1 |
|
$this->combatActions[] = new CombatActions\SkillAttack(); |
96
|
1 |
|
$this->combatActions[] = new CombatActions\SkillSpecial(); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
public function getRound(): int { |
100
|
1 |
|
return $this->round; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getRoundLimit(): int { |
104
|
1 |
|
return $this->roundLimit; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function setRoundLimit(int $roundLimit): void { |
108
|
|
|
$this->roundLimit = Numbers::range($roundLimit, 1, PHP_INT_MAX); |
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
|
1 |
|
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
|
|
|
* @return Collection|ICombatAction[] |
185
|
|
|
*/ |
186
|
|
|
public function getCombatActions(): Collection { |
187
|
1 |
|
return $this->combatActions; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get winner of combat |
192
|
|
|
* |
193
|
|
|
* @staticvar int $result |
194
|
|
|
* @return int Winning team/0 |
195
|
|
|
*/ |
196
|
|
|
public function getWinner(): int { |
197
|
1 |
|
static $result = 0; |
198
|
1 |
|
if($result === 0) { |
199
|
1 |
|
$result = call_user_func($this->victoryCondition, $this); |
200
|
1 |
|
$result = Numbers::range($result, 0, 2); |
201
|
|
|
} |
202
|
1 |
|
return $result; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @internal |
207
|
|
|
*/ |
208
|
|
|
public function getTeam(Character $character): Team { |
209
|
1 |
|
return $this->team1->hasItems(["id" => $character->id]) ? $this->team1 : $this->team2; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @internal |
214
|
|
|
*/ |
215
|
|
|
public function getEnemyTeam(Character $character): Team { |
216
|
1 |
|
return $this->team1->hasItems(["id" => $character->id]) ? $this->team2 : $this->team1; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function applyEffectProviders(self $combat): void { |
220
|
|
|
/** @var Character[] $characters */ |
221
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
222
|
1 |
|
foreach($characters as $character) { |
223
|
1 |
|
$character->applyEffectProviders(); |
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 |
|
$character->effects->removeByFilter(["duration!=" => CharacterEffect::DURATION_FOREVER]); |
290
|
|
|
} |
291
|
1 |
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Add winner to the log |
295
|
|
|
*/ |
296
|
|
|
public function logCombatResult(self $combat): void { |
297
|
1 |
|
$combat->log->round = 5000; |
298
|
|
|
$params = [ |
299
|
1 |
|
"team1name" => $combat->team1->name, "team1damage" => $combat->damage[1], |
300
|
1 |
|
"team2name" => $combat->team2->name, "team2damage" => $combat->damage[2], |
301
|
|
|
]; |
302
|
1 |
|
if($combat->winner === 1) { |
303
|
1 |
|
$params["winner"] = $combat->team1->name; |
304
|
|
|
} else { |
305
|
1 |
|
$params["winner"] = $combat->team2->name; |
306
|
|
|
} |
307
|
1 |
|
$combat->log->logText("combat.log.combatEnd", $params); |
308
|
1 |
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Log start of a round |
312
|
|
|
*/ |
313
|
|
|
public function logRoundNumber(self $combat): void { |
314
|
1 |
|
$combat->log->round = ++$this->round; |
315
|
1 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Decrease duration of effects and recalculate stats |
319
|
|
|
*/ |
320
|
|
|
public function recalculateStats(self $combat): void { |
321
|
|
|
/** @var Character[] $characters */ |
322
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
323
|
1 |
|
foreach($characters as $character) { |
324
|
1 |
|
$character->recalculateStats(); |
325
|
|
|
} |
326
|
1 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Reset characters' initiative |
330
|
|
|
*/ |
331
|
|
|
public function resetInitiative(self $combat): void { |
332
|
|
|
/** @var Character[] $characters */ |
333
|
1 |
|
$characters = array_merge($combat->team1->toArray(), $combat->team2->toArray()); |
334
|
1 |
|
foreach($characters as $character) { |
335
|
1 |
|
$character->resetInitiative(); |
336
|
|
|
} |
337
|
1 |
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Select target for attack |
341
|
|
|
* |
342
|
|
|
* @internal |
343
|
|
|
*/ |
344
|
|
|
public function selectAttackTarget(Character $attacker): ?Character { |
345
|
1 |
|
$enemyTeam = $this->getEnemyTeam($attacker); |
346
|
1 |
|
$rangedWeapon = ($attacker->equipment->hasItems(["%class%" => Weapon::class, "worn" => true, "ranged" => true,])); |
347
|
1 |
|
if(!$rangedWeapon) { |
348
|
1 |
|
$rowToAttack = $enemyTeam->rowToAttack; |
349
|
1 |
|
if(is_null($rowToAttack)) { |
350
|
|
|
return null; |
351
|
|
|
} |
352
|
|
|
/** @var Team $enemies */ |
353
|
1 |
|
$enemies = Team::fromArray($enemyTeam->getItems(["positionRow" => $rowToAttack, "hitpoints>" => 0,]), $enemyTeam->name); |
|
|
|
|
354
|
|
|
} else { |
355
|
|
|
$enemies = $enemyTeam; |
356
|
|
|
} |
357
|
1 |
|
$target = $enemies->getLowestHpCharacter(); |
358
|
1 |
|
if(!is_null($target)) { |
359
|
1 |
|
return $target; |
360
|
|
|
} |
361
|
1 |
|
return $enemies->getRandomCharacter(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Main stage of a round |
366
|
|
|
* |
367
|
|
|
* @throws NotImplementedException |
368
|
|
|
*/ |
369
|
|
|
public function mainStage(self $combat): void { |
370
|
|
|
/** @var Character[] $characters */ |
371
|
1 |
|
$characters = array_merge($combat->team1->usableMembers, $combat->team2->usableMembers); |
372
|
1 |
|
usort($characters, function(Character $a, Character $b) { |
373
|
1 |
|
return -1 * strcmp((string) $a->initiative, (string) $b->initiative); |
374
|
1 |
|
}); |
375
|
1 |
|
foreach($characters as $character) { |
376
|
1 |
|
$action = $combat->actionSelector->chooseAction($combat, $character); |
377
|
1 |
|
if(is_null($action)) { |
378
|
|
|
break; |
379
|
|
|
} |
380
|
|
|
/** @var ICombatAction $combatAction */ |
381
|
1 |
|
foreach($this->combatActions as $combatAction) { |
382
|
1 |
|
if($combatAction->getName() === $action) { |
383
|
1 |
|
$combatAction->do($combat, $character); |
384
|
1 |
|
continue 2; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
throw new NotImplementedException("Action $action is not implemented."); |
388
|
|
|
} |
389
|
1 |
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Executes the combat |
393
|
|
|
* |
394
|
|
|
* @return int Winning team |
395
|
|
|
*/ |
396
|
|
|
public function execute(): int { |
397
|
1 |
|
if(!isset($this->team1)) { |
398
|
1 |
|
throw new InvalidStateException("Teams are not set."); |
399
|
|
|
} |
400
|
1 |
|
$this->onCombatStart($this); |
401
|
1 |
|
while($this->round <= $this->roundLimit) { |
402
|
1 |
|
$this->onRoundStart($this); |
403
|
1 |
|
if($this->getWinner() > 0) { |
404
|
1 |
|
break; |
405
|
|
|
} |
406
|
1 |
|
$this->onRound($this); |
407
|
1 |
|
$this->onRoundEnd($this); |
408
|
1 |
|
if($this->getWinner() > 0) { |
409
|
|
|
break; |
410
|
|
|
} |
411
|
|
|
} |
412
|
1 |
|
$this->onCombatEnd($this); |
413
|
1 |
|
return $this->getWinner(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Harm poisoned characters at start of round |
418
|
|
|
*/ |
419
|
|
|
public function applyPoison(self $combat): void { |
420
|
|
|
/** @var Character[] $characters */ |
421
|
1 |
|
$characters = array_merge($combat->team1->aliveMembers, $combat->team2->aliveMembers); |
422
|
1 |
|
foreach($characters as $character) { |
423
|
1 |
|
foreach($character->effects as $effect) { |
424
|
1 |
|
if($effect->type === SkillSpecial::TYPE_POISON) { |
425
|
1 |
|
$character->harm($effect->value); |
426
|
|
|
$action = [ |
427
|
1 |
|
"action" => CombatLogEntry::ACTION_POISON, "result" => true, "amount" => $effect->value, |
428
|
1 |
|
"character1" => $character, "character2" => $character, |
429
|
|
|
]; |
430
|
1 |
|
$combat->log->log($action); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
} |
434
|
1 |
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Log dealt damage |
438
|
|
|
*/ |
439
|
|
|
public function logDamage(Character $attacker, int $amount): void { |
440
|
1 |
|
$team = $this->team1->hasItems(["id" => $attacker->id]) ? 1 : 2; |
441
|
1 |
|
$this->damage[$team] += $amount; |
442
|
1 |
|
} |
443
|
|
|
|
444
|
|
|
public function getLog(): CombatLogger { |
445
|
1 |
|
return $this->log; |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
?> |