forked from amyjko/Gidget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.js
1588 lines (1146 loc) · 40.7 KB
/
runtime.js
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
/*
STATE
A WORLD contains a set of THINGS
A THING contains a set of properties and functions
*/
function isDef(value) { return typeof value !== 'undefined'; }
var GIDGET = {};
// Instantiates a runtime for a Thing, usually Gidget, but other Things can do things too.
GIDGET.Runtime = function(thing, world) {
// The index of the current step
this.pc = undefined;
// The pc that executed before this
this.lastPC = undefined;
// Remember the world to run against.
this.world = world;
this.thing = thing;
// Prepare the thing's state.
this.resultsStack = [];
this.scanned = [];
this.analyzed = [];
this.grabbed = [];
this.focused = [];
this.arguments = {};
this.asked = [];
// This is a list of conditionals to execute each step; event-driven behaviors.
this.rules = [];
// Determines what image is chosen for the thing.
this.state = "default";
this.decisions = [];
this.getCurrentLine = function() {
if(!this.isExecuting()) return undefined;
else return this.steps[this.pc].representativeToken.line;
}
// Returns whether there's an active, executing execution.
this.isExecuting = function() { return this.pc < this.steps.length && (this.isExecutingGoal === true || thing.energy > 0); };
this.steps = undefined;
this.isExecutingGoal = undefined;
// Returns true if the given thing is in the scope on the top of the scope stack.
this.isFocused = function(thing) { return this.focused.length > 0; };
this.isFocusedOn = function(thing) { return this.focused.length > 0 && this.focused[0] === thing; };
this.pushFocus = function(thing) { this.focused.unshift(thing); };
this.popFocus = function() { return this.focused.shift(); };
this.peekFocus = function() {
if(this.focused.length > 0) {
return this.focused[0];
} else {
return undefined;
}
};
this.pushAsked = function(thing) { this.asked.unshift(thing); };
this.popAsked = function() { return this.asked.shift(); };
this.peekAsked = function() { return this.asked.length > 0 ? this.asked[0] : undefined; }
this.pushResults = function(results, query) {
results.query = query;
this.resultsStack.unshift(results);
};
this.popResults = function() { return this.resultsStack.shift(); };
this.popResult = function() {
if(this.resultsStack.length > 0)
return this.resultsStack[0].shift();
else {
console.error("Shouldn't be popping on an empty results stack.");
return undefined;
}
};
this.peekResult = function() { return this.resultsStack[0][0]; };
this.peekResults = function() { return this.resultsStack.length > 0 ? this.resultsStack[0] : undefined; };
// Shorthand for runtime Step_ side effects
this.IncrementPC = function(runtime, increment) {
this.runtime = runtime;
this.increment = increment;
this.kind = 'IncrementPC';
this.execute = function() { this.runtime.pc += this.increment; }
};
this.PopResults = function(runtime) {
this.runtime = runtime;
this.kind = 'PopResults';
this.execute = function() { this.runtime.popResults(); };
};
this.PushResults = function(runtime, results, query) {
this.runtime = runtime;
this.kind = 'PushResults';
this.results = results;
this.query = query;
this.execute = function() { this.runtime.pushResults(this.results, this.query); };
};
this.PopResult = function(runtime) {
this.runtime = runtime;
this.kind = 'PopResult';
this.execute = function() { this.runtime.popResult(); };
}
this.ClearFocus = function(runtime) {
this.runtime = runtime;
this.kind = 'ClearFocus';
this.execute = function() { while(this.runtime.isFocused()) this.runtime.popFocus(); };
};
this.ClearResults = function(runtime) {
this.runtime = runtime;
this.kind = 'ClearResults';
this.execute = function() {
while(this.runtime.hasRecentResults()) this.runtime.popResults();
};
};
this.Move = function(runtime, rowIncrement, columnIncrement, done) {
this.runtime = runtime;
this.kind = 'Move';
this.rowIncrement = rowIncrement;
this.columnIncrement = columnIncrement;
this.execute = function() {
this.runtime.world.place(this.runtime.thing, this.runtime.thing.row + this.rowIncrement, this.runtime.thing.column + this.columnIncrement);
// Place all of the things this thing has grabbed.
var i;
for(i = 0; i < this.runtime.grabbed.length; i++) {
this.runtime.world.place(this.runtime.grabbed[i], this.runtime.thing.row, this.runtime.thing.column);
}
};
};
this.PushScanned = function(runtime, thing) {
this.runtime = runtime;
this.kind = 'PushScanned';
this.execute = function() {
if($.inArray(thing, runtime.scanned) < 0)
runtime.scanned.unshift(thing);
};
};
this.PushAnalyzed = function(runtime, thing) {
this.runtime = runtime;
this.kind = 'PushAnalyzed';
this.execute = function() {
if($.inArray(thing, runtime.analyzed) < 0)
runtime.analyzed.unshift(thing);
};
};
this.PushGrabbed = function(runtime, thing) {
this.runtime = runtime;
this.kind = 'PushGrabbed';
this.thing = thing;
this.execute = function() {
// If the thing isn't grabbed, grab it.
if($.inArray(this.thing, this.runtime.grabbed) < 0) {
// Remove the thing from everything else that's grabbed it.
var i;
for(i = 0; i < this.runtime.world.things.length; i++) {
var index = $.inArray(this.thing, this.runtime.world.things[i].runtime.grabbed);
if(index >= 0)
this.runtime.world.things[i].runtime.grabbed.splice(index, 1);
}
// Grab it!
this.runtime.grabbed.unshift(this.thing);
}
};
};
this.PopGrabbed = function(runtime, thing) {
this.runtime = runtime;
this.kind = 'PopGrabbed';
this.thing = thing;
this.execute = function() {
// Save this for later so the UI can avoid computing it.
this.index = $.inArray(this.thing, this.runtime.grabbed);
runtime.grabbed.splice(this.index, 1);
};
};
this.PushFocus = function(runtime, thing) {
this.runtime = runtime;
this.kind = 'PushFocus';
this.thing = thing;
this.execute = function() {
this.runtime.pushFocus(this.thing);
};
};
this.PopFocus = function(runtime, thing) {
this.runtime = runtime;
this.kind = 'PopFocus';
this.execute = function() {
this.runtime.popFocus();
};
};
this.Say = function(runtime, message) {
this.runtime = runtime;
this.kind = 'Say';
this.message = message;
this.execute = function() {};
};
this.addDecision = function(step, message, action) { this.decisions.push(new this.Decision(this, step, message, action)); }
this.hasRecentResults = function() { return this.resultsStack.length > 0 && this.resultsStack[0].length > 0; };
this.queryMatches = function(query, thing) {
// If the query is undefined, then it matches.
if(!isDef(query)) return true;
var name = query.text;
if(name.toLowerCase() === thing.name.toLowerCase()) return true;
if(name.length > 1 && name.charAt(name.length - 1).toLowerCase() === 's' && name.substring(0, name.length - 1).toLowerCase() === thing.name.toLowerCase())
return true;
// Does the query match the names of one of the arguments? Is the thing passed in
// assigned to the argument?
if(this.arguments.hasOwnProperty(name) && this.arguments[name] === thing)
return true;
return false;
};
this.step = function(executingRule) {
// Regardless of whether this object is done, we execute its rules.
var i, j;
var steps;
// If we're not executing a rule, execute steps.
if(!isDef(executingRule)) {
for(i = 0; i < this.rules.length; i++) {
// Remember the old instructions and steps.
var ongoingSteps = this.steps;
var ongoingPC = this.pc;
this.steps = this.rules[i].steps;
this.pc = 0;
// Execute all of the steps until done.
while(this.pc < this.steps.length) {
// Execute this step.
var decisions = this.step(true);
for(j = 0; j < decisions.length; j++)
decisions[j].execute();
}
this.steps = ongoingSteps;
this.pc = ongoingPC;
}
}
// If this code is done, no need to execute further.
if(this.pc < 0 || this.pc >= this.steps.length) {
return undefined;
}
if (GIDGET.experiment.verbose) {
console.log("[" + thing.name + "]: " + this.pc + " " + this.steps[this.pc].toString());
}
var step = this.steps[this.pc];
// Remember the last program counter for the UI, so it can know what executed.
this.lastPC = this.pc;
// Execute the step, gathering decisions.
step.execute(this);
// Gather the decisions made for the caller to execute individually.
var decisions = this.decisions;
// Restore the decisions to an empty array.
this.decisions = [];
// Ask how much energy to deduct.
var deduction = step.cost(this);
// Deduct the energy.
this.thing.energy -= deduction;
//ADAPTIVE: maintain energy record
GIDGET.ui.minEnergy = GIDGET.ui.minEnergy < this.thing.energy ? GIDGET.ui.minEnergy : this.thing.energy;
GIDGET.ui.energyUsed += deduction;
console.log(GIDGET.ui.energyUsed);
return decisions;
};
// Arguments is an object literal of things, keyed by the things' names.
this.start = function(code, isGoal, arguments) {
// Parse the program
this.steps = isGoal === true ? GIDGET.parser.parseGoal(code) : GIDGET.parser.parseScript(code);
this.isExecutingGoal = isGoal;
// Print out the steps, just for reference
if (GIDGET.experiment.verbose) {
var i;
for(i = 0; i < this.steps.length; i++) {
console.log(i + " " + this.steps[i].toString());
}
}
// Prepare the execution metadata.
this.pc = 0;
this.resultsStack = [];
this.focused = [];
this.decisions = [];
this.asked = [];
this.arguments = arguments;
this.isGoal = isGoal;
};
// Represents a decision that a Step_ made while executing. Messages can refer to
// runtime state, which can then be highlighted in the user interface to explain the decision.
this.Decision = function(runtime, step, message, action) {
this.runtime = runtime;
this.step = step;
// Remember the function to execute.
this.action = action;
this.thought = message;
this.execute = function() {
if(isDef(action))
action.execute();
if(isDef(this.thought.emotion))
this.runtime.state = this.thought.emotion;
};
};
};
// The collection of steps that know how to execute a Gidget program, produced by the parser.
GIDGET.runtime = {
Step_IF: function(ast, representativeToken, offset) {
return {
ast: ast,
representativeToken: representativeToken,
offset: offset,
toString: function() { return "if " + this.offset; },
cost: function(runtime) { return 0; },
execute: function(runtime) {
var results = runtime.peekResults();
if(results.length > 0) {
runtime.addDecision(
this,
GIDGET.text.if_true(),
new runtime.IncrementPC(runtime, 1));
}
else {
runtime.addDecision(
this,
GIDGET.text.if_false(),
new runtime.IncrementPC(runtime, this.offset));
}
runtime.addDecision(
this,
GIDGET.text.if_popResults(),
new runtime.PopResults(runtime));
}
};
},
Step_WHEN: function(ast, representativeToken, steps) {
return {
ast: ast,
representativeToken: representativeToken,
steps: steps,
toString: function() { return "when"; },
cost: function(runtime) { return 0; },
execute: function(runtime) {
// Add the rule to the thing's runtime's cognizant list of rules.
runtime.rules.push(this);
// Next step
runtime.pc++;
}
};
},
Step_IS: function(ast, representativeToken, keyword, name) {
return {
ast: ast,
representativeToken: representativeToken,
keyword: keyword,
name: name,
toString: function() { return "" + this.keyword.text + " " + this.name.text; },
cost: function(runtime) { return 0; },
execute: function(runtime) {
var results = runtime.peekResults();
var newResults = [];
var i;
var hasTag = undefined;
var positive = keyword.text === 'is' || keyword.text === 'are';
var allTrue = true;
for(i = 0; i < results.length; i++) {
hasTag = results[i].tags.hasOwnProperty(this.name.text.toLowerCase());
if((positive && hasTag) || (!positive && !hasTag)) {
runtime.addDecision(this, GIDGET.text.is_positive(results[i].name, i, this.keyword.text, this.name.text));
newResults.push(results[i]);
}
else {
allTrue = false;
runtime.addDecision(this, GIDGET.text.is_negative(results[i].name, i, this.keyword.text, this.name.text));
}
}
runtime.addDecision(
this,
GIDGET.text.is_popResults(),
new runtime.PopResults(runtime));
runtime.addDecision(
this,
GIDGET.text.is_newResults(),
new runtime.PushResults(runtime, allTrue ? newResults : []));
runtime.pc++;
}
};
},
Step_UNKNOWN: function(ast, representativeToken, tokens, message) {
return {
ast: ast,
representativeToken: representativeToken,
tokens: tokens,
message: message,
toString: function() {
var i;
var array = "[";
for(i = 0; i < this.tokens.length; i++)
array = array + this.tokens[i].text + (i < this.tokens.length - 1 ? "," : "");
return 'unknown ' + array + ']';
},
cost: function(runtime) { return 0; },
execute: function(runtime) {
var i;
var string = "";
for(i = 0; i < this.tokens.length; i++)
string = string + this.tokens[i].text + " ";
runtime.addDecision(
this,
this.message,
undefined);
// Clear the results and focus stacks
runtime.addDecision(
this,
GIDGET.text.unknown_clearFocus(),
new runtime.ClearFocus(runtime));
runtime.addDecision(
this,
GIDGET.text.unknown_clearResults(),
new runtime.ClearResults(runtime));
runtime.addDecision(
this,
GIDGET.text.unknown_nextStep(),
new runtime.IncrementPC(runtime, 1));
}
};
},
// Scans the next thing on the list.
Step_SCAN: function(ast, representativeToken) {
return {
ast: ast,
representativeToken: representativeToken,
offset: undefined,
toString: function() { return "scan " + this.offset; },
cost: function(runtime) { return 1; },
execute: function(runtime) {
if(runtime.hasRecentResults()) {
runtime.addDecision(
this,
GIDGET.text.scan_success(runtime.peekResult().name),
new runtime.PushScanned(runtime, runtime.peekResult()));
runtime.pc++;
}
else {
runtime.pc += this.offset;
}
}
};
},
// Scans the next thing on the list.
Step_NAME: function(ast, representativeToken, name) {
return {
ast: ast,
representativeToken: representativeToken,
name: name,
offset: undefined,
toString: function() { return "name " + this.name.text; },
cost: function(runtime) { return 1; },
execute: function(runtime) {
if(runtime.hasRecentResults()) {
var result = runtime.peekResult();
result.name = this.name.text;
runtime.addDecision(this, GIDGET.text.name_success(this.name.text));
runtime.pc++;
}
else {
runtime.pc += this.offset;
}
}
};
},
// Go to the next object in the query on the stack
Step_GO: function(ast, representativeToken, avoidToken) {
return {
ast: ast,
representativeToken: representativeToken,
avoid: avoidToken,
offset: undefined,
toString: function() { return "go " + (isDef(this.avoid) ? "avoid " + this.avoid.text + " " : "") + this.offset; },
cost: function(runtime) {
return 1 + runtime.grabbed.length;
},
execute: function(runtime) {
// If we haven't set the clock, set it.
if(!isDef(this.wait)) {
this.wait = runtime.thing.speed;
}
// Count down the wait.
this.wait--;
// If it's still above zero, skip moving.
if(this.wait > 0)
return;
// Otherwise, reset the clock and move!
this.wait = runtime.thing.speed;
// Are there any targets to move to?
if(runtime.hasRecentResults()) {
// What are we going to?
var thing = runtime.peekResult();
// Dijkstra's algorithm, adapted to find shortest path from Gidget to target.
function findShortestPath(grid, currentRow, currentColumn, goalRow, goalColumn, level, nameToAvoid, checkWithinOne) {
function isValid(queue, cells, grid, row, column, level, theNameToAvoid, withinOne) {
// If it's outside the bounds of the world, it's not a good location.
if(row < 0) return false;
if(row >= grid.length) return false;
if(column < 0) return false;
if(column >= grid[0].length) return false;
// Check height restrictions at this location.
var i;
for(i = 0; i < grid[row][column].length; i++) {
var thing = grid[row][column][i];
if(thing.level > level) {
return false;
}
}
// Have we already checked this? Not valid.
if($.inArray(cells[row][column], queue) < 0) return false;
// Check for things to avoid.
if(isDef(theNameToAvoid)) {
// Are we checking within one?
if(withinOne) {
var nearby = [[row-1, column],[row-1,column-1],[row,column-1],[row+1,column-1],[row+1,column],[row+1,column+1],[row,column+1],[row-1,column+1]];
var spot;
for(spot = 0; spot < nearby.length; spot++) {
var r = nearby[spot][0]
var c = nearby[spot][1];
// If this is a legal position
if(r >= 0 && r < grid.length && c >= 0 && c < grid[0].length) {
// Go through all of the things at this position...
for(i = 0; i < grid[r][c].length; i++) {
// Does the thing have the same name as the thing to avoid?
var thing = grid[r][c][i];
if(isDef(thing.name) && thing.name.toLowerCase() === theNameToAvoid.toLowerCase()) {
return false;
}
}
}
}
}
// Otherwise, just check for the cell itself
else {
for(i = 0; i < grid[row][column].length; i++) {
// Does the thing have the same name as the thing to avoid?
var thing = grid[row][column][i];
if(isDef(thing.name) && thing.name.toLowerCase() === theNameToAvoid.toLowerCase()) {
return false;
}
}
}
}
// Otherwise, it's valid.
return true;
}
// Make a queue of cells.
var queue = [];
// Make a grid of cells for bookkeeping.
var cells = new Array(grid.length);
var row, col;
for(row = 0; row < grid.length; row++) {
cells[row] = new Array(grid[0].length);
for(col = 0; col < grid[0].length; col++) {
cells[row][col] = {
row: row,
column: col,
distance: undefined,
previous: undefined
};
// Add all cells to the queue.
queue.push(cells[row][col]);
}
}
var start = cells[currentRow][currentColumn];
var goal = cells[goalRow][goalColumn];
start.distance = 0;
while(queue.length > 0) {
// Find the cell with the smallest distance.
var i;
var closestCell = undefined;
for(i = 0; i < queue.length; i++) {
if(isDef(queue[i].distance) && (closestCell === undefined || queue[i].distance < closestCell.distance))
closestCell = queue[i];
}
// If no cell remains that is accessible from source, we have failed.
if(closestCell === undefined)
break;
// Remove the closest cell from the queue.
queue.splice($.inArray(closestCell, queue), 1);
// Gather a list of valid neighbors of the closest cell.
var neighbors = [];
// Is the cell above a valid, passable, that has not been visited?
if(isValid(queue, cells, grid, closestCell.row - 1, closestCell.column, level, nameToAvoid, checkWithinOne))
neighbors.push(cells[closestCell.row - 1][closestCell.column]);
if(isValid(queue, cells, grid, closestCell.row + 1, closestCell.column, level, nameToAvoid, checkWithinOne))
neighbors.push(cells[closestCell.row + 1][closestCell.column]);
if(isValid(queue, cells, grid, closestCell.row, closestCell.column - 1, level, nameToAvoid, checkWithinOne))
neighbors.push(cells[closestCell.row][closestCell.column - 1]);
if(isValid(queue, cells, grid, closestCell.row, closestCell.column + 1, level, nameToAvoid, checkWithinOne))
neighbors.push(cells[closestCell.row][closestCell.column + 1]);
// For each valid neighbor,
for(i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i];
var alt = closestCell.distance + 1;
// If navigating to this neighbor is less than the distance of the neighbor to the goal,
// then update the neighbors distance.
if(neighbor.distance === undefined || alt < neighbor.distance) {
neighbor.distance = alt;
neighbor.previous = closestCell;
}
}
}
var path = [];
while(isDef(goal)) {
path.unshift(goal);
goal = goal.previous;
}
// If the path is non empty and begins at the start point, then return it.
if(path.length > 0 && path[0].row === currentRow && path[0].column === currentColumn)
return path;
// Otherwise, an empty array represents failure.
else
return [];
}
var avoidText = isDef(this.avoid) ? this.avoid.text : undefined;
// Find the shortest path to the target using the above algorithm, avoid thing things within 1 unit
var path = findShortestPath(runtime.world.grid, runtime.thing.row, runtime.thing.column, thing.row, thing.column, runtime.thing.level, avoidText, true);
// If we were avoiding and there was no path, try just avoiding going on top of things.
var noPath = false;
if(isDef(this.avoid) && path.length <= 1) {
noPath = true;
path = findShortestPath(runtime.world.grid, runtime.thing.row, runtime.thing.column, thing.row, thing.column, runtime.thing.level, avoidText, false);
}
// If we're avoiding and there was still no path, try not avoiding at all.
if(isDef(this.avoid) && path.length <= 1) {
noPath = true;
path = findShortestPath(runtime.world.grid, runtime.thing.row, runtime.thing.column, thing.row, thing.column, runtime.thing.level, undefined, false);
}
// If there was a path, remember it.
if(path.length > 1) {
// Move the next direction in the path
var rowIncrement = path[1].row - runtime.thing.row;
var columnIncrement = path[1].column - runtime.thing.column;
var done = runtime.thing.row + rowIncrement === thing.row && runtime.thing.column + columnIncrement === thing.column;
// If we're moving somewhere, say so!
if(rowIncrement !== 0 || columnIncrement !== 0)
runtime.addDecision(
this,
noPath ?
GIDGET.text.go_dangerousStep(thing.name, this.avoid.text) :
GIDGET.text.go_step(thing.name),
new runtime.Move(runtime, rowIncrement, columnIncrement, done));
// We're at it! Execute the command on it, if there is one, otherwise, go to the next thing.
if(done) {
runtime.path = undefined;
runtime.addDecision(
this,
GIDGET.text.go_arrive(thing.name),
new runtime.IncrementPC(runtime, 1));
}
}
else if(runtime.thing.row === thing.row && runtime.thing.column === thing.column) {
runtime.addDecision(
this,
GIDGET.text.go_alreadyAt(thing.name),
new runtime.IncrementPC(runtime, 1));
}
// There are two behavior's we've had implemented in the past here: one is to keep
// looping until a path opens up, the other is to just continue forward.
else {
runtime.path = undefined;
runtime.addDecision(
this,
GIDGET.text.go_noPath(thing.name),
new runtime.IncrementPC(runtime, 1));
}
}
else {
runtime.addDecision(
this,
GIDGET.text.go_finished(),
new runtime.IncrementPC(runtime, this.offset));
}
}
};
},
// Scans the next thing on the list.
Step_ANALYZE: function(ast, representativeToken) {
return {
ast: ast,
representativeToken: representativeToken,
offset: undefined,
toString: function() { return "analyze " + this.offset; },
cost: function(runtime) { return 1; },
execute: function(runtime) {
if(runtime.hasRecentResults()) {
runtime.addDecision(
this,
GIDGET.text.analyze_success(runtime.peekResult().name),
new runtime.PushAnalyzed(runtime, runtime.peekResult()));
runtime.pc++;
}
else {
runtime.pc += this.offset;
}
}
};
},
// Pops the next Thing in the query result on the top of the stack.
// If there's nothing left in the query, go to the next instruction.
Step_NEXT: function(ast, representativeToken, offset) {
return {
ast: ast,
representativeToken: representativeToken,
offset: offset,
toString: function() { return "next " + this.offset; },
cost: function(runtime) { return 0; },
execute: function(runtime) {
// Assuming there's a result left, pop it since we're done with it.
if(runtime.hasRecentResults())
runtime.popResult();
// If there are results left, continue to the next one.
if(runtime.hasRecentResults()) {
runtime.pc += this.offset;
}
// If there aren't results left, pop the empty list and move on to the next step.
else {
runtime.popResults();
runtime.pc++;
}
}
};
},
Step_GRAB: function(ast, representativeToken) {
return {
ast: ast,
representativeToken: representativeToken,
offset: undefined,
toString: function() { return "grab"; },
cost: function(runtime) { return 1; },
execute: function(runtime) {
if(runtime.hasRecentResults()) {
runtime.addDecision(
this,
GIDGET.text.grab_success(runtime.peekResult().name),
new runtime.PushGrabbed(runtime, runtime.peekResult()));
runtime.pc++;
}
else {
runtime.pc+= this.offset;
}
}
};
},
Step_DROP: function(ast, representativeToken) {