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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
|
var ready_done = false,
ready = function() {
if (ready_done)
return;
ready_done = true;
loadSol();
if (isChallenge)
challengeLoad();
}
if (document.readyState === 'complete')
ready();
else if (document.addEventListener) { // gecko, webkit, opera, IE 9
document.addEventListener("DOMContentLoaded", ready, false);
window.addEventListener("load", ready, false);
}
else if (document.attachEvent) { // IE 8-
document.attachEvent("onreadystatechange", ready);
window.attachEvent("onload", ready);
}
var wallColor = false;
var wallEmblem = false;
var wallOrientation = 0;
var isChallenge = false;
var isTutorial = false;
var solution = new Array();
var blocks = new Array();
var count = new Array();
var mapdata = new Array();
var mapjson = new Array();
var htmlnotification = '';
var jsonmapdata = new Object;
//var jsonmapdata.solutions = new Array();
var mapType; // 1 = simple, 2 = normal, ...; used for mixpanel tracking
var pressedGoTime = 0;
function loadSol(sol, moves) {
if (sol == null)
if (document.getElementById('mapsol') != undefined)
sol = document.getElementById('mapsol').innerHTML;
if (sol) {
tmp = sol.split(':');
position = tmp[1].split('.');
mapid = tmp[0];
clearwalls(mapid);
for(var i in position) {
if (document.getElementById(mapid+','+position[i]) != undefined) {
object = document.getElementById(mapid+','+position[i]);
grid_click(object);
}
}
}
if (moves && mapid) {
updateDsp(mapid, 'dspCount', moves+ " moves");
}
}
function showNotification(html) {
var div = document.createElement('div');
var pref = '<div class="notification" align="center">';
pref += '<div class="notification_close"><a href="javascript:" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);">[Close]</a> </div> ';
pref += '<div style="overflow:auto;height:295px;">';
var suff = '</div></div>';
//var suff = '<button onclick="">'
//suff += 'Close</button></div>';
div.innerHTML = pref+html+suff;
document.body.appendChild(div.firstChild);
}
function changeWallColor(newColor) {
playerWallColor = newColor;
loadSol(null);
}
function changeWallEmblem(newEmblem) {
playerWallEmblem = newEmblem;
loadSol(null);
}
function linkEmblem(emblem, orientation) {
orientation = orientation - 0;
var url = 'images/marks/';
if (orientation == 0) return url+emblem;
return url+'rotate.php?r='+orientation+'&emblem='+emblem;
}
function setWallStyle(playerObject) {
if (typeof playerObject !== 'object') return;
wallColor = playerObject.wallColor;
wallEmblem = playerObject.wallEmblem;
wallOrientation = playerObject.wallOrientation;
}
function grid_click(obj) {
//Prepare data
tmp = obj.id.split(',');
mapid = tmp[0] - 0;
y = tmp[1];
x = tmp[2];
//The users solution - prepare it if it's not started
if (solution[mapid] == undefined) {
getmapdata(mapid);
}
//Is this placing a wall, or removing one?
var tmp = obj.id;
childdiv = document.getElementById('child_'+tmp);
if (obj.cv) {
//Removing a wall
obj.cv = false;
//Remove Customized Background Color & Image
obj.style.backgroundColor = '';
obj.style.backgroundImage = '';
obj.setAttribute("class", "o");
childdiv.setAttribute("class", "child");
blocks[mapid]++;
//Remove wall
solution[mapid] = solution[mapid].replace('.'+y+','+x+'.', '.');
} else {
//Placing a wall
if (blocks[mapid] < 1) {
updateDsp(mapid, 'dspWalls', "OUT!");
return;
}
obj.cv = true;
//Color goes on the bottom, Parent.
//Then the chosen emblem in the Parent.
// Then the emblem and color are smoothed with the faceted tile on top.
//childdiv.removeAttribute("class");
childdiv.setAttribute("class", "child w");
if (wallColor == false) setWallStyle(userObj);
obj.style.backgroundColor = wallColor;
if (wallEmblem) {
obj.style.backgroundImage="url("+linkEmblem(wallEmblem, wallOrientation)+")";
}
//Add Wall
solution[mapid] += y+','+x+'.';
blocks[mapid]--;
}
//document.getElementById('blocksdisplay').innerHTML = "<b>"+blocks[mapid]+"</b>";
if (isChallenge == true) {
challengeWall(mapid);
}
updateDsp(mapid, 'dspWalls', blocks[mapid]+" walls");
//document.getElementById(mapid+',dspWalls').innerHTML = " "+blocks[mapid]+" walls";
}
function updateDsp(mapid, element, data) {
//if (mapdata[mapid] == undefined)
// return;
//if (mapdata[mapid].example != true) {
if (document.getElementById(mapid+','+element) != undefined) {
handle = document.getElementById(mapid+','+element);
handle.innerHTML = data;
}
//}
}
function getmapdata(mapid) {
//console.log('getting data for mapID', mapid);
if (typeof(mapdata[mapid]) != 'object')
mapdata[mapid] = decryptJSON(jsonmapdata[mapid]);
blocks[mapid] = mapdata[mapid].walls;
solution[mapid] = '.';
updateDsp(mapid, 'dspWalls', blocks[mapid]+" walls");
}
function doSend(mapid) {
if (solution[mapid] == undefined) {
getmapdata(mapid);
}
pressedGoTime = new Date().getTime();
reqstr = "isChallenge="+isChallenge
reqstr += "&r=getpath"
reqstr += "&mapcode="+mapdata[mapid].code;
reqstr += "&mapid="+mapid;
reqstr += "&solution="+solution[mapid];
ajax.requestFile = "do.php?"+reqstr; //prepare strdata
ajax.onCompletion = request_path_done; // specify function to be executed on response
ajax.runAJAX();
}
function requestSol(mapID) {
ajax.requestFile = "do.php?r=getsol&mapID="+mapID; //prepare strdata
ajax.onCompletion = requestSolDone; // specify function to be executed on response
ajax.runAJAX();
}
function requestChallengeSolution(mapID, challengeID) {
ajax.requestFile = "do.php?r=getChallengeSolution&mapID="+mapID+'&challengeID='+challengeID; //prepare strdata
ajax.onCompletion = requestSolDone; // specify function to be executed on response
ajax.runAJAX();
}
function requestSolDone() {
var JO;
JO = decryptJSON(ajax.response);
if (JO.solution == 'undefined')
return;
//clearwalls(JO.mapid);
loadSol(JO.mapid + ":" + JO.solution, JO.moves);
}
function clearwalls(mapid) {
if (solution[mapid] == undefined) return;
walls = solution[mapid].split('.');
for(var i in walls) {
tmp = walls[i].split(',');
eid = mapid+','+tmp[0]+','+tmp[1];
if (document.getElementById(eid) != undefined) {
obj = document.getElementById(eid);
obj.cv = false;
childdiv = document.getElementById('child_'+obj.id);
//Reset childdiv to it's default.
childdiv.removeAttribute("class");
childdiv.setAttribute("class", "child");
//return the td obj back to it's default.
obj.style.backgroundColor = '';
obj.style.backgroundImage = '';
obj.setAttribute("class", "o");
}
}
solution[mapid] = undefined;
getmapdata(mapid);
}
//TODO:An undo button rather than confirm..
function resetwalls(mapid) {
answer = confirm("Remove walls and start fresh?");
if (answer) {
clearwalls(mapid);
}
}
function request_path_done() {
var JO = decryptJSON(ajax.response);
var mapid = JO.mapid;
mapjson[mapid] = JO;
var speedbox = document.getElementById(mapid+',speed'),
speed = speedbox.options[speedbox.selectedIndex].text,
mute = !checkSound(mapid);
nowTime = new Date().getTime();
var responseTime = nowTime - pressedGoTime;
mixpanel.track('click go', {
'speed': speed,
'mute': mute,
'mapid': mapid,
'type': mapType,
'response time': responseTime
});
for(var i in JO.error)
console.error('\n JO error ' + JO.error[i]);
if (JO.blocked) {
alert("The path is blocked!");
return;
}
var disptext = "Record: "+JO.best+" by "+JO.bestby;
if (isChallenge)
disptext = '';
updateDsp(JO.mapid, 'dspID', disptext);
mapdata[mapid].moveCount = new Object;
mapdata[mapid].usedTiles = new Array();
mapdata[mapid].restoreTiles = new Array();
mapdata[mapid].pathColor = new Object;
mapdata[mapid].pathsPending = JO.path.length;
mapdata[mapid].isMultiPath = (JO.path.length > 1);
for(i in JO.path) {
mapdata[mapid].moveCount[i] = 0;
mapdata[mapid].pathColor[i] = '#ffffff';
animatePath(JO.path[i].path, mapid, JO.path[i].start, i);
}
}
function decryptJSON(text) {
if (typeof(text) == 'undefined') return false;
var JO;
if (typeof(JSON) == 'undefined') {
JO = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
} else {
JO = JSON.parse(text);
}
return JO;
}
function animatePath(path, mapid, start, pathNumber) {
var tmp = start.split(',');
var y = tmp[0];
var x = tmp[1];
var p = path;
//Prepare the path
var c = p.substring(0, 1);
p = p.substring(1);
document.getElementById(mapid+',btn').disabled = true;
doanimate(x, y, p, c, mapid, pathNumber);
}
function animatePathDone(mapid) {
document.getElementById(mapid+',btn').disabled = false;
if (isChallenge == true) {
challengeGo(mapid);
}
if (typeof(currentPage) == "object") {
scoresRequestPage(mapid, currentPage[mapid]);
}
//Mark off challenges
//TODO: This hack is stupidd :(
if(isChallenge && isTutorial == false)
{
for(var i = 0; i < mapjson[mapid].completedChallenges.length; i++)
{
var challengeId = mapjson[mapid].completedChallenges[i];
var handle = document.getElementById("challenge_id_" + challengeId);
if (handle.className.indexOf('challenge_complete') < 0) {
handle.className = "challenge_complete";
flashelement("challenge_id_" + challengeId, 4);
}
}
}
}
function checkSound(mapid) {
if (getCookie('pref_mute') == 'true') {
return false;
}
if (typeof(soundManager) != 'object') {
return false;
}
return true;
}
function doanimate(x, y, p, c, mapid, pathNumber) {
//x, y position
//p path string being trunicated.
//c current target code.
//t next target code.
t = p.substring(0, 1);
//Animate current square, and move to next one.
if (count[mapid] == undefined) {
count[mapid] = 0;
}
//Set the color for the first target.
if (count[mapid] == 0 && !(c > 0))
mapdata[mapid].pathColor[pathNumber] = targetColor(c);
//Display movecount
if (mapdata[mapid].moveCount[1] > 0 && mapdata[mapid].moveCount[0] > 0) {
var colorScores = '<span class="green">' + mapdata[mapid].moveCount[0] + '</span> + ';
colorScores += '<span class="red">' + mapdata[mapid].moveCount[1] + '</span> = ' + count[mapid] + " moves";
updateDsp(mapid, 'dspCount', colorScores);
//updateDsp(mapid, 'dspCount', '<span class="green">' + mapdata[mapid].moveCount[0] + ' + ' + mapdata[mapid].moveCount[1] + ' = ' + count[mapid] + " moves");
} else {
updateDsp(mapid, 'dspCount', count[mapid]+ " moves");
}
//document.getElementById(mapid+',dspCount').innerHTML = count[mapid]+ " moves";
//Get a handle on the element.
eid = mapid+','+x+','+y;
//Verify.
if (document.getElementById(eid) == undefined) {
console.error("Path exited field...?");
animatePathDone(mapid);
return;
}
handle = document.getElementById(eid);
//Animate the first square in a path.
if (count[mapid] == 0 && !(c > 0)) {
count[mapid]--;
mapdata[mapid].moveCount[pathNumber]--;
c = t;
if (!(c > 0))
c = '2';
}
switch(c) {
//The path is moving to a new position
case '1': //1 - Up
case '2': //2 - Right
case '3': //3 - Down
case '4': //4 - Left
//Track move count
count[mapid]++;
mapdata[mapid].moveCount[pathNumber]++;
//Notify users on score levels;
switch(count[mapid]) {
case 100: case 200: case 300: case 400:
case 500: case 600: case 700: case 800:
case 900: case 1000:
if (checkSound(mapid)) {
soundManager.setVolume('charm', 40);
soundManager.setPan('charm', 75)
soundManager.play('charm');
}
//Flash
flashelement(mapid+',dspCount', 4);
break;
}
var childID = 'child_'+handle.id;
childdiv = document.getElementById(childID);
if (childdiv.className.indexOf('w') < 0) {
childdiv.setAttribute('class', 'transition path'+pathNumber+'-'+c);
handle.style.backgroundColor = mapdata[mapid].pathColor[pathNumber];
var string = "if (document.getElementById('"+'child_'+eid+"').className == 'transition path"+pathNumber+'-'+c+"')";
string += "document.getElementById('"+'child_'+eid+"').setAttribute('class', 'child');";
setTimeout(string, 855);
//Maintain disabled appearnce of checkpoints
if (handle.pressed == true) {
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '#dddddd';", 865);
} else {
string = "if (document.getElementById('"+childID+"').className.indexOf('w') < 0) ";
string += "document.getElementById('"+eid+"').style.backgroundColor = '';";
setTimeout(string, 865);
}
}
break;
//Teleports
case 't': case 'm': case 'g': case 'i': case 'k':
//Outs
case 'u': case 'n': case 'h': case 'j': case 'l':
case 'r':
if (mapdata[mapid].isMultiPath == false) {
handle.style.backgroundColor = '#dddddd';
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '#dddddd';", 865);
handle.pressed = true;
} else {
if (contains(mapdata[mapid].usedTiles, eid)) {
handle.style.backgroundColor = '#dddddd';
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '#dddddd';", 865);
handle.pressed = true;
} else {
//mapdata[mapid].usedTiles.push(eid);
//break;
}
}
if (contains(mapdata[mapid].usedTiles, eid) == false)
mapdata[mapid].usedTiles.push(eid);
//mapdata[mapid].restoreTiles.push("document.getElementById('"+eid+"').style.backgroundColor = '';");
//mapdata[mapid].restoreTiles.push("document.getElementById('"+eid+"').pressed = false;");
//alert(mapdata[mapid].pathColor[pathNumber]);
//break;
}
//Sound effects
if (t == 'r') {
if (checkSound(mapid)) {
soundManager.setVolume('bling', 40);
soundManager.setPan('bling', -75)
soundManager.setVolume('blingb', 40);
soundManager.setPan('blingb', 75)
if (pathNumber == 0)
soundManager.play('bling');
if (pathNumber == 1)
soundManager.play('blingb');
}
}
//Done messing with current target
//Now take pre-action regarding the next tile.
//Speaking of the next tile - does it exist?
//End of the line?
if (t == '') {
mapdata[mapid].pathsPending--;
//console.log('path pending', mapdata[mapid].pathsPending);
if (mapdata[mapid].pathsPending > 0)
return;
//console.log('path pending complete', mapdata[mapid].pathsPending);
//Did we beat or tie any records?
//Saw someone do this, thought it was clever.
//Switch for range result.
var disptext = ""
var improvedScore = (count[mapid] > mapjson[mapid].mybest && mapjson[mapid].mybest != "0");
switch (true) {
case (count[mapid] > mapjson[mapid].best):
disptext = "Beat "+mapjson[mapid].bestby+"'s record of "+mapjson[mapid].best+" with "+count[mapid]+"!";
break;
case (count[mapid] == mapjson[mapid].best):
disptext = "Tied "+mapjson[mapid].bestby+"'s record of "+mapjson[mapid].best;
break;
case (mapjson[mapid].mybest == "0"):
disptext = "You scored "+count[mapid]+"!";
break;
case (count[mapid] > mapjson[mapid].mybest):
disptext = "Improved score "+mapjson[mapid].mybest+ " to "+count[mapid];
break;
case (count[mapid] == mapjson[mapid].mybest):
disptext = "Tied personal best of "+count[mapid];
break;
case (count[mapid] < mapjson[mapid].mybest):
disptext = "You got "+count[mapid]+". Your best is "+mapjson[mapid].mybest;
break;
}
//if anything worth mentioning happend let them know.
if (disptext != "") {
if (checkSound(mapid) && improvedScore) {
soundManager.setVolume('charm', 50);
soundManager.setVolume('sc', 50);
soundManager.play('charm');
soundManager.play('sc');
}
updateDsp(mapid, 'dspID', disptext);
flashelement(mapid+',dspID', 8, "#FF3377");
}
//This is the end, lets reset stuff to defaults.
count[mapid] = 0;
mapdata[mapid].pathColor[pathNumber] = '#ffffff';
//Bring the color back to our checkpoints/teleports.
var eid;
for(var i in mapdata[mapid].usedTiles) {
//eval(mapdata[mapid].restoreTiles[i]);
//setTimeout((mapdata[mapid].restoreTiles[i]), 2000);
eid = mapdata[mapid].usedTiles[i];
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '';", 2500);
setTimeout("document.getElementById('"+eid+"').pressed = false;" , 2500);
}
//Clear
mapdata[mapid].usedTiles = new Array();
//We're done,
animatePathDone(mapid);
return;
}
//The next tile exists, how fast should we get there?
rs = 84;
//How fast should we be going?
selectbox = document.getElementById(mapid+',speed');
var selectedSpeed = selectbox.options[selectbox.selectedIndex].value;
switch (selectedSpeed) {
case '1':
rs =180;
break;
case '2':
rs =94;
break;
case '3':
rs =44;
break;
case '4':
rs =22;
break;
case '5':
rs =0;
break;
}
//The next path code.
switch(t) {
//Are we just moving someplace?
case '1':
x--;
break; //1 - Up
case '2':
y++;
break; //2 - Right
case '3':
x++;
break; //3 - Down
case '4':
y--;
break; //4 - Left
//Special codes within the path.
//Did we aquire a target?
//Checkpoint targets:
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
rs = rs + 410;
if (selectedSpeed <= 2) rs = rs + 200;
mapdata[mapid].pathColor[pathNumber] = targetColor(t);
break;
//Hey, we've ran into a teleport.
case 'u': //tp1
case 'n': //tp2
case 'h': //tp3
case 'j': //tp4
case 'l': //tp5
//Get teleport coords
tmp = p.split(t);
loc = tmp[1].split(',');
y = loc[0];
x = loc[1];
//Flash teleport-out
//Teleport Element ID
tpEid = mapid+','+x+','+y;
if (checkSound(mapid)) {
soundManager.setVolume('ufoblip', 30);
if (pathNumber == 0)
soundManager.setPan('ufoblip', 70);
else
soundManager.setPan('ufoblip', -70);
soundManager.play('ufoblip');
}
document.getElementById(tpEid).style.backgroundColor='';
flashelement(tpEid, 8, mapdata[mapid].pathColor[pathNumber]);
//The path once teleported - and an r to indicate to gray the teleport-out too.
p = 'q'+tmp[2];
//Slow down
rs = rs + 1200;
break;
}
//Remove move from p
p = p.substring(1);
//rs = (10 * p.length) + 40;
// if (mapdata[mapid].moveCount[1] < mapdata[mapid].moveCount[pathNumber] - 2
// || mapdata[mapid].moveCount[0] < mapdata[mapid].moveCount[pathNumber] - 2)
// rs = rs + 100;
if (count[mapid] % 2 == 1 && rs == 0) {
doanimate(x,y,p,t,mapid,pathNumber);
} else {
setTimeout("doanimate("+x+","+y+",'"+p+"','"+t+"','"+mapid+"','"+pathNumber+"')",rs);
}
}
function targetColor(target) {
var r = '#ccc';
switch(target) {
case 'a':
r = '#F777FF';
break;
case 'b':
r = '#FFFF11';
break;
case 'c':
r = '#FF4466';
break;
case 'd':
r = '#ff9911';
break;
case 'e':
r = '#00FFFF';
break;
case 'f':
r = '#ccc';
}
return r;
}
function flashelement(eid, times, color, speed) {
if (typeof(document.getElementById(eid)) == 'undefined') return;
var elementToFlash = document.getElementById(eid);
if (elementToFlash.isBeingFlashed == true) return;
elementToFlash.isBeingFlashed = true;
if (!color) {
color = "#FFFF44";
}
if (!speed) {
speed = 220;
}
speedon = speed * .5;
var currentclass = elementToFlash.className;
if (elementToFlash.classOrigName != undefined)
currentclass = elementToFlash.classOrigName;
var currentColor = elementToFlash.style.backgroundColor;
elementToFlash.className='no_transition '+currentclass;
elementToFlash.style.backgroundColor = '#000000';
for (var i=0; i<times; i++) {
//Flash bright
setTimeout("document.getElementById('"+eid+"').style.color = '#000000'", i*speed);
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '"+color+"'", i*speed);
//Flash out
setTimeout("document.getElementById('"+eid+"').style.color = ''", (i*speed) + speedon);
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = ''", (i*speed) + speedon);
}
setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '"+currentColor+"'", (i*speed) + 200);
setTimeout("document.getElementById('"+eid+"').isBeingFlashed = false", (i*speed) + 220);
}
function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
//Shows a solution temporarly
function useSolution(mapid, inputSolution, moves, tempWallColor, tempWallEmblem, tempWallOrientation, solutionID) {
$('.solutionSelected').removeClass('solutionSelected');
$('#solution_'+solutionID).addClass('solutionSelected');
solution[mapid] = inputSolution;
var animateA = "showTempSolution(\""+mapid+"\", \""+inputSolution+"\", \""+moves+"\", \""+tempWallColor+"\", \""+tempWallEmblem+"\", \""+tempWallOrientation+"\");";
var animateB = "showTempSolution(\""+mapid+"\", \""+inputSolution+"\", \""+moves+"\", false, false, false);";
//TODO: Sticky colors for the placed walls by the user would be cool.
//var animateC = "wallColor = false; wallEmblem = false;";
setTimeout(animateA, 50);
setTimeout(animateB, 150);
setTimeout(animateA, 250);
setTimeout(animateB, 350);
setTimeout(animateA, 450);
setTimeout(animateB, 550);
}
//Shows a solution for temporary use, see 'RestoreSolution'
function showTempSolution(mapid, tempSolution, moves, tempWallColor, tempWallEmblem, tempWallOrientation) {
//console.log('showTempSolution', mapid, solution, moves, tempWallColor, tempWallEmblem);
var savedSolution = '';
if (typeof tempSolution == 'undefined') tempSolution = '';
if (typeof solution[mapid] !== 'undefined') {
savedSolution = solution[mapid];
}
wallColor = tempWallColor;
wallEmblem = tempWallEmblem;
wallOrientation = tempWallOrientation;
position = tempSolution.split('.');
clearwalls(mapid);
for(var i in position) {
if (document.getElementById(mapid+','+position[i]) != undefined) {
object = document.getElementById(mapid+','+position[i]);
grid_click(object);
}
}
if (moves && mapid) {
updateDsp(mapid, 'dspCount', moves+ " moves");
}
mapdata[mapid].savedSolution = savedSolution;
}
//Restores a solution after a showTempSolution
function restoreSolution(mapid) {
showTempSolution(mapid, mapdata[mapid].savedSolution, 0, false, false);
}
function displayMap(mapid, divID, goalSize, solution, moves, challengeMap, isThumb) {
var stringURL = 'a/map/'+mapid+".js";
if (challengeMap == true) stringURL = 'a/challenge/'+mapid+".js";
$.ajax({
type: "GET",
url: stringURL,
dataType: 'json',
cache: true,
data: '',
//TODO: Better fail option?
fail: (function() { console.log("FAIL Map Download"); }),
complete: function(data) {
if (isThumb) {
$("#"+divID).html(mapThumbnailHTML(decryptJSON(data.responseText), goalSize)).fadeIn('fast');
} else {
$("#"+divID).html(mapAsHTML(decryptJSON(data.responseText), goalSize)).fadeIn('fast');
//$("#"+divID).html(mapAsHTML(decryptJSON(data.responseText), goalSize)).show();
mapdata[mapid].savedSolution = solution;
restoreSolution(mapid);
}
}
});
}
var Tile = {
"o" : "Open",
"s" : "Start",
"f" : "Finish",
"c" : "Checkpoint",
"r" : "Rock",
"t" : "Teleport In",
"u" : "Teleport Out",
"p" : "Unbuildable",
"x" : "Single-Path-Rock"};
//Map as object. If target width is NULL or False, default width is used.
function mapAsHTML(map, targetWidth, mapEditor) {
map.mapid = map.ID;
//console.log("MapID:", map.mapid);
//console.log("MapObj", map);
mapdata[map.ID] = map;
//Map bigger than target width?
if (!targetWidth || (map.width * 35) <= targetWidth)
{
//Use standard size.
targetWidth = (map.width * 35);
}
var scale = map.width / targetWidth;
//alert(scale);
var width = parseInt(map.width / scale);
var height = parseInt(map.height / scale);
var tileWidth = parseInt(width / map.width);
width = tileWidth * map.width;
var mapgrid = '';
mapgrid += '<div style="clear:both;"></div><div class="map playable" style="width:'+width+'px; height:'+height+'px">';
for (var y in map.tiles) {
for (var x in map.tiles[y]) {
var type = map.tiles[y][x][0];
var value = map.tiles[y][x][1];
if (!value) value = '';
//TODO: If we want to change this line to something that's not retarded _
// we'll need to do a TON of other work... See Blossom "Implement mapclass"
var oldy = (y*1)+1;
var idHandle = map.ID+','+oldy+','+x;
// GOAL WOULD BE THIS LINE INSTEAD.. As it's not retarded.
//var idHandle = map.ID+','+x+','+y;
//oldy is used for Position too... for now
if (mapEditor == true) {
mapgrid += "<div style='float:left; width:"+tileWidth+"px; height:"+tileWidth+"px; ' class='mapcell "+type+value+"' title='Position: "+x+","+oldy+"' id='"+idHandle+"' onMouseOver='mapEditOver(this)' onMouseDown='mapEditClick(this)' >";
mapgrid += "<div id='child_"+idHandle+"' class='child'></div></div>";
} else if (type == 'o') {
mapgrid += "<div style='float:left; width:"+tileWidth+"px; height:"+tileWidth+"px; ' class='mapcell "+type+value+"' title='Position: "+x+","+oldy+"' id='"+idHandle+"' onClick='grid_click(this)' >";
mapgrid += "<div id='child_"+idHandle+"' class='child'></div></div>";
} else {
mapgrid += "<div style='float:left; width:"+tileWidth+"px; height:"+tileWidth+"px; ' class='mapcell "+type+value+"' title='"+Tile[type]+" "+value+" On: "+x+","+oldy+"' id='"+idHandle+"' >";
mapgrid += "<div id='child_"+idHandle+"' class='child'></div></div>";
}
}
}
mapgrid += '</div><div style="clear:both"></div>';
if (mapEditor == true) return mapgrid;
var r = '';
//TODO: Track down where that 1 pixel is comingfrom, width-1 is a hack.
r += "<div id='"+map.ID+",outer' class='grid_outer' style='width:"+(width)+"px;height:"+(height+45)+"px;'>";
r += " <div class='grid_dsp_left' style='width:60%;'>";
r += " <div id='"+map.ID+",dspID' title='MapID: "+map.ID+"'>";
r += " MapID: "+map.ID;
r += " </div>";
r += " </div>";
r += " <div id='"+map.ID+",dsptr' class='grid_dsp_right' style='width:38%;'>";
r += " <span id='"+map.ID+",dspWalls' class='grid_dsp_data'> ";
r += " "+map.walls+" walls";
r += " </span>";
r += " <span>";
r += " ( <a href='javascript:resetwalls("+map.ID+")'>Reset</a> )";
r += " </span>";
r += " </div>";
r += mapgrid;
r += " <div id='"+map.ID+",dspbl' class='grid_dsp_left' style='width:60%;'> ";
r += " <input id='"+map.ID+",btn' type='button' onclick='doSend("+map.ID+")' value='Go!' />";
r += " Speed:";
r += getSpeedOptions(map.ID);
r += " </div>";
r += " <div class='grid_dsp_mid' style='width:5%;'>";
r += getMuteOption(map.ID);
r += " </div>";
r += " <div id='"+map.ID+",dspbr' class='grid_dsp_right' style='width:34%;'> ";
r += " <div id='"+map.ID+",dspCount' class='grid_dsp_data'> ";
r += " 0 moves";
r += " </div>";
r += " </div>";
r += "</div>";
return r;
}
function mapThumbnailHTML(map, targetWidth, isActive) {
if (!targetWidth) targetWidth = 120;
var scale = map.width / targetWidth;
var width = parseInt(map.width / scale);
var height = parseInt(map.height / scale);
var tileWidth = parseInt(width / map.width);
width = tileWidth * map.width;
height = tileWidth * map.height;
var mapgrid = '';
var r = '';
r += map.name;
mapgrid += '<div class="map" style="width:'+width+'px; height:'+height+'px">';
for (var y in map.tiles) {
for (var x in map.tiles[y]) {
var type = map.tiles[y][x][0];
var value = map.tiles[y][x][1];
if (!value) value = '';
mapgrid += "<div style='float:left; width:"+tileWidth+"px; height:"+tileWidth+"px; ' class='mapcell "+type+value+"'>";
mapgrid += "</div>";
}
}
mapgrid += '</div>';
r += mapgrid;
return r;
}
function setMute(value)
{
var value = getCookie('pref_mute');
$('.mapMute').removeClass("mapMute_"+value);
if (value == 'true') {
value = 'false';
soundManager.setVolume('pit', 20);
soundManager.setPan('pit', -60)
soundManager.play('pit');
} else {
value = 'true';
}
savePref('mute', value);
$('.mapMute').addClass("mapMute_"+value);
}
function getMuteOption(mapID) {
var r = '';
var muted = 'false';
if (getCookie('pref_mute') == 'true') {
muted = "true";
}
r += "<a title='Mute sound?' class='mapMute mapMute_"+muted+" unselectable' href='javascript:setMute()' id='mapMute'/></a>";
return r;
}
function getSpeedOptions(mapID) {
var listObj = new Object;
var selectedSpeed = 2;
if (getCookie('pref_speed')) {
selectedSpeed = getCookie('pref_speed');
}
listObj[1] = 'Slow';
listObj[2] = 'Med';
listObj[3] = 'Fast';
listObj[4] = 'Ultra';
if (userObj.hasInsaneSpeed) listObj[5] = 'Insane';
var r = '';
r += " <select onChange='savePref(\"speed\", this.value)' id='"+mapID+",speed'>";
for (var i in listObj) {
r += "<option value='"+i+"'";
if (i == selectedSpeed) r += "selected='selected'";
r += ">"+listObj[i]+"</option>";
}
r += " </select>";
return r;
}
function savePref(pref, value) {
setCookie('pref_'+pref, value, 9999);
}
//Cookie functions from w3schools.com
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
return "";
}
|