level save/load functions
[vrlugburz] / tools / dunger / src / level.c
index f61f3f6..3a1a9bf 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <GL/gl.h>
+#include <errno.h>
+#include <treestore.h>
 #include "level.h"
 
-extern int view_width, view_height;
-
-struct level *create_level(int xsz, int ysz)
+int init_level(struct level *lvl, int xsz, int ysz)
 {
-       struct level *lvl;
-
-       if(!(lvl = malloc(sizeof *lvl))) {
-               return 0;
-       }
        if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) {
                free(lvl);
-               return 0;
+               return -1;
        }
        lvl->width = xsz;
        lvl->height = ysz;
-       return lvl;
+       return 0;
 }
 
-void free_level(struct level *lvl)
+void destroy_level(struct level *lvl)
 {
        if(!lvl) return;
        free(lvl->cells);
-       free(lvl);
 }
 
-static void draw_cell(float x, float y, float sz, struct cell *cell)
+int load_level(struct level *lvl, const char *fname)
 {
-       static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
+       FILE *fp;
+       struct ts_node *ts, *node, *iter;
+       int sz, cx, cy;
+       struct cell *cell;
+
+       if(!(fp = fopen(fname, "rb"))) {
+               return -1;
+       }
+
+       if(!(ts = ts_load(fname))) {
+               fprintf(stderr, "failed to load level: %s\n", fname);
+               return -1;
+       }
+       if(strcmp(ts->name, "dunged_level") != 0) {
+               fprintf(stderr, "invalid or corrupted level file: %s\n", fname);
+               ts_free_tree(ts);
+               return -1;
+       }
+
+       if((sz = ts_get_attr_int(ts, "size", 0)) <= 0) {
+               sz = 32;
+       }
+
+       if(init_level(lvl, sz, sz) == -1) {
+               fprintf(stderr, "failed to initialize a %dx%d level\n", sz, sz);
+               ts_free_tree(ts);
+               return -1;
+       }
 
-       if(cell) {
-               glColor3fv(colors[cell->type]);
-       } else {
-               glColor3f(1, 1, 1);
+       iter = ts->child_list;
+       while(iter) {
+               node = iter;
+               iter = iter->next;
+
+               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) {
+                               fprintf(stderr, "ignoring cell with invalid or missing coordinates\n");
+                               continue;
+                       }
+                       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 */
+               }
        }
-       glVertex2f(x, y);
-       glVertex2f(x + sz, y);
-       glVertex2f(x + sz, y + sz);
-       glVertex2f(x, y + sz);
+
+       ts_free_tree(ts);
+       return 0;
 }
 
-void draw_level(struct level *lvl)
+int save_level(struct level *lvl, const char *fname)
 {
        int i, j;
-       float x, y, dx, dy, cellsz;
+       struct ts_node *root, *node;
+       struct ts_attr *attr;
        struct cell *cell;
 
-       dx = view_width / lvl->width;
-       dy = view_height / lvl->height;
-       cellsz = dx > dy ? dy : dx;
+       if(!(root = ts_alloc_node()) || ts_set_node_name(root, "dunged_level") == -1) {
+               goto err;
+       }
 
-       glBegin(GL_QUADS);
-       cell = lvl->cells;
-       y = 0;
-       for(i=0; i<lvl->height; i++) {
-               x = 0;
-               for(j=0; j<lvl->width; j++) {
-                       draw_cell(x, y, cellsz, cell++);
-                       x += cellsz;
-               }
-               y += cellsz;
+       if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "size") == -1) {
+               ts_free_attr(attr);
+               goto err;
        }
-       glEnd();
+       ts_set_valuei(&attr->val, lvl->width);
+       ts_add_attr(root, attr);
 
-       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-       glBegin(GL_QUADS);
-       y = 0;
        for(i=0; i<lvl->height; i++) {
-               x = 0;
                for(j=0; j<lvl->width; j++) {
-                       draw_cell(x, y, cellsz, 0);
-                       x += cellsz;
+                       cell = lvl->cells + i * lvl->width + j;
+                       if(cell->type == CELL_SOLID) continue;
+
+                       if(!(node = ts_alloc_node()) || ts_set_node_name(node, "cell") == -1) {
+                               ts_free_node(node);
+                               goto err;
+                       }
+                       if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "x") == -1) {
+                               ts_free_attr(attr);
+                               goto err;
+                       }
+                       ts_set_valuei(&attr->val, j);
+                       ts_add_attr(node, attr);
+                       if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "y") == -1) {
+                               ts_free_attr(attr);
+                               goto err;
+                       }
+                       ts_set_valuei(&attr->val, i);
+                       ts_add_attr(node, attr);
+
+                       if(cell->type == CELL_BLOCKED) {
+                               if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "blocked") == -1) {
+                                       ts_free_attr(attr);
+                                       goto err;
+                               }
+                               ts_set_valuei(&attr->val, 1);
+                               ts_add_attr(node, attr);
+                       }
+
+                       ts_add_child(root, node);
                }
-               y += cellsz;
        }
-       glEnd();
-       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+
+       if(ts_save(root, fname) == -1) {
+               fprintf(stderr, "failed to save level: %s\n", fname);
+               ts_free_tree(root);
+               return -1;
+       }
+       ts_free_tree(root);
+       return 0;
+
+err:
+       fprintf(stderr, "failed to construct treestore tree\n");
+       ts_free_tree(root);
+       return -1;
 }