6 struct level *init_level(const char *descstr)
8 const char *str, *line;
9 int i, j, ncols = 0, nrows = 0;
17 if(ncols > 0 && str > line && str - line != ncols) {
18 panic(get_pc(), "init_level: inconsistent ncols (%d != %d)\n", str - line, ncols);
22 while(*++str == '\n');
30 panic(get_pc(), "init_level: width is not pow2 (%d)\n", ncols);
33 lvl = malloc_nf(sizeof *lvl);
35 lvl->xmask = ncols - 1;
37 lvl->orgx = lvl->width >> 1;
38 lvl->orgy = lvl->height >> 1;
39 lvl->cells = calloc_nf(ncols * nrows, sizeof *lvl->cells);
46 for(i=0; i<nrows; i++) {
47 for(j=0; j<ncols; j++) {
51 cell->type = CELL_SOLID;
53 cell->type = CELL_WALK;
56 while(*++str == '\n') str++;
63 void free_level(struct level *lvl)
71 lvl->mobs = lvl->mobs->next;
76 lvl->items = lvl->items->next;
81 struct {int dx, dy;} visoffs[8][32] = {
83 {{-2,-4}, {2,-4}, {-1,-4}, {1,-4}, {0,-4}, {-1,-3}, {1,-3}, {0,-3}, {-1,-2},
84 {1,-2}, {0,-2}, {0,-1}, {0,0}},
86 {{4,-4}, {3,-4}, {4,-3}, {2,-4}, {4,-2}, {3,-3}, {2,-3}, {3,-2}, {1,-3},
87 {3,-1}, {2,-2}, {1,-2}, {2,-1}, {1,-1}, {0,0}},
89 {{4,-2}, {4,2}, {4,-1}, {4,1}, {4,0}, {3,-1}, {3,1}, {3,0}, {2,-1}, {2,1},
92 {{4,4}, {4,3}, {3,4}, {4,2}, {2,4}, {3,3}, {3,2}, {2,3}, {3,1}, {1,3},
93 {2,2}, {2,1}, {1,2}, {1,1}, {0,0}},
95 {{-2,4}, {2,4}, {-1,4}, {1,4}, {0,4}, {-1,3}, {1,3}, {0,3}, {-1,2}, {1,2},
98 {{-4,4}, {-4,3}, {-3,4}, {-4,2}, {-2,4}, {-3,3}, {-3,2}, {-2,3}, {-3,1},
99 {-1,3}, {-2,2}, {-2,1}, {-1,2}, {-1,1}, {0,0}},
101 {{-4,-2}, {-4,2}, {-4,-1}, {-4,1}, {-4,0}, {-3,-1}, {-3,1}, {-3,0}, {-2,-1},
102 {-2,1}, {-2,0}, {-1,0}, {0,0}},
104 {{-4,-4}, {-3,-4}, {-4,-3}, {-2,-4}, {-4,-2}, {-3,-3}, {-2,-3}, {-3,-2},
105 {-1,-3}, {-3,-1}, {-2,-2}, {-1,-2}, {-2,-1}, {-1,-1}, {0,0}}
108 void upd_vis(struct level *lvl, struct player *p)
114 pos_to_cell(lvl, p->x, p->y, &p->cx, &p->cy);
118 dir = 0; /* TODO use p->theta */
121 x = p->cx + visoffs[dir][idx].dx;
122 y = p->cy + visoffs[dir][idx].dy;
123 cptr = lvl->cells + y * lvl->width + x;
124 lvl->vis[lvl->numvis++] = cptr;
125 } while(visoffs[dir][idx].dx | visoffs[dir][idx].dy);
128 void cell_to_pos(struct level *lvl, int cx, int cy, int32_t *px, int32_t *py)
130 *px = (cx - lvl->orgx) * CELL_SIZE;
131 *py = (cy - lvl->orgy) * CELL_SIZE;
134 void pos_to_cell(struct level *lvl, int32_t px, int32_t py, int *cx, int *cy)
136 *cx = (px + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgx;
137 *cy = (py + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgy;