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))) {
22 void destroy_level(struct level *lvl)
30 int load_level(struct level *lvl, const char *fname)
32 struct ts_node *ts, *node, *iter;
36 lvl->fname = strdup(fname);
37 if((lvl->dirname = malloc(strlen(fname) + 1))) {
38 path_dir(lvl->fname, lvl->dirname);
41 if(!(ts = ts_load(fname))) {
42 fprintf(stderr, "failed to load level: %s\n", fname);
45 if(strcmp(ts->name, "dunged_level") != 0) {
46 fprintf(stderr, "invalid or corrupted level file: %s\n", fname);
51 if((sz = ts_get_attr_int(ts, "size", 0)) <= 0) {
55 if(init_level(lvl, sz, sz) == -1) {
56 fprintf(stderr, "failed to initialize a %dx%d level\n", sz, sz);
61 iter = ts->child_list;
66 if(strcmp(node->name, "tileset") == 0) {
67 load_tileset(lvl, node);
69 } else if(strcmp(node->name, "cell") == 0) {
70 cx = ts_get_attr_int(node, "x", -1);
71 cy = ts_get_attr_int(node, "y", -1);
72 if(cx < 0 || cy < 0 || cx >= sz || cy >= sz) {
73 fprintf(stderr, "ignoring cell with invalid or missing coordinates\n");
76 cell = lvl->cells + cy * sz + cx;
77 cell->type = ts_get_attr_int(node, "blocked", 0) ? CELL_BLOCKED : CELL_WALK;
79 /* abuse the next pointer to hang the treestore node temporarilly */
80 cell->next = (struct cell*)node;
84 /* assign wall types to all occupied cells */
86 for(i=0; i<lvl->height; i++) {
87 for(j=0; j<lvl->width; j++) {
88 if(cell->type == CELL_SOLID) {
93 /* TODO take wall choice from the level file into account */
94 /* TODO detect corners */
95 node = (struct ts_node*)cell->next;
98 if(j <= 0 || cell[-1].type == CELL_SOLID) {
99 cell->wall[0] = TILE_STRAIGHT;
101 if(i <= 0 || cell[-lvl->width].type == CELL_SOLID) {
102 cell->wall[1] = TILE_STRAIGHT;
104 if(j >= lvl->width - 1 || cell[1].type == CELL_SOLID) {
105 cell->wall[2] = TILE_STRAIGHT;
107 if(i >= lvl->height - 1 || cell[lvl->width].type == CELL_SOLID) {
108 cell->wall[3] = TILE_STRAIGHT;
119 /* TODO: save tileset info */
120 int save_level(struct level *lvl, const char *fname)
123 struct ts_node *root, *node;
124 struct ts_attr *attr;
127 if(!(root = ts_alloc_node()) || ts_set_node_name(root, "dunged_level") == -1) {
131 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "size") == -1) {
135 ts_set_valuei(&attr->val, lvl->width);
136 ts_add_attr(root, attr);
138 for(i=0; i<lvl->height; i++) {
139 for(j=0; j<lvl->width; j++) {
140 cell = lvl->cells + i * lvl->width + j;
141 if(cell->type == CELL_SOLID) continue;
143 if(!(node = ts_alloc_node()) || ts_set_node_name(node, "cell") == -1) {
147 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "x") == -1) {
151 ts_set_valuei(&attr->val, j);
152 ts_add_attr(node, attr);
153 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "y") == -1) {
157 ts_set_valuei(&attr->val, i);
158 ts_add_attr(node, attr);
160 if(cell->type == CELL_BLOCKED) {
161 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "blocked") == -1) {
165 ts_set_valuei(&attr->val, 1);
166 ts_add_attr(node, attr);
169 ts_add_child(root, node);
173 if(ts_save(root, fname) == -1) {
174 fprintf(stderr, "failed to save level: %s\n", fname);
182 fprintf(stderr, "failed to construct treestore tree\n");
187 static int load_tileset(struct level *lvl, struct ts_node *tsn)
189 static const char *tile_types[] = {"straight", "corner", "door", 0};
194 struct ts_node *node;
197 node = tsn->child_list;
199 if(strcmp(node->name, "tile") == 0) {
200 if(!(tile = calloc(1, sizeof *tile))) {
201 fprintf(stderr, "failed to allocate tile\n");
204 if((str = ts_get_attr_str(node, "name", 0))) {
205 tile->name = strdup(str);
207 if((str = ts_get_attr_str(node, "type", 0))) {
208 for(i=0; tile_types[i]; i++) {
209 if(strcmp(str, tile_types[i]) == 0) {
215 if((str = ts_get_attr_str(node, "scene", 0))) {
217 path = alloca(strlen(lvl->dirname) + strlen(str) + 2);
218 combine_path(lvl->dirname, str, path);
222 load_scenefile(&tile->scn, path);
225 if(tile->name && tile->scn.meshlist) { /* valid tile */
226 tile->next = lvl->tiles;
229 fprintf(stderr, "load_tileset: skipping invalid tile: %s\n",
230 tile->name ? tile->name : "missing tile name");
240 struct tile *find_level_tile(struct level *lvl, const char *tname)
242 struct tile *tile = lvl->tiles;
244 if(strcmp(tile->name, tname) == 0) {
252 int gen_cell_geom(struct level *lvl, struct cell *cell)
255 struct meshgroup *wallgeom;
257 struct mesh *mesh, *tmesh;
262 if(!(tstr = find_level_tile(lvl, "straight"))) {
266 if(!(wallgeom = malloc(sizeof *wallgeom))) {
269 init_meshgroup(wallgeom);
272 if(cell->wall[i] == TILE_STRAIGHT) { /* TODO: support other wall types */
273 cgm_mrotation_y(xform, i * M_PI / 2.0f);
275 tmesh = tstr->scn.meshlist;
277 if(!(mesh = malloc(sizeof *mesh))) {
281 /* create a copy of the tile mesh */
282 if(copy_mesh(mesh, tmesh) == -1) {
286 if(i) xform_mesh(mesh, xform); /* rotate it to match the wall angle */
288 /* add it to the level meshlist */
289 mesh->next = lvl->meshlist;
290 lvl->meshlist = mesh;
292 /* add it to the meshgroup */
293 if(add_meshgroup_mesh(wallgeom, mesh) == -1) {
304 /* TODO: append to other existing meshgroups for detail objects */
305 cell->mgrp = wallgeom;
311 int gen_level_geom(struct level *lvl)
316 for(i=0; i<lvl->height; i++) {
317 for(j=0; j<lvl->width; j++) {
318 cell = lvl->cells + i * lvl->width + j;
319 if(cell->type != CELL_SOLID) {
320 if(gen_cell_geom(lvl, cell) == -1) {