Skip to content

Commit 2828941

Browse files
committed
final push commit #1: lots of stuff all at once
1 parent 1095107 commit 2828941

18 files changed

Lines changed: 401 additions & 72 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ appobj = src/app.obj src/options.obj src/treestor.obj src/ts_text.obj &
66
src/dynarr.obj src/util.obj src/scr_menu.obj src/scr_game.obj src/lut.obj &
77
src/xmath.obj src/xmath_s.obj src/image.obj src/tiles.obj src/level.obj &
88
src/rend.obj src/json.obj src/tiledfmt.obj src/mob.obj src/sprite.obj &
9-
src/psys.obj src/audio.obj src/aufile.obj src/auwav.obj
9+
src/psys.obj src/audio.obj src/aufile.obj src/auwav.obj src/ai.obj
1010
g3dobj = src/g3d/g3d.obj src/g3d/polyfill.obj
1111

1212
incpath = -Isrc -Isrc/dos -Ilibs -Ilibs/imago/src
@@ -18,7 +18,7 @@ appobj = src\app.obj src\options.obj src\treestor.obj src\ts_text.obj &
1818
src\dynarr.obj src\util.obj src\scr_menu.obj src\scr_game.obj src\lut.obj &
1919
src\xmath.obj src\xmath_s.obj src\image.obj src\tiles.obj src\level.obj &
2020
src\rend.obj src\json.obj src\tiledfmt.obj src\mob.obj src\sprite.obj &
21-
src\psys.obj src\audio.obj src\aufile.obj src\auwav.obj
21+
src\psys.obj src\audio.obj src\aufile.obj src\auwav.obj src\ai.obj
2222
g3dobj = src\g3d\g3d.obj src\g3d\polyfill.obj
2323

2424
incpath = -Isrc -Isrc\dos -Ilibs -Ilibs\imago\src

src/ai.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <stdio.h>
2+
#include "ai.h"
3+
#include "app.h"
4+
#include "util.h"
5+
#include "psys.h"
6+
7+
#define GUARD_DMG 16
8+
#define GUARD_FIRE_DELAY 800
9+
10+
static void spawn_bullet_psys(struct level_cell *cell, int32_t x, int32_t y, int32_t dx, int32_t dy);
11+
12+
void ai_guard(struct mob *mob, int32_t dt)
13+
{
14+
int32_t tx, ty, dx, dy;
15+
struct mob *hitmob;
16+
17+
mob->state_t += dt;
18+
if(mob->spr.frm < mob->spr.nfrm) {
19+
mob->spr.frm++;
20+
}
21+
22+
if(mob->state == MOB_DEAD) return;
23+
24+
if(player.state == MOB_DEAD) return;
25+
26+
if(mob->fire_cooldown) {
27+
mob->fire_cooldown -= dt;
28+
if(mob->fire_cooldown < 0) mob->fire_cooldown = 0;
29+
}
30+
31+
tx = player.x;
32+
ty = player.y;
33+
34+
dx = tx - mob->x;
35+
dy = ty - mob->y;
36+
37+
if((hitmob = raycast(mob->lvl, mob->x, mob->y, dx, dy, mob)) == &player) {
38+
mob_lookat(mob, tx, ty);
39+
40+
if(!mob->fire_cooldown) {
41+
mob_state(mob, MOB_FIRE);
42+
player.dmg += GUARD_DMG;
43+
mob->fire_cooldown = GUARD_FIRE_DELAY;
44+
45+
spawn_bullet_psys(player.cell, player.x, player.y, -dx, -dy);
46+
}
47+
}
48+
}
49+
50+
static void spawn_bullet_psys(struct level_cell *cell, int32_t x, int32_t y, int32_t dx, int32_t dy)
51+
{
52+
struct psys *ps;
53+
54+
ps = malloc_nf(sizeof *ps);
55+
*ps = psys_gunhit;
56+
ps->x = x;
57+
ps->y = y;
58+
59+
/* poor man's normalize */
60+
if(dx > 0) {
61+
dx = 0x100;
62+
} else if(dx < 0) {
63+
dx = -0x100;
64+
}
65+
if(dy > 0) {
66+
dy = 0x100;
67+
} else if(dy < 0) {
68+
dy = -0x100;
69+
}
70+
ps->dirx = dx;
71+
ps->diry = dy;
72+
73+
psys_add_emitter(&cell->psys, ps);
74+
}

src/ai.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef AI_H_
2+
#define AI_H_
3+
4+
#include "mob.h"
5+
6+
void ai_guard(struct mob *mob, int32_t dt);
7+
8+
#endif /* AI_H_ */

src/app.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "g3d/g3d.h"
88
#include "timer.h"
99
#include "options.h"
10+
#include "psys.h"
1011

1112
#ifndef NO_SOUND
1213
#include "audio.h"
@@ -23,6 +24,8 @@ struct app_screen *cur_scr;
2324
struct tileimg *tile_inval;
2425
int cur_bpl;
2526

