starts to sortof look like a dungeon if you squint
[vrlugburz] / src / level.c
index 70af648..383ebb5 100644 (file)
@@ -10,12 +10,16 @@ static int load_tileset(struct level *lvl, struct ts_node *tsn);
 
 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;
 }
 
@@ -32,11 +36,7 @@ int load_level(struct level *lvl, const char *fname)
        struct ts_node *ts, *node, *iter;
        int i, j, sz, cx, cy;
        struct cell *cell;
-
-       lvl->fname = strdup(fname);
-       if((lvl->dirname = malloc(strlen(fname) + 1))) {
-               path_dir(lvl->fname, lvl->dirname);
-       }
+       float *vecptr;
 
        if(!(ts = ts_load(fname))) {
                fprintf(stderr, "failed to load level: %s\n", fname);
@@ -58,6 +58,20 @@ 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];
+       }
+
        iter = ts->child_list;
        while(iter) {
                node = iter;
@@ -95,16 +109,16 @@ int load_level(struct level *lvl, const char *fname)
                        node = (struct ts_node*)cell->next;
                        cell->next = 0;
 
-                       if(j <= 0 || cell[-1].type == CELL_SOLID) {
+                       if(i >= lvl->height - 1 || cell[lvl->width].type == CELL_SOLID) {
                                cell->wall[0] = TILE_STRAIGHT;
                        }
-                       if(i <= 0 || cell[-lvl->width].type == CELL_SOLID) {
+                       if(j >= lvl->width - 1 || cell[1].type == CELL_SOLID) {
                                cell->wall[1] = TILE_STRAIGHT;
                        }
-                       if(j >= lvl->width - 1 || cell[1].type == CELL_SOLID) {
+                       if(i <= 0 || cell[-lvl->width].type == CELL_SOLID) {
                                cell->wall[2] = TILE_STRAIGHT;
                        }
-                       if(i >= lvl->height - 1 || cell[lvl->width].type == CELL_SOLID) {
+                       if(j <= 0 || cell[-1].type == CELL_SOLID) {
                                cell->wall[3] = TILE_STRAIGHT;
                        }
 
@@ -135,6 +149,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; i<lvl->height; i++) {
                for(j=0; j<lvl->width; j++) {
                        cell = lvl->cells + i * lvl->width + j;
@@ -184,9 +212,11 @@ err:
        return -1;
 }
 
+#ifndef LEVEL_EDITOR
+
 static int load_tileset(struct level *lvl, struct ts_node *tsn)
 {
-       static const char *tile_types[] = {"straight", "corner", "door", 0};
+       static const char *tile_types[] = {"empty", "straight", "corner", "door", 0};
 
        int i;
        char *path;
@@ -237,11 +267,11 @@ static int load_tileset(struct level *lvl, struct ts_node *tsn)
        return 0;
 }
 
-struct tile *find_level_tile(struct level *lvl, const char *tname)
+struct tile *find_level_tile(struct level *lvl, int type)
 {
        struct tile *tile = lvl->tiles;
        while(tile) {
-               if(strcmp(tile->name, tname) == 0) {
+               if(tile->type == type) {
                        return tile;
                }
                tile = tile->next;
@@ -257,9 +287,7 @@ int gen_cell_geom(struct level *lvl, struct cell *cell)
        struct mesh *mesh, *tmesh;
        float xform[16];
 
-       printf("foo!\n");
-
-       if(!(tstr = find_level_tile(lvl, "straight"))) {
+       if(!(tstr = find_level_tile(lvl, TILE_STRAIGHT))) {
                return -1;
        }
 
@@ -318,6 +346,7 @@ int gen_level_geom(struct level *lvl)
                        cell = lvl->cells + i * lvl->width + j;
                        if(cell->type != CELL_SOLID) {
                                if(gen_cell_geom(lvl, cell) == -1) {
+                                       printf("failed to generate cell\n");
                                        return -1;
                                }
                        }
@@ -325,3 +354,12 @@ int gen_level_geom(struct level *lvl)
        }
        return 0;
 }
+
+#else
+
+static int load_tileset(struct level *lvl, struct ts_node *tsn)
+{
+       return 0;               /* in the level editor we don't need tileset loading */
+}
+
+#endif /* !LEVEL_EDITOR */