Implement an optional JSON report writer. Currently writes readable maps for Tiled.

This commit is contained in:
Enno Rehling 2015-03-29 14:03:56 +02:00
parent d802b297e9
commit 9664f2b191
10 changed files with 131 additions and 31 deletions

View File

@ -79,6 +79,7 @@ set (ERESSEA_SRC
skill.c skill.c
json.c json.c
creport.c creport.c
jsreport.c
economy.c economy.c
give.c give.c
items.c items.c

View File

@ -25,6 +25,7 @@
#include "report.h" #include "report.h"
#include "items.h" #include "items.h"
#include "creport.h" #include "creport.h"
#include "jsreport.h"
#include "names.h" #include "names.h"
#include "wormhole.h" #include "wormhole.h"
#include "spells.h" #include "spells.h"
@ -56,6 +57,7 @@ void game_init(void)
register_nr(); register_nr();
register_cr(); register_cr();
register_jsreport();
register_races(); register_races();
register_spells(); register_spells();

80
src/jsreport.c Normal file
View File

@ -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 <limits.h>
#include <stdio.h>
#include <math.h>
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);
}

24
src/jsreport.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
/*
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
+-------------------+ Stefan Reich <reich@halbling.de>
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

View File

@ -1262,7 +1262,10 @@ faction *readfaction(struct gamedata * data)
/* Kein Report eingestellt, Fehler */ /* Kein Report eingestellt, Fehler */
f->options |= n; f->options |= n;
} }
if (data->version < JSON_REPORT_VERSION) {
/* mistakes were made in the past*/
f->options &= ~want(O_JSON);
}
sfp = &f->allies; sfp = &f->allies;
for (;;) { for (;;) {
int aid = 0; int aid = 0;

View File

@ -35,7 +35,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAXTERRAINS 20 #define MAXTERRAINS 14
const char *terraindata[MAXTERRAINS] = { const char *terraindata[MAXTERRAINS] = {
"ocean", "ocean",
@ -46,19 +46,12 @@ const char *terraindata[MAXTERRAINS] = {
"mountain", "mountain",
"glacier", "glacier",
"firewall", "firewall",
NULL, /* dungeon module */
NULL, /* former grassland */
"fog", "fog",
"thickfog", "thickfog",
"volcano", "volcano",
"activevolcano", "activevolcano",
"iceberg_sleep", "iceberg_sleep",
"iceberg", "iceberg"
NULL, /* museum module */
NULL, /* museum module */
NULL, /* former magicstorm */
NULL /* museum module */
}; };
static terrain_type *registered_terrains; static terrain_type *registered_terrains;

View File

@ -16,25 +16,19 @@ extern "C" {
enum { enum {
T_OCEAN = 0, T_OCEAN = 0,
T_PLAIN = 1, T_PLAIN,
T_SWAMP = 2, T_SWAMP,
T_DESERT = 3, /* kann aus T_PLAIN entstehen */ T_DESERT,
T_HIGHLAND = 4, T_HIGHLAND,
T_MOUNTAIN = 5, T_MOUNTAIN,
T_GLACIER = 6, /* kann aus T_MOUNTAIN entstehen */ T_GLACIER,
T_FIREWALL = 7, /* Unpassierbar */ T_FIREWALL,
/* T_HELL = 8, Hölle */ T_ASTRAL,
/* T_GRASSLAND = 9, */ T_ASTRALB,
T_ASTRAL = 10, T_VOLCANO,
T_ASTRALB = 11, T_VOLCANO_SMOKING,
T_VOLCANO = 12, T_ICEBERG_SLEEP,
T_VOLCANO_SMOKING = 13, T_ICEBERG,
T_ICEBERG_SLEEP = 14,
T_ICEBERG = 15,
/* T_HALL1 = 16, */
/* T_CORRIDOR1 = 17, */
/* T_MAGICSTORM = 18, */
/* T_WALL1 = 19, */
NOTERRAIN = (terrain_t) - 1 NOTERRAIN = (terrain_t) - 1
}; };

View File

@ -167,7 +167,7 @@ enum {
O_REPORT, /* 1 */ O_REPORT, /* 1 */
O_COMPUTER, /* 2 */ O_COMPUTER, /* 2 */
O_ZUGVORLAGE, /* 4 */ O_ZUGVORLAGE, /* 4 */
O_UNUSED_3, O_JSON, /* 8 */
O_STATISTICS, /* 16 */ O_STATISTICS, /* 16 */
O_DEBUG, /* 32 */ O_DEBUG, /* 32 */
O_COMPRESS, /* 64 */ O_COMPRESS, /* 64 */

View File

@ -28,8 +28,9 @@
#define SAVEGAMEID_VERSION 343 /* instead of XMLNAME, save the game.id parameter from the config */ #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 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 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 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 */ #define STREAM_VERSION 2 /* internal encoding of binary files */

View File

@ -1,3 +1,4 @@
#pragma once
/* /*
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
@ -19,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef H_KRNL_REPORTS #ifndef H_KRNL_REPORTS
#define H_KRNL_REPORTS #define H_KRNL_REPORTS
#include <platform.h>
#include <time.h> #include <time.h>
#include <kernel/objtypes.h> #include <kernel/objtypes.h>
#include <kernel/types.h> #include <kernel/types.h>