9 static int load_tileset(struct level *lvl, struct ts_node *tsn);
11 int init_level(struct level *lvl, int xsz, int ysz)
13 memset(lvl, 0, sizeof *lvl);
15 if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) {
21 lvl->cell_size = DEF_CELL_SIZE;
22 lvl->px = lvl->py = -1;
26 void destroy_level(struct level *lvl)
34 int load_level(struct level *lvl, const char *fname)
36 struct ts_node *ts, *node, *iter;
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 lvl->fname = strdup(fname);
62 if((lvl->dirname = malloc(strlen(fname) + 1))) {
64 path_dir(lvl->fname, lvl->dirname);
68 lvl->cell_size = ts_get_attr_num(ts, "cellsize", DEF_CELL_SIZE);
70 if((vecptr = ts_get_attr_vec(ts, "player", 0))) {
75 iter = ts->child_list;
80 if(strcmp(node->name, "tileset") == 0) {
81 load_tileset(lvl, node);
83 } else if(strcmp(node->name, "cell") == 0) {
84 cx = ts_get_attr_int(node, "x", -1);
85 cy = ts_get_attr_int(node, "y", -1);
86 if(cx < 0 || cy < 0 || cx >= sz || cy >= sz) {
87 fprintf(stderr, "ignoring cell with invalid or missing coordinates\n");
90 cell = lvl->cells + cy * sz + cx;
91 cell->type = ts_get_attr_int(node, "blocked", 0) ? CELL_BLOCKED : CELL_WALK;
93 /* abuse the next pointer to hang the treestore node temporarilly */
94 cell->next = (struct cell*)node;
98 /* assign wall types to all occupied cells */
100 for(i=0; i<lvl->height; i++) {
101 for(j=0; j<lvl->width; j++) {
102 if(cell->type == CELL_SOLID) {
107 /* TODO take wall choice from the level file into account */
108 /* TODO detect corners */
109 node = (struct ts_node*)cell->next;
112 if(i >= lvl->height - 1 || cell[lvl->width].type == CELL_SOLID) {
113 cell->wall[0] = TILE_STRAIGHT;
115 if(j >= lvl->width - 1 || cell[1].type == CELL_SOLID) {
116 cell->wall[1] = TILE_STRAIGHT;
118 if(i <= 0 || cell[-lvl->width].type == CELL_SOLID) {
119 cell->wall[2] = TILE_STRAIGHT;
121 if(j <= 0 || cell[-1].type == CELL_SOLID) {
122 cell->wall[3] = TILE_STRAIGHT;
133 /* TODO: save tileset info */
134 int save_level(struct level *lvl, const char *fname)
137 struct ts_node *root, *node;
138 struct ts_attr *attr;
141 if(!(root = ts_alloc_node()) || ts_set_node_name(root, "dunged_level") == -1) {
145 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "size") == -1) {
149 ts_set_valuei(&attr->val, lvl->width);
150 ts_add_attr(root, attr);
152 if(lvl->cell_size && (attr = ts_alloc_attr())) {
153 ts_set_attr_name(attr, "cellsize");
154 ts_set_valuef(&attr->val, lvl->cell_size);
155 ts_add_attr(root, attr);
158 if(lvl->px >= 0 && lvl->px < lvl->width && lvl->py >= 0 && lvl->py < lvl->height) {
159 if((attr = ts_alloc_attr())) {
160 ts_set_attr_name(attr, "player");
161 ts_set_valueiv(&attr->val, 2, lvl->px, lvl->py);
162 ts_add_attr(root, attr);
166 for(i=0; i<lvl->height; i++) {
167 for(j=0; j<lvl->width; j++) {
168 cell = lvl->cells + i * lvl->width + j;
169 if(cell->type == CELL_SOLID) continue;
171 if(!(node = ts_alloc_node()) || ts_set_node_name(node, "cell") == -1) {
175 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "x") == -1) {
179 ts_set_valuei(&attr->val, j);
180 ts_add_attr(node, attr);
181 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "y") == -1) {
185 ts_set_valuei(&attr->val, i);
186 ts_add_attr(node, attr);
188 if(cell->type == CELL_BLOCKED) {
189 if(!(attr = ts_alloc_attr()) || ts_set_attr_name(attr, "blocked") == -1) {
193 ts_set_valuei(&attr->val, 1);
194 ts_add_attr(node, attr);
197 ts_add_child(root, node);
201 if(ts_save(root, fname) == -1) {
202 fprintf(stderr, "failed to save level: %s\n", fname);
210 fprintf(stderr, "failed to construct treestore tree\n");
217 static int load_tileset(struct level *lvl, struct ts_node *tsn)
219 static const char *tile_types[] = {
220 "open", "straight", "corner", "tee", "cross", "str2open", 0
226 struct ts_node *node;
229 node = tsn->child_list;
231 if(strcmp(node->name, "tile") == 0) {
232 if(!(tile = calloc(1, sizeof *tile))) {
233 fprintf(stderr, "failed to allocate tile\n");
236 if((str = ts_get_attr_str(node, "name", 0))) {
237 tile->name = strdup(str);
239 if((str = ts_get_attr_str(node, "type", 0))) {
240 for(i=0; tile_types[i]; i++) {
241 if(strcmp(str, tile_types[i]) == 0) {
247 if((str = ts_get_attr_str(node, "scene", 0))) {
249 path = alloca(strlen(lvl->dirname) + strlen(str) + 2);
250 combine_path(lvl->dirname, str, path);
254 load_scenefile(&tile->scn, path);
257 if(tile->name && tile->scn.meshlist) { /* valid tile */
258 tile->next = lvl->tiles;
261 fprintf(stderr, "load_tileset: skipping invalid tile: %s\n",
262 tile->name ? tile->name : "missing tile name");
272 struct tile *find_level_tile(struct level *lvl, int type)
274 struct tile *tile = lvl->tiles;
276 if(tile->type == type) {
284 int gen_cell_geom(struct level *lvl, struct cell *cell)
287 struct meshgroup *wallgeom;
289 struct mesh *mesh, *tmesh;
292 if(!(tstr = find_level_tile(lvl, TILE_STRAIGHT))) {
296 if(!(wallgeom = malloc(sizeof *wallgeom))) {
299 init_meshgroup(wallgeom);
302 if(cell->wall[i] == TILE_STRAIGHT) { /* TODO: support other wall types */
303 cgm_mrotation_y(xform, i * M_PI / 2.0f);
305 tmesh = tstr->scn.meshlist;
307 if(!(mesh = malloc(sizeof *mesh))) {
311 /* create a copy of the tile mesh */
312 if(copy_mesh(mesh, tmesh) == -1) {
316 if(i) xform_mesh(mesh, xform); /* rotate it to match the wall angle */
318 /* add it to the level meshlist */
319 mesh->next = lvl->meshlist;
320 lvl->meshlist = mesh;
322 /* add it to the meshgroup */
323 if(add_meshgroup_mesh(wallgeom, mesh) == -1) {
334 /* TODO: append to other existing meshgroups for detail objects */
335 cell->mgrp = wallgeom;
341 int gen_level_geom(struct level *lvl)
346 for(i=0; i<lvl->height; i++) {
347 for(j=0; j<lvl->width; j++) {
348 cell = lvl->cells + i * lvl->width + j;
349 if(cell->type != CELL_SOLID) {
350 if(gen_cell_geom(lvl, cell) == -1) {
351 printf("failed to generate cell\n");
362 static int load_tileset(struct level *lvl, struct ts_node *tsn)
364 return 0; /* in the level editor we don't need tileset loading */
367 #endif /* !LEVEL_EDITOR */