aboutsummaryrefslogtreecommitdiff
path: root/src/sok-ctx.c
blob: e4a922b039d786aaa5f0ddfff9b3d1bac1c503c6 (plain)
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
#include <stdbool.h>
#include <stdio.h> // debugging
#include <string.h> // memset()
#include "sok.h"

#define UNUSED(a) ((void) (a))

#define VALID_POS(pos) ( \
  (pos).x < SOK_LEVEL_MAX_WIDTH && \
  (pos).y < SOK_LEVEL_MAX_HEIGHT \
)

#define POINTS_EQUAL(a, b) ( \
  ((a).x == (b).x) && \
  ((a).y == (b).y) \
)

static bool
on_size(
  const sok_level_parser_t * const parser,
  const sok_pos_t pos
) {
  sok_ctx_t *ctx = (sok_ctx_t*) parser->user_data;

  if (!VALID_POS(pos)) {
    // size out of bounds
    return false;
  }

  // save width and height
  ctx->level.size = pos;

  // return success
  return true;
}

static bool
on_home(
  const sok_level_parser_t * const parser,
  const sok_pos_t pos
) {
  sok_ctx_t *ctx = (sok_ctx_t*) parser->user_data;

  if (!VALID_POS(pos)) {
    // position out of bounds
    return false;
  }

  // save start position
  ctx->level.home = pos;

  // return success
  return true;
}

static bool
on_wall(
  const sok_level_parser_t * const parser,
  const sok_pos_t pos
) {
  sok_ctx_t *ctx = (sok_ctx_t*) parser->user_data;

  if (!VALID_POS(pos)) {
    // position out of bounds
    return false;
  }

  // save wall
  ctx->level.walls[pos.y * ctx->level.size.x + pos.x] = true;

  // return sucess
  return true;
}

static bool
on_goal(
  const sok_level_parser_t * const parser,
  const sok_pos_t pos
) {
  sok_ctx_t *ctx = (sok_ctx_t*) parser->user_data;

  if (!VALID_POS(pos)) {
    // position out of bounds
    return false;
  }

  if (ctx->level.num_goals >= SOK_LEVEL_MAX_GOALS - 1) {
    // too many goals
    return false;
  }

  // save goal
  ctx->level.goals[ctx->level.num_goals++] = pos;

  // return sucess
  return true;
}

static bool
on_box(
  const sok_level_parser_t * const parser,
  const sok_pos_t pos
) {
  sok_ctx_t *ctx = (sok_ctx_t*) parser->user_data;

  if (!VALID_POS(pos)) {
    // position out of bounds
    return false;
  }

  if (ctx->level.num_boxes >= SOK_LEVEL_MAX_BOXES - 1) {
    // too many boxes
    return false;
  }

  // save box
  ctx->level.boxes[ctx->level.num_boxes++] = pos;

  // return sucess
  return true;
}

static bool
on_junk(
  const sok_level_parser_t * const parser,
  const size_t ofs,
  const char tile
) {
  UNUSED(parser);
  UNUSED(ofs);
  UNUSED(tile);

  // TODO

  // return sucess
  return true;
}

static const sok_level_parser_cbs_t
LEVEL_PARSER_CBS = {
  .on_size = on_size,
  .on_home = on_home,
  .on_wall = on_wall,
  .on_goal = on_goal,
  .on_box  = on_box,
  .on_junk = on_junk,
};

void
sok_ctx_init(sok_ctx_t * const ctx, void *user_data) {
  memset(ctx, 0, sizeof(sok_ctx_t));
  ctx->user_data = user_data;
}

bool
sok_ctx_set_level(
  sok_ctx_t * const ctx,
  const char * const str
) {

  // clear level
  ctx->level.num_goals = 0;
  ctx->level.num_boxes = 0;
  memset(ctx->level.walls, 0, SOK_LEVEL_MAX_WIDTH * SOK_LEVEL_MAX_HEIGHT);

  // parse level
  sok_level_parser_t parser;
  sok_level_parser_init(&parser, &LEVEL_PARSER_CBS, ctx);
  if (!sok_level_parser_parse(&parser, str, strlen(str))) {
    // return failure
    return false;
  }

  // init moves
  ctx->num_moves = 0;

  // init player
  ctx->home = ctx->level.home;

  // init boxes
  memcpy(ctx->boxes, ctx->level.boxes, ctx->level.num_boxes * sizeof(sok_pos_t));

  // return success
  return true;
}