27+
int showdbg;
28+
2629
/* available screens */
2730
#define MAX_SCREENS 8
2831
static struct app_screen *screens[MAX_SCREENS];
@@ -41,6 +44,8 @@ int app_init(void)
4144
g3d_init();
4245
g3d_framebuffer(FB_WIDTH, FB_HEIGHT, 0);
4346

47+
psys_init_prefabs();
48+
4449
/* initialize screens */
4550
screens[num_screens++] = &scr_menu;
4651
screens[num_screens++] = &scr_game;
@@ -109,6 +114,10 @@ void app_keyboard(int key, int press)
109114
case 27:
110115
app_quit();
111116
break;
117+
118+
case KEY_F3:
119+
showdbg ^= 1;
120+
break;
112121
}
113122
}
114123

src/app.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define APP_H_
33

44
#include "szint.h"
5+
#include "mob.h"
56

67
#define FB_WIDTH 320
78
#define FB_HEIGHT 240
@@ -65,6 +66,10 @@ extern struct tileimg *tile_inval; /* invalid tile */
6566
/* current bitplane, to avoid passing it to EVERY drawing function */
6667
extern int cur_bpl;
6768

69+
extern struct mob player;
70+
71+
extern int showdbg;
72+
6873

6974
int app_init(void);
7075
void app_shutdown(void);

src/crossgl/fakevga.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ void vga_rect_outline(uint8_t *vmem, int x, int y, int w, int h, uint8_t color)
138138
vga_hline(vmem, x + 1, y + h - 1, w - 2, color);
139139
}
140140

141+
void vga_fillrect(uint8_t *vmem, int x, int y, int w, int h, uint8_t color)
142+
{
143+
vmem += y * SCANLEN + x;
144+
145+
while(h-- > 0) {
146+
memset(vmem, color, w);
147+
vmem += SCANLEN;
148+
}
149+
}
150+
141151
static uint32_t *convbuf;
142152
static int convbuf_size;
143153

src/g3d/g3d.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#include "util.h"
77
#include "xmath.h"
88

9-
struct rect {
10-
int x0, y0, x1, y1;
11-
};
12-
139
static int32_t mvmat[16];
1410
static int32_t pmat[16];
1511
static int32_t mvpmat[16];

src/level.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <stdio.h>
2+
#include <limits.h>
23
#include <assert.h>
34
#include "level.h"
45
#include "tiles.h"
56
#include "util.h"
67
#include "dynarr.h"
78
#include "mob.h"
9+
#include "app.h"
810
#include "psys.h"
911

1012
struct tileset tileset;
@@ -221,3 +223,82 @@ int cell_remove_psys(struct level_cell *cell, struct psys *ps)
221223
}
222224
return 0;
223225
}
226+
227+
228+
struct mob *raycast(struct level *lvl, int32_t x, int32_t y, int32_t dx, int32_t dy, struct mob *ignmob)
229+
{
230+
int i, cx, cy, exit_dir;
231+
int32_t hslope, vslope, nx, ny, rdx, rdy, hitx, hity;
232+
struct level_cell *cell;
233+
struct mob *cellmob, *hitmob;
234+
int32_t hit_dist, t;
235+
236+
grid_to_cell(x, y, &cx, &cy);
237+
if(!(cell = get_level_cell(lvl, cx, cy))) {
238+
return 0;
239+
}
240+
241+
hslope = dx ? (dy << 8) / dx : 256;
242+
vslope = dy ? (dx << 8) / dy : 256;
243+
244+
for(i=0; i<MAX_BEAM_SEG; i++) {
245+
exit_dir = ray_step(lvl, x, y, dx, dy, hslope, vslope, &nx, &ny);
246+
247+
rdx = nx - x;
248+
rdy = ny - y;
249+
250+
hitmob = 0;
251+
hit_dist = INT_MAX;
252+
253+
if(ignmob != &player) {
254+
if((t = mob_rayhit(&player, x, y, rdx, rdy, &hitx, &hity)) >= 0) {
255+
hitmob = &player;
256+
hit_dist = t;
257+
}
258+
}
259+
260+
cellmob = cell->mobs;
261+
while(cellmob) {
262+
if(cellmob != ignmob) {
263+
if((t = mob_rayhit(cellmob, x, y, rdx, rdy, &hitx, &hity)) >= 0 && t < hit_dist) {
264+
hitmob = cellmob;
265+
hit_dist = t;
266+
}
267+
}
268+
cellmob = cellmob->next;
269+
}
270+
271+
if(hitmob) {
272+
return hitmob;
273+
}
274+
275+
if(!(cell->flags & CELL_EXIT(exit_dir))) {
276+
break;
277+
}
278+
279+
switch(exit_dir) {
280+
case DIR_E:
281+
if(cell->cx >= lvl->size - 1) goto break_loop;
282+
cell++;
283+
break;
284+
case DIR_W:
285+
if(cell->cx <= 0) goto break_loop;
286+
cell--;
287+
break;
288+
case DIR_S:
289+
if(cell->cy >= lvl->size - 1) goto break_loop;
290+
cell += lvl->size;
291+
break;
292+
case DIR_N:
293+
if(cell->cy <= 0) goto break_loop;
294+
cell -= lvl->size;
295+
break;
296+
}
297+
298+
x = nx;
299+
y = ny;
300+
}
301+
break_loop:
302+
return 0;
303+
}
304+

src/level.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#define CELL_XSZ (TILE_XSZ << 1)
1414
#define CELL_YSZ (TILE_YSZ << 1)
1515

16-
struct rect {
17-
int x, y, w, h;
18-
};
19-
2016
enum { DIR_N, DIR_W, DIR_S, DIR_E };
2117
enum { DIR8_N, DIR8_NW, DIR8_W, DIR8_SW, DIR8_S, DIR8_SE, DIR8_E, DIR8_NE };
2218

@@ -91,6 +87,8 @@ int cell_remove_beamseg(struct level_cell *cell, struct beamseg *bs);
9187
void cell_add_psys(struct level_cell *cell, struct psys *ps);
9288
int cell_remove_psys(struct level_cell *cell, struct psys *ps);
9389

90+
struct mob *raycast(struct level *lvl, int32_t x, int32_t y, int32_t dx, int32_t dy, struct mob *ignmob);
91+
9492
/* implicit in these conversions is the tile size: 64x32 */
9593
static INLINE void vscr_to_grid(int sx, int sy, int32_t *gridx, int32_t *gridy)
9694
{
@@ -127,4 +125,37 @@ static INLINE void grid_to_cell(int32_t gx, int32_t gy, int *cx, int *cy)
127125
*cy = (gy + 0x80) >> 8;
128126
}
129127

128+
static INLINE int ray_step(struct level *lvl, int32_t x, int32_t y, int32_t dx,
129+
int32_t dy, int32_t hslope, int32_t vslope, int32_t *nx, int32_t *ny)
130+
{
131+
int32_t x1, y1, offs;
132+
133+
/* convert to origin at upper-left, rather than center of cells, to make
134+
* stepping simpler
135+
*/
136+
x += 128;
137+
y += 128;
138+
139+
/* find next boundaries when stepping horizontally or vertically */
140+
x1 = (dx > 0 ? x + 256 : x - 1) & ~0xff;
141+
/*hslope = (dy << 8) / dx;*/
142+
y1 = y + ((hslope * (x1 - x)) >> 8);
143+
offs = y1 - (y & ~0xff);
144+
145+
if(offs < 256 && offs >= 0) {
146+
*nx = x1 - 128;
147+
*ny = y1 - 128;
148+
return dx > 0 ? DIR_E : DIR_W;
149+
}
150+
151+
y1 = (dy > 0 ? y + 256 : y - 1) & ~0xff;
152+
/*vslope = (dx << 8) / dy;*/
153+
x1 = x + ((vslope * (y1 - y)) >> 8);
154+
155+
*nx = x1 - 128;
156+
*ny = y1 - 128;
157+
return dy > 0 ? DIR_S : DIR_N;
158+
}
159+
160+
130161
#endif /* LEVEL_H_ */

src/mob.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,38 +101,6 @@ void mob_state(struct mob *mob, int st)
101101
mob->spr.frm = 0;
102102
}
103103

104-
static INLINE int ray_step(struct level *lvl, int32_t x, int32_t y, int32_t dx,
105-
int32_t dy, int32_t hslope, int32_t vslope, int32_t *nx, int32_t *ny)
106-
{
107-
int32_t x1, y1, offs;
108-
109-
/* convert to origin at upper-left, rather than center of cells, to make
110-
* stepping simpler
111-
*/
112-
x += 128;
113-
y += 128;
114-
115-
/* find next boundaries when stepping horizontally or vertically */
116-
x1 = (dx > 0 ? x + 256 : x - 1) & ~0xff;
117-
/*hslope = (dy << 8) / dx;*/
118-
y1 = y + ((hslope * (x1 - x)) >> 8);
119-
offs = y1 - (y & ~0xff);
120-
121-
if(offs < 256 && offs >= 0) {
122-
*nx = x1 - 128;
123-
*ny = y1 - 128;
124-
return dx > 0 ? DIR_E : DIR_W;
125-
}
126-
127-
y1 = (dy > 0 ? y + 256 : y - 1) & ~0xff;
128-
/*vslope = (dx << 8) / dy;*/
129-
x1 = x + ((vslope * (y1 - y)) >> 8);
130-
131-
*nx = x1 - 128;
132-
*ny = y1 - 128;
133-
return dy > 0 ? DIR_S : DIR_N;
134-
}
135-
136104
void mob_beam(struct mob *mob, int32_t tx, int32_t ty, int dmg)
137105
{
138106
int i, exit_dir;

0 commit comments

Comments
 (0)