diff options
| author | KonAki <konaki@anokonaki.org> | 2026-10-15 14:57:24 +0800 |
|---|---|---|
| committer | KonAki <konaki@anokonaki.org> | 2026-10-15 14:57:24 +0800 |
| commit | eef87380a41357bc3bb52149ec33f1787ebd2b40 (patch) | |
| tree | 17a9eac41d940c2733a24a60644eee0cd9798973 | |
| parent | dcdff7cbf60eb04546c9174ae12c067bc6b4a405 (diff) | |
add walking and terrain checks to rmmap
| -rw-r--r-- | kon/konrmmap.c | 99 | ||||
| -rw-r--r-- | kon/konrmmap.h | 41 |
2 files changed, 140 insertions, 0 deletions
diff --git a/kon/konrmmap.c b/kon/konrmmap.c index 0487d84..5e895a3 100644 --- a/kon/konrmmap.c +++ b/kon/konrmmap.c @@ -63,3 +63,102 @@ int kon_rmTile(const kon_rmMap_t *map, int layer, int x, int y) if (x < 0 || y < 0 || x >= map->width || y >= map->height) return 0; return map->tiles[((size_t)layer * (size_t)map->height + (size_t)y) * (size_t)map->width + (size_t)x]; } + +/*** walking ***/ + +int kon_rmFlags(const kon_rmTileset_t *tileset, int tile) +{ + if (!tileset || !tileset->flags || tile < 0 || tile >= tileset->flagCount) return 0; + return tileset->flags[tile]; +} + +int kon_rmRoundX(const kon_rmMap_t *map, int x) +{ + if (!map || map->width < 1 || (map->scrollType != 2 && map->scrollType != 3)) return x; + return ((x % map->width) + map->width) % map->width; +} + +int kon_rmRoundY(const kon_rmMap_t *map, int y) +{ + if (!map || map->height < 1 || (map->scrollType != 1 && map->scrollType != 3)) return y; + return ((y % map->height) + map->height) % map->height; +} + +int kon_rmValid(const kon_rmMap_t *map, int x, int y) +{ + if (!map) return 0; + x = kon_rmRoundX(map, x); + y = kon_rmRoundY(map, y); + return x >= 0 && y >= 0 && x < map->width && y < map->height; +} + +/* the layers from the top one down, like RPG Maker looks at them */ +int kon_rmPassable(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y, int dir) +{ + int bit, layer; + + if (!map || dir < 2 || dir > 8 || dir % 2) return 0; + x = kon_rmRoundX(map, x); + y = kon_rmRoundY(map, y); + bit = (1 << (dir / 2 - 1)) & 0x0f; + for (layer = 3; layer >= 0; layer--) { + int flags = kon_rmFlags(tileset, kon_rmTile(map, layer, x, y)); + + if (flags & KON_RM_FLAG_NO_EFFECT) continue; + if ((flags & bit) == 0) return 1; + if ((flags & bit) == bit) return 0; + } + return 0; +} + +int kon_rmCanWalk(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y, int dir) +{ + int dx = 0, dy = 0, x2, y2; + + if (!map) return 0; + switch (dir) { + case KON_RM_DOWN: dy = 1; break; + case KON_RM_LEFT: dx = -1; break; + case KON_RM_RIGHT: dx = 1; break; + case KON_RM_UP: dy = -1; break; + default: return 0; + } + x2 = kon_rmRoundX(map, x + dx); + y2 = kon_rmRoundY(map, y + dy); + if (!kon_rmValid(map, x2, y2)) return 0; + if (!kon_rmPassable(map, tileset, x, y, dir)) return 0; + return kon_rmPassable(map, tileset, x2, y2, 10 - dir); +} + +int kon_rmTerrainTag(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y) +{ + int layer; + + if (!kon_rmValid(map, x, y)) return 0; + x = kon_rmRoundX(map, x); + y = kon_rmRoundY(map, y); + for (layer = 3; layer >= 0; layer--) { + int tag = kon_rmFlags(tileset, kon_rmTile(map, layer, x, y)) >> 12; + + if (tag > 0) return tag; + } + return 0; +} + +int kon_rmRegion(const kon_rmMap_t *map, int x, int y) +{ + return kon_rmTile(map, 5, x, y); +} + +int kon_rmHasFlag(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y, int flag) +{ + int layer; + + if (!kon_rmValid(map, x, y)) return 0; + x = kon_rmRoundX(map, x); + y = kon_rmRoundY(map, y); + for (layer = 0; layer < 4; layer++) { + if (kon_rmFlags(tileset, kon_rmTile(map, layer, x, y)) & flag) return 1; + } + return 0; +} diff --git a/kon/konrmmap.h b/kon/konrmmap.h index 6274822..27bbe40 100644 --- a/kon/konrmmap.h +++ b/kon/konrmmap.h @@ -144,6 +144,47 @@ int kon_rmEventPage(const kon_rmEvent_t *event, int mapId, const kon_rmState_t * /* layer is 0 to 5, the four tile layers, the shadows and the regions. 0 outside of the map or for a layer that is not there */ int kon_rmTile(const kon_rmMap_t *map, int layer, int x, int y); +/*** walking ***/ + +/* the directions RPG Maker uses, the numbers on a keypad */ +#define KON_RM_DOWN 2 +#define KON_RM_LEFT 4 +#define KON_RM_RIGHT 6 +#define KON_RM_UP 8 + +/* the flags of a tile in a tileset, the tile layers of the map read from the top one down. a tile past the end of the flags + has none. these are RPG Maker's own bits, in kon_rmFlags and below */ +#define KON_RM_FLAG_NO_EFFECT 0x10 /* a star in the editor: this tile does not decide whether you can walk */ +#define KON_RM_FLAG_LADDER 0x20 +#define KON_RM_FLAG_BUSH 0x40 +#define KON_RM_FLAG_COUNTER 0x80 +#define KON_RM_FLAG_DAMAGE 0x100 + +int kon_rmFlags(const kon_rmTileset_t *tileset, int tile); + +/* is x, y inside the map, once a map that loops has wrapped it. loops are the map's scroll type */ +int kon_rmValid(const kon_rmMap_t *map, int x, int y); + +/* wraps x or y for a map that loops that way, other positions stay as they are */ +int kon_rmRoundX(const kon_rmMap_t *map, int x); +int kon_rmRoundY(const kon_rmMap_t *map, int y); + +/* can something on x, y leave this tile going dir, as far as the tiles say. no events and no outside of the map here */ +int kon_rmPassable(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y, int dir); + +/* can it step from x, y to the next tile in dir: the next tile is on the map, and both tiles let it through, the one it is + on on the way out and the next on the way in. what an event on the tile would say about it is for the game to ask */ +int kon_rmCanWalk(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y, int dir); + +/* the terrain tag a tile is marked with in the editor, 0 for none, 0 outside of the map. the top layer that has one wins */ +int kon_rmTerrainTag(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y); + +/* the region painted on x, y, 0 for none */ +int kon_rmRegion(const kon_rmMap_t *map, int x, int y); + +/* 1 when any of the layers of that tile has the flag, 0 outside of the map */ +int kon_rmHasFlag(const kon_rmMap_t *map, const kon_rmTileset_t *tileset, int x, int y, int flag); + #ifdef __cplusplus } #endif |