static bool
tile_is_floor(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos
) {
  return (
    (pos.x < SOK_LEVEL_MAX_WIDTH - 1) &&
    (pos.y < SOK_LEVEL_MAX_HEIGHT - 1) &&
    !(ctx->level.walls[pos.y * ctx->level.size.x + pos.x])
  );
}

static bool
tile_is_box(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos
) {
  for (size_t i = 0; i < ctx->level.num_boxes; i++) {
    // fprintf(stderr, "D: ctx->boxes[%zu] = {%u, %u}\n", i, ctx->boxes[i].x, ctx->boxes[i].y);
    if (POINTS_EQUAL(ctx->boxes[i], pos)) {
      return true;
    }
  }

  return false;
}

static bool
tile_is_goal(
  const sok_ctx_t * const ctx,
  const sok_pos_t pos
) {
  for (size_t i = 0; i < ctx->level.num_goals; i++) {
    if (POINTS_EQUAL(pos, ctx->level.goals[i])) {
      return true;
    }
  }

  return false;
}

static bool
sok_can_move(
  sok_ctx_t * const ctx,
  const sok_dir_t dir
) {
  if (ctx->num_moves >= SOK_CTX_MAX_MOVES - 1) {
    // no more move slots available
    return false;
  }

  if (dir == SOK_DIR_UP) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x, .y = ctx->home.y - 1 },
      { .x = ctx->home.x, .y = ctx->home.y - 2 },
    };

    fprintf(
      stderr,
      "D: ctx->home = {%u, %u}, ps[] = {{%u, %u}, {%u, %u}};\n"
      "D: tile_is_floor(ctx, ps[0]) = %c, tile_is_box(ctx, ps[0]) = %c\n"
      "D: tile_is_floor(ctx, ps[1]) = %c, tile_is_box(ctx, ps[1]) = %c\n"
      "",
      ctx->home.x, ctx->home.y,
      ps[0].x, ps[0].y,
      ps[1].x, ps[1].y,
      tile_is_floor(ctx, ps[0]) ? 't' : 'f',
      tile_is_box(ctx, ps[0]) ? 't' : 'f',
      tile_is_floor(ctx, ps[1]) ? 't' : 'f',
      tile_is_box(ctx, ps[1]) ? 't' : 'f'
    );

    return ((
      (ctx->home.y > 0) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) || (
      (ctx->home.y > 1) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ));
  } else if (dir == SOK_DIR_DOWN) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x, .y = ctx->home.y + 1 },
      { .x = ctx->home.x, .y = ctx->home.y + 2 },
    };

    return ((
      (ctx->home.y < SOK_LEVEL_MAX_HEIGHT - 1) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) || (
      (ctx->home.y < SOK_LEVEL_MAX_HEIGHT - 2) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ));
  } else if (dir == SOK_DIR_LEFT) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x - 1, .y = ctx->home.y },
      { .x = ctx->home.x - 2, .y = ctx->home.y },
    };

    return ((
      (ctx->home.x > 0) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) || (
      (ctx->home.x > 1) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ));
  } else if (dir == SOK_DIR_RIGHT) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x + 1, .y = ctx->home.y },
      { .x = ctx->home.x + 2, .y = ctx->home.y },
    };

    return ((
      (ctx->home.x < SOK_LEVEL_MAX_WIDTH - 1) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) || (
      (ctx->home.x < SOK_LEVEL_MAX_WIDTH - 2) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ));
  }

  // return failure
  return false;
}

static bool
sok_ctx_push_move(
  sok_ctx_t * const ctx,
  const sok_dir_t dir,
  const bool is_push
) {
  if (ctx->num_moves >= SOK_CTX_MAX_MOVES - 1) {
    return false;
  }

  ctx->moves[ctx->num_moves].pos = ctx->home;
  ctx->moves[ctx->num_moves].dir = dir;
  ctx->moves[ctx->num_moves].is_push = is_push;
  ctx->num_moves++;

  return true;
}

