-
Notifications
You must be signed in to change notification settings - Fork 0
/
combat.php
412 lines (349 loc) · 9.53 KB
/
combat.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
<?php
require_once('combatrendering.php'); // All output rendering & displaying should go in the rendering lib script.
/*
The core of the combat system, includes the player objects and the combat object, as well as definitions of some attack-type constants.
*/
// The translations of integers into moves. Unfortunately, it's hard to translate them back into display messages in this form. Perhaps they should be remade as array values so that display strings can be applied.
define('STRIKE_HI', 0);
define('STRIKE_MID', 1);
define('STRIKE_LO', 2);
define('STRIKE_DUCK_LO', 3);
define('STRIKE_DUCK_MID', 4);
define('STRIKE_JUMP_HI', 5);
define('STRIKE_JUMP_MID', 6);
define('GUARD', 7);
define('REVERSAL_HI', 8);
define('REVERSAL_MID', 9);
define('REVERSAL_LO', 10);
define('BLOCK_HI', 11);
define('BLOCK_MID', 12);
define('BLOCK_LO', 13);
define('BLOCK_DUCK_LO', 14);
define('JUMP', 15);
define('DUCK', 16);
define('SEQ_MIN', 3);
define('SEQ_MAX', 255); // This is probably supposed to be 25, not 255.
define('DEBUG', false); // While debugging, display more information than you would with a player.
// Should potentially go in the rendering area eventually, but they're here to keep in sync for now.
$move_display_strings = array(
0=>'Strike Hi',
1=>'Strike Mid',
2=>'Strike Lo',
3=>'Strike and duck lo',
4=>'Strike and duck mid',
5=>'Jumping high strike',
6=>'Jumping middle strike',
7=>'Guard',
8=>'Reversal Hi',
9=>'Reversal Mid',
10=>'Reversal Lo',
11=>'Block Hi',
12=>'Block Mid',
13=>'Block Lo',
14=>'Block and Duck Lo',
15=>'Jump',
16=>'Duck',
);
class CombatSession
{
// 1s indicate attacker success, 0s indicate misses, and 2s indicate potential for reversal.
public static $STRIFE_MATRIX = array(
STRIKE_HI => array(
BLOCK_HI => 0,
BLOCK_MID => 1,
BLOCK_LO => 1,
BLOCK_DUCK_LO => 0,
JUMP => 1,
DUCK => 2
),
STRIKE_MID => array(
BLOCK_HI => 1,
BLOCK_MID => 2,
BLOCK_LO => 1,
BLOCK_DUCK_LO => 0,
JUMP => 1,
DUCK => 1
),
STRIKE_LO => array(
BLOCK_HI => 1,
BLOCK_MID => 1,
BLOCK_LO => 0,
BLOCK_DUCK_LO => 2,
JUMP => 0,
DUCK => 1
),
STRIKE_DUCK_MID => array(
BLOCK_HI => 1,
BLOCK_MID => 0,
BLOCK_LO => 2,
BLOCK_DUCK_LO => 1,
JUMP => 0,
DUCK => 1
),
STRIKE_DUCK_LO => array(
BLOCK_HI => 1,
BLOCK_MID => 1,
BLOCK_LO => 0,
BLOCK_DUCK_LO => 0,
JUMP => 0,
DUCK => 1
),
STRIKE_JUMP_HI => array(
BLOCK_HI => 2,
BLOCK_MID => 1,
BLOCK_LO => 1,
BLOCK_DUCK_LO => 0,
JUMP => 1,
DUCK => 0
),
STRIKE_JUMP_MID => array(
BLOCK_HI => 0,
BLOCK_MID => 0,
BLOCK_LO => 1,
BLOCK_DUCK_LO => 1,
JUMP => 2,
DUCK => 1
)
);
private $m_actors = array();
private $m_queue = array();
private $m_points = 0;
private $m_defenseIndex = 0;
private $m_offenseIndex = 0;
private $outcome = array();
public function __construct($p_attacker, $p_defender)
{
if(count($p_defender->getDefense())<1){ // For now, only set the defender's defense if it hasn't already been set in the defender object initially.
$p_defender->setDefense($this->createDefense());
}
$p_attacker->setDefense($this->createDefense());
$this->m_actors['attacker'] = $p_attacker;
$this->m_actors['defender'] = $p_defender;
}
private function createDefense()
{
// Sets a defense sequence between 10 and 15 long! Which is pretty damn long.
return $this->createSequence(rand(10, 15), 11, 16);
}
private function createOffense()
{
// Sets an offense sequence between 6 and 10 long!
return $this->createSequence(rand(6, 10), 0, 6);
}
// Creates a sequence of integers so long, within the range of two max integers depending on whether it's defense or offense.
private function createSequence($p_length, $p_min, $p_max)
{
$sequence = array();
for ($i = 0;$i < $p_length; $i++)
{
$sequence[] = rand($p_min, $p_max);
}
return $sequence;
}
public function setOffense($p_moves)
{
$this->m_actors['attacker']->setOffense($p_moves);
}
// Add a single entry to the outcome, including current hitpoints for both sides.
private function addSpar($message, $attacker_hp, $defender_hp)
{
// Append array of information to the outcome.
$this->outcome[] = array('message'=>$message, 'attacker_hp'=>$attacker_hp, 'defender_hp'=>$defender_hp);
}
// Get the entire outcome for display rendering.
public function outcome()
{
return $this->outcome;
}
// The main combat comparison section, based on pre-made defense sequences from the defender.
public function strife()
{
$breaker = 0;
$comboCounter = 0;
$reversalMulti = 0;
$repeat = 0;
$broken = false;
$offenseSequence = $this->m_actors['attacker']->getOffense();
$defenseSequence = $this->m_actors['defender']->getDefense();
$end = count($offenseSequence);
$pattern = count($defenseSequence);
if(DEBUG){
// Only while debugging clarify the pattern length and attack length.
echo "End length is: ".$end.", pattern length is: ".$pattern."\n";
}
$spar_block = ''; // The combined messages that will be displayed per round of combat.
// Is this a single round, or a single happening-between-inputs period?
for ($this->m_offenseIndex = 0; $this->m_offenseIndex < $end; $this->m_offenseIndex++, $this->m_defenseIndex++)
{
$offenseMove = $offenseSequence[$this->m_offenseIndex % $end];
$defenseMove = $defenseSequence[$this->m_defenseIndex % $pattern];
if ($this->m_offenseIndex > 1)
{
if ($offenseMove == $offenseSequence[($this->m_offenseIndex-1) % $end])
{
$repeat = 2;
if ($offenseMove == $offenseSequence[($this->m_offenseIndex-2) % $end])
{
$repeat = 3;
}
}
else
{
$repeat = 0;
}
}
$strife = $this->resolve($offenseMove, $defenseMove);
if ($strife == 1)
{
$this->m_points += $strife;
$this->m_actors['defender']->setHP($this->m_actors['defender']->getHP()-1);
$spar_block .= $defenseMove. " ";
$spar_block .= "Hit!\n";
$breaker = 0;
$comboCounter++;
}
else
{
if ($comboCounter > 2)
{
if ($reversalMulti)
{
$spar_block .= "C-c-c-combo x$comboCounter X".($reversalMulti+1)."!!!\n";
}
else
{
$spar_block .= "C-c-c-combo x$comboCounter!!!\n";
}
}
$breaker += $strife+1;
}
if ($breaker > 0 && $repeat)
{
$repeat = 0;
// *** THROW ***
$broken = true;
$spar_block .= $defenseMove. " Your opponent throws you; you land hard!\n";
$this->m_actors['attacker']->setHP($this->m_actors['attacker']->getHP()-2);
}
else if ($breaker > 1)
{
$broken = true;
$spar_block .= $defenseMove. " ";
if (($strife == 2) || (($this->m_offenseIndex + 1) >= $end))
{
$spar_block .= (($strife == 2) ? "Reversal" : "Counter-attack"). "!!!\n";
$reverseOffense = $this->createOffense();
$reverseDefense = $this->m_actors['attacker']->getDefense();
$reverseEnd = count($reverseOffense);
$reversePattern = count($reverseDefense);
$this->m_actors['defender']->setOffense($reverseOffense);
$r = 0;
$reverseBreaker = 0;
for ($q = 0;$q < $reverseEnd; $q++, $r++)
{
$offenseMove = $reverseOffense[$q % $reverseEnd];
$defenseMove = $reverseDefense[$r % $reversePattern];
$strife = $this->resolve($offenseMove, $defenseMove);
if ($strife == 1)
{
$comboCounter = 0;
$reverseBreaker = 0;
$this->m_actors['attacker']->setHP($this->m_actors['attacker']->getHP()-1);
$spar_block .= "You get hit!\n";
}
else
{
$reverseBreaker++;
if ($r == 0)
{
$spar_block .= "Double reversal!!!\n";
$reversalMulti++;
$broken = false;
break;
}
else if ($reverseBreaker > 1)
{
$reversalMulti = 0;
$spar_block .= "You defend!\n";
break;
}
else
{
$reversalMulti = 0;
$spar_block .= "You block...\n";
}
}
}
if ($q == $reverseEnd)
{
$reversalMulti = 0;
$spar_block .= "Your opponent steps back and taunts you!!\n";
}
}
else if ($comboCounter > 2)
{
$reversalMulti = 0;
$spar_block .= "C-c-c-combo breaker!!!!!!!\n";
}
else
{
$reversalMulti = 0;
$comboCounter = 0;
$spar_block .= "Miss!\n";
}
}
else if ($breaker > 0)
{
$reversalMulti = 0;
$comboCounter = 0;
$spar_block .= $defenseMove. " ";
$spar_block .= "Miss!\n";
}
if ($broken) break;
}
if ($comboCounter > 2)
{
$spar_block .= "C-c-c-combo x$comboCounter!!!\n";
}
// Set a set of output into the outcome array for later display.
$this->addSpar($spar_block, $this->m_actors['attacker']->getHP(), $this->m_actors['defender']->getHP());
return true;
}
public function isComplete()
{
return array_reduce($this->m_actors, function ($a, $b) { return ($a || ($b->getHP() <= 0)); });
}
private function resolve($p_attack, $p_block)
{
return CombatSession::$STRIFE_MATRIX[$p_attack][$p_block];
}
}
// A defender or attacker's stored state.
class CombatActor
{
private $m_hp = 0;
private $m_defense = array();
private $m_offense = array();
public function __construct()
{
}
public function setDefense($p_moves)
{
$this->m_defense = $p_moves;
}
public function setOffense($p_moves)
{
$this->m_offense = $p_moves;
}
public function setHP($p_number)
{
$this->m_hp = $p_number;
}
public function getDefense()
{ return $this->m_defense; }
public function getOffense()
{ return $this->m_offense; }
public function getHP()
{ return $this->m_hp; }
}
?>