aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-08 19:52:42 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-08 19:52:42 -0500
commitc4915c5cf04246b9e776ada61f6c917f363a78b7 (patch)
tree20d20cd31f63a390fa5d0f64c5f3ff8413d7e16a
parent9d91f9a175293787de13d03522763ffe2dd4a8cb (diff)
downloadsok-c4915c5cf04246b9e776ada61f6c917f363a78b7.tar.bz2
sok-c4915c5cf04246b9e776ada61f6c917f363a78b7.zip
isolate wall access
-rw-r--r--src/libsok/sok-ctx.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/src/libsok/sok-ctx.c b/src/libsok/sok-ctx.c
index d19682a..460c7ad 100644
--- a/src/libsok/sok-ctx.c
+++ b/src/libsok/sok-ctx.c
@@ -15,6 +15,29 @@
)
static bool
+ctx_is_wall(
+ const sok_ctx_t * const ctx,
+ const sok_pos_t pos
+) {
+ return ctx->level.walls[pos.y * ctx->level.size.x + pos.x];
+}
+
+static void
+ctx_set_wall(
+ sok_ctx_t * const ctx,
+ const sok_pos_t pos
+) {
+ ctx->level.walls[pos.y * ctx->level.size.x + pos.x] = true;
+}
+
+static void
+ctx_clear_walls(
+ sok_ctx_t * const ctx
+) {
+ memset(ctx->level.walls, 0, SOK_LEVEL_MAX_WIDTH * SOK_LEVEL_MAX_HEIGHT);
+}
+
+static bool
on_size(
const sok_level_parser_t * const parser,
const sok_pos_t pos
@@ -65,7 +88,7 @@ on_wall(
}
// save wall
- ctx->level.walls[pos.y * ctx->level.size.x + pos.x] = true;
+ ctx_set_wall(ctx, pos);
// return sucess
return true;
@@ -194,7 +217,7 @@ sok_ctx_set_level(
// 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);
+ ctx_clear_walls(ctx);
// parse level
sok_level_parser_t parser;
@@ -224,7 +247,7 @@ tile_is_floor(
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])
+ !ctx_is_wall(ctx, pos)
);
}
@@ -449,12 +472,12 @@ bool sok_ctx_walk(
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,
- };
+ const sok_pos_t pos = {
+ .y = i / ctx->level.size.x,
+ .x = i % ctx->level.size.x,
+ };
+ if (ctx_is_wall(ctx, pos)) {
// emit wall
if (!cbs->on_wall(ctx, pos, data)) {
return false;