static bool
sok_ctx_pop_move(
  sok_ctx_t * const ctx,
  sok_move_t * const ret
) {
  if (!ctx->num_moves) {
    // return failure
    return false;
  }

  // decriment moves
  ctx->num_moves--;

  if (ret) {
    // save popped move
    *ret = ctx->moves[ctx->num_moves];
  }

  // return success
  return true;
}

static bool
sok_ctx_move_box(
  sok_ctx_t * const ctx,
  const sok_pos_t old_pos,
  const sok_pos_t new_pos
) {
  for (size_t i = 0; i < ctx->level.num_boxes; i++) {
    if (POINTS_EQUAL(ctx->boxes[i], old_pos)) {
      ctx->boxes[i] = new_pos;
      return true;
    }
  }

  return false;
}

bool
sok_ctx_is_done(
  sok_ctx_t * const ctx
) {
  for (size_t i = 0; i < ctx->level.num_goals; i++) {
    bool on_goal = false;

    for (size_t j = 0; !on_goal && j < ctx->level.num_boxes; j++) {
      if (POINTS_EQUAL(ctx->level.goals[i], ctx->boxes[j])) {
        // found box on this goal
        on_goal = true;
      }
    }

    if (!on_goal) {
      // no box on this goal, return false
      return false;
    }
  }

  // return success
  return true;
}

bool
sok_ctx_move(
  sok_ctx_t * const ctx,
  const sok_dir_t dir
) {
  if (!sok_can_move(ctx, dir)) {
    fprintf(stderr, "can_move failed\n");
    return false;
  }

  if (dir == SOK_DIR_UP) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x, .y = ctx->home.y - 1 },
      { .x = ctx->home.x, .y = ctx->home.y - 2 },
    };

    if (
      (ctx->home.y > 0) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) { 
      // push move
      if (!sok_ctx_push_move(ctx, dir, false)) {
        return false;
      }

      // update position
      ctx->home.y--;

      // return success
      return true;
    } else if (
      (ctx->home.y > 1) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ) { 
      // push move
      if (!sok_ctx_push_move(ctx, dir, true)) {
        return false;
      }

      // move box
      if (!sok_ctx_move_box(ctx, ps[0], ps[1])) {
        return false;
      }

      // update position
      ctx->home.y--;

      // return success
      return true;
    }
  } else if (dir == SOK_DIR_DOWN) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x, .y = ctx->home.y + 1 },
      { .x = ctx->home.x, .y = ctx->home.y + 2 },
    };

    if (
      (ctx->home.y < SOK_LEVEL_MAX_HEIGHT - 1) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) {
      // push move
      if (!sok_ctx_push_move(ctx, dir, false)) {
        return false;
      }

      // update position
      ctx->home.y++;

      // return success
      return true;
    } else if (
      (ctx->home.y < SOK_LEVEL_MAX_HEIGHT - 2) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ) {
      // push move
      if (!sok_ctx_push_move(ctx, dir, true)) {
        return false;
      }

      // move box
      if (!sok_ctx_move_box(ctx, ps[0], ps[1])) {
        return false;
      }

      // update position
      ctx->home.y++;

      // return success
      return true;
    }
  } else if (dir == SOK_DIR_LEFT) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x - 1, .y = ctx->home.y },
      { .x = ctx->home.x - 2, .y = ctx->home.y },
    };

    if (
      (ctx->home.x > 1) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) { 
      // push move
      if (!sok_ctx_push_move(ctx, dir, false)) {
        return false;
      }

      // update position
      ctx->home.x--;

      // return success
      return true;
    } else if (
      (ctx->home.x > 1) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) &&
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ) {
      // push move
      if (!sok_ctx_push_move(ctx, dir, true)) {
        return false;
      }

      // move box
      if (!sok_ctx_move_box(ctx, ps[0], ps[1])) {
        return false;
      }

      // update position
      ctx->home.x--;

      // return success
      return true;
    }
  } else if (dir == SOK_DIR_RIGHT) {
    const sok_pos_t ps[2] = {
      { .x = ctx->home.x + 1, .y = ctx->home.y },
      { .x = ctx->home.x + 2, .y = ctx->home.y },
    };

    if (
      (ctx->home.x < SOK_LEVEL_MAX_WIDTH - 1) &&
      tile_is_floor(ctx, ps[0]) &&
      !tile_is_box(ctx, ps[0])
    ) {
      // push move
      if (!sok_ctx_push_move(ctx, dir, false)) {
        return false;
      }

      // update position
      ctx->home.x++;

      // return success
      return true;
    } else if (
      (ctx->home.x < SOK_LEVEL_MAX_WIDTH - 2) &&
      tile_is_floor(ctx, ps[0]) &&
      tile_is_box(ctx, ps[0]) && 
      tile_is_floor(ctx, ps[1]) &&
      !tile_is_box(ctx, ps[1])
    ) {
      // push move
      if (!sok_ctx_push_move(ctx, dir, true)) {
        return false;
      }

      // move box
      if (!sok_ctx_move_box(ctx, ps[0], ps[1])) {
        return false;
      }

      // update position
      ctx->home.x++;

      // return success
      return true;
    }
  }

  // return failure
  return false;
}

