forked from github/game-off-2012
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.htm
More file actions
864 lines (767 loc) · 25.9 KB
/
Copy pathindex.htm
File metadata and controls
864 lines (767 loc) · 25.9 KB
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
<!DOCTYPE html>
<html>
<head>
<title>GitHub Game Off 2012</title>
<script type="text/javascript">
var cellWidth = 16;
var cellHeight = 16;
var cellsH = 16;
var cellsV = 16;
var borderWidth = 16;
function getWidth() {
return cellWidth * cellsH;
}
function getHeight() {
return cellHeight * cellsV;
}
var levels = ["Tutorial_1", "Tutorial_2", "Tutorial_3", "Tutorial_4"];
</script>
<link rel="stylesheet/less" type="text/less" href="style.less">
<script type="text/javascript" src="less.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div class="overlay" id="alert" style="display: none">
<div id="content">
<span id="message"></span><br />
<button onclick="hideAlert()">Ok</button>
</div>
</div>
<div class="overlay" id="confirm" style="display: none">
<div id="content">
<span id="message"></span><br />
<span id="buttons"></span>
</div>
</div>
<div class="overlay" id="prompt" style="display: none">
<div id="content">
<span id="message"></span><br />
<input type="text" id="input"><br />
<button id="ok" onclick="hidePrompt(true)">Ok</button><button id="cancel" onclick="hidePrompt(false)">Cancel</button>
</div>
</div>
<script type="text/javascript">
var onHide;
function showAlert(msg, callback) {
$("#alert #message").text(msg);
$("#alert").show();
onHide = callback;
}
function hideAlert() {
$("#alert").hide();
if(onHide)
onHide();
}
function showConfirm(msg, callback, buttons) {
if(!callback) return;
onHide = callback;
var b = buttons ? buttons : ["Yes", "No"];
var t = "";
for(var i = 0; i < b.length; i++)
t += '<button onclick="hideConfirm(' + i + ')">' + b[i] + '</button>';
$("#confirm #buttons").html(t);
$("#confirm #message").text(msg);
$("#confirm").show();
}
function hideConfirm(n) {
$("#confirm").hide();
onHide(n);
}
function showPrompt(msg, def, callback) {
if(!callback) return;
onHide = callback;
$("#prompt #message").text(msg);
$("#prompt #input").val(def ? def : "");
$("#prompt").show();
$("#prompt #input").select();
}
$("#prompt #input").keyup(function(event){
if(event.keyCode == 13)
$("#prompt #ok").click();
});
function hidePrompt(complete) {
$("#prompt").hide();
if(complete) onHide($("#prompt #input").val());
}
</script>
<div id="container">
<div id="arena">
<div id="border-tl"></div><div id="border-t"></div><div id="border-tr"></div>
<div id="border-l"></div><canvas id="gameCanvas"></canvas><div id="border-r"></div>
<div id="border-bl"></div><div id="border-b"></div><div id="border-br"></div>
</div>
<div id="controls">
Functions: <select id="functions" name="functions">
<option value="main">main</option>
</select><br />
<button id="createFunction" onClick="createFunction()">Create Function</button>
<button id="deleteFunction" onCLick="deleteFunction()" disabled="true">Delete Function</button><br />
Instructions:<br />
<select id="code" multiple>
</select><br />
<div style="font-style: italic; font-size: 12px; margin: 0px; padding: 0px; text-align: center;">
To insert code before a command, select the command<br />
To append code, <span class="fakeLink" onClick="$('#code').val('')">deselect</span> everything
</div>
<ul id="commands">
Move:<br />
<li><button id="moveLeft" onClick="addCommand(1)"><img alt="Move Left" src="images/cmd_left.png" /></button></li>
<li><button id="moveRight" onClick="addCommand(2)"><img alt="Move Right" src="images/cmd_right.png" /></button></li>
<li><button id="moveUp" onClick="addCommand(3)"><img alt="Move Up" src="images/cmd_up.png" /></button></li>
<li><button id="moveDown" onClick="addCommand(4)"><img alt="Move Down" src="images/cmd_down.png" /></button></li><br />
Branch:<br />
<li><button id="branchLeft" onClick="addBranchCommand(5)"><img alt="Branch Left" src="images/cmd_branch_left.png" /></button></li>
<li><button id="branchRight" onClick="addBranchCommand(6)"><img alt="Branch Right" src="images/cmd_branch_right.png" /></button></li>
<li><button id="branchUp" onClick="addBranchCommand(7)"><img alt="Branch Up" src="images/cmd_branch_up.png" /></button></li>
<li><button id="branchDown" onClick="addBranchCommand(8)"><img alt="Branch Down" src="images/cmd_branch_down.png" /></button></li>
</ul>
<div id="commandsExtra">
Others:<br />
<button id="doNothing" onClick="addCommand(0)">Do Nothing</button>
<button id="deleteCommand" onClick="deleteCommands()" disabled="true">Delete Command(s)</button>
<button id="clearCommands" onClick="clearCommands()">Clear Commands</button>
</div>
</div>
<div id="mainControls">
<button id="runCode" onClick="runCode()" disabled="true">Run Code</button>
<button id="createLevel" onClick="window.open('leveleditor.htm')">Create Custom Levels</button>
<button id="loadLevel" onClick="askLoadLevel()">Load Custom Level</button>
</div>
<div id="info">
</div>
</div>
<script type="text/javascript">
var c = document.getElementById("gameCanvas");
var ctx = c.getContext("2d");
var w = c.width = getWidth(); var h = c.height = getHeight();
var mainBot;
var otherBots = new Array();
var mainBotCmds = new Array();
var functions = new Array();
var stepTimer = 0;
var ticksPerStep = 16;
var running = false;
var ranOnce = false;
var levelComplete = false;
var customLevel = false;
var mainInstruction = -1;
var otherInstructions = new Array();
var isBeingMovedTo = new Array();
var DO_NOTHING = 0;
var MOVE_LEFT = 1;
var MOVE_RIGHT = 2;
var MOVE_UP = 3;
var MOVE_DOWN = 4;
var BRANCH_LEFT = 5;
var BRANCH_RIGHT = 6;
var BRANCH_UP = 7;
var BRANCH_DOWN = 8;
var mapData;
var levelLoaded = false;
var goals;
var levers;
var doors;
var levelStart;
var MD_EMPTY = 0;
var MD_BLOCK = 1;
var MD_GOAL = 2;
var MD_LEVER = 3;
var MD_DOOR = 4;
var levelNum = 0;
var fails = new Array();
function Bot(x, y, func) {
this.x = x;
this.y = y;
this.nx = x;
this.ny = y;
this.func = func;
this.cloning = false;
this.onGoal = function() {
for(var i = 0; i < goals.length; i++)
if(this.x == goals[i].x && this.y == goals[i].y)
return true;
return false;
}
}
function botAtPos(x, y) {
if((mainBot.x == x && mainBot.y == y) || (mainBot.nx == x && mainBot.ny == y)) return true;
for(var i = 0; i < otherBots.length; i++)
if((otherBots[i].x == x && otherBots[i].y == y) || (otherBots[i].nx == x && otherBots[i].ny == y))
return true;
return false;
}
function Goal(x, y) {
this.x = x;
this.y = y;
this.isComplete = function() {
return botAtPos(this.x, this.y);
};
}
function Lever(x, y, colour) {
this.x = x;
this.y = y;
this.colour = colour;
}
function Door(x, y, colour) {
this.x = x;
this.y = y;
this.open = false;
this.colour = colour;
}
function Position(x, y) {
this.x = x;
this.y = y;
}
function Command(id, meta) {
this.id = id;
this.meta = meta;
}
var cmdText = ["Do Nothing", "Move Left", "Move Right", "Move Up", "Move Down", "Branch Left", "Branch Right", "Branch Up", "Branch Down"];
var botImg = new Image();
botImg.src = "images/bot.png";
var blockImg = new Image();
blockImg.src = "images/block.png";
var goalImg = new Image();
goalImg.src = "images/goal.png";
var leverRImg = new Image(); leverRImg.src = "images/lever_red.png";
var leverGImg = new Image(); leverGImg.src = "images/lever_green.png";
var leverBImg = new Image(); leverBImg.src = "images/lever_blue.png";
var doorRImg = new Image(); doorRImg.src = "images/door_red.png";
var doorGImg = new Image(); doorGImg.src = "images/door_green.png";
var doorBImg = new Image(); doorBImg.src = "images/door_blue.png";
function updateButtonDisables() {
$("#deleteCommand").attr('disabled', !$("#code").val());
$("#deleteFunction").attr('disabled', $("#functions").val() == "main");
$("#runCode").attr('disabled', !levelLoaded || $("#code option").length == 0 || ranOnce || running);
}
function populateCommandList() {
$("#code").html("");
var cmdList = mainBotCmds;
if($("#functions").val() != "main")
cmdList = functions[parseInt($("#functions").val().substring(1))];
var n = $("#code option").length;
for(var i = 0; i < cmdList.length; i++) {
$("#code").append($('<option>', { value: "c" + n, text : cmdText[cmdList[i].id] + (cmdList[i].meta ? " (" + cmdList[i].meta + ")" : "") }));
n++;
}
}
function createFunction(stay, callback) {
if(running) return;
showPrompt("Function name:", "", function(name) {
var validName = (name != "" && name.length < 15);
if(validName)
$("#functions option").each(function(i) {
if($(this).text() == name)
validName = false;
});
if(name != null) {
if(!validName)
showAlert("Invalid name: " + name);
else {
var n = $("#functions option").length - 1;
$("#functions").append($("<option>", { value: "f" + n, text: name}));
if(!stay) $("#functions").val("f" + n);
functions.push(new Array());
updateButtonDisables();
populateCommandList();
callback(name);
return;
}
}
updateButtonDisables();
populateCommandList();
});
}
function deleteFunction() {
if(running) return;
if($("#functions").val() != "main")
showConfirm("Are you sure?", function(id) {
if(id == 0) {
var n = parseInt($("#functions").val().substring(1));
functions.splice(n, 1);
$("#functions :selected").remove();
$("#functions option").each(function(i) {
if($(this).val() != "main")
$(this).val("f" + (i - 1));
});
}
updateButtonDisables();
});
}
function addCommand(id) {
if(running) return;
addMetaCommand(id, "");
}
function addBranchCommand(id) {
if(running) return;
var funcs = [];
$("#functions option").each(function(i) {
funcs.push($(this).text());
});
var count = funcs.length;
funcs.push("Create New Command");
funcs.push("Cancel");
showConfirm("Choose a function to branch into", function(n) {
if(n < count) {
var name = "";
if(n == 0) name = "main";
else name = $('#functions [value="f' + (n - 1) + '"]').text();
addMetaCommand(id, name);
}
if(n == count)
createFunction(true, function(name) {
addMetaCommand(id, name);
});
}, funcs);
}
function addMetaCommand(id, meta) {
if(running) return;
cmd = new Command(id, meta);
var before = -1;
if($("#code :selected").length == 1)
before = parseInt($("#code :selected").val().substring(1));
if($("#functions").val() == "main") {
if(before >= 0)
mainBotCmds.splice(before, 0, cmd);
else
mainBotCmds.push(cmd);
}
else {
var n = parseInt($("#functions").val().substring(1));
if(before >= 0)
functions[n].splice(before, 0, cmd);
else
functions[n].push(cmd);
}
populateCommandList();
updateButtonDisables();
}
function deleteCommands() {
if(running) return;
var n;
if($("#functions").val() != "main")
n = parseInt($("#functions").val().substring(1));
var toSplice = new Array();
$("#code :selected").each(function(num) {
var i = parseInt($(this).val().substring(1));
toSplice.push(i - toSplice.length);
});
$("#code :selected").remove();
$("#code option").each(function(i) {
$(this).val("c" + i);
});
for(var i = 0; i < toSplice.length; i++) {
var x = toSplice[i];
if($("#functions").val() == "main")
mainBotCmds.splice(x, 1);
else
functions[parseInt($("#functions").val().substring(1))].splice(x, 1);
};
updateButtonDisables();
}
function clearCommands() {
if(running) return;
if($("#functions").val() == "main")
mainBotCmds = [];
else
functions[parseInt($("#functions").val().substring(1))] = [];
populateCommandList();
}
$("#code").change(updateButtonDisables);
$("#functions").change(function() {
updateButtonDisables();
populateCommandList();
});
function resetCode() {
functions = new Array();
mainBotCmds = new Array();
$("#functions option").each(function(i) {
if($(this).val() != "main")
$(this).remove();
});
$("#functions").val("main");
populateCommandList();
}
function askLoadLevel() {
showPrompt("Copy level text below: ", "", function(level) {
loadCustomLevel(level);
});
}
function loadLevel() {
return loadOnlineLevel("levels/" + levels[levelNum] + ".txt");
}
function loadCustomLevel(level) {
customLevel = true;
loaded = true;
levelLoaded = false;
ranOnce = false;
levelComplete = false;
goals = new Array();
mapData = new Array();
for(var i = 0; i < cellsH; i++)
mapData.push(new Array());
var foundStart = false;
var redDoors = new Array();
var greenDoors = new Array();
var blueDoors = new Array();
var redLevers = new Array();
var greenLevers = new Array();
var blueLevers = new Array();
for(var j = 0; j < cellsV; j++)
for(var i = 0; i < cellsH; i++) {
if(j * cellsH + i < level.length) {
var c = level.charAt(j * cellsH + i);
mapData[i][j] = c == " " ? MD_EMPTY : c == "#" ? MD_BLOCK : c == "X" ? MD_GOAL : c == "r" || c == "g" || c == "b" ? MD_LEVER : c == "R" || c == "G" || c == "B" ? MD_DOOR : MD_EMPTY;
if(c == "X") goals.push(new Goal(i, j));
if(c == "R") redDoors.push(new Door(i, j, 0));
if(c == "G") greenDoors.push(new Door(i, j, 1));
if(c == "B") blueDoors.push(new Door(i, j, 2));
if(c == "r") redLevers.push(new Lever(i, j, 0));
if(c == "g") greenLevers.push(new Lever(i, j, 1));
if(c == "b") blueLevers.push(new Lever(i, j, 2));
if(c == "S")
if(foundStart) {
showAlert("Invalid level, multiple start positions found");
return;
}
else {
mainBot.x = mainBot.nx = i;
mainBot.y = mainBot.ny = j;
levelStart = new Position(i, j);
foundStart = true;
}
}
else
mapData[i][j] = MD_EMPTY;
}
levers = redLevers.concat(greenLevers, blueLevers);
doors = redDoors.concat(greenDoors, blueDoors);
if(!foundStart) {
showAlert("Invalid level, no start positions found");
return;
}
if(level.length > cellsV * cellsH)
$("#info").text(level.substring(cellsH * cellsV));
levelLoaded = true;
updateButtonDisables();
updateButtonDisables();
}
function loadOnlineLevel(level) {
customLevel = false;
var loaded = false;
$.ajax({
url : level,
success : function(result) {
loaded = true;
levelLoaded = false;
ranOnce = false;
levelComplete = false;
goals = new Array();
mapData = new Array();
for(var i = 0; i < cellsH; i++)
mapData.push(new Array());
var d = result.split("\n");
var foundStart = false;
var redDoors = new Array();
var greenDoors = new Array();
var blueDoors = new Array();
var redLevers = new Array();
var greenLevers = new Array();
var blueLevers = new Array();
for(var j = 0; j < cellsV; j++)
for(var i = 0; i < cellsH; i++) {
if(j < d.length && i < d[j].length) {
var c = d[j].charAt(i);
mapData[i][j] = c == " " ? MD_EMPTY : c == "#" ? MD_BLOCK : c == "X" ? MD_GOAL : c == "r" || c == "g" || c == "b" ? MD_LEVER : c == "R" || c == "G" || c == "B" ? MD_DOOR : MD_EMPTY;
if(c == "X") goals.push(new Goal(i, j));
if(c == "R") redDoors.push(new Door(i, j, 0));
if(c == "G") greenDoors.push(new Door(i, j, 1));
if(c == "B") blueDoors.push(new Door(i, j, 2));
if(c == "r") redLevers.push(new Lever(i, j, 0));
if(c == "g") greenLevers.push(new Lever(i, j, 1));
if(c == "b") blueLevers.push(new Lever(i, j, 2));
if(c == "S")
if(foundStart) {
showAlert("Invalid level, multiple start positions found");
return;
}
else {
mainBot.x = mainBot.nx = i;
mainBot.y = mainBot.ny = j;
levelStart = new Position(i, j);
foundStart = true;
}
}
else
mapData[i][j] = MD_EMPTY;
}
levers = redLevers.concat(greenLevers, blueLevers);
doors = redDoors.concat(greenDoors, blueDoors);
if(!foundStart) {
showAlert("Invalid level, no start positions found");
return;
}
if(d.length > cellsV) {
var text = "";
for(var i = 0; i < d.length - cellsV; i++)
text += (text == "" ? "" : "<br />") + d[cellsV + i];
$("#info").text(text);
}
levelLoaded = true;
updateButtonDisables();
}
});
return loaded;
}
function getDoorAt(x, y) {
for(var i = 0; i < doors.length; i++)
if(doors[i].x == x && doors[i].y == y)
return doors[i];
return null;
}
function getLeverAt(x, y) {
for(var i = 0; i < levers.length; i++)
if(levers[i].x == x && levers[i].y == y)
return levers[i];
return null;
}
function spaceClear(x, y) {
if(mapData[x][y] == MD_BLOCK) return false;
if(mapData[x][y] == MD_DOOR) return getDoorAt(x, y).open;
return !botAtPos(x, y);
}
function createBranch(x, y, func, fbot) {
var bot = new Bot(fbot.x, fbot.y, func);
bot.nx = x;
bot.ny = y;
bot.cloning = true;
otherBots.push(bot);
otherInstructions.push(-1);
}
function processCommand(cmd, bot) {
bot.nx = bot.x;
bot.ny = bot.y;
if(cmd.id == DO_NOTHING) return;
if(cmd.id == MOVE_LEFT && bot.x > 0 && spaceClear(bot.x - 1, bot.y)) bot.nx = bot.x - 1;
if(cmd.id == MOVE_RIGHT && bot.x < cellsH - 1 && spaceClear(bot.x + 1, bot.y)) bot.nx = bot.x + 1;
if(cmd.id == MOVE_UP && bot.y > 0 && spaceClear(bot.x, bot.y - 1)) bot.ny = bot.y - 1;
if(cmd.id == MOVE_DOWN && bot.y < cellsV - 1 && spaceClear(bot.x, bot.y + 1)) bot.ny = bot.y + 1;
if(cmd.id == BRANCH_LEFT && bot.x > 0 && spaceClear(bot.x - 1, bot.y)) createBranch(bot.x - 1, bot.y, cmd.meta, bot);
if(cmd.id == BRANCH_RIGHT && bot.x < cellsH - 1 && spaceClear(bot.x + 1, bot.y)) createBranch(bot.x + 1, bot.y, cmd.meta, bot);
if(cmd.id == BRANCH_UP && bot.y > 0 && spaceClear(bot.x, bot.y - 1)) createBranch(bot.x, bot.y - 1, cmd.meta, bot);
if(cmd.id == BRANCH_DOWN && bot.y < cellsV - 1 && spaceClear(bot.x, bot.y + 1)) createBranch(bot.x, bot.y + 1, cmd.meta, bot);
}
function getCodeForFunction(name) {
if(name == "main")
return mainBotCmds;
var n = -1;
$("#functions option").each(function(i) {
if($(this).text() == name)
n = parseInt($(this).val().substring(1));
});
if(n > -1) return functions[n];
}
function runCode() {
if(levelLoaded) {
running = true;
mainInstruction = -1;
otherInstructions = new Array();
otherBots = new Array();
ranOnce = true;
updateButtonDisables();
}
}
function resetLevel() {
mainBot.x = mainBot.nx = levelStart.x;
mainBot.y = mainBot.ny = levelStart.y;
ranOnce = false;
levelComplete = false;
otherBots = new Array();
updateButtonDisables();
running = false;
fails = new Array();
}
function onUpdate() {
if(running) {
if(stepTimer <= 0) {
isBeingMovedTo = new Array();
for(var i = 0; i < cellsH; i++)
isBeingMovedTo.push(new Array());
for(var j = 0; j < cellsV; j++)
for(var i = 0; i < cellsH; i++)
isBeingMovedTo[i][j] = false;
var len = otherBots.length;
mainInstruction++;
mainBot.x = mainBot.nx;
mainBot.y = mainBot.ny;
stepTimer = ticksPerStep;
var finished = true;
if(mainInstruction < getCodeForFunction(mainBot.func).length) {
finished = false;
processCommand(getCodeForFunction(mainBot.func)[mainInstruction], mainBot);
}
for(var i = 0; i < len; i++) {
otherInstructions[i]++;
otherBots[i].x = otherBots[i].nx;
otherBots[i].y = otherBots[i].ny;
otherBots[i].cloning = false;
var fetchedCode = getCodeForFunction(otherBots[i].func);
if(otherInstructions[i] < fetchedCode.length) {
finished = false;
processCommand(fetchedCode[otherInstructions[i]], otherBots[i]);
}
}
var redPressed = false;
var greenPressed = false;
var bluePressed = false;
for(var i = 0; i < levers.length; i++)
if(botAtPos(levers[i].x, levers[i].y))
switch(levers[i].colour) {
case 0:
redPressed = true;
break;
case 1:
greenPressed = true;
break;
case 2:
bluePressed = true;
break;
}
for(var i = 0; i < doors.length; i++)
switch(doors[i].colour) {
case 0:
doors[i].open = redPressed;
break;
case 1:
doors[i].open = greenPressed;
break;
case 2:
doors[i].open = bluePressed;
break;
}
if(finished) {
running = false;
fails = new Array();
for(var i = 0; i < goals.length; i++)
if(!goals[i].isComplete())
fails.push(goals[i]);
if(!mainBot.onGoal())
fails.push(mainBot);
for(var i = 0; i < otherBots.length; i++)
if(!otherBots[i].onGoal())
fails.push(otherBots[i]);
if(fails.length > 0) {
showAlert("Some objects have failed! Check all goals are complete and no redundant bots are made!", function x() {
resetLevel();
});
return;
}
if(customLevel)
showConfirm("Custom level complete, would you like to go back to normal levels?", function(n) {
if(n == 0) nextLevel();
});
else {
if(levelNum < levels.length - 1)
showAlert("Level Complete! Advancing to the next level", function() {
nextLevel();
});
else
showAlert("Game Complete! Currently have no more levels left for you, maybe try some custom levels?");
}
updateButtonDisables();
}
}
else
stepTimer--;
}
}
function nextLevel() {
levelNum++;
resetCode();
loadLevel();
resetLevel();
}
function draw() {
ctx.save();
ctx.fillStyle = "#E0E0E0";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "#808080";
ctx.lineWidth = 1;
for(var i = 0; i <= cellsH; i++) {
ctx.beginPath();
ctx.moveTo(i * cellWidth, 0);
ctx.lineTo(i * cellWidth, h);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cellHeight);
ctx.lineTo(w, i * cellHeight);
ctx.stroke();
}
if(levelLoaded) {
for(var i = 0; i < cellsH; i++)
for(var j = 0; j < cellsV; j++) {
if(mapData[i][j] == MD_BLOCK) ctx.drawImage(blockImg, i * cellWidth, j * cellHeight);
if(mapData[i][j] == MD_GOAL) ctx.drawImage(goalImg, i * cellWidth, j * cellHeight);
if(mapData[i][j] == MD_LEVER) {
var l = getLeverAt(i, j);
var img;
switch(l.colour) {
case 0:
img = leverRImg;
break;
case 1:
img = leverGImg;
break;
case 2:
img = leverBImg;
break;
}
if(img) ctx.drawImage(img, i * cellWidth, j * cellHeight);
}
if(mapData[i][j] == MD_DOOR) {
var d = getDoorAt(i, j);
if(!d.open) {
var img;
switch(d.colour) {
case 0:
img = doorRImg;
break;
case 1:
img = doorGImg;
break;
case 2:
img = doorBImg;
break;
}
if(img) ctx.drawImage(img, i * cellWidth, j * cellHeight);
}
}
}
ctx.drawImage(botImg, cellWidth * (mainBot.x + (mainBot.nx - mainBot.x) * (1 - stepTimer / ticksPerStep)), cellHeight * (mainBot.y + (mainBot.ny - mainBot.y) * (1 - stepTimer / ticksPerStep)));
for(var i = 0; i < otherBots.length; i++) {
if(otherBots[i].cloning) ctx.globalAlpha = 0.5;
ctx.drawImage(botImg, cellWidth * (otherBots[i].x + (otherBots[i].nx - otherBots[i].x) * (1 - stepTimer / ticksPerStep)), cellHeight * (otherBots[i].y + (otherBots[i].ny - otherBots[i].y) * (1 - stepTimer / ticksPerStep)));
ctx.globalAlpha = 1;
}
for(var i = 0; i < fails.length; i++) {
ctx.globalAlpha = 0.5;
ctx.fillStyle = "#FF0000"
ctx.fillRect(fails[i].x * cellWidth, fails[i].y * cellHeight, cellWidth, cellHeight);
ctx.globalAlpha = 1;
}
}
ctx.restore();
}
function gameLoop() {
onUpdate();
draw();
}
$(document).ready(function() {
mainBot = new Bot(0, 0, "main");
branches = new Array();
loadLevel();
setInterval(gameLoop, 10);
});
</script>
</body>
</html>