From 1c139de3545b8bd7d244a2d50b615ddcce03537a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 8 Jun 2008 10:17:29 +0000 Subject: [PATCH] - code to seed adamantium - cleaner definition of raw materials --- src/common/gamecode/xmlreport.c | 16 ------ src/common/kernel/resources.c | 86 ++++++++++++++------------------- src/common/kernel/resources.h | 9 ++-- src/common/kernel/xmlreader.c | 8 +++ src/common/modules/autoseed.c | 18 +++++++ src/common/modules/autoseed.h | 2 + src/eressea/korrektur.c | 20 -------- src/eressea/lua/eressea.cpp | 1 - src/eressea/lua/test.cpp | 21 +++++++- src/eressea/server.cpp | 1 - src/res/eressea/items.xml | 5 +- src/res/resources.xml | 6 +-- 12 files changed, 92 insertions(+), 101 deletions(-) diff --git a/src/common/gamecode/xmlreport.c b/src/common/gamecode/xmlreport.c index 97dff0537..e461d06c6 100644 --- a/src/common/gamecode/xmlreport.c +++ b/src/common/gamecode/xmlreport.c @@ -84,22 +84,6 @@ typedef struct xml_context { xmlNsPtr ns_xml; } xml_context; -#if 0 -static const xmlChar * -xml_s(const char * str) -{ - static xmlChar buffer[1024]; - const char * inbuf = str; - char * outbuf = (char *)buffer; - size_t inbytes = strlen(str)+1; - size_t outbytes = sizeof(buffer) - 1; - - unicode_latin1_to_utf8(outbuf, &outbytes, inbuf, &inbytes); - buffer[outbytes] = 0; - return buffer; -} -#endif - static const xmlChar * xml_i(double number) { diff --git a/src/common/kernel/resources.c b/src/common/kernel/resources.c index 9bb627490..ad910a992 100644 --- a/src/common/kernel/resources.c +++ b/src/common/kernel/resources.c @@ -60,6 +60,23 @@ update_resource(struct rawmaterial * res, double modifier) assert(res->amount>0); } +void +add_resource(region * r, int level, int base, int divisor, const resource_type * rtype) +{ + struct rawmaterial * rm = calloc(sizeof(struct rawmaterial), 1); + + rm->next = r->resources; + r->resources = rm; + rm->level = level; + rm->startlevel = level; + rm->base = base; + rm->divisor = divisor; + rm->flags = 0; + rm->type = rmt_get(rtype); + update_resource(rm, 1.0); + rm->type->terraform(rm, r); +} + void terraform_resources(region * r) { @@ -78,18 +95,7 @@ terraform_resources(region * r) if (rm) continue; if (chance(production->chance)) { - rm = calloc(sizeof(struct rawmaterial), 1); - - rm->next = r->resources; - r->resources = rm; - rm->level = dice_rand(production->startlevel); - rm->startlevel = rm->level; - rm->base = dice_rand(production->base); - rm->divisor = dice_rand(production->divisor); - rm->flags = 0; - rm->type = rmt_get(production->type); - update_resource(rm, 1.0); - rm->type->terraform(rm, r); + add_resource(r, dice_rand(production->startlevel), dice_rand(production->base), dice_rand(production->divisor), production->type); } } } @@ -131,7 +137,7 @@ visible_default(const rawmaterial *res, int skilllevel) /* resources are visible, if skill equals minimum skill to mine them * plus current level of difficulty */ { - const struct item_type * itype = olditemtype[res->type->_itype]; + const struct item_type * itype = res->type->rtype->itype; if (res->level<=1 && res->level + itype->construction->minskill <= skilllevel+1) { assert (res->amount>0); return res->amount; @@ -168,33 +174,6 @@ rm_get(region * r, const struct resource_type * rtype) return rm; } -struct rawmaterial_type rm_stones = { - "rm_stone", - I_STONE, NULL, - terraform_default, - NULL, - use_default, - visible_default -}; - -struct rawmaterial_type rm_iron = { - "rm_iron", - I_IRON, NULL, - terraform_default, - NULL, - use_default, - visible_default -}; - -struct rawmaterial_type rm_laen = { - "rm_laen", - I_LAEN, NULL, - terraform_default, - NULL, - use_default, - visible_default -}; - struct rawmaterial_type * rawmaterialtypes = 0; struct rawmaterial_type * @@ -213,18 +192,23 @@ rmt_get(const struct resource_type * rtype) return rmt; } +struct rawmaterial_type * +rmt_create(const struct resource_type * rtype, const char * name) +{ + rawmaterial_type * rmtype = malloc(sizeof(rawmaterial_type)); + rmtype->name = strdup(name); + rmtype->rtype = rtype; + rmtype->terraform = terraform_default; + rmtype->update = NULL; + rmtype->use = use_default; + rmtype->visible = visible_default; + rmtype->next = rawmaterialtypes; + rawmaterialtypes = rmtype; + return rmtype; +} + static void add_rawmaterial(struct rawmaterial_type * rmtype) { - rmtype->rtype = item2resource(olditemtype[rmtype->_itype]); - rmtype->next = rawmaterialtypes; - rawmaterialtypes = rmtype; -} - -void -init_rawmaterials(void) -{ - add_rawmaterial(&rm_stones); - add_rawmaterial(&rm_iron); - add_rawmaterial(&rm_laen); + rmtype->rtype = rmtype->rtype; } diff --git a/src/common/kernel/resources.h b/src/common/kernel/resources.h index 49eb2a24b..9c99ad6f8 100644 --- a/src/common/kernel/resources.h +++ b/src/common/kernel/resources.h @@ -32,8 +32,7 @@ typedef struct rawmaterial { } rawmaterial; typedef struct rawmaterial_type { - const char * name; - item_t _itype; /* what you'll be producing. hack - needs resource_type */ + char * name; const struct resource_type * rtype; void (*terraform) (struct rawmaterial *, const struct region *); @@ -52,13 +51,11 @@ extern void terraform_resources(struct region * r); extern void read_resources(struct region * r); extern void write_resources(struct region * r); extern struct rawmaterial * rm_get(struct region *, const struct resource_type *); -extern void init_rawmaterials(void); extern struct rawmaterial_type * rmt_find(const char * str); extern struct rawmaterial_type * rmt_get(const struct resource_type *); -extern struct rawmaterial_type rm_stones; -extern struct rawmaterial_type rm_iron; -extern struct rawmaterial_type rm_laen; +extern void add_resource(struct region * r, int level, int base, int divisor, const struct resource_type * rtype); +extern struct rawmaterial_type * rmt_create(const struct resource_type * rtype, const char * name); #ifdef __cplusplus } diff --git a/src/common/kernel/xmlreader.c b/src/common/kernel/xmlreader.c index eef649ccf..5464bfcb9 100644 --- a/src/common/kernel/xmlreader.c +++ b/src/common/kernel/xmlreader.c @@ -21,6 +21,7 @@ without prior permission by the authors of Eressea. #include "message.h" #include "race.h" #include "region.h" +#include "resources.h" #include "ship.h" #include "terrain.h" #include "skill.h" @@ -950,6 +951,13 @@ parse_resources(xmlDocPtr doc) if (name) xmlFree(name); if (appearance) xmlFree(appearance); + name = xmlGetProp(node, BAD_CAST "material"); + if (name) { + rmt_create(rtype, (const char *)name); + xmlFree(name); + } + + if (gamecode_enabled) { /* reading eressea/resources/resource/function */ xpath->node = node; diff --git a/src/common/modules/autoseed.c b/src/common/modules/autoseed.c index 7b590ccfa..58cfe0f37 100644 --- a/src/common/modules/autoseed.c +++ b/src/common/modules/autoseed.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,10 @@ #include #include +#include + /* util includes */ +#include #include #include #include @@ -64,6 +68,20 @@ random_terrain(boolean distribution) return terrain; } +int +seed_adamantium(region * r, int base) +{ + const resource_type * rtype = rt_find("adamantium"); + rawmaterial * rm; + for (rm = r->resources;rm;rm=rm->next) { + if (rm->type->rtype==rtype) break; + } + if (!rm) { + add_resource(r, 1, base, 150, rtype); + } + return 0; +} + static int count_demand(const region *r) diff --git a/src/common/modules/autoseed.h b/src/common/modules/autoseed.h index a3ae1dca0..f16cd0560 100644 --- a/src/common/modules/autoseed.h +++ b/src/common/modules/autoseed.h @@ -38,6 +38,8 @@ extern void get_island(struct region * root, struct region_list ** rlist); extern int fix_demand(struct region *r); extern const struct terrain_type * random_terrain(boolean use_distribution); +extern int seed_adamantium(struct region * r, int base); + #ifdef __cplusplus } #endif diff --git a/src/eressea/korrektur.c b/src/eressea/korrektur.c index 722452bb9..2a82f0f00 100644 --- a/src/eressea/korrektur.c +++ b/src/eressea/korrektur.c @@ -376,25 +376,6 @@ growing_trees(void) return 0; } -static int fix_adamantium(void) -{ - faction * f; - for (f=factions;f;f=f->next) { - const struct item_type * itype; - int o = 1; - int p = (f->no % 5)?1:0; - int a = (f->no % 5)?0:1; - - itype = it_find("adamantium"); - i_change(&f->items, itype, o-i_get(f->items, itype)); - itype = it_find("adamantiumaxe"); - i_change(&f->items, itype, a-i_get(f->items, itype)); - itype = it_find("adamantiumplate"); - i_change(&f->items, itype, p-i_get(f->items, itype)); - } - return 0; -} - #include #include typedef struct gate_data { @@ -914,7 +895,6 @@ korrektur(void) do_once("asfi", &fix_astral_firewalls); fix_astralplane(); fix_toads(); - do_once("admt", &fix_adamantium); /* fix_heroes(); */ verify_owners(false); diff --git a/src/eressea/lua/eressea.cpp b/src/eressea/lua/eressea.cpp index 9af304270..a4a9303bc 100644 --- a/src/eressea/lua/eressea.cpp +++ b/src/eressea/lua/eressea.cpp @@ -217,7 +217,6 @@ is_function(struct lua_State * luaState, const char * fname) if (type(fun)==LUA_TFUNCTION) { return true; } - log_warning(("Lua global object %s is not a function, type is %u\n", fname, type(fun))); if (type(fun)!=LUA_TNIL) { log_warning(("Lua global object %s is not a function, type is %u\n", fname, type(fun))); } diff --git a/src/eressea/lua/test.cpp b/src/eressea/lua/test.cpp index acc080fa3..e088d9344 100644 --- a/src/eressea/lua/test.cpp +++ b/src/eressea/lua/test.cpp @@ -20,8 +20,10 @@ using namespace luabind; #include #include -#include #include +#include +#include +#include static const char * loc_getskill(const char * loc, const char * locstring) @@ -41,6 +43,22 @@ loc_getkeyword(const char * loc, const char * locstring) return keywords[result]; } +static void +adamantium(region * r) +{ + region_list *rp, *rlist = NULL; + get_island(r, &rlist); + + for (rp=rlist;rp;rp=rp->next) { + region * ri = rp->data; + if (ri->terrain==newterrain(T_MOUNTAIN)) { + int base = 1 << (rng_int() % 4); + seed_adamantium(r, base); + } + } + free_regionlist(rlist); +} + void bind_test(lua_State * L) { @@ -48,6 +66,7 @@ bind_test(lua_State * L) def("loc_skill", &loc_getskill), def("loc_keyword", &loc_getkeyword), def("reorder_units", &reorder_units), + def("seed_adamantium", &adamantium), def("rng_int", &rng_int) ]; } diff --git a/src/eressea/server.cpp b/src/eressea/server.cpp index 80efccf13..7e46506db 100644 --- a/src/eressea/server.cpp +++ b/src/eressea/server.cpp @@ -236,7 +236,6 @@ game_init(void) init_archetypes(); init_attributes(); init_itemtypes(); - init_rawmaterials(); init_gmcmd(); #if INFOCMD_MODULE diff --git a/src/res/eressea/items.xml b/src/res/eressea/items.xml index a80402dc2..33b0ca2ca 100644 --- a/src/res/eressea/items.xml +++ b/src/res/eressea/items.xml @@ -164,7 +164,7 @@ - + @@ -176,7 +176,8 @@ - + + diff --git a/src/res/resources.xml b/src/res/resources.xml index 9347c5451..4bdc0d38e 100644 --- a/src/res/resources.xml +++ b/src/res/resources.xml @@ -79,7 +79,7 @@ - + @@ -91,7 +91,7 @@ - + @@ -100,7 +100,7 @@ - +