From cb9106473b890380357d00003766760fa6098ace Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 29 Oct 2005 14:12:46 +0000 Subject: [PATCH] Special non-item resources like hp and peasant can now be defined externally. removed all the hardcoded armor from the source. --- src/common/gamecode/luck.c | 1 - src/common/gamecode/report.c | 7 +- src/common/items/weapons.c | 45 --------- src/common/kernel/item.c | 79 ++++------------ src/common/kernel/item.h | 40 ++------ src/common/kernel/message.c | 3 - src/common/kernel/pool.c | 11 --- src/common/kernel/xmlreader.c | 2 +- src/eressea/lua/bindings.h | 1 + src/eressea/lua/script.cpp | 168 +++++++++++++++++++++++++++++++++- src/eressea/lua/spell.cpp | 100 -------------------- src/eressea/server.cpp | 1 + src/res/races.xml | 30 +++--- src/res/resources.xml | 72 ++++++++++++++- src/res/spells.xml | 46 +++++----- src/scripts/familiars.lua | 31 +++++++ 16 files changed, 339 insertions(+), 298 deletions(-) diff --git a/src/common/gamecode/luck.c b/src/common/gamecode/luck.c index 69d011b81..3f90db880 100644 --- a/src/common/gamecode/luck.c +++ b/src/common/gamecode/luck.c @@ -140,7 +140,6 @@ lucky_magic_item(const unit *u) olditemtype[I_RING_OF_NIMBLEFINGER], olditemtype[I_TROLLBELT], olditemtype[I_TACTICCRYSTAL], - olditemtype[I_RING_OF_REGENERATION], olditemtype[I_SACK_OF_CONSERVATION], }; diff --git a/src/common/gamecode/report.c b/src/common/gamecode/report.c index 9c978edc2..62290e073 100644 --- a/src/common/gamecode/report.c +++ b/src/common/gamecode/report.c @@ -1123,14 +1123,15 @@ describe(FILE * F, const region * r, int partial, faction * f) /* peasants & silver */ if (rpeasants(r)) { - bufp += sprintf(bufp, ", %d", rpeasants(r)); + int n = rpeasants(r); + bufp += sprintf(bufp, ", %d", n); if(fval(r, RF_ORCIFIED)) { strcpy(bufp++, " "); - bufp += strxcpy(bufp, LOC(f->locale, rpeasants(r)==1?"rc_orc":"rc_orc_p")); + bufp += strxcpy(bufp, LOC(f->locale, n==1?"rc_orc":"rc_orc_p")); } else { strcpy(bufp++, " "); - bufp += strxcpy(bufp, LOC(f->locale, resourcename(oldresourcetype[R_PEASANTS], rpeasants(r)!=1))); + bufp += strxcpy(bufp, LOC(f->locale, n==1?"peasant":"peasant_p")); } if (rmoney(r) && partial == 0) { diff --git a/src/common/items/weapons.c b/src/common/items/weapons.c index 45b5b1296..5ea6a007c 100644 --- a/src/common/items/weapons.c +++ b/src/common/items/weapons.c @@ -59,7 +59,6 @@ enum { WP_LONGBOW, WP_SPEAR, WP_LANCE, - WP_RUSTY_HALBERD, WP_NONE, WP_MAX }; @@ -112,8 +111,6 @@ static weapondata weapontable[WP_MAX + 1] = {0.00, "1d10+0", "1d12+2", I_SPEAR, SK_SPEAR, 0, 0, false, false, { RL_NONE, 0}, PIERCE }, /* Lanze */ {0.00, "1d5", "2d6+5", I_LANCE, SK_SPEAR, 0, -2, false, false, { RL_NONE, 0}, PIERCE }, - /* Rostige Hellebarde */ - {0.00, "2d6", "2d6", I_RUSTY_HALBERD, SK_SPEAR, -2, 1, false, false, { RL_NONE, 0}, CUT }, /* Unbewaffnet */ {0.00, "1d5+0", "1d6+0", I_WOOD, SK_MELEE, 0, 0, false, false, { RL_NONE, 0}, BASH }, /* Dummy */ @@ -238,47 +235,6 @@ attack_catapult(const troop * at, int * casualties, int row) return false; /* keine weitren attacken */ } -enum { - AR_PLATE, - AR_CHAIN, - AR_RUSTY_CHAIN, - AR_SHIELD, - AR_RUSTY_SHIELD, - AR_MAX -}; - -typedef struct armordata { - double penalty; - double magres; - int prot; - boolean shield; - item_t item; -} armordata; - -static armordata armortable[] = -/* penalty; magres; prot; shield; item; */ -{ - { 0.30, 0.00, 5, 0, I_PLATE_ARMOR}, - { 0.15, 0.00, 3, 0, I_CHAIN_MAIL}, - { 0.30, 0.00, 3, 0, I_RUSTY_CHAIN_MAIL}, - {-0.15, 0.00, 1, 1, I_SHIELD}, - { 0.00, 0.00, 1, 1, I_RUSTY_SHIELD}, - { 0.00, 0.00, 0, 0, MAX_ITEMS } -}; - -static void -init_oldarmor(void) -{ - armordata * ad = armortable; - for (;ad->item!=MAX_ITEMS;++ad) { - item_type * itype = olditemtype[ad->item]; - unsigned int flags = 0; - - if (ad->shield) flags |= ATF_SHIELD; - new_armortype(itype, ad->penalty, ad->magres, ad->prot, flags); - } -} - static void init_oldweapons(void) { @@ -352,5 +308,4 @@ void init_weapons(void) { init_oldweapons(); - init_oldarmor(); } diff --git a/src/common/kernel/item.c b/src/common/kernel/item.c index c2f3e2c33..f3c8237f2 100644 --- a/src/common/kernel/item.c +++ b/src/common/kernel/item.c @@ -584,7 +584,6 @@ give_horses(const unit * s, const unit * d, const item_type * itype, int n, stru return true; } -#define R_MAXUNITRESOURCE R_HITPOINTS #define R_MINOTHER R_SILVER #define R_MINHERB R_PLAIN_1 #define R_MINPOTION R_FAST @@ -788,14 +787,6 @@ static t_item itemdata[MAXITEMS] = { {"Bogen", "Bögen", "Bogen", "Bögen"}, IS_PRODUCT, SK_WEAPONSMITH, 2, {0, 1, 0, 0, 0, 0}, 100, 0, 0, NULL }, - { /* I_CHAIN_MAIL */ - {"Kettenhemd", "Kettenhemden", "Kettenhemd", "Kettenhemden"}, - IS_PRODUCT, SK_ARMORER, 3, {3, 0, 0, 0, 0, 0}, 200, 0, 0, NULL - }, - { /* I_PLATE_ARMOR */ - {"Plattenpanzer", "Plattenpanzer", "Plattenpanzer", "Plattenpanzer"}, - IS_PRODUCT, SK_ARMORER, 4, {5, 0, 0, 0, 0, 0}, 400, 0, 0, NULL - }, { /* I_BALM */ {"Balsam", "Balsam", "Balsam", "Balsam"}, IS_LUXURY, 0, 0, {0, 0, 0, 0, 0, 0}, 200, 4, 0, NULL @@ -863,10 +854,6 @@ static t_item itemdata[MAXITEMS] = { {"Laen", "Laen", "Laen", "Laen"}, IS_RESOURCE, SK_MINING, 7, {0, 0, 0, 0, 0, 0}, 200, 0, 0, NULL }, - { /* I_SHIELD 42 */ - {"Schild", "Schilde", "Schild", "Schilde"}, - IS_PRODUCT, SK_ARMORER, 2, {1, 0, 0, 0, 0, 0}, 100, 0, 0, NULL - }, { /* I_LANCE 44 */ {"Lanze", "Lanzen", "Lanze", "Lanzen"}, IS_PRODUCT, SK_WEAPONSMITH, 2, {0, 2, 0, 0, 0, 0}, 200, 0, 0, NULL @@ -925,45 +912,28 @@ static t_item itemdata[MAXITEMS] = { "Seeschlangenkopf", "Seeschlangenköpfe"}, IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 500, 0, 0, NULL }, + { /* I_SPHERE_OF_INVISIBILITY */ + {"Sphäre der Unsichtbarkeit", "Sphären der Unsichtbarkeit", "", ""}, + IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL + }, + { /* I_TOADSLIME 73 */ + {"Tiegel mit Krötenschleim", "Tiegel mit Krötenschleim", + "", ""}, + IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL + }, + { /* I_BAG_OF_HOLDING */ + {"Zauberbeutel", "Zauberbeutel", "Zauberbeutel", "Zauberbeutel"}, + IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, FL_ITEM_NOTINBAG|FL_ITEM_NOTLOST, NULL + }, + { /* I_SACK_OF_CONSERVATION */ + {"Magischer Kräuterbeutel", "Magische Kräuterbeutel", "", ""}, + IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL + }, { /* I_TACTICCRYSTAL 71 */ {"Traumauge", "Traumaugen", "", ""}, IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, &use_tacticcrystal }, - { /* I_RING_OF_REGENERATION 72 */ - {"Ring der Regeneration", "Ringe der Regeneration", - "", ""}, - IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL - }, - { /* I_TOADSLIME 73 */ - {"Tiegel mit Krötenschleim", "Tiegel mit Krötenschleim", - "", ""}, - IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL - }, - { - {"Zauberbeutel", "Zauberbeutel", "Zauberbeutel", "Zauberbeutel"}, - IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, FL_ITEM_NOTINBAG|FL_ITEM_NOTLOST, NULL - }, - { /* I_RUSTY_SHIELD 42 */ - {"Rostiger Schild", "Rostige Schilde", "Rostiger Schild", "Rostige Schilde"}, - IS_PRODUCT, SK_ARMORER, 2, {1, 0, 0, 0, 0, 0}, 100, 0, 0, NULL - }, - { /* I_RUSTY_CHAIN_MAIL */ - {"Rostiges Kettenhemd", "Rostige Kettenhemden", "Rostiges Kettenhemd", "Rostige Kettenhemden"}, - IS_PRODUCT, SK_ARMORER, 3, {3, 0, 0, 0, 0, 0}, 200, 0, 0, NULL - }, - { /* I_SACK_OF_CONSERVATION */ - {"Magischer Kräuterbeutel", "Magische Kräuterbeutel", "", ""}, - IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL - }, - { /* I_SPHERE_OF_INVISIBILITY */ - {"Sphäre der Unsichtbarkeit", "Sphären der Unsichtbarkeit", "", ""}, - IS_MAGIC, 0, 0, {0, 0, 0, 0, 0, 0}, 100, 0, 0, NULL - }, - { /* I_RUSTY_HALBERD */ - {"Rostige Hellebarde", "Rostige Hellebarden", "Rostige Hellebarde", "Rostige Hellebarden"}, - IS_PRODUCT, SK_WEAPONSMITH, 3, {1, 1, 0, 0, 0, 0}, 200, 0, 0, NULL - } }; const item_t matresource[] = { @@ -1504,7 +1474,6 @@ init_oldpotions(void) resource_type * r_silver; resource_type * r_aura; resource_type * r_permaura; -resource_type * r_peasants; resource_type * r_unit; resource_type * r_hp; @@ -1538,14 +1507,8 @@ item_score(item_t i) return 100; case I_WAGON: return 60; - case I_SHIELD: - return 30; case I_LAENSWORD: return 400; - case I_CHAIN_MAIL: - return 40; - case I_PLATE_ARMOR: - return 60; case I_BALM: case I_SPICES: case I_JEWELERY: @@ -1598,9 +1561,6 @@ init_resources(void) r_hp = new_resourcetype(&names[6], NULL, RTF_NONE); r_hp->uchange = res_changehp; - r_peasants = new_resourcetype(&names[8], NULL, RTF_NONE); - r_peasants->uchange = res_changepeasants; - r_aura = new_resourcetype(&names[10], NULL, RTF_NONE); r_aura->uchange = res_changeaura; @@ -1610,9 +1570,6 @@ init_resources(void) oldresourcetype[R_SILVER] = r_silver; oldresourcetype[R_AURA] = r_aura; oldresourcetype[R_PERMAURA] = r_permaura; - oldresourcetype[R_HITPOINTS] = r_hp; - oldresourcetype[R_PEASANTS] = r_peasants; - oldresourcetype[R_UNIT] = r_unit; /* alte typen registrieren: */ init_olditems(); @@ -1806,5 +1763,5 @@ register_resources(void) register_function((pf_generic)give_horses, "givehorses"); /* make sure noone has deleted an I_ tpe without deleting the R_ type that goes with it! */ - assert((int)I_SACK_OF_CONSERVATION == (int)R_SACK_OF_CONSERVATION); + assert((int)I_TACTICCRYSTAL == (int)R_TACTICCRYSTAL); } diff --git a/src/common/kernel/item.h b/src/common/kernel/item.h index 9c1869b4e..327e1cc59 100644 --- a/src/common/kernel/item.h +++ b/src/common/kernel/item.h @@ -240,7 +240,6 @@ extern potion_type * new_potiontype(item_type * itype, int level); extern resource_type * r_silver; extern resource_type * r_aura; extern resource_type * r_permaura; -extern resource_type * r_peasants; extern resource_type * r_unit; extern resource_type * r_hp; @@ -267,8 +266,6 @@ enum { I_CATAPULT, I_SPEAR, I_LONGBOW, - I_CHAIN_MAIL, /* 10 */ - I_PLATE_ARMOR, I_BALM, I_SPICES, I_JEWELERY, @@ -286,7 +283,6 @@ enum { I_CHASTITY_BELT, /* bleibt */ I_LAENSWORD, I_LAEN, - I_SHIELD, I_LANCE, I_MALLORN, I_DRACHENBLUT, @@ -301,22 +297,11 @@ enum { I_RUNESWORD, I_AURAKULUM, I_SEASERPENTHEAD, + I_SPHERE_OF_INVISIBILITY, + I_TOADSLIME, + I_BAG_OF_HOLDING, + I_SACK_OF_CONSERVATION, I_TACTICCRYSTAL, - I_RING_OF_REGENERATION, - I_TOADSLIME, - I_BAG_OF_HOLDING, - I_RUSTY_SHIELD, - I_RUSTY_CHAIN_MAIL, - I_SACK_OF_CONSERVATION, - I_SPHERE_OF_INVISIBILITY, - I_RUSTY_HALBERD, -#ifdef COMPATIBILITY - I_STAB_DES_SCHICKSALS, - I_STAB_DER_VERDAMMNIS, - I_STAB_DES_TODES, - I_STAB_DES_CHAOS, - I_STECKEN_DER_MACHT, -#endif MAX_ITEMS /* do not use outside item.c ! */ }; @@ -330,8 +315,6 @@ enum { R_CATAPULT, R_SPEAR, R_LONGBOW, - R_CHAIN_MAIL, - R_PLATE_ARMOR, R_BALM, R_SPICES, R_JEWELERY, @@ -348,7 +331,6 @@ enum { R_CHASTITY_BELT, R_EOGSWORD, R_EOG, - R_SHIELD, R_LANCE, R_MALLORN, R_DRACHENBLUT, @@ -363,13 +345,11 @@ enum { R_RUNESWORD, R_AURAKULUM, R_SEASERPENTHEAD, + R_SPHERE_OF_INVISIBILITY, + R_TOADSLIME, + R_BAG_OF_HOLDING, + R_SACK_OF_CONSERVATION, R_TACTICCRYSTAL, - R_RING_OF_REGENERATION, - R_TOADSLIME, - R_BAG_OF_HOLDING, - R_RUSTY_SHIELD, - R_RUSTY_CHAIN_MAIL, - R_SACK_OF_CONSERVATION, /* POTIONS: */ R_TREES, @@ -378,10 +358,6 @@ enum { R_SILVER, R_AURA, /* Aura */ R_PERMAURA, /* Permanente Aura */ - R_HITPOINTS, /* Trefferpunkte */ - R_PEASANTS, /* Regionsbauern */ - - R_UNIT, /* Einheit (gib x einheit) */ MAX_RESOURCES, /* do not use outside item.c ! */ NORESOURCE = (resource_t) - 1 diff --git a/src/common/kernel/message.c b/src/common/kernel/message.c index bdf1b0f41..ad5c76dc1 100644 --- a/src/common/kernel/message.c +++ b/src/common/kernel/message.c @@ -287,9 +287,6 @@ new_message(struct faction * receiver, const char* sig, ...) case 'X': args[i].v = (void*)va_arg(marker, const resource_type *); break; - case 'x': - args[i].v = oldresourcetype[(resource_t)va_arg(marker, int)]; - break; case 't': case 'i': args[i].i = va_arg(marker, int); diff --git a/src/common/kernel/pool.c b/src/common/kernel/pool.c index 4b88cee7b..45f342d04 100644 --- a/src/common/kernel/pool.c +++ b/src/common/kernel/pool.c @@ -82,10 +82,6 @@ get_resource(const unit * u, const resource_type * rtype) return get_spellpoints(u); if (rtype == oldresourcetype[R_PERMAURA]) return max_spellpoints(u->region, u); - if (rtype == oldresourcetype[R_HITPOINTS]) - return u->hp; - if (rtype == oldresourcetype[R_PEASANTS]) - return rpeasants(u->region); assert(!"unbekannte ressource entdeckt"); return 0; } @@ -101,13 +97,6 @@ change_resource(unit * u, const resource_type * rtype, int change) i = change_spellpoints(u, change); else if (rtype == oldresourcetype[R_PERMAURA]) i = change_maxspellpoints(u, change); - else if (rtype == oldresourcetype[R_HITPOINTS]) - i = change_hitpoints(u, change); - else if (rtype == oldresourcetype[R_PEASANTS]) { - i = rpeasants(u->region) + change; - if (i < 0) i = 0; - rsetpeasants(u->region, i); - } else assert(!"undefined resource detected. rtype->uchange not initialized."); assert(i >= 0 && (i < 100000000)); /* Softer Test, daß kein Unfug diff --git a/src/common/kernel/xmlreader.c b/src/common/kernel/xmlreader.c index fe0b494c0..0224bee3b 100644 --- a/src/common/kernel/xmlreader.c +++ b/src/common/kernel/xmlreader.c @@ -1212,7 +1212,7 @@ parse_spells(xmlDocPtr doc) sp->components[k].type = rt_find((const char *)property); assert(sp->components[k].type); xmlFree(property); - sp->components[k].amount = (resource_t)xml_ivalue(node, "amount", 1); + sp->components[k].amount = xml_ivalue(node, "amount", 1); sp->components[k].cost = SPC_FIX; property = xmlGetProp(node, BAD_CAST "cost"); if (property!=NULL) { diff --git a/src/eressea/lua/bindings.h b/src/eressea/lua/bindings.h index f9bdbd970..fed44e29c 100644 --- a/src/eressea/lua/bindings.h +++ b/src/eressea/lua/bindings.h @@ -12,5 +12,6 @@ extern void bind_spell(struct lua_State * L) ; extern void bind_item(struct lua_State * L); extern void bind_event(struct lua_State * L); extern void bind_message(struct lua_State * L); +extern void bind_script(struct lua_State * L); #endif diff --git a/src/eressea/lua/script.cpp b/src/eressea/lua/script.cpp index c4a0ff58a..b080c6e91 100644 --- a/src/eressea/lua/script.cpp +++ b/src/eressea/lua/script.cpp @@ -14,10 +14,17 @@ #include "eressea.h" #include "script.h" -#include +// kernel includes +#include +#include #include +#include +#include +#include +// util includes #include +#include #include #include @@ -25,6 +32,8 @@ #include #include +static lua_State * luaState; + static void free_script(attrib * a) { if (a->data.v!=NULL) { @@ -72,3 +81,160 @@ setscript(struct attrib ** ap, void * fptr) } a->data.v = fptr; } + +/** callback to use lua for spell functions */ +static int +lua_callspell(castorder *co) +{ + const char * fname = co->sp->sname; + unit * mage = (unit*)co->magician; + int retval = -1; + + if (co->familiar) { + mage = co->familiar; + } + + try { + retval = luabind::call_function(luaState, fname, co->rt, mage, co->level, co->force); + } + catch (luabind::error& e) { + lua_State* L = e.state(); + const char* error = lua_tostring(L, -1); + log_error(("An exception occured while %s tried to call '%s': %s.\n", + unitname(mage), fname, error)); + lua_pop(L, 1); + std::terminate(); + } + return retval; +} + +/** callback for an item-use function written in lua. */ +static int +lua_useitem(struct unit * u, const struct item_type * itype, int amount, struct order * ord) +{ + int retval = 0; + char fname[64]; + snprintf(fname, sizeof(fname), "use_%s", itype->rtype->_name[0]); + + luabind::object globals = luabind::get_globals(luaState); + luabind::object fun = globals.at(fname); + if (fun.is_valid()) { + if (fun.type()!=LUA_TFUNCTION) { + log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type())); + } else { + try { + retval = luabind::call_function(luaState, fname, u, amount); + } + catch (luabind::error& e) { + lua_State* L = e.state(); + const char* error = lua_tostring(L, -1); + log_error(("An exception occured while %s tried to call '%s': %s.\n", + unitname(u), fname, error)); + lua_pop(L, 1); + std::terminate(); + } + } + } + return retval; +} + +/** callback to initialize a familiar from lua. */ +static void +lua_initfamiliar(unit * u) +{ + char fname[64]; + snprintf(fname, sizeof(fname), "initfamiliar_%s", u->race->_name[0]); + + luabind::object globals = luabind::get_globals(luaState); + luabind::object fun = globals.at(fname); + if (fun.is_valid()) { + if (fun.type()!=LUA_TFUNCTION) { + log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type())); + } else { + try { + luabind::call_function(luaState, fname, u); + } + catch (luabind::error& e) { + lua_State* L = e.state(); + const char* error = lua_tostring(L, -1); + log_error(("An exception occured while %s tried to call '%s': %s.\n", + unitname(u), fname, error)); + lua_pop(L, 1); + std::terminate(); + } + } + } + + snprintf(fname, sizeof(fname), "%s_familiar", u->race->_name[0]); + equip_unit(u, get_equipment(fname)); +} + +static int +lua_changeresource(unit * u, const struct resource_type * rtype, int delta) +{ + char fname[64]; + snprintf(fname, sizeof(fname), "%s_changeresource", rtype->_name[0]); + int retval = -1; + + luabind::object globals = luabind::get_globals(luaState); + luabind::object fun = globals.at(fname); + if (fun.is_valid()) { + if (fun.type()!=LUA_TFUNCTION) { + log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type())); + } else { + try { + retval = luabind::call_function(luaState, fname, u, delta); + } + catch (luabind::error& e) { + lua_State* L = e.state(); + const char* error = lua_tostring(L, -1); + log_error(("An exception occured while %s tried to call '%s': %s.\n", + unitname(u), fname, error)); + lua_pop(L, 1); + std::terminate(); + } + } + } + return retval; +} + + +static int +lua_getresource(unit * u, const struct resource_type * rtype) +{ + char fname[64]; + snprintf(fname, sizeof(fname), "%s_getresource", rtype->_name[0]); + int retval = -1; + + luabind::object globals = luabind::get_globals(luaState); + luabind::object fun = globals.at(fname); + if (fun.is_valid()) { + if (fun.type()!=LUA_TFUNCTION) { + log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type())); + } else { + try { + retval = luabind::call_function(luaState, fname, u); + } + catch (luabind::error& e) { + lua_State* L = e.state(); + const char* error = lua_tostring(L, -1); + log_error(("An exception occured while %s tried to call '%s': %s.\n", + unitname(u), fname, error)); + lua_pop(L, 1); + std::terminate(); + } + } + } + return retval; +} + +void +bind_script(lua_State * L) +{ + luaState = L; + register_function((pf_generic)&lua_callspell, "lua_castspell"); + register_function((pf_generic)&lua_initfamiliar, "lua_initfamiliar"); + register_function((pf_generic)&lua_useitem, "lua_useitem"); + register_function((pf_generic)&lua_getresource, "lua_getresource"); + register_function((pf_generic)&lua_changeresource, "lua_changeresource"); +} diff --git a/src/eressea/lua/spell.cpp b/src/eressea/lua/spell.cpp index 2b8a9fb93..da9872fcb 100644 --- a/src/eressea/lua/spell.cpp +++ b/src/eressea/lua/spell.cpp @@ -3,20 +3,11 @@ #include "list.h" // kernel includes -#include #include -#include -#include -#include -#include - -// util includes -#include // lua includes #include #include -#include using namespace luabind; @@ -26,104 +17,13 @@ spell_getschool(const spell& sp) return magietypen[sp.magietyp]; } -static lua_State * luaState; - -int -call_spell(castorder *co) -{ - const char * fname = co->sp->sname; - unit * mage = (unit*)co->magician; - int retval = -1; - - if (co->familiar) { - mage = co->familiar; - } - - try { - retval = luabind::call_function(luaState, fname, co->rt, mage, co->level, co->force); - } - catch (luabind::error& e) { - lua_State* L = e.state(); - const char* error = lua_tostring(L, -1); - log_error(("An exception occured while %s tried to call '%s': %s.\n", - unitname(mage), fname, error)); - lua_pop(L, 1); - std::terminate(); - } - return retval; -} - -static void -init_familiar(unit * u) -{ - char fname[64]; - snprintf(fname, sizeof(fname), "initfamiliar_%s", u->race->_name[0]); - - luabind::object globals = luabind::get_globals(luaState); - luabind::object fun = globals.at(fname); - if (fun.is_valid()) { - if (fun.type()!=LUA_TFUNCTION) { - log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type())); - } else { - try { - luabind::call_function(luaState, fname, u); - } - catch (luabind::error& e) { - lua_State* L = e.state(); - const char* error = lua_tostring(L, -1); - log_error(("An exception occured while %s tried to call '%s': %s.\n", - unitname(u), fname, error)); - lua_pop(L, 1); - std::terminate(); - } - } - } - - snprintf(fname, sizeof(fname), "%s_familiar", u->race->_name[0]); - equip_unit(u, get_equipment(fname)); -} - -static int -use_item(struct unit * u, const struct item_type * itype, int amount, struct order * ord) -{ - int retval = 0; - char fname[64]; - snprintf(fname, sizeof(fname), "use_%s", itype->rtype->_name[0]); - - luabind::object globals = luabind::get_globals(luaState); - luabind::object fun = globals.at(fname); - if (fun.is_valid()) { - if (fun.type()!=LUA_TFUNCTION) { - log_warning(("Lua global object %s is not a function, type is %u\n", fname, fun.type())); - } else { - try { - retval = luabind::call_function(luaState, fname, u, amount); - } - catch (luabind::error& e) { - lua_State* L = e.state(); - const char* error = lua_tostring(L, -1); - log_error(("An exception occured while %s tried to call '%s': %s.\n", - unitname(u), fname, error)); - lua_pop(L, 1); - std::terminate(); - } - } - } - return retval; -} - - void bind_spell(lua_State * L) { - luaState = L; module(L)[ class_("spell") .def_readonly("name", &spell::sname) .def_readonly("level", &spell::level) .property("school", &spell_getschool) ]; - register_function((pf_generic)&call_spell, "luaspell"); - register_function((pf_generic)&init_familiar, "luafamiliar"); - register_function((pf_generic)&use_item, "luaitem"); } diff --git a/src/eressea/server.cpp b/src/eressea/server.cpp index f42331cb6..df6e1c2e0 100644 --- a/src/eressea/server.cpp +++ b/src/eressea/server.cpp @@ -297,6 +297,7 @@ lua_init(void) luaopen_table(luaState); luabind::open(luaState); bind_eressea(luaState); + bind_script(luaState); bind_spell(luaState); bind_alliance(luaState); bind_region(luaState); diff --git a/src/res/races.xml b/src/res/races.xml index 6fc0ffef6..d4eae19f6 100644 --- a/src/res/races.xml +++ b/src/res/races.xml @@ -93,7 +93,7 @@ - + @@ -123,7 +123,7 @@ - + @@ -153,7 +153,7 @@ - + @@ -184,7 +184,7 @@ - + @@ -219,7 +219,7 @@ - + @@ -250,7 +250,7 @@ - + @@ -283,7 +283,7 @@ - + @@ -344,7 +344,7 @@ - + @@ -375,7 +375,7 @@ - + @@ -405,7 +405,7 @@ - + @@ -437,7 +437,7 @@ - + @@ -469,7 +469,7 @@ - + @@ -499,7 +499,7 @@ - + @@ -532,7 +532,7 @@ - + @@ -562,7 +562,7 @@ - + diff --git a/src/res/resources.xml b/src/res/resources.xml index 34b674ad9..e49769266 100644 --- a/src/res/resources.xml +++ b/src/res/resources.xml @@ -101,7 +101,7 @@ - + @@ -112,8 +112,31 @@ + + + + + + + + + + + + + + + + + + + + + + + - + @@ -226,6 +249,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/res/spells.xml b/src/res/spells.xml index 07751a5e6..8b43ab3ee 100644 --- a/src/res/spells.xml +++ b/src/res/spells.xml @@ -2,21 +2,21 @@ - + - + - + @@ -24,28 +24,28 @@ - + - + - + - + @@ -53,27 +53,27 @@ - + - + - + - + @@ -81,21 +81,21 @@ - + - + - + @@ -103,34 +103,34 @@ - + - + - + - + - + @@ -138,7 +138,7 @@ - + @@ -146,20 +146,20 @@ - + - + - + diff --git a/src/scripts/familiars.lua b/src/scripts/familiars.lua index 9b1d49123..cbb2ebdd7 100644 --- a/src/scripts/familiars.lua +++ b/src/scripts/familiars.lua @@ -1,3 +1,34 @@ function initfamiliar_lynx(u) print("a lynx is born :-)") end + +function peasant_getresource(u) + return u.region:get_resource("peasant") +end + +function peasant_changeresource(u, delta) + local p = u.region:get_resource("peasant") + p = p + delta + if p < 0 then + p = 0 + end + u.region:set_resource("peasant", p) + return p +end + +function hp_getresource(u) + return u.hp +end + +function hp_changeresource(u, delta) + local hp = u.hp + delta + + if hp < u.number then + if hp < 0 then + hp = 0 + end + u.number = hp + end + u.hp = hp + return hp +end