7 struct level *init_level(const char *descstr)
9 const char *str, *line;
10 int i, j, x, ncols = 0, nrows = 0;
18 if(ncols > 0 && str > line && str - line != ncols) {
19 panic(get_pc(), "init_level: inconsistent ncols (%d != %d)\n", str - line, ncols);
23 while(*++str == '\n');
31 panic(get_pc(), "init_level: width is not pow2 (%d)\n", ncols);
34 lvl = malloc_nf(sizeof *lvl);
36 lvl->xmask = ncols - 1;
38 lvl->orgx = lvl->width >> 1;
39 lvl->orgy = lvl->height >> 1;
40 lvl->cells = calloc_nf(ncols * nrows, sizeof *lvl->cells);
54 for(i=0; i<nrows; i++) {
55 for(j=0; j<ncols; j++) {
59 cell->type = CELL_SOLID;
61 cell->type = CELL_WALK;
70 while(*++str == '\n') str++;
77 void free_level(struct level *lvl)
85 lvl->mobs = lvl->mobs->next;
90 lvl->items = lvl->items->next;
95 struct cell *level_cell(struct level *lvl, int cx, int cy)
97 return lvl->cells + (cy << lvl->xshift) + cx;
100 /* generated with tools/vistab */
101 struct {int dx, dy;} visoffs[8][32] = {
103 {{-4,-4}, {4,-4}, {-3,-4}, {3,-4}, {-2,-4}, {2,-4}, {-3,-3}, {3,-3}, {-1,-4},
104 {1,-4}, {0,-4}, {-2,-3}, {2,-3}, {-1,-3}, {1,-3}, {0,-3}, {-2,-2}, {2,-2},
105 {-1,-2}, {1,-2}, {0,-2}, {-1,-1}, {1,-1}, {0,-1}, {0,0}},
107 {{4,-4}, {3,-4}, {4,-3}, {2,-4}, {4,-2}, {3,-3}, {1,-4}, {4,-1}, {0,-4},
108 {4,0}, {2,-3}, {3,-2}, {1,-3}, {3,-1}, {0,-3}, {3,0}, {2,-2}, {1,-2},
109 {2,-1}, {0,-2}, {2,0}, {1,-1}, {0,-1}, {1,0}, {0,0}},
111 {{4,-4}, {4,4}, {4,-3}, {4,3}, {4,-2}, {4,2}, {3,-3}, {3,3}, {4,-1}, {4,1},
112 {4,0}, {3,-2}, {3,2}, {3,-1}, {3,1}, {3,0}, {2,-2}, {2,2}, {2,-1}, {2,1},
113 {2,0}, {1,-1}, {1,1}, {1,0}, {0,0}},
115 {{4,4}, {4,3}, {3,4}, {4,2}, {2,4}, {3,3}, {4,1}, {1,4}, {4,0}, {0,4},
116 {3,2}, {2,3}, {3,1}, {1,3}, {3,0}, {0,3}, {2,2}, {2,1}, {1,2}, {2,0},
117 {0,2}, {1,1}, {1,0}, {0,1}, {0,0}},
119 {{-4,4}, {4,4}, {-3,4}, {3,4}, {-2,4}, {2,4}, {-3,3}, {3,3}, {-1,4}, {1,4},
120 {0,4}, {-2,3}, {2,3}, {-1,3}, {1,3}, {0,3}, {-2,2}, {2,2}, {-1,2}, {1,2},
121 {0,2}, {-1,1}, {1,1}, {0,1}, {0,0}},
123 {{-4,4}, {-4,3}, {-3,4}, {-4,2}, {-2,4}, {-3,3}, {-4,1}, {-1,4}, {-4,0},
124 {0,4}, {-3,2}, {-2,3}, {-3,1}, {-1,3}, {-3,0}, {0,3}, {-2,2}, {-2,1},
125 {-1,2}, {-2,0}, {0,2}, {-1,1}, {-1,0}, {0,1}, {0,0}},
127 {{-4,-4}, {-4,4}, {-4,-3}, {-4,3}, {-4,-2}, {-4,2}, {-3,-3}, {-3,3}, {-4,-1},
128 {-4,1}, {-4,0}, {-3,-2}, {-3,2}, {-3,-1}, {-3,1}, {-3,0}, {-2,-2}, {-2,2},
129 {-2,-1}, {-2,1}, {-2,0}, {-1,-1}, {-1,1}, {-1,0}, {0,0}},
131 {{-4,-4}, {-3,-4}, {-4,-3}, {-2,-4}, {-4,-2}, {-3,-3}, {-1,-4}, {-4,-1},
132 {0,-4}, {-4,0}, {-2,-3}, {-3,-2}, {-1,-3}, {-3,-1}, {0,-3}, {-3,0}, {-2,-2},
133 {-1,-2}, {-2,-1}, {0,-2}, {-2,0}, {-1,-1}, {0,-1}, {-1,0}, {0,0}}
136 void upd_vis(struct level *lvl, struct player *p)
143 pos_to_cell(lvl, p->x, p->y, &p->cx, &p->cy);
147 theta = X_2PI - p->theta + X_2PI / 16;
148 if(theta >= X_2PI) theta -= X_2PI;
149 dir = (theta << 3) / X_2PI; /* p->theta is always [0, 2pi) */
150 if(dir < 0 || dir >= 8) {
151 panic(get_pc(), "dir: %d\ntheta: %d.%d (%d)\n", dir, p->theta >> 16,
152 p->theta & 0xffff, p->theta);
156 x = p->cx + visoffs[dir][idx].dx;
157 y = p->cy + visoffs[dir][idx].dy;
158 cptr = lvl->cells + y * lvl->width + x;
159 if(cptr->type != CELL_SOLID) {
160 lvl->vis[lvl->numvis++] = cptr;
162 } while(visoffs[dir][idx].dx | visoffs[dir][idx].dy);
165 void cell_to_pos(struct level *lvl, int cx, int cy, int32_t *px, int32_t *py)
167 *px = (cx - lvl->orgx) * CELL_SIZE;
168 *py = (cy - lvl->orgy) * CELL_SIZE;
171 void pos_to_cell(struct level *lvl, int32_t px, int32_t py, int *cx, int *cy)
173 *cx = (px + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgx;
174 *cy = (py + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgy;