- 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">
</File>
</Filter>
<Filter
Name="Spells"
Filter="">
<File
RelativePath="..\spells\alp.c">
</File>
</Filter>
<File
RelativePath=".\creation.c">
</File>

View File

@ -3349,77 +3349,75 @@ canheal(const unit *u)
static void
monthly_healing(void)
{
region *r;
unit *u;
int p;
int healingcurse = 0;
curse *c = NULL;
static const curse_type * heal_ct;
heal_ct = ct_find("healingzone");
region *r;
static const curse_type * heal_ct = NULL;
if (heal_ct==NULL) heal_ct = ct_find("healingzone");
for (r = regions; r; r = r->next) {
if (heal_ct) {
/* bonus zurücksetzen */
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;
for (r = regions; r; r = r->next) {
unit *u;
int healingcurse = 0;
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
* oder verändertes Ausdauertalent verursacht */
if (u->hp > umhp) {
u->hp -= (int) ceil((u->hp - umhp) / 2.0);
if (u->hp < umhp)
u->hp = umhp;
}
/* hp über Maximum bauen sich ab. Wird zb durch Elixier der Macht
* oder verändertes Ausdauertalent verursacht */
if (u->hp > umhp) {
u->hp -= (int) ceil((u->hp - umhp) / 2.0);
if (u->hp < umhp)
u->hp = umhp;
}
if((u->race->flags & RCF_NOHEAL) || fval(u, UFL_HUNGER) || fspecial(u->faction, FS_UNDEAD))
continue;
if((u->race->flags & RCF_NOHEAL) || fval(u, UFL_HUNGER) || fspecial(u->faction, FS_UNDEAD))
continue;
if(rterrain(r) == T_OCEAN && !u->ship && !(canswim(u)))
continue;
if(rterrain(r) == T_OCEAN && !u->ship && !(canswim(u)))
continue;
if(fspecial(u->faction, FS_REGENERATION)) {
u->hp = umhp;
continue;
}
if(fspecial(u->faction, FS_REGENERATION)) {
u->hp = umhp;
continue;
}
if (u->hp < umhp && (p=canheal(u)) > 0) {
/* Mind 1 HP wird pro Runde geheilt, weil angenommen wird,
das alle Personen mind. 10 HP haben. */
int max_unit = max(umhp, u->number * 10);
p = canheal(u);
if (u->hp < umhp && p > 0) {
/* Mind 1 HP wird pro Runde geheilt, weil angenommen wird,
das alle Personen mind. 10 HP haben. */
int max_unit = max(umhp, u->number * 10);
#ifdef NEW_TAVERN
struct building * b = inside_building(u);
const struct building_type * btype = b?b->type:NULL;
if (btype == bt_find("inn")) {
max_unit = max_unit * 3 / 2;
}
struct building * b = inside_building(u);
const struct building_type * btype = b?b->type:NULL;
if (btype == bt_find("inn")) {
max_unit = max_unit * 3 / 2;
}
#endif
/* der healing curse verändert den Regenerationsprozentsatz.
* Wenn dies für negative Heilung benutzt wird, kann es zu
* negativen u->hp führen! */
if (healingcurse != 0) {
p += healingcurse;
}
/* der healing curse verändert den Regenerationsprozentsatz.
* Wenn dies für negative Heilung benutzt wird, kann es zu
* negativen u->hp führen! */
if (healingcurse != 0) {
p += healingcurse;
}
/* Aufaddieren der geheilten HP. */
u->hp = min(u->hp + max_unit*p/100, umhp);
if (u->hp < umhp && (rand() % 10 < max_unit % 10)){
++u->hp;
}
/* soll man an negativer regeneration sterben können? */
if (u->hp <= 0){
u->hp = 1;
}
}
}
}
/* Aufaddieren der geheilten HP. */
u->hp = min(u->hp + max_unit*p/100, umhp);
if (u->hp < umhp && (rand() % 10 < max_unit % 10)){
++u->hp;
}
/* soll man an negativer regeneration sterben können? */
if (u->hp <= 0){
u->hp = 1;
}
}
}
}
}
static void
@ -3518,7 +3516,8 @@ use(void)
itype = finditemtype(t, u->faction->locale);
if (itype!=NULL) {
use_item(u, itype, n, S->s);
int i = use_item(u, itype, n, S->s);
assert(i<=0);
} else {
cmistake(u, S->s, 43, MSG_PRODUCE);
}

View File

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

View File

@ -2296,30 +2296,55 @@ attrib_type at_showitem = {
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 *
finditemtype(const char * name, const struct locale * lang)
{
local_names * in = inames;
void * i;
local_names * in = inames;
void * i;
while (in) {
if (in->lang==lang) break;
in=in->next;
}
if (!in) {
const item_type * itl = itemtypes;
in = calloc(sizeof(local_names), 1);
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;
return (const item_type*)i;
while (in!=NULL) {
if (in->lang==lang) break;
in=in->next;
}
if (in==NULL) {
init_itemnames();
for (in=inames;in->lang!=lang;in=in->next) ;
}
if (findtoken(&in->names, name, &i)==E_TOK_NOMATCH) return NULL;
return (const item_type*)i;
}
static void

View File

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

View File

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

View File

@ -211,6 +211,9 @@
<File
RelativePath="..\header.txt">
</File>
<File
RelativePath="..\scripts\item.lua">
</File>
<File
RelativePath="..\todo.txt">
</File>
@ -269,6 +272,9 @@
DisableLanguageExtensions="FALSE"/>
</FileConfiguration>
</File>
<File
RelativePath=".\lua\item.cpp">
</File>
<File
RelativePath=".\lua\list.h">
</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_eressea(struct lua_State * L);
extern void bind_spell(lua_State * L) ;
extern void bind_item(struct lua_State * L);
#endif

View File

@ -98,6 +98,13 @@ get_gamename(void)
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
bind_eressea(lua_State * L)
{
@ -113,6 +120,7 @@ bind_eressea(lua_State * L)
def("add_equipment", &lua_addequipment),
def("get_turn", &get_turn),
def("remove_empty_units", &remove_empty_units),
def("set_string", &lua_setstring),
def("set_flag", &set_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);
#endif
bind_region(luaState);
bind_item(luaState);
bind_faction(luaState);
bind_unit(luaState);
bind_ship(luaState);