X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Flevel.c;h=d74d52f3cdcedccb7aead388328ccb57b2790b64;hb=7aad8db2edab014c0f750de76bc24bdab63aef2a;hp=0ae8d3e9f574777da201dbf0c549ec2ff4ece057;hpb=5d7112b5fad9e69a556898a705eeef2307bb83d9;p=gbajam22 diff --git a/src/level.c b/src/level.c index 0ae8d3e..d74d52f 100644 --- a/src/level.c +++ b/src/level.c @@ -1,11 +1,13 @@ #include "util.h" #include "debug.h" #include "level.h" +#include "player.h" +#include "xgl.h" struct level *init_level(const char *descstr) { const char *str, *line; - int i, j, ncols = 0, nrows = 0; + int i, j, x, ncols = 0, nrows = 0; struct level *lvl; struct cell *cell; @@ -33,10 +35,19 @@ struct level *init_level(const char *descstr) lvl->width = ncols; lvl->xmask = ncols - 1; lvl->height = nrows; + lvl->orgx = lvl->width >> 1; + lvl->orgy = lvl->height >> 1; lvl->cells = calloc_nf(ncols * nrows, sizeof *lvl->cells); lvl->mobs = 0; lvl->items = 0; + lvl->xshift = 0; + x = ncols - 1; + while(x) { + x >>= 1; + lvl->xshift++; + } + str = descstr; cell = lvl->cells; @@ -48,6 +59,12 @@ struct level *init_level(const char *descstr) cell->type = CELL_SOLID; } else { cell->type = CELL_WALK; + switch(*str) { + case 's': + lvl->orgx = j; + lvl->orgy = i; + break; + } } cell++; while(*++str == '\n') str++; @@ -75,28 +92,84 @@ void free_level(struct level *lvl) } } -void upd_vis(struct level *lvl, int32_t px, int32_t py, int32_t angle) +struct cell *level_cell(struct level *lvl, int cx, int cy) { - int cx, cy; + return lvl->cells + (cy << lvl->xshift) + cx; +} - lvl->numvis = 0; +/* generated with tools/vistab */ +struct {int dx, dy;} visoffs[8][32] = { + /* dir 0 */ + {{-4,-4}, {4,-4}, {-3,-4}, {3,-4}, {-2,-4}, {2,-4}, {-3,-3}, {3,-3}, {-1,-4}, + {1,-4}, {0,-4}, {-2,-3}, {2,-3}, {-1,-3}, {1,-3}, {0,-3}, {-2,-2}, {2,-2}, + {-1,-2}, {1,-2}, {0,-2}, {-1,-1}, {1,-1}, {0,-1}, {0,0}}, + /* dir 1 */ + {{4,-4}, {3,-4}, {4,-3}, {2,-4}, {4,-2}, {3,-3}, {1,-4}, {4,-1}, {0,-4}, + {4,0}, {2,-3}, {3,-2}, {1,-3}, {3,-1}, {0,-3}, {3,0}, {2,-2}, {1,-2}, + {2,-1}, {0,-2}, {2,0}, {1,-1}, {0,-1}, {1,0}, {0,0}}, + /* dir 2 */ + {{4,-4}, {4,4}, {4,-3}, {4,3}, {4,-2}, {4,2}, {3,-3}, {3,3}, {4,-1}, {4,1}, + {4,0}, {3,-2}, {3,2}, {3,-1}, {3,1}, {3,0}, {2,-2}, {2,2}, {2,-1}, {2,1}, + {2,0}, {1,-1}, {1,1}, {1,0}, {0,0}}, + /* dir 3 */ + {{4,4}, {4,3}, {3,4}, {4,2}, {2,4}, {3,3}, {4,1}, {1,4}, {4,0}, {0,4}, + {3,2}, {2,3}, {3,1}, {1,3}, {3,0}, {0,3}, {2,2}, {2,1}, {1,2}, {2,0}, + {0,2}, {1,1}, {1,0}, {0,1}, {0,0}}, + /* dir 4 */ + {{-4,4}, {4,4}, {-3,4}, {3,4}, {-2,4}, {2,4}, {-3,3}, {3,3}, {-1,4}, {1,4}, + {0,4}, {-2,3}, {2,3}, {-1,3}, {1,3}, {0,3}, {-2,2}, {2,2}, {-1,2}, {1,2}, + {0,2}, {-1,1}, {1,1}, {0,1}, {0,0}}, + /* dir 5 */ + {{-4,4}, {-4,3}, {-3,4}, {-4,2}, {-2,4}, {-3,3}, {-4,1}, {-1,4}, {-4,0}, + {0,4}, {-3,2}, {-2,3}, {-3,1}, {-1,3}, {-3,0}, {0,3}, {-2,2}, {-2,1}, + {-1,2}, {-2,0}, {0,2}, {-1,1}, {-1,0}, {0,1}, {0,0}}, + /* dir 6 */ + {{-4,-4}, {-4,4}, {-4,-3}, {-4,3}, {-4,-2}, {-4,2}, {-3,-3}, {-3,3}, {-4,-1}, + {-4,1}, {-4,0}, {-3,-2}, {-3,2}, {-3,-1}, {-3,1}, {-3,0}, {-2,-2}, {-2,2}, + {-2,-1}, {-2,1}, {-2,0}, {-1,-1}, {-1,1}, {-1,0}, {0,0}}, + /* dir 7 */ + {{-4,-4}, {-3,-4}, {-4,-3}, {-2,-4}, {-4,-2}, {-3,-3}, {-1,-4}, {-4,-1}, + {0,-4}, {-4,0}, {-2,-3}, {-3,-2}, {-1,-3}, {-3,-1}, {0,-3}, {-3,0}, {-2,-2}, + {-1,-2}, {-2,-1}, {0,-2}, {-2,0}, {-1,-1}, {0,-1}, {-1,0}, {0,0}} +}; + +void upd_vis(struct level *lvl, struct player *p) +{ + int dir, idx; + int x, y; + struct cell *cptr; + int32_t theta; - pos_to_cell(px, py, &cx, &cy); + pos_to_cell(lvl, p->x, p->y, &p->cx, &p->cy); - /* TODO: cont. */ + lvl->numvis = 0; + idx = -1; + theta = X_2PI - p->theta + X_2PI / 16; + if(theta >= X_2PI) theta -= X_2PI; + dir = (theta << 3) / X_2PI; /* p->theta is always [0, 2pi) */ + if(dir < 0 || dir >= 8) { + panic(get_pc(), "dir: %d\ntheta: %d.%d (%d)\n", dir, p->theta >> 16, + p->theta & 0xffff, p->theta); + } + do { + idx++; + x = p->cx + visoffs[dir][idx].dx; + y = p->cy + visoffs[dir][idx].dy; + cptr = lvl->cells + y * lvl->width + x; + if(cptr->type != CELL_SOLID) { + lvl->vis[lvl->numvis++] = cptr; + } + } while(visoffs[dir][idx].dx | visoffs[dir][idx].dy); } -void cell_to_pos(int cx, int cy, int32_t *px, int32_t *py) +void cell_to_pos(struct level *lvl, int cx, int cy, int32_t *px, int32_t *py) { - *px = cx * CELL_SIZE - (CELL_SIZE >> 1); - *py = cy * CELL_SIZE - (CELL_SIZE >> 1); + *px = (cx - lvl->orgx) * CELL_SIZE; + *py = (cy - lvl->orgy) * CELL_SIZE; } -void pos_to_cell(int32_t px, int32_t py, int *cx, int *cy) +void pos_to_cell(struct level *lvl, int32_t px, int32_t py, int *cx, int *cy) { - _Static_assert((CELL_SIZE & ~0xff) == CELL_SIZE, - "CELL_SIZE >> 8 in pos_to_cell will lose significant bits"); - - *cx = ((px + (CELL_SIZE >> 1)) << 8) / (CELL_SIZE >> 8); - *cy = ((py + (CELL_SIZE >> 1)) << 8) / (CELL_SIZE >> 8); + *cx = (px + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgx; + *cy = (py + (CELL_SIZE >> 1)) / CELL_SIZE + lvl->orgy; }