/
NpcController.php
636 lines (546 loc) · 21.7 KB
/
NpcController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
namespace NinjaWars\core\control;
use Pimple\Container;
use NinjaWars\core\control\AbstractController;
use NinjaWars\core\Filter;
use NinjaWars\core\data\Npc;
use NinjaWars\core\control\Combat;
use NinjaWars\core\data\NpcFactory;
use NinjaWars\core\data\Item;
use NinjaWars\core\data\Inventory;
use NinjaWars\core\data\Player;
use NinjaWars\core\data\Event;
use NinjaWars\core\extensions\SessionFactory;
use NinjaWars\core\extensions\StreamedViewResponse;
use NinjaWars\core\environment\RequestWrapper;
/**
* Handles displaying npcs and attacking specific npcs
*/
class NpcController extends AbstractController {
const ALIVE = true;
const PRIV = false;
const HIGH_TURNS = 50;
const ITEM_DECREASES_GOLD_DIVISOR = 1.11;
const ONI_DAMAGE_CAP = 20;
const RANDOM_ENCOUNTER_DIVISOR = 400;
const SAMURAI_REWARD_DMG = 100;
const ONI_TURN_LOSS = 10;
const ONI_KILL_LOSS = 1;
const MIN_LEVEL_FOR_BOUNTY = 5;
const MAX_LEVEL_FOR_BOUNTY = 50;
public static $STEALTH_REMOVING_NPCS = ['samurai', 'oni'];
private $randomness = null;
/**
* Inject different seed when non-randomness is needed (for testing)
*/
public function __construct($options=[]) {
if (isset($options['randomness']) && is_callable($options['randomness'])) {
$this->randomness = $options['randomness'];
} else {
$this->randomness = function() {
return mt_rand() / mt_getrandmax();
};
}
}
/**
* Run the random encounter
*
* @note
* Currently only random enc. is an Oni attack! Yay! They take turns and a
* kill and do a little damage.
*/
private function randomEncounter(Player $player) {
$oni_health_loss = rand(1, self::ONI_DAMAGE_CAP);
$multiple_rewards = false;
$oni_killed = false;
$item = null;
$player->changeTurns(-1*self::ONI_TURN_LOSS);
$player->harm($oni_health_loss);
$player->subtractKills(self::ONI_KILL_LOSS);
if ($player->health > 0) { // if you survive
$inventory = new Inventory($player);
if ($player->turns > self::HIGH_TURNS) { // And your turns are high/you are energetic, you can kill them.
$oni_killed = true;
$item = Item::findByIdentity('dimmak');
$quantity = 1;
$inventory->add($item->identity(), $quantity);
} else if ($player->turns > floor(self::HIGH_TURNS/2) && rand()&1) {
// If your turns are somewhat high/you have some energy, 50/50 chance you can kill them.
$oni_killed = true;
$item = Item::findByIdentity('ginsengroot');
$multiple_rewards = true;
$quantity = 4;
$inventory->add($item->identity(), $quantity);
}
}
$player->save();
return [
'npc.oni.tpl',
[
'victory' => $oni_killed,
'item' => $item,
'multiple_rewards' => $multiple_rewards,
],
];
}
/**
* Wrapper for session storage of thief attacking
*
* @param Container
* @return int
*/
private function getThiefCounter(Container $p_dependencies) {
return $p_dependencies['session']->get('thief_counter', 1);
}
/**
* Wrapper for session storage of thief attacking
*
* @param int $num
* @param Container
* @return void
*/
private function setThiefCounter($num, Container $p_dependencies) {
$p_dependencies['session']->set('thief_counter', $num);
}
/**
* The reward for defeating an npc, less if items popped
*
* @param Npc $npco
* @param boolean $reward_item Any items were rewarded.
* @return int
* @note
* If npc gold explicitly set to 0, reward gold will be totally skipped
* "rich" npcs will have a higher gold minimum
*/
private function calcReceivedGold(Npc $npco, $reward_item) {
if ($npco->gold() === 0) { // These npcs simply don't give gold.
return 0;
}
// Hack a little off max gold if items received.
$divisor = 1;
if ($reward_item) {
$divisor = self::ITEM_DECREASES_GOLD_DIVISOR;
}
return rand($npco->minGold(), floor($npco->gold()/$divisor));
}
/**
* Handle Standard Abstract Npcs
*
* @param String $victim
* @param Player $player
* @param Array $npcs
* @return array [$npc_template, $combat_data]
*/
private function attackAbstractNpc($victim, Player $player, $npcs) {
$npc_stats = $npcs[$victim]; // Pull an npcs individual stats with generic fallbacks.
$npco = new Npc($npc_stats); // Construct the npc object.
$display_name = (isset($npc_stats['name']) ? $npc_stats['name'] : ucfirst($victim));
$status_effect = (isset($npc_stats['status']) ? $npc_stats['status'] : null);
$reward_item = (isset($npc_stats['item']) && $npc_stats['item'] ? $npc_stats['item'] : null);
$is_quick = (boolean) ($npco->getSpeed() > $player->getSpeed()); // Beyond basic speed and they see you coming, so show that message.
$is_weaker = ($npco->getStrength() * 3) < $player->getStrength(); // Npc much weaker?
$is_stronger = ($npco->getStrength()) > ($player->getStrength() * 3); // Npc More than twice as strong?
$image = (isset($npc_stats['img']) ? $npc_stats['img'] : null);
// Assume defeat...
$victory = false;
$received_gold = null;
$received_items = null;
$added_bounty = '';
$is_rewarded = null; // Gets items or gold.
$statuses = null;
$status_classes = null;
$image_path = null;
// If the image exists, set the path to it for use on the page.
if ($image && file_exists(SERVER_ROOT.'www/images/characters/'.$image)) {
$image_path = IMAGE_ROOT.'characters/'.$image;
}
// ******* FIGHT Logic ***********
$npc_damage = $npco->damage();
$survive_fight = $player->harm($npc_damage);
$kill_npc = ($npco->getHealth() < $player->damage());
if ($survive_fight > 0) {
// The ninja survived, they get any gold the npc has.
$received_gold = $this->calcReceivedGold($npco, (bool) $reward_item);
$player->setGold($player->gold + $received_gold);
$received_items = array();
if ($kill_npc) {
$victory = true;
// Victory occurred, reward the poor sap.
if ($npco->inventory()) {
$inventory = new Inventory($player);
foreach (array_keys($npco->inventory()) as $l_item) {
$item = Item::findByIdentity($l_item);
$received_items[] = $item->getName();
$inventory->add($item->identity(), 1);
}
}
// Add bounty where applicable for npcs.
if ($npco->bountyMod() > 0 &&
$player->level > self::MIN_LEVEL_FOR_BOUNTY &&
$player->level <= self::MAX_LEVEL_FOR_BOUNTY
) {
$added_bounty = Combat::runBountyExchange($player, $npco, $npco->bountyMod());
}
}
$is_rewarded = (bool) $received_gold || (bool)count($received_items);
if (isset($npc_stats['status']) && null !== $npc_stats['status']) {
$player->addStatus($npc_stats['status']);
// Get the statuses and status classes for display.
$statuses = implode(', ', Player::getStatusList());
$status_classes = implode(' ', Player::getStatusList());
}
}
$player->save();
return [
'npc.abstract.tpl',
[
'victim' => $victim,
'display_name' => $display_name,
'attack_damage' => $npc_damage,
'status_effect' => $status_effect,
'display_statuses' => $statuses,
'display_statuses_classes' => $status_classes,
'received_gold' => $received_gold,
'received_display_items' => $received_items,
'is_rewarded' => $is_rewarded,
'victory' => $victory,
'survive_fight' => $survive_fight,
'kill_npc' => $kill_npc,
'image_path' => $image_path,
'npc_stats' => $npc_stats,
'is_quick' => $is_quick,
'added_bounty' => $added_bounty,
'is_villager' => $npco->hasTrait('villager'),
'race' => $npco->race(),
'is_weaker' => $is_weaker,
'is_stronger' => $is_stronger,
]
];
}
/**
* Injectable randomness.
*
* @return boolean
* @note
* Used to be rand(1, 400) === 1
*/
private function startRandomEncounter() {
$randomness = $this->randomness;
return (boolean) (ceil($randomness() * self::RANDOM_ENCOUNTER_DIVISOR) == self::RANDOM_ENCOUNTER_DIVISOR);
}
/**
* Attack a specific npc
*
* @param Container
* @return StreamedViewResponse
*/
public function attack(Container $p_dependencies) {
$request = RequestWrapper::$request;
$url_part = $request->getRequestUri();
if (preg_match('#\/(\w+)(\/)?$#', $url_part, $matches)) {
$victim = $matches[1];
} else {
$victim = null; // No match, victim is null.
}
$turn_cost = 1;
$health = true;
$combat_data = [];
$player = $p_dependencies['current_player'];
$error_template = 'npc.no-one.tpl'; // Error template also used down below.
$npc_template = $error_template; // Error condition by default.
$npcs = NpcFactory::npcsData();
$possible_npcs = array_merge(array_column(NpcFactory::customNpcs(), 'identity'), array_keys($npcs));
$victim = (in_array($victim, $possible_npcs) ? $victim : null); // Filter to only the correct options.
$standard_npcs = [
'peasant' => 'attackVillager',
'merchant' => 'attackMerchant',
'guard' => 'attackGuard',
];
$method = null;
if ($player && $player->turns > 0 && !empty($victim)) {
// Strip stealth when attacking special NPCs
if ($player->hasStatus('stealth') && in_array(strtolower($victim), self::$STEALTH_REMOVING_NPCS)) {
$player->subtractStatus(STEALTH);
}
if ($this->startRandomEncounter()) {
$method = 'randomEncounter';
} elseif (array_key_exists($victim, $npcs)) {
list($npc_template, $combat_data) = $this->attackAbstractNpc($victim, $player, $npcs);
} else if (array_key_exists($victim, $standard_npcs)) {
$method = $standard_npcs[$victim];
} else if ($victim == "samurai") {
if ($player->level < 2) {
$turn_cost = 0;
$npc_template = 'npc.samurai-too-weak.tpl';
} else if ($player->kills < 1) {
$turn_cost = 0;
$npc_template = 'npc.samurai-too-tired.tpl';
} else {
$method = 'attackSamurai';
}
} else if ($victim == 'thief') {
// Check the counter to see whether they've attacked a thief multiple times in a row.
$counter = $this->getThiefCounter($p_dependencies);
$this->setThiefCounter($counter+1, $p_dependencies); // Incremement the current state of the counter.
if ($counter > 20 && rand(1, 3) == 3) {
// Only after many attacks do you have the chance to be attacked back by the group of thieves.
$this->setThiefCounter(0, $p_dependencies); // Reset the counter to zero.
$method = 'attackGroupOfThieves';
} else {
$method = 'attackNormalThief';
}
}
if (is_callable([$this, $method], false)) {
list($npc_template, $combat_data) = $this->$method($player);
}
if ($player->health <= 0) { // FINAL CHECK FOR DEATH
$player->death();
$health = false;
Event::create((int)"SysMsg", $player->id(), "DEATH: You have been killed by a $victim.");
}
// Subtract the turn cost for attacking an npc
// almost always 1 apart from perhaps oni or group-of-thieves
$player->changeTurns(-1*$turn_cost);
$player->save();
}
// Uses a sub-template inside for specific npcs.
$parts = [
'victim' => $victim, // merge may override in theory
'npc_template' => $npc_template,
'attacked' => 1,
'turns' => $player? $player->turns : null,
'health' => $health,
];
return new StreamedViewResponse('Battle', 'npc.tpl', $parts + $combat_data, ['quickstat' => 'player']);
}
private function attackGuard(Player $player) {
$damage = rand(1, $player->getStrength() + 10);
$herb = false;
$gold = 0;
$bounty = 0;
if ($victory = $player->harm($damage)) {
$gold = rand(1, $player->getStrength() + 40);
$player->setGold($player->gold + $gold);
if ($player->level > 15) {
$bounty = 10 * floor(($player->level - 10) / 5);
$player->setBounty($player->bounty + $bounty);
}
// chance of getting an herb for Kampo
if (rand(1, 9) == 9) {
$herb = true;
$inventory = new Inventory($player);
$inventory->add('ginsengroot', 1);
}
} else {
$damage = 0;
}
return [
'npc.guard.tpl',
[
'attack' => $damage,
'gold' => $gold,
'bounty' => $bounty,
'victory' => $victory,
'herb' => $herb,
],
];
}
private function attackVillager(Player $player) {
$damage = rand(0, 10);
$just_villager = rand(0, 20);
$bounty = 0;
$gold = 0;
if ($victory = $player->harm($damage)) {
$gold = rand(0, 20);
$player->setGold($player->gold + $gold);
// *** Bounty or no bounty ***
if ($player->level > 1 && $player->level <= 20) {
$bounty = floor($player->level / 3);
$player->setBounty($player->bounty + $bounty);
}
if (!$just_villager) {
// Something beyond just a villager, drop a shuriken
$inventory = new Inventory($player);
$inventory->add('shuriken', 1);
}
}
$player->save();
return [
'npc.peasant.tpl',
[
'just_villager' => $just_villager,
'attack' => $damage,
'gold' => $gold,
'level' => $player->level,
'bounty' => $bounty,
'victory' => $victory,
],
];
}
private function attackSamurai(Player $player) {
$gold = 0;
$victory = false;
$drop = false;
$drop_display = null;
$damage = [
rand(1, $player->getStrength()),
rand(10, 10 + round($player->getStrength() * 1.2)),
];
if (rand(0, 1)) {
$damage[] = rand(30 + round($player->getStrength() * 0.2), 30 + round($player->getStrength() * 1.7));
} else { //Instant death.
$damage[] = abs($player->health - $damage[0] - $damage[1]);
}
for ($i = 0; $i < count($damage) && $player->health > 0; ++$i) {
$player->harm($damage[$i]);
}
if ($player->health > 0) { // Ninja still has health after all attacks
$victory = true;
$gold = rand(50, 50 + $damage[2] + $damage[1]);
$player->addKills(1);
$player->setGold($player->gold + $gold);
$inventory = new Inventory($player);
// If samurai dmg high, but ninja lived, give rewards
if ($damage[2] > self::SAMURAI_REWARD_DMG) {
$drop = true;
if (rand(0, 1)) {
$drop_display = 'mushroom powder';
$dropItem = 'amanita';
} else {
$drop_display = 'a strange herb';
$dropItem = 'ginsengroot';
}
$inventory->add($dropItem, 1);
}
// If the final damage was the exact max damage
if ($damage[2] == $player->getStrength() * 3) {
$drop = true;
$drop_display = 'a black scroll';
$inventory->add('dimmak', 1);
}
}
$player->save();
return [
'npc.samurai.tpl',
[
'samurai_damage_array' => $damage,
'gold' => $gold,
'victory' => $victory,
'ninja_str' => $player->getStrength(),
'level' => $player->level,
'attacker_kills' => $player->kills,
'drop' => $drop,
'drop_display' => $drop_display,
],
];
}
private function attackGroupOfThieves(Player $player) {
$damage = rand(50, 150);
if ($victory = $player->harm($damage)) {
// The den of thieves didn't accomplish their goal
$gold = rand(100, 300);
if ($damage > 120) { // Powerful attack gives an additional disadvantage
$player->subtractKills(1);
}
$player->setGold($player->gold + $gold);
$inventory = new Inventory($player);
$inventory->add('phosphor', 1);
} else { // If the den of theives killed the attacker.
$gold = 0;
}
$player->save();
return [
'npc.thief-group.tpl',
[
'attack' => $damage,
'gold' => $gold,
'victory' => $victory,
],
];
}
/**
* Attack merchant
*/
private function attackMerchant($player) {
$damage = rand(15, 35);
$bounty = 0;
// Player killed NPC
if ($victory = $player->harm($damage)) {
$gold = rand(20, 70);
$player->setGold($player->gold + $gold);
if ($damage > 34) {
$inventory = new Inventory($player);
$inventory->add('phosphor', 1);
}
if ($player->level > 10) {
$bounty = 5 * floor(($player->level - 5) / 3);
$player->setBounty($player->bounty + $bounty);
}
} else { // NPC killed player
$damage = $gold = 0;
}
$player->save();
return [
'npc.merchant.tpl',
[
'attack' => $damage,
'gold' => $gold,
'bounty' => $bounty,
'victory' => $victory,
],
];
}
/**
* Normal attack on a single thief.
*/
private function attackNormalThief(Player $player) {
$damage = rand(0, 35); // Damage done
$gold = 0;
if ($victory = $player->harm($damage)) {
$gold = rand(0, 40); // Gold in question
if ($damage > 30) { // Steal gold
$player->setGold(max(0, $player->gold - $gold));
} else if ($damage < 30) { // award gold and item
$player->setGold($player->gold + $gold);
$inventory = new Inventory($player);
$inventory->add('shuriken', 1);
}
}
$player->save();
return [
'npc.thief.tpl',
[
'attack' => $damage,
'gold' => $gold,
'victory' => $victory,
],
];
}
/**
* Obtain the npcs data.
*
* @return Array
*/
private function npcs() {
return [
'abstract_npcs' => NpcFactory::npcsData(),
'custom_npcs' => NpcFactory::customNpcs(),
];
}
/**
* Get the list of npcs in a subtemplate.
*
* @param Container
* @return Response
*/
public function index(Container $p_dependencies) {
$all_npcs = $this->npcs();
$other_npcs = $all_npcs['abstract_npcs'];
$npcs = $all_npcs['custom_npcs'];
$template = 'npc.list.tpl';
$title = 'Npcs';
$parts = ['npcs' => $npcs, 'other_npcs' => $other_npcs];
$options = ['quickstats' => 'player'];
return new StreamedViewResponse($title, $template, $parts, $options);
}
}