bool
sok_ctx_undo(
  sok_ctx_t * const ctx
) {
  sok_move_t move;
  if (!sok_ctx_pop_move(ctx, &move)) {
    // return failure
    return false;
  }

  if (move.is_push) {
    switch (move.dir) {
    case SOK_DIR_UP:
      {
        const sok_pos_t box_pos = { move.pos.x, move.pos.y - 2 };
        sok_ctx_move_box(ctx, box_pos, ctx->home);
      }

      break;
    case SOK_DIR_DOWN:
      {
        const sok_pos_t box_pos = { move.pos.x, move.pos.y + 2 };
        sok_ctx_move_box(ctx, box_pos, ctx->home);
      }

      break;
    case SOK_DIR_LEFT:
      {
        const sok_pos_t box_pos = { move.pos.x - 2, move.pos.y };
        sok_ctx_move_box(ctx, box_pos, ctx->home);
      }

      break;
    case SOK_DIR_RIGHT:
      {
        const sok_pos_t box_pos = { move.pos.x + 2, move.pos.y };
        sok_ctx_move_box(ctx, box_pos, ctx->home);
      }

      break;
    default:
      // never reached
      return false;
    }
  }

  // update position
  ctx->home = move.pos;

  // return success
  return true;
}

bool sok_ctx_walk(
  const sok_ctx_t * const ctx,
  const sok_ctx_walk_cbs_t * const cbs,
  void * const data
) {
  if (!cbs) {
    return false;
  }

  if (cbs->on_size) {
    // emit size
    if (!cbs->on_size(ctx, ctx->level.size, data)) {
      return false;
    }
  }

  if (cbs->on_home) {
    const bool has_goal = tile_is_goal(ctx, ctx->home);

    // emit home
    if (!cbs->on_home(ctx, ctx->home, has_goal, data)) {
      return false;
    }
  }

  if (cbs->on_wall) {
    // walk walls
    for (size_t i = 0; i < (ctx->level.size.x * ctx->level.size.y); i++) {
      if (ctx->level.walls[i]) {
        const sok_pos_t pos = {
          .y = i / ctx->level.size.x,
          .x = i % ctx->level.size.x,
        };

        // emit wall
        if (!cbs->on_wall(ctx, pos, data)) {
          return false;
        }
      }
    }
  }

  if (cbs->on_goal) {
    // walk goals
    for (size_t i = 0; i < ctx->level.num_goals; i++) {
      const bool has_player = POINTS_EQUAL(ctx->level.goals[i], ctx->home);
      const bool has_box = tile_is_box(ctx, ctx->level.goals[i]);

      // emit goal
      if (!cbs->on_goal(ctx, ctx->level.goals[i], has_player, has_box, data)) {
        return false;
      }
    }
  }

  if (cbs->on_box) {
    // walk boxes
    for (size_t i = 0; i < ctx->level.num_boxes; i++) {
      const bool has_goal = tile_is_goal(ctx, ctx->boxes[i]);

      // emit box
      if (!cbs->on_box(ctx, ctx->boxes[i], has_goal, data)) {
        return false;
      }
    }
  }

  if (cbs->on_move) {
    // walk moves
    for (size_t i = 0; i < ctx->num_moves; i++) {
      // emit move
      if (!cbs->on_move(ctx, ctx->moves[i], data)) {
        return false;
      }
    }
  }

  // return success
  return true;
}