building-attribute in den von mapper gelinkten Teil getan. Tricks mit age-Funktion..

This commit is contained in:
Enno Rehling 2004-07-10 23:06:58 +00:00
parent fb1696c19e
commit e8bc9ce888
4 changed files with 57 additions and 58 deletions

View File

@ -40,6 +40,51 @@
/* attributes includes */ /* attributes includes */
#include <attributes/matmod.h> #include <attributes/matmod.h>
static void
lc_init(struct attrib *a)
{
a->data.v = calloc(1, sizeof(building_action));
}
static void
lc_done(struct attrib *a)
{
building_action * data = (building_action*)a->data.v;
if (data->fname) free(data->fname);
free(data);
}
static void
lc_write(const struct attrib * a, FILE* F)
{
building_action * data = (building_action*)a->data.v;
const char * fname = data->fname;
building * b = data->b;
write_building_reference(b, F);
fwritestr(F, fname);
fputc(' ', F);
}
static int
lc_read(struct attrib * a, FILE* F)
{
char lbuf[256];
building_action * data = (building_action*)a->data.v;
read_building_reference(&data->b, F);
freadstr(F, lbuf, sizeof(lbuf));
data->fname = strdup(lbuf);
return AT_READ_OK;
}
attrib_type at_building_action = {
"lcbuilding",
lc_init, lc_done,
NULL,
lc_write, lc_read
};
attrib_type at_nodestroy = { attrib_type at_nodestroy = {
"nodestroy", "nodestroy",
NULL, NULL, NULL, NULL, NULL, NULL,

View File

@ -140,6 +140,12 @@ extern struct building *findbuilding(int n);
extern struct unit * buildingowner(const struct region * r, const struct building * b); extern struct unit * buildingowner(const struct region * r, const struct building * b);
extern attrib_type at_nodestroy; extern attrib_type at_nodestroy;
extern attrib_type at_building_action;
typedef struct building_action {
building * b;
char * fname;
} building_action;
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -3230,6 +3230,7 @@ attrib_init(void)
#endif #endif
at_register(&at_speedup); at_register(&at_speedup);
at_register(&at_nodestroy); at_register(&at_nodestroy);
at_register(&at_building_action);
} }
void void

View File

@ -26,30 +26,11 @@ add_building(region * r, const char * name)
return new_building(btype, r, NULL); return new_building(btype, r, NULL);
} }
typedef struct lcbuilding_data {
building * b;
char * fname;
} lcbuilding_data;
static void
lc_init(struct attrib *a)
{
a->data.v = calloc(1, sizeof(lcbuilding_data));
}
static void
lc_done(struct attrib *a)
{
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
if (data->fname) free(data->fname);
free(data);
}
static int static int
lc_age(struct attrib * a) lc_age(struct attrib * a)
{ {
lua_State * L = (lua_State *)global.vm_state; lua_State * L = (lua_State *)global.vm_state;
lcbuilding_data * data = (lcbuilding_data*)a->data.v; building_action * data = (building_action*)a->data.v;
const char * fname = data->fname; const char * fname = data->fname;
building * b = data->b; building * b = data->b;
@ -62,45 +43,11 @@ lc_age(struct attrib * a)
return luabind::call_function<int>(L, fname, *b); return luabind::call_function<int>(L, fname, *b);
} }
static void
lc_write(const struct attrib * a, FILE* F)
{
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
const char * fname = data->fname;
building * b = data->b;
write_building_reference(b, F);
fwritestr(F, fname);
fputc(' ', F);
}
static int
lc_read(struct attrib * a, FILE* F)
{
char lbuf[256];
lcbuilding_data * data = (lcbuilding_data*)a->data.v;
read_building_reference(&data->b, F);
freadstr(F, lbuf, sizeof(lbuf));
data->fname = strdup(lbuf);
return AT_READ_OK;
}
attrib_type at_luacall_building = {
"lcbuilding",
lc_init,
lc_done,
lc_age,
lc_write,
lc_read,
ATF_UNIQUE
};
static int static int
building_addaction(building& b, const char * fname) building_addaction(building& b, const char * fname)
{ {
attrib * a = a_add(&b.attribs, a_new(&at_luacall_building)); attrib * a = a_add(&b.attribs, a_new(&at_building_action));
lcbuilding_data * data = (lcbuilding_data*)a->data.v; building_action * data = (building_action*)a->data.v;
data->b = &b; data->b = &b;
data->fname = strdup(fname); data->fname = strdup(fname);
@ -172,7 +119,7 @@ building_units(const building& b) {
void void
bind_building(lua_State * L) bind_building(lua_State * L)
{ {
at_register(&at_luacall_building); at_building_action.age = lc_age;
module(L)[ module(L)[
def("get_building", &findbuilding), def("get_building", &findbuilding),
def("add_building", &add_building), def("add_building", &add_building),