-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
362 lines (330 loc) · 13.9 KB
/
functions.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
<?php
//Allow for functions to be requested by ajax (for example), this will then check to see if this page has the needed function then execute it.
if (session_status() == PHP_SESSION_NONE) {
$_SESSION['dungeon']['uid'] = uniqid();
session_unset();
//session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
}
session_start();
//INIT
$directions = array("N" => "North", "S" => "South", "E" => "East", "W" => "West");
//Generate SEED
if(isset($_GET['seed'])){$GLOBALS['seed'] = $_GET['seed'];}else{$GLOBALS['seed'] = 42;}
srand($GLOBALS['seed']);
if(isset($_GET['function'])){
$funcParams = [];
$function = $_GET['function']; // 1
$callable = is_callable($function, false, $callable_name);
if($callable){
if(isset($_SESSION['dungeon'])){
$funcParams[0] = $_SESSION['dungeon'];
}else{
$funcParams[0] = "";
}
if(isset($_POST['from_uid'])){
$funcParams[1] = $_POST['from_uid'];
}else{
$funcParams[1] = "";
}
$result = call_user_func_array($function, $funcParams);
}else{
return false;
}
}
// Generate the Goal for the adventure
function goal($g,$type){
$str = file_get_contents('json/goals.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid goals.json','Your goals.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
if (isset($json['goals'][0][$type]) || isset($g)){
//Seed Random Number Generator used by PHP to control output
//srand($GLOBALS['seed']);
$rand_key = array_rand($json['goals'][0][$type], $g);
$_SESSION['dungeon']['goal'] = $json['goals'][0][$type][$rand_key]['goal'];
//echo $_SESSION['dungeon']['goal'];
}else{
error_modal('ERROR: Invalid dungeon type selected','Your dungeon type selection is invalid, please correct and try again.');
}
}
}
// Generate the Introduction of the adventure
function introduction(){
$str = file_get_contents('json/introductions.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid introductions.json','Your introductions.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
//Seed Random Number Generator used by PHP to control output
//srand($GLOBALS['seed']);
$rand_key = array_rand($json['introductions']);
$_SESSION['dungeon']['intro'] = $json['introductions'][$rand_key]['intro'];
//echo $_SESSION['dungeon']['intro'];
}
}
function rand_dungeon_starting_area($chamber_id){
// Generate the random starting area for the adventure
$str = file_get_contents('json/dungeon_starting_areas.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_starting_areas.json','Your dungeon_starting_areas.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
//Seed Random Number Generator used by PHP to control output
//srand($GLOBALS['seed']);
$rand_key = array_rand($json['starting_areas'],1);
$dungeon[$chamber_id] = $json['starting_areas'][$rand_key];
$_SESSION['dungeon'][$chamber_id] = $json['starting_areas'][$rand_key];
//Generate Unique ID for this instance
$dungeon['uid'] = uniqid();
$_SESSION['dungeon'][$chamber_id]['uid'] = uniqid();
}
}
function rand_dungeon_chamber($chamber_id,$from_uid){
// Generate the random starting area for the adventure
$str = file_get_contents('json/dungeon_chambers.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_chambers.json','Your dungeon_chambers.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
//Seed Random Number Generator used by PHP to control output
//srand($GLOBALS['seed']);
$rand_key = array_rand($json['chambers'][0]['lair'],1);
$dungeon[$chamber_id] = $json['chambers'][0]['lair'][$rand_key];
$_SESSION['dungeon'][$chamber_id] = $json['chambers'][0]['lair'][$rand_key];
//Generate Unique ID for this instance
$dungeon['uid'] = uniqid();
if($from_uid){$_SESSION['dungeon'][$chamber_id]['from_uid'] = $from_uid;}
$_SESSION['dungeon'][$chamber_id]['uid'] = uniqid();
ksort($_SESSION['dungeon'][$chamber_id]);
$chamber = $_SESSION['dungeon'][$chamber_id];
return $_SESSION['dungeon'];
}
}
// Generate the random dungeon passage for the adventure
function rand_dungeon_passage($uid,$direction){
//pg. 290 5e DMG - Passage width can be determined randomly as well.
$str = file_get_contents('json/dungeon_passages.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_starting_areas.json','Your dungeon_starting_areas.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
//Seed Random Number Generator used by PHP to control output
$rand_key = array_rand($json['passage'],1);
$passage = $json['passage'][$rand_key];
//Secret Door?
//if there is a chance of a secret door - Perception DC 20 or 30
if($json['passage'][$rand_key]['secret_chance']){
$chance = rand($json['passage'][$rand_key]['secret_chance'],100);
$passage['secret']['chance'] = $chance;
if(intval($chance) <= intval($json['passage'][$rand_key]['secret_chance'])){
//Success Door Found!
$passage['secret']['description'] = "There is a Secret Door to the South.";
}{
//Theres no door here!
$passage['secret']['description'] = "No hidden doors found.";
}
}
//Generate Unique ID for this instance
$passage['uid'] = uniqid();
//Pass FROM UID value
if(isset($uid)){
$passage['from_uid'] = $uid;
}
//This needs to change to the location being generated for and not fixed to starting area
//Using $uid provided, append to array at that point
//FIND $uid and append to that part of the array
//$_SESSION['dungeon']['starting_area'][$direction]['passage'] = $passage;
return $passage;
}
}
function door_check($passage,$direction){
$door = [];
$doors = explode(',',$passage['doors']);
if (in_array("N", $doors))
{ $door = rand_dungeon_door($passage,$direction,'N'); }
if (in_array("S", $doors))
{ $door = rand_dungeon_door($passage,$direction,'S'); }
if (in_array("E", $doors))
{ $door = rand_dungeon_door($passage,$direction,'E'); }
if (in_array("W", $doors))
{ $door = rand_dungeon_door($passage,$direction,'W'); }
return $door;
}
// Generate the random dungeon door for the adventure
function rand_dungeon_door($passage,$passage_direction,$door_direction){
$door = [];
$str = file_get_contents('json/dungeon_doors.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_starting_areas.json','Your dungeon_starting_areas.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
//Seed Random Number Generator used by PHP to control output
//srand($GLOBALS['seed']);
$rand_key = array_rand($json['door'],1);
$door = $json['door'][$rand_key];
//Generate Unique ID for this instance
$door['uid'] = uniqid();
$door['direction'] = $door_direction;
return $door;
}
}
function dungeon_door($chamber_id,$direction,$BuildQueue){
$doors = [];
$doors = explode(',',$_SESSION['dungeon'][$chamber_id]['doors']);
if ($doors){
foreach($doors as $direction){
$door = door_check($_SESSION['dungeon'][$chamber_id],$direction);
if($door){
$check = rand_dungeon_beyond_door($door['uid']);
if($check == 'passage'){
//echo "ADD PASSAGE TO QUEUE<br>";
$BuildQueue2 = [];
$BuildQueue2['type'] = $check;
$BuildQueue2['uid'] = $door['uid'];
array_push($BuildQueue,$BuildQueue2);
}
if($check == 'chamber'){
//echo "ADD CHAMBER TO QUEUE<br>";
$BuildQueue2 = [];
$BuildQueue2['type'] = $check;
$BuildQueue2['uid'] = $door['uid'];
array_push($BuildQueue,$BuildQueue2);
}
if($check == 'stairs'){
//Set from_uid for next chamber connected to this door
//echo "ADD STAIRS TO QUEUE<br>";
$BuildQueue2 = [];
$BuildQueue2['type'] = $check;
$BuildQueue2['uid'] = $door['uid'];
array_push($BuildQueue,$BuildQueue2);
}
}}}
return $BuildQueue;
}
// Generate the random chamber purpose for the adventure
function rand_dungeon_beyond_door(){
$beyond = ["passage","passage","passage","passage","passage","passage","passage","chamber","chamber","chamber","chamber","chamber","stairs"];
$rand_key = array_rand($beyond, 1);
return $beyond[$rand_key];
}
// Generate the random chamber purpose for the adventure
function rand_dungeon_chamber_purpose($type,$chamber_id){
$purpose = [];
$str = file_get_contents('json/dungeon_chamber_purpose.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_chamber_purpose.json','Your dungeon_chamber_purpose.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
$rand_key = array_rand($json['purpose'][0][$type],1);
$purpose = $json['purpose'][0][$type][$rand_key]['purpose'];
$_SESSION['dungeon'][$chamber_id]['purpose'] = $purpose;
return $purpose;
}
}
// Generate the chamber state
function rand_dungeon_chamber_state($type,$chamber_id){
$state = [];
$str = file_get_contents('json/dungeon_chamber_state.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_chamber_state.json','Your dungeon_chamber_state.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
$rand_key = array_rand($json['states'][0][$type],1);
$state = $json['states'][0][$type][$rand_key]['state'];
$_SESSION['dungeon'][$chamber_id]['state'] = $state;
ksort($_SESSION['dungeon'][$chamber_id]);
return $state;
}
}
// Generate the dungeon chamber contents
function rand_dungeon_chamber_contents($type,$chamber_id){
$contents = [];
$str = file_get_contents('json/dungeon_chamber_contents.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_chamber_contents.json','Your dungeon_chamber_contents.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
$rand_key = array_rand($json['contents'][0][$type],1);
$content = $json['contents'][0][$type][$rand_key];
$_SESSION['dungeon'][$chamber_id]['content'] = $content;
ksort($_SESSION['dungeon'][$chamber_id]);
return $content;
}
}
// Generate the dungeon chamber monsters
function add_monster($type,$chamber_id,$level,$difficulty){
$monsters = [];
$str = file_get_contents('json/dungeon_monsters.json');
$json = json_decode($str, true); // decode the JSON into an associative array
if ($json === null
&& json_last_error() !== JSON_ERROR_NONE) {
error_modal('ERROR: Invalid dungeon_monsters.json','Your dungeon_monsters.json file is invalid, please correct and try again.<br>[See <a href="http://jsonlint.com/" target="_blank">http://jsonlint.com/</a>]');
}else{
//Generate enough monsters to fit the CR of the encounter/dungeon
$monsters = [];
//Get dungeon level and multiply it by the avg number of players (4) divided by half
$monster_cr = 0;
$encounter_cr = $level*$difficulty;
//Get monster CR
while($encounter_cr > $monster_cr){
$rand_key = array_rand($json['monsters'][0][$type],1);
$monster = $json['monsters'][0][$type][$rand_key];
array_push($monsters,$monster);
$monster_cr += $monster['cr'];
}
$_SESSION['dungeon'][$chamber_id]['monster'] = $monsters;
ksort($_SESSION['dungeon'][$chamber_id]);
return $monsters;
}
}
//Handle Errors
function error_modal($title,$message){
echo '<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header alert-danger">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">';
echo $title;
echo '</h4></div><div class="modal-body">';
echo $message;
echo '</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div></div></div>
<script>$("#myModal").modal();</script>';
}
function search($arr, $key)
{
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$results = [];
foreach ($ritit as $leafValue) {
$path = [];
foreach (range(0, $ritit->getDepth()) as $depth) {
$path[] = $ritit->getSubIterator($depth)->key();
}
$results[] = join('_', $path);
}
array_pop($path);
return $path;
}
function insert_into_array($path,$a2,$label)
{
$dest = &$_SESSION['dungeon'];
foreach($path as $pathSegment) {
$dest = &$dest[$pathSegment];
}
$dest[$label] = $a2;
return true;
}
?>