From 10b2d08a0a75d63b803e256838406d608eaba29e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 26 Nov 2005 11:49:20 +0000 Subject: [PATCH] new attribute "object" allows arbitrary named data to be added to any object (improvement on the "key" attribute) --- src/common/attributes/Jamfile | 3 +- src/common/attributes/attributes.c | 35 ++-- src/common/attributes/attributes.vcproj | 16 +- .../attributes/{at_movement.c => movement.c} | 2 +- .../attributes/{at_movement.h => movement.h} | 0 src/common/attributes/object.c | 177 ++++++++++++++++++ src/common/attributes/object.h | 37 ++++ src/common/kernel/faction.c | 28 +++ src/common/kernel/faction.h | 4 + src/common/kernel/movement.c | 2 +- src/common/kernel/save.c | 26 --- src/common/kernel/save.h | 3 - src/mapper/map_units.c | 2 +- 13 files changed, 286 insertions(+), 49 deletions(-) rename src/common/attributes/{at_movement.c => movement.c} (98%) rename src/common/attributes/{at_movement.h => movement.h} (100%) create mode 100644 src/common/attributes/object.c create mode 100644 src/common/attributes/object.h diff --git a/src/common/attributes/Jamfile b/src/common/attributes/Jamfile index af137def9..c26c21843 100644 --- a/src/common/attributes/Jamfile +++ b/src/common/attributes/Jamfile @@ -10,7 +10,6 @@ SubDirHdrs $(SUBDIR)/../.. ; SOURCES = aggressive.c alliance.c - at_movement.c attributes.c fleechance.c follow.c @@ -20,7 +19,9 @@ SOURCES = iceberg.c key.c matmod.c + movement.c moved.c + object.c option.c orcification.c otherfaction.c diff --git a/src/common/attributes/attributes.c b/src/common/attributes/attributes.c index 91663b7cc..9a92af535 100644 --- a/src/common/attributes/attributes.c +++ b/src/common/attributes/attributes.c @@ -17,34 +17,42 @@ #include "attributes.h" /* attributes includes */ -#include "key.h" -#include "gm.h" -#include "targetregion.h" -#include "orcification.h" -#include "reduceproduction.h" #include "follow.h" -#include "iceberg.h" +#include "gm.h" #include "hate.h" -#include "overrideroads.h" +#include "iceberg.h" +#include "key.h" +#include "moved.h" +#include "movement.h" +#include "object.h" +#include "orcification.h" #include "otherfaction.h" +#include "overrideroads.h" #include "racename.h" #include "raceprefix.h" +#include "reduceproduction.h" #include "synonym.h" -#include "at_movement.h" +#include "targetregion.h" +#include "variable.h" #ifdef USE_UGROUPS -# include "ugroup.h" +# include "ugroup.h" #endif #ifdef AT_OPTION # include "option.h" #endif -#include "moved.h" -#include "variable.h" #ifdef WDW_PYRAMID -#include "alliance.h" +# include "alliance.h" #endif +/* kernel includes */ +#include +#include +#include +#include +#include + /* util includes */ -#include +#include /* * library initialization @@ -53,6 +61,7 @@ void init_attributes(void) { + at_register(&at_object); at_register(&at_overrideroads); at_register(&at_raceprefix); diff --git a/src/common/attributes/attributes.vcproj b/src/common/attributes/attributes.vcproj index 955cc08fd..423a84092 100644 --- a/src/common/attributes/attributes.vcproj +++ b/src/common/attributes/attributes.vcproj @@ -3,6 +3,7 @@ ProjectType="Visual C++" Version="7.10" Name="attributes" + RootNamespace="attributes" SccProjectName="" SccLocalPath=""> @@ -205,6 +206,9 @@ + + @@ -245,9 +249,6 @@ RelativePath=".\viewrange.h"> - - @@ -278,6 +279,15 @@ + + + + + + diff --git a/src/common/attributes/at_movement.c b/src/common/attributes/movement.c similarity index 98% rename from src/common/attributes/at_movement.c rename to src/common/attributes/movement.c index d6a7ca6f2..3e2d70d85 100644 --- a/src/common/attributes/at_movement.c +++ b/src/common/attributes/movement.c @@ -15,7 +15,7 @@ #include #include #include -#include "at_movement.h" +#include "movement.h" static void write_movement(const attrib * a, FILE * F) diff --git a/src/common/attributes/at_movement.h b/src/common/attributes/movement.h similarity index 100% rename from src/common/attributes/at_movement.h rename to src/common/attributes/movement.h diff --git a/src/common/attributes/object.c b/src/common/attributes/object.c new file mode 100644 index 000000000..63fcb71cd --- /dev/null +++ b/src/common/attributes/object.c @@ -0,0 +1,177 @@ +/* vi: set ts=2: + * + * + * Eressea PB(E)M host Copyright (C) 1998-2003 + * Christian Schlittchen (corwin@amber.kn-bremen.de) + * Katja Zedel (katze@felidae.kn-bremen.de) + * Henning Peters (faroul@beyond.kn-bremen.de) + * Enno Rehling (enno@eressea-pbem.de) + * Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de) + * + * This program may not be used, modified or distributed without + * prior permission by the authors of Eressea. + */ + +#include +#include +#include "object.h" + +/* kernel includes */ +#include +#include +#include +#include +#include + +/* util includes */ +#include + +/* stdc includes */ +#include + +typedef struct object_data { + object_type type; + char * name; + union { + int i; + char * str; + double real; + struct unit * u; + struct region * r; + struct building * b; + struct ship * sh; + struct faction * f; + } data; +} object_data; + +static void +object_write(const attrib *a, FILE *F) +{ + const object_data * data = (object_data *)a->data.v; + int type = (int)data->type; + fprintf(F, "%s %d", data->name, type); + switch (data->type) { + case TUNIT: + write_unit_reference(data->data.u, F); + break; + case TFACTION: + write_faction_reference(data->data.f, F); + break; + case TBUILDING: + write_building_reference(data->data.b, F); + break; + case TSHIP: + /* write_ship_reference(data->data.sh, F); */ + assert(!"not implemented"); + break; + case TREGION: + write_region_reference(data->data.r, F); + break; + case TNONE: + break; + default: + assert(!"illegal type in object-attribute"); + } +} + +static int +object_read(attrib *a, FILE *F) +{ + object_data * data = (object_data *)a->data.v; + int type; + char name[64]; + fscanf(F, "%s %d", name, &type); + data->type = (object_type)type; + switch (data->type) { + case TUNIT: + return read_unit_reference(&data->data.u, F); + case TFACTION: + return read_faction_reference(&data->data.f, F); + case TBUILDING: + return read_building_reference(&data->data.b, F); + case TSHIP: + /* return read_ship_reference(&data->data.sh, F); */ + assert(!"not implemented"); + break; + case TREGION: + return read_region_reference(&data->data.r, F); + case TNONE: + break; + default: + return AT_READ_FAIL; + } + return AT_READ_OK; +} + +static void +object_init(attrib * a) +{ + object_data * data; + a->data.v = malloc(sizeof(object_data)); + data = (object_data *)a->data.v; + data->type = TNONE; +} + +static void +object_done(attrib * a) +{ + object_data * data = (object_data *)a->data.v; + if (data->type == TSTRING) free(data->data.str); + free(a->data.v); +} + +attrib_type at_object = { + "object", object_init, object_done, NULL, + object_write, object_read +}; + +const char * +object_name(const attrib * a) +{ + object_data * data = (object_data *)a->data.v; + return data->name; +} + +struct attrib * +object_create(const char * name, object_type type, variant value) +{ + attrib * a = a_new(&at_object); + object_data * data = (object_data *)a->data.v; + + data->type = type; + data->name = strdup(name); + switch (type) { + case TSTRING: + data->data.str = strdup(value.v); + break; + case TINTEGER: + data->data.i = value.i; + break; + case TREAL: + data->data.real = value.f; + break; + case TREGION: + data->data.r = (region*)value.v; + break; + case TBUILDING: + data->data.b = (building*)value.v; + break; + case TFACTION: + data->data.f = (faction*)value.v; + break; + case TUNIT: + data->data.u = (unit*)value.v; + break; + case TSHIP: + data->data.sh = (ship*)value.v; + break; + case TNONE: + break; + default: + assert(!"invalid object-type"); + break; + } + return a; +} + +extern void object_get(const struct attrib * a, variant * value, object_type * type); diff --git a/src/common/attributes/object.h b/src/common/attributes/object.h new file mode 100644 index 000000000..8b92d4588 --- /dev/null +++ b/src/common/attributes/object.h @@ -0,0 +1,37 @@ +/* vi: set ts=2: + +-------------------+ 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_ATTRIBUTE_OBJECT +#define H_ATTRIBUTE_OBJECT + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + TNONE = 0, TINTEGER = 1, TREAL = 2, TSTRING = 3, + TUNIT = 10, TFACTION = 11, TREGION = 12, TBUILDING = 13, TSHIP = 14, +} object_type; + +extern attrib_type at_object; + +extern struct attrib * object_create(const char * name, object_type type, variant value); +extern void object_get(const struct attrib * a, variant * value, object_type * type); +extern const char * object_name(const struct attrib * a); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/src/common/kernel/faction.c b/src/common/kernel/faction.c index ecddd7baf..8630a8be4 100644 --- a/src/common/kernel/faction.c +++ b/src/common/kernel/faction.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -178,6 +179,33 @@ checkpasswd(const faction * f, const char * passwd, boolean shortp) return false; } + +int +read_faction_reference(faction ** f, FILE * F) +{ + variant id; + if (global.data_version >= BASE36IDS_VERSION) { + char zText[10]; + fscanf(F, "%s ", zText); + id.i = atoi36(zText); + } else { + fscanf(F, "%d ", &id.i); + } + if (id.i<0) { + *f = NULL; + return AT_READ_FAIL; + } + *f = findfaction(id.i); + if (*f==NULL) ur_add(id, (void**)f, resolve_faction); + return AT_READ_OK; +} + +void +write_faction_reference(const faction * f, FILE * F) +{ + fprintf(F, "%s ", itoa36(f->no)); +} + void destroyfaction(faction * f) { diff --git a/src/common/kernel/faction.h b/src/common/kernel/faction.h index 0e286bd31..2463478c0 100644 --- a/src/common/kernel/faction.h +++ b/src/common/kernel/faction.h @@ -130,6 +130,10 @@ extern void add_enemy(struct faction * f, struct faction * enemy); extern void remove_enemy(struct faction * f, struct faction * enemy); #endif +extern void write_faction_reference(const struct faction * f, FILE * F); +extern int read_faction_reference(struct faction ** f, FILE * F); + + #ifdef SMART_INTERVALS extern void update_interval(struct faction * f, struct region * r); #endif diff --git a/src/common/kernel/movement.c b/src/common/kernel/movement.c index 4c7b583b3..621da349e 100644 --- a/src/common/kernel/movement.c +++ b/src/common/kernel/movement.c @@ -60,7 +60,7 @@ /* attributes includes */ #include #include -#include +#include #include int * storms; diff --git a/src/common/kernel/save.c b/src/common/kernel/save.c index c808fd066..53c46b5ad 100644 --- a/src/common/kernel/save.c +++ b/src/common/kernel/save.c @@ -988,32 +988,6 @@ lastturn(void) return turn; } -int -read_faction_reference(faction ** f, FILE * F) -{ - variant id; - if (global.data_version >= BASE36IDS_VERSION) { - char zText[10]; - fscanf(F, "%s ", zText); - id.i = atoi36(zText); - } else { - fscanf(F, "%d ", &id.i); - } - if (id.i<0) { - *f = NULL; - return AT_READ_FAIL; - } - *f = findfaction(id.i); - if (*f==NULL) ur_add(id, (void**)f, resolve_faction); - return AT_READ_OK; -} - -void -write_faction_reference(const faction * f, FILE * F) -{ - fprintf(F, "%s ", itoa36(f->no)); -} - void fwriteorder(FILE * F, const order * ord, const struct locale * lang) { diff --git a/src/common/kernel/save.h b/src/common/kernel/save.h index a3dd6e6ca..06bb45b01 100644 --- a/src/common/kernel/save.h +++ b/src/common/kernel/save.h @@ -56,9 +56,6 @@ extern void write_items(FILE *f, struct item *it); extern void a_read(FILE * f, struct attrib ** attribs); extern void a_write(FILE * f, const struct attrib * attribs); -extern void write_faction_reference(const struct faction * f, FILE * F); -extern int read_faction_reference(struct faction ** f, FILE * F); - extern const char * datapath(void); #if RESOURCE_CONVERSION diff --git a/src/mapper/map_units.c b/src/mapper/map_units.c index 51a9cc62a..8520fee24 100644 --- a/src/mapper/map_units.c +++ b/src/mapper/map_units.c @@ -33,7 +33,7 @@ #include #include -#include +#include /* libc includes */ #include