forked from Rox64/NXEngine
-
Notifications
You must be signed in to change notification settings - Fork 3
/
slope.cpp
385 lines (318 loc) · 8.38 KB
/
slope.cpp
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
#include "nx.h"
#include "slope.fdh"
//#define DEBUG_SLOPE
static SlopeTable slopetable[SLOPE_LAST+1];
// creates the slope tables
bool initslopetable(void)
{
int x, y, ya, mx;
int curtable, opposing_table;
int inverttable, invertfliptable;
int flipmx, flipy;
stat("initslopetable: generating slopetables.");
memset(slopetable, 0, sizeof(slopetable));
ya = TILE_H-1;
for(x=0;x<TILE_W*2;x++)
{
if (x < TILE_W)
{
mx = x;
curtable = SLOPE_FWD1;
opposing_table = SLOPE_BACK2;
inverttable = SLOPE_CEIL_BACK1;
invertfliptable = SLOPE_CEIL_FWD2;
}
else
{
mx = x - TILE_W;
curtable = SLOPE_FWD2;
opposing_table = SLOPE_BACK1;
inverttable = SLOPE_CEIL_BACK2;
invertfliptable = SLOPE_CEIL_FWD1;
}
for(y=ya;y<TILE_H;y++)
{
flipmx = TILE_W-1-mx;
flipy = TILE_H-1-y;
slopetable[curtable].table[mx][y] = 1;
slopetable[opposing_table].table[flipmx][y] = 1;
slopetable[inverttable].table[mx][flipy] = 1;
slopetable[invertfliptable].table[flipmx][flipy] = 1;
}
if (x & 1) ya--;
}
return 0;
}
/*
void c------------------------------() {}
*/
// X and Y are non-CSFd pixel coordinates relative to the upper-left of the map.
// if the given pixel is inside of a slope, returns the slope type 1-8. else, returns 0.
uint8_t ReadSlopeTable(int x, int y)
{
int mx, my;
int slopetype;
uint8_t t;
#ifdef DEBUG_SLOPE
DrawSlopeTablesOnTiles();
#endif
// convert coordinates into a tile and check if the tile is a slope tile
mx = (x / TILE_W);
my = (y / TILE_H);
if (mx < 0 || my < 0 || mx >= map.xsize || my >= map.ysize)
return 0;
t = map.tiles[mx][my];
if (tileattr[t] & TA_SLOPE)
{
slopetype = (tilecode[t] & 0x07) + 1; // extract slope type from tile code
// get offset from the tile
x %= TILE_W; y %= TILE_H;
if (slopetable[slopetype].table[x][y])
return slopetype;
}
return 0;
}
// returns true if any of the points in the given point list
// are on the solid portion of a slope tile.
bool IsSlopeAtPointList(Object *o, SIFPointList *points)
{
int x, y, i;
for(i=0;i<points->count;i++)
{
x = (o->x >> CSF) + points->point[i].x;
y = (o->y >> CSF) + points->point[i].y;
if (ReadSlopeTable(x, y)) return 1;
}
return 0;
}
/*
void c------------------------------() {}
*/
// returns nonzero (the slope type) if the object is standing on a slope.
int CheckStandOnSlope(Object *o)
{
int x, y, st;
y = (o->y >> CSF) + sprites[o->sprite].slopebox.y2 + 1;
x = (o->x >> CSF);
if ((st = ReadSlopeTable(x + sprites[o->sprite].slopebox.x1, y))) return st;
if ((st = ReadSlopeTable(x + sprites[o->sprite].slopebox.x2, y))) return st;
return 0;
}
// returns nonzero (the slope type) if the objects blocku should be set
// because of a ceiling slope.
int CheckBoppedHeadOnSlope(Object *o)
{
int x, y, st;
y = (o->y >> CSF) + sprites[o->sprite].slopebox.y1 - 1;
x = (o->x >> CSF);
// without this, you get stuck in the save area below Gum Door in Grasstown
//if (o == player) y += 4;
if ((st = ReadSlopeTable(x + sprites[o->sprite].slopebox.x1, y))) return st;
if ((st = ReadSlopeTable(x + sprites[o->sprite].slopebox.x2, y))) return st;
return 0;
}
// move an object laterally, and have it climb slopes as it approaches them.
// We also have to move the object down as it goes down the slope.
// Otherwise, it would "skip" down the slope ungracefully.
// returns 1 if the object was blocked by a wall.
bool movehandleslope(Object *o, int xinertia)
{
int xoff, opposing_x;
int newx, newy, oldy;
char blocked_wall;
if (!xinertia) return 0;
// for objects which don't follow slope, just treat the slope as a blockl/r
if (!(o->nxflags & NXFLAG_FOLLOW_SLOPE))
{
if (xinertia > 0)
{
if (o->blockr) return 1;
}
else
{
if (o->blockl) return 1;
}
o->x += xinertia;
return 0;
}
newx = o->x;
newy = o->y;
// determine which side of the bounding box to use based on which way
// we're traveling
if (xinertia > 0)
{ // moving right (right side of slopebox hits slopes first)
opposing_x = sprites[o->sprite].slopebox.x1;
xoff = sprites[o->sprite].slopebox.x2;
}
else
{ // move left (left side of slopebox hits slopes first)
opposing_x = sprites[o->sprite].slopebox.x2;
xoff = sprites[o->sprite].slopebox.x1;
}
// check the opposing side at y+1 to see if we were standing on a slope before the move.
uint8_t old_floor_slope, old_ceil_slope;
old_floor_slope = ReadSlopeTable((newx>>CSF) + opposing_x, \
(newy>>CSF) + sprites[o->sprite].slopebox.y2 + 1);
old_ceil_slope = ReadSlopeTable((newx>>CSF) + opposing_x, \
(newy>>CSF) + sprites[o->sprite].slopebox.y1 - 1);
// move the object
newx += xinertia;
// check the opposing side again and if now we're not standing any more,
// we moved down the slope, so add +1 to the object's Y coordinate.
if (old_floor_slope && !ReadSlopeTable((newx>>CSF) + opposing_x, \
(newy>>CSF) + sprites[o->sprite].slopebox.y2 + 1))
{
bool walking_down = false;
// only trigger if it's the correct slope type so that we would be walking down it if
// we were going in the direction we're going. prevents being shoved down 1px when
// exiting the top of a slope.
if (xinertia < 0)
{
if (old_floor_slope == SLOPE_FWD1 || \
old_floor_slope == SLOPE_FWD2)
{
walking_down = true;
}
}
else if (xinertia > 0)
{
if (old_floor_slope == SLOPE_BACK1 || \
old_floor_slope == SLOPE_BACK2)
{
walking_down = true;
}
}
if (walking_down)
{
newy += (1<<CSF);
}
}
// the same for ceiling slopes
if (old_ceil_slope && !ReadSlopeTable((newx>>CSF) + opposing_x, \
(newy>>CSF) + sprites[o->sprite].slopebox.y1 - 1))
{
bool moveme = false;
if (xinertia < 0)
{
if (old_ceil_slope == SLOPE_CEIL_BACK1 || \
old_ceil_slope == SLOPE_CEIL_BACK2)
{
moveme = true;
}
}
else if (xinertia > 0)
{
if (old_ceil_slope == SLOPE_CEIL_FWD1 || \
old_ceil_slope == SLOPE_CEIL_FWD2)
{
moveme = true;
}
}
if (moveme)
{ // moving down (actually up) the "descending" (closer to real ceil) portion
// of a ceiling slope tile. Reverse of floor slope thingy above.
newy -= (1<<CSF);
}
}
// check the coordinate and see if it's inside a slope tile.
// if so, move the object up 1 Y pixel.
uint8_t moved_into_ceil_slope = ReadSlopeTable((newx>>CSF) + xoff, \
(newy>>CSF) + sprites[o->sprite].slopebox.y1);
if (moved_into_ceil_slope)
{
newy += (1<<CSF);
}
uint8_t moved_into_floor_slope = ReadSlopeTable((newx>>CSF) + xoff, \
(newy>>CSF) + sprites[o->sprite].slopebox.y2);
if (moved_into_floor_slope)
{
newy -= (1<<CSF);
}
// can't move if blocked by a wall. but if we've moved up or down 1px, be sure and update
// the blockr/l state before declaring we can't move--otherwise we can get stuck at the
// top of a slope with the bottom blockr/l stuck at the top px of the adjacent solid tile.
oldy = o->y;
o->y = newy;
if (xinertia > 0)
{
if (oldy != newy)
o->UpdateBlockStates(RIGHTMASK);
blocked_wall = o->blockr;
}
else
{
if (oldy != newy)
o->UpdateBlockStates(LEFTMASK);
blocked_wall = o->blockl;
}
if (blocked_wall)
{ // we can't actually move...so reset Y position
o->y = oldy;
}
else
{ // can move...complete the move by setting the X position too
o->x = newx;
}
return blocked_wall;
}
/*
void c------------------------------() {}
*/
#ifdef DEBUG_SLOPE
// debug crap
void DrawSlopeTablesOnTiles()
{
static int lastmap = -1;
if (game.curmap != lastmap)
{
lastmap = game.curmap;
for(int i=0;i<256;i++)
{
if (tileattr[i] & TA_SLOPE)
{
DrawSlopeTableOnTile((tilecode[i]&7)+1, i);
}
}
}
}
void DrawSlopeTableOnTile(int table, int tile)
{
SDL_Rect dstrect;
int x, y;
extern SDL_Surface *tileset;
for(y=0;y<TILE_H;y++)
{
for(x=0;x<TILE_W;x++)
{
dstrect.x = (tile & 15) << 5;
dstrect.y = (tile >> 4) << 5;
dstrect.w = 2;
dstrect.h = 2;
dstrect.x += x*2;
dstrect.y += y*2;
if (slopetable[table].table[x][y])
{
if (table > 4) // floor slopes
SDL_FillRect(tileset, &dstrect, SDL_MapRGB(tileset->format, 0, 255, 0));
else
SDL_FillRect(tileset, &dstrect, SDL_MapRGB(tileset->format, 255, 0, 0));
}
}
}
}
void dumpslopetable(int t)
{
int x, y;
char buffer[80];
stat("\nDumping slope table %d:", t);
for(y=0;y<TILE_H;y++)
{
for(x=0;x<TILE_W;x++)
{
buffer[x] = slopetable[t].table[x][y] + '0';
}
buffer[x] = 0;
stat("%s", buffer);
}
}
#endif