- 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

@ -3349,77 +3349,75 @@ canheal(const unit *u)
static void 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;
/* bonus zurücksetzen */ int healingcurse = 0;
healingcurse = 0;
c = get_curse(r->attribs, heal_ct);
if (c) {
healingcurse = curse_geteffect(c);
}
}
for (u = r->units; u; u = u->next) {
int umhp;
umhp = unit_max_hp(u) * u->number; if (heal_ct!=NULL) {
/* bonus zurücksetzen */
curse * c = get_curse(r->attribs, heal_ct);
if (c!=NULL) {
healingcurse = curse_geteffect(c);
}
}
for (u = r->units; u; u = u->next) {
int umhp = unit_max_hp(u) * u->number;
int p;
/* 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 */
if (u->hp > umhp) { if (u->hp > umhp) {
u->hp -= (int) ceil((u->hp - umhp) / 2.0); u->hp -= (int) ceil((u->hp - umhp) / 2.0);
if (u->hp < umhp) if (u->hp < umhp)
u->hp = umhp; u->hp = umhp;
} }
if((u->race->flags & RCF_NOHEAL) || fval(u, UFL_HUNGER) || fspecial(u->faction, FS_UNDEAD)) if((u->race->flags & RCF_NOHEAL) || fval(u, UFL_HUNGER) || fspecial(u->faction, FS_UNDEAD))
continue; continue;
if(rterrain(r) == T_OCEAN && !u->ship && !(canswim(u))) if(rterrain(r) == T_OCEAN && !u->ship && !(canswim(u)))
continue; continue;
if(fspecial(u->faction, FS_REGENERATION)) { if(fspecial(u->faction, FS_REGENERATION)) {
u->hp = umhp; u->hp = umhp;
continue; continue;
} }
if (u->hp < umhp && (p=canheal(u)) > 0) { p = canheal(u);
/* Mind 1 HP wird pro Runde geheilt, weil angenommen wird, if (u->hp < umhp && p > 0) {
das alle Personen mind. 10 HP haben. */ /* Mind 1 HP wird pro Runde geheilt, weil angenommen wird,
int max_unit = max(umhp, u->number * 10); das alle Personen mind. 10 HP haben. */
int max_unit = max(umhp, u->number * 10);
#ifdef NEW_TAVERN #ifdef NEW_TAVERN
struct building * b = inside_building(u); struct building * b = inside_building(u);
const struct building_type * btype = b?b->type:NULL; const struct building_type * btype = b?b->type:NULL;
if (btype == bt_find("inn")) { if (btype == bt_find("inn")) {
max_unit = max_unit * 3 / 2; max_unit = max_unit * 3 / 2;
} }
#endif #endif
/* der healing curse verändert den Regenerationsprozentsatz. /* der healing curse verändert den Regenerationsprozentsatz.
* Wenn dies für negative Heilung benutzt wird, kann es zu * Wenn dies für negative Heilung benutzt wird, kann es zu
* negativen u->hp führen! */ * negativen u->hp führen! */
if (healingcurse != 0) { if (healingcurse != 0) {
p += healingcurse; p += healingcurse;
} }
/* Aufaddieren der geheilten HP. */ /* Aufaddieren der geheilten HP. */
u->hp = min(u->hp + max_unit*p/100, umhp); u->hp = min(u->hp + max_unit*p/100, umhp);
if (u->hp < umhp && (rand() % 10 < max_unit % 10)){ if (u->hp < umhp && (rand() % 10 < max_unit % 10)){
++u->hp; ++u->hp;
} }
/* soll man an negativer regeneration sterben können? */ /* soll man an negativer regeneration sterben können? */
if (u->hp <= 0){ if (u->hp <= 0){
u->hp = 1; u->hp = 1;
} }
} }
} }
} }
} }
static void static void
@ -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

@ -1580,15 +1580,15 @@ sp_reanimate(fighter * fi, int level, double power, spell * sp)
int int
sp_keeploot(fighter * fi, int level, double power, spell * sp) sp_keeploot(fighter * fi, int level, double power, spell * sp)
{ {
battle *b = fi->side->battle; battle *b = fi->side->battle;
sprintf(buf, "%s zaubert %s.", unitname(fi->unit), sprintf(buf, "%s zaubert %s.", unitname(fi->unit),
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;
} }
int int

View File

@ -2296,30 +2296,55 @@ 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; if (findtoken(&in->names, name, &i)==E_TOK_NOMATCH) return NULL;
while (itl) { return (const item_type*)i;
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;
return (const item_type*)i;
} }
static void static void

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);