- added a storage interface for Lua- adde towershields for e2k9

- added new test for storage
This commit is contained in:
Enno Rehling 2009-06-02 22:12:29 +00:00
parent 93e34d979d
commit 7b24248ac2
21 changed files with 293 additions and 23 deletions

View File

@ -12,6 +12,7 @@
#include "eressea/tolua/bind_message.c" #include "eressea/tolua/bind_message.c"
#include "eressea/tolua/bind_hashtable.c" #include "eressea/tolua/bind_hashtable.c"
#include "eressea/tolua/bind_gmtool.c" #include "eressea/tolua/bind_gmtool.c"
#include "eressea/tolua/bind_storage.c"
#include "eressea/tolua/helpers.c" #include "eressea/tolua/helpers.c"
#endif #endif

View File

@ -211,6 +211,17 @@ HelpMask(void)
* if this returns a non-zero value, then you cannot give those modes to a * if this returns a non-zero value, then you cannot give those modes to a
* faction unless they are in the same alliance. * faction unless they are in the same alliance.
*/ */
int
HelpMask(void)
{
static int help_mask = 0;
if (help_mask==0) {
help_mask = get_param_int(global.parameters, "rules.help.mask", HELP_ALL);
}
return help_mask;
}
int int
AllianceRestricted(void) AllianceRestricted(void)
{ {

View File

@ -736,6 +736,14 @@ mod_elves_only(const unit * u, const region * r, skill_t sk, int value)
return -118; return -118;
} }
static int
mod_dwarves_only(const unit * u, const region * r, skill_t sk, int value)
{
if (u->race == new_race[RC_DWARF]) return value;
unused(r);
return -118;
}
static void static void
init_olditems(void) init_olditems(void)
{ {
@ -1159,6 +1167,7 @@ void
register_resources(void) register_resources(void)
{ {
register_function((pf_generic)mod_elves_only, "mod_elves_only"); register_function((pf_generic)mod_elves_only, "mod_elves_only");
register_function((pf_generic)mod_dwarves_only, "mod_dwarves_only");
register_function((pf_generic)res_changeitem, "changeitem"); register_function((pf_generic)res_changeitem, "changeitem");
register_function((pf_generic)res_changeperson, "changeperson"); register_function((pf_generic)res_changeperson, "changeperson");
register_function((pf_generic)res_changepeasants, "changepeasants"); register_function((pf_generic)res_changepeasants, "changepeasants");

View File

@ -2159,7 +2159,7 @@ is_familiar(const unit *u)
} }
static void static void
write_unit(const attrib * a, struct storage * store) a_write_unit(const attrib * a, struct storage * store)
{ {
unit * u = (unit*)a->data.v; unit * u = (unit*)a->data.v;
write_unit_reference(u, store); write_unit_reference(u, store);
@ -2418,7 +2418,7 @@ attrib_type at_familiarmage = {
NULL, NULL,
NULL, NULL,
age_unit, age_unit,
write_unit, a_write_unit,
read_magician, read_magician,
ATF_UNIQUE ATF_UNIQUE
}; };
@ -2428,7 +2428,7 @@ attrib_type at_familiar = {
NULL, NULL,
NULL, NULL,
age_unit, age_unit,
write_unit, a_write_unit,
read_familiar, read_familiar,
ATF_UNIQUE ATF_UNIQUE
}; };
@ -2438,7 +2438,7 @@ attrib_type at_clonemage = {
NULL, NULL,
NULL, NULL,
age_unit, age_unit,
write_unit, a_write_unit,
read_magician, read_magician,
ATF_UNIQUE ATF_UNIQUE
}; };
@ -2448,7 +2448,7 @@ attrib_type at_clone = {
NULL, NULL,
NULL, NULL,
age_unit, age_unit,
write_unit, a_write_unit,
read_clone, read_clone,
ATF_UNIQUE ATF_UNIQUE
}; };

View File

