diff --git a/scripts/tests/bindings.lua b/scripts/tests/bindings.lua index 189d34769..9aab6fd57 100644 --- a/scripts/tests/bindings.lua +++ b/scripts/tests/bindings.lua @@ -34,7 +34,6 @@ function test_process() assert_equal("function", _G.type(eressea.process.siege)) assert_equal("function", _G.type(eressea.process.leave)) assert_equal("function", _G.type(eressea.process.promote)) - assert_equal("function", _G.type(eressea.process.renumber)) assert_equal("function", _G.type(eressea.process.restack)) assert_equal("function", _G.type(eressea.process.set_spells)) assert_equal("function", _G.type(eressea.process.set_help)) diff --git a/scripts/tests/orders.lua b/scripts/tests/orders.lua index a0b03f825..dde9508e9 100644 --- a/scripts/tests/orders.lua +++ b/scripts/tests/orders.lua @@ -165,7 +165,7 @@ end function test_process_renumber() u:add_order("NUMMER EINHEIT 'ii'") - eressea.process.renumber() + process_orders() assert_equal(666, u.id) end diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4e600e0d..4616fc8e3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -115,6 +115,7 @@ set (ERESSEA_SRC morale.c monster.c randenc.c + renumber.c volcano.c chaos.c spy.c diff --git a/src/bind_process.c b/src/bind_process.c index e8420c654..2b44eb19d 100755 --- a/src/bind_process.c +++ b/src/bind_process.c @@ -182,11 +182,6 @@ void process_promote(void) { process_cmd(K_PROMOTION, promotion_cmd, 0); } -void process_renumber(void) { - process_cmd(K_NUMBER, renumber_cmd, 0); - renumber_factions(); -} - void process_restack(void) { restack_units(); } diff --git a/src/laws.c b/src/laws.c index 33fd56779..cf365fc7a 100644 --- a/src/laws.c +++ b/src/laws.c @@ -32,6 +32,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "monster.h" #include "move.h" #include "randenc.h" +#include "renumber.h" #include "spy.h" #include "study.h" #include "wormhole.h" @@ -2742,74 +2743,6 @@ void sinkships(struct region * r) } } -static attrib_type at_number = { - "faction_renum", - NULL, NULL, NULL, NULL, NULL, NULL, - ATF_UNIQUE -}; - -void renumber_factions(void) -/* gibt parteien neue nummern */ -{ - struct renum { - struct renum *next; - int want; - faction *faction; - attrib *attrib; - } *renum = NULL, *rp; - faction *f; - for (f = factions; f; f = f->next) { - attrib *a = a_find(f->attribs, &at_number); - int want; - struct renum **rn; - faction *old; - - if (!a) - continue; - want = a->data.i; - if (fval(f, FFL_NEWID)) { - ADDMSG(&f->msgs, msg_message("renumber_twice", "id", want)); - continue; - } - old = findfaction(want); - if (old) { - a_remove(&f->attribs, a); - ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); - continue; - } - if (!faction_id_is_unused(want)) { - a_remove(&f->attribs, a); - ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); - continue; - } - for (rn = &renum; *rn; rn = &(*rn)->next) { - if ((*rn)->want >= want) - break; - } - if (*rn && (*rn)->want == want) { - ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); - } - else { - struct renum *r = calloc(sizeof(struct renum), 1); - r->next = *rn; - r->attrib = a; - r->faction = f; - r->want = want; - *rn = r; - } - } - for (rp = renum; rp; rp = rp->next) { - f = rp->faction; - a_remove(&f->attribs, rp->attrib); - renumber_faction(f, rp->want); - } - while (renum) { - rp = renum->next; - free(renum); - renum = rp; - } -} - void restack_units(void) { region *r; @@ -2897,118 +2830,6 @@ void restack_units(void) } } -int renumber_cmd(unit * u, order * ord) -{ - char token[128]; - const char *s; - int i = 0; - faction *f = u->faction; - - init_order(ord); - s = gettoken(token, sizeof(token)); - switch (findparam_ex(s, u->faction->locale)) { - - case P_FACTION: - s = gettoken(token, sizeof(token)); - if (s && *s) { - int id = atoi36((const char *)s); - attrib *a = a_find(f->attribs, &at_number); - if (!a) - a = a_add(&f->attribs, a_new(&at_number)); - a->data.i = id; - } - break; - - case P_UNIT: - s = gettoken(token, sizeof(token)); - if (s && *s) { - i = atoi36((const char *)s); - if (i <= 0 || i > MAX_UNIT_NR) { - cmistake(u, ord, 114, MSG_EVENT); - break; - } - - if (forbiddenid(i)) { - cmistake(u, ord, 116, MSG_EVENT); - break; - } - - if (findunitg(i, u->region)) { - cmistake(u, ord, 115, MSG_EVENT); - break; - } - } - renumber_unit(u, i); - break; - - case P_SHIP: - if (!u->ship) { - cmistake(u, ord, 144, MSG_EVENT); - break; - } - if (ship_owner(u->ship) != u) { - cmistake(u, ord, 146, MSG_EVENT); - break; - } - if (u->ship->coast != NODIRECTION) { - cmistake(u, ord, 116, MSG_EVENT); - break; - } - s = gettoken(token, sizeof(token)); - if (s == NULL || *s == 0) { - i = newcontainerid(); - } - else { - i = atoi36((const char *)s); - if (i <= 0 || i > MAX_CONTAINER_NR) { - cmistake(u, ord, 114, MSG_EVENT); - break; - } - if (findship(i) || findbuilding(i)) { - cmistake(u, ord, 115, MSG_EVENT); - break; - } - } - sunhash(u->ship); - u->ship->no = i; - shash(u->ship); - break; - case P_BUILDING: - case P_GEBAEUDE: - if (!u->building) { - cmistake(u, ord, 145, MSG_EVENT); - break; - } - if (building_owner(u->building) != u) { - cmistake(u, ord, 148, MSG_EVENT); - break; - } - s = gettoken(token, sizeof(token)); - if (*s == 0) { - i = newcontainerid(); - } - else { - i = atoi36((const char *)s); - if (i <= 0 || i > MAX_CONTAINER_NR) { - cmistake(u, ord, 114, MSG_EVENT); - break; - } - if (findship(i) || findbuilding(i)) { - cmistake(u, ord, 115, MSG_EVENT); - break; - } - } - bunhash(u->building); - u->building->no = i; - bhash(u->building); - break; - - default: - cmistake(u, ord, 239, MSG_EVENT); - } - return 0; -} - /* blesses stone circles create an astral protection in the astral region * above the shield, which prevents chaos suction and other spells. * The shield is created when a magician enters the blessed stone circle, diff --git a/src/laws.h b/src/laws.h index c5e9d573c..5406a50d4 100755 --- a/src/laws.h +++ b/src/laws.h @@ -59,7 +59,6 @@ extern "C" { void defaultorders(void); void quit(void); void monthly_healing(void); - void renumber_factions(void); void restack_units(void); void update_long_order(struct unit *u); void sinkships(struct region * r); @@ -83,7 +82,6 @@ extern "C" { int leave_cmd(struct unit *u, struct order *ord); int pay_cmd(struct unit *u, struct order *ord); int promotion_cmd(struct unit *u, struct order *ord); - int renumber_cmd(struct unit *u, struct order *ord); int combatspell_cmd(struct unit *u, struct order *ord); int contact_cmd(struct unit *u, struct order *ord); int guard_on_cmd(struct unit *u, struct order *ord); diff --git a/src/process.pkg b/src/process.pkg index 5db5c7344..a81917293 100755 --- a/src/process.pkg +++ b/src/process.pkg @@ -25,7 +25,6 @@ module eressea { void process_leave @ leave(void); /* LEAVE */ void process_maintenance @ maintenance(void); /* PAY */ void process_promote @ promote(void); /* PROMOTE */ - void process_renumber @ renumber(void); /* RENUMBER */ void process_restack @ restack(void); /* SORT */ void process_setspells @ set_spells(void); /* COMBATSPELL */ void process_sethelp @ set_help(void); /* HELP */ diff --git a/src/process.pkg.c b/src/process.pkg.c index 787258a96..42dff20ff 100644 --- a/src/process.pkg.c +++ b/src/process.pkg.c @@ -531,30 +531,6 @@ static int tolua_process_eressea_process_promote00(lua_State* tolua_S) #endif } -/* function: process_renumber */ -static int tolua_process_eressea_process_renumber00(lua_State* tolua_S) -{ -#ifndef TOLUA_RELEASE - tolua_Error tolua_err; - if ( - !tolua_isnoobj(tolua_S,1,&tolua_err) - ) - goto tolua_lerror; - else -#endif - { - { - process_renumber(); - } - } - return 0; -#ifndef TOLUA_RELEASE - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'renumber'.",&tolua_err); - return 0; -#endif -} - /* function: process_restack */ static int tolua_process_eressea_process_restack00(lua_State* tolua_S) { @@ -1021,7 +997,6 @@ LUALIB_API int luaopen_process (lua_State* tolua_S) tolua_function(tolua_S,"leave",tolua_process_eressea_process_leave00); tolua_function(tolua_S,"maintenance",tolua_process_eressea_process_maintenance00); tolua_function(tolua_S,"promote",tolua_process_eressea_process_promote00); - tolua_function(tolua_S,"renumber",tolua_process_eressea_process_renumber00); tolua_function(tolua_S,"restack",tolua_process_eressea_process_restack00); tolua_function(tolua_S,"set_spells",tolua_process_eressea_process_set_spells00); tolua_function(tolua_S,"set_help",tolua_process_eressea_process_set_help00); diff --git a/src/renumber.c b/src/renumber.c new file mode 100644 index 000000000..259dfcc2b --- /dev/null +++ b/src/renumber.c @@ -0,0 +1,198 @@ +#include +#include "renumber.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +static attrib_type at_number = { + "faction_renum", + NULL, NULL, NULL, NULL, NULL, NULL, + ATF_UNIQUE +}; + +void renumber_factions(void) +/* gibt parteien neue nummern */ +{ + struct renum { + struct renum *next; + int want; + faction *faction; + attrib *attrib; + } *renum = NULL, *rp; + faction *f; + for (f = factions; f; f = f->next) { + attrib *a = a_find(f->attribs, &at_number); + int want; + struct renum **rn; + faction *old; + + if (!a) + continue; + want = a->data.i; + if (fval(f, FFL_NEWID)) { + ADDMSG(&f->msgs, msg_message("renumber_twice", "id", want)); + continue; + } + old = findfaction(want); + if (old) { + a_remove(&f->attribs, a); + ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); + continue; + } + if (!faction_id_is_unused(want)) { + a_remove(&f->attribs, a); + ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); + continue; + } + for (rn = &renum; *rn; rn = &(*rn)->next) { + if ((*rn)->want >= want) + break; + } + if (*rn && (*rn)->want == want) { + ADDMSG(&f->msgs, msg_message("renumber_inuse", "id", want)); + } + else { + struct renum *r = calloc(sizeof(struct renum), 1); + r->next = *rn; + r->attrib = a; + r->faction = f; + r->want = want; + *rn = r; + } + } + for (rp = renum; rp; rp = rp->next) { + f = rp->faction; + a_remove(&f->attribs, rp->attrib); + renumber_faction(f, rp->want); + } + while (renum) { + rp = renum->next; + free(renum); + renum = rp; + } +} + +int renumber_cmd(unit * u, order * ord) +{ + char token[128]; + const char *s; + int i = 0; + faction *f = u->faction; + + init_order(ord); + s = gettoken(token, sizeof(token)); + switch (findparam_ex(s, u->faction->locale)) { + + case P_FACTION: + s = gettoken(token, sizeof(token)); + if (s && *s) { + int id = atoi36((const char *)s); + attrib *a = a_find(f->attribs, &at_number); + if (!a) + a = a_add(&f->attribs, a_new(&at_number)); + a->data.i = id; + } + break; + + case P_UNIT: + s = gettoken(token, sizeof(token)); + if (s && *s) { + i = atoi36((const char *)s); + if (i <= 0 || i > MAX_UNIT_NR) { + cmistake(u, ord, 114, MSG_EVENT); + break; + } + + if (forbiddenid(i)) { + cmistake(u, ord, 116, MSG_EVENT); + break; + } + + if (findunitg(i, u->region)) { + cmistake(u, ord, 115, MSG_EVENT); + break; + } + } + renumber_unit(u, i); + break; + + case P_SHIP: + if (!u->ship) { + cmistake(u, ord, 144, MSG_EVENT); + break; + } + if (ship_owner(u->ship) != u) { + cmistake(u, ord, 146, MSG_EVENT); + break; + } + if (u->ship->coast != NODIRECTION) { + cmistake(u, ord, 116, MSG_EVENT); + break; + } + s = gettoken(token, sizeof(token)); + if (s == NULL || *s == 0) { + i = newcontainerid(); + } + else { + i = atoi36((const char *)s); + if (i <= 0 || i > MAX_CONTAINER_NR) { + cmistake(u, ord, 114, MSG_EVENT); + break; + } + if (findship(i) || findbuilding(i)) { + cmistake(u, ord, 115, MSG_EVENT); + break; + } + } + sunhash(u->ship); + u->ship->no = i; + shash(u->ship); + break; + case P_BUILDING: + case P_GEBAEUDE: + if (!u->building) { + cmistake(u, ord, 145, MSG_EVENT); + break; + } + if (building_owner(u->building) != u) { + cmistake(u, ord, 148, MSG_EVENT); + break; + } + s = gettoken(token, sizeof(token)); + if (*s == 0) { + i = newcontainerid(); + } + else { + i = atoi36((const char *)s); + if (i <= 0 || i > MAX_CONTAINER_NR) { + cmistake(u, ord, 114, MSG_EVENT); + break; + } + if (findship(i) || findbuilding(i)) { + cmistake(u, ord, 115, MSG_EVENT); + break; + } + } + bunhash(u->building); + u->building->no = i; + bhash(u->building); + break; + + default: + cmistake(u, ord, 239, MSG_EVENT); + } + return 0; +} + diff --git a/src/renumber.h b/src/renumber.h new file mode 100644 index 000000000..244b8043f --- /dev/null +++ b/src/renumber.h @@ -0,0 +1,12 @@ +#pragma once + +#ifndef H_RENUMBER +#define H_RENUMBER + + struct unit; + struct order; + + void renumber_factions(void); + int renumber_cmd(struct unit *u, struct order *ord); + +#endif