Mercurial > molko
comparison src/libmlk-rpg/rpg/map-file.c @ 411:d74f53299252
make: add basic GNU make support
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 07 Oct 2022 14:35:31 +0200 |
parents | 7321511052f6 |
children | 7d2ebc334c8c |
comparison
equal
deleted
inserted
replaced
410:1bf7d6669f0a | 411:d74f53299252 |
---|---|
31 #include <core/image.h> | 31 #include <core/image.h> |
32 #include <core/trace.h> | 32 #include <core/trace.h> |
33 #include <core/zfile.h> | 33 #include <core/zfile.h> |
34 | 34 |
35 #include "map-file.h" | 35 #include "map-file.h" |
36 #include "rpg_p.h" | |
37 | 36 |
38 #define MAX_F(v) MAX_F_(v) | 37 #define MAX_F(v) MAX_F_(v) |
39 #define MAX_F_(v) "%" #v "[^\n|]" | 38 #define MAX_F_(v) "%" #v "[^\n|]" |
40 | 39 |
41 struct context { | 40 struct context { |
56 else if (strcmp(layer_name, "foreground") == 0) | 55 else if (strcmp(layer_name, "foreground") == 0) |
57 layer_type = MAP_LAYER_TYPE_FOREGROUND; | 56 layer_type = MAP_LAYER_TYPE_FOREGROUND; |
58 else if (strcmp(layer_name, "above") == 0) | 57 else if (strcmp(layer_name, "above") == 0) |
59 layer_type = MAP_LAYER_TYPE_ABOVE; | 58 layer_type = MAP_LAYER_TYPE_ABOVE; |
60 else | 59 else |
61 return errorf(_("invalid layer type: %s"), layer_name); | 60 return errorf("invalid layer type: %s", layer_name); |
62 | 61 |
63 amount = ctx->map->columns * ctx->map->rows; | 62 amount = ctx->map->columns * ctx->map->rows; |
64 current = 0; | 63 current = 0; |
65 | 64 |
66 /* | 65 /* |
87 | 86 |
88 while (fscanf(ctx->fp, "%d|%d|%u|%u|%d|%128[^\n]\n", &x, &y, &w, &h, &block, exec) >= 5) { | 87 while (fscanf(ctx->fp, "%d|%d|%u|%u|%d|%128[^\n]\n", &x, &y, &w, &h, &block, exec) >= 5) { |
89 struct map_block *reg; | 88 struct map_block *reg; |
90 | 89 |
91 if (!ctx->mf->load_action) { | 90 if (!ctx->mf->load_action) { |
92 tracef(_("ignoring action %d,%d,%u,%u,%d,%s"), x, y, w, h, block, exec); | 91 tracef("ignoring action %d,%d,%u,%u,%d,%s", x, y, w, h, block, exec); |
93 continue; | 92 continue; |
94 } | 93 } |
95 | 94 |
96 ctx->mf->load_action(ctx->map, x, y, w, h, exec); | 95 ctx->mf->load_action(ctx->map, x, y, w, h, exec); |
97 | 96 |
125 { | 124 { |
126 char layer_name[32 + 1] = {0}; | 125 char layer_name[32 + 1] = {0}; |
127 | 126 |
128 /* Check if weight/height has been specified. */ | 127 /* Check if weight/height has been specified. */ |
129 if (ctx->map->columns == 0 || ctx->map->rows == 0) | 128 if (ctx->map->columns == 0 || ctx->map->rows == 0) |
130 return errorf(_("missing map dimensions before layer")); | 129 return errorf("missing map dimensions before layer"); |
131 | 130 |
132 /* Determine layer type. */ | 131 /* Determine layer type. */ |
133 if (sscanf(line, "layer|%32s", layer_name) <= 0) | 132 if (sscanf(line, "layer|%32s", layer_name) <= 0) |
134 return errorf(_("missing layer type definition")); | 133 return errorf("missing layer type definition"); |
135 | 134 |
136 if (strcmp(layer_name, "actions") == 0) | 135 if (strcmp(layer_name, "actions") == 0) |
137 return parse_actions(ctx); | 136 return parse_actions(ctx); |
138 | 137 |
139 return parse_layer_tiles(ctx, layer_name); | 138 return parse_layer_tiles(ctx, layer_name); |
145 char path[PATH_MAX] = {0}, *p; | 144 char path[PATH_MAX] = {0}, *p; |
146 struct map_file *mf = ctx->mf; | 145 struct map_file *mf = ctx->mf; |
147 struct tileset_file *tf = &mf->tileset_file; | 146 struct tileset_file *tf = &mf->tileset_file; |
148 | 147 |
149 if (!(p = strchr(line, '|'))) | 148 if (!(p = strchr(line, '|'))) |
150 return errorf(_("could not parse tileset")); | 149 return errorf("could not parse tileset"); |
151 | 150 |
152 snprintf(path, sizeof (path), "%s/%s", ctx->basedir, p + 1); | 151 snprintf(path, sizeof (path), "%s/%s", ctx->basedir, p + 1); |
153 | 152 |
154 if (tileset_file_open(tf, &mf->tileset, path) < 0) | 153 if (tileset_file_open(tf, &mf->tileset, path) < 0) |
155 return -1; | 154 return -1; |
161 | 160 |
162 static int | 161 static int |
163 parse_title(struct context *ctx, const char *line) | 162 parse_title(struct context *ctx, const char *line) |
164 { | 163 { |
165 if (sscanf(line, "title|" MAX_F(MAP_FILE_TITLE_MAX), ctx->mf->title) != 1 || strlen(ctx->mf->title) == 0) | 164 if (sscanf(line, "title|" MAX_F(MAP_FILE_TITLE_MAX), ctx->mf->title) != 1 || strlen(ctx->mf->title) == 0) |
166 return errorf(_("null map title")); | 165 return errorf("null map title"); |
167 | 166 |
168 ctx->map->title = ctx->mf->title; | 167 ctx->map->title = ctx->mf->title; |
169 | 168 |
170 return 0; | 169 return 0; |
171 } | 170 } |
172 | 171 |
173 static int | 172 static int |
174 parse_columns(struct context *ctx, const char *line) | 173 parse_columns(struct context *ctx, const char *line) |
175 { | 174 { |
176 if (sscanf(line, "columns|%u", &ctx->map->columns) != 1 || ctx->map->columns == 0) | 175 if (sscanf(line, "columns|%u", &ctx->map->columns) != 1 || ctx->map->columns == 0) |
177 return errorf(_("null map columns")); | 176 return errorf("null map columns"); |
178 | 177 |
179 return 0; | 178 return 0; |
180 } | 179 } |
181 | 180 |
182 static int | 181 static int |
183 parse_rows(struct context *ctx, const char *line) | 182 parse_rows(struct context *ctx, const char *line) |
184 { | 183 { |
185 if (sscanf(line, "rows|%u", &ctx->map->rows) != 1 || ctx->map->rows == 0) | 184 if (sscanf(line, "rows|%u", &ctx->map->rows) != 1 || ctx->map->rows == 0) |
186 return errorf(_("null map rows")); | 185 return errorf("null map rows"); |
187 | 186 |
188 return 0; | 187 return 0; |
189 } | 188 } |
190 | 189 |
191 static int | 190 static int |
192 parse_origin(struct context *ctx, const char *line) | 191 parse_origin(struct context *ctx, const char *line) |
193 { | 192 { |
194 if (sscanf(line, "origin|%d|%d", &ctx->map->player_x, &ctx->map->player_y) != 2) | 193 if (sscanf(line, "origin|%d|%d", &ctx->map->player_x, &ctx->map->player_y) != 2) |
195 return errorf(_("invalid origin")); | 194 return errorf("invalid origin"); |
196 | 195 |
197 return 0; | 196 return 0; |
198 } | 197 } |
199 | 198 |
200 static int | 199 static int |
244 { | 243 { |
245 /* | 244 /* |
246 * Check that we have parsed every required components. | 245 * Check that we have parsed every required components. |
247 */ | 246 */ |
248 if (!map->title) | 247 if (!map->title) |
249 return errorf(_("missing title")); | 248 return errorf("missing title"); |
250 | 249 |
251 /* | 250 /* |
252 * We don't need to check width/height because parsing layers and | 251 * We don't need to check width/height because parsing layers and |
253 * tilesets already check for their presence, so only check layers. | 252 * tilesets already check for their presence, so only check layers. |
254 */ | 253 */ |
255 if (!map->layers[0].tiles) | 254 if (!map->layers[0].tiles) |
256 return errorf(_("missing background layer")); | 255 return errorf("missing background layer"); |
257 if (!map->layers[1].tiles) | 256 if (!map->layers[1].tiles) |
258 return errorf(_("missing foreground layer")); | 257 return errorf("missing foreground layer"); |
259 if (!tileset_ok(map->tileset)) | 258 if (!tileset_ok(map->tileset)) |
260 return errorf(_("missing tileset")); | 259 return errorf("missing tileset"); |
261 | 260 |
262 return 0; | 261 return 0; |
263 } | 262 } |
264 | 263 |
265 int | 264 int |