/
fancydice.php
494 lines (437 loc) · 10.6 KB
/
fancydice.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
<?php
//fancydice.php
namespace hanelyp\fancydice;
class fancydice
{
/*
=pod
=head1 NAME
fancydice - scriptable dice
=head1 SYNOPSIS
roll dice defined by a specialty scripting language
=head1 USAGE
require ("fancydice.php");
// macro 'd' to implement common dice notation
$macros = array('d' => '@[1_>]' );
$fancydice = new fancydice($macros);
$roll = $fancydice->roll("3d6");
// $roll contains 3 random #s in 1..6
$total = $fancydice->sum($roll);
=cut
=head1 Etc
Derived from fancydice.pm
Extended syntax version
Copyright (c)2008-2015 Peter Hanely. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of GPL2 or later.
=head1 LANGUAGE
dice spec examples
3d6 Roll 3 6-sided dice
1d20+12 Roll 1 20-sided die and add 12
The dice spec cannot have spaces in it. You can roll multiple specs
at once by separating them with spaces.
The spec consists of the following tokens:
{number} A literal number
Example: 13
{string} Text enclosed in quotes
A string retains independent identity across adds, not
supported for -, *, and /
Example: "**"
{alpha} A word containing only letters
is a macro, which can be a full or partial
dice spec.
Example: fire
Resolves to: 1d4+1
(spec) A spec inside parenthesis will be evaluated to a single
number before other tokens are evaluated.
Example: (1,2)
Resolves to: 3
_ This will evaluate to a list of numbers, from the
preceding to the following, inclusive.
Example: 1_5
Resolves to: 1,2,3,4,5
[spec] A token will be chosen at random from within this spec.
Example: [1,2,3]
Resolves to: one of: 1, 2, 3
@ Repeats the following token a number of times equal to
the previous token. following token should be {number}
(spec) or [spec].
Example: 3@5
Resolves to: 5,5,5
+ - These only effect the sign of the following number.
Examples1: +3 Example2: -5
Resolves to: 3 -5
* / These perform multiplication and division.
Example1: 6*2 Example2: 9/3
Resolves to: 12 3
> This may only be used in macros, and lets the
macro pull in the following token.
Example: The macro 'd' is defined as '@[1_>]'
so 3d8 resolves to 3@[1_8], which resolves to
[1_8][1_8][1_8] which might resolve to 3,7,4.
Other Any other symbol is discarded, but serves to
separate tokens
Example: 3,2 The comma is discarded,
Resolves as: 3,2 but prevents it from resolving as 32.
=cut
*/
public static $VERSION = "2015.0221";
public static $default_macros = array(
'd' => '@[1_>]',
'f' => '@[-10,2_(>-1),(>+10)]' // house rules, critical success/fail as -10, max+10
);
public $debug = 0;
public $macros;
public $randval;
public $maxtokens = 1000;
private $tokencount;
public $language;
public $numsum = 0;
function __construct($_macros = false, $seed = false, $language = false)
{
if (!$_macros)
{
$_macros = self::$default_macros;
}
$this->macros = $_macros;
if (!$seed)
{
$seed = time();
}
$this->language = $language;
$this->randval = $seed;
}
// not the best random number generator, but insures repeatable results with a supplied seed
// and allows seed to be reset or per generator instance without messing up anything else
function rand($min = 0, $max = 0)
{
// based on ANSI C LCG algorithm
$this->randval = ($this->randval*1103515245 + 12345)%(1<<31);
$r = ($this->randval >> 16);
//echo "$r\n";
// $r < 0? shouldn't be possible, but was found
/* if (($max-$min+1) <= 0)
{
echo "$max-$min<br />";
} // */
if ($max <= $min)
{
$t = $max; $max = $min; $min = $t;
}
if ($max == $min)
{
return $min;
}
return $min + abs($r)%($max-$min+1);
}
function push_token(&$stack, $token)
{
$this->tokencount++;
if ($this->tokencount > $this->maxtokens)
{
//echo 'too many tokens<br />';
//throw new \Exception('Too Many Dice Tokens');
throw new \Exception($this->language->lang('FANCYDICE_EXCESS_TOKENS'));
}
array_push($stack, $token);
}
function sum($vals)
{
$sum = '';
$strings = array();
while (count($vals))
{
$val = array_shift($vals);
if (is_array($val))
{
//echo "sub array in sum\n";
$val = $this->sum($val);
}
//print_r($val); echo "\n";
//if (preg_match('/[^\d]/', $val))
if (!is_numeric($val))
{ array_push ($strings, $val); }
else
{ $sum += $val; }
}
$this->numsum = 1*$sum;
if (count($strings))
{ return $sum.' '.join(' ', $strings); }
return 1*$sum;
}
function selection($list)
{
if ($this->debug)
{
echo ("select from ".join (',', $list)."\n");
}
//return $list[rand(0,count($list)-1)];
//print_r($list);
// if (count($list) == 1)
// {
// return $list[0];
// }
return $list[$this->rand(0,count($list)-1)];
}
function parsetoken($roll)
{
//my ($token, @roll, $list);
if ($roll == '')
{
return array('', '', ']');
}
if (preg_match('/^\s*(\d+)(.*)/', $roll, $matches)) # number
{
return array($matches[1], $matches[2], '#');
}
if (preg_match('/^\s*"([^"]*)"(.*)/', $roll, $matches)) # string
{
return array($matches[1], $matches[2], '"');
}
if (preg_match('/^\s*([a-zA-Z]+)(.*)/', $roll, $matches)) # macro
{
return array($matches[1], $matches[2], 'm');
}
if (preg_match('/^\s*([\_\@\-\*\/])(.*)/', $roll, $matches) ) # _, @, -, *, /
{
return array($matches[1], $matches[2], $matches[1]);
}
if (preg_match('/^\s*(\()(.*)/', $roll, $matches) ) # sum of...
{
$stack = array();
$t = $this->parse($matches[2], $stack);
$list = $t[0];
$roll = $t[1];
$token = $this->sum($list);
return array($token, $roll, '#');
}
if (preg_match('/^\s*(\[)(.*)/', $roll, $matches) ) # selection of...
{
$stack = array();
$t = $this->parse($matches[2], $stack);
$list = $t[0];
$roll = $t[1];
$token = $this->selection($list);
//if (preg_match('/^\d+$/', $token))
//if (preg_match('/[^\d]/', $token))
if (!is_numeric($token))
{ $type = '"'; }
else
{ $type = '#'; }
return array($token, $roll, $type);
}
if (preg_match('/^\s*(\?)(.*)/', $roll, $matches))
{
return array('?', $matches[2], '?');
}
if (preg_match('/^\s*([\]\)])(.*)/', $roll, $matches))# end tags
{
//print_r($matches);
return array($matches[1], $matches[2], ']');
}
# else
preg_match('/\s*(.)(.*)/', $roll, $matches);
return array($matches[1], $matches[2], $matches[1]);
}
function eval_token(&$roll, &$rolll, $token, $type, &$sign, &$endtoken)
{
// $sign = 1;
if ($this->debug)
{ echo (join (',', $rolll). ": $token, $roll, $type\n"); }
if (($type == '#') || ($type == '['))
{
//array_push($rolll, $sign*$token);
$this->push_token($rolll, $sign*$token);
$sign = 1;
}
if ($type == '"')
{
//array_push($rolll, $token);
$this->push_token($rolll, $token);
$sign = 1;
}
elseif ($type == 'm')
{
//print_r($this->macros);
if (isset($this->macros[strtolower($token)]))
{
$macro = $this->macros[strtolower($token)];
if ($this->debug)
{ echo "macro $token => $macro\n"; }
if (preg_match('/>/', $macro))
{
//echo $roll, "\n";
$t = $this->parsetoken($roll);
//print_r($t);
$token = $t[0];
$roll = $t[1];
$macro = preg_replace('/>/', $token, $macro);
if ($this->debug)
{ echo "macro $token => $macro\n"; }
}
$l = $this->parse($macro, $rolll);
$list = $l[0];
}
else
{
if ($this->debug)
{
echo 'unknown macro '.$token."\n";
}
$list = $token;
}
if ($this->debug)
{
echo (join (',', $rolll). "\n");
echo ("result => ".join (',', $list). "\n");
}
//array_merge($rolll, $list);
}
elseif ($type == '_')
{
$t = $this->parsetoken($roll);
$token = $t[0];
$roll = $t[1];
$last = array_pop($rolll);
#if ($last <= 0) { $last = 1; }
if (!preg_match('/\d/', $token))
{
$token = 0;
}
if ($last > $token)
{
$i = $last; $last = $token; $token = $i;
}
for ($i = $last; $i <= $token; $i++)
{
//array_push($rolll, $i);
$this->push_token($rolll, $i);
}
}
elseif ($type == '@')
{
$last = array_pop($rolll);
$last = $last?$last:1;
for ($i = 0; $i < $last; $i++)
{
if ($this->debug)
{ echo "running $roll\n"; }
$t = $this->parsetoken($roll);
$token = $t[0];
$next = $t[1];
$ntype = $t[2];
$next1 = $this->eval_token($next, $rolll, $token, $ntype, $sign, $endtoken);
/* #$this->addtoken(\@roll, $token, $ntype, $sign);
if ($this->debug)
{
echo "type $ntype\n";
}
if ($ntype == '#')
{ array_push($rolll, $token*$sign); }
else
{ array_push($rolll, $token); }
if ($this->debug)
{
echo "token $token\n";
echo ("repeat $i ".join (',', $rolll). ": @\n");
} // */
}
$sign = 1;
$roll = $next1;
}
elseif ($type == '-')
{
$sign = -1;
}
elseif ($type == '*')
{
$last = array_pop($rolll);
$t = $this->parsetoken($roll);
$token = $t[0];
$next = $t[1];
$ntype = $t[2];
//array_push($rolll, $last*$token);
$this->push_token($rolll, $last*$token);
$roll = $next;
}
elseif ($type == '/')
{
$last = array_pop($rolll);
$t = $this->parsetoken($roll);
$token = $t[0];
$next = $t[1];
$ntype = $t[2];
//array_push($rolll, intval($last/$token));
if ($token != 0)
{
$this->push_token($rolll, intval($last/$token));
}
else
{
$this->push_token($rolll, 'div0');
}
$roll = $next;
}
elseif ($type == '?')
{
if ($this->debug)
{
echo ("before if ".join (',', $rolll). ": ?\n");
}
$last = array_pop($rolll);
if ($this->debug)
{
echo ("condition ".$last. ": ?\n");
}
if ($last)
{
$t = array();
$l = $this->sum($this->parse($roll, $t));
// print_r($l); echo "\n";
array_splice ($rolll, -1,0, $l);
// echo join (',', $rolll),"\n";
}
if ($this->debug)
{
echo ("after if ".join (',', $rolll). ": ?\n");
}
$roll = '';
}
elseif ($type == ']')
{
$endtoken = true;
}
return $roll;
}
function parse($roll, &$rolll)
{
$sign = 1;
$endtoken = false;
while ((isset($roll)) && (!$endtoken))
{
$t = $this->parsetoken($roll);
$token = $t[0];
$roll = $t[1];
$type = $t[2];
$this->eval_token($roll, $rolll, $token, $type, $sign, $endtoken);
}
return array($rolll, $roll);
}
function roll($roll)
{
$this->tokencount = 0;
$stack = array();
try
{
$l = $this->parse($roll, $stack);
}
catch(\Exception $e)
{
array_push($stack, $e->getMessage());
$l = array($stack);
}
//print_r($l);
return $l[0];
}
}
?>