diff --git a/src/battle.c b/src/battle.c index 018763093..eb6d06155 100644 --- a/src/battle.c +++ b/src/battle.c @@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "move.h" #include "laws.h" #include "skill.h" +#include "monster.h" #include #include diff --git a/src/bind_faction.c b/src/bind_faction.c index 163685a36..8ac87c1d4 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -14,6 +14,7 @@ without prior permission by the authors of Eressea. #include "bind_faction.h" #include "bind_unit.h" #include "bindings.h" +#include "helpers.h" #include #include @@ -325,6 +326,14 @@ static int tolua_faction_destroy(lua_State * L) return 0; } +static int tolua_faction_get(lua_State * L) +{ + int no = tolua_toid(L, 1, 0); + faction *f = findfaction(no); + tolua_pushusertype(L, f, TOLUA_CAST "faction"); + return 1; +} + static int tolua_faction_create(lua_State * L) { const char *email = tolua_tostring(L, 1, 0); @@ -510,7 +519,10 @@ void tolua_faction_open(lua_State * L) tolua_module(L, NULL, 0); tolua_beginmodule(L, NULL); { - tolua_cclass(L, TOLUA_CAST "faction", TOLUA_CAST "faction", TOLUA_CAST "", + tolua_beginmodule(L, TOLUA_CAST "eressea"); + tolua_function(L, TOLUA_CAST "get_faction", tolua_faction_get); + tolua_endmodule(L); + tolua_cclass(L, TOLUA_CAST "faction", TOLUA_CAST "faction", TOLUA_CAST "", NULL); tolua_beginmodule(L, TOLUA_CAST "faction"); { @@ -561,9 +573,10 @@ void tolua_faction_open(lua_State * L) tolua_function(L, TOLUA_CAST "add_item", tolua_faction_add_item); tolua_variable(L, TOLUA_CAST "items", tolua_faction_get_items, NULL); - tolua_function(L, TOLUA_CAST "renumber", &tolua_faction_renumber); - tolua_function(L, TOLUA_CAST "create", &tolua_faction_create); - tolua_function(L, TOLUA_CAST "destroy", &tolua_faction_destroy); + tolua_function(L, TOLUA_CAST "renumber", tolua_faction_renumber); + tolua_function(L, TOLUA_CAST "create", tolua_faction_create); + tolua_function(L, TOLUA_CAST "get", tolua_faction_get); + tolua_function(L, TOLUA_CAST "destroy", tolua_faction_destroy); #ifdef TODO def("faction_origin", &faction_getorigin, pure_out_value(_2) + pure_out_value(_3)),.def_readwrite("subscription", diff --git a/src/bind_monsters.c b/src/bind_monsters.c index dc7bc012b..49b05ccc9 100644 --- a/src/bind_monsters.c +++ b/src/bind_monsters.c @@ -1,5 +1,6 @@ #include #include "spells/shipcurse.h" +#include "monster.h" #include #include diff --git a/src/bindings.c b/src/bindings.c index cb7da3899..87eab4c34 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -565,14 +565,6 @@ static int tolua_read_turn(lua_State * L) return 1; } -static int tolua_get_faction(lua_State * L) -{ - int no = tolua_toid(L, 1, 0); - faction *f = findfaction(no); - tolua_pushusertype(L, f, TOLUA_CAST "faction"); - return 1; -} - static int tolua_get_region(lua_State * L) { int x = (int)tolua_tonumber(L, 1, 0); @@ -1137,7 +1129,6 @@ int tolua_bindings_open(lua_State * L) tolua_function(L, TOLUA_CAST "get_ship", &config_get_stype); } tolua_endmodule(L); tolua_function(L, TOLUA_CAST "get_region_by_id", tolua_get_region_byid); - tolua_function(L, TOLUA_CAST "get_faction", tolua_get_faction); tolua_function(L, TOLUA_CAST "get_unit", tolua_get_unit); tolua_function(L, TOLUA_CAST "get_alliance", tolua_get_alliance); tolua_function(L, TOLUA_CAST "get_ship", tolua_get_ship); diff --git a/src/economy.c b/src/economy.c index d5cc4eca5..02c609a73 100644 --- a/src/economy.c +++ b/src/economy.c @@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "randenc.h" #include "spy.h" #include "move.h" +#include "monster.h" #include "reports.h" /* kernel includes */ diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 8204afd67..dbbd24aa0 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -143,24 +143,6 @@ void set_show_item(faction * f, const struct item_type *itype) a->data.v = (void *)itype; } -faction *get_or_create_monsters(void) -{ - faction *f = findfaction(666); - if (!f) { - const race *rc = rc_get_or_create("dragon"); - const char *email = get_param(global.parameters, "monster.email"); - f = addfaction(email ? email : "noreply@eressea.de", NULL, rc, NULL, 0); - renumber_faction(f, 666); - faction_setname(f, "Monster"); - fset(f, FFL_NPC | FFL_NOIDLEOUT); - } - return f; -} - -faction *get_monsters(void) { - return get_or_create_monsters(); -} - const unit *random_unit_in_faction(const faction * f) { unit *u; @@ -632,7 +614,7 @@ void remove_empty_factions(void) /* monster (0) werden nicht entfernt. alive kann beim readgame * () auf 0 gesetzt werden, wenn monsters keine einheiten mehr * haben. */ - if ((f->units == NULL || f->alive == 0) && !is_monsters(f)) { + if ((f->units == NULL || f->alive == 0) && !fval(f, FFL_NOIDLEOUT)) { ursprung *ur = f->ursprung; while (ur && ur->id != 0) ur = ur->next; diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 0ec42fe5c..3c110424c 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -54,8 +54,6 @@ extern "C" { #define FFL_SAVEMASK (FFL_DEFENDER|FFL_NEWID|FFL_GM|FFL_NPC|FFL_DBENTRY|FFL_NOIDLEOUT) -#define is_monsters(f) (f && fval(f, FFL_NPC) && f==get_monsters()) - typedef struct faction { struct faction *next; struct faction *nexthash; @@ -116,8 +114,6 @@ void fhash(struct faction *f); void funhash(struct faction *f); struct faction *findfaction(int n); -struct faction *get_monsters(void); -struct faction *get_or_create_monsters(void); int max_magicians(const faction * f); void set_show_item(faction * f, const struct item_type *itype); diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 644fb83e0..b0d0d2ee2 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -7,6 +7,7 @@ #include #include +#include "monster.h" #include #include diff --git a/src/kernel/messages.c b/src/kernel/messages.c index 654916971..d70423251 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -257,7 +257,7 @@ message * cmistake(const unit * u, struct order *ord, int mno, int mtype) static char msgname[20]; unused_arg(mtype); - if (is_monsters(u->faction)) + if (fval(u->faction, FFL_NPC)) return 0; sprintf(msgname, "error%d", mno); result = msg_feedback(u, ord, msgname, ""); diff --git a/src/kernel/save.c b/src/kernel/save.c index a7d6ce0c2..fcaa4897b 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -222,7 +222,7 @@ static faction *factionorders(void) f = findfaction(fid); - if (f != NULL && !is_monsters(f)) { + if (f != NULL && !fval(f, FFL_NPC)) { const char *pass = getstrtoken(); if (!checkpasswd(f, (const char *)pass, true)) { @@ -499,7 +499,6 @@ static int resolve_owner(variant id, void *address) f = findfaction(id.i); if (f == NULL) { log_error("region has an invalid owner (%s)\n", itoa36(id.i)); - f = get_monsters(); } } owner->owner = f; @@ -1338,9 +1337,10 @@ faction *readfaction(struct gamedata * data) READ_INT(data->store, &n); f->options = n; - if ((n & (want(O_REPORT) | want(O_COMPUTER))) == 0 && !is_monsters(f)) { + n = want(O_REPORT) | want(O_COMPUTER); + if ((f->options & n) == 0) { /* Kein Report eingestellt, Fehler */ - f->options |= (want(O_REPORT) | want(O_ZUGVORLAGE)); + f->options |= n; } sfp = &f->allies; @@ -1720,7 +1720,7 @@ int readgame(const char *filename, int backup) if (mage) { faction *f = u->faction; int skl = effskill(u, SK_MAGIC); - if (!is_monsters(f) && f->magiegebiet == M_GRAY) { + if (!fval(f, FFL_NPC) && f->magiegebiet == M_GRAY) { log_error("faction %s had magic=gray, fixing (%s)\n", factionname(f), magic_school[mage->magietyp]); f->magiegebiet = mage->magietyp; } @@ -1778,9 +1778,8 @@ int readgame(const char *filename, int backup) return 0; } -static void clear_monster_orders(void) +static void clear_npc_orders(faction *f) { - faction *f = get_monsters(); if (f) { unit *u; for (u = f->units; u; u = u->nextF) { @@ -1804,7 +1803,6 @@ int writegame(const char *filename) stream strm; FILE *F; - clear_monster_orders(); sprintf(path, "%s/%s", datapath(), filename); #ifdef HAVE_UNISTD_H if (access(path, R_OK) == 0) { @@ -1884,6 +1882,9 @@ int writegame(const char *filename) log_printf(stdout, " - Schreibe %d Parteien...\n", n); for (f = factions; f; f = f->next) { + if (fval(f, FFL_NPC)) { + clear_npc_orders(f); + } writefaction(&gdata, f); WRITE_SECTION(&store); } diff --git a/src/kernel/teleport.c b/src/kernel/teleport.c index 2bec55440..2fc7794f1 100644 --- a/src/kernel/teleport.c +++ b/src/kernel/teleport.c @@ -34,6 +34,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +#include "monster.h" + /* libc includes */ #include diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 87648994b..460a941bc 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -362,14 +362,6 @@ int gift_items(unit * u, int flags) return retval; } -void make_zombie(unit * u) -{ - u_setfaction(u, get_monsters()); - scale_number(u, 1); - u_setrace(u, get_race(RC_ZOMBIE)); - u->irace = NULL; -} - /** remove the unit from the list of active units. * the unit is not actually freed, because there may still be references * dangling to it (from messages, for example). To free all removed units, @@ -733,7 +725,7 @@ void set_level(unit * u, skill_t sk, int value) { skill *sv = u->skills; - assert(sk != SK_MAGIC || !u->faction || u->number == 1 || is_monsters(u->faction)); + assert(sk != SK_MAGIC || !u->faction || u->number == 1 || fval(u->faction, FFL_NPC)); if (!skill_enabled(sk)) return; @@ -1197,7 +1189,7 @@ skill *add_skill(unit * u, skill_t id) sv->weeks = 1; sv->old = 0; sv->id = id; - if (id == SK_MAGIC && u->faction && !is_monsters(u->faction)) { + if (id == SK_MAGIC && u->faction && !fval(u->faction, FFL_NPC)) { assert(u->number==1 && max_magicians(u->faction) >= u->number); } return sv; diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index fbf4f32ac..7083a85c9 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -404,7 +404,7 @@ get_island_info(region * root, int *size_p, int *inhabited_p, int *maxage_p) if (r->units) { unit *u; for (u = r->units; u; u = u->next) { - if (!is_monsters(u->faction) && u->faction->age > maxage) { + if (!fval(u->faction, FFL_NOIDLEOUT) && u->faction->age > maxage) { maxage = u->faction->age; } } diff --git a/src/modules/score.c b/src/modules/score.c index 5bad3ab68..da9fc10a0 100644 --- a/src/modules/score.c +++ b/src/modules/score.c @@ -46,7 +46,7 @@ int average_score_of_age(int age, int a) int sum = 0, count = 0; for (f = factions; f; f = f->next) { - if (!is_monsters(f) && f->age <= age + a + if (!fval(f, FFL_NPC) && f->age <= age + a && f->age >= age - a && f->race != get_race(RC_TEMPLATE)) { sum += f->score; count++; @@ -143,7 +143,7 @@ void score(void) for (fc = factions; fc; fc = fc->next) { fc->score = fc->score / 5; - if (!is_monsters(fc) && fc->race != get_race(RC_TEMPLATE)) { + if (!fval(fc, FFL_NPC) && fc->race != get_race(RC_TEMPLATE)) { allscores += fc->score; } } diff --git a/src/monster.c b/src/monster.c index c162fe3cd..1dd81b06e 100644 --- a/src/monster.c +++ b/src/monster.c @@ -216,3 +216,30 @@ void monster_kills_peasants(unit * u) } } } + +faction *get_or_create_monsters(void) +{ + faction *f = findfaction(MONSTER_ID); + if (!f) { + const race *rc = rc_get_or_create("dragon"); + const char *email = get_param(global.parameters, "monster.email"); + f = addfaction(email ? email : "noreply@eressea.de", NULL, rc, NULL, 0); + renumber_faction(f, MONSTER_ID); + faction_setname(f, "Monster"); + fset(f, FFL_NPC | FFL_NOIDLEOUT); + } + return f; +} + +faction *get_monsters(void) { + return get_or_create_monsters(); +} + +void make_zombie(unit * u) +{ + u_setfaction(u, get_monsters()); + scale_number(u, 1); + u_setrace(u, get_race(RC_ZOMBIE)); + u->irace = NULL; +} + diff --git a/src/monster.h b/src/monster.h index 27a8f23f7..02eab5519 100644 --- a/src/monster.h +++ b/src/monster.h @@ -22,8 +22,16 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif +#define MONSTER_ID 666 + void monster_kills_peasants(struct unit *u); bool monster_is_waiting(const struct unit *u); + struct faction *get_monsters(void); + struct faction *get_or_create_monsters(void); + void make_zombie(struct unit * u); + +#define is_monsters(f) (f && fval(f, FFL_NPC) && f==get_monsters()) + #ifdef __cplusplus } diff --git a/src/move.c b/src/move.c index 4a9bd7ba7..c4b6b4202 100644 --- a/src/move.c +++ b/src/move.c @@ -24,6 +24,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "reports.h" #include "alchemy.h" #include "vortex.h" +#include "monster.h" #include #include diff --git a/src/races/zombies.c b/src/races/zombies.c index 9f1d6c764..97a826c3f 100644 --- a/src/races/zombies.c +++ b/src/races/zombies.c @@ -24,6 +24,8 @@ /* util iclude */ #include +#include "monster.h" + /* libc includes */ #include diff --git a/src/randenc.c b/src/randenc.c index e4bd79c46..93f3bb6a8 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -1139,13 +1139,6 @@ static void demon_skillchanges(void) while (weeks--) learn_skill(u, sv->id, 1.0); } - if (sv->old > sv->level) { - if (verbosity >= 3) { - log_printf(stdout, "%s dropped from %u to %u:%u in %s\n", - unitname(u), sv->old, sv->level, sv->weeks, skillname(sv->id, - NULL)); - } - } } ++sv; } diff --git a/src/report.c b/src/report.c index 4b2765158..9f0c6ba32 100644 --- a/src/report.c +++ b/src/report.c @@ -23,6 +23,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "reports.h" #include "laws.h" +#include "monster.h" + /* modules includes */ #include diff --git a/src/reports.c b/src/reports.c index 54e072a46..f22165e0e 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1688,7 +1688,7 @@ static void nmr_warnings(void) faction *f, *fa; #define FRIEND (HELP_GUARD|HELP_MONEY) for (f = factions; f; f = f->next) { - if (!is_monsters(f) && (turn - f->lastorders) >= 2) { + if (!fval(f, FFL_NOIDLEOUT) && (turn - f->lastorders) >= 2) { message *msg = NULL; for (fa = factions; fa; fa = fa->next) { int warn = 0; diff --git a/src/spells.c b/src/spells.c index daf114640..7929b004f 100644 --- a/src/spells.c +++ b/src/spells.c @@ -20,6 +20,7 @@ #include "laws.h" #include "spells.h" #include "direction.h" +#include "monster.h" #include #include diff --git a/src/spells/alp.c b/src/spells/alp.c index 82cf8c02f..73d17662a 100644 --- a/src/spells/alp.c +++ b/src/spells/alp.c @@ -28,6 +28,8 @@ #include #include +#include "monster.h" + #include #include diff --git a/src/summary.c b/src/summary.c index 70a908a26..df8bd1066 100644 --- a/src/summary.c +++ b/src/summary.c @@ -14,6 +14,7 @@ #include "summary.h" #include "laws.h" +#include "monster.h" #include #include @@ -88,7 +89,7 @@ int update_nmrs(void) if (fval(f, FFL_ISNEW)) { ++newplayers; } - else if (!is_monsters(f) && f->alive) { + else if (!fval(f, FFL_NOIDLEOUT) && f->alive) { int nmr = turn - f->lastorders + 1; if (nmr < 0 || nmr > NMRTimeout()) { log_error("faction %s has %d NMRS\n", factionid(f), nmr); diff --git a/src/upkeep.c b/src/upkeep.c index c807ec1d1..5eecc1b42 100644 --- a/src/upkeep.c +++ b/src/upkeep.c @@ -15,6 +15,7 @@ #include "alchemy.h" #include "economy.h" +#include "monster.h" #include