Übergabe von Silber an die Bauern hat nicht funktioniert

- added give-item() functionality to lua
This commit is contained in:
Enno Rehling 2007-12-18 07:51:04 +00:00
parent 06e15c24d5
commit ef77f0ad2b
5 changed files with 53 additions and 29 deletions

View file

@ -79,18 +79,6 @@ add_give(unit * u, unit * u2, int n, const resource_type * rtype, struct order *
}
}
static void
give_peasants(int n, const item_type * itype, unit * src)
{
region *r = src->region;
/* horses are given to the region via itype->give! */
if (itype->rtype==r_silver) {
rsetmoney(r, rmoney(r) + n);
}
}
int
give_item(int want, const item_type * itype, unit * src, unit * dest, struct order * ord)
{
@ -101,10 +89,10 @@ give_item(int want, const item_type * itype, unit * src, unit * dest, struct ord
n = get_pooled(src, item2resource(itype), GET_DEFAULT, want);
n = min(want, n);
if (dest && src->faction != dest->faction && src->faction->age < GiveRestriction()) {
if (ord!=NULL) {
if (ord!=NULL) {
ADDMSG(&src->faction->msgs, msg_feedback(src, ord, "giverestriction",
"turns", GiveRestriction()));
}
}
return -1;
} else if (n == 0) {
int reserve = get_reservation(src, itype->rtype);
@ -130,18 +118,15 @@ give_item(int want, const item_type * itype, unit * src, unit * dest, struct ord
}
#endif
#endif
handle_event(src->attribs, "give", dest);
handle_event(dest->attribs, "receive", src);
#if defined(MUSEUM_MODULE) && defined(TODO)
/* TODO: Einen Trigger für den museums-warden benutzen! */
if (a_find(dest->attribs, &at_warden)) {
/* warden_add_give(src, dest, itype, n); */
}
/* TODO: use a trigger for the museum warden! */
if (a_find(dest->attribs, &at_warden)) {
warden_add_give(src, dest, itype, n);
}
#endif
} else {
/* gib bauern */
give_peasants(use, itype, src);
handle_event(dest->attribs, "receive", src);
}
handle_event(src->attribs, "give", dest);
}
add_give(src, dest, n, item2resource(itype), ord, error);
if (error) return -1;

View file

@ -45,16 +45,16 @@ summon_igjarjuk(struct unit * u, const struct item_type * itype, int amount, str
}
}
static boolean
static int
give_igjarjuk(const struct unit * src, const struct unit * d, const struct item_type * itype, int n, struct order * ord)
{
ADDMSG(&src->faction->msgs, msg_feedback(src, ord, "error_giveeye", ""));
return false;
return 0;
}
void
register_demonseye(void)
{
register_item_use(summon_igjarjuk, "useigjarjuk");
register_function((pf_generic)give_igjarjuk, "giveigjarjuk");
register_item_give(give_igjarjuk, "giveigjarjuk");
}

View file

@ -571,7 +571,9 @@ static int
give_horses(unit * s, unit * d, const item_type * itype, int n, struct order * ord)
{
if (d==NULL) {
rsethorses(s->region, rhorses(s->region) + n);
int use = use_pooled(s, item2resource(itype), GET_SLACK, n);
if (use<n) use += use_pooled(s, item2resource(itype), GET_RESERVE|GET_POOLED_SLACK, n-use);
rsethorses(s->region, rhorses(s->region) + use);
return 0;
}
return -1; /* use the mechanism */
@ -581,7 +583,9 @@ static int
give_money(unit * s, unit * d, const item_type * itype, int n, struct order * ord)
{
if (d==NULL) {
rsetmoney(s->region, rmoney(s->region) + n);
int use = use_pooled(s, item2resource(itype), GET_SLACK, n);
if (use<n) use += use_pooled(s, item2resource(itype), GET_RESERVE|GET_POOLED_SLACK, n-use);
rsetmoney(s->region, rmoney(s->region) + use);
return 0;
}
return -1; /* use the mechanism */
@ -747,6 +751,12 @@ heal(unit * user, int effect)
return effect;
}
void
register_item_give(int (*foo) (struct unit *, struct unit *, const struct item_type *, int, struct order *), const char * name)
{
register_function((pf_generic)foo, name);
}
void
register_item_use(int (*foo) (struct unit *, const struct item_type *, int, struct order *), const char * name)
{
@ -1145,7 +1155,7 @@ register_resources(void)
register_item_use(use_magicboost, "usemagicboost");
register_item_use(use_snowball, "usesnowball");
register_function((pf_generic)give_horses, "givehorses");
register_item_give(give_horses, "givehorses");
/* make sure noone has deleted an I_ tpe without deleting the R_ type that goes with it! */
assert((int)I_TACTICCRYSTAL == (int)R_TACTICCRYSTAL);

View file

@ -333,6 +333,7 @@ extern struct attrib_type at_seenitem; /* knows this potion's description, no ne
extern void register_resources(void);
extern void init_resources(void);
extern void register_item_give(int (*foo) (struct unit *, struct unit *, const struct item_type *, int, struct order *), const char * name);
extern void register_item_use(int (*foo) (struct unit *, const struct item_type *, int, struct order *), const char * name);
extern void register_item_useonother(int (*foo) (struct unit *, int, const struct item_type *, int, struct order *), const char * name);

View file

@ -23,6 +23,33 @@
using namespace luabind;
static int
lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order * ord)
{
char fname[64];
int retval = -1;
const char * iname = itype->rtype->_name[0];
assert(s!=NULL);
strcat(strcpy(fname, iname), "_give");
lua_State * L = (lua_State *)global.vm_state;
if (is_function(L, fname)) {
try {
retval = luabind::call_function<int>(L, fname, s, d, iname, n);
}
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(s), fname, error));
lua_pop(L, 1);
std::terminate();
}
}
return retval;
}
static int
lua_useitem(struct unit * u, const struct item_type * itype,
int amount, struct order *ord)
@ -121,6 +148,7 @@ bind_item(lua_State * L)
{
register_function((pf_generic)produce_resource, "lua_produceresource");
register_function((pf_generic)limit_resource, "lua_limitresource");
register_item_give(lua_giveitem, "lua_giveitem");
module(L)[
def("register_item", &item_register)