2016-11-11 21:41:25 +01:00
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2014-07-23 11:54:51 +02:00
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
2016-09-10 17:41:44 +02:00
|
|
|
#include <kernel/version.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "save.h"
|
|
|
|
|
|
|
|
#include "alchemy.h"
|
|
|
|
#include "alliance.h"
|
2012-06-30 20:07:28 +02:00
|
|
|
#include "ally.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "connection.h"
|
|
|
|
#include "building.h"
|
|
|
|
#include "faction.h"
|
|
|
|
#include "group.h"
|
|
|
|
#include "item.h"
|
2014-06-09 18:54:48 +02:00
|
|
|
#include "messages.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "move.h"
|
|
|
|
#include "objtypes.h"
|
|
|
|
#include "order.h"
|
|
|
|
#include "pathfinder.h"
|
|
|
|
#include "plane.h"
|
|
|
|
#include "race.h"
|
|
|
|
#include "region.h"
|
|
|
|
#include "resources.h"
|
|
|
|
#include "ship.h"
|
|
|
|
#include "skill.h"
|
|
|
|
#include "spell.h"
|
2012-05-24 09:56:54 +02:00
|
|
|
#include "spellbook.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "terrain.h"
|
2011-03-07 08:02:35 +01:00
|
|
|
#include "terrainid.h" /* only for conversion code */
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "unit.h"
|
2014-12-17 17:22:26 +01:00
|
|
|
#include "lighthouse.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* attributes includes */
|
|
|
|
#include <attributes/key.h>
|
2012-04-08 03:11:58 +02:00
|
|
|
#include <triggers/timeout.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* util includes */
|
2017-03-04 20:59:43 +01:00
|
|
|
#include <util/assert.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/attrib.h>
|
|
|
|
#include <util/base36.h>
|
|
|
|
#include <util/bsdstring.h>
|
|
|
|
#include <util/event.h>
|
|
|
|
#include <util/filereader.h>
|
2016-02-13 13:42:02 +01:00
|
|
|
#include <util/gamedata.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/goodies.h>
|
2016-02-12 22:06:31 +01:00
|
|
|
#include <util/gamedata.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/lists.h>
|
|
|
|
#include <util/log.h>
|
|
|
|
#include <util/parser.h>
|
2016-01-12 23:52:30 +01:00
|
|
|
#include <util/password.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/rand.h>
|
|
|
|
#include <util/resolve.h>
|
|
|
|
#include <util/rng.h>
|
|
|
|
#include <util/umlaut.h>
|
2016-11-11 00:30:49 +01:00
|
|
|
#include <util/unicode.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2017-01-26 17:41:21 +01:00
|
|
|
#include <selist.h>
|
2014-11-03 22:29:04 +01:00
|
|
|
#include <stream.h>
|
|
|
|
#include <filestream.h>
|
2013-12-31 10:06:28 +01:00
|
|
|
#include <storage.h>
|
|
|
|
#include <binarystore.h>
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/* libc includes */
|
|
|
|
#include <string.h>
|
2013-12-31 10:25:25 +01:00
|
|
|
#include <errno.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define xisdigit(c) (((c) >= '0' && (c) <= '9') || (c) == '-')
|
|
|
|
|
|
|
|
#define ESCAPE_FIX
|
|
|
|
#define MAXORDERS 256
|
|
|
|
#define MAXPERSISTENT 128
|
|
|
|
|
|
|
|
/* exported symbols symbols */
|
|
|
|
int firstx = 0, firsty = 0;
|
2016-11-13 19:40:38 +01:00
|
|
|
|
2017-02-18 21:15:14 +01:00
|
|
|
/* TODO: is this still important? */
|
2014-06-10 05:14:08 +02:00
|
|
|
int enc_gamedata = ENCODING_UTF8;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* local symbols */
|
2011-03-07 08:02:35 +01:00
|
|
|
static region *current_region;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
char *rns(FILE * f, char *c, size_t size)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
char *s = c;
|
|
|
|
do {
|
|
|
|
*s = (char)getc(f);
|
|
|
|
} while (*s != '"');
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
*s = (char)getc(f);
|
|
|
|
if (*s == '"')
|
|
|
|
break;
|
|
|
|
if (s < c + size)
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
return c;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static unit *unitorders(FILE * F, int enc, struct faction *f)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
int i;
|
|
|
|
unit *u;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = getid();
|
|
|
|
u = findunitg(i, NULL);
|
|
|
|
|
|
|
|
if (u && u_race(u) == get_race(RC_SPELL))
|
|
|
|
return NULL;
|
|
|
|
if (u && u->faction == f) {
|
|
|
|
order **ordp;
|
|
|
|
|
|
|
|
if (!fval(u, UFL_ORDERS)) {
|
|
|
|
/* alle wiederholbaren, langen befehle werden gesichert: */
|
|
|
|
fset(u, UFL_ORDERS);
|
|
|
|
u->old_orders = u->orders;
|
|
|
|
ordp = &u->old_orders;
|
|
|
|
while (*ordp) {
|
|
|
|
order *ord = *ordp;
|
2015-08-04 22:47:55 +02:00
|
|
|
keyword_t kwd = getkeyword(ord);
|
|
|
|
if (!is_repeated(kwd)) {
|
2014-07-23 11:54:51 +02:00
|
|
|
*ordp = ord->next;
|
|
|
|
ord->next = NULL;
|
|
|
|
free_order(ord);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ordp = &ord->next;
|
|
|
|
}
|
2011-03-08 08:44:20 +01:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
else {
|
|
|
|
free_orders(&u->orders);
|
2012-05-19 08:57:20 +02:00
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
u->orders = 0;
|
|
|
|
|
|
|
|
ordp = &u->orders;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
for (;;) {
|
|
|
|
const char *s;
|
|
|
|
/* Erst wenn wir sicher sind, dass kein Befehl
|
|
|
|
* eingegeben wurde, checken wir, ob nun eine neue
|
|
|
|
* Einheit oder ein neuer Spieler drankommt */
|
|
|
|
|
|
|
|
s = getbuf(F, enc);
|
|
|
|
if (s == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (s[0]) {
|
|
|
|
if (s[0] != '@') {
|
2016-08-29 18:31:09 +02:00
|
|
|
char token[64];
|
2014-07-23 11:54:51 +02:00
|
|
|
const char *stok = s;
|
2016-08-29 18:31:09 +02:00
|
|
|
stok = parse_token(&stok, token, sizeof(token));
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
if (stok) {
|
|
|
|
bool quit = false;
|
|
|
|
param_t param = findparam(stok, u->faction->locale);
|
|
|
|
switch (param) {
|
|
|
|
case P_UNIT:
|
|
|
|
case P_REGION:
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
case P_FACTION:
|
|
|
|
case P_NEXT:
|
|
|
|
case P_GAMENAME:
|
|
|
|
/* these terminate the orders, so we apply extra checking */
|
|
|
|
if (strlen(stok) >= 3) {
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
quit = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (quit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 19:53:09 +02:00
|
|
|
/* Nun wird der Befehl erzeut und eingehängt */
|
2014-07-23 11:54:51 +02:00
|
|
|
*ordp = parse_order(s, u->faction->locale);
|
|
|
|
if (*ordp) {
|
|
|
|
ordp = &(*ordp)->next;
|
|
|
|
}
|
2016-11-05 18:20:56 +01:00
|
|
|
else {
|
|
|
|
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s));
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return u;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static faction *factionorders(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
faction *f = NULL;
|
|
|
|
int fid = getid();
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
f = findfaction(fid);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2014-11-11 16:53:56 +01:00
|
|
|
if (f != NULL && !fval(f, FFL_NPC)) {
|
2014-12-22 14:30:04 +01:00
|
|
|
char token[128];
|
2014-12-22 16:28:17 +01:00
|
|
|
const char *pass = gettoken(token, sizeof(token));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-12-22 14:30:04 +01:00
|
|
|
if (!checkpasswd(f, (const char *)pass)) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("Invalid password for faction %s", itoa36(fid));
|
2017-01-22 12:43:48 +01:00
|
|
|
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass));
|
2014-07-23 11:54:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-02 21:37:12 +02:00
|
|
|
/* Die Partei hat sich zumindest gemeldet, so dass sie noch
|
2015-07-06 19:53:09 +02:00
|
|
|
* nicht als untätig gilt */
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2016-02-23 08:35:31 +01:00
|
|
|
/* TODO: +1 ist ein Workaround, weil cturn erst in process_orders
|
|
|
|
* incrementiert wird. */
|
2014-07-23 11:54:51 +02:00
|
|
|
f->lastorders = global.data_turn + 1;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("orders for invalid faction %s", itoa36(fid));
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
return f;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int readorders(const char *filename)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
FILE *F = NULL;
|
|
|
|
const char *b;
|
|
|
|
int nfactions = 0;
|
|
|
|
struct faction *f = NULL;
|
|
|
|
|
2016-02-01 12:27:13 +01:00
|
|
|
F = fopen(filename, "r");
|
2014-07-23 11:54:51 +02:00
|
|
|
if (!F) {
|
|
|
|
perror(filename);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-08-11 12:02:16 +02:00
|
|
|
log_info("reading orders from %s", filename);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
/* TODO: recognize UTF8 BOM */
|
|
|
|
b = getbuf(F, enc_gamedata);
|
|
|
|
|
|
|
|
/* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten
|
|
|
|
* Partei */
|
|
|
|
|
|
|
|
while (b) {
|
2014-12-22 14:21:24 +01:00
|
|
|
char token[128];
|
2014-07-23 11:54:51 +02:00
|
|
|
const struct locale *lang = f ? f->locale : default_locale;
|
2014-12-18 17:47:49 +01:00
|
|
|
param_t p;
|
|
|
|
const char *s;
|
2014-12-22 14:21:24 +01:00
|
|
|
init_tokens_str(b);
|
2014-12-22 16:28:17 +01:00
|
|
|
s = gettoken(token, sizeof(token));
|
2017-01-23 10:47:49 +01:00
|
|
|
p = findparam_block(s, lang, true);
|
2014-12-18 17:47:49 +01:00
|
|
|
switch (p) {
|
2014-07-23 11:54:51 +02:00
|
|
|
case P_GAMENAME:
|
|
|
|
case P_FACTION:
|
|
|
|
f = factionorders();
|
|
|
|
if (f) {
|
|
|
|
++nfactions;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = getbuf(F, enc_gamedata);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* in factionorders wird nur eine zeile gelesen:
|
|
|
|
* diejenige mit dem passwort. Die befehle der units
|
|
|
|
* werden geloescht, und die Partei wird als aktiv
|
|
|
|
* vermerkt. */
|
|
|
|
|
|
|
|
case P_UNIT:
|
2016-11-25 10:48:38 +01:00
|
|
|
if (!f || !unitorders(F, enc_gamedata, f)) {
|
2014-07-23 11:54:51 +02:00
|
|
|
do {
|
2014-12-18 17:47:49 +01:00
|
|
|
b = getbuf(F, enc_gamedata);
|
|
|
|
if (!b) {
|
|
|
|
break;
|
|
|
|
}
|
2014-12-22 14:21:24 +01:00
|
|
|
init_tokens_str(b);
|
2014-12-28 22:07:53 +01:00
|
|
|
s = gettoken(token, sizeof(token));
|
|
|
|
p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
|
2014-07-23 11:54:51 +02:00
|
|
|
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
|
|
|
|
&& p != P_GAMENAME);
|
2017-01-23 09:22:29 +01:00
|
|
|
}
|
2016-11-25 10:48:38 +01:00
|
|
|
break;
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
/* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue
|
|
|
|
* Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut
|
|
|
|
* durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf
|
2015-07-06 19:53:09 +02:00
|
|
|
* auf alle Fälle nicht überschreiben! Bei allen anderen Einträgen hier
|
2015-08-02 21:37:12 +02:00
|
|
|
* muss buf erneut gefüllt werden, da die betreffende Information in nur
|
|
|
|
* einer Zeile steht, und nun die nächste gelesen werden muss. */
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
case P_NEXT:
|
|
|
|
f = NULL;
|
|
|
|
b = getbuf(F, enc_gamedata);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
b = getbuf(F, enc_gamedata);
|
2011-03-08 08:44:20 +01:00
|
|
|
break;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(F);
|
2014-08-11 12:02:16 +02:00
|
|
|
log_info("done reading orders for %d factions", nfactions);
|
2014-07-23 11:54:51 +02:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* #define INNER_WORLD */
|
2015-07-06 19:53:09 +02:00
|
|
|
/* fürs debuggen nur den inneren Teil der Welt laden */
|
2010-08-08 10:06:34 +02:00
|
|
|
/* -9;-27;-1;-19;Sumpfloch */
|
2011-03-07 08:02:35 +01:00
|
|
|
int inner_world(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
static int xy[2] = { 18, -45 };
|
|
|
|
static int size[2] = { 27, 27 };
|
|
|
|
|
|
|
|
if (r->x >= xy[0] && r->x < xy[0] + size[0] && r->y >= xy[1]
|
|
|
|
&& r->y < xy[1] + size[1])
|
|
|
|
return 2;
|
|
|
|
if (r->x >= xy[0] - 9 && r->x < xy[0] + size[0] + 9 && r->y >= xy[1] - 9
|
|
|
|
&& r->y < xy[1] + size[1] + 9)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int maxregions = -1;
|
|
|
|
int loadplane = 0;
|
|
|
|
|
|
|
|
enum {
|
2014-07-23 11:54:51 +02:00
|
|
|
U_MAN,
|
|
|
|
U_UNDEAD,
|
|
|
|
U_ILLUSION,
|
|
|
|
U_FIREDRAGON,
|
|
|
|
U_DRAGON,
|
|
|
|
U_WYRM,
|
|
|
|
U_SPELL,
|
|
|
|
U_TAVERNE,
|
|
|
|
U_MONSTER,
|
|
|
|
U_BIRTHDAYDRAGON,
|
|
|
|
U_TREEMAN,
|
|
|
|
MAXTYPES
|
2010-08-08 10:06:34 +02:00
|
|
|
};
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
race_t typus2race(unsigned char typus)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
if (typus > 0 && typus <= 11)
|
|
|
|
return (race_t)(typus - 1);
|
|
|
|
return NORACE;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-02-13 15:44:15 +01:00
|
|
|
static void read_alliances(struct gamedata *data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2016-02-13 15:44:15 +01:00
|
|
|
storage *store = data->store;
|
2014-07-23 11:54:51 +02:00
|
|
|
char pbuf[8];
|
|
|
|
int id, terminator = 0;
|
2016-02-13 15:44:15 +01:00
|
|
|
if (data->version < ALLIANCELEADER_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
terminator = atoi36("end");
|
|
|
|
READ_STR(store, pbuf, sizeof(pbuf));
|
|
|
|
id = atoi36(pbuf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
READ_INT(store, &id);
|
|
|
|
}
|
|
|
|
while (id != terminator) {
|
|
|
|
char aname[128];
|
|
|
|
alliance *al;
|
|
|
|
READ_STR(store, aname, sizeof(aname));
|
2016-08-17 21:22:50 +02:00
|
|
|
al = new_alliance(id, aname);
|
2016-02-13 15:44:15 +01:00
|
|
|
if (data->version >= OWNER_2_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(store, &al->flags);
|
|
|
|
}
|
2016-02-13 15:44:15 +01:00
|
|
|
if (data->version >= ALLIANCELEADER_VERSION) {
|
2016-02-13 20:38:32 +01:00
|
|
|
read_reference(&al->_leader, data, read_faction_reference,
|
2014-07-23 11:54:51 +02:00
|
|
|
resolve_faction);
|
|
|
|
READ_INT(store, &id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
READ_STR(store, pbuf, sizeof(pbuf));
|
|
|
|
id = atoi36(pbuf);
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 18:59:30 +02:00
|
|
|
void read_planes(gamedata *data) {
|
|
|
|
struct storage *store = data->store;
|
|
|
|
int nread;
|
|
|
|
char name[32];
|
|
|
|
|
|
|
|
/* Planes */
|
|
|
|
planes = NULL;
|
|
|
|
READ_INT(store, &nread);
|
|
|
|
while (--nread >= 0) {
|
|
|
|
int id;
|
|
|
|
variant fno;
|
|
|
|
plane *pl;
|
|
|
|
|
|
|
|
READ_INT(store, &id);
|
|
|
|
pl = getplanebyid(id);
|
|
|
|
|
|
|
|
if (pl == NULL) {
|
|
|
|
pl = calloc(1, sizeof(plane));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_warning("the plane with id=%d already exists.", id);
|
|
|
|
}
|
|
|
|
pl->id = id;
|
|
|
|
READ_STR(store, name, sizeof(name));
|
2017-01-10 16:31:05 +01:00
|
|
|
pl->name = strdup(name);
|
2016-08-18 18:59:30 +02:00
|
|
|
READ_INT(store, &pl->minx);
|
|
|
|
READ_INT(store, &pl->maxx);
|
|
|
|
READ_INT(store, &pl->miny);
|
|
|
|
READ_INT(store, &pl->maxy);
|
|
|
|
READ_INT(store, &pl->flags);
|
|
|
|
|
|
|
|
/* read watchers */
|
|
|
|
if (data->version < FIX_WATCHERS_VERSION) {
|
|
|
|
char rname[64];
|
|
|
|
/* before this version, watcher storage was pretty broken. we are incompatible and don't read them */
|
|
|
|
for (;;) {
|
|
|
|
READ_TOK(store, rname, sizeof(rname));
|
|
|
|
if (strcmp(rname, "end") == 0) {
|
|
|
|
break; /* this is most likely the end of the list */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error(
|
|
|
|
("This datafile contains watchers, but we are unable to read them."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* WATCHERS - eliminated in February 2016, ca. turn 966 */
|
|
|
|
if (data->version < NOWATCH_VERSION) {
|
|
|
|
fno = read_faction_reference(data);
|
|
|
|
while (fno.i) {
|
|
|
|
fno = read_faction_reference(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_attribs(data, &pl->attribs, pl);
|
2017-02-18 21:15:14 +01:00
|
|
|
if (pl->id != 1094969858) { /* Regatta */
|
2016-08-18 18:59:30 +02:00
|
|
|
addlist(&planes, pl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_planes(storage *store) {
|
|
|
|
plane *pl;
|
|
|
|
/* Write planes */
|
|
|
|
WRITE_INT(store, listlen(planes));
|
|
|
|
for (pl = planes; pl; pl = pl->next) {
|
|
|
|
WRITE_INT(store, pl->id);
|
|
|
|
WRITE_STR(store, pl->name);
|
|
|
|
WRITE_INT(store, pl->minx);
|
|
|
|
WRITE_INT(store, pl->maxx);
|
|
|
|
WRITE_INT(store, pl->miny);
|
|
|
|
WRITE_INT(store, pl->maxy);
|
|
|
|
WRITE_INT(store, pl->flags);
|
|
|
|
#if RELEASE_VERSION < NOWATCH_VERSION
|
|
|
|
write_faction_reference(NULL, store); /* mark the end of pl->watchers (gone since T966) */
|
|
|
|
#endif
|
|
|
|
a_write(store, pl->attribs, pl);
|
|
|
|
WRITE_SECTION(store);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
void write_alliances(struct gamedata *data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
alliance *al = alliances;
|
|
|
|
while (al) {
|
|
|
|
if (al->_leader) {
|
|
|
|
WRITE_INT(data->store, al->id);
|
|
|
|
WRITE_STR(data->store, al->name);
|
|
|
|
WRITE_INT(data->store, (int)al->flags);
|
|
|
|
write_faction_reference(al->_leader, data->store);
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
}
|
|
|
|
al = al->next;
|
|
|
|
}
|
|
|
|
WRITE_INT(data->store, 0);
|
|
|
|
WRITE_SECTION(data->store);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int resolve_owner(variant id, void *address)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
region_owner *owner = (region_owner *)address;
|
|
|
|
int result = 0;
|
|
|
|
faction *f = NULL;
|
|
|
|
if (id.i != 0) {
|
|
|
|
f = findfaction(id.i);
|
|
|
|
if (f == NULL) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("region has an invalid owner (%s)", itoa36(id.i));
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
owner->owner = f;
|
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
static void read_owner(struct gamedata *data, region_owner ** powner)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
int since_turn;
|
|
|
|
|
|
|
|
READ_INT(data->store, &since_turn);
|
|
|
|
if (since_turn >= 0) {
|
|
|
|
region_owner *owner = malloc(sizeof(region_owner));
|
|
|
|
owner->since_turn = since_turn;
|
|
|
|
READ_INT(data->store, &owner->morale_turn);
|
|
|
|
if (data->version >= MOURNING_VERSION) {
|
|
|
|
READ_INT(data->store, &owner->flags);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
owner->flags = 0;
|
|
|
|
}
|
2015-11-12 16:09:26 +01:00
|
|
|
if (data->version >= OWNER_3_VERSION) {
|
|
|
|
int id;
|
|
|
|
READ_INT(data->store, &id);
|
|
|
|
owner->last_owner = id ? findfaction(id) : NULL;
|
2016-02-23 08:35:31 +01:00
|
|
|
}
|
|
|
|
else if (data->version >= OWNER_2_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
int id;
|
2015-11-12 16:09:26 +01:00
|
|
|
alliance *a;
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(data->store, &id);
|
2015-11-12 16:09:26 +01:00
|
|
|
a = id ? findalliance(id) : NULL;
|
|
|
|
/* don't know which faction, take the leader */
|
2016-02-23 08:35:31 +01:00
|
|
|
owner->last_owner = a ? a->_leader : NULL;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-11-12 16:09:26 +01:00
|
|
|
owner->last_owner = NULL;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2016-02-13 20:38:32 +01:00
|
|
|
read_reference(owner, data, &read_faction_reference, &resolve_owner);
|
2014-07-23 11:54:51 +02:00
|
|
|
*powner = owner;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*powner = 0;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
static void write_owner(struct gamedata *data, region_owner * owner)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
if (owner) {
|
2016-02-16 07:30:26 +01:00
|
|
|
faction *f;
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_INT(data->store, owner->since_turn);
|
2016-09-08 19:48:36 +02:00
|
|
|
if (owner->since_turn >= 0) {
|
|
|
|
WRITE_INT(data->store, owner->morale_turn);
|
|
|
|
WRITE_INT(data->store, owner->flags);
|
|
|
|
f = owner->last_owner;
|
|
|
|
write_faction_reference((f && f->_alive) ? f : NULL, data->store);
|
|
|
|
f = owner->owner;
|
|
|
|
write_faction_reference((f && f->_alive) ? f : NULL, data->store);
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRITE_INT(data->store, -1);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int current_turn(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
char zText[MAX_PATH];
|
|
|
|
int cturn = 0;
|
|
|
|
FILE *F;
|
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
join_path(basepath(), "turn", zText, sizeof(zText));
|
2014-07-23 11:54:51 +02:00
|
|
|
F = fopen(zText, "r");
|
|
|
|
if (!F) {
|
|
|
|
perror(zText);
|
|
|
|
}
|
|
|
|
else {
|
2015-11-04 11:45:13 +01:00
|
|
|
int c = fscanf(F, "%d\n", &cturn);
|
2014-07-23 11:54:51 +02:00
|
|
|
fclose(F);
|
2015-11-04 11:45:13 +01:00
|
|
|
if (c != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
return cturn;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-31 10:06:28 +01:00
|
|
|
writeorder(struct gamedata *data, const struct order *ord,
|
2016-02-23 08:35:31 +01:00
|
|
|
const struct locale *lang)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
char obuf[1024];
|
|
|
|
write_order(ord, obuf, sizeof(obuf));
|
|
|
|
if (obuf[0])
|
|
|
|
WRITE_STR(data->store, obuf);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
unit *read_unit(struct gamedata *data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-05-25 05:25:06 +02:00
|
|
|
unit *u;
|
2016-08-28 21:44:09 +02:00
|
|
|
const race *rc;
|
2014-05-25 05:25:06 +02:00
|
|
|
int number, n, p;
|
|
|
|
order **orderp;
|
|
|
|
char obuf[DISPLAYSIZE];
|
|
|
|
faction *f;
|
|
|
|
char rname[32];
|
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
if (n <= 0) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("data contains invalid unit %d.", n);
|
2014-07-23 11:54:51 +02:00
|
|
|
assert(n > 0);
|
2014-05-25 05:25:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
u = findunit(n);
|
|
|
|
if (u) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("reading unit %s that already exists.", unitname(u));
|
2014-05-25 05:25:06 +02:00
|
|
|
while (u->attribs) {
|
|
|
|
a_remove(&u->attribs, u->attribs);
|
|
|
|
}
|
|
|
|
while (u->items) {
|
|
|
|
i_free(i_remove(&u->items, u->items));
|
|
|
|
}
|
|
|
|
free(u->skills);
|
|
|
|
u->skills = 0;
|
|
|
|
u->skill_size = 0;
|
|
|
|
u_setfaction(u, NULL);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-05-25 05:25:06 +02:00
|
|
|
u = calloc(sizeof(unit), 1);
|
2017-03-04 20:59:43 +01:00
|
|
|
assert_alloc(u);
|
2014-05-25 05:25:06 +02:00
|
|
|
u->no = n;
|
|
|
|
uhash(u);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-05-25 05:25:06 +02:00
|
|
|
READ_INT(data->store, &n);
|
|
|
|
f = findfaction(n);
|
|
|
|
if (f != u->faction) {
|
|
|
|
u_setfaction(u, f);
|
|
|
|
}
|
2017-03-11 19:36:26 +01:00
|
|
|
if (!u->faction) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("unit %s has faction == NULL", itoa36(u->no));
|
2014-05-25 05:25:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
READ_STR(data->store, obuf, sizeof(obuf));
|
2016-11-11 00:30:49 +01:00
|
|
|
if (unicode_utf8_trim(obuf)!=0) {
|
2016-11-11 11:09:57 +01:00
|
|
|
log_warning("trim unit %s name to '%s'", itoa36(u->no), obuf);
|
2016-11-11 21:46:56 +01:00
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
u->_name = obuf[0] ? strdup(obuf) : 0;
|
2014-07-23 11:54:51 +02:00
|
|
|
if (lomem) {
|
|
|
|
READ_STR(data->store, NULL, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
READ_STR(data->store, obuf, sizeof(obuf));
|
2016-11-11 00:30:49 +01:00
|
|
|
if (unicode_utf8_trim(obuf)!=0) {
|
2016-11-11 11:09:57 +01:00
|
|
|
log_warning("trim unit %s info to '%s'", itoa36(u->no), obuf);
|
2016-11-11 21:46:56 +01:00
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
u->display = obuf[0] ? strdup(obuf) : 0;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
READ_INT(data->store, &number);
|
2014-05-25 05:25:06 +02:00
|
|
|
set_number(u, number);
|
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(data->store, &n);
|
|
|
|
u->age = (short)n;
|
|
|
|
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_TOK(data->store, rname, sizeof(rname));
|
2016-08-28 21:44:09 +02:00
|
|
|
rc = rc_find(rname);
|
|
|
|
assert(rc);
|
|
|
|
u_setrace(u, rc);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_TOK(data->store, rname, sizeof(rname));
|
2014-07-23 11:54:51 +02:00
|
|
|
if (rname[0] && skill_enabled(SK_STEALTH))
|
|
|
|
u->irace = rc_find(rname);
|
|
|
|
else
|
|
|
|
u->irace = NULL;
|
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
if (n > 0) {
|
|
|
|
building * b = findbuilding(n);
|
|
|
|
if (b) {
|
|
|
|
u_set_building(u, b);
|
|
|
|
if (fval(u, UFL_OWNER)) {
|
|
|
|
building_set_owner(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("read_unit: unit in unkown building '%s'", itoa36(n));
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
if (n > 0) {
|
|
|
|
ship * sh = findship(n);
|
|
|
|
if (sh) {
|
|
|
|
u_set_ship(u, sh);
|
|
|
|
if (fval(u, UFL_OWNER)) {
|
|
|
|
ship_set_owner(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("read_unit: unit in unkown ship '%s'", itoa36(n));
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
READ_INT(data->store, &n);
|
2014-07-23 11:54:51 +02:00
|
|
|
setstatus(u, n);
|
|
|
|
READ_INT(data->store, &u->flags);
|
|
|
|
u->flags &= UFL_SAVEMASK;
|
2015-11-09 13:36:52 +01:00
|
|
|
if ((u->flags & UFL_ANON_FACTION) && !rule_stealth_anon()) {
|
2014-07-23 11:54:51 +02:00
|
|
|
/* if this rule is broken, then fix broken units */
|
|
|
|
u->flags -= UFL_ANON_FACTION;
|
2016-05-29 10:58:49 +02:00
|
|
|
log_warning("%s was anonymous.", unitname(u));
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
/* Persistente Befehle einlesen */
|
|
|
|
free_orders(&u->orders);
|
|
|
|
READ_STR(data->store, obuf, sizeof(obuf));
|
|
|
|
p = n = 0;
|
|
|
|
orderp = &u->orders;
|
|
|
|
while (obuf[0]) {
|
|
|
|
if (!lomem) {
|
|
|
|
order *ord = parse_order(obuf, u->faction->locale);
|
|
|
|
if (ord != NULL) {
|
|
|
|
if (++n < MAXORDERS) {
|
|
|
|
if (!is_persistent(ord) || ++p < MAXPERSISTENT) {
|
|
|
|
*orderp = ord;
|
|
|
|
orderp = &ord->next;
|
|
|
|
ord = NULL;
|
|
|
|
}
|
|
|
|
else if (p == MAXPERSISTENT) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_info("%s had %d or more persistent orders", unitname(u), MAXPERSISTENT);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (n == MAXORDERS) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_info("%s had %d or more orders", unitname(u), MAXORDERS);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
if (ord != NULL)
|
|
|
|
free_order(ord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
READ_STR(data->store, obuf, sizeof(obuf));
|
|
|
|
}
|
|
|
|
set_order(&u->thisorder, NULL);
|
|
|
|
|
|
|
|
assert(u_race(u));
|
|
|
|
for (;;) {
|
|
|
|
int n, level, weeks;
|
|
|
|
skill_t sk;
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
sk = (skill_t)n;
|
|
|
|
if (sk == NOSKILL) break;
|
|
|
|
READ_INT(data->store, &level);
|
|
|
|
READ_INT(data->store, &weeks);
|
|
|
|
if (level) {
|
|
|
|
skill *sv = add_skill(u, sk);
|
|
|
|
sv->level = sv->old = (unsigned char)level;
|
|
|
|
sv->weeks = (unsigned char)weeks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_items(data->store, &u->items);
|
|
|
|
READ_INT(data->store, &u->hp);
|
|
|
|
if (u->hp < u->number) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("Einheit %s hat %u Personen, und %u Trefferpunkte", itoa36(u->no), u->number, u->hp);
|
2014-07-23 11:54:51 +02:00
|
|
|
u->hp = u->number;
|
|
|
|
}
|
2016-02-13 13:42:02 +01:00
|
|
|
read_attribs(data, &u->attribs, u);
|
2014-07-23 11:54:51 +02:00
|
|
|
return u;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
void write_unit(struct gamedata *data, const unit * u)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
order *ord;
|
|
|
|
int i, p = 0;
|
|
|
|
unsigned int flags = u->flags & UFL_SAVEMASK;
|
|
|
|
const race *irace = u_irace(u);
|
|
|
|
|
|
|
|
write_unit_reference(u, data->store);
|
2016-02-17 13:55:48 +01:00
|
|
|
assert(u->faction->_alive);
|
2014-07-23 11:54:51 +02:00
|
|
|
write_faction_reference(u->faction, data->store);
|
2015-02-11 07:55:35 +01:00
|
|
|
WRITE_STR(data->store, u->_name);
|
|
|
|
WRITE_STR(data->store, u->display ? u->display : "");
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_INT(data->store, u->number);
|
|
|
|
WRITE_INT(data->store, u->age);
|
2014-08-24 21:49:55 +02:00
|
|
|
WRITE_TOK(data->store, u_race(u)->_name);
|
|
|
|
WRITE_TOK(data->store, (irace && irace != u_race(u)) ? irace->_name : "");
|
2014-07-23 11:54:51 +02:00
|
|
|
write_building_reference(u->building, data->store);
|
|
|
|
write_ship_reference(u->ship, data->store);
|
|
|
|
WRITE_INT(data->store, u->status);
|
|
|
|
if (u->building && u == building_owner(u->building)) flags |= UFL_OWNER;
|
|
|
|
if (u->ship && u == ship_owner(u->ship)) flags |= UFL_OWNER;
|
|
|
|
WRITE_INT(data->store, flags);
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
for (ord = u->old_orders; ord; ord = ord->next) {
|
|
|
|
if (++p < MAXPERSISTENT) {
|
|
|
|
writeorder(data, ord, u->faction->locale);
|
|
|
|
}
|
|
|
|
else {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_info("%s had %d or more persistent orders", unitname(u), MAXPERSISTENT);
|
2014-07-23 11:54:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (ord = u->orders; ord; ord = ord->next) {
|
2015-08-04 22:47:55 +02:00
|
|
|
keyword_t kwd = getkeyword(ord);
|
|
|
|
if (u->old_orders && is_repeated(kwd))
|
2014-07-23 11:54:51 +02:00
|
|
|
continue; /* has new defaults */
|
|
|
|
if (is_persistent(ord)) {
|
|
|
|
if (++p < MAXPERSISTENT) {
|
|
|
|
writeorder(data, ord, u->faction->locale);
|
|
|
|
}
|
|
|
|
else {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_info("%s had %d or more persistent orders", unitname(u), MAXPERSISTENT);
|
2014-07-23 11:54:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* write an empty string to terminate the list */
|
|
|
|
WRITE_STR(data->store, "");
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
|
|
|
|
assert(u_race(u));
|
|
|
|
|
|
|
|
for (i = 0; i != u->skill_size; ++i) {
|
|
|
|
skill *sv = u->skills + i;
|
|
|
|
assert(sv->weeks <= sv->level * 2 + 1);
|
|
|
|
if (sv->level > 0) {
|
|
|
|
WRITE_INT(data->store, sv->id);
|
|
|
|
WRITE_INT(data->store, sv->level);
|
|
|
|
WRITE_INT(data->store, sv->weeks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WRITE_INT(data->store, -1);
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
write_items(data->store, u->items);
|
|
|
|
WRITE_SECTION(data->store);
|
2015-11-09 16:39:30 +01:00
|
|
|
if (u->hp == 0 && u_race(u)!= get_race(RC_SPELL)) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("unit %s has 0 hitpoints, adjusting.", itoa36(u->no));
|
2014-07-23 11:54:51 +02:00
|
|
|
((unit *)u)->hp = u->number;
|
|
|
|
}
|
|
|
|
WRITE_INT(data->store, u->hp);
|
|
|
|
WRITE_SECTION(data->store);
|
2016-02-09 00:28:23 +01:00
|
|
|
write_attribs(data->store, u->attribs, u);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_SECTION(data->store);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
static region *readregion(struct gamedata *data, int x, int y)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
region *r = findregion(x, y);
|
|
|
|
const terrain_type *terrain;
|
2014-12-12 22:52:23 +01:00
|
|
|
char name[NAMESIZE];
|
2014-07-23 11:54:51 +02:00
|
|
|
int uid = 0;
|
|
|
|
int n;
|
|
|
|
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_INT(data->store, &uid);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
if (r == NULL) {
|
|
|
|
plane *pl = findplane(x, y);
|
|
|
|
r = new_region(x, y, pl, uid);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(uid == 0 || r->uid == uid);
|
|
|
|
current_region = r;
|
|
|
|
while (r->attribs)
|
|
|
|
a_remove(&r->attribs, r->attribs);
|
|
|
|
if (r->land) {
|
2015-10-29 11:17:31 +01:00
|
|
|
free_land(r->land);
|
|
|
|
r->land = 0;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
while (r->resources) {
|
|
|
|
rawmaterial *rm = r->resources;
|
|
|
|
r->resources = rm->next;
|
|
|
|
free(rm);
|
|
|
|
}
|
|
|
|
r->land = 0;
|
|
|
|
}
|
|
|
|
if (lomem) {
|
|
|
|
READ_STR(data->store, NULL, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char info[DISPLAYSIZE];
|
|
|
|
READ_STR(data->store, info, sizeof(info));
|
2016-11-11 00:30:49 +01:00
|
|
|
if (unicode_utf8_trim(info)!=0) {
|
2016-11-11 11:09:57 +01:00
|
|
|
log_warning("trim region %d info to '%s'", uid, info);
|
2016-11-11 00:30:49 +01:00
|
|
|
};
|
2014-07-23 11:54:51 +02:00
|
|
|
region_setinfo(r, info);
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
terrain = get_terrain(name);
|
|
|
|
if (terrain == NULL) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("Unknown terrain '%s'", name);
|
2014-12-12 22:52:23 +01:00
|
|
|
assert(!"unknown terrain");
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
r->terrain = terrain;
|
|
|
|
READ_INT(data->store, &r->flags);
|
2013-12-31 10:06:28 +01:00
|
|
|
READ_INT(data->store, &n);
|
2014-07-23 11:54:51 +02:00
|
|
|
r->age = (unsigned short)n;
|
|
|
|
|
|
|
|
if (fval(r->terrain, LAND_REGION)) {
|
|
|
|
r->land = calloc(1, sizeof(land_region));
|
|
|
|
READ_STR(data->store, name, sizeof(name));
|
2016-11-11 00:30:49 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
2016-11-11 11:09:57 +01:00
|
|
|
log_warning("trim region %d name to '%s'", uid, name);
|
2016-11-11 00:30:49 +01:00
|
|
|
};
|
2017-01-10 16:31:05 +01:00
|
|
|
r->land->name = strdup(name);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
if (r->land) {
|
|
|
|
int i;
|
|
|
|
rawmaterial **pres = &r->resources;
|
|
|
|
|
|
|
|
READ_INT(data->store, &i);
|
|
|
|
if (i < 0) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("number of trees in %s is %d.", regionname(r, NULL), i);
|
2014-07-23 11:54:51 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
rsettrees(r, 0, i);
|
|
|
|
READ_INT(data->store, &i);
|
|
|
|
if (i < 0) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("number of young trees in %s is %d.", regionname(r, NULL), i);
|
2014-07-23 11:54:51 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
rsettrees(r, 1, i);
|
|
|
|
READ_INT(data->store, &i);
|
|
|
|
if (i < 0) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("number of seeds in %s is %d.", regionname(r, NULL), i);
|
2014-07-23 11:54:51 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
rsettrees(r, 2, i);
|
|
|
|
|
|
|
|
READ_INT(data->store, &i);
|
|
|
|
rsethorses(r, i);
|
|
|
|
assert(*pres == NULL);
|
|
|
|
for (;;) {
|
|
|
|
rawmaterial *res;
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
if (strcmp(name, "end") == 0)
|
2014-07-23 11:54:51 +02:00
|
|
|
break;
|
|
|
|
res = malloc(sizeof(rawmaterial));
|
2014-12-12 22:52:23 +01:00
|
|
|
res->type = rmt_find(name);
|
2014-07-23 11:54:51 +02:00
|
|
|
if (res->type == NULL) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("invalid resourcetype %s in data.", name);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
assert(res->type != NULL);
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
res->level = n;
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
res->amount = n;
|
|
|
|
res->flags = 0;
|
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
res->startlevel = n;
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
res->base = n;
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
res->divisor = n;
|
|
|
|
|
|
|
|
*pres = res;
|
|
|
|
pres = &res->next;
|
|
|
|
}
|
|
|
|
*pres = NULL;
|
|
|
|
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
if (strcmp(name, "noherb") != 0) {
|
|
|
|
const resource_type *rtype = rt_find(name);
|
2014-07-23 11:54:51 +02:00
|
|
|
assert(rtype && rtype->itype && fval(rtype->itype, ITF_HERB));
|
|
|
|
rsetherbtype(r, rtype->itype);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rsetherbtype(r, NULL);
|
|
|
|
}
|
|
|
|
READ_INT(data->store, &n);
|
2016-08-18 19:04:48 +02:00
|
|
|
rsetherbs(r, n);
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(data->store, &n);
|
2016-01-24 14:31:24 +01:00
|
|
|
if (n < 0) {
|
|
|
|
/* bug 2182 */
|
|
|
|
log_error("data has negative peasants: %d in %s", n, regionname(r, 0));
|
|
|
|
rsetpeasants(r, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rsetpeasants(r, n);
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(data->store, &n);
|
|
|
|
rsetmoney(r, n);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
assert(r->terrain != NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
if (r->land) {
|
|
|
|
int n;
|
|
|
|
for (;;) {
|
|
|
|
const struct resource_type *rtype;
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
if (!strcmp(name, "end"))
|
2014-06-25 07:44:05 +02:00
|
|
|
break;
|
2014-12-12 22:52:23 +01:00
|
|
|
rtype = rt_find(name);
|
2014-06-25 07:44:05 +02:00
|
|
|
assert(rtype && rtype->ltype);
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
r_setdemand(r, rtype->ltype, n);
|
|
|
|
}
|
2016-10-24 13:54:53 +02:00
|
|
|
if (!r->land->demands) {
|
|
|
|
fix_demand(r);
|
|
|
|
}
|
2014-12-12 22:52:23 +01:00
|
|
|
read_items(data->store, &r->land->items);
|
2014-06-25 07:44:05 +02:00
|
|
|
if (data->version >= REGIONOWNER_VERSION) {
|
|
|
|
READ_INT(data->store, &n);
|
2017-01-10 16:31:05 +01:00
|
|
|
region_set_morale(r, MAX(0, (short)n), -1);
|
2014-06-25 07:44:05 +02:00
|
|
|
read_owner(data, &r->land->ownership);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2016-02-13 13:42:02 +01:00
|
|
|
read_attribs(data, &r->attribs, r);
|
2014-06-25 07:44:05 +02:00
|
|
|
return r;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 01:03:07 +01:00
|
|
|
region *read_region(gamedata *data)
|
|
|
|
{
|
|
|
|
storage *store = data->store;
|
|
|
|
region *r;
|
|
|
|
int x, y;
|
|
|
|
READ_INT(store, &x);
|
|
|
|
READ_INT(store, &y);
|
|
|
|
r = readregion(data, x, y);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
void writeregion(struct gamedata *data, const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-07-07 00:49:12 +02:00
|
|
|
assert(r);
|
|
|
|
assert(data);
|
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_INT(data->store, r->uid);
|
|
|
|
WRITE_STR(data->store, region_getinfo(r));
|
|
|
|
WRITE_TOK(data->store, r->terrain->_name);
|
|
|
|
WRITE_INT(data->store, r->flags & RF_SAVEMASK);
|
|
|
|
WRITE_INT(data->store, r->age);
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
if (fval(r->terrain, LAND_REGION)) {
|
|
|
|
const item_type *rht;
|
|
|
|
struct demand *demand;
|
|
|
|
rawmaterial *res = r->resources;
|
2016-02-23 08:35:31 +01:00
|
|
|
|
2015-07-07 00:49:12 +02:00
|
|
|
assert(r->land);
|
|
|
|
WRITE_STR(data->store, (const char *)r->land->name);
|
2014-07-23 11:54:51 +02:00
|
|
|
assert(rtrees(r, 0) >= 0);
|
|
|
|
assert(rtrees(r, 1) >= 0);
|
|
|
|
assert(rtrees(r, 2) >= 0);
|
|
|
|
WRITE_INT(data->store, rtrees(r, 0));
|
|
|
|
WRITE_INT(data->store, rtrees(r, 1));
|
|
|
|
WRITE_INT(data->store, rtrees(r, 2));
|
|
|
|
WRITE_INT(data->store, rhorses(r));
|
|
|
|
|
|
|
|
while (res) {
|
2017-02-23 07:53:28 +01:00
|
|
|
WRITE_TOK(data->store, res->type->rtype->_name);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_INT(data->store, res->level);
|
|
|
|
WRITE_INT(data->store, res->amount);
|
|
|
|
WRITE_INT(data->store, res->startlevel);
|
|
|
|
WRITE_INT(data->store, res->base);
|
|
|
|
WRITE_INT(data->store, res->divisor);
|
|
|
|
res = res->next;
|
|
|
|
}
|
|
|
|
WRITE_TOK(data->store, "end");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
rht = rherbtype(r);
|
|
|
|
if (rht) {
|
|
|
|
WRITE_TOK(data->store, resourcename(rht->rtype, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRITE_TOK(data->store, "noherb");
|
|
|
|
}
|
|
|
|
WRITE_INT(data->store, rherbs(r));
|
|
|
|
WRITE_INT(data->store, rpeasants(r));
|
|
|
|
WRITE_INT(data->store, rmoney(r));
|
2015-07-07 00:49:12 +02:00
|
|
|
for (demand = r->land->demands; demand; demand = demand->next) {
|
|
|
|
WRITE_TOK(data->store, resourcename(demand->type->itype->rtype, 0));
|
|
|
|
WRITE_INT(data->store, demand->value);
|
2014-12-25 22:38:01 +01:00
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_TOK(data->store, "end");
|
|
|
|
write_items(data->store, r->land->items);
|
|
|
|
WRITE_SECTION(data->store);
|
2010-08-08 10:06:34 +02:00
|
|
|
#if RELEASE_VERSION>=REGIONOWNER_VERSION
|
2015-12-07 17:59:50 +01:00
|
|
|
WRITE_INT(data->store, region_get_morale(r));
|
2014-07-23 11:54:51 +02:00
|
|
|
write_owner(data, r->land->ownership);
|
|
|
|
WRITE_SECTION(data->store);
|
2010-08-08 10:06:34 +02:00
|
|
|
#endif
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2016-02-09 00:28:23 +01:00
|
|
|
write_attribs(data->store, r->attribs, r);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_SECTION(data->store);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 01:03:07 +01:00
|
|
|
void write_region(gamedata *data, const region *r)
|
|
|
|
{
|
|
|
|
storage *store = data->store;
|
|
|
|
WRITE_INT(store, r->x);
|
|
|
|
WRITE_INT(store, r->y);
|
|
|
|
writeregion(data, r);
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static ally **addally(const faction * f, ally ** sfp, int aid, int state)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
struct faction *af = findfaction(aid);
|
|
|
|
ally *sf;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
state &= ~HELP_OBSERVE;
|
2010-08-08 10:06:34 +02:00
|
|
|
#ifndef REGIONOWNERS
|
2014-07-23 11:54:51 +02:00
|
|
|
state &= ~HELP_TRAVEL;
|
2010-08-08 10:06:34 +02:00
|
|
|
#endif
|
2014-07-23 11:54:51 +02:00
|
|
|
state &= HelpMask();
|
|
|
|
|
|
|
|
if (state == 0)
|
|
|
|
return sfp;
|
|
|
|
|
2014-10-31 15:52:40 +01:00
|
|
|
while (*sfp) {
|
|
|
|
sfp = &(*sfp)->next;
|
|
|
|
}
|
|
|
|
|
2014-10-31 15:13:05 +01:00
|
|
|
sf = ally_add(sfp, af);
|
2014-07-23 11:54:51 +02:00
|
|
|
if (!sf->faction) {
|
|
|
|
variant id;
|
|
|
|
id.i = aid;
|
|
|
|
ur_add(id, &sf->faction, resolve_faction);
|
|
|
|
}
|
|
|
|
sf->status = state & HELP_ALL;
|
|
|
|
|
|
|
|
return &sf->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 23:19:35 +02:00
|
|
|
int get_spell_level_faction(const spell * sp, void * cbdata)
|
2012-05-24 09:56:54 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
static spellbook * common = 0;
|
|
|
|
spellbook * book;
|
|
|
|
faction * f = (faction *)cbdata;
|
|
|
|
spellbook_entry * sbe;
|
|
|
|
|
|
|
|
book = get_spellbook(magic_school[f->magiegebiet]);
|
|
|
|
if (book) {
|
|
|
|
sbe = spellbook_get(book, sp);
|
|
|
|
if (sbe) return sbe->level;
|
|
|
|
}
|
|
|
|
if (!common) {
|
|
|
|
common = get_spellbook(magic_school[M_COMMON]);
|
|
|
|
}
|
|
|
|
sbe = spellbook_get(common, sp);
|
|
|
|
if (sbe) {
|
|
|
|
return sbe->level;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("read_spellbook: faction '%s' has a spell with unknown level: '%s'", factionname(f), sp->sname);
|
|
|
|
}
|
|
|
|
return 0;
|
2012-05-26 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2016-02-20 22:23:19 +01:00
|
|
|
static char * getpasswd(int fno) {
|
2016-02-20 18:21:39 +01:00
|
|
|
const char *prefix = itoa36(fno);
|
|
|
|
size_t len = strlen(prefix);
|
|
|
|
FILE * F = fopen("passwords.txt", "r");
|
|
|
|
char line[80];
|
|
|
|
if (F) {
|
|
|
|
while (!feof(F)) {
|
|
|
|
fgets(line, sizeof(line), F);
|
2016-02-23 08:35:31 +01:00
|
|
|
if (line[len] == ':' && strncmp(prefix, line, len) == 0) {
|
|
|
|
size_t slen = strlen(line) - 1;
|
|
|
|
assert(line[slen] == '\n');
|
2016-02-20 22:23:19 +01:00
|
|
|
line[slen] = 0;
|
2016-02-20 18:21:39 +01:00
|
|
|
fclose(F);
|
2017-01-10 16:31:05 +01:00
|
|
|
return strdup(line + len + 1);
|
2016-02-20 18:21:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(F);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-02-21 10:10:26 +01:00
|
|
|
|
|
|
|
static void read_password(gamedata *data, faction *f) {
|
|
|
|
char name[128];
|
|
|
|
READ_STR(data->store, name, sizeof(name));
|
2016-05-29 10:58:49 +02:00
|
|
|
if (name[0] == '$' && data->version == BADCRYPT_VERSION) {
|
2016-02-21 11:01:50 +01:00
|
|
|
char * pass = getpasswd(f->no);
|
|
|
|
if (pass) {
|
2016-02-12 08:31:42 +01:00
|
|
|
faction_setpassword(f, password_encode(pass, PASSWORD_DEFAULT));
|
2017-02-18 21:15:14 +01:00
|
|
|
free(pass); /* TODO: remove this allocation! */
|
2016-02-21 11:01:50 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-05-20 20:49:47 +02:00
|
|
|
log_error("data version is BADCRYPT but %s not in password.txt", itoa36(f->no));
|
2016-02-21 11:01:50 +01:00
|
|
|
}
|
2016-02-21 10:10:26 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-02-12 08:31:42 +01:00
|
|
|
faction_setpassword(f, (data->version >= CRYPT_VERSION) ? name : password_encode(name, PASSWORD_DEFAULT));
|
2016-02-21 10:10:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _test_read_password(gamedata *data, faction *f) {
|
|
|
|
read_password(data, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_password(gamedata *data, const faction *f) {
|
2016-02-21 11:01:50 +01:00
|
|
|
WRITE_TOK(data->store, (const char *)f->_password);
|
2016-02-21 10:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void _test_write_password(gamedata *data, const faction *f) {
|
|
|
|
write_password(data, f);
|
|
|
|
}
|
|
|
|
|
2016-11-12 00:47:25 +01:00
|
|
|
faction *read_faction(struct gamedata * data)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
ally **sfp;
|
|
|
|
int planes, n;
|
|
|
|
faction *f;
|
|
|
|
char name[DISPLAYSIZE];
|
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
2016-02-05 23:10:05 +01:00
|
|
|
assert(n > 0);
|
2014-07-23 11:54:51 +02:00
|
|
|
f = findfaction(n);
|
|
|
|
if (f == NULL) {
|
|
|
|
f = (faction *)calloc(1, sizeof(faction));
|
|
|
|
f->no = n;
|
|
|
|
}
|
|
|
|
else {
|
2016-11-12 00:47:25 +01:00
|
|
|
f->allies = NULL; /* FIXME: mem leak */
|
|
|
|
while (f->attribs) {
|
2014-07-23 11:54:51 +02:00
|
|
|
a_remove(&f->attribs, f->attribs);
|
2016-11-12 00:47:25 +01:00
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
READ_INT(data->store, &f->subscription);
|
|
|
|
|
2015-08-19 19:43:47 +02:00
|
|
|
if (data->version >= SPELL_LEVEL_VERSION) {
|
|
|
|
READ_INT(data->store, &f->max_spelllevel);
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
if (alliances || data->version >= OWNER_2_VERSION) {
|
|
|
|
int allianceid;
|
|
|
|
READ_INT(data->store, &allianceid);
|
|
|
|
if (allianceid > 0)
|
|
|
|
f->alliance = findalliance(allianceid);
|
|
|
|
if (f->alliance) {
|
|
|
|
alliance *al = f->alliance;
|
|
|
|
if (al->flags & ALF_NON_ALLIED) {
|
|
|
|
assert(!al->members
|
|
|
|
|| !"non-allied dummy-alliance has more than one member");
|
|
|
|
}
|
2017-01-26 17:41:21 +01:00
|
|
|
selist_push(&al->members, f);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
else if (rule_region_owners()) {
|
|
|
|
/* compat fix for non-allied factions */
|
|
|
|
alliance *al = makealliance(0, NULL);
|
|
|
|
setalliance(f, al);
|
|
|
|
}
|
|
|
|
if (data->version >= OWNER_2_VERSION) {
|
|
|
|
READ_INT(data->store, &f->alliance_joindate);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f->alliance_joindate = turn - 10; /* we're guessing something safe here */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
READ_STR(data->store, name, sizeof(name));
|
2016-11-11 00:30:49 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
2016-11-11 11:09:57 +01:00
|
|
|
log_warning("trim faction %s name to '%s'", itoa36(f->no), name);
|
2016-11-11 00:30:49 +01:00
|
|
|
};
|
2017-01-10 16:31:05 +01:00
|
|
|
f->name = strdup(name);
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_STR(data->store, name, sizeof(name));
|
2016-11-11 00:30:49 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
2016-11-11 11:09:57 +01:00
|
|
|
log_warning("trim faction %s banner to '%s'", itoa36(f->no), name);
|
2016-11-11 00:30:49 +01:00
|
|
|
};
|
2017-01-10 16:31:05 +01:00
|
|
|
f->banner = strdup(name);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-11-17 21:29:15 +01:00
|
|
|
log_debug(" - Lese Partei %s (%s)", f->name, itoa36(f->no));
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
if (set_email(&f->email, name) != 0) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_warning("Invalid email address for faction %s: %s", itoa36(f->no), name);
|
2014-07-23 11:54:51 +02:00
|
|
|
set_email(&f->email, "");
|
|
|
|
}
|
|
|
|
|
2016-02-21 10:10:26 +01:00
|
|
|
read_password(data, f);
|
2014-08-14 05:06:36 +02:00
|
|
|
if (data->version < NOOVERRIDE_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_STR(data->store, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
f->locale = get_locale(name);
|
2014-11-23 21:15:09 +01:00
|
|
|
if (!f->locale) f->locale = default_locale;
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(data->store, &f->lastorders);
|
|
|
|
READ_INT(data->store, &f->age);
|
|
|
|
READ_STR(data->store, name, sizeof(name));
|
|
|
|
f->race = rc_find(name);
|
2014-06-01 11:07:22 +02:00
|
|
|
if (!f->race) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("unknown race in data: %s", name);
|
2014-06-01 11:07:22 +02:00
|
|
|
}
|
|
|
|
assert(f->race);
|
2013-12-31 10:06:28 +01:00
|
|
|
READ_INT(data->store, &n);
|
2014-07-23 11:54:51 +02:00
|
|
|
f->magiegebiet = (magic_t)n;
|
|
|
|
|
|
|
|
if (data->version < FOSS_VERSION) {
|
|
|
|
/* ignore karma */
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-05-10 18:57:04 +02:00
|
|
|
READ_INT(data->store, &f->flags);
|
|
|
|
if (data->version < INTFLAGS_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
if (f->no == 0 || f->no == 666) {
|
2014-10-16 07:46:08 +02:00
|
|
|
f->flags = FFL_NPC | FFL_NOIDLEOUT;
|
2014-05-10 18:57:04 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-02-13 13:42:02 +01:00
|
|
|
read_attribs(data, &f->attribs, f);
|
2014-12-12 23:00:30 +01:00
|
|
|
read_items(data->store, &f->items);
|
2010-08-08 10:06:34 +02:00
|
|
|
for (;;) {
|
2013-12-31 10:06:28 +01:00
|
|
|
READ_TOK(data->store, name, sizeof(name));
|
2014-07-23 11:54:51 +02:00
|
|
|
if (strcmp("end", name) == 0)
|
|
|
|
break;
|
|
|
|
READ_INT(data->store, &n); /* there used to be a level here, which is now ignored */
|
|
|
|
}
|
|
|
|
READ_INT(data->store, &planes);
|
|
|
|
while (--planes >= 0) {
|
|
|
|
int id, ux, uy;
|
|
|
|
READ_INT(data->store, &id);
|
|
|
|
READ_INT(data->store, &ux);
|
|
|
|
READ_INT(data->store, &uy);
|
2015-05-19 08:02:32 +02:00
|
|
|
faction_setorigin(f, id, ux, uy);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
f->newbies = 0;
|
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
f->options = n;
|
|
|
|
|
2014-11-11 16:53:56 +01:00
|
|
|
n = want(O_REPORT) | want(O_COMPUTER);
|
|
|
|
if ((f->options & n) == 0) {
|
2014-07-23 11:54:51 +02:00
|
|
|
/* Kein Report eingestellt, Fehler */
|
2014-11-11 16:53:56 +01:00
|
|
|
f->options |= n;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2015-03-29 14:03:56 +02:00
|
|
|
if (data->version < JSON_REPORT_VERSION) {
|
|
|
|
/* mistakes were made in the past*/
|
|
|
|
f->options &= ~want(O_JSON);
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
sfp = &f->allies;
|
2014-12-12 23:00:30 +01:00
|
|
|
for (;;) {
|
|
|
|
int aid = 0;
|
|
|
|
READ_INT(data->store, &aid);
|
|
|
|
if (aid > 0) {
|
|
|
|
int state;
|
2014-07-23 11:54:51 +02:00
|
|
|
READ_INT(data->store, &state);
|
|
|
|
sfp = addally(f, sfp, aid, state);
|
|
|
|
}
|
2014-12-12 23:00:30 +01:00
|
|
|
else {
|
|
|
|
break;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-13 13:42:02 +01:00
|
|
|
read_groups(data, f);
|
2014-07-23 11:54:51 +02:00
|
|
|
f->spellbook = 0;
|
|
|
|
if (data->version >= REGIONOWNER_VERSION) {
|
2016-02-13 15:48:48 +01:00
|
|
|
read_spellbook(FactionSpells() ? &f->spellbook : 0, data, get_spell_level_faction, (void *)f);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
return f;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 00:47:25 +01:00
|
|
|
void write_faction(struct gamedata *data, const faction * f)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
ally *sf;
|
|
|
|
ursprung *ur;
|
|
|
|
|
2016-02-18 08:11:39 +01:00
|
|
|
assert(f->_alive);
|
2014-07-23 11:54:51 +02:00
|
|
|
write_faction_reference(f, data->store);
|
|
|
|
WRITE_INT(data->store, f->subscription);
|
2015-08-19 19:43:47 +02:00
|
|
|
#if RELEASE_VERSION >= SPELL_LEVEL_VERSION
|
|
|
|
WRITE_INT(data->store, f->max_spelllevel);
|
|
|
|
#endif
|
2014-07-23 11:54:51 +02:00
|
|
|
if (f->alliance) {
|
|
|
|
WRITE_INT(data->store, f->alliance->id);
|
|
|
|
if (f->alliance->flags & ALF_NON_ALLIED) {
|
|
|
|
assert(f == f->alliance->_leader
|
|
|
|
|| !"non-allied faction is not leader of its own dummy-alliance.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WRITE_INT(data->store, 0);
|
|
|
|
}
|
|
|
|
WRITE_INT(data->store, f->alliance_joindate);
|
|
|
|
|
2016-01-12 06:46:51 +01:00
|
|
|
WRITE_STR(data->store, f->name);
|
|
|
|
WRITE_STR(data->store, f->banner);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_STR(data->store, f->email);
|
2016-02-21 10:10:26 +01:00
|
|
|
write_password(data, f);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_TOK(data->store, locale_name(f->locale));
|
|
|
|
WRITE_INT(data->store, f->lastorders);
|
|
|
|
WRITE_INT(data->store, f->age);
|
2014-08-24 21:49:55 +02:00
|
|
|
WRITE_TOK(data->store, f->race->_name);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
WRITE_INT(data->store, f->magiegebiet);
|
|
|
|
|
|
|
|
WRITE_INT(data->store, f->flags & FFL_SAVEMASK);
|
2016-02-09 00:28:23 +01:00
|
|
|
write_attribs(data->store, f->attribs, f);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
write_items(data->store, f->items);
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
WRITE_TOK(data->store, "end");
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
WRITE_INT(data->store, listlen(f->ursprung));
|
|
|
|
for (ur = f->ursprung; ur; ur = ur->next) {
|
|
|
|
WRITE_INT(data->store, ur->id);
|
|
|
|
WRITE_INT(data->store, ur->x);
|
|
|
|
WRITE_INT(data->store, ur->y);
|
|
|
|
}
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
WRITE_INT(data->store, f->options & ~want(O_DEBUG));
|
|
|
|
WRITE_SECTION(data->store);
|
|
|
|
|
|
|
|
for (sf = f->allies; sf; sf = sf->next) {
|
2015-11-02 18:06:19 +01:00
|
|
|
int no;
|
|
|
|
int status;
|
|
|
|
|
2015-11-01 21:43:18 +01:00
|
|
|
assert(sf->faction);
|
|
|
|
|
2015-11-02 18:06:19 +01:00
|
|
|
no = sf->faction->no;
|
|
|
|
status = alliedfaction(NULL, f, sf->faction, HELP_ALL);
|
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
if (status != 0) {
|
|
|
|
WRITE_INT(data->store, no);
|
|
|
|
WRITE_INT(data->store, sf->status);
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 10:06:28 +01:00
|
|
|
WRITE_INT(data->store, 0);
|
2014-07-23 11:54:51 +02:00
|
|
|
WRITE_SECTION(data->store);
|
2014-11-03 18:10:05 +01:00
|
|
|
write_groups(data->store, f);
|
2014-07-23 11:54:51 +02:00
|
|
|
write_spellbook(f->spellbook, data->store);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 19:43:47 +02:00
|
|
|
static int cb_sb_maxlevel(spellbook_entry *sbe, void *cbdata) {
|
|
|
|
faction *f = (faction *)cbdata;
|
|
|
|
if (sbe->level > f->max_spelllevel) {
|
|
|
|
f->max_spelllevel = sbe->level;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-14 01:35:45 +01:00
|
|
|
int readgame(const char *filename)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2016-02-25 19:12:38 +01:00
|
|
|
int n;
|
2014-07-23 11:54:51 +02:00
|
|
|
char path[MAX_PATH];
|
|
|
|
gamedata gdata = { 0 };
|
|
|
|
storage store;
|
2014-11-03 22:29:04 +01:00
|
|
|
stream strm;
|
2014-07-23 11:54:51 +02:00
|
|
|
FILE *F;
|
2015-11-11 15:54:14 +01:00
|
|
|
size_t sz;
|
2013-12-31 10:06:28 +01:00
|
|
|
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("- reading game data from %s", filename);
|
2016-02-05 23:10:05 +01:00
|
|
|
join_path(datapath(), filename, path, sizeof(path));
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
F = fopen(path, "rb");
|
2014-07-23 11:54:51 +02:00
|
|
|
if (!F) {
|
|
|
|
perror(path);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-11 15:54:14 +01:00
|
|
|
sz = fread(&gdata.version, sizeof(int), 1, F);
|
2016-02-23 08:35:31 +01:00
|
|
|
if (sz != sizeof(int) || gdata.version >= INTPAK_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
int stream_version;
|
2015-11-04 11:46:57 +01:00
|
|
|
size_t sz = fread(&stream_version, sizeof(int), 1, F);
|
2016-02-23 08:35:31 +01:00
|
|
|
assert((sz == 1 && stream_version == STREAM_VERSION) || !"unsupported data format");
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
assert(gdata.version >= MIN_VERSION || !"unsupported data format");
|
2015-04-19 08:13:40 +02:00
|
|
|
assert(gdata.version <= MAX_VERSION || !"unsupported data format");
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2014-11-03 22:29:04 +01:00
|
|
|
fstream_init(&strm, F);
|
|
|
|
binstore_init(&store, &strm);
|
2014-07-23 11:54:51 +02:00
|
|
|
gdata.store = &store;
|
|
|
|
|
2014-08-14 05:06:36 +02:00
|
|
|
if (gdata.version >= BUILDNO_VERSION) {
|
|
|
|
int build;
|
|
|
|
READ_INT(&store, &build);
|
|
|
|
log_debug("data in %s created with build %d.", filename, build);
|
|
|
|
}
|
2016-02-25 19:12:38 +01:00
|
|
|
n = read_game(&gdata);
|
|
|
|
binstore_done(&store);
|
|
|
|
fstream_done(&strm);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:41:25 +01:00
|
|
|
void write_building(gamedata *data, const building *b)
|
|
|
|
{
|
|
|
|
storage *store = data->store;
|
|
|
|
|
|
|
|
write_building_reference(b, store);
|
|
|
|
WRITE_STR(store, b->name);
|
|
|
|
WRITE_STR(store, b->display ? b->display : "");
|
|
|
|
WRITE_INT(store, b->size);
|
|
|
|
WRITE_TOK(store, b->type->_name);
|
|
|
|
write_attribs(store, b->attribs, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct building *read_building(gamedata *data) {
|
|
|
|
char name[DISPLAYSIZE];
|
|
|
|
building *b;
|
|
|
|
storage * store = data->store;
|
|
|
|
|
|
|
|
b = (building *)calloc(1, sizeof(building));
|
|
|
|
READ_INT(store, &b->no);
|
|
|
|
bhash(b);
|
|
|
|
READ_STR(store, name, sizeof(name));
|
2016-11-11 21:46:56 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
|
|
|
log_warning("trim building %s name to '%s'", itoa36(b->no), name);
|
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
b->name = strdup(name);
|
2016-11-11 21:41:25 +01:00
|
|
|
if (lomem) {
|
|
|
|
READ_STR(store, NULL, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
READ_STR(store, name, sizeof(name));
|
2016-11-11 21:46:56 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
|
|
|
log_warning("trim building %s info to '%s'", itoa36(b->no), name);
|
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
b->display = strdup(name);
|
2016-11-11 21:41:25 +01:00
|
|
|
}
|
|
|
|
READ_INT(store, &b->size);
|
|
|
|
READ_STR(store, name, sizeof(name));
|
|
|
|
b->type = bt_find(name);
|
2017-02-24 14:29:14 +01:00
|
|
|
if (!b->type) {
|
|
|
|
log_error("building %d has unknown type %s", b->no, name);
|
|
|
|
b->type = bt_find("building");
|
|
|
|
assert(b->type);
|
|
|
|
}
|
2016-11-11 21:41:25 +01:00
|
|
|
read_attribs(data, &b->attribs, b);
|
|
|
|
|
2017-02-18 21:15:14 +01:00
|
|
|
/* repairs, bug 2221: */
|
2016-11-11 21:41:25 +01:00
|
|
|
if (b->type->maxsize>0 && b->size>b->type->maxsize) {
|
|
|
|
log_error("building too big: %s (%s size %d of %d), fixing.", buildingname(b), b->type->_name, b->size, b->type->maxsize);
|
|
|
|
b->size = b->type->maxsize;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2016-11-11 22:25:56 +01:00
|
|
|
void write_ship(gamedata *data, const ship *sh)
|
|
|
|
{
|
|
|
|
storage *store = data->store;
|
|
|
|
write_ship_reference(sh, store);
|
|
|
|
WRITE_STR(store, (const char *)sh->name);
|
|
|
|
WRITE_STR(store, sh->display ? (const char *)sh->display : "");
|
|
|
|
WRITE_TOK(store, sh->type->_name);
|
|
|
|
WRITE_INT(store, sh->size);
|
|
|
|
WRITE_INT(store, sh->damage);
|
|
|
|
WRITE_INT(store, sh->flags & SFL_SAVEMASK);
|
|
|
|
assert((sh->type->flags & SFL_NOCOAST) == 0 || sh->coast == NODIRECTION);
|
|
|
|
WRITE_INT(store, sh->coast);
|
|
|
|
write_attribs(store, sh->attribs, sh);
|
|
|
|
}
|
|
|
|
|
|
|
|
ship *read_ship(struct gamedata *data)
|
|
|
|
{
|
2016-02-25 19:12:38 +01:00
|
|
|
char name[DISPLAYSIZE];
|
2016-11-11 22:25:56 +01:00
|
|
|
ship *sh;
|
|
|
|
int n;
|
|
|
|
storage *store = data->store;
|
|
|
|
|
|
|
|
sh = (ship *)calloc(1, sizeof(ship));
|
|
|
|
READ_INT(store, &sh->no);
|
|
|
|
shash(sh);
|
|
|
|
READ_STR(store, name, sizeof(name));
|
2016-11-11 22:28:22 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
|
|
|
log_warning("trim ship %s name to '%s'", itoa36(sh->no), name);
|
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
sh->name = strdup(name);
|
2016-11-11 22:25:56 +01:00
|
|
|
if (lomem) {
|
|
|
|
READ_STR(store, NULL, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
READ_STR(store, name, sizeof(name));
|
2016-11-11 22:28:22 +01:00
|
|
|
if (unicode_utf8_trim(name)!=0) {
|
|
|
|
log_warning("trim ship %s info to '%s'", itoa36(sh->no), name);
|
|
|
|
}
|
2017-01-10 16:31:05 +01:00
|
|
|
sh->display = strdup(name);
|
2016-11-11 22:25:56 +01:00
|
|
|
}
|
|
|
|
READ_STR(store, name, sizeof(name));
|
|
|
|
sh->type = st_find(name);
|
|
|
|
if (sh->type == NULL) {
|
|
|
|
/* old datafiles */
|
|
|
|
sh->type = st_find((const char *)LOC(default_locale, name));
|
|
|
|
}
|
|
|
|
assert(sh->type || !"ship_type not registered!");
|
|
|
|
|
|
|
|
READ_INT(store, &sh->size);
|
|
|
|
READ_INT(store, &sh->damage);
|
|
|
|
if (data->version >= FOSS_VERSION) {
|
|
|
|
READ_INT(store, &sh->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attribute rekursiv einlesen */
|
|
|
|
|
|
|
|
READ_INT(store, &n);
|
|
|
|
sh->coast = (direction_t)n;
|
|
|
|
if (sh->type->flags & SFL_NOCOAST) {
|
|
|
|
sh->coast = NODIRECTION;
|
|
|
|
}
|
|
|
|
read_attribs(data, &sh->attribs, sh);
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int read_game(gamedata *data) {
|
|
|
|
int p, nread;
|
2016-02-25 19:12:38 +01:00
|
|
|
faction *f, **fp;
|
|
|
|
region *r;
|
2016-11-11 21:41:25 +01:00
|
|
|
building **bp;
|
2016-02-25 19:12:38 +01:00
|
|
|
ship **shp;
|
|
|
|
unit *u;
|
|
|
|
int rmax = maxregions;
|
|
|
|
storage * store = data->store;
|
2016-11-11 21:41:25 +01:00
|
|
|
const struct building_type *bt_lighthouse = bt_find("lighthouse");
|
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
if (data->version >= SAVEGAMEID_VERSION) {
|
2014-07-23 11:54:51 +02:00
|
|
|
int gameid;
|
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
READ_INT(store, &gameid);
|
2014-07-23 11:54:51 +02:00
|
|
|
if (gameid != game_id()) {
|
2015-11-03 22:56:05 +01:00
|
|
|
log_warning("game mismatch: datafile contains game %d, but config is for %d", gameid, game_id());
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-12 23:00:30 +01:00
|
|
|
else {
|
2016-02-25 19:12:38 +01:00
|
|
|
READ_STR(store, NULL, 0);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2016-02-25 19:12:38 +01:00
|
|
|
read_attribs(data, &global.attribs, NULL);
|
|
|
|
READ_INT(store, &turn);
|
2014-07-23 11:54:51 +02:00
|
|
|
global.data_turn = turn;
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug(" - reading turn %d", turn);
|
2014-07-23 11:54:51 +02:00
|
|
|
rng_init(turn);
|
2016-02-26 19:01:28 +01:00
|
|
|
READ_INT(store, NULL); /* max_unique_id = ignore */
|
2016-03-13 09:49:04 +01:00
|
|
|
READ_INT(store, &nextborder);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-08-18 18:59:30 +02:00
|
|
|
read_planes(data);
|
2016-02-25 19:12:38 +01:00
|
|
|
read_alliances(data);
|
|
|
|
READ_INT(store, &nread);
|
2015-09-26 22:42:22 +02:00
|
|
|
log_debug(" - Einzulesende Parteien: %d\n", nread);
|
2014-07-23 11:54:51 +02:00
|
|
|
fp = &factions;
|
2016-11-12 00:47:25 +01:00
|
|
|
while (*fp) {
|
2014-07-23 11:54:51 +02:00
|
|
|
fp = &(*fp)->next;
|
2016-11-12 00:47:25 +01:00
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
while (--nread >= 0) {
|
2016-11-12 00:47:25 +01:00
|
|
|
faction *f = read_faction(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
*fp = f;
|
|
|
|
fp = &f->next;
|
|
|
|
fhash(f);
|
|
|
|
}
|
|
|
|
*fp = 0;
|
|
|
|
|
|
|
|
/* Regionen */
|
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
READ_INT(store, &nread);
|
2016-02-26 19:01:28 +01:00
|
|
|
assert(nread < MAXREGIONS && nread>=0);
|
2014-07-23 11:54:51 +02:00
|
|
|
if (rmax < 0) {
|
|
|
|
rmax = nread;
|
|
|
|
}
|
2015-09-26 22:42:22 +02:00
|
|
|
log_debug(" - Einzulesende Regionen: %d/%d\r", rmax, nread);
|
2016-11-12 01:03:07 +01:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
while (--nread >= 0) {
|
|
|
|
unit **up;
|
|
|
|
|
2016-11-12 01:03:07 +01:00
|
|
|
r = read_region(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
/* Burgen */
|
2016-02-25 19:12:38 +01:00
|
|
|
READ_INT(store, &p);
|
2014-07-23 11:54:51 +02:00
|
|
|
bp = &r->buildings;
|
|
|
|
|
|
|
|
while (--p >= 0) {
|
2016-11-11 21:41:25 +01:00
|
|
|
building *b = *bp = read_building(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
if (b->type == bt_lighthouse) {
|
|
|
|
r->flags |= RF_LIGHTHOUSE;
|
|
|
|
}
|
2016-11-11 22:25:56 +01:00
|
|
|
b->region = r;
|
2016-11-11 21:41:25 +01:00
|
|
|
bp = &b->next;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
/* Schiffe */
|
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
READ_INT(store, &p);
|
2014-07-23 11:54:51 +02:00
|
|
|
shp = &r->ships;
|
|
|
|
|
|
|
|
while (--p >= 0) {
|
2016-11-11 22:25:56 +01:00
|
|
|
ship *sh = *shp = read_ship(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
sh->region = r;
|
|
|
|
shp = &sh->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*shp = 0;
|
|
|
|
|
|
|
|
/* Einheiten */
|
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
READ_INT(store, &p);
|
2014-07-23 11:54:51 +02:00
|
|
|
up = &r->units;
|
|
|
|
|
|
|
|
while (--p >= 0) {
|
2016-02-25 19:12:38 +01:00
|
|
|
unit *u = read_unit(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
if (data->version < JSON_REPORT_VERSION) {
|
2015-02-11 07:55:35 +01:00
|
|
|
if (u->_name && fval(u->faction, FFL_NPC)) {
|
2015-04-21 12:35:39 +02:00
|
|
|
if (!u->_name[0] || unit_name_equals_race(u)) {
|
2014-12-09 07:44:26 +01:00
|
|
|
unit_setname(u, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
assert(u->region == NULL);
|
|
|
|
u->region = r;
|
|
|
|
*up = u;
|
|
|
|
up = &u->next;
|
|
|
|
|
2016-11-12 01:03:07 +01:00
|
|
|
update_interval(u->faction, r);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2016-11-12 01:03:07 +01:00
|
|
|
|
|
|
|
if ((nread & 0x3FF) == 0) { /* das spart extrem Zeit */
|
|
|
|
log_debug(" - Einzulesende Regionen: %d/%d * %d,%d \r", rmax, nread, r->x, r->y);
|
|
|
|
}
|
|
|
|
--rmax;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2016-02-25 19:12:38 +01:00
|
|
|
read_borders(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
/* Unaufgeloeste Zeiger initialisieren */
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("fixing unresolved references.");
|
2014-07-23 11:54:51 +02:00
|
|
|
resolve();
|
|
|
|
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("updating area information for lighthouses.");
|
2014-07-23 11:54:51 +02:00
|
|
|
for (r = regions; r; r = r->next) {
|
|
|
|
if (r->flags & RF_LIGHTHOUSE) {
|
|
|
|
building *b;
|
2016-09-17 19:52:13 +02:00
|
|
|
for (b = r->buildings; b; b = b->next) {
|
2014-07-23 11:54:51 +02:00
|
|
|
update_lighthouse(b);
|
2016-09-17 19:52:13 +02:00
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("marking factions as alive.");
|
2014-07-23 11:54:51 +02:00
|
|
|
for (f = factions; f; f = f->next) {
|
|
|
|
if (f->flags & FFL_NPC) {
|
2016-01-11 11:54:45 +01:00
|
|
|
f->_alive = true;
|
2015-08-02 22:08:35 +02:00
|
|
|
f->magiegebiet = M_GRAY;
|
2014-07-23 11:54:51 +02:00
|
|
|
if (f->no == 0) {
|
|
|
|
int no = 666;
|
|
|
|
while (findfaction(no))
|
|
|
|
++no;
|
2016-05-29 10:58:49 +02:00
|
|
|
log_warning("renum(monsters, %d)", no);
|
2014-07-23 11:54:51 +02:00
|
|
|
renumber_faction(f, no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (u = f->units; u; u = u->nextF) {
|
2016-02-25 19:12:38 +01:00
|
|
|
if (data->version < SPELL_LEVEL_VERSION) {
|
2015-08-19 19:43:47 +02:00
|
|
|
sc_mage *mage = get_mage(u);
|
|
|
|
if (mage) {
|
|
|
|
faction *f = u->faction;
|
2015-08-27 16:16:55 +02:00
|
|
|
int skl = effskill(u, SK_MAGIC, 0);
|
2015-08-19 19:43:47 +02:00
|
|
|
if (f->magiegebiet == M_GRAY) {
|
2016-05-29 10:58:49 +02:00
|
|
|
log_error("faction %s had magic=gray, fixing (%s)", factionname(f), magic_school[mage->magietyp]);
|
2015-08-19 19:43:47 +02:00
|
|
|
f->magiegebiet = mage->magietyp;
|
|
|
|
}
|
|
|
|
if (f->max_spelllevel < skl) {
|
|
|
|
f->max_spelllevel = skl;
|
|
|
|
}
|
|
|
|
if (mage->spellcount < 0) {
|
|
|
|
mage->spellcount = 0;
|
|
|
|
}
|
2015-08-02 22:08:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
if (u->number > 0) {
|
2016-01-11 11:54:45 +01:00
|
|
|
f->_alive = true;
|
2016-02-25 19:12:38 +01:00
|
|
|
if (data->version >= SPELL_LEVEL_VERSION) {
|
2015-08-19 19:43:47 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-25 19:12:38 +01:00
|
|
|
if (data->version < SPELL_LEVEL_VERSION && f->spellbook) {
|
2015-08-19 19:43:47 +02:00
|
|
|
spellbook_foreach(f->spellbook, cb_sb_maxlevel, f);
|
|
|
|
}
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loadplane || maxregions >= 0) {
|
|
|
|
remove_empty_factions();
|
|
|
|
}
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug("Done loading turn %d.", turn);
|
2015-08-20 12:13:09 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-11-11 16:53:56 +01:00
|
|
|
static void clear_npc_orders(faction *f)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
if (f) {
|
|
|
|
unit *u;
|
|
|
|
for (u = f->units; u; u = u->nextF) {
|
|
|
|
free_orders(&u->orders);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 10:06:28 +01:00
|
|
|
int writegame(const char *filename)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-23 11:54:51 +02:00
|
|
|
int n;
|
|
|
|
char path[MAX_PATH];
|
|
|
|
gamedata gdata;
|
|
|
|
storage store;
|
2014-11-03 22:29:04 +01:00
|
|
|
stream strm;
|
2014-07-23 11:54:51 +02:00
|
|
|
FILE *F;
|
|
|
|
|
2016-02-01 09:26:24 +01:00
|
|
|
create_directories();
|
|
|
|
join_path(datapath(), filename, path, sizeof(path));
|
2010-08-08 10:06:34 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2015-11-05 13:22:51 +01:00
|
|
|
/* make sure we don't overwrite an existing file (hard links) */
|
2017-01-10 18:40:01 +01:00
|
|
|
if (remove(path)!=0) {
|
2016-09-11 17:19:27 +02:00
|
|
|
if (errno==ENOENT) {
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
#endif
|
2016-02-05 23:10:05 +01:00
|
|
|
F = fopen(path, "wb");
|
2013-12-31 10:06:28 +01:00
|
|
|
if (!F) {
|
2016-02-01 09:26:24 +01:00
|
|
|
perror(path);
|
|
|
|
return -1;
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
gdata.store = &store;
|
|
|
|
gdata.version = RELEASE_VERSION;
|
|
|
|
fwrite(&gdata.version, sizeof(int), 1, F);
|
2016-02-25 19:12:38 +01:00
|
|
|
n = STREAM_VERSION;
|
2014-07-23 11:54:51 +02:00
|
|
|
fwrite(&n, sizeof(int), 1, F);
|
|
|
|
|
2014-11-03 22:29:04 +01:00
|
|
|
fstream_init(&strm, F);
|
|
|
|
binstore_init(&store, &strm);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-09-15 20:11:38 +02:00
|
|
|
WRITE_INT(&store, version_no(eressea_version()));
|
2016-02-25 19:12:38 +01:00
|
|
|
n = write_game(&gdata);
|
|
|
|
binstore_done(&store);
|
|
|
|
fstream_done(&strm);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int write_game(gamedata *data) {
|
|
|
|
storage * store = data->store;
|
|
|
|
region *r;
|
|
|
|
faction *f;
|
|
|
|
int n;
|
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
/* globale Variablen */
|
2016-02-25 19:12:38 +01:00
|
|
|
assert(data->version <= MAX_VERSION && data->version >= MIN_VERSION);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_INT(store, game_id());
|
|
|
|
WRITE_SECTION(store);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
write_attribs(store, global.attribs, NULL);
|
|
|
|
WRITE_SECTION(store);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_INT(store, turn);
|
2016-02-26 19:01:28 +01:00
|
|
|
WRITE_INT(store, 0 /* max_unique_id */);
|
2016-03-13 09:49:04 +01:00
|
|
|
WRITE_INT(store, nextborder);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
write_planes(store);
|
|
|
|
write_alliances(data);
|
2014-07-23 11:54:51 +02:00
|
|
|
n = listlen(factions);
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_INT(store, n);
|
|
|
|
WRITE_SECTION(store);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-05-29 10:58:49 +02:00
|
|
|
log_debug(" - Schreibe %d Parteien...", n);
|
2014-07-23 11:54:51 +02:00
|
|
|
for (f = factions; f; f = f->next) {
|
2014-11-11 16:53:56 +01:00
|
|
|
if (fval(f, FFL_NPC)) {
|
|
|
|
clear_npc_orders(f);
|
|
|
|
}
|
2016-11-12 00:47:25 +01:00
|
|
|
write_faction(data, f);
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_SECTION(store);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write regions */
|
|
|
|
|
|
|
|
n = listlen(regions);
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_INT(store, n);
|
|
|
|
WRITE_SECTION(store);
|
2015-09-26 22:42:22 +02:00
|
|
|
log_debug(" - Schreibe Regionen: %d", n);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
|
|
|
for (r = regions; r; r = r->next, --n) {
|
2016-02-25 19:12:38 +01:00
|
|
|
ship *sh;
|
|
|
|
building *b;
|
|
|
|
unit *u;
|
2014-07-23 11:54:51 +02:00
|
|
|
/* plus leerzeile */
|
|
|
|
if ((n % 1024) == 0) { /* das spart extrem Zeit */
|
2015-09-26 22:42:22 +02:00
|
|
|
log_debug(" - Schreibe Regionen: %d", n);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_SECTION(store);
|
2016-11-12 01:03:07 +01:00
|
|
|
write_region(data, r);
|
2014-07-23 11:54:51 +02:00
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_INT(store, listlen(r->buildings));
|
|
|
|
WRITE_SECTION(store);
|
2014-07-23 11:54:51 +02:00
|
|
|
for (b = r->buildings; b; b = b->next) {
|
2016-11-11 22:25:56 +01:00
|
|
|
assert(b->region == r);
|
2016-11-11 21:41:25 +01:00
|
|
|
write_building(data, b);
|
2016-02-25 19:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WRITE_INT(store, listlen(r->ships));
|
|
|
|
WRITE_SECTION(store);
|
2014-07-23 11:54:51 +02:00
|
|
|
for (sh = r->ships; sh; sh = sh->next) {
|
|
|
|
assert(sh->region == r);
|
2016-11-11 22:25:56 +01:00
|
|
|
write_ship(data, sh);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_INT(store, listlen(r->units));
|
|
|
|
WRITE_SECTION(store);
|
2014-07-23 11:54:51 +02:00
|
|
|
for (u = r->units; u; u = u->next) {
|
2016-11-11 22:25:56 +01:00
|
|
|
assert(u->region == r);
|
2016-02-25 19:12:38 +01:00
|
|
|
write_unit(data, u);
|
2014-07-23 11:54:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-25 19:12:38 +01:00
|
|
|
WRITE_SECTION(store);
|
|
|
|
write_borders(store);
|
|
|
|
WRITE_SECTION(store);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-23 11:54:51 +02:00
|
|
|
return 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|