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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
<?PHP
function DisplayMap($mapMatrix, $idprefix = 1, $example = false, $speed = NULL) {
//Iterate through $mapMatrix and generate the html
$maptable = ""; //The string to return to the database.
$index = 0; //The current number of tiles from the last tile saved.
if ($speed == NULL) {
if ($example) {
$speed = 1;
} else {
$speed = 2;
}
}
for($i = 1; $i < count($mapMatrix); $i++)
{
$maptable .= "<tr>";
for($j = 0; $j < count($mapMatrix[$i]); $j++)
{
//==
$index++;
$handle = "$idprefix,$i,$j";
switch($mapMatrix[$i][$j])
{
case 's': $maptable .= "<td class='grid_td_start' id='$handle' ></td>"; break;
case 'f': $maptable .= "<td class='grid_td_finish' id='$handle' ></td>"; break;
//TP1
case 't': $maptable .= "<td title='Teleport 1 in' class='grid_td_tp1_in' id='$handle' ></td>"; break;
case 'u': $maptable .= "<td title='Teleport 1 out' class='grid_td_tp1_out' id='$handle' ></td>"; break;
//TP2
case 'm': $maptable .= "<td title='Teleport 2 in' class='grid_td_tp2_in' id='$handle' ></td>"; break;
case 'n': $maptable .= "<td title='Teleport 2 out'class='grid_td_tp2_out' id='$handle' ></td>"; break;
//TP3
case 'g': $maptable .= "<td title='Teleport 3 in' class='grid_td_tp3_in' id='$handle' ></td>"; break;
case 'h': $maptable .= "<td title='Teleport 3 out'class='grid_td_tp3_out' id='$handle' ></td>"; break;
//TP4
case 'i': $maptable .= "<td title='Teleport 4 in' class='grid_td_tp4_in' id='$handle' ></td>"; break;
case 'j': $maptable .= "<td title='Teleport 4 out'class='grid_td_tp4_out' id='$handle' ></td>"; break;
//TP5
case 'k': $maptable .= "<td title='Teleport 5 in' class='grid_td_tp5_in' id='$handle' ></td>"; break;
case 'l': $maptable .= "<td title='Teleport 5 out'class='grid_td_tp5_out' id='$handle' ></td>"; break;
case 'a': $maptable .= "<td class='grid_td_cpa' id='$handle' ></td>"; break;
case 'b': $maptable .= "<td class='grid_td_cpb' id='$handle' ></td>"; break;
case 'c': $maptable .= "<td class='grid_td_cpc' id='$handle' ></td>"; break;
case 'd': $maptable .= "<td class='grid_td_cpd' id='$handle' ></td>"; break;
case 'e': $maptable .= "<td class='grid_td_cpe' id='$handle' ></td>"; break;
case 'r': $maptable .= "<td class='grid_td_rocks' id='$handle' ></td>"; break; //rock
case 'w': $maptable .= "<td class='grid_td_walls' id='$handle' name='true' onClick='grid_click(this)' ></td>"; break; //wall
//default: $maptable .= "<td class='grid_td' id='$handle' onClick='grid_click(this)' >".$index."</td>";
default: $maptable .= "<td title='Position: $i,$j' class='grid_td' id='$handle' onClick='grid_click(this)' ></td>";
//default: $maptable .= "<td class='grid_td' id='$handle' onClick='grid_click(this)' >".$mapMatrix[$i][$j]."</td>";
}
}
$maptable .= "</tr>";
}
//Prepare mapdata.
$mapdata['height'] = $mapMatrix[0][0];
$mapdata['width'] = $mapMatrix[0][1];
$mapdata['points'] = $mapMatrix[0][2];
$mapdata['rocks'] = $mapMatrix[0][3];
$mapdata['walls'] = $mapMatrix[0][4];
$mapdata['teleports'] = $mapMatrix[0][5];
$mapdata['example'] = $example;
$mapdata['mapid'] = $idprefix;
$path = routePath($mapMatrix, '');
$mapdata['code'] = GenerateMapCode($mapMatrix);
$width = (($j * 35) + 2).'px';
//$width = (($j * 23) + 2).'px';
$i -= 1;
$height = (($i * 35)).'px';
//$height = (($i * 22) + 2).'px';
$jsonmap = json_encode($mapdata);
$mapdatadiv .= "<div id='$idprefix,mapdata' style='visibility:hidden;display:none'>";
$mapdatadiv .= $jsonmap;
$mapdatadiv .= '</div>';
$maptable = "<table style='width:$width;height:$height;' class='grid_table'>
$maptable
</table>";
$prefSpeed = $_COOKIE['pref_speed'];
$speedOption['Slow'] = 1;
$speedOption['Med'] = 2;
$speedOption['Fast'] = 3;
$speedOption['Ultra'] = 4;
if (!in_array($prefSpeed, $speedOption))
$prefSpeed = '2';
foreach ($speedOption as $key => $value) {
$rOption .= "<option value='$value'";
if ($prefSpeed == $value)
$rOption .= " selected='selected'";
$rOption .= ">$key</option>\n";
}
if ($_COOKIE['pref_mute'] == "true") {
$mutebutton = "<label><input onclick='savePref(\"mute\", this.checked)' type='checkbox' id='$idprefix,mute' checked='checked' />Mute</label>";
} else {
$mutebutton = "<label><input onclick='savePref(\"mute\", this.checked)' type='checkbox' id='$idprefix,mute' />Mute</label>";
}
if ($example) {
$output = $maptable;
$output .= "<input id='$idprefix,btn' type='button' onclick='doSend($idprefix)' value='Test' />";
$output .= "
<div style='display:none;'>
<input type='checkbox' id='$idprefix,mute' checked=true />
<select id='$idprefix,speed'>
$rOption
</select>
</div>
";
$output .= $mapdatadiv;
$output = "<div style='width:$width;height:$height;'>
$output
</div>";
return $output;
}
//$date = date("m-d-y");
$output = "
<div id='$idprefix,outer' class='grid_outer' style='width:".($width+2)."px;height:".($height+50)."px;'>
<div class='grid_dsp_left dsp_49'>
<div id='$idprefix,dspID' title='MapID: $idprefix'>
MapID: $idprefix
</div>
</div>
<div id='$idprefix,dsptr' class='grid_dsp_right dsp_33'>
<span id='$idprefix,dspWalls' class='grid_dsp_data'>
".$mapdata['walls']." walls
</span>
<span>
( <a href='javascript:resetwalls($idprefix)'>Reset</a> )
</span>
</div>
$maptable
<div id='$idprefix,dspbl' class='grid_dsp_left dsp_49'>
<input id='$idprefix,btn' type='button' onclick='doSend($idprefix)' value='Go!' />
Speed:
<select onChange='savePref(\"speed\", this.value)' id='$idprefix,speed'>
$rOption
</select>
</div>
<div class='grid_dsp_mid dsp_16'>
$mutebutton
</div>
<div id='$idprefix,dspbr' class='grid_dsp_right dsp_33'>
<div id='$idprefix,dspCount' class='grid_dsp_data'>
".$path['moves']." moves
</div>
</div>
$mapdatadiv
</div>
";
return $output;
}
//Generates map
function GenerateMap($rows, $cols, $rockchance, $numBlocks = -1, $cp = -1, $tp = -1) {
//!! Possibility of inf loop here.
do {
$randvalue = rand(1, ($rows * $cols));
//As long as it isn't the first, or last column.
//if ((($randvalue +1) % ($rows)) > 1) {
//As long as it isn't in the first, 2nd, last and 2nd to last column.
if ((($randvalue +2) % ($rows)) > 3) {
$unique[] = $randvalue;
$unique = array_unique($unique);
$unique = array_values($unique);
}
} while (count($unique) < 15);
if ($numBlocks == -1)
$numBlocks = Rand(7, (int)($rows * $cols) * .12);
if ($cp == -1)
$cp = rand(0, 5);
if ($tp == -1)
$tp = rand(0, 2);
$tp = $tp * 2; //Requires an out-teleport.
$cpnames = Array("a", "b", "c", "d", "e");
$tpnames = Array("t", "u", "m", "n", 'g', 'h', 'i', 'j', 'k', 'l');
$teleport = Array();
$checkpoint = Array();
$i = 0;
for($p = 0; $p < $cp; $p++) {
$checkpoint[$cpnames[$p]] = $unique[$i];
$i++;
}
for($p = 0; $p < $tp; $p++) {
$teleport[$tpnames[$p]] = $unique[$i];
$i++;
}
$rocks = 0; //Number of rocks in the maze.
// We need to make sure the map we construct is valid.
// so we throw this in a do-while.
do {
$p = -1;
//Begin loop to populate grid.
for( $y = 1; $y <= $cols; $y++) { //Number of Columns
for( $x = 0; $x < $rows; $x++) { //Number of Rows
$p++;
//Start and Finish squares.
if ($x == 0) {
$grid[$y][$x] = "s";
} elseif ($x == $rows - 1) {
$grid[$y][$x] = "f";
//Randomly Placed Rocks
} elseif (rand(1, $rockchance) == 2) {
$grid[$y][$x] = "r";
$rocks++;
//!! rock count could be off if covered by checkpoint.
//Just a normal square.
} else {
$grid[$y][$x] = "o";
}
//Absolutely placed points; Checkpoints.
foreach ($checkpoint as $key => $v) {
if ($v == $p) {
$grid[$y][$x] = $key;
}
} //Teleports too
foreach ($teleport as $key => $v) {
if ($v == $p) {
$grid[$y][$x] = $key;
}
}
} //Rows
} //Cols
//Fill $grid[0] with header information
$grid[0][0] = $rows;
$grid[0][1] = $cols;
$grid[0][2] = count($checkpoint);
$grid[0][3] = $rocks;
$grid[0][4] = $numBlocks;
$grid[0][5] = count($teleport);
//Confirm the map isn't broken to start-out.
$path = routePath($grid);
//Only repeat if it's blocked.
} while ($path['blocked'] == true);
return $grid;
}
//Turns a mapMatrix into a code - see GenerateMapByCode
function GenerateMapCode($mapMatrix) {
//Iterate through $mapMatrix and generate the code used to save and
// load the map through the database.
// 0.1 Snap. (added mapsize header data)
// 0.2 Rex - Added #checkpoints, #rocks, #walls to header data; adjusted loops.
$code = ""; //The string to return to the database.
$index = 0; //The current number of tiles from the last tile saved.
// $mapMatrix[0] stores header data--dimensions, #checkpoints, #rocks, #walls
// $mapMatrix[1] count will always be the width.
$mapsize = $mapMatrix[0][0].'x'.$mapMatrix[0][1]; //Width x Height
$code = $mapsize.
'.c'.$mapMatrix[0][2].
'.r'.$mapMatrix[0][3].
'.w'.$mapMatrix[0][4].
'.t'.$mapMatrix[0][5];
// dimensions + # checkpoints + # rocks + # placeable walls
//echo $code."<br />";
$code .= ".:";
for( $i = 1; $i < count($mapMatrix); $i++)
{
for( $j = 0; $j < count($mapMatrix[$i]); $j++)
{
if($mapMatrix[$i][$j] != 'o')
{
//As long as the tile is NOT open, embed it in the code.
$code .= $index.$mapMatrix[$i][$j].'.';
$index = -1;
//==echo "<br /><b>".$index.$mapMatrix[$i][$j]."</b><br />";
}
$index += 1;
}
}
return $code;
}
//Turns a mapcode into a mapMatrix, - see GenerateMapCode
function GenerateMapByCode($code) {
//Create $mapMatrix by iterating through $code (a string value).
//==$mapMatrix = array();
$tmp = explode( ":", $code);
$headers = explode( '.', $tmp[0]);
$splitCode = explode( '.', $tmp[1]);
//Extract header information.
//==$mapMatrix[0] = array();
$dimensions = explode( 'x', $headers[0]);
$mapMatrix[0][0] = $dimensions[0]; //Width
$mapMatrix[0][1] = $dimensions[1]; //Height
//Select the next parameters by ignoring the character label.
$mapMatrix[0][2] = (int)substr($headers[1], 1); //Number of Checkpoints
$mapMatrix[0][3] = (int)substr($headers[2], 1); //Number of Rocks
$mapMatrix[0][4] = (int)substr($headers[3], 1); //Number of Wall Blocks
$mapMatrix[0][5] = (int)substr($headers[4], 1); //Number of Teleports
//Printing out parameters for debug purposes...
/*
echo "<br />Map Parameters:<br />";
echo "mapMatrix[0][0]: ".$mapMatrix[0][0]."<br />";
echo "mapMatrix[0][1]: ".$mapMatrix[0][1]."<br />";
echo "mapMatrix[0][2]: ".$mapMatrix[0][2]."<br />";
echo "mapMatrix[0][3]: ".$mapMatrix[0][3]."<br />";
echo "mapMatrix[0][4]: ".$mapMatrix[0][4]."<br />";
echo "mapMatrix[0][5]: ".$mapMatrix[0][5]."<br />";
*/
//Begin creating our mapMatrix
$t = -1;
$index = 0;
for( $i = 1; $i <= $mapMatrix[0][1]; $i++) { //Number of Rows
for( $j = 0; $j < $mapMatrix[0][0]; $j++) { //Number of Columns
$t++;
$next = substr($splitCode[$index], 0, strlen($splitCode[$index]) - 1);
if ($next == $t) {
$type = substr($splitCode[$index], -1, 1);
$mapMatrix[$i][$j] = $type;
$index++;
$t = -1;
//echo "type:".$type."<br />";
//echo "number:".$next."<br />";
//echo "original:".$splitCode[$index]."<br />";
} else {
$mapMatrix[$i][$j] = 'o'; //Empty Tile
}
//echo "Value:".$mapMatrix[$i][$j]."<br />";
}
}
//echo "mapMatrix[1][0]: ".$mapMatrix[1][0]."<br />";
//echo "mapMatrix[1][1]: ".$mapMatrix[1][1]."<br />";
return $mapMatrix;
//Snap Stops
//Iterate through the code and adjust spaces as directed.
/* UNREACHABLE CODE
$index = 0;
for ( $i = 4; $i < count($splitCode); $i++)
{
echo "<br />";
$index += (int)$splitCode[$i];
$type = $splitCode[$i][strlen($splitCode[$i])-1];
//$tile = GetTile($mapMatrix, $index);
echo "$x = (int)($index / ".$mapMatrix[0][0].");<br />";
$x = (int)($index / $mapMatrix[0][0]);
$y = $id % $mapMatrix[0][0];
echo "splitCode: ".$splitCode[$i]."<br />";
echo "Index: ".$index." -- ".$x.",".$y." to ".$type;
echo "<br />";
$mapMatrix[$x][$y] = $type;
}
return $mapMatrix; */
}
//Returns a mapMatrix merged with a solution/maze.
function MergeMapSolution($mapMatrix, $solution) {
//echo $solution;
$sa = explode( '.', $solution);
foreach($sa as $v) {
if ($v == '') continue;
$v= explode(",", $v);
$i = $v[0];
$j = $v[1];
//Trying to place a wall - where?
if ($mapMatrix[$i][$j] <> 'o') return -1;
$mapMatrix[$i][$j] = 'w';
//Are we out of blocks?
//if ($mapMatrix[0][2] < 1) return -2;
$mapMatrix[0][4]--;
}
return $mapMatrix;
}
function seperateMapSolution($mapMatrix) {
for( $i = 1; $i <= $mapMatrix[0][1]; $i++) //Number of Rows
for( $j = 0; $j < $mapMatrix[0][0]; $j++) //Number of Columns
if ($mapMatrix[$i][$j] == 'w')
$solution .= "$i,$j.";
$solution = ".".$solution;
return $solution;
}
//This is required to identify identical solutions, or even 'close' ones.
function formSolution($solution) {
$tmp = explode(".", $solution);
$tmp = array_filter($tmp);
sort($tmp);
$tmp = '.'.implode(".", $tmp).'.';
return $tmp;
}
//Returns the best solution.
function getSolution($userID, $mapID) {
include_once('db.inc.php');
$sql = "SELECT `solution`
FROM `solutions`
WHERE `userID` = '$userID' AND
`mapID` = '$mapID'
";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
list($solution) = mysql_fetch_row($result);
return $solution;
}
}
function getMapCode($mapID) {
include_once('db.inc.php');
$sql = "SELECT `code`
FROM `maps`
WHERE `ID` = '$mapID'
";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
list($map) = mysql_fetch_row($result);
return $map;
}
}
function pastMap($maptype, $daysago) {
$sql = "
SELECT `mapID`
FROM `mapOfTheDay`
WHERE DATE_ADD(CURDATE(), INTERVAL -$daysago DAY) =
DATE_FORMAT(mapDate,'%Y-%m-%d') AND
`mapType` = '$maptype'
";
//echo "<br />$sql<br />";
$result = mysql_query($sql) or die(mysql_error());
//No map for today?
if (mysql_num_rows($result) == 0)
return -1;
$r = mysql_result($result, 0, 'mapID');
//echo "result: $r";
return $r;
}
// Returns: ARRAY( blocked, path, start, end )
function findPath($mapMatrix, $start = '0,1.', $target = 'f') {
$seed = explode(".", $start);
foreach ($seed as &$v) {
$v = explode(",", $v);
$v[2] = $v[0].','.$v[1];
}
//print_r ($seed);
$index = count($seed);
do {
foreach ($seed as $key => $v) {
//Search the squares around, to spread the seeds
for($i = 1; $i <= 4; $i++) {
$x = $v[0];
$y = $v[1];
//Create a handle on the squares around it.
switch($i){
case 1: $y--; break; //up
case 2: $x++; break; //right
case 3: $y++; break; //down
case 4: $x--; break; //left
}
if ($y < 1 OR $x < 0) continue 1;
//What's there?
switch($mapMatrix[$y][$x]) {
case $target: //Finishline!
//Our search is over.
$r['blocked'] = false;
$r['path'] = $seed[$key][3].$i;
$r['start'] = $v[2];
$r['end'] = "$x,$y";
return $r;
break;
// Teleports m t g i k
case "m": case "t": case "g": case "i": case "k":
$path = $mapMatrix[$y][$x];
case "o": //Available squares
//!!
case "s": case "f": //Start and end tiles
case "a": case "b": case "c": case "d": case "e": //Checkpoints too
case "u": case "n": case "h": case "j": case "l": //Teleport-out towers included!
//Plant Seed here
$seed[$index][0] = $x;
$seed[$index][1] = $y;
//Save our starting position.
$seed[$index][2] = $v[2];
//Save 'PATH'
$path = $i.$path;
$seed[$index][3] = $v[3].$path;
$path = '';
//Been there, done that.
$mapMatrix[$y][$x] = null;
//Move index
$index++;
break;
}
}
//Running out of seeds?
if (count($seed) < 2) {
$r['blocked'] = true;
$r['path'] = $seed[$key][3].$i;
$r['end'] = "$x,$y";
return $r;
}
//Lets not try this again.
unset($seed[$key]);
}
} while ( 1);
echo "Ran outa seeds.<br />";
print_r($seed);
return $mapMatrix;
}
/* UNUSED FUNCTION
function GetTile($mapMatrix, $id)
{
//Returns the location in $mapMatrix indicated by $id
$toReturn = &$mapMatrix [ (int)($id / $mapMatrix[0][0]) ]
[ $id % $mapMatrix[0][0] ];
return $toReturn;
}
*/
//Routes a path through all checkpoints and teleports, returning an array.
// [path] path-string. [blocked] boolean, [moves] int.
function routePath($mygrid, $start = '') {
//== This should grab the start positions by scaning the map.
if ($start == '') {
for ($i = 1; $i <= $mygrid[0][1]; $i++) {
$start .= "0,$i.";
}
}
//Checkpoint names
$cpnames = Array("a", "b", "c", "d", "e");
//Get the amount of checkpoints on this map.
$cpcount = $mygrid[0][2];
//Add the existing checkpoints to targets.
for($p = 0; $p < $cpcount; $p++) {
$target[] = $cpnames[$p];
}
//Always need the finish line.
$target[] = 'f';
//Assume that we're not blocked, and raise a red flag later.
$blocked = false;
//All possible teleports in play.
$tpnames = Array('t', 'm', 'g', 'i', 'k');
$tpcount = intval($mygrid[0][5] * .5);
//Add the existing checkpoints to targets.
for($p = 0; $p < $tpcount; $p++) {
$teleport[] = $tpnames[$p];
}
$teleout['t'] = 'u';
$teleout['m'] = 'n';
$teleout['g'] = 'h';
$teleout['i'] = 'j';
$teleout['k'] = 'l';
//$r['tparray'] = $teleport;
//$r['cparray'] = $target;
//Loop through all the targets.
foreach($target as $t) {
//Path from where we are, to the target.
$p = Findpath ($mygrid, $start, $t);
//It's possible to start from multiple places;
//so mark where we ended up starting from.
if ($r['start'] == '')
$r['start'] = $p['start'];
//$f = 0;
//1449
//2263
do {
//Make sure there is a teleport to search.
if (! is_array($teleport))
break 1;
//Search through the path to find first teleport hit, if any.
$pathary = str_split($p['path']);
//As per usual, it's best to assume that you've failed.
$foundtele = false;
//Search through pathary and compare all existing teleports.
foreach ($pathary as $position => $char) {
foreach ($teleport as $ktel => $port) {
//$r['z'] .= ';'.$f++;
if ($port == $char) {
//Disable teleport
//$teleport[$ktel] = '';
unset($teleport[$ktel]);
//$teleport = array_values($teleport);
//We found it
$foundtele = true;
break 2;
}
}
}
if ($foundtele == false) {
break 1;
}
$outchar = $teleout[$port];
if ($position === false) continue;
//if ($teleactive[$port] === false) continue;
//Where is the tele $out location.
$x = Findpath($mygrid, $start, $outchar);
$out = $x['end'];
if ($x['blocked']) $blocked = true; //Optional?
//New path starting from our out-location.
$z = Findpath($mygrid, $out, $t);
if ($z['blocked']) $blocked = true; //Optional?
//Deactivate teleport
$teleactive[$port] = false;
//$r['debug'] = "TPD: $port";
//Apply modified path, and warp-cordinates.
//123 _Tele_ 222
//123
$p['path'] = substr($p['path'], 0, $position + 1);
//123
//123 U CORDS U
$p['path'] .= $outchar.$out.$outchar;
//123 U CORDS U
//123 U CORDS U 2322
$p['path'] .= $z['path'];
$movesoffset -= countmoves($out);
//}
} while ($foundtele);
//$start = explode(",", $p['end']);
$start = $p['end'];
if ($p['blocked']) $blocked = true;
$tpath .= $t.$p['path'].'r';
}
$r['blocked'] = $blocked;
$r['path'] = $tpath;
//$moves = count_chars($tpath, 0);
$moves = countmoves($tpath);
$moves += $movesoffset;
$r['moves'] = $moves;
//$r['moves'] = print_r($moves);
return $r;
}
//For use with routepath;
function cmp($a, $b) {
if ($a === false) $a = 10000;
if ($b === false) $b = 10000;
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
//For use with routepath;
function countmoves($path) {
$moves = substr_count($path, '1');
$moves += substr_count($path, '2');
$moves += substr_count($path, '3');
$moves += substr_count($path, '4');
return $moves;
}
//Returns a random selection of one of the arguements.
function weight() {
$weights = func_get_args();
return $weights[rand(0, (count($weights) -1))];
}
?>
|