foo
[gbajam22] / src / level.c
1 #include "util.h"
2 #include "debug.h"
3 #include "level.h"
4 #include "player.h"
5 #include "xgl.h"
6
7 struct level *init_level(const char *descstr)
8 {
9         const char *str, *line;
10         int i, j, ncols = 0, nrows = 0;
11         struct level *lvl;
12         struct cell *cell;
13
14         str = line = descstr;
15
16         while(*str) {
17                 if(*str == '\n') {
18                         if(ncols > 0 && str > line && str - line != ncols) {
19                                 panic(get_pc(), "init_level: inconsistent ncols (%d != %d)\n", str - line, ncols);
20                         }
21                         ncols = str - line;
22                         nrows++;
23                         while(*++str == '\n');
24                         line = str;
25                 } else {
26                         str++;
27                 }
28         }
29
30         if(!ispow2(ncols)) {
31                 panic(get_pc(), "init_level: width is not pow2 (%d)\n", ncols);
32         }
33
34         lvl = malloc_nf(sizeof *lvl);
35         lvl->width = ncols;
36         lvl->xmask = ncols - 1;
37         lvl->height = nrows;
38         lvl->orgx = lvl->width >> 1;
39         lvl->orgy = lvl->height >> 1;
40         lvl->cells = calloc_nf(ncols * nrows, sizeof *lvl->cells);
41         lvl->mobs = 0;
42         lvl->items = 0;
43
44         str = descstr;
45         cell = lvl->cells;
46
47         for(i=0; i<nrows; i++) {
48                 for(j=0; j<ncols; j++) {
49                         cell->x = j;
50                         cell->y = i;
51                         if(*str == '#') {
52                                 cell->type = CELL_SOLID;
53                         } else {
54                                 cell->type = CELL_WALK;
55                         }
56                         cell++;
57                         while(*++str == '\n') str++;
58                 }
59         }
60
61         return lvl;
62 }
63
64 void free_level(struct level *lvl)
65 {
66         void *tmp;
67
68         free(lvl->cells);
69
70         while(lvl->mobs) {
71                 tmp = lvl->mobs;
72                 lvl->mobs = lvl->mobs->next;
73                 free(tmp);
74         }
75         while(lvl->items) {
76                 tmp = lvl->items;
77                 lvl->items = lvl->items->next;
78                 free(tmp);
79         }
80 }
81
82 struct {int dx, dy;} visoffs[8][32] = {
83         /* dir 0 */
84         {{-4,-4}, {4,-4}, {-3,-4}, {3,-4}, {-2,-4}, {2,-4}, {-3,-3}, {3,-3}, {-1,-4},
85          {1,-4}, {0,-4}, {-2,-3}, {2,-3}, {-1,-3}, {1,-3}, {0,-3}, {-2,-2}, {2,-2},
86          {-1,-2}, {1,-2}, {0,-2}, {-1,-1}, {1,-1}, {0,-1}, {0,0}},
87         /* dir 1 */
88         {{4,-4}, {3,-4}, {4,-3}, {2,-4}, {4,-2}, {3,-3}, {1,-4}, {4,-1}, {0,-4},
89          {4,0}, {2,-3}, {3,-2}, {1,-3}, {3,-1}, {0,-3}, {3,0}, {2,-2}, {1,-2},
90          {2,-1}, {0,-2}, {2,0}, {1,-1}, {0,-1}, {1,0}, {0,0}},
91         /* dir 2 */
92         {{4,-4}, {4,4}, {4,-3}, {4,3}, {4,-2}, {4,2}, {3,-3}, {3,3}, {4,-1}, {4,1},
93          {4,0}, {3,-2}, {3,2}, {3,-1}, {3,1}, {3,0}, {2,-2}, {2,2}, {2,-1}, {2,1},
94          {2,0}, {1,-1}, {1,1}, {1,0}, {0,0}},
95         /* dir 3 */
96         {{4,4}, {4,3}, {3,4}, {4,2}, {2,4}, {3,3}, {4,1}, {1,4}, {4,0}, {0,4},
97          {3,2}, {2,3}, {3,1}, {1,3}, {3,0}, {0,3}, {2,2}, {2,1}, {1,2}, {2,0},
98          {0,2}, {1,1}, {1,0}, {0,1}, {0,0}},
99         /* dir 4 */
100         {{-4,4}, {4,4}, {-3,4}, {3,4}, {-2,4}, {2,4}, {-3,3}, {3,3}, {-1,4}, {1,4},
101          {0,4}, {-2,3}, {2,3}, {-1,3}, {1,3}, {0,3}, {-2,2}, {2,2}, {-1,2}, {1,2},
102          {0,2}, {-1,1}, {1,1}, {0,1}, {0,0}},
103         /* dir 5 */
104         {{-4,4}, {-4,3}, {-3,4}, {-4,2}, {-2,4}, {-3,3}, {-4,1}, {-1,4}, {-4,0},
105          {0,4}, {-3,2}, {-2,3}, {-3,1}, {-1,3}, {-3,0}, {0,3}, {-2,2}, {-2,1},
106          {-1,2}, {-2,0}, {0,2}, {-1,1}, {-1,0}, {0,1}, {0,0}},
107         /* dir 6 */
108         {{-4,-4}, {-4,4}, {-4,-3}, {-4,3}, {-4,-2}, {-4,2}, {-3,-3}, {-3,3}, {-4,-1},
109          {-4,1}, {-4,0}, {-3,-2}, {-3,2}, {-3,-1}, {-3,1}, {-3,0}, {-2,-2}, {-2,2},
110          {-2,-1}, {-2,1}, {-2,0}, {-1,-1}, {-1,1}, {-1,0}, {0,0}},
111         /* dir 7 */
112         {{-4,-4}, {-3,-4}, {-4,-3}, {-2,-4}, {-4,-2}, {-3,-3}, {-1,-4}, {-4,-1},
113          {0,-4}, {-4,0}, {-2,-3}, {-3,-2}, {-1,-3}, {-3,-1}, {0,-3}, {-3,0}, {-2,-2},
114          {-1,-2}, {-2,-1}, {0,-2}, {-2,0}, {-1,-1}, {0,-1}, {-1,0}, {0,0}}
115 };
116
117 void upd_vis(struct level *lvl, struct player *p)
118 {
119         int dir, idx;
120         int x, y;
121         struct cell *cptr;
122         int32_t theta;
123
124         pos_to_cell(lvl, p->x, p->y, &p->cx, &p->cy);
125
126         lvl->numvis = 0;
127         idx = -1;
128         theta = p->theta + X_2PI / 16;
129         if(theta >= X_2PI) theta -= X_2PI;
130         dir = 7 - (theta << 3) / X_2PI; /* p->theta is always [0, 2pi) */
131         dbg_drawstr(0, 0, "dir: %d", dir);
132         if(dir < 0 || dir >= 8) {
133                 panic(get_pc(), "dir: %d\ntheta: %d.%d (%d)\n", dir, p->theta >> 16,
134                                 p->theta & 0xffff, p->theta);
135         }
136         do {
137                 idx++;
138                 x = p->cx + visoffs[dir][idx].dx;
139                 y = p->cy + visoffs[dir][idx].dy;
140                 cptr = lvl->cells + y * lvl->width + x;
141                 lvl->vis[lvl->numvis++] = cptr;
142         } while(visoffs[dir][idx].dx | visoffs[dir][idx].dy);
143 }
144
145 void cell_to_pos(struct level *lvl, int cx, int cy, int32_t *px, int32_t *py)
146 {
147         *px = (cx - lvl->orgx) * CELL_SIZE;
148         *py = (cy - lvl->orgy) * CELL_SIZE;
149 }
150
151 void pos_to_cell(struct level *lvl, int32_t px, int32_t py, int *cx, int *cy)
152 {
153         *cx = (px + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgx;
154         *cy = (py + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgy;
155 }