9 static int load_tileset(struct level *lvl, struct ts_node *tsn);
11 int init_level(struct level *lvl, int xsz, int ysz)
13 if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) {
19 lvl->cell_size = DEF_CELL_SIZE;
23 void destroy_level(struct level *lvl)
31 int load_level(struct level *lvl, const char *fname)
33 struct ts_node *ts, *node, *iter;
38 lvl->fname = strdup(fname);
39 if((lvl->dirname = malloc(strlen(fname) + 1))) {
40 path_dir(lvl->fname, lvl->dirname);
43 if(!(ts = ts_load(fname))) {
44 fprintf(stderr, "failed to load level: %s\n", fname);
47 if(strcmp(ts->name, "dunged_level") != 0) {
48 fprintf(stderr, "invalid or corrupted level file: %s\n", fname);
53 if((sz = ts_get_attr_int(ts, "size", 0)) <= 0) {
57 if(init_level(lvl, sz, sz) == -1) {
58 fprintf(stderr, "failed to initialize a %dx%d level\n", sz, sz);
62 lvl->cell_size = ts_get_attr_num(ts, "cellsize", DEF_CELL_SIZE);
64 if((vecptr = ts_get_attr_vec(ts, "player", 0))) {
69 iter = ts->child_list;
74 if(strcmp(node->name, "tileset") == 0) {
75 load_tileset(lvl, node);
77 } else if(strcmp(node->name, "cell") == 0) {
78 cx = ts_get_attr_int(node, "x", -1);
79 cy = ts_get_attr_int(node, "y", -1);
80 if(cx < 0 || cy < 0 || cx >= sz || cy >= sz) {
81 fprintf(stderr, "ignoring cell with invalid or missing coordinates\n");
84 cell = lvl->cells + cy * sz + cx;
85 cell->type = ts_get_attr_int(node, "blocked", 0) ? CELL_BLOCKED : CELL_WALK;
87 /* abuse the next pointer to hang the treestore node temporarilly */
88 cell->next = (struct cell*)node;
92 /* assign wall types to all occupied cells */
94 for(i=0; i<lvl->height; i++) {
95 for(j=0; j<lvl->width; j++) {
96 if(cell->type == CELL_SOLID) {
101 /* TODO take wall choice from the level file into account */
102 /* TODO detect corners */
103 node = (struct ts_node*)cell->next;
106 if(j <= 0 || cell[-1].type == CELL_SOLID) {
107 cell->wall[0] = TILE_STRAIGHT;
109 if(i <= 0 || cell[-lvl->width].type == CELL_SOLID) {
110 cell->wall[1] = TILE_STRAIGHT;
112 if(j >= lvl->width - 1 || cell[1].type == CELL_SOLID) {
113 cell->wall[2] = TILE_STRAIGHT;
115 if(i >= lvl->height - 1 || cell[lvl->width].type == CELL_SOLID) {
116 cell->wall[3] = TILE_STRAIGHT;
127 /* TODO: save tileset info */
128 int save_level(struct level *lvl, const char *fname)
131 struct ts_node *root, *node;
132 struct ts_attr *attr;
135 if(!(root = ts_alloc_node()) || ts_set_node_name(root, "dunged_level") == -1) {
139 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "size") == -1) {
143 ts_set_valuei(&attr->val, lvl->width);
144 ts_add_attr(root, attr);
146 for(i=0; i<lvl->height; i++) {
147 for(j=0; j<lvl->width; j++) {
148 cell = lvl->cells + i * lvl->width + j;
149 if(cell->type == CELL_SOLID) continue;
151 if(!(node = ts_alloc_node()) || ts_set_node_name(node, "cell") == -1) {
155 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "x") == -1) {
159 ts_set_valuei(&attr->val, j);
160 ts_add_attr(node, attr);
161 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "y") == -1) {
165 ts_set_valuei(&attr->val, i);
166 ts_add_attr(node, attr);
168 if(cell->type == CELL_BLOCKED) {
169 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "blocked") == -1) {
173 ts_set_valuei(&attr->val, 1);
174 ts_add_attr(node, attr);
177 ts_add_child(root, node);
181 if(ts_save(root, fname) == -1) {
182 fprintf(stderr, "failed to save level: %s\n", fname);
190 fprintf(stderr, "failed to construct treestore tree\n");
195 static int load_tileset(struct level *lvl, struct ts_node *tsn)
197 static const char *tile_types[] = {"empty", "straight", "corner", "door", 0};
202 struct ts_node *node;
205 node = tsn->child_list;
207 if(strcmp(node->name, "tile") == 0) {
208 if(!(tile = calloc(1, sizeof *tile))) {
209 fprintf(stderr, "failed to allocate tile\n");
212 if((str = ts_get_attr_str(node, "name", 0))) {
213 tile->name = strdup(str);
215 if((str = ts_get_attr_str(node, "type", 0))) {
216 for(i=0; tile_types[i]; i++) {
217 if(strcmp(str, tile_types[i]) == 0) {
223 if((str = ts_get_attr_str(node, "scene", 0))) {
225 path = alloca(strlen(lvl->dirname) + strlen(str) + 2);
226 combine_path(lvl->dirname, str, path);
230 load_scenefile(&tile->scn, path);
233 if(tile->name && tile->scn.meshlist) { /* valid tile */
234 tile->next = lvl->tiles;
237 fprintf(stderr, "load_tileset: skipping invalid tile: %s\n",
238 tile->name ? tile->name : "missing tile name");
248 struct tile *find_level_tile(struct level *lvl, int type)
250 struct tile *tile = lvl->tiles;
252 if(tile->type == type) {
260 int gen_cell_geom(struct level *lvl, struct cell *cell)
263 struct meshgroup *wallgeom;
265 struct mesh *mesh, *tmesh;
268 if(!(tstr = find_level_tile(lvl, TILE_STRAIGHT))) {
272 if(!(wallgeom = malloc(sizeof *wallgeom))) {
275 init_meshgroup(wallgeom);
278 if(cell->wall[i] == TILE_STRAIGHT) { /* TODO: support other wall types */
279 cgm_mrotation_y(xform, i * M_PI / 2.0f);
281 tmesh = tstr->scn.meshlist;
283 if(!(mesh = malloc(sizeof *mesh))) {
287 /* create a copy of the tile mesh */
288 if(copy_mesh(mesh, tmesh) == -1) {
292 if(i) xform_mesh(mesh, xform); /* rotate it to match the wall angle */
294 /* add it to the level meshlist */
295 mesh->next = lvl->meshlist;
296 lvl->meshlist = mesh;
298 /* add it to the meshgroup */
299 if(add_meshgroup_mesh(wallgeom, mesh) == -1) {
310 /* TODO: append to other existing meshgroups for detail objects */
311 cell->mgrp = wallgeom;
317 int gen_level_geom(struct level *lvl)
322 for(i=0; i<lvl->height; i++) {
323 for(j=0; j<lvl->width; j++) {
324 cell = lvl->cells + i * lvl->width + j;
325 if(cell->type != CELL_SOLID) {
326 if(gen_cell_geom(lvl, cell) == -1) {
327 printf("failed to generate cell\n");