X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Flevel.c;h=6867e64e357f5d8a711e42205dfe00d6cd1e704b;hb=6a120e64b1c3092d8929292882641d376ad0993b;hp=9e5fe44c633fcffa989af1eb6fdeb6cf64203b7f;hpb=6066118fc6a58b379f52b9aaaf45200b136812b9;p=vrlugburz diff --git a/src/level.c b/src/level.c index 9e5fe44..6867e64 100644 --- a/src/level.c +++ b/src/level.c @@ -4,18 +4,24 @@ #include #include #include "level.h" +#include "tileset.h" #include "fs.h" -static int load_tileset(struct level *lvl, struct ts_node *tsn); +static int detect_cell_tile(struct level *lvl, int x, int y, int *rot); + int init_level(struct level *lvl, int xsz, int ysz) { + memset(lvl, 0, sizeof *lvl); + if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) { free(lvl); return -1; } lvl->width = xsz; lvl->height = ysz; + lvl->cell_size = DEF_CELL_SIZE; + lvl->px = lvl->py = -1; return 0; } @@ -30,13 +36,10 @@ void destroy_level(struct level *lvl) int load_level(struct level *lvl, const char *fname) { struct ts_node *ts, *node, *iter; - int sz, cx, cy; + int i, j, sz, cx, cy, tiletype; struct cell *cell; - - lvl->fname = strdup(fname); - if((lvl->dirname = malloc(strlen(fname) + 1))) { - path_dir(lvl->fname, lvl->dirname); - } + float *vecptr; + const char *str; if(!(ts = ts_load(fname))) { fprintf(stderr, "failed to load level: %s\n", fname); @@ -58,15 +61,30 @@ int load_level(struct level *lvl, const char *fname) return -1; } + lvl->fname = strdup(fname); + if((lvl->dirname = malloc(strlen(fname) + 1))) { +#ifndef LEVEL_EDITOR + path_dir(lvl->fname, lvl->dirname); +#endif + } + + lvl->cell_size = ts_get_attr_num(ts, "cellsize", DEF_CELL_SIZE); + + if((vecptr = ts_get_attr_vec(ts, "player", 0))) { + lvl->px = vecptr[0]; + lvl->py = vecptr[1]; + } + + if((str = ts_get_attr_str(ts, "tileset", 0))) { + lvl->tset = get_tileset(str); + } + iter = ts->child_list; while(iter) { node = iter; iter = iter->next; - if(strcmp(node->name, "tileset") == 0) { - load_tileset(lvl, node); - - } else if(strcmp(node->name, "cell") == 0) { + if(strcmp(node->name, "cell") == 0) { cx = ts_get_attr_int(node, "x", -1); cy = ts_get_attr_int(node, "y", -1); if(cx < 0 || cy < 0 || cx >= sz || cy >= sz) { @@ -75,7 +93,31 @@ int load_level(struct level *lvl, const char *fname) } cell = lvl->cells + cy * sz + cx; cell->type = ts_get_attr_int(node, "blocked", 0) ? CELL_BLOCKED : CELL_WALK; - /* TODO wall tiles and detail objects */ + + /* abuse the next pointer to hang the treestore node temporarilly */ + cell->next = (struct cell*)node; + } + } + + cell = lvl->cells; + for(i=0; iheight; i++) { + for(j=0; jwidth; j++) { + if(cell->type == CELL_SOLID) { + cell++; + continue; + } + + node = (struct ts_node*)cell->next; + cell->next = 0; + + if((tiletype = tile_type(ts_get_attr_str(node, "tiletype", 0))) == -1) { + /* no tile-type specified, try to guess */ + tiletype = detect_cell_tile(lvl, j, i, &cell->tilerot); + } + + cell->tile = get_tile(lvl->tset, tiletype); + + cell++; } } @@ -102,6 +144,20 @@ int save_level(struct level *lvl, const char *fname) ts_set_valuei(&attr->val, lvl->width); ts_add_attr(root, attr); + if(lvl->cell_size && (attr = ts_alloc_attr())) { + ts_set_attr_name(attr, "cellsize"); + ts_set_valuef(&attr->val, lvl->cell_size); + ts_add_attr(root, attr); + } + + if(lvl->px >= 0 && lvl->px < lvl->width && lvl->py >= 0 && lvl->py < lvl->height) { + if((attr = ts_alloc_attr())) { + ts_set_attr_name(attr, "player"); + ts_set_valueiv(&attr->val, 2, lvl->px, lvl->py); + ts_add_attr(root, attr); + } + } + for(i=0; iheight; i++) { for(j=0; jwidth; j++) { cell = lvl->cells + i * lvl->width + j; @@ -151,55 +207,106 @@ err: return -1; } -static int load_tileset(struct level *lvl, struct ts_node *tsn) +#ifndef LEVEL_EDITOR + +static int get_cell_type(struct level *lvl, int x, int y) { - static const char *tile_types[] = {"straight", "corner", "door", 0}; + if(x < 0 || x >= lvl->width || y < 0 || y >= lvl->height) { + return CELL_SOLID; + } + return lvl->cells[y * lvl->width + x].type; +} - int i; - char *path; - const char *str; - struct ts_node *node; - struct tile *tile; - - node = tsn->child_list; - while(node) { - if(strcmp(node->name, "tile") == 0) { - if(!(tile = calloc(1, sizeof *tile))) { - fprintf(stderr, "failed to allocate tile\n"); - return -1; - } - if((str = ts_get_attr_str(node, "name", 0))) { - tile->name = strdup(str); - } - if((str = ts_get_attr_str(node, "type", 0))) { - for(i=0; tile_types[i]; i++) { - if(strcmp(str, tile_types[i]) == 0) { - tile->type = i; - break; - } - } - } - if((str = ts_get_attr_str(node, "scene", 0))) { - if(lvl->dirname) { - path = alloca(strlen(lvl->dirname) + strlen(str) + 2); - combine_path(lvl->dirname, str, path); - } else { - path = (char*)str; - } - load_scenefile(&tile->scn, path); - } +static int detect_cell_tile(struct level *lvl, int x, int y, int *rot) +{ + int i, j, bit; + unsigned int adj = 0; - if(tile->name && tile->scn.meshlist) { /* valid tile */ - tile->next = lvl->tiles; - lvl->tiles = tile; - } else { - fprintf(stderr, "load_tileset: skipping invalid tile: %s\n", - tile->name ? tile->name : "missing tile name"); - free(tile); + bit = 0; + for(i=0; i<3; i++) { + for(j=0; j<3; j++) { + if(i == 1 && j == 1) continue; + if(get_cell_type(lvl, x + j - 1, y + i - 1) == CELL_SOLID) { + adj |= 1 << bit; } + bit++; } - node = node->next; } - return 0; + *rot = 0; + + switch(adj) { + case 0: + case 0757: + /* really we'd need a separate tile type for "all neighbors solid", but we + * probably never going to need that in practice, so fuck it. + */ + return TILE_OPEN; + + case 0555: /* N-S corridor */ + *rot = 1; + case 0707: /* W-E corridor */ + return TILE_STR; + + case 0745: /* S-E corner */ + *rot = 1; + case 0715: /* S-W corner */ + return TILE_CORNER; + case 0547: /* N-E corner */ + *rot = 2; + return TILE_CORNER; + case 0517: /* N-W corner */ + *rot = 3; + return TILE_CORNER; + + case 0507: /* N tee */ + *rot = 3; + case 0515: /* W tee */ + return TILE_TEE; + case 0705: /* S tee */ + *rot = 1; + return TILE_TEE; + case 0545: /* E tee */ + *rot = 2; + return TILE_TEE; + + case 0505: /* cross */ + return TILE_CROSS; + + case 0700: /* S stropen */ + case 0701: + case 0704: + return TILE_STROPEN; + case 0444: /* E stropen */ + case 0445: + case 0544: + *rot = 1; + return TILE_STROPEN; + case 0007: /* N stropen */ + case 0407: + case 0107: + *rot = 2; + return TILE_STROPEN; + case 0111: /* W stropen */ + case 0511: + case 0115: + *rot = 3; + return TILE_STROPEN; + + case 0404: /* E str2open */ + return TILE_STR2OPEN; + case 0005: /* N str2open */ + *rot = 1; + return TILE_STR2OPEN; + case 0101: /* W str2open */ + *rot = 2; + return TILE_STR2OPEN; + case 0500: /* S str2open */ + *rot = 3; + return TILE_STR2OPEN; + } + + return TILE_OPEN; } + +#endif /* !LEVEL_EDITOR */