-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_definitions.php
643 lines (451 loc) · 13.4 KB
/
class_definitions.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
637
638
639
640
641
642
643
<?php
include_once("name_generator.php");
include_once("procedural_generator.php");
include_once("item_list.php");
class StateManager {
static function ChangeState(&$charData, $newState) {
// TODO: Validate state?
$charData->previousState = $charData->state;
$charData->state = $newState;
}
}
class CharacterSaveData {
public $name = null; // str
public $class = null; // str
public $xp = 0;
public $level = 0; // int
public $hp = 0; // int
public $hpMax = 0; // int
public $mp = 0; // int
public $mpMax = 0; // int
public $weapon = null; // str
public $weaponVal = 0; // int
public $precision = 0;
public $endurance = 0;
public $reflexes = 0;
public $strength = 0;
public $oddness = 0;
public $nerve = 0;
public $acuity = 0;
// Used for Barbarians
public $weapon2 = null; // str
public $weapon2Val = 0;
public $armour = null; // str
public $armourVal = 0; // int
public $gold = 0; // int
public $inventory = null;
public $state = GameStates::Dynasty;
public $previousState = null;
// Used when patching in new features in order to not lose last state.
public $patchState = null;
public $patchPrevState = null;
// Used for procedural generation.
public $randomSeed = 0; // int
public $restStart = 0;
public $restEnd = 0;
public $lastInputD = 0; // int (timestamp)
public $spellbook = null;
public $nick = null;
public $kills = 0;
public $rageTurns = 0;
public $tentUseCount = 0;
// Abilities are locked after use in combat (and unlocked on leaving combat)
public $lockedAbilities = null;
public $manaCostReductions = null;
public $streak = null;
public function lazyGetStreak() {
// Check for broken streak data.
$isBorked = is_numeric($this->streak);
if ( is_null($this->streak) || $isBorked ) {
$this->streak = new Streak();
}
return $this->streak;
}
}
class MapSaveData {
public $playerX = 0; // int
public $playerY = 0; // int
// Used for retreating.
public $lastPlayerX = 0; // int
public $lastPlayerY = 0; // int
public $map = null; // Map
}
class DynastySaveData {
public $name = "";
public $gold = 0;
public $level = 0;
public $precision = 0;
public $endurance = 0;
public $reflexes = 0;
public $strength = 0;
public $oddness = 0;
public $nerve = 0;
public $acuity = 0;
}
// Function Matches is called on each InputFragment, and the callback is called if it does match the input.
class InputFragment {
public $token; // str
public $uid; // str
public $displayString; // str
public function Matches($input) {
$tokenMatch = strcasecmp($input, $this->token) == 0;
$uidMatch = strcasecmp($input, $this->uid) == 0;
return $tokenMatch || $uidMatch;
}
public function FireCallback($charData, $mapData, $dynData) {
call_user_func($this->callback, $charData, $mapData, $dynData);
}
// ctor
function __construct($inToken, $inCallback) {
$this->token = $inToken;
$this->callback = $inCallback;
}
private $callback;
}
class Monster {
function __construct($playerLevel, $distance) {
// Randomly generate monster level based on player level and distance to the origin.
$this->InitLevel($playerLevel, $distance);
$this->InitStats($playerLevel);
$this->name = NameGenerator::Monster($playerLevel);
if ( $this->elite ) {
$this->name = "ELITE " . $this->name;
}
}
public $level; // int
public $name; // str
public $hp; // int
public $hpMax; // int
public $elite; // bool
public $attack = 0; // 0, unless elite.
private function InitLevel($playerLevel, $distance) {
$chanceInHundred = rand(1, 100);
// e.g. 0 at 0 distance, 5 at max distance
$mapHalfSize = floor(ProcGen::GetMapSize() / 2); // 50
$boundary = floor($mapHalfSize / 5); // 10
$distanceFactor = floor($distance / $boundary); //
// Level +3 - 5%
if ( $chanceInHundred > 95 ) {
$this->level = $playerLevel + 3 + $distanceFactor;
}
// Level +2 - 10%
else if ( $chanceInHundred > 85 ) {
$this->level = $playerLevel + 2 + $distanceFactor;
}
// Level +1 - 20%
else if ( $chanceInHundred > 70 ) {
$this->level = $playerLevel + 1 + $distanceFactor;
}
// Level == - 50%
else if ( $chanceInHundred > 20 ) {
$this->level = $playerLevel + $distanceFactor;
}
// Level -1 - 15%
else {
$this->level = $playerLevel - 1 + $distanceFactor;
// Don't want level 0 monsters :P
$this->level = max($this->level, 1);
}
}
private function InitStats($playerLevel) {
// Firstly, determine if it's elite.
$chanceInHundred = rand(1, 100);
$this->elite = $chanceInHundred > 90;
// Elite monsters simply hit much harder.
if ( $this->elite ) {
$this->attack = $this->level;
}
// Monsters scale in a very slightly non-linear way.
$this->hpMax = floor(pow($this->level, 1.4) * 10);
$this->hp = $this->hpMax;
}
public function getConnedNameStr($playerLevel) {
$colours = ["\x039", "\x030", "\x038", "\x038", "\x034", "\x0313"];
// Will be between -1 and ~8
$levelDiff = $this->level - $playerLevel;
// We want level== and level+1 to look identical
if ( $levelDiff == 1 ) {
$levelDiff = 0;
}
$maxIndex = count($colours) - 1;
$diffIndex = max(0, min($levelDiff + 1, $maxIndex));
$conColour = $colours[$diffIndex];
return ($conColour . "$this->name\x03\x03");
}
}
class Map {
public $grid; // 2D array of Rooms
function __construct() {
$grid = array();
}
public function GetRoom($x, $y) {
$room = null;
if ( isset($this->grid[$x][$y]) ) {
$room = $this->grid[$x][$y];
}
return $room;
}
}
class ShopEquipment {
public $name;
public $level;
public $gpCost;
public $type; // ShopEquipment::Armour or ShopEquipment::Weapon
function __construct($name, $level, $type) {
$this->name = $name;
$this->level = $level;
// This complicated formula was retrofitted from excel.
// ROUND(19*EXP(0.15*B21), -1)
$this->gpCost = round(19 * exp(0.15 * $level), -1);
$this->type = $type;
}
public function getCost() {
return $this->gpCost;
}
const Armour = 0;
const Weapon = 1;
}
class Room {
public $x = 0; // int
public $y = 0; // int
public $occupant = null; // Monster
function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
}
class Shop {
public $stock = null;
public $equipment = null;
public function isItemOrEquipment($itemName) {
$isItem = isset($this->stock[$itemName]);
$isEquipment = isset($this->equipment[$itemName]);
return $isItem || $isEquipment;
}
public function getStockDetailsForItem($itemName, $charData) {
$output = [];
if ( isset($this->stock[$itemName]) ) {
$output[] = $this->stock[$itemName];
$item = findItem($itemName);
$output[] = $item->getCost($charData);
$output[] = null;
}
else if ( isset($this->equipment[$itemName]) ) {
$output[] = 1;
$shopEquip = $this->equipment[$itemName];
$output[] = $shopEquip->getCost($charData);
$output[] = $shopEquip->level;
}
return $output;
}
private function addStockItem($item) {
$itemName = $item->name;
if ( !isset($this->stock[$itemName]) ) {
$this->stock[$itemName] = 0;
}
$this->stock[$itemName]++;
}
public function removeStockItem($itemName) {
$this->stock[$itemName]--;
if ( $this->stock[$itemName] <= 0 ) {
// Remove the item from our stock completely if we run out.
unset($this->stock[$itemName]);
}
}
public function addEquipment($itemName, $itemLevel, $type) {
$this->equipment[$itemName] = new ShopEquipment($itemName, $itemLevel, $type);
}
public function removeEquipment($itemName) {
unset($this->equipment[$itemName]);
}
private function InitRandomEquipment($playerLevel, $distance) {
// e.g. 0 at 0 distance, 10 at max distance
$mapHalfSize = floor(ProcGen::GetMapSize() / 2); // 50
$boundary = floor($mapHalfSize / 10); // 10
$distanceFactor = floor($distance / $boundary);
// NB: Can spawn both armour AND weapon.
// Weapon
$oneInHundred = rand(1, 100);
if ( true || $oneInHundred > 50 ) {
$weaponName = NameGenerator::Weapon($playerLevel);
$randomFactor = rand(-2, 2);
$weaponLvl = rand($playerLevel, $playerLevel + $distanceFactor + $randomFactor);
$weaponLvl = max(1, $weaponLvl);
$this->addEquipment($weaponName, $weaponLvl, ShopEquipment::Weapon);
}
// Armour
$oneInHundred = rand(1, 100);
if ( $oneInHundred > 50 ) {
$armourName = NameGenerator::Armour($playerLevel);
$randomFactor = rand(-2, 2);
$armourLvl = rand($playerLevel, $playerLevel + $distanceFactor + $randomFactor);
$armourLvl = max(1, $armourLvl);
$this->addEquipment($armourName, $armourLvl, ShopEquipment::Armour);
}
}
private function getPotionPrefix($playerLevel) {
$prefixes = ["minor ", "lesser ", "", "greater ", "major ", "super ", "giant "];
$prefixIndex = floor($playerLevel / 10);
$remainder = $playerLevel % 10;
$oneInTen = rand(1, 10);
// This is a bit confusing:
// If we're at level 14, remainder will be 4, and prefixIndex 1
// If we roll less than a 4, we will use prefixIndex 0 instead
// Chance to roll >= remainder goes up as level does, so we see higher-level
// potions as level increases.
if ( $oneInTen < $remainder ) {
$prefixIndex = max(0, $prefixIndex - 1);
}
return $prefixes[$prefixIndex];
}
private function InitItemList($playerLevel, $distance) {
// Potion quality is based on player level.
$prefix = $this->getPotionPrefix($playerLevel);
// Every shop has these items...
$healthPotion = findItem($prefix . "health potion");
$magicPotion = findItem($prefix . "magic potion");
$tent = findItem("tent");
// ... but in random quantities.
$numInStock = rand(0, 5);
for ( $i = 0; $i < $numInStock; ++$i ) {
$this->addStockItem($healthPotion);
}
$numInStock = rand(0, 3);
for ( $i = 0; $i < $numInStock; ++$i ) {
$this->addStockItem($magicPotion);
}
$numInStock = rand(0, 1);
for ( $i = 0; $i < $numInStock; ++$i ) {
$this->addStockItem($tent);
}
// Random weapons/armour.
$this->InitRandomEquipment($playerLevel, $distance);
}
public function isEmpty() {
return empty($this->stock);
}
function __construct($playerLevel, $distance) {
$this->stock = [];
$this->InitItemList($playerLevel, $distance);
}
}
class Spell {
public $name;
public $mpCost;
public $isHeal;
protected $damageCallback;
public $isAbility;
public function Cast(&$charData) {
return call_user_func($this->damageCallback, $charData);
}
function __construct($name, $mp, $isHeal, $damageCallback) {
$this->name = $name;
$this->mpCost = $mp;
$this->isHeal = $isHeal;
$this->damageCallback = $damageCallback;
$this->isAbility = false;
}
}
class Ability extends Spell {
function __construct($name, $mp, $isHeal, $damageCallback) {
$this->name = $name;
$this->mpCost = $mp;
$this->isHeal = $isHeal;
$this->damageCallback = $damageCallback;
$this->isAbility = true;
}
}
class Streak {
public $currentValue = 0;
const MAX_STREAK = 10;
public function increase($levelDiff, $isElite = false) {
$increaseVal = 0;
if ( $levelDiff >= 0 ) {
// ==, +1, +2, +3, +4, +5
$streakIncreases = [1, 1, 1, 2, 3, 5];
$maxIndex = count($streakIncreases) - 1;
$levelDiffIndex = min($levelDiff, $maxIndex);
$increaseVal = $streakIncreases[$levelDiffIndex];
// Elite monsters give us a double contribution.
if ( $isElite ) {
$increaseVal *= 2;
}
}
$this->currentValue += $increaseVal;
}
public function reset() {
$this->currentValue = 0;
}
public function decrease($decVal) {
$this->currentValue -= $decVal;
$this->currentValue = max(0, $this->currentValue);
}
public function getStreakMultiplier() {
// MIN(POWER(BASE, LEVEL), 10)
$STREAK_BASE = 1.1;
return min(round(pow($STREAK_BASE, $this->currentValue), 1), Streak::MAX_STREAK);
}
public function getStreakString() {
$streakVal = $this->currentValue;
$streakMul = $this->getStreakMultiplier();
$streakStr = "";
if ( $streakVal >= 25 ) {
$streakStr = "YOU'RE ON A FUCKING RAMPAGE! ";
}
else if ( $streakVal >= 20 ) {
$streakStr = "You're really causing havoc! ";
}
else if ( $streakVal >= 15 ) {
$streakStr = "You're making a proper mess! ";
}
else if ( $streakVal >= 10 ) {
$streakStr = "You're beginning to believe! ";
}
else if ( $streakVal >= 5 ) {
$streakStr = "You're getting warmed up! ";
}
else {
$streakStr = "You're just getting started! ";
}
$streakStr .= " ($streakVal kills, ${streakMul}x GP)";
return $streakStr;
}
}
class UIDAllocator {
public $fragments;
function __construct(&$fragments) {
$this->fragments = $fragments;
}
public function Allocate() {
$uids = array();
foreach( $this->fragments as $fragment ) {
$fragmentName = $fragment->token;
$uidSet = false;
$currentChar = 0;
do {
if ( $currentChar > ( strlen($fragmentName) - 1 ) ) {
echo "ERROR: Cannot set up UID for $fragmentName!\n";
exit(12);
}
$char = strtolower($fragmentName[$currentChar]);
// UNIQUE identifier.
if ( !in_array($char, $uids) ) {
$fragment->uid = $char;
$uidSet = true;
$uids[] = $char;
$this->SetNameWithUID($fragment);
}
$currentChar++;
} while ( !$uidSet );
}
}
private function SetNameWithUID(&$fragment) {
$str = $fragment->token;
$uid = $fragment->uid;
$uidIndicator = "($uid)";
$pos = stripos($str, $uid);
if ($pos !== false) {
$fragment->displayString = substr_replace($str, $uidIndicator, $pos, strlen($uid));
}
}
}