refactoring: move all monster-related code into monster.c

make some features depend on being NPCs, not monster faction.
This commit is contained in:
Enno Rehling 2014-11-11 16:53:56 +01:00
parent ebd115b04a
commit 5b9eb9080f
25 changed files with 86 additions and 67 deletions

View file

@ -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 <kernel/alliance.h>
#include <kernel/build.h>

View file

@ -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 <kernel/alliance.h>
#include <kernel/faction.h>
@ -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",

View file

@ -1,5 +1,6 @@
#include <platform.h>
#include "spells/shipcurse.h"
#include "monster.h"
#include <kernel/equipment.h>
#include <kernel/faction.h>

View file

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

View file

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

View file

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

View file

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

View file

@ -7,6 +7,7 @@
#include <kernel/config.h>
#include <util/language.h>
#include "monster.h"
#include <CuTest.h>
#include <tests.h>

View file

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

View file

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

View file

@ -34,6 +34,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/log.h>
#include <util/rng.h>
#include "monster.h"
/* libc includes */
#include <assert.h>

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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 <kernel/build.h>
#include <kernel/building.h>

View file

@ -24,6 +24,8 @@
/* util iclude */
#include <util/rng.h>
#include "monster.h"
/* libc includes */
#include <stdlib.h>

View file

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

View file

@ -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 <modules/score.h>

View file

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

View file

@ -20,6 +20,7 @@
#include "laws.h"
#include "spells.h"
#include "direction.h"
#include "monster.h"
#include <spells/borders.h>
#include <spells/buildingcurse.h>

View file

@ -28,6 +28,8 @@
#include <util/resolve.h>
#include <util/umlaut.h>
#include "monster.h"
#include <storage.h>
#include <triggers/createcurse.h>

View file

@ -14,6 +14,7 @@
#include "summary.h"
#include "laws.h"
#include "monster.h"
#include <kernel/alliance.h>
#include <kernel/calendar.h>
@ -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);

View file

@ -15,6 +15,7 @@
#include "alchemy.h"
#include "economy.h"
#include "monster.h"
#include <assert.h>