comparison librpg/rpg/map-file.c @ 212:ddfe0a211169

rpg: experiment with map teleport
author David Demelier <markand@malikania.fr>
date Mon, 16 Nov 2020 14:26:13 +0100
parents adcbb7ccfdee
children 64f24b482722
comparison
equal deleted inserted replaced
211:adcbb7ccfdee 212:ddfe0a211169
27 #include <string.h> 27 #include <string.h>
28 28
29 #include <core/alloc.h> 29 #include <core/alloc.h>
30 #include <core/error.h> 30 #include <core/error.h>
31 #include <core/image.h> 31 #include <core/image.h>
32 #include <core/trace.h>
32 33
33 #include "map-file.h" 34 #include "map-file.h"
34 35
35 /* Create %<v>c string literal for scanf */ 36 /* Create %<v>c string literal for scanf */
36 #define MAX_F(v) MAX_F_(v) 37 #define MAX_F(v) MAX_F_(v)
56 57
57 return 0; 58 return 0;
58 } 59 }
59 60
60 static bool 61 static bool
61 parse_layer(struct parser *ps, const char *line) 62 parse_tiles(struct parser *ps, const char *layer_name)
62 { 63 {
63 char layer_name[32 + 1] = {0};
64 enum map_layer_type layer_type; 64 enum map_layer_type layer_type;
65 size_t amount, current; 65 size_t amount, current;
66
67 /* Check if weight/height has been specified. */
68 if (ps->map->w == 0 || ps->map->h == 0)
69 return errorf("missing map dimensions before layer");
70
71 /* Determine layer type. */
72 if (sscanf(line, "layer|%32s", layer_name) <= 0)
73 return errorf("missing layer type definition");
74 66
75 if (strcmp(layer_name, "background") == 0) 67 if (strcmp(layer_name, "background") == 0)
76 layer_type = MAP_LAYER_TYPE_BACKGROUND; 68 layer_type = MAP_LAYER_TYPE_BACKGROUND;
77 else if (strcmp(layer_name, "foreground") == 0) 69 else if (strcmp(layer_name, "foreground") == 0)
78 layer_type = MAP_LAYER_TYPE_FOREGROUND; 70 layer_type = MAP_LAYER_TYPE_FOREGROUND;
93 ps->mf->layers[layer_type].tiles[current] = tile; 85 ps->mf->layers[layer_type].tiles[current] = tile;
94 86
95 ps->map->layers[layer_type].tiles = ps->mf->layers[layer_type].tiles; 87 ps->map->layers[layer_type].tiles = ps->mf->layers[layer_type].tiles;
96 88
97 return true; 89 return true;
90 }
91
92 static bool
93 parse_actions(struct parser *ps)
94 {
95 char exec[128 + 1];
96 int x = 0, y = 0;
97 unsigned int w = 0, h = 0;
98
99 while (fscanf(ps->fp, "%d|%d|%u|%u|%128[^\n]\n", &x, &y, &w, &h, exec) == 5) {
100 struct action *act;
101
102 if (!ps->mf->load_action) {
103 tracef("ignoring action %d,%d,%u,%u,%s", x, y, w, h, exec);
104 continue;
105 }
106
107 if ((act = ps->mf->load_action(ps->map, x, y, w, h, exec)))
108 action_stack_add(&ps->map->actions, act);
109 }
110
111 return true;
112 }
113
114 static bool
115 parse_layer(struct parser *ps, const char *line)
116 {
117 char layer_name[32 + 1] = {0};
118
119 /* Check if weight/height has been specified. */
120 if (ps->map->w == 0 || ps->map->h == 0)
121 return errorf("missing map dimensions before layer");
122
123 /* Determine layer type. */
124 if (sscanf(line, "layer|%32s", layer_name) <= 0)
125 return errorf("missing layer type definition");
126
127 if (strcmp(layer_name, "actions") == 0)
128 return parse_actions(ps);
129
130 return parse_tiles(ps, layer_name);
98 } 131 }
99 132
100 static bool 133 static bool
101 parse_tileset(struct parser *ps, const char *line) 134 parse_tileset(struct parser *ps, const char *line)
102 { 135 {
304 assert(map); 337 assert(map);
305 338
306 FILE *fp; 339 FILE *fp;
307 bool ret = true; 340 bool ret = true;
308 341
309 memset(file, 0, sizeof (*file));
310 memset(map, 0, sizeof (*map)); 342 memset(map, 0, sizeof (*map));
311 343
312 if (!(fp = fopen(path, "r"))) 344 if (!(fp = fopen(path, "r")))
313 return errorf("%s", strerror(errno)); 345 return errorf("%s", strerror(errno));
314 346