at_variable is as good as gone (just a stub for compat)

This commit is contained in:
Enno Rehling 2008-12-20 20:58:35 +00:00
parent 15d08ab901
commit 71f5bc7b6f
13 changed files with 92 additions and 150 deletions

View File

@ -25,103 +25,21 @@
#include <string.h>
#include <assert.h>
typedef struct {
char *key;
char *value;
} variable;
static void
initialize_variable(struct attrib * a)
{
a->data.v = malloc(sizeof(variable));
}
static void
finalize_variable(struct attrib * a)
{
free(a->data.v);
}
static void
write_variable(const struct attrib * a, storage * store)
{
variable * var = (variable *)a->data.v;
store->w_tok(store, var->key);
store->w_str(store, var->value);
}
static int
read_variable(struct attrib *a, storage * store)
{
variable * var = (variable *)a->data.v;
char * key = store->r_tok(store);
char * value = store->r_str(store);
free(key);
free(value);
var->key = store->r_tok(store);
var->value = store->r_str(store);
return AT_READ_OK;
return AT_READ_FAIL;
}
attrib_type at_variable = {
"variable", initialize_variable, finalize_variable, NULL,
write_variable, read_variable
"variable", NULL, NULL, NULL,
NULL, read_variable
};
const char *
get_variable(attrib *a, const char *key)
{
attrib *ap = a_find(a, &at_variable);
while (ap && ap->type==&at_variable) {
variable * var = (variable *)ap->data.v;
if (strcmp(key, var->key) == 0) {
return var->value;
}
ap = ap->next;
}
return NULL;
}
void
set_variable(attrib **app, const char *key, const char *value)
{
attrib *ap = a_find(*app, &at_variable);
assert(value);
while (ap && ap->type==&at_variable) {
variable * var = (variable *)ap->data.v;
if (strcmp(key, var->key) == 0) {
free(var->value);
var->value = strdup(value);
return;
}
ap = ap->next;
}
ap = a_add(app, a_new(&at_variable));
((variable *)ap->data.v)->key = strdup(key);
((variable *)ap->data.v)->value = strdup(value);
}
void
delete_variable(attrib **app, const char *key)
{
attrib *ap = a_find(*app, &at_variable);
while (ap && ap->type==&at_variable) {
variable * var = (variable *)ap->data.v;
if (strcmp(key, var->key) == 0) {
free(var->key);
free(var->value);
a_remove(app, ap);
break;
}
ap = ap->next;
}
}
void
init_variable(void)
{

View File

@ -17,9 +17,6 @@
extern "C" {
#endif
const char *get_variable(struct attrib *a, const char *key);
void set_variable(struct attrib **app, const char *key, const char *value);
void delete_variable(struct attrib **app, const char *key);
void init_variable(void);
#ifdef __cplusplus

View File

@ -66,7 +66,7 @@
/* attributes includes */
#include <attributes/racename.h>
#include <attributes/raceprefix.h>
#include <attributes/variable.h>
#include <attributes/object.h>
/* util includes */
#include <util/attrib.h>
@ -984,8 +984,9 @@ quit_cmd(unit * u, struct order * ord)
cmistake(u, ord, 316, MSG_EVENT);
return 0;
} else {
const char * token = itoa36(f2_id);
set_variable(&f->attribs, "quit", token);
variant var;
var.i = f2_id;
object_create("quit", TINTEGER, var);
}
}
}
@ -1006,14 +1007,21 @@ quit(void)
faction * f = *fptr;
if (f->flags & FFL_QUIT) {
if (EnhancedQuit()) {
const char * token = get_variable(f->attribs, "quit");
if(token != NULL) {
int f2_id = atoi36(token);
faction *f2 = findfaction(f2_id);
attrib * a = a_find(f->attribs, &at_object);
if (a) {
variant var;
object_type type;
var.i = 0;
object_get(a, &type, &var);
assert(var.i && type==TINTEGER);
if (var.i) {
int f2_id = var.i;
faction *f2 = findfaction(f2_id);
assert(f2_id>0);
assert(f2!=NULL);
transfer_faction(f, f2);
assert(f2_id>0);
assert(f2!=NULL);
transfer_faction(f, f2);
}
}
}
destroyfaction(f);

View File

@ -132,24 +132,6 @@ faction_setpolicy(faction * a, faction * b, const char * flag, bool value)
}
}
static const char *
faction_get_variable(faction * f, const char *key)
{
return get_variable(f->attribs, key);
}
static void
faction_set_variable(faction * f, const char *key, const char *value)
{
set_variable(&f->attribs, key, value);
}
static void
faction_delete_variable(faction * f, const char *key)
{
return delete_variable(&f->attribs, key);
}
static int
faction_additem(faction * f, const char * iname, int number)
{
@ -290,7 +272,6 @@ bind_faction(lua_State * L)
module(L)[
def("factions", &get_factions, return_stl_iterator),
def("get_faction", &findfaction),
def("add_faction", &add_faction),
def("faction_origin", &faction_getorigin, pure_out_value(_2) + pure_out_value(_3)),
class_<struct faction>("faction")
@ -299,11 +280,6 @@ bind_faction(lua_State * L)
.def("set_policy", &faction_setpolicy)
.def("get_policy", &faction_getpolicy)
// temporary variables
.def("set_variable", &faction_set_variable)
.def("get_variable", &faction_get_variable)
.def("delete_variable", &faction_delete_variable)
// heroes
.def("heroes", &faction_countheroes)
.def("max_heroes", &faction_maxheroes)

View File

@ -147,7 +147,10 @@ tolua_building_open(lua_State* tolua_S)
tolua_variable(tolua_S, "units", tolua_building_get_units, NULL);
tolua_variable(tolua_S, "region", tolua_building_get_region, tolua_building_set_region);
tolua_function(tolua_S, "add_action", tolua_building_addaction);
#ifdef TODO
.property("type", &building_gettype)
.def_readwrite("size", &building::size)
#endif
tolua_variable(tolua_S, "objects", tolua_building_get_objects, 0);
tolua_function(tolua_S, "create", tolua_building_create);

View File

@ -423,7 +423,38 @@ tolua_faction_open(lua_State* tolua_S)
tolua_function(tolua_S, "renumber", tolua_faction_renumber);
tolua_function(tolua_S, "create", tolua_faction_create);
#ifdef TODO
def("faction_origin", &faction_getorigin, pure_out_value(_2) + pure_out_value(_3)),
// heroes
.def("heroes", &faction_countheroes)
.def("max_heroes", &faction_maxheroes)
.def_readonly("name", &faction::name)
.def_readonly("score", &faction::score)
.def_readonly("id", &faction::no)
.def_readwrite("age", &faction::age)
.def_readwrite("options", &faction::options)
.def_readwrite("flags", &faction::flags)
.def_readwrite("subscription", &faction::subscription)
.def_readwrite("lastturn", &faction::lastorders)
.def("add_item", &faction_additem)
.property("items", &faction_items, return_stl_iterator)
.property("x", &faction_getorigin_x, &faction_setorigin_x)
.property("y", &faction_getorigin_y, &faction_setorigin_y)
.def("renum", &faction_renumber)
.def("add_notice", &faction_addnotice)
.property("password", &faction_get_passw, &faction_set_passw)
.property("info", &faction_get_banner, &faction_set_banner)
.property("email", &faction_get_email, &faction_set_email)
.property("locale", &faction_getlocale, &faction_setlocale)
.property("units", &faction_units, return_stl_iterator)
.property("alliance", &faction_getalliance, &faction_setalliance)
.property("race", &faction_getrace, &faction_setrace)
.property("objects", &eressea::get_objects<faction>)
#endif
tolua_variable(tolua_S, "objects", tolua_faction_get_objects, NULL);
}
tolua_endmodule(tolua_S);

View File

@ -119,7 +119,16 @@ tolua_ship_open(lua_State* tolua_S)
tolua_variable(tolua_S, "id", tolua_ship_get_id, NULL);
tolua_variable(tolua_S, "name", tolua_ship_get_name, tolua_ship_set_name);
tolua_variable(tolua_S, "units", tolua_ship_get_units, NULL);
#ifdef TODO
.property("type", &ship_gettype)
.property("weight", &ship_getweight)
.property("capacity", &ship_getcapacity)
.property("maxsize", &ship_maxsize)
.property("region", &ship_getregion, &ship_setregion)
.def_readwrite("damage", &ship::damage)
.def_readwrite("size", &ship::size)
.def_readwrite("coast", &ship::coast)
#endif
tolua_variable(tolua_S, "objects", tolua_ship_get_objects, 0);
tolua_function(tolua_S, "create", tolua_ship_create);

View File

@ -52,7 +52,7 @@ end
function test_reorder()
r = terraform(0, 0, "plain")
f = add_faction("enno@ix.de", "orc", "de")
f = faction.create("enno@ix.de", "orc", "de")
s1 = add_ship(r, "boat")
s1.size = 1
s2 = add_ship(r, "boat")

View File

@ -33,13 +33,13 @@ function test_movement()
r3:set_road(east, 1.0)
r4:set_road(west, 1.0)
orcs = add_faction("orcs@eressea.de", "orc", "de")
orcs = faction.create("orcs@eressea.de", "orc", "de")
orcs.age = 20
aqua = add_faction("aqua@eressea.de", "aquarian", "de")
aqua = faction.create("aqua@eressea.de", "aquarian", "de")
aqua.age = 20
bugs = add_faction("bugz@eressea.de", "insect", "de")
bugs = faction.create("bugz@eressea.de", "insect", "de")
bugs.age = 20
orc = mkunit(orcs, r0, 10)
@ -139,7 +139,7 @@ end
function test_sail()
r0 = terraform(0, 0, "plain")
orcs = add_faction("enno@eressea.de", "orc", "de")
orcs = faction.create("enno@eressea.de", "orc", "de")
orcs.age = 20
orc = add_unit(orcs, r0)
@ -169,7 +169,7 @@ function test_handler()
plain = terraform(0, 0, "plain")
skill = 8
f = add_faction("enno@eressea.de", "orc", "de")
f = faction.create("enno@eressea.de", "orc", "de")
f.age = 20
u = add_unit(f, plain)
@ -180,7 +180,7 @@ function test_handler()
u:add_handler("message", msg_handler)
msg = "BOTSCHAFT EINHEIT " .. itoa36(u.id) .. " Du~Elf~stinken"
f = add_faction("enno@eressea.de", "elf", "de")
f = faction.create("enno@eressea.de", "elf", "de")
f.age = 20
u = add_unit(f, plain)
@ -197,7 +197,7 @@ function test_combat()
plain = terraform(0, 0, "plain")
skill = 8
f = add_faction("enno@eressea.de", "orc", "de")
f = faction.create("enno@eressea.de", "orc", "de")
f.age = 20
u = add_unit(f, plain)
@ -211,7 +211,7 @@ function test_combat()
u:add_order("BEFÖRDERUNG")
attack = "ATTACKIERE " .. itoa36(u.id)
f = add_faction("enno@eressea.de", "elf", "de")
f = faction.create("enno@eressea.de", "elf", "de")
f.age = 20
u = add_unit(f, plain)
@ -232,7 +232,7 @@ function test_rewards()
plain = terraform(0, 0, "plain")
skill = 5
f = add_faction("enno@eressea.de", "human", "de")
f = faction.create("enno@eressea.de", "human", "de")
f.age = 20
u = add_unit(f, plain)
u.number = 10
@ -253,7 +253,7 @@ function test_rewards()
u:add_order("MACHEN Elfenbogen")
u:add_order("NUMMER PARTEI test")
f = add_faction("enno@eressea.de", "elf", "de")
f = faction.create("enno@eressea.de", "elf", "de")
f.age = 20
u = add_unit(f, plain)
u.number = 7
@ -297,7 +297,7 @@ end
function test_give()
plain = terraform(0, 0, "plain")
f = add_faction("enno@eressea.de", "human", "de")
f = faction.create("enno@eressea.de", "human", "de")
f.age = 20
u = add_unit(f, plain)
u.number = 10
@ -345,7 +345,7 @@ function test_parser()
plain = terraform(0, 0, "plain")
skill = 5
f = add_faction("enno@eressea.de", "human", "de")
f = faction.create("enno@eressea.de", "human", "de")
f.age = 20
u = add_unit(f, plain)
u.number = 10
@ -359,7 +359,7 @@ function test_fail()
plain = terraform(0, 0, "plain")
skill = 5
f = add_faction("enno@eressea.de", "human", "de")
f = faction.create("enno@eressea.de", "human", "de")
print(f)
end

View File

@ -53,7 +53,7 @@ function make_faction(position, alliance, number, email, race)
local units = (1+skillno)*6 / number -- jede allianz kriegt 168 leute
local money = units * 5 * 10 -- jede allianz kriegt 8400 silber
local f = add_faction(email, race, "de")
local f = faction.create(email, race, "de")
if f == nil then
print("could not create " .. email .. " " .. race)
return

View File

@ -22,7 +22,7 @@ function sphinx_handler()
hintText[14] = "Das Schiff mit dem Stern im Wappen liegt neben dem des Kriegers, der einen Zweihänder führt"
for i=0,4,1 do
possibleHint[i] = u:faction:get_variable("sphinxhint"..tostring(i))
possibleHint[i] = u:faction.objects:get("sphinxhint"..tostring(i))
end
hint = math.random(0,4)
@ -34,7 +34,7 @@ function sphinx_handler()
str = evt:get_string(0)
u2 = evt:get_unit(1)
if str.lower() == "hinweis" then
if u2:faction:get_variable("sphinxGotHint"..itoa36(u.id) then
if u2:faction.objects:get("sphinxGotHint"..itoa36(u.id) then
send_gotHint(u2)
else
send_hint(u2, u)

View File

@ -7,12 +7,12 @@ function init_sphinxhints()
hints[i] = 0
end
for i=0,4,1 do
if faction:get_variable("sphinxhint"..tostring(i)) == nil then
if faction.objects:get("sphinxhint"..tostring(i)) == nil then
repeat
hint = math.random(0,14)
until hints[hint] = 0
hints[hint] = 1
faction:set_variable("sphinxhint"..tostring(i),tostring(hint))
faction.objects:set("sphinxhint"..tostring(i), tostring(hint))
end
end
end

View File

@ -12,12 +12,12 @@ function init_sphinxhints()
hints[i] = 0
end
for i=0,4,1 do
if f:get_variable("sphinxhint"..tostring(i)) == nil then
if f.objects:get("sphinxhint"..tostring(i)) == nil then
repeat
hint = math.random(0,14)
until hints[hint] == 0
hints[hint] = 1
f:set_variable("sphinxhint"..tostring(i),tostring(hint))
f.objects:set("sphinxhint" .. tostring(i), tostring(hint))
end
end
end
@ -74,7 +74,7 @@ function sphinx_handler()
str = evt:get_string(0)
u2 = evt:get_unit(1)
if string.lower(str) == "hinweis" then
if u2.faction:get_variable("sphinxGotHint"..itoa36(u.id)) ~= nil then
if u2.faction.objects:get("sphinxGotHint"..itoa36(u.id)) ~= nil then
send_gotHint(u2)
else
send_hint(u2, u)