From 9664f2b191807d20e69ba3c0206b26e939131a8c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 29 Mar 2015 14:03:56 +0200 Subject: [PATCH] Implement an optional JSON report writer. Currently writes readable maps for Tiled. --- src/CMakeLists.txt | 1 + src/eressea.c | 2 ++ src/jsreport.c | 80 ++++++++++++++++++++++++++++++++++++++++++ src/jsreport.h | 24 +++++++++++++ src/kernel/save.c | 5 ++- src/kernel/terrain.c | 11 ++---- src/kernel/terrainid.h | 32 +++++++---------- src/kernel/types.h | 2 +- src/kernel/version.h | 3 +- src/reports.h | 2 ++ 10 files changed, 131 insertions(+), 31 deletions(-) create mode 100644 src/jsreport.c create mode 100644 src/jsreport.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 336b9b7cf..e972d962f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -79,6 +79,7 @@ set (ERESSEA_SRC skill.c json.c creport.c + jsreport.c economy.c give.c items.c diff --git a/src/eressea.c b/src/eressea.c index 4bfe59daf..14cce6267 100755 --- a/src/eressea.c +++ b/src/eressea.c @@ -25,6 +25,7 @@ #include "report.h" #include "items.h" #include "creport.h" +#include "jsreport.h" #include "names.h" #include "wormhole.h" #include "spells.h" @@ -56,6 +57,7 @@ void game_init(void) register_nr(); register_cr(); + register_jsreport(); register_races(); register_spells(); diff --git a/src/jsreport.c b/src/jsreport.c new file mode 100644 index 000000000..4c9890329 --- /dev/null +++ b/src/jsreport.c @@ -0,0 +1,80 @@ +#include "reports.h" +#include "jsreport.h" +#include "kernel/region.h" +#include "kernel/terrain.h" +#include "kernel/terrainid.h" +#include "kernel/config.h" + +#include +#include +#include + +static void coor_to_tiled(int *x, int *y) { + *y = -*y; + *x = *x - (*y + 1) / 2; +} + +static void coor_from_tiled(int *x, int *y) { + *x = *x + (*y + 1) / 2; + *y = -*y; +} + +static int report_json(const char *filename, report_context * ctx, const char *charset) +{ + if (get_param_int(global.parameters, "feature.jsreport.enable", 0) != 0) { + FILE * F = fopen(filename, "w"); + if (F) { + int x, y, minx = INT_MAX, maxx = INT_MIN, miny = INT_MAX, maxy = INT_MIN; + seen_region *sr; + region *r; + /* traverse all regions */ + for (sr = NULL, r = ctx->first; sr == NULL && r != ctx->last; r = r->next) { + sr = find_seen(ctx->seen, r); + } + for (; sr != NULL; sr = sr->next) { + int tx = sr->r->x; + int ty = sr->r->y; + coor_to_tiled(&tx, &ty); + if (ty < miny) miny = ty; + else if (ty > maxy) maxy = ty; + if (tx < minx) minx = tx; + else if (tx > maxx) maxx = tx; + } + if (maxx >= minx && maxy >= miny) { + int w = maxx - minx + 1, h = maxy - miny + 1; + fputs("{ \"orientation\":\"hexagonal\",\"staggeraxis\":\"y\",", F); + fprintf(F, "\"staggerindex\":\"%s\", \"height\":%d, \"width\":%d, \"layers\":[", (miny & 1) ? "odd" : "even", h, w); + fprintf(F, "{ \"height\":%d, \"width\":%d, ", h, w); + fputs("\"visible\":true, \"opacity\":1, \"type\":\"tilelayer\", \"name\":\"terrain\", \"x\":0, \"y\":0, \"data\":[", F); + for (y = miny; y <= maxy; ++y) { + for (x = minx; x <= maxx; ++x) { + int data = 0; + int tx = x, ty = y; + coor_from_tiled(&tx, &ty); + r = findregion(tx, ty); + if (r) { + sr = find_seen(ctx->seen, r); + if (sr) { + terrain_t ter = oldterrain(r->terrain); + data = 1 + (int)ter; + } + } + fprintf(F, "%d", data); + if (x != maxx || y != maxy) fputs(", ", F); + } + } + fputs("]}], \"tilesets\": [{\"firstgid\": 1, \"image\": \"magellan.png\", \"imageheight\": 192, \"imagewidth\": 256," + "\"margin\": 0, \"name\": \"hextiles\", \"properties\": { }, \"spacing\": 0, " + "\"tileheight\" : 64, \"tilewidth\" : 64 }], \"tilewidth\": 64, \"tileheight\": 96}", F); + } + return 0; + } + return ferror(F); + } + return 0; +} + +void register_jsreport(void) +{ + register_reporttype("json", &report_json, 1 << O_JSON); +} diff --git a/src/jsreport.h b/src/jsreport.h new file mode 100644 index 000000000..fa3f7b115 --- /dev/null +++ b/src/jsreport.h @@ -0,0 +1,24 @@ +#pragma once +/* ++-------------------+ Christian Schlittchen +| | Enno Rehling +| Eressea PBEM host | Katja Zedel +| (c) 1998 - 2003 | Henning Peters +| | Ingo Wilken ++-------------------+ Stefan Reich + +This program may not be used, modified or distributed +without prior permission by the authors of Eressea. +*/ +#ifndef H_GC_JSREPORT +#define H_GC_JSREPORT +#ifdef __cplusplus +extern "C" { +#endif + + void register_jsreport(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/kernel/save.c b/src/kernel/save.c index dc7444667..9aebba95a 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1262,7 +1262,10 @@ faction *readfaction(struct gamedata * data) /* Kein Report eingestellt, Fehler */ f->options |= n; } - + if (data->version < JSON_REPORT_VERSION) { + /* mistakes were made in the past*/ + f->options &= ~want(O_JSON); + } sfp = &f->allies; for (;;) { int aid = 0; diff --git a/src/kernel/terrain.c b/src/kernel/terrain.c index f89144a6f..313811b47 100644 --- a/src/kernel/terrain.c +++ b/src/kernel/terrain.c @@ -35,7 +35,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -#define MAXTERRAINS 20 +#define MAXTERRAINS 14 const char *terraindata[MAXTERRAINS] = { "ocean", @@ -46,19 +46,12 @@ const char *terraindata[MAXTERRAINS] = { "mountain", "glacier", "firewall", - NULL, /* dungeon module */ - NULL, /* former grassland */ "fog", "thickfog", "volcano", "activevolcano", "iceberg_sleep", - "iceberg", - - NULL, /* museum module */ - NULL, /* museum module */ - NULL, /* former magicstorm */ - NULL /* museum module */ + "iceberg" }; static terrain_type *registered_terrains; diff --git a/src/kernel/terrainid.h b/src/kernel/terrainid.h index 0685a60e9..7d353d042 100644 --- a/src/kernel/terrainid.h +++ b/src/kernel/terrainid.h @@ -16,25 +16,19 @@ extern "C" { enum { T_OCEAN = 0, - T_PLAIN = 1, - T_SWAMP = 2, - T_DESERT = 3, /* kann aus T_PLAIN entstehen */ - T_HIGHLAND = 4, - T_MOUNTAIN = 5, - T_GLACIER = 6, /* kann aus T_MOUNTAIN entstehen */ - T_FIREWALL = 7, /* Unpassierbar */ - /* T_HELL = 8, Hölle */ - /* T_GRASSLAND = 9, */ - T_ASTRAL = 10, - T_ASTRALB = 11, - T_VOLCANO = 12, - T_VOLCANO_SMOKING = 13, - T_ICEBERG_SLEEP = 14, - T_ICEBERG = 15, - /* T_HALL1 = 16, */ - /* T_CORRIDOR1 = 17, */ - /* T_MAGICSTORM = 18, */ - /* T_WALL1 = 19, */ + T_PLAIN, + T_SWAMP, + T_DESERT, + T_HIGHLAND, + T_MOUNTAIN, + T_GLACIER, + T_FIREWALL, + T_ASTRAL, + T_ASTRALB, + T_VOLCANO, + T_VOLCANO_SMOKING, + T_ICEBERG_SLEEP, + T_ICEBERG, NOTERRAIN = (terrain_t) - 1 }; diff --git a/src/kernel/types.h b/src/kernel/types.h index 27a5925ca..632dad425 100644 --- a/src/kernel/types.h +++ b/src/kernel/types.h @@ -167,7 +167,7 @@ enum { O_REPORT, /* 1 */ O_COMPUTER, /* 2 */ O_ZUGVORLAGE, /* 4 */ - O_UNUSED_3, + O_JSON, /* 8 */ O_STATISTICS, /* 16 */ O_DEBUG, /* 32 */ O_COMPRESS, /* 64 */ diff --git a/src/kernel/version.h b/src/kernel/version.h index 516893437..e91b3030c 100644 --- a/src/kernel/version.h +++ b/src/kernel/version.h @@ -28,8 +28,9 @@ #define SAVEGAMEID_VERSION 343 /* instead of XMLNAME, save the game.id parameter from the config */ #define BUILDNO_VERSION 344 /* storing the build number in the save */ #define AUTO_RACENAME_VERSION 345 /* NPC units with name==NULL will automatically get their race for a name */ +#define JSON_REPORT_VERSION 346 /* bit 3 in f->options flags the json report */ #define MIN_VERSION INTPAK_VERSION /* minimal datafile we support */ -#define RELEASE_VERSION AUTO_RACENAME_VERSION /* current datafile */ +#define RELEASE_VERSION JSON_REPORT_VERSION /* current datafile */ #define STREAM_VERSION 2 /* internal encoding of binary files */ diff --git a/src/reports.h b/src/reports.h index cbca88866..9b0f468ad 100644 --- a/src/reports.h +++ b/src/reports.h @@ -1,3 +1,4 @@ +#pragma once /* Copyright (c) 1998-2015, Enno Rehling Katja Zedel #include #include #include