@ -646,7 +646,7 @@ writeorder(struct storage * store, const struct order * ord, const struct locale
} }
unit * unit *
readunit(struct storage * store) read_unit(struct storage * store)
{ {
skill_t sk; skill_t sk;
unit * u; unit * u;
@ -815,7 +815,7 @@ readunit(struct storage * store)
} }
void void
writeunit(struct storage * store, const unit * u) write_unit(struct storage * store, const unit * u)
{ {
order * ord; order * ord;
int i, p = 0; int i, p = 0;
@ -1521,7 +1521,7 @@ readgame(const char * filename, int mode, int backup)
up = &r->units; up = &r->units;
while (--p >= 0) { while (--p >= 0) {
unit * u = readunit(store); unit * u = read_unit(store);
sc_mage * mage; sc_mage * mage;
assert(u->region==NULL); assert(u->region==NULL);
@ -1718,7 +1718,7 @@ writegame(const char *filename, int mode)
store->w_int(store, listlen(r->units)); store->w_int(store, listlen(r->units));
store->w_brk(store); store->w_brk(store);
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
writeunit(store, u); write_unit(store, u);
} }
} }
store->w_brk(store); store->w_brk(store);

View File

@ -41,13 +41,6 @@ int writegame(const char *filename, int mode);
extern void rsf(FILE * F, char *s, size_t len); extern void rsf(FILE * F, char *s, size_t len);
#define IO_READ 0x01
#define IO_WRITE 0x02
#define IO_BINARY 0x04
#define IO_TEXT 0x08
#define IO_DEFAULT IO_BINARY
/* Versionsänderungen: */ /* Versionsänderungen: */
extern int data_version; extern int data_version;
extern const char *xmlfile; extern const char *xmlfile;
@ -59,6 +52,9 @@ extern int lastturn(void);
extern void read_items(struct storage * store, struct item **it); extern void read_items(struct storage * store, struct item **it);
extern void write_items(struct storage * store, struct item *it); extern void write_items(struct storage * store, struct item *it);
extern void write_unit(struct storage * store, const struct unit * u);
extern struct unit * read_unit(struct storage * store);
extern const char * datapath(void); extern const char * datapath(void);
extern int a_readint(struct attrib * a, struct storage * store); extern int a_readint(struct attrib * a, struct storage * store);

View File

@ -1427,7 +1427,7 @@ create_unit(region * r, faction * f, int number, const struct race *urace, int i
/* zuerst in die Region setzen, da zb Drachennamen den Regionsnamen /* zuerst in die Region setzen, da zb Drachennamen den Regionsnamen
* enthalten */ * enthalten */
move_unit(u, r, NULL); if (r) move_unit(u, r, NULL);
/* u->race muss bereits gesetzt sein, wird für default-hp gebraucht */ /* u->race muss bereits gesetzt sein, wird für default-hp gebraucht */
/* u->region auch */ /* u->region auch */

View File

@ -33,6 +33,12 @@ typedef struct storage {
void * userdata; void * userdata;
} storage; } storage;
#define IO_READ 0x01
#define IO_WRITE 0x02
#define IO_BINARY 0x04
#define IO_TEXT 0x08
#define IO_DEFAULT IO_BINARY
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -67,7 +67,7 @@
/> />
<Tool <Tool
Name="VCLinkerTool" Name="VCLinkerTool"
AdditionalDependencies="libxml2.lib pdcurses.lib lua5.1_d.lib tolua++.lib" AdditionalDependencies="libxml2.lib pdcurses.lib lua5.1_d.lib tolua.lib"
OutputFile="$(OutDir)\eressea.exe" OutputFile="$(OutDir)\eressea.exe"
LinkIncremental="2" LinkIncremental="2"
GenerateDebugInformation="true" GenerateDebugInformation="true"
@ -143,7 +143,7 @@
/> />
<Tool <Tool
Name="VCLinkerTool" Name="VCLinkerTool"
AdditionalDependencies="libxml2.lib pdcurses.lib lua5.1.lib tolua++.lib" AdditionalDependencies="libxml2.lib pdcurses.lib lua5.1.lib tolua.lib"
OutputFile="$(OutDir)\eressea.exe" OutputFile="$(OutDir)\eressea.exe"
LinkIncremental="1" LinkIncremental="1"
GenerateDebugInformation="true" GenerateDebugInformation="true"

View File

@ -332,6 +332,14 @@
RelativePath=".\tolua\bind_ship.h" RelativePath=".\tolua\bind_ship.h"
> >
</File> </File>
<File
RelativePath=".\tolua\bind_storage.c"
>
</File>
<File
RelativePath=".\tolua\bind_storage.h"
>
</File>
<File <File
RelativePath=".\tolua\bind_unit.c" RelativePath=".\tolua\bind_unit.c"
> >

View File

@ -57,6 +57,7 @@
#include <util/log.h> #include <util/log.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/storage.h>
#include <libxml/encoding.h> #include <libxml/encoding.h>

View File

@ -111,6 +111,7 @@
#include "tolua/bind_building.h" #include "tolua/bind_building.h"
#include "tolua/bind_region.h" #include "tolua/bind_region.h"
#include "tolua/bind_gmtool.h" #include "tolua/bind_gmtool.h"
#include "tolua/bind_storage.h"
#endif // BINDINGS_TOLUA #endif // BINDINGS_TOLUA
#ifdef BINDINGS_LUABIND #ifdef BINDINGS_LUABIND
@ -301,6 +302,7 @@ lua_init(void)
tolua_message_open(L); tolua_message_open(L);
tolua_hashtable_open(L); tolua_hashtable_open(L);
tolua_gmtool_open(L); tolua_gmtool_open(L);
tolua_storage_open(L);
#endif #endif
#ifdef BINDINGS_LUABIND #ifdef BINDINGS_LUABIND

View File

@ -116,6 +116,20 @@ tolua_faction_get_id(lua_State* tolua_S)
return 1; return 1;
} }
static int
tolua_faction_set_id(lua_State* tolua_S)
{
faction * self = (faction *)tolua_tousertype(tolua_S, 1, 0);
int id = (int)tolua_tonumber(tolua_S, 2, 0);
if (findfaction(id)==NULL) {
renumber_faction(self, id);
lua_pushboolean(tolua_S, 1);
} else {
lua_pushboolean(tolua_S, 0);
}
return 1;
}
static int static int
tolua_faction_get_age(lua_State* tolua_S) tolua_faction_get_age(lua_State* tolua_S)
{ {
@ -436,7 +450,7 @@ tolua_faction_open(lua_State* tolua_S)
tolua_variable(tolua_S, "race", tolua_faction_get_race, tolua_faction_set_race); tolua_variable(tolua_S, "race", tolua_faction_get_race, tolua_faction_set_race);
tolua_variable(tolua_S, "alliance", tolua_faction_get_alliance, tolua_faction_set_alliance); tolua_variable(tolua_S, "alliance", tolua_faction_get_alliance, tolua_faction_set_alliance);
tolua_variable(tolua_S, "score", tolua_faction_get_score, NULL); tolua_variable(tolua_S, "score", tolua_faction_get_score, NULL);
tolua_variable(tolua_S, "id", tolua_faction_get_id, NULL); tolua_variable(tolua_S, "id", tolua_faction_get_id, tolua_faction_set_id);
tolua_variable(tolua_S, "age", tolua_faction_get_age, tolua_faction_set_age); tolua_variable(tolua_S, "age", tolua_faction_get_age, tolua_faction_set_age);
tolua_variable(tolua_S, "options", tolua_faction_get_options, NULL); tolua_variable(tolua_S, "options", tolua_faction_get_options, NULL);
tolua_variable(tolua_S, "flags", tolua_faction_get_flags, NULL); tolua_variable(tolua_S, "flags", tolua_faction_get_flags, NULL);

View File

@ -51,6 +51,26 @@ static int tolua_ship_get_name(lua_State* tolua_S)
return 1; return 1;
} }
static int tolua_ship_get_region(lua_State* tolua_S)
{
ship* self = (ship*) tolua_tousertype(tolua_S, 1, 0);
if (self) {
tolua_pushusertype(tolua_S, self->region, "region");
return 1;
}
return 0;
}
static int tolua_ship_set_region(lua_State* tolua_S)
{
ship* self = (ship*) tolua_tousertype(tolua_S, 1, 0);
region * r = (region*) tolua_tousertype(tolua_S, 1, 0);
if (self) {
move_ship(self, self->region, r, NULL);
}
return 0;
}
static int tolua_ship_set_name(lua_State* tolua_S) static int tolua_ship_set_name(lua_State* tolua_S)
{ {
ship* self = (ship*)tolua_tousertype(tolua_S, 1, 0); ship* self = (ship*)tolua_tousertype(tolua_S, 1, 0);
@ -119,6 +139,7 @@ tolua_ship_open(lua_State* tolua_S)
tolua_variable(tolua_S, "id", tolua_ship_get_id, NULL); tolua_variable(tolua_S, "id", tolua_ship_get_id, NULL);
tolua_variable(tolua_S, "name", tolua_ship_get_name, tolua_ship_set_name); tolua_variable(tolua_S, "name", tolua_ship_get_name, tolua_ship_set_name);
tolua_variable(tolua_S, "units", tolua_ship_get_units, NULL); tolua_variable(tolua_S, "units", tolua_ship_get_units, NULL);
tolua_variable(tolua_S, "region", tolua_ship_get_region, tolua_ship_set_region);
#ifdef TODO #ifdef TODO
.property("type", &ship_gettype) .property("type", &ship_gettype)
.property("weight", &ship_getweight) .property("weight", &ship_getweight)

View File

@ -0,0 +1,143 @@
/* vi: set ts=2:
+-------------------+
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
| (c) 1998 - 2008 | Katja Zedel <katze@felidae.kn-bremen.de>
| | Henning Peters <faroul@beyond.kn-bremen.de>
+-------------------+
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <config.h>
#include "bind_storage.h"
#include <util/storage.h>
#include <kernel/save.h>
#include <kernel/textstore.h>
#include <kernel/binarystore.h>
#include <math.h>
#include <lua.h>
#include <tolua.h>
static int
tolua_storage_create(lua_State* tolua_S)
{
const char * filename = tolua_tostring(tolua_S, 1, 0);
const char * type = tolua_tostring(tolua_S, 2, "rb");
storage * store = 0;
int mode = IO_READ;
if (strchr(type, 't')) {
store = malloc(sizeof(text_store));
memcpy(store, &text_store, sizeof(text_store));
} else {
store = malloc(sizeof(binary_store));
memcpy(store, &binary_store, sizeof(binary_store));
}
if (strchr(type, 'r')) mode = IO_READ;
if (strchr(type, 'w')) mode = IO_WRITE;
store->open(store, filename, mode);
tolua_pushusertype(tolua_S, (void*)store, "storage");
return 1;
}
static int
tolua_storage_read_unit(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
struct unit * u = read_unit(self);
tolua_pushusertype(tolua_S, (void*)u, "unit");
return 1;
}
static int
tolua_storage_write_unit(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
struct unit * u = (struct unit *)tolua_tousertype(tolua_S, 2, 0);
write_unit(self, u);
return 0;
}
static int
tolua_storage_read_float(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
float num = self->r_flt(self);
tolua_pushnumber(tolua_S, (lua_Number)num);
return 1;
}
static int
tolua_storage_read_int(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
int num = self->r_int(self);
tolua_pushnumber(tolua_S, (lua_Number)num);
return 1;
}
static int
tolua_storage_write(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
if (tolua_isnumber(tolua_S, 2, 0, 0)) {
lua_Number num = tolua_tonumber(tolua_S, 2, 0);
double n;
if (modf(num, &n)==0.0) {
self->w_int(self, (int)num);
} else {
self->w_flt(self, (float)num);
}
}
return 0;
}
static int
tolua_storage_tostring(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
char name[64];
snprintf(name, sizeof(name), "<storage enc=%d ver=%d>", self->encoding, self->version);
lua_pushstring(tolua_S, name);
return 1;
}
static int
tolua_storage_close(lua_State *tolua_S)
{
storage * self = (storage *)tolua_tousertype(tolua_S, 1, 0);
self->close(self);
return 0;
}
void
tolua_storage_open(lua_State* tolua_S)
{
/* register user types */
tolua_usertype(tolua_S, "storage");
tolua_module(tolua_S, NULL, 0);
tolua_beginmodule(tolua_S, NULL);
{
tolua_cclass(tolua_S, "storage", "storage", "", NULL);
tolua_beginmodule(tolua_S, "storage");
{
tolua_function(tolua_S, "__tostring", tolua_storage_tostring);
tolua_function(tolua_S, "write", tolua_storage_write);
tolua_function(tolua_S, "read_int", tolua_storage_read_int);
tolua_function(tolua_S, "read_float", tolua_storage_read_float);
tolua_function(tolua_S, "write_unit", tolua_storage_write_unit);
tolua_function(tolua_S, "read_unit", tolua_storage_read_unit);
tolua_function(tolua_S, "close", tolua_storage_close);
tolua_function(tolua_S, "create", tolua_storage_create);
}
tolua_endmodule(tolua_S);
}
tolua_endmodule(tolua_S);
}

View File

@ -0,0 +1,22 @@
/* vi: set ts=2:
+-------------------+
| | Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
| (c) 1998 - 2008 | Katja Zedel <katze@felidae.kn-bremen.de>
| | Henning Peters <faroul@beyond.kn-bremen.de>
+-------------------+
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#ifdef __cplusplus
extern "C" {
#endif
struct lua_State;
void tolua_storage_open(struct lua_State *tolua_S);
#ifdef __cplusplus
}
#endif

View File

@ -53,6 +53,7 @@ without prior permission by the authors of Eressea.
#include <util/log.h> #include <util/log.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/rng.h> #include <util/rng.h>
#include <util/storage.h>
#include <tolua.h> #include <tolua.h>
#include <lua.h> #include <lua.h>

View File

@ -166,7 +166,7 @@
</construction> </construction>
</building> </building>
<building name="castle" capacity="1" protection="yes" taxes="true"> <building name="castle" capacity="1" protection="yes">
<function name="name" value="castle_name"/> <function name="name" value="castle_name"/>
<construction skill="building" minskill="1" maxsize="2" reqsize="1"> <construction skill="building" minskill="1" maxsize="2" reqsize="1">
<requirement type="stone" quantity="1"/> <requirement type="stone" quantity="1"/>

View File

@ -1,6 +1,16 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<resources> <resources>
<resource name="towershield">
<item weight="200" score="60">
<construction skill="armorer" minskill="6" reqsize="1">
<modifier function="mod_dwarves_only"/>
<requirement type="iron" quantity="2"/>
</construction>
<armor ac="2" penalty="-0.20" magres="0.0" shield="yes" />
</item>
</resource>
<resource name="p2" appearance="vial"> <resource name="p2" appearance="vial">
<item weight="0" score="30"> <item weight="0" score="30">
<function name="use" value="usepotion"/> <function name="use" value="usepotion"/>

View File

@ -868,7 +868,7 @@
<arg name="herb" type="resource"/> <arg name="herb" type="resource"/>
</type> </type>
<text locale="de">"Auf dem Markt werden $resource($product,0) und $resource($herb,0) feilgeboten."</text> <text locale="de">"Auf dem Markt werden $resource($product,0) und $resource($herb,0) feilgeboten."</text>
<text locale="de">"The local market offers $resource($product,0) and $resource($herb,0)."</text> <text locale="en">"The local market offers $resource($product,0) and $resource($herb,0)."</text>
</message> </message>
<message name="newbie_password" section="events"> <message name="newbie_password" section="events">
<type> <type>

View File

@ -345,6 +345,30 @@ local function spells_csv()
fail = 1 fail = 1
end end
function test_storage()
free_game()
local r = region.create(0, 0, "plain")
local f = faction.create("enno@eressea.de", "human", "de")
f.id = 42
local u = unit.create(f, r, 1)
u:add_item("money", u.number * 100)
store = storage.create("test.unit.dat", "wb")
assert(store)
store:write_unit(u)
store:close()
free_game()
-- recreate world:
r = region.create(0, 0, "plain")
f = faction.create("enno@eressea.de", "human", "de")
f.id = 42
store = storage.create("test.unit.dat", "rb")
assert(store)
u = store:read_unit()
store:close()
assert(u)
assert(u:get_item("money") == u.number * 100)
end
loadscript("extensions.lua") loadscript("extensions.lua")
tests = { tests = {
["alliance"] = test_alliance, ["alliance"] = test_alliance,
@ -364,9 +388,10 @@ tests = {
["spells"] = test_spells ["spells"] = test_spells
} }
mytests = { mytests = {
["storage"] = test_storage
} }
fail = 0 fail = 0
for k, v in pairs(tests) do for k, v in pairs(mytests) do
local status, err = pcall(v) local status, err = pcall(v)
if not status then if not status then
fail = fail + 1 fail = fail + 1