-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.c
1401 lines (1244 loc) · 40.7 KB
/
runner.c
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
#include "puzzleData.h"
#include "render.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define EMPTY_ID 1
int onBoard(Runtime *rt, int x, int y) {
if (x < 0 || x >= rt->width || y < 0 || y >= rt->height) {
return 0;
}
return 1;
}
int legendObjId(Runtime *rt, int legendId, int x, int y) {
int tileIndex;
for (int i = 0; i < rt->pd->layerCount; i++) {
tileIndex = (i * rt->width * rt->height) + (y * rt->width) + x;
if (rt->map[tileIndex] != -1 &&
aliasLegendContains(legendId, rt->objects[rt->map[tileIndex]].objId)) {
return rt->map[tileIndex];
}
}
return -1;
}
Direction absoluteDirection(Direction applicationDirection, Direction ruleDir) {
switch (ruleDir) {
case UP:
case DOWN:
case LEFT:
case RIGHT:
case NONE:
case USE:
return ruleDir;
case COND_NO:
case UNSPECIFIED:
case STATIONARY:
return NONE;
case RANDOMDIR:
return (Direction)rand() % 4;
case HORIZONTAL:
if (applicationDirection == UP) {
return LEFT;
} else if (applicationDirection == DOWN) {
return RIGHT;
} else {
return applicationDirection;
}
case VERTICAL:
if (applicationDirection == LEFT) {
return UP;
} else if (applicationDirection == RIGHT) {
return DOWN;
} else {
return applicationDirection;
}
default:
fprintf(stderr,
"err: (absoluteDirection) unsupported direction (ad: %i rd: %i)\n",
applicationDirection, ruleDir);
return NONE;
}
}
int matchesDirection(Direction ruleDir, Direction applicationDir, Direction dir,
int ignoreUnspec) {
if (dir == COND_NO || (ignoreUnspec && ruleDir == UNSPECIFIED)) {
return 1;
}
Direction absoluteDir = absoluteDirection(applicationDir, ruleDir);
if (absoluteDir == dir) {
return 1;
} else if (absoluteDir == HORIZONTAL && (dir == LEFT || dir == RIGHT)) {
return 1;
} else if (absoluteDir == VERTICAL && (dir == UP || dir == DOWN)) {
return 1;
} else {
return 0;
}
}
void addToMove(Runtime *rt, int objIndex, Direction direction) {
/* printf("Adding %i %s\n", objIndex, dirName(direction)); */
for (int i = 0; i < rt->toMoveCount; i++) {
if (rt->toMove[i].objIndex == objIndex) {
rt->objects[objIndex].moving = direction;
return;
}
}
if (rt->toMoveCount < rt->toMoveCapacity) {
} else {
rt->toMoveCapacity += 50;
rt->toMove = realloc(rt->toMove, sizeof(ToMove) * rt->toMoveCapacity);
}
rt->toMove[rt->toMoveCount].objIndex = objIndex;
rt->objects[objIndex].moving = direction;
rt->toMoveCount++;
}
// MAPS
int legendAt(Runtime *rt, int legendId, int x, int y) {
int width = rt->width;
int bytePerRecord = rt->pd->objectCount / 8 + 1;
int byteIndex =
((y * width * bytePerRecord * 8) + (x * bytePerRecord * 8)) / 8;
for (int byteOffset = 0; byteOffset < bytePerRecord; byteOffset++) {
if ((rt->pd->aliasLegend[legendId].mask[byteOffset] &
rt->oMap[byteIndex + byteOffset]) != 0) {
return 1;
}
}
return 0;
}
void buildOMap(Runtime *rt) {
int width = rt->width;
int bytePerRecord = rt->pd->objectCount / 8 + 1;
if (rt->hasOMap) {
free(rt->oMap);
}
rt->hasOMap = 1;
rt->oMap = calloc(rt->height * rt->width * bytePerRecord, 1);
for (int x = 0; x < rt->width; x++) {
for (int y = 0; y < rt->height; y++) {
for (int layerId = 0; layerId < rt->pd->layerCount; layerId++) {
int tileIndex =
(layerId * rt->width * rt->height) + (y * rt->width) + x;
if (rt->map[tileIndex] != -1 &&
rt->objects[rt->map[tileIndex]].deleted == 0) {
int element = (y * width * bytePerRecord * 8) +
(x * bytePerRecord * 8) +
rt->objects[rt->map[tileIndex]].objId;
unsigned int byte_index = element / 8;
unsigned int bit_index = element % 8;
unsigned int bit_mask = (1 << bit_index);
rt->oMap[byte_index] |= bit_mask;
}
}
}
}
}
void buildMap(Runtime *rt) {
if (rt->hasMap) {
free(rt->map);
}
rt->hasMap = 1;
rt->map = malloc((sizeof(int) * rt->height * rt->width * rt->pd->layerCount));
for (int i = 0; i < (rt->pd->layerCount * rt->height * rt->width); i++) {
rt->map[i] = -1;
}
}
void updateObjInMap(Runtime *rt, int objIndex) {
int layerId, x, y, tileIndex;
layerId = objectLayer(rt->objects[objIndex].objId);
x = rt->objects[objIndex].x;
y = rt->objects[objIndex].y;
tileIndex = (layerId * rt->width * rt->height) + (y * rt->width) + x;
rt->map[tileIndex] = objIndex;
// OMAP
int bytePerRecord = rt->pd->objectCount / 8 + 1;
int element = (y * rt->width * bytePerRecord * 8) + (x * bytePerRecord * 8) +
rt->objects[objIndex].objId;
rt->oMap[element / 8] |= (1 << element % 8);
}
void removeObjFromOMap(Runtime *rt, int objIndex) {
int x, y;
int width = rt->width;
int bytePerRecord = rt->pd->objectCount / 8 + 1;
x = rt->objects[objIndex].x;
y = rt->objects[objIndex].y;
int element = (y * width * bytePerRecord * 8) + (x * bytePerRecord * 8) +
rt->objects[objIndex].objId;
unsigned int byte_index = element / 8;
unsigned int bit_index = element % 8;
unsigned int bit_mask = (1 << bit_index);
rt->oMap[byte_index] &= ~(bit_mask);
}
void removeObjFromMap(Runtime *rt, int objIndex) {
int layerId, x, y, tileIndex;
layerId = objectLayer(rt->objects[objIndex].objId);
x = rt->objects[objIndex].x;
y = rt->objects[objIndex].y;
tileIndex = (layerId * rt->width * rt->height) + (y * rt->width) + x;
rt->map[tileIndex] = -1;
removeObjFromOMap(rt, objIndex);
}
// END MAPS
void addObj(Runtime *rt, int objId, int x, int y) {
if (rt->objectCount + 1 >= rt->objectCapacity) {
rt->objectCapacity = rt->objectCapacity * 2;
rt->objects = realloc(rt->objects, sizeof(Obj) * rt->objectCapacity);
}
rt->objects[rt->objectCount].objId = objId;
rt->objects[rt->objectCount].x = x;
rt->objects[rt->objectCount].y = y;
rt->objects[rt->objectCount].deleted = 0;
rt->objects[rt->objectCount].moving = NONE;
updateObjInMap(rt, rt->objectCount);
rt->objectCount++;
}
void cleanup(Runtime *rt) {
int oldObjCount = rt->objectCount;
Obj *oldObjs = rt->objects;
buildMap(rt);
rt->objectCount = 0;
rt->objects = malloc(sizeof(Obj) * rt->objectCapacity);
for (int i = 0; i < oldObjCount; i++) {
if (oldObjs[i].deleted == 0) {
addObj(rt, oldObjs[i].objId, oldObjs[i].x, oldObjs[i].y);
}
}
free(oldObjs);
}
void loadTile(Runtime *rt, char tile, int x, int y) {
int id = legendIdForGlyph(tile);
addObj(rt, rt->pd->aliasLegend[rt->backgroundId].objects[0], x, y);
for (int i = 0; i < rt->pd->glyphLegend[id].objectCount; i++) {
int objId = rt->pd->glyphLegend[id].objects[i];
if (objId != -1) {
addObj(rt, objId, x, y);
}
}
}
void updateLevel(Runtime *rt) {
if (checkWinConditions(rt) == 1) {
nextLevel(rt);
}
}
void initGame(Runtime *rt) {
srand((unsigned)time(&rt->randomSeed));
rt->levelIndex = 0;
rt->gameWon = 0;
rt->prevHistoryCount = 0;
rt->deadCount = 0;
rt->objectCount = 0;
rt->objectCapacity = 250;
rt->objects = malloc(sizeof(Obj) * rt->objectCapacity);
rt->toMoveCount = 0;
rt->toMoveCapacity = 1000;
rt->toMove = malloc(sizeof(ToMove) * rt->toMoveCapacity);
rt->historyCount = 0;
rt->historyCapacity = 1000;
rt->history = malloc(sizeof(Direction) * rt->historyCapacity);
rt->statesCount = 0;
rt->statesCapacity = 1000;
rt->states = malloc(sizeof(State) * rt->statesCapacity);
for (int i = 0; i < rt->statesCapacity; i++) {
rt->states[i].objectCapacity = 0;
rt->states[i].objectCount = 0;
}
rt->hasMap = 0;
rt->hasOMap = 0;
}
void endGame(Runtime *rt) {
free(rt->objects);
free(rt->toMove);
free(rt->history);
for (int i = 0; i < rt->statesCount; i++) {
free(rt->states[i].objects);
free(rt->states[i].map);
free(rt->states[i].oMap);
rt->states[i].objectCount = 0;
rt->states[i].objectCapacity = 0;
}
free(rt->states);
if (rt->hasMap) {
free(rt->map);
}
rt->hasMap = 0;
if (rt->hasOMap) {
free(rt->oMap);
}
rt->hasOMap = 0;
freePuzzle();
}
void undo(Runtime *rt) {
if (rt->didUndo) {
// Don't undo more then once per update
return;
}
rt->levelIndex = rt->states[rt->statesCount - 1].levelIndex;
rt->levelType = levelType(rt->levelIndex);
rt->height = rt->pd->levels[rt->levelIndex].height;
rt->width = rt->pd->levels[rt->levelIndex].width;
rt->objectCount = rt->states[rt->statesCount - 1].objectCount;
rt->objectCapacity = rt->states[rt->statesCount - 1].objectCapacity;
rt->objects =
realloc(rt->objects,
sizeof(Obj) * rt->states[rt->statesCount - 1].objectCapacity);
memcpy(rt->objects, rt->states[rt->statesCount - 1].objects,
sizeof(Obj) * rt->objectCapacity);
rt->map = realloc(
rt->map, (sizeof(int) * rt->height * rt->width * rt->pd->layerCount));
memcpy(rt->map, rt->states[rt->statesCount - 1].map,
(sizeof(int) * rt->height * rt->width * rt->pd->layerCount));
int bytePerRecord = rt->pd->objectCount / 8 + 1;
/* rt->oMap = realloc(rt->oMap, (rt->height * rt->width * bytePerRecord)); */
memcpy(rt->oMap, rt->states[rt->statesCount - 1].oMap,
(rt->height * rt->width * bytePerRecord));
free(rt->states[rt->statesCount - 1].map);
free(rt->states[rt->statesCount - 1].oMap);
free(rt->states[rt->statesCount - 1].objects);
rt->states[rt->statesCount - 1].objectCount = 0;
rt->states[rt->statesCount - 1].objectCapacity = 0;
rt->statesCount--;
rt->historyCount--;
rt->didUndo = 1;
}
int isMovable(Runtime *rt, int x, int y, int layerIndex) {
if (onBoard(rt, x, y)) {
if (layerIndex == -1) {
printf("err: isMovable without a layer\n");
return 0;
}
int tileIndex = (layerIndex * rt->width * rt->height) + (y * rt->width) + x;
/* if (tileIndex > (rt->pd->layerCount * rt->width * rt->height)) */
if (rt->map[tileIndex] == -1) {
return 1;
} else {
return 0;
}
}
return 0;
}
int deltaX(Direction dir) {
switch (dir) {
case UP:
case DOWN:
case USE:
case NONE:
return 0;
case LEFT:
return -1;
case RIGHT:
return 1;
default:
fprintf(stderr, "Err: deltaX bad direction %i\n", dir);
return 0;
}
}
int deltaY(Direction dir) {
switch (dir) {
case LEFT:
case RIGHT:
case USE:
case NONE:
return 0;
case UP:
return -1;
case DOWN:
return 1;
default:
fprintf(stderr, "Err: deltaY bad direction %i\n", dir);
return 0;
}
}
// This is require for things like,
// [ Selector Block ] [ EmptyTile ] -> [ Selector ] [ FilledTile Block]
// assume Block is `Block = RedBlock or BlueBlock`
// In rules like this `Block` has to refer to the same specific block that was
// matched Even if the rule that part that matched it is in a different
// position.
int specificLegendId(Runtime *rt, int legendId, Match *match) {
// TODO: we should know this when we match, we shouldn't need this
int objId;
for (int i = 0; i < match->partCount; i++) {
if (match->parts[i].newObject == 0) {
objId = rt->objects[match->parts[i].objIndex].objId;
if (aliasLegendContains(legendId, objId)) {
return objId;
}
}
}
return rt->pd->aliasLegend[legendId].objects[0];
}
void debugRuleApplication(Runtime *rt, Match *match) {
printf("Rule: %s\n", ruleString(match->ruleIndex));
for (int i = 0; i < match->partCount; i++) {
printf("MatchPart %i/%i\n new: %i, objIndex: %i, goalId: %i (%s), dir: "
"%i, id: (%i,%i)\n",
i, match->partCount, match->parts[i].newObject,
match->parts[i].objIndex, match->parts[i].goalId,
objectName(match->parts[i].goalId), match->parts[i].actualDirection,
match->parts[i].goalX, match->parts[i].goalY);
}
}
void applyMatch(Runtime *rt, Match *match) {
if (rt->pd->verboseLogging) {
debugRuleApplication(rt, match);
}
if (match->cancel) {
rt->toMoveCount = 0;
undo(rt);
return;
}
if (ruleCommandContains(&rt->pd->rules[match->ruleIndex], AGAIN) &&
match->partCount > 0) {
rt->doAgain = 1;
}
for (int i = 0; i < match->partCount; i++) {
if (match->parts[i].newObject && match->parts[i].goalId != EMPTY_ID) {
/* printf("%i/%i - adding object %s (%i,%i)\n", i, match->partCount,
* objectName(specificLegendId(rt, match->parts[i].goalId, match)),
* match->parts[i].goalX, match->parts[i].goalY); */
addObj(rt, specificLegendId(rt, match->parts[i].goalId, match),
match->parts[i].goalX, match->parts[i].goalY);
if (match->parts[i].goalDirection != -1 &&
(match->parts[i].goalDirection != UNSPECIFIED &&
match->parts[i].goalDirection != NONE)) {
/* printf("%i/%i - adding movement to new object above ^ \n", i,
* match->partCount); */
addToMove(rt, rt->objectCount - 1, match->parts[i].goalDirection);
}
} else {
if (match->parts[i].goalId == EMPTY_ID) {
/* printf("%i/%i - deleting object %s (%i,%i)\n", i, match->partCount,
* objectName(rt->objects[match->parts[i].objIndex].objId),
* match->parts[i].goalX, match->parts[i].goalY); */
rt->objects[match->parts[i].objIndex].deleted = 1;
removeObjFromMap(rt, match->parts[i].objIndex);
rt->deadCount++;
} else if (match->parts[i].goalId == -1 &&
match->parts[i].goalDirection != UNSPECIFIED) {
/* printf("%i/%i - adding movement to existing object %s (%i,%i)\n", i,
* match->partCount,
* objectName(rt->objects[match->parts[i].objIndex].objId),
* match->parts[i].goalX, match->parts[i].goalY); */
addToMove(rt, match->parts[i].objIndex, match->parts[i].goalDirection);
}
}
}
match->partCount = 0;
}
int distance(int i, int j) { return abs(i - j); }
int matchAtDistance(Direction dir, int x, int y, int targetX, int targetY) {
switch (dir) {
case DOWN:
if (x == targetX && targetY > y) {
return 1;
} else {
return 0;
}
case RIGHT:
if (y == targetY && targetX > x) {
return 1;
} else {
return 0;
}
default:
fprintf(stderr, "err: (matchAtDistance) unsupported dir %i", dir);
return 0;
}
}
int directionMatches(Runtime *rt, Rule *rule, int patternId, int partId,
int identId, Direction appDir, int x, int y) {
int foundMoving = 0;
Direction ruleDir =
rule->matchPatterns[patternId].parts[partId].identity[identId].direction;
int legendId =
rule->matchPatterns[patternId].parts[partId].identity[identId].legendId;
for (int i = 0; i < rt->toMoveCount; i++) {
if (rt->objects[rt->toMove[i].objIndex].x == x &&
rt->objects[rt->toMove[i].objIndex].y == y &&
aliasLegendContains(legendId,
rt->objects[rt->toMove[i].objIndex].objId)) {
foundMoving = 1;
if (matchesDirection(ruleDir, appDir,
rt->objects[rt->toMove[i].objIndex].moving, 1)) {
return 1;
}
}
}
if (foundMoving == 0 && (ruleDir == STATIONARY || ruleDir == UNSPECIFIED)) {
return 1;
}
return 0;
}
int resultDirectionMatches(Runtime *rt, Rule *rule, int patternId, int partId,
Direction appDir, int x, int y,
int ignoreUnspecified) {
int matched = 0;
int foundMoving = 0;
for (int identId = 0;
identId < rule->resultPatterns[patternId].parts[partId].identityCount;
identId++) {
matched = 0;
Direction ruleDir = rule->resultPatterns[patternId]
.parts[partId]
.identity[identId]
.direction;
int legendId = rule->resultPatterns[patternId]
.parts[partId]
.identity[identId]
.legendId;
if (legendId == EMPTY_ID || ruleDir == COND_NO) {
return 1;
} else {
for (int i = 0; i < rt->toMoveCount; i++) {
if (rt->objects[rt->toMove[i].objIndex].x == x &&
rt->objects[rt->toMove[i].objIndex].y == y &&
aliasLegendContains(legendId,
rt->objects[rt->toMove[i].objIndex].objId)) {
foundMoving = 1;
if (matchesDirection(ruleDir, appDir,
rt->objects[rt->toMove[i].objIndex].moving,
ignoreUnspecified)) {
matched = 1;
}
}
}
// TODO: we should track if we found it, but it was moving the wrong way
// above
if (foundMoving == 0 && (ruleDir == STATIONARY ||
(ignoreUnspecified && ruleDir == UNSPECIFIED))) {
matched = 1;
}
}
if (matched == 0) {
return 0;
}
}
return 1;
}
int alreadyResultIdentity(Runtime *rt, Rule *rule, int patternId, int partId,
int identId, Direction appDir, int x, int y) {
Direction ruleDir =
rule->resultPatterns[patternId].parts[partId].identity[identId].direction;
int legendId =
rule->resultPatterns[patternId].parts[partId].identity[identId].legendId;
// TODO: this shouldn't work
// we can't just grab the ident at the same index and get the right
// thing semantically it tends to work because humans are likely to list
// the idents in the same order, but we shouldn't rely on that behavior
int ignoreUnspecified;
if (ruleDir == UNSPECIFIED && rule->matchPatterns[patternId]
.parts[partId]
.identity[identId]
.direction != UNSPECIFIED) {
ignoreUnspecified = 0;
} else {
ignoreUnspecified = 1;
}
int matched = 0;
if (legendId == EMPTY_ID) {
return 0;
} else {
if (ruleDir == COND_NO) {
if (legendAt(rt, legendId, x, y) == 0) {
matched = 1;
}
} else {
if (legendAt(rt, legendId, x, y)) {
if (rule->executionTime == LATE ||
resultDirectionMatches(rt, rule, patternId, partId, appDir, x, y,
ignoreUnspecified)) {
matched = 1;
}
}
}
return matched;
}
}
Direction matchLegendDirection(Rule *rule, int patternId, int partId,
int legendId) {
if (rule->matchPatterns[patternId].parts[partId].identityCount <= 0) {
return UNSPECIFIED;
}
for (int i = 0;
i < rule->matchPatterns[patternId].parts[partId].identityCount; i++) {
if (rule->matchPatterns[patternId].parts[partId].identity[i].legendId ==
legendId) {
return rule->matchPatterns[patternId].parts[partId].identity[i].direction;
}
}
return UNSPECIFIED;
}
int resultHasLegendId(Rule *rule, int patternId, int partId, int legendId,
int objId) {
if (rule->resultPatterns[patternId].parts[partId].identityCount == 0) {
return 0;
}
int resultLegendId;
for (int i = 0;
i < rule->resultPatterns[patternId].parts[partId].identityCount; i++) {
resultLegendId =
rule->resultPatterns[patternId].parts[partId].identity[i].legendId;
if (resultLegendId == legendId ||
aliasLegendContains(resultLegendId, objId)) {
return 1;
}
}
return 0;
}
int alreadyResult(Runtime *rt, Rule *rule, int patternId, int partId,
Direction appDir, int x, int y) {
if (rule->resultPatterns[patternId].parts[partId].identityCount <= 0) {
return 0;
}
for (int i = 0;
i < rule->resultPatterns[patternId].parts[partId].identityCount; i++) {
if (alreadyResultIdentity(rt, rule, patternId, partId, i, appDir, x, y) ==
0) {
return 0;
}
}
return 1;
}
void replaceTile(Runtime *rt, Rule *rule, int patternId, int partId,
Direction appDir, Match *match) {
if (rule->resultPatternCount == 0) {
if (ruleCommandContains(rule, CANCEL)) {
match->cancel = 1;
return;
}
return;
}
// remove things in the match side
for (int identId = 0;
identId < rule->matchPatterns[patternId].parts[partId].identityCount;
identId++) {
Direction ruleDir = rule->matchPatterns[patternId]
.parts[partId]
.identity[identId]
.direction;
int legendId =
rule->matchPatterns[patternId].parts[partId].identity[identId].legendId;
// TODO: we must have looked this up already, can we reuse?
int objIndex = legendObjId(rt, legendId, match->targetX, match->targetY);
if (objIndex != -1) {
int resultIncludesSelf = resultHasLegendId(
rule, patternId, partId, legendId, rt->objects[objIndex].objId);
if (ruleDir != COND_NO && legendId != EMPTY_ID &&
resultIncludesSelf == 0) {
match->parts[match->partCount].newObject = 0;
match->parts[match->partCount].goalId = EMPTY_ID;
match->parts[match->partCount].objIndex = objIndex;
// TODO: get this from the board instead
match->parts[match->partCount].objectId = rt->objects[objIndex].objId;
match->parts[match->partCount].goalX = rt->objects[objIndex].x;
match->parts[match->partCount].goalY = rt->objects[objIndex].y;
match->parts[match->partCount].goalDirection = UNSPECIFIED;
match->partCount++;
}
}
}
if (rule->resultPatternCount == 0 || patternId >= rule->resultPatternCount ||
(patternId < rule->resultPatternCount &&
partId >= rule->resultPatterns[patternId].partCount)) {
return;
} else {
if (alreadyResult(rt, rule, patternId, partId, appDir, match->targetX,
match->targetY)) {
return;
}
}
// add the result side at this square
int resultIdentCount =
rule->resultPatterns[patternId].parts[partId].identityCount;
for (int identId = 0; identId < resultIdentCount; identId++) {
Direction ruleDir = rule->resultPatterns[patternId]
.parts[partId]
.identity[identId]
.direction;
int legendId = rule->resultPatterns[patternId]
.parts[partId]
.identity[identId]
.legendId;
Direction matchDir =
matchLegendDirection(rule, patternId, partId, legendId);
int objIndex = legendObjId(rt, legendId, match->targetX, match->targetY);
if (legendId != EMPTY_ID) {
if (ruleDir != COND_NO) {
if (objIndex == -1) {
match->parts[match->partCount].newObject = 1;
match->parts[match->partCount].goalId = legendId;
match->parts[match->partCount].objIndex = -1;
match->parts[match->partCount].objectId = -1;
// TODO: we need to get this from the board
/* match->parts[match->partCount].objectId = specificLegendId(rt,
* legendId, match); */
match->parts[match->partCount].goalX = match->targetX;
match->parts[match->partCount].goalY = match->targetY;
match->parts[match->partCount].goalDirection =
absoluteDirection(appDir, ruleDir);
match->partCount++;
} else {
if ((((matchDir == UP || matchDir == DOWN || matchDir == LEFT ||
matchDir == RIGHT) &&
ruleDir == UNSPECIFIED) ||
(ruleDir == UP || ruleDir == DOWN || ruleDir == LEFT ||
ruleDir == RIGHT)) &&
ruleDir != matchDir) {
match->parts[match->partCount].newObject = 0;
match->parts[match->partCount].goalId =
-1; // aliasLegendObjectId(legendId, 0);
match->parts[match->partCount].goalX = match->targetX;
match->parts[match->partCount].goalY = match->targetY;
match->parts[match->partCount].objIndex = objIndex;
match->parts[match->partCount].objectId =
rt->objects[objIndex].objId;
match->parts[match->partCount].goalDirection =
absoluteDirection(appDir, ruleDir);
match->partCount++;
}
}
} else if (objIndex !=
-1) { // NOT EMPTY AND IS A COND_NO AND THE LEGEND_ID IS THERE
match->parts[match->partCount].newObject = 0;
match->parts[match->partCount].goalId = EMPTY_ID;
match->parts[match->partCount].goalX = match->targetX;
match->parts[match->partCount].goalY = match->targetY;
match->parts[match->partCount].objIndex = objIndex;
match->parts[match->partCount].objectId = rt->objects[objIndex].objId;
match->parts[match->partCount].goalDirection =
absoluteDirection(appDir, ruleDir);
match->partCount++;
}
}
}
}
int partIdentity(Runtime *rt, Rule *rule, int patternId, int partId,
int identId, Direction appDir, Match *match) {
int x = match->targetX;
int y = match->targetY;
Direction ruleDir =
rule->matchPatterns[patternId].parts[partId].identity[identId].direction;
int legendId =
rule->matchPatterns[patternId].parts[partId].identity[identId].legendId;
if (legendId == EMPTY_ID) {
return 1;
} else {
if (ruleDir == COND_NO) {
if (legendAt(rt, legendId, x, y) == 0) {
return 1;
}
} else {
if (legendAt(rt, legendId, x, y)) {
if (rule->executionTime == LATE ||
directionMatches(rt, rule, patternId, partId, identId, appDir, x,
y)) {
return 1;
}
}
}
return 0;
}
}
int partIdentitys(Runtime *rt, Rule *rule, int patternId, int partId,
Direction appDir, Match *match) {
int x = match->targetX;
int y = match->targetY;
// TODO: just double checking
if (onBoard(rt, x, y)) {
for (int i = 0;
i < rule->matchPatterns[patternId].parts[partId].identityCount; i++) {
if (partIdentity(rt, rule, patternId, partId, i, appDir, match) == 0) {
return 0;
}
}
return 1;
}
return 0;
}
int identitysAtDistance(Runtime *rt, Rule *rule, int patternId, int partId,
Direction appDir, int distance, Match *match) {
int dist = distance;
while (onBoard(rt, match->targetX, match->targetY)) {
if (partIdentitys(rt, rule, patternId, partId, appDir, match)) {
return dist;
}
dist++;
match->targetX = match->cursorX + (dist * deltaX(appDir));
match->targetY = match->cursorY + (dist * deltaY(appDir));
}
return -1;
}
int completeMatch(Runtime *rt, Rule *rule, int patternId, Direction appDir,
Match *match) {
int prevPartCount = match->partCount;
int anyDistance = 0;
int distance = 0;
int dist = 0;
int success = 1;
for (int partId = 0; partId < rule->matchPatterns[patternId].partCount;
partId++) {
success = 0;
match->targetX = match->cursorX + (distance * deltaX(appDir));
match->targetY = match->cursorY + (distance * deltaY(appDir));
if (rule->matchPatterns[patternId].parts[partId].isSpread) {
distance--;
anyDistance = 1;
success = 1;
} else {
if (anyDistance == 1) {
dist = identitysAtDistance(rt, rule, patternId, partId, appDir,
distance, match);
if (dist != -1) {
distance = dist;
success = 1;
anyDistance = 0;
} else {
success = 0;
anyDistance = 0;
}
} else {
if (partIdentitys(rt, rule, patternId, partId, appDir, match)) {
success = 1;
}
}
if (success) {
int beforePartCount = match->partCount;
replaceTile(rt, rule, patternId, partId, appDir, match);
int afterPartCount = match->partCount;
// TODO: this sucks
if (rule->matchPatterns[patternId].parts[partId].identityCount > 0 &&
rule->resultPatternCount > 0 &&
rule->resultPatterns[patternId].partCount > 0 &&
rule->resultPatterns[patternId].parts[partId].identityCount > 0 &&
rule->matchPatterns[patternId].parts[partId].identityCount <
rule->resultPatterns[patternId].parts[partId].identityCount &&
beforePartCount >= afterPartCount) {
/* printf("FAILING DUE TO LACK OF CHANFGES\n"); */
success = 0;
match->partCount = prevPartCount;
}
}
}
if (success != 1) {
// failed to find an object that matches the next part, fail
/* printf("Failed to complete match setting %i to %i\n", match->partCount,
* prevPartCount); */
match->partCount = prevPartCount;
match->cancel = 0;
return 0;
}
distance++;
}
return 1;
}
int hasSpace(Runtime *rt, Rule *rule, Pattern *pattern, Match *match) {
int spaceRequired = 0;
for (int p = 0; p < pattern->partCount; p++) {
if (pattern->parts[p].isSpread == 0) {
spaceRequired++;
}
}
switch (rule->directionConstraint) {
case DOWN:
return (match->cursorY + spaceRequired <= rt->height);
case RIGHT:
return (match->cursorX + spaceRequired <= rt->width);
default:
printf("DON't see me ever\n");
return 1;
}
}
int applyPattern(Runtime *rt, Rule *rule, int patternId, Match *match) {
int applied;
int matched = 0;
int prevPartCount = match->partCount;
for (int x = 0; x < rt->width; x++) {
for (int y = 0; y < rt->height; y++) {
match->targetX = match->cursorX = x;
match->targetY = match->cursorY = y;
if (hasSpace(rt, rule, &rule->matchPatterns[patternId], match)) {
applied = completeMatch(rt, rule, patternId, rule->directionConstraint,
match);
if (applied) {
matched = 1;
if (match->partCount > prevPartCount || match->cancel) {
// under some conditions we should be able to NOT return here
// if that is the case we can do all update in one walk of the board
/* if (rule->matchPatterns[patternId].parts[0].identityCount <
* rule->resultPatterns[patternId].parts[0].identityCount) { */
/* return 1; */
/* } */
return 1;
} else {
match->partCount = prevPartCount;
match->cancel = 0;
}
}
}
}
}
match->cursorX = -1;
match->cursorY = -1;
match->targetX = -1;
match->targetY = -1;
return matched;
}
int applyRule(Runtime *rt, Rule *rule, Match *match) {
match->appliedDirection = rule->directionConstraint;
for (int i = 0; i < rule->matchPatternCount; i++) {
if (applyPattern(rt, rule, i, match) == 0) {
return 0;
}
}
if (rt->pd->verboseLogging) {
debugRender(rt, match);