7 struct level *init_level(const char *descstr)
9 const char *str, *line;
10 int i, j, 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);
47 for(i=0; i<nrows; i++) {
48 for(j=0; j<ncols; j++) {
52 cell->type = CELL_SOLID;
54 cell->type = CELL_WALK;
57 while(*++str == '\n') str++;
64 void free_level(struct level *lvl)
72 lvl->mobs = lvl->mobs->next;
77 lvl->items = lvl->items->next;
82 struct {int dx, dy;} visoffs[8][32] = {
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}},
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}},
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}},
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}},
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}},
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}},
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}},
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}}
117 void upd_vis(struct level *lvl, struct player *p)
124 pos_to_cell(lvl, p->x, p->y, &p->cx, &p->cy);
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);
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);
145 void cell_to_pos(struct level *lvl, int cx, int cy, int32_t *px, int32_t *py)
147 *px = (cx - lvl->orgx) * CELL_SIZE;
148 *py = (cy - lvl->orgy) * CELL_SIZE;
151 void pos_to_cell(struct level *lvl, int32_t px, int32_t py, int *cx, int *cy)
153 *cx = (px + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgx;
154 *cy = (py + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgy;