- Lua extension, adding items from scriot.

- http://eressea.upb.de/mantis/bug_view_page.php?bug_id=0000117
  Bugfix "Beute bewahren"
This commit is contained in:
Enno Rehling 2004-06-07 21:43:55 +00:00
parent 7a6935daf7
commit a1b016b1ce
11 changed files with 192 additions and 95 deletions

View File

@ -196,13 +196,6 @@
RelativePath=".\randenc.h"> RelativePath=".\randenc.h">
</File> </File>
</Filter> </Filter>
<Filter
Name="Spells"
Filter="">
<File
RelativePath="..\spells\alp.c">
</File>
</Filter>
<File <File
RelativePath=".\creation.c"> RelativePath=".\creation.c">
</File> </File>

View File

@ -3350,26 +3350,23 @@ static void
monthly_healing(void) monthly_healing(void)
{ {
region *r; region *r;
unit *u; static const curse_type * heal_ct = NULL;
int p; if (heal_ct==NULL) heal_ct = ct_find("healingzone");
int healingcurse = 0;
curse *c = NULL;
static const curse_type * heal_ct;
heal_ct = ct_find("healingzone");
for (r = regions; r; r = r->next) { for (r = regions; r; r = r->next) {
if (heal_ct) { unit *u;
int healingcurse = 0;
if (heal_ct!=NULL) {
/* bonus zurücksetzen */ /* bonus zurücksetzen */
healingcurse = 0; curse * c = get_curse(r->attribs, heal_ct);
c = get_curse(r->attribs, heal_ct); if (c!=NULL) {
if (c) {
healingcurse = curse_geteffect(c); healingcurse = curse_geteffect(c);
} }
} }
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
int umhp; int umhp = unit_max_hp(u) * u->number;
int p;
umhp = unit_max_hp(u) * u->number;
/* hp über Maximum bauen sich ab. Wird zb durch Elixier der Macht /* hp über Maximum bauen sich ab. Wird zb durch Elixier der Macht
* oder verändertes Ausdauertalent verursacht */ * oder verändertes Ausdauertalent verursacht */
@ -3390,7 +3387,8 @@ monthly_healing(void)
continue; continue;
} }
if (u->hp < umhp && (p=canheal(u)) > 0) { p = canheal(u);
if (u->hp < umhp && p > 0) {
/* Mind 1 HP wird pro Runde geheilt, weil angenommen wird, /* Mind 1 HP wird pro Runde geheilt, weil angenommen wird,
das alle Personen mind. 10 HP haben. */ das alle Personen mind. 10 HP haben. */
int max_unit = max(umhp, u->number * 10); int max_unit = max(umhp, u->number * 10);
@ -3518,7 +3516,8 @@ use(void)
itype = finditemtype(t, u->faction->locale); itype = finditemtype(t, u->faction->locale);
if (itype!=NULL) { if (itype!=NULL) {
use_item(u, itype, n, S->s); int i = use_item(u, itype, n, S->s);
assert(i<=0);
} else { } else {
cmistake(u, S->s, 43, MSG_PRODUCE); cmistake(u, S->s, 43, MSG_PRODUCE);
} }

View File

@ -1586,7 +1586,7 @@ sp_keeploot(fighter * fi, int level, double power, spell * sp)
spell_name(sp, default_locale)); spell_name(sp, default_locale));
battlerecord(b, buf); battlerecord(b, buf);
b->keeploot = (int)max(50, b->keeploot + 5*power); b->keeploot = (int)max(25, b->keeploot + 5*power);
return level; return level;
} }

View File

@ -2296,27 +2296,52 @@ attrib_type at_showitem = {
static local_names * inames; static local_names * inames;
void
init_itemnames(void)
{
int i;
for (i=0;localenames[i];++i) {
const struct locale * lang = find_locale(localenames[i]);
const item_type * itl = itemtypes;
boolean exist = false;
local_names * in = inames;
while (in!=NULL) {
if (in->lang==lang) {
exist = true;
break;
}
in = in->next;
}
if (in==NULL) in = calloc(sizeof(local_names), 1);
in->next = inames;
in->lang = lang;
while (itl) {
void * result = NULL;
const char * iname = locale_string(lang, itl->rtype->_name[0]);
if (!exist || findtoken(&in->names, iname, &result)==E_TOK_NOMATCH || result!=itl) {
addtoken(&in->names, iname, (void*)itl);
addtoken(&in->names, locale_string(lang, itl->rtype->_name[1]), (void*)itl);
}
itl=itl->next;
}
inames = in;
}
}
const item_type * const item_type *
finditemtype(const char * name, const struct locale * lang) finditemtype(const char * name, const struct locale * lang)
{ {
local_names * in = inames; local_names * in = inames;
void * i; void * i;
while (in) { while (in!=NULL) {
if (in->lang==lang) break; if (in->lang==lang) break;
in=in->next; in=in->next;
} }
if (!in) { if (in==NULL) {
const item_type * itl = itemtypes; init_itemnames();
in = calloc(sizeof(local_names), 1); for (in=inames;in->lang!=lang;in=in->next) ;
in->next = inames;
in->lang = lang;
while (itl) {
addtoken(&in->names, locale_string(lang, itl->rtype->_name[0]), (void*)itl);
addtoken(&in->names, locale_string(lang, itl->rtype->_name[1]), (void*)itl);
itl=itl->next;
}
inames = in;
} }
if (findtoken(&in->names, name, &i)==E_TOK_NOMATCH) return NULL; if (findtoken(&in->names, name, &i)==E_TOK_NOMATCH) return NULL;
return (const item_type*)i; return (const item_type*)i;

View File

@ -137,6 +137,7 @@ typedef struct item_type {
extern item_type * itemtypes; extern item_type * itemtypes;
extern const item_type * finditemtype(const char * name, const struct locale * lang); extern const item_type * finditemtype(const char * name, const struct locale * lang);
extern void init_itemnames(void);
typedef struct luxury_type { typedef struct luxury_type {
struct luxury_type * next; struct luxury_type * next;

View File

@ -22,6 +22,7 @@ LUASERVER_SOURCES =
<lua>ship.cpp <lua>ship.cpp
<lua>spell.cpp <lua>spell.cpp
<lua>unit.cpp <lua>unit.cpp
<lua>item.cpp
server.cpp server.cpp
korrektur.c korrektur.c
console.c console.c

View File

@ -211,6 +211,9 @@
<File <File
RelativePath="..\header.txt"> RelativePath="..\header.txt">
</File> </File>
<File
RelativePath="..\scripts\item.lua">
</File>
<File <File
RelativePath="..\todo.txt"> RelativePath="..\todo.txt">
</File> </File>
@ -269,6 +272,9 @@
DisableLanguageExtensions="FALSE"/> DisableLanguageExtensions="FALSE"/>
</FileConfiguration> </FileConfiguration>
</File> </File>
<File
RelativePath=".\lua\item.cpp">
</File>
<File <File
RelativePath=".\lua\list.h"> RelativePath=".\lua\list.h">
</File> </File>

View File

@ -11,5 +11,6 @@ extern void bind_faction(struct lua_State * L);
extern void bind_alliance(struct lua_State * L); extern void bind_alliance(struct lua_State * L);
extern void bind_eressea(struct lua_State * L); extern void bind_eressea(struct lua_State * L);
extern void bind_spell(lua_State * L) ; extern void bind_spell(lua_State * L) ;
extern void bind_item(struct lua_State * L);
#endif #endif

View File

@ -98,6 +98,13 @@ get_gamename(void)
return global.gamename; return global.gamename;
} }
static void
lua_setstring(const char * lname, const char * key, const char * str)
{
struct locale * lang = find_locale(lname);
locale_setstring(lang, key, str);
}
void void
bind_eressea(lua_State * L) bind_eressea(lua_State * L)
{ {
@ -113,6 +120,7 @@ bind_eressea(lua_State * L)
def("add_equipment", &lua_addequipment), def("add_equipment", &lua_addequipment),
def("get_turn", &get_turn), def("get_turn", &get_turn),
def("remove_empty_units", &remove_empty_units), def("remove_empty_units", &remove_empty_units),
def("set_string", &lua_setstring),
def("set_flag", &set_flag), def("set_flag", &set_flag),
def("get_flag", &get_flag), def("get_flag", &get_flag),

62
src/eressea/lua/item.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <config.h>
#include <eressea.h>
// kernel includes
#include <kernel/unit.h>
#include <kernel/item.h>
#include <util/attrib.h>
// lua includes
#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
using namespace luabind;
static int
lua_useitem(struct unit * u, const struct item_type * itype,
int amount, const char *cmd)
{
char fname[64];
lua_State * L = (lua_State *)global.vm_state;
int retval;
const char * iname = itype->rtype->_name[0];
assert(u!=NULL);
strcat(strcpy(fname, iname), "_use");
{
luabind::object globals = luabind::get_globals(L);
if (globals.at(fname).type()!=LUA_TFUNCTION) return -1;
}
retval = luabind::call_function<int>(L, fname, *u, amount);
return retval;
}
static void
item_register(const char * name, const char * appearance)
{
resource_type * rtype = (resource_type *)calloc(1, sizeof(resource_type));
item_type * itype = (item_type *)calloc(1, sizeof(item_type));
rtype->_name[0] = strdup(name);
rtype->_name[1] = strcat(strcpy((char*)malloc(strlen(name)+3), name), "_p");
rtype->_appearance[0] = strdup(appearance);
rtype->_appearance[1] = strcat(strcpy((char*)malloc(strlen(appearance)+3), appearance), "_p");
itype->use = lua_useitem;
itype->rtype = rtype;
rt_register(rtype);
it_register(itype);
init_itemnames();
}
void
bind_item(lua_State * L)
{
module(L)[
def("register_item", &item_register)
];
}

View File

@ -277,6 +277,7 @@ lua_init(void)
bind_alliance(luaState); bind_alliance(luaState);
#endif #endif
bind_region(luaState); bind_region(luaState);
bind_item(luaState);
bind_faction(luaState); bind_faction(luaState);
bind_unit(luaState); bind_unit(luaState);
bind_ship(luaState); bind_ship(luaState);