move renumber command to a separate module.

This commit is contained in:
Enno Rehling 2016-11-15 23:34:20 +01:00
parent 772bc427aa
commit 17365edff7
10 changed files with 213 additions and 215 deletions

View File

@ -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))

View File

@ -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

View File

@ -115,6 +115,7 @@ set (ERESSEA_SRC
morale.c
monster.c
randenc.c
renumber.c
volcano.c
chaos.c
spy.c

View File

@ -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();
}

View File

@ -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,

View File

@ -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);

View File

@ -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 */

View File

@ -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);

198
src/renumber.c Normal file
View File

@ -0,0 +1,198 @@
#include <platform.h>
#include "renumber.h"
#include <kernel/config.h>
#include <kernel/faction.h>
#include <kernel/building.h>
#include <kernel/ship.h>
#include <kernel/unit.h>
#include <kernel/order.h>
#include <kernel/messages.h>
#include <util/attrib.h>
#include <util/base36.h>
#include <util/parser.h>
#include <assert.h>
#include <stdlib.h>
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;
}

12
src/renumber.h Normal file
View File

@ -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