summaryrefslogtreecommitdiff
path: root/kon/konrmmap.c
blob: 5e895a3c81b69fe5e97295eeaead6603382b8457 (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
/* SPDX-License-Identifier: MIT */
/*
 * konrmmap.c
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "konrmmap.h"

#include <stddef.h>

/*** pages ***/

static int pageActive(const kon_rmPage_t *page, int mapId, int eventId, const kon_rmState_t *state)
{
	if (page->hasSwitch1 && !(state && state->switchOn && state->switchOn(page->switch1, state->user))) return 0;
	if (page->hasSwitch2 && !(state && state->switchOn && state->switchOn(page->switch2, state->user))) return 0;
	if (page->hasVariable) {
		int value = state && state->variable ? state->variable(page->variable, state->user) : 0;

		if (value < page->variableValue) return 0;
	}
	if (page->hasSelfSwitch && !(state && state->selfSwitch && state->selfSwitch(mapId, eventId, page->selfSwitch, state->user))) return 0;
	if (page->hasItem && !(state && state->hasItem && state->hasItem(page->item, state->user))) return 0;
	if (page->hasActor && !(state && state->hasActor && state->hasActor(page->actor, state->user))) return 0;
	return 1;
}

int kon_rmEventPage(const kon_rmEvent_t *event, int mapId, const kon_rmState_t *state)
{
	int i;

	if (!event || !event->pages) return -1;
	for (i = event->pageCount - 1; i >= 0; i--) {
		if (pageActive(&event->pages[i], mapId, event->id, state)) return i;
	}
	return -1;
}

/*** tiles ***/

int kon_rmTile(const kon_rmMap_t *map, int layer, int x, int y)
{
	if (!map || !map->tiles || layer < 0 || layer >= KON_RM_LAYERS) return 0;
	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;
}