-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.all.c
344 lines (288 loc) · 8.83 KB
/
cursor.all.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
// File created: 2011-09-02 23:36:15
#include "cursor.all.h"
#include <alloca.h>
#include <assert.h>
#include <string.h>
#if !MUSHSPACE_93
#include "space/jump-to-box.98.h"
#endif
#if MUSHSPACE_93
#define STATIC_BOX(sp) (&(sp)->box)
#else
#define STATIC_BOX(sp) (&(sp)->static_box)
#endif
#ifdef MUSH_ENABLE_EXPENSIVE_DEBUGGING
#define DEBUG_CHECK(cursor, c) \
assert (mushspace_get(cursor->space, mushcursor_get_pos(cursor)) == c)
#else
#define DEBUG_CHECK(cursor, c)
#endif
#define BAD_CURSOR_MODE MUSH_UNREACHABLE("invalid cursor mode")
#if !MUSHSPACE_93
static bool mushcursor_recalibrate(void*);
#endif
const size_t mushcursor_sizeof = sizeof(mushcursor);
mushcursor* mushcursor_init(void* vp, mushspace* space, mushcoords pos) {
mushcursor *cursor;
if (vp)
cursor = vp;
else if (!(cursor = malloc(sizeof *cursor)))
goto fail;
#if !MUSHSPACE_93
cursor->box_iter_aux_size = mushboxen_iter_aux_size_init;
cursor->box_iter_aux = malloc(cursor->box_iter_aux_size);
if (!cursor->box_iter_aux && cursor->box_iter_aux_size)
goto fail_freecursor;
if (!mushspace_add_invalidatee(space, mushcursor_recalibrate, cursor))
goto fail_freeaux;
#endif
cursor->space = space;
#if MUSHSPACE_93
cursor->rel_pos = mushcoords_sub(pos, MUSHSTATICAABB_BEG);
#else
if (!mushcursor_get_box(cursor, pos))
mushcursor_set_nowhere_pos(cursor, pos);
#endif
return cursor;
#if !MUSHSPACE_93
fail_freeaux:
free(cursor->box_iter_aux);
fail_freecursor:
if (!vp)
free(cursor);
#endif
fail:
return NULL;
}
void mushcursor_free(mushcursor* cursor) {
#if MUSHSPACE_93
(void)cursor;
#else
free(cursor->box_iter_aux);
mushspace_del_invalidatee(cursor->space, cursor);
#endif
}
mushcursor* mushcursor_copy(
void* vp, const mushcursor* cursor, mushspace* space)
{
mushcursor *copy;
if (vp)
copy = vp;
else if (!(copy = malloc(sizeof *copy)))
goto fail;
memcpy(copy, cursor, sizeof *copy);
if (space)
copy->space = space;
#if !MUSHSPACE_93
copy->box_iter_aux = malloc(copy->box_iter_aux_size);
if (!copy->box_iter_aux && copy->box_iter_aux_size)
goto fail_freecopy;
if (!mushspace_add_invalidatee(copy->space, mushcursor_recalibrate, copy))
goto fail_freeaux;
#endif
#if !MUSHSPACE_93
// We assume that cursor was already in a valid state, so we don't need to
// fix the position if the space doesn't change.
if (copy->space != cursor->space)
mushcursor_recalibrate(copy);
#endif
return copy;
#if !MUSHSPACE_93
fail_freeaux:
free(copy->box_iter_aux);
fail_freecopy:
if (!vp)
free(copy);
#endif
fail:
return NULL;
}
mushcoords mushcursor_get_pos(const mushcursor* cursor) {
switch (MUSHCURSOR_MODE(cursor)) {
case MushCursorMode_static:
return mushcoords_add(cursor->rel_pos, MUSHSTATICAABB_BEG);
#if !MUSHSPACE_93
case MushCursorMode_dynamic:
return mushcoords_add(cursor->rel_pos, cursor->obeg);
#endif
}
BAD_CURSOR_MODE;
}
void mushcursor_set_pos(mushcursor* cursor, mushcoords pos) {
switch (MUSHCURSOR_MODE(cursor)) {
case MushCursorMode_static:
cursor->rel_pos = mushcoords_sub(pos, MUSHSTATICAABB_BEG);
return;
#if !MUSHSPACE_93
case MushCursorMode_dynamic:
cursor->rel_pos = mushcoords_sub(pos, cursor->obeg);
return;
#endif
}
BAD_CURSOR_MODE;
}
bool mushcursor_in_box(const mushcursor* cursor) {
switch (MUSHCURSOR_MODE(cursor)) {
case MushCursorMode_static:
return mushbounds_contains(&MUSHSTATICAABB_REL_BOUNDS, cursor->rel_pos);
#if !MUSHSPACE_93
case MushCursorMode_dynamic:
return mushbounds_contains(&cursor->rel_bounds, cursor->rel_pos);
#endif
}
BAD_CURSOR_MODE;
}
#if !MUSHSPACE_93
bool mushcursor_get_box(mushcursor* cursor, mushcoords pos) {
if (mushstaticaabb_contains(pos)) {
cursor->mode = MushCursorMode_static;
mushcursor_tessellate(cursor, pos);
return true;
}
mushspace *sp = cursor->space;
cursor->box_iter =
mushboxen_get_iter(&sp->boxen, pos, cursor->box_iter_aux);
if (!mushboxen_iter_is_null(cursor->box_iter)) {
cursor->box = mushboxen_iter_box(cursor->box_iter);
cursor->mode = MushCursorMode_dynamic;
mushcursor_tessellate(cursor, pos);
return true;
}
return false;
}
#endif
mushcell mushcursor_get(mushcursor* cursor) {
#if !MUSHSPACE_93
if (!mushcursor_in_box(cursor)
&& !mushcursor_get_box(cursor, mushcursor_get_pos(cursor)))
{
DEBUG_CHECK(cursor, ' ');
return ' ';
}
#endif
return mushcursor_get_unsafe(cursor);
}
mushcell mushcursor_get_unsafe(mushcursor* cursor) {
assert (mushcursor_in_box(cursor));
mushspace *sp = cursor->space;
mushcell c;
switch (MUSHCURSOR_MODE(cursor)) {
case MushCursorMode_static:
c = mushstaticaabb_get_no_offset(STATIC_BOX(sp), cursor->rel_pos);
break;
#if !MUSHSPACE_93
case MushCursorMode_dynamic:
c = mushaabb_get_no_offset(cursor->box, cursor->rel_pos);
break;
#endif
default: BAD_CURSOR_MODE;
}
DEBUG_CHECK(cursor, c);
return c;
}
void mushcursor_put(mushcursor* cursor, mushcell c) {
#if !MUSHSPACE_93
if (!mushcursor_in_box(cursor)) {
mushcoords pos = mushcursor_get_pos(cursor);
if (!mushcursor_get_box(cursor, pos)) {
mushspace_put(cursor->space, pos, c);
DEBUG_CHECK(cursor, c);
return;
}
}
#endif
mushcursor_put_unsafe(cursor, c);
}
void mushcursor_put_unsafe(mushcursor* cursor, mushcell c) {
assert (mushcursor_in_box(cursor));
mushspace *sp = cursor->space;
switch (MUSHCURSOR_MODE(cursor)) {
case MushCursorMode_static:
mushstaticaabb_put_no_offset(STATIC_BOX(sp), cursor->rel_pos, c);
break;
#if !MUSHSPACE_93
case MushCursorMode_dynamic:
mushaabb_put_no_offset(cursor->box, cursor->rel_pos, c);
break;
#endif
default: BAD_CURSOR_MODE;
}
DEBUG_CHECK(cursor, c);
}
void mushcursor_advance(mushcursor* cursor, mushcoords delta) {
mushcoords_add_into(&cursor->rel_pos, delta);
}
void mushcursor_retreat(mushcursor* cursor, mushcoords delta) {
mushcoords_sub_into(&cursor->rel_pos, delta);
}
#if !MUSHSPACE_93
static bool mushcursor_recalibrate(void* p) {
mushcursor *cursor = p;
const size_t aux_size = mushboxen_iter_aux_size(&cursor->space->boxen);
if (aux_size > cursor->box_iter_aux_size) {
void *aux = realloc(cursor->box_iter_aux, aux_size);
if (!aux)
return false;
cursor->box_iter_aux = aux;
cursor->box_iter_aux_size = aux_size;
}
mushcoords pos = mushcursor_get_pos(cursor);
if (!mushcursor_get_box(cursor, pos))
mushcursor_set_nowhere_pos(cursor, pos);
return true;
}
#endif
#if MUSHSPACE_93
void mushcursor93_wrap(mushcursor* cursor) {
cursor->rel_pos.x %= MUSHSTATICAABB_SIZE.x;
cursor->rel_pos.y %= MUSHSTATICAABB_SIZE.y;
}
#else
void mushcursor_tessellate(mushcursor* cursor, mushcoords pos) {
mushspace *sp = cursor->space;
void *aux = alloca(mushboxen_iter_aux_size(&sp->boxen));
switch (MUSHCURSOR_MODE(cursor)) {
case MushCursorMode_static:
cursor->rel_pos = mushcoords_sub(pos, MUSHSTATICAABB_BEG);
break;
case MushCursorMode_dynamic: {
// cursor->box now becomes only a view. it shares its data array with the
// original box, but has different bounds. In addition, it is weird: its
// width and height are not its own, so that index calculation in the
// _no_offset functions works correctly.
//
// BE CAREFUL! Only the *_no_offset functions work properly on it, since
// the others (notably, _get_idx and thereby _get and _put) tend to
// depend on the bounds matching the data and the width/height being
// sensible.
mushbounds bounds = cursor->box->bounds;
cursor->obeg = bounds.beg;
// Here we need to tessellate only with the boxes above cursor->box.
mushbounds_tessellate(&bounds, pos, &MUSHSTATICAABB_BOUNDS);
for (mushboxen_iter_above it =
mushboxen_iter_above_init(&sp->boxen, cursor->box_iter, aux);
!mushboxen_iter_above_done( it, &sp->boxen);
mushboxen_iter_above_next(&it, &sp->boxen))
{
mushbounds_tessellate(
&bounds, pos, &mushboxen_iter_above_box(it)->bounds);
}
cursor->rel_pos = mushcoords_sub(pos, cursor->obeg);
cursor->rel_bounds =
(mushbounds){mushcoords_sub(bounds.beg, cursor->obeg),
mushcoords_sub(bounds.end, cursor->obeg)};
break;
}
default: BAD_CURSOR_MODE;
}
}
#endif
void mushcursor_set_nowhere_pos(mushcursor* cursor, mushcoords pos) {
#if !MUSHSPACE_93
// Since we are "nowhere", we can set an arbitrary mode: any functionality
// that cares about the mode handles the not-in-a-box case anyway. Prefer
// static because it's the fastest to work with.
cursor->mode = MushCursorMode_static;
#endif
mushcursor_set_pos(cursor, pos);
}