server/src/kernel/unit.c

1851 lines
44 KiB
C
Raw Normal View History

2019-01-24 16:34:07 +01:00
#ifdef _MSC_VER
# include <platform.h>
#endif
2010-08-08 10:06:34 +02:00
#include <kernel/config.h>
#include "unit.h"
#include "ally.h"
2018-09-29 19:32:39 +02:00
#include "attrib.h"
2010-08-08 10:06:34 +02:00
#include "building.h"
#include "calendar.h"
2010-08-08 10:06:34 +02:00
#include "connection.h"
#include "curse.h"
2018-09-29 19:32:39 +02:00
#include "event.h"
#include "faction.h"
#include "gamedata.h"
#include "group.h"
#include "guard.h"
2010-08-08 10:06:34 +02:00
#include "item.h"
#include "move.h"
#include "order.h"
#include "plane.h"
#include "race.h"
#include "region.h"
#include "spell.h"
2012-05-26 17:20:26 +02:00
#include "spellbook.h"
2010-08-08 10:06:34 +02:00
#include "ship.h"
#include "skill.h"
#include "terrain.h"
#include "terrainid.h"
2010-08-08 10:06:34 +02:00
#include <attributes/otherfaction.h>
#include <attributes/racename.h>
#include <attributes/stealth.h>
2010-08-08 10:06:34 +02:00
#include <spells/unitcurse.h>
#include <spells/regioncurse.h>
2010-08-08 10:06:34 +02:00
/* util includes */
#include <util/base36.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/macros.h>
2018-09-29 19:32:39 +02:00
#include <util/param.h>
#include <util/parser.h>
2015-11-16 18:41:52 +01:00
#include <util/rand.h>
2010-08-08 10:06:34 +02:00
#include <util/resolve.h>
#include <util/rng.h>
#include <util/strings.h>
2010-08-08 10:06:34 +02:00
#include <util/variant.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
/* libc includes */
#include <assert.h>
2015-11-24 19:15:53 +01:00
#include <ctype.h>
2010-08-08 10:06:34 +02:00
#include <limits.h>
#include <stdlib.h>
2010-08-08 10:06:34 +02:00
#include <string.h>
#include <math.h>
int weight(const unit * u)
{
int w = 0, n = 0, in_bag = 0;
const resource_type *rtype = get_resourcetype(R_BAG_OF_HOLDING);
item *itm;
for (itm = u->items; itm; itm = itm->next) {
w = itm->type->weight * itm->number;
n += w;
if (rtype && !fval(itm->type, ITF_BIG)) {
in_bag += w;
}
}
n += u->number * u_race(u)->weight;
if (rtype) {
w = i_get(u->items, rtype->itype) * BAGCAPACITY;
if (w > in_bag) w = in_bag;
n -= w;
}
return n;
}
2010-08-08 10:06:34 +02:00
attrib_type at_creator = {
"creator"
2014-07-15 15:46:02 +02:00
/* Rest ist NULL; temporaeres, nicht alterndes Attribut */
2010-08-08 10:06:34 +02:00
};
unit *findunit(int n)
{
if (n <= 0) {
return NULL;
}
return ufindhash(n);
}
unit *findunitr(const region * r, int n)
{
unit *u;
/* findunit regional! */
assert(n > 0);
u = ufindhash(n);
return (u && u->region == r) ? u : 0;
}
2010-08-08 10:06:34 +02:00
#define UMAXHASH MAXUNITS
2011-03-07 08:02:35 +01:00
static unit *unithash[UMAXHASH];
static unit *delmarker = (unit *)unithash; /* a funny hack */
2010-08-08 10:06:34 +02:00
#define HASH_STATISTICS 1
#if HASH_STATISTICS
static int hash_requests;
static int hash_misses;
#endif
2011-03-07 08:02:35 +01:00
void uhash(unit * u)
2010-08-08 10:06:34 +02:00
{
int key = HASH1(u->no, UMAXHASH), gk = HASH2(u->no, UMAXHASH);
while (unithash[key] != NULL && unithash[key] != delmarker
&& unithash[key] != u) {
key = (key + gk) % UMAXHASH;
}
assert(unithash[key] != u || !"trying to add the same unit twice");
unithash[key] = u;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void uunhash(unit * u)
2010-08-08 10:06:34 +02:00
{
int key = HASH1(u->no, UMAXHASH), gk = HASH2(u->no, UMAXHASH);
while (unithash[key] != NULL && unithash[key] != u) {
key = (key + gk) % UMAXHASH;
}
assert(unithash[key] == u || !"trying to remove a unit that is not hashed");
unithash[key] = delmarker;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
unit *ufindhash(int uid)
2010-08-08 10:06:34 +02:00
{
assert(uid >= 0);
2010-08-08 10:06:34 +02:00
#if HASH_STATISTICS
++hash_requests;
2010-08-08 10:06:34 +02:00
#endif
if (uid >= 0) {
int key = HASH1(uid, UMAXHASH), gk = HASH2(uid, UMAXHASH);
while (unithash[key] != NULL && (unithash[key] == delmarker
|| unithash[key]->no != uid)) {
key = (key + gk) % UMAXHASH;
2010-08-08 10:06:34 +02:00
#if HASH_STATISTICS
++hash_misses;
2010-08-08 10:06:34 +02:00
#endif
}
return unithash[key];
2010-08-08 10:06:34 +02:00
}
return NULL;
2010-08-08 10:06:34 +02:00
}
typedef struct buddy {
struct buddy *next;
int number;
faction *faction;
unit *unit;
} buddy;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
static buddy *get_friends(const unit * u, int *numfriends)
2010-08-08 10:06:34 +02:00
{
buddy *friends = 0;
faction *f = u->faction;
region *r = u->region;
int number = 0;
unit *u2;
for (u2 = r->units; u2; u2 = u2->next) {
if (u2->faction != f && u2->number > 0) {
int allied = 0;
if (config_get_int("rules.alliances", 0) != 0) {
allied = (f->alliance && f->alliance == u2->faction->alliance);
}
else if (alliedunit(u, u2->faction, HELP_MONEY)
&& alliedunit(u2, f, HELP_GIVE)) {
allied = 1;
}
if (allied) {
buddy *nf, **fr = &friends;
/* some units won't take stuff: */
if (u_race(u2)->ec_flags & ECF_GETITEM) {
while (*fr && (*fr)->faction->no < u2->faction->no)
fr = &(*fr)->next;
nf = *fr;
if (nf == NULL || nf->faction != u2->faction) {
nf = malloc(sizeof(buddy));
2018-11-17 22:01:15 +01:00
assert(nf);
nf->next = *fr;
nf->faction = u2->faction;
nf->unit = u2;
nf->number = 0;
*fr = nf;
}
nf->number += u2->number;
number += u2->number;
}
2010-08-08 10:06:34 +02:00
}
}
}
if (numfriends)
*numfriends = number;
return friends;
2010-08-08 10:06:34 +02:00
}
/** give all items to friends or peasants.
* this function returns 0 on success, or 1 if there are items that
* could not be destroyed.
*/
2011-03-07 08:02:35 +01:00
int gift_items(unit * u, int flags)
2010-08-08 10:06:34 +02:00
{
const struct resource_type *rsilver = get_resourcetype(R_SILVER);
const struct resource_type *rhorse = get_resourcetype(R_HORSE);
region *r = u->region;
item **itm_p = &u->items;
int retval = 0;
assert(u->region);
if (u->items == NULL || fval(u_race(u), RCF_ILLUSIONARY))
return 0;
/* at first, I should try giving my crap to my own units in this region */
if (u->faction && (u->faction->flags & FFL_QUIT) == 0 && (flags & GIFT_SELF)) {
unit *u2, *u3 = NULL;
for (u2 = r->units; u2; u2 = u2->next) {
if (u2 != u && u2->faction == u->faction && u2->number > 0) {
/* some units won't take stuff: */
if (u_race(u2)->ec_flags & ECF_GETITEM) {
2017-07-17 12:31:47 +02:00
i_merge(&u2->items, &u->items);
u->items = NULL;
break;
}
else if (!u3) {
/* pick a last-chance recipient: */
u3 = u2;
}
}
}
if (u->items && u3) {
/* if nobody else takes it, we give it to a unit that has issues */
i_merge(&u3->items, &u->items);
2010-08-08 10:06:34 +02:00
u->items = NULL;
}
if (u->items == NULL)
return 0;
2010-08-08 10:06:34 +02:00
}
/* if I have friends, I'll try to give my stuff to them */
if (u->faction && (flags & GIFT_FRIENDS)) {
int number = 0;
buddy *friends = get_friends(u, &number);
while (friends) {
struct buddy *nf = friends;
unit *u2 = nf->unit;
item *itm = u->items;
while (itm != NULL) {
const item_type *itype = itm->type;
item *itn = itm->next;
int n = itm->number;
n = n * nf->number / number;
if (n > 0) {
i_change(&u->items, itype, -n);
i_change(&u2->items, itype, n);
}
itm = itn;
}
number -= nf->number;
friends = nf->next;
free(nf);
}
if (u->items == NULL)
return 0;
2010-08-08 10:06:34 +02:00
}
/* last, but not least, give money and horses to peasants */
while (*itm_p) {
item *itm = *itm_p;
if (flags & GIFT_PEASANTS) {
if (u->region->land) {
if (itm->type->rtype == rsilver) {
rsetmoney(r, rmoney(r) + itm->number);
itm->number = 0;
}
else if (itm->type->rtype == rhorse) {
rsethorses(r, rhorses(r) + itm->number);
itm->number = 0;
}
}
2010-08-08 10:06:34 +02:00
}
if (itm->number > 0 && (itm->type->flags & ITF_NOTLOST)) {
itm_p = &itm->next;
retval = -1;
2010-08-08 10:06:34 +02:00
}
else {
i_remove(itm_p, itm);
i_free(itm);
}
2010-08-08 10:06:34 +02:00
}
return retval;
2010-08-08 10:06:34 +02:00
}
/** 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,
2010-08-08 10:06:34 +02:00
* call free_units().
* returns 0 on success, or -1 if unit could not be removed.
*/
2011-03-07 08:02:35 +01:00
static unit *deleted_units = NULL;
2010-08-08 10:06:34 +02:00
struct faction *dfindhash(int no) {
2017-12-08 19:26:26 +01:00
unit *u;
for (u = deleted_units; u; u = u->next) {
if (u->no == no) {
return u->faction;
}
}
2017-12-08 19:26:26 +01:00
return NULL;
}
2011-03-07 08:02:35 +01:00
int remove_unit(unit ** ulist, unit * u)
2010-08-08 10:06:34 +02:00
{
int result;
2010-08-08 10:06:34 +02:00
assert(ufindhash(u->no));
handle_event(u->attribs, "destroy", u);
2010-08-08 10:06:34 +02:00
result = gift_items(u, GIFT_SELF | GIFT_FRIENDS | GIFT_PEASANTS);
if (result != 0) {
make_zombie(u);
return -1;
}
2011-03-07 08:02:35 +01:00
if (u->number) {
set_number(u, 0);
}
leave(u, true);
u->region = NULL;
2010-08-08 10:06:34 +02:00
uunhash(u);
if (ulist) {
while (*ulist != u) {
ulist = &(*ulist)->next;
}
assert(*ulist == u);
*ulist = u->next;
2010-08-08 10:06:34 +02:00
}
if (u->faction) {
if (count_unit(u)) {
--u->faction->num_units;
}
if (u->faction->units == u) {
u->faction->units = u->nextF;
}
}
if (u->prevF) {
u->prevF->nextF = u->nextF;
}
if (u->nextF) {
u->nextF->prevF = u->prevF;
}
u->nextF = 0;
u->prevF = 0;
u->next = deleted_units;
deleted_units = u;
2010-08-08 10:06:34 +02:00
u->region = NULL;
2010-08-08 10:06:34 +02:00
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
unit *findnewunit(const region * r, const faction * f, int n)
2010-08-08 10:06:34 +02:00
{
unit *u2;
2010-08-08 10:06:34 +02:00
if (n == 0)
return 0;
2010-08-08 10:06:34 +02:00
for (u2 = r->units; u2; u2 = u2->next)
if (u2->faction == f && ualias(u2) == n)
return u2;
2018-02-25 18:35:40 +01:00
for (u2 = r->units; u2; u2 = u2->next)
if (ualias(u2) == n)
return u2;
2018-02-25 18:35:40 +01:00
return 0;
2010-08-08 10:06:34 +02:00
}
/* ------------------------------------------------------------- */
/*********************/
/* at_alias */
/*********************/
static attrib_type at_alias = {
"alias",
DEFAULT_INIT,
DEFAULT_FINALIZE,
DEFAULT_AGE,
NO_WRITE,
NO_READ
2010-08-08 10:06:34 +02:00
};
2016-11-17 21:08:35 +01:00
/** remember old unit.no (for the creport, mostly)
* if alias is positive, then this unit was a TEMP
* if alias is negative, then this unit has been RENUMBERed
*/
2011-03-07 08:02:35 +01:00
int ualias(const unit * u)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_find(u->attribs, &at_alias);
if (!a)
return 0;
return a->data.i;
2010-08-08 10:06:34 +02:00
}
void usetalias(unit *u, int alias)
{
a_add(&u->attribs, a_new(&at_alias))->data.i = alias;
}
int a_readprivate(variant *var, void *owner, gamedata *data)
2010-08-08 10:06:34 +02:00
{
struct storage *store = data->store;
char lbuf[DISPLAYSIZE];
READ_STR(store, lbuf, sizeof(lbuf));
var->v = str_strdup(lbuf);
return (var->v) ? AT_READ_OK : AT_READ_FAIL;
2010-08-08 10:06:34 +02:00
}
/*********************/
/* at_private */
/*********************/
attrib_type at_private = {
"private",
DEFAULT_INIT,
a_finalizestring,
DEFAULT_AGE,
a_writestring,
a_readprivate
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
const char *u_description(const unit * u, const struct locale *lang)
2010-08-08 10:06:34 +02:00
{
if (u->display_id > 0) {
return unit_getinfo(u);
}
2017-02-20 07:47:36 +01:00
else {
char zText[64];
const char * d;
const race * rc = u_race(u);
snprintf(zText, sizeof(zText), "describe_%s", rc->_name);
d = locale_getstring(lang, zText);
if (d) {
return d;
}
}
return NULL;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
const char *uprivate(const unit * u)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_find(u->attribs, &at_private);
if (!a)
return NULL;
return a->data.v;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void usetprivate(unit * u, const char *str)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_find(u->attribs, &at_private);
2010-08-08 10:06:34 +02:00
if (str == NULL || *str == 0) {
if (a) {
a_remove(&u->attribs, a);
}
return;
}
if (!a) {
a = a_add(&u->attribs, a_new(&at_private));
}
if (a->data.v) {
free(a->data.v);
}
a->data.v = str_strdup(str);
2010-08-08 10:06:34 +02:00
}
/***
** init & cleanup module
**/
2011-03-07 08:02:35 +01:00
void free_units(void)
2010-08-08 10:06:34 +02:00
{
while (deleted_units) {
unit *u = deleted_units;
deleted_units = deleted_units->next;
free_unit(u);
free(u);
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void write_unit_reference(const unit * u, struct storage *store)
2010-08-08 10:06:34 +02:00
{
WRITE_INT(store, (u && u->region) ? u->no : 0);
2010-08-08 10:06:34 +02:00
}
2017-09-21 16:26:53 +02:00
void resolve_unit(unit *u)
2010-08-08 10:06:34 +02:00
{
2017-09-21 16:26:53 +02:00
resolve(RESOLVE_UNIT | u->no, u);
2010-08-08 10:06:34 +02:00
}
2017-09-21 16:26:53 +02:00
int read_unit_reference(gamedata * data, unit **up, resolve_fun fun)
2010-08-08 10:06:34 +02:00
{
int id;
READ_INT(data->store, &id);
2017-09-21 16:26:53 +02:00
if (id > 0) {
*up = findunit(id);
if (*up == NULL) {
*up = NULL;
2017-09-22 16:39:46 +02:00
ur_add(RESOLVE_UNIT | id, (void **)up, fun);
2017-09-21 16:26:53 +02:00
}
}
else {
*up = NULL;
}
return id;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int get_level(const unit * u, skill_t id)
2010-08-08 10:06:34 +02:00
{
assert(id != NOSKILL);
if (skill_enabled(id)) {
skill *sv = u->skills;
while (sv != u->skills + u->skill_size && sv->id <= id) {
if (sv->id == id) {
return sv->level;
}
++sv;
}
2010-08-08 10:06:34 +02:00
}
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void set_level(unit * u, skill_t sk, int value)
2010-08-08 10:06:34 +02:00
{
skill *sv = u->skills;
2010-08-08 10:06:34 +02:00
2020-08-15 23:29:40 +02:00
assert(sk != SK_MAGIC || value==0 || !u->faction || u->number == 1 || fval(u->faction, FFL_NPC));
2018-02-03 14:16:01 +01:00
assert(value <= CHAR_MAX && value >= CHAR_MIN);
if (!skill_enabled(sk))
return;
2010-08-08 10:06:34 +02:00
if (value == 0) {
remove_skill(u, sk);
return;
2010-08-08 10:06:34 +02:00
}
while (sv != u->skills + u->skill_size && sv->id <= sk) {
if (sv->id == sk) {
sk_set(sv, value);
return;
}
++sv;
}
sk_set(add_skill(u, sk), value);
2010-08-08 10:06:34 +02:00
}
static int leftship_age(struct attrib *a, void *owner)
2010-08-08 10:06:34 +02:00
{
/* must be aged, so it doesn't affect report generation (cansee) */
UNUSED_ARG(a);
UNUSED_ARG(owner);
return AT_AGE_REMOVE; /* remove me */
2010-08-08 10:06:34 +02:00
}
static attrib_type at_leftship = {
"leftship", NULL, NULL, leftship_age
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
static attrib *make_leftship(struct ship *leftship)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_new(&at_leftship);
a->data.v = leftship;
return a;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void set_leftship(unit * u, ship * sh)
2010-08-08 10:06:34 +02:00
{
a_add(&u->attribs, make_leftship(sh));
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
ship *leftship(const unit * u)
2010-08-08 10:06:34 +02:00
{
attrib *a = a_find(u->attribs, &at_leftship);
2010-08-08 10:06:34 +02:00
/* Achtung: Es ist nicht garantiert, dass der Rueckgabewert zu jedem
* Zeitpunkt noch auf ein existierendes Schiff zeigt! */
2010-08-08 10:06:34 +02:00
if (a)
return (ship *)(a->data.v);
2010-08-08 10:06:34 +02:00
return NULL;
2010-08-08 10:06:34 +02:00
}
void u_set_building(unit * u, building * b)
{
assert(!b || !u->building); /* you must leave first */
u->building = b;
if (b && (!b->_owner || b->_owner->number <= 0)) {
building_set_owner(u);
}
}
void u_set_ship(unit * u, ship * sh)
{
assert(!u->ship); /* you must leave_ship */
u->ship = sh;
if (sh && (!sh->_owner || sh->_owner->number <= 0)) {
ship_set_owner(u);
}
}
2011-03-07 08:02:35 +01:00
void leave_ship(unit * u)
2010-08-08 10:06:34 +02:00
{
struct ship *sh = u->ship;
u->ship = NULL;
if (sh->_owner == u) {
ship_update_owner(sh);
sh->_owner = ship_owner(sh);
}
set_leftship(u, sh);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void leave_building(unit * u)
2010-08-08 10:06:34 +02:00
{
building * b = u->building;
2010-08-08 10:06:34 +02:00
u->building = NULL;
if (b->_owner == u) {
building_update_owner(b);
assert(b->_owner != u);
}
2010-08-08 10:06:34 +02:00
}
bool can_leave(unit * u)
2010-08-08 10:06:34 +02:00
{
2016-09-23 20:36:57 +02:00
static int config;
static bool rule_leave;
2010-08-08 10:06:34 +02:00
if (!u->building) {
return true;
}
2016-09-23 20:36:57 +02:00
if (config_changed(&config)) {
rule_leave = config_get_int("rules.move.owner_leave", 0) != 0;
}
if (rule_leave && u->building && u == building_owner(u->building)) {
return false;
}
return true;
2010-08-08 10:06:34 +02:00
}
bool leave(unit * u, bool force)
2010-08-08 10:06:34 +02:00
{
if (!force) {
if (!can_leave(u)) {
return false;
}
}
if (u->building) {
leave_building(u);
2010-08-08 10:06:34 +02:00
}
else if (u->ship) {
leave_ship(u);
}
return true;
2010-08-08 10:06:34 +02:00
}
bool can_survive(const unit * u, const region * r)
2010-08-08 10:06:34 +02:00
{
if ((fval(r->terrain, WALK_INTO) && (u_race(u)->flags & RCF_WALK))
|| (fval(r->terrain, SWIM_INTO) && (u_race(u)->flags & RCF_SWIM))
|| (fval(r->terrain, FLY_INTO) && (u_race(u)->flags & RCF_FLY))) {
2010-08-08 10:06:34 +02:00
if (has_horses(u) && !fval(r->terrain, WALK_INTO))
return false;
2010-08-08 10:06:34 +02:00
if (r->attribs) {
if (fval(u_race(u), RCF_UNDEAD) && curse_active(get_curse(r->attribs, &ct_holyground)))
return false;
}
return true;
}
return false;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void move_unit(unit * u, region * r, unit ** ulist)
2010-08-08 10:06:34 +02:00
{
assert(u && r);
assert(u->faction || !"this unit is dead");
if (u->region == r)
return;
if (!ulist)
ulist = (&r->units);
if (u->region) {
setguard(u, false);
fset(u, UFL_MOVED);
if (u->ship || u->building) {
/* can_leave must be checked in travel_i */
2010-08-08 10:06:34 +02:00
#ifndef NDEBUG
bool result = leave(u, false);
assert(result);
2010-08-08 10:06:34 +02:00
#else
leave(u, false);
2010-08-08 10:06:34 +02:00
#endif
}
translist(&u->region->units, ulist, u);
}
else {
addlist(ulist, u);
2010-08-08 10:06:34 +02:00
}
update_interval(u->faction, r);
u->region = r;
2010-08-08 10:06:34 +02:00
}
/* ist mist, aber wegen nicht skalierender attribute notwendig: */
#include "alchemy.h"
void clone_men(const unit * u, unit * dst, int n)
2010-08-08 10:06:34 +02:00
{
const attrib *a;
region *r = u->region;
2010-08-08 10:06:34 +02:00
if (n == 0)
return;
assert(n > 0);
/* "hat attackiert"-status wird uebergeben */
2010-08-08 10:06:34 +02:00
if (dst) {
skill_t sk;
ship *sh;
2011-03-07 08:02:35 +01:00
assert(dst->number + n > 0);
2010-08-08 10:06:34 +02:00
for (sk = 0; sk != MAXSKILLS; ++sk) {
int weeks, level = 0;
skill *sv = unit_skill(u, sk);
skill *sn = unit_skill(dst, sk);
2010-08-08 10:06:34 +02:00
if (sv == NULL && sn == NULL)
continue;
if (sn == NULL && dst->number == 0) {
/* new unit, easy to solve */
level = sv->level;
weeks = sv->weeks;
}
else {
double dlevel = 0.0;
if (sv && sv->level) {
dlevel += (sv->level + 1 - sv->weeks / (sv->level + 1.0)) * n;
level += sv->level * n;
}
if (sn && sn->level) {
dlevel +=
(sn->level + 1 - sn->weeks / (sn->level + 1.0)) * dst->number;
level += sn->level * dst->number;
}
dlevel = dlevel / (n + dst->number);
level = level / (n + dst->number);
if (level <= dlevel) {
/* apply the remaining fraction to the number of weeks to go.
* subtract the according number of weeks, getting closer to the
* next level */
level = (int)dlevel;
weeks = (level + 1) - (int)((dlevel - level) * (level + 1));
}
else {
/* make it harder to reach the next level.
* weeks+level is the max difficulty, 1 - the fraction between
* level and dlevel applied to the number of weeks between this
* and the previous level is the added difficutly */
level = (int)dlevel + 1;
weeks = 1 + 2 * level - (int)((1 + dlevel - level) * level);
}
}
if (level) {
if (sn == NULL)
sn = add_skill(dst, sk);
sn->level = (unsigned char)level;
sn->weeks = (unsigned char)weeks;
assert(sn->weeks > 0 && sn->weeks <= sn->level * 2 + 1);
2015-11-05 08:32:21 +01:00
assert(dst->number != 0 || (sv && sn->level == sv->level
&& sn->weeks == sv->weeks));
}
else if (sn) {
remove_skill(dst, sk);
sn = NULL;
}
2010-08-08 10:06:34 +02:00
}
a = a_find(u->attribs, &at_effect);
while (a && a->type == &at_effect) {
effect_data *olde = (effect_data *)a->data.v;
if (olde->value)
change_effect(dst, olde->type, olde->value);
a = a->next;
2010-08-08 10:06:34 +02:00
}
sh = leftship(u);
if (sh != NULL)
set_leftship(dst, sh);
dst->flags |=
u->flags & (UFL_LONGACTION | UFL_NOTMOVING | UFL_HUNGER | UFL_MOVED |
UFL_ENTER);
if (u->attribs) {
transfer_curse(u, dst, n);
}
set_number(dst, dst->number + n);
dst->hp += (long long)u->hp * n / u->number;
assert(dst->hp >= dst->number);
/* TODO: Das ist schnarchlahm! und gehoert nicht hierhin */
a = a_find(dst->attribs, &at_effect);
while (a && a->type == &at_effect) {
attrib *an = a->next;
effect_data *olde = (effect_data *)a->data.v;
int e = get_effect(u, olde->type);
if (e != 0)
change_effect(dst, olde->type, -e);
a = an;
}
}
else if (r->land) {
if ((u_race(u)->ec_flags & ECF_REC_ETHEREAL) == 0) {
const race *rc = u_race(u);
int p = rpeasants(r);
p += (int)(n * rc->recruit_multi);
rsetpeasants(r, p);
2010-08-08 10:06:34 +02:00
}
}
2010-08-08 10:06:34 +02:00
}
void transfermen(unit * u, unit * dst, int n)
{
clone_men(u, dst, n);
scale_number(u, u->number - n);
}
2011-03-07 08:02:35 +01:00
struct building *inside_building(const struct unit *u)
2010-08-08 10:06:34 +02:00
{
if (!u->building) {
return NULL;
}
else if (!building_finished(u->building)) {
/* Gebaeude noch nicht fertig */
return NULL;
}
else {
int p = 0, cap = buildingcapacity(u->building);
const unit *u2;
for (u2 = u->region->units; u2; u2 = u2->next) {
if (u2->building == u->building) {
p += u2->number;
if (u2 == u) {
if (p <= cap)
return u->building;
return NULL;
}
if (p > cap)
return NULL;
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
return NULL;
2010-08-08 10:06:34 +02:00
}
void u_freeorders(unit *u)
{
free_orders(&u->orders);
set_order(&u->thisorder, NULL);
}
2011-03-07 08:02:35 +01:00
void u_setfaction(unit * u, faction * f)
2010-08-08 10:06:34 +02:00
{
if (u->faction == f)
return;
if (u->faction) {
if (count_unit(u)) {
--u->faction->num_units;
u->faction->num_people -= u->number;
}
2019-08-25 17:43:18 +02:00
set_group(u, NULL);
if (u->nextF) {
u->nextF->prevF = u->prevF;
}
if (u->prevF) {
u->prevF->nextF = u->nextF;
}
else {
u->faction->units = u->nextF;
}
}
2010-08-08 10:06:34 +02:00
if (f != NULL) {
if (f->units) {
f->units->prevF = u;
}
u->prevF = NULL;
u->nextF = f->units;
f->units = u;
}
else {
u->nextF = NULL;
}
u->faction = f;
if (u->region) {
update_interval(f, u->region);
}
if (f && count_unit(u)) {
++f->num_units;
f->num_people += u->number;
}
2010-08-08 10:06:34 +02:00
}
bool count_unit(const unit *u)
{
const race *rc = u_race(u);
/* spells are invisible. units we cannot see do not count to our limit */
return rc == NULL || (rc->flags & RCF_INVISIBLE) == 0;
}
2011-03-07 08:02:35 +01:00
void set_number(unit * u, int count)
2010-08-08 10:06:34 +02:00
{
assert(count >= 0);
assert(count <= UNIT_MAXSIZE);
2010-08-08 10:06:34 +02:00
if (count == 0) {
u->flags &= ~(UFL_HERO);
}
if (u->faction && count_unit(u)) {
u->faction->num_people += count - u->number;
}
u->number = count;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void remove_skill(unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
int i;
for (i = 0; i != u->skill_size; ++i) {
skill *sv = u->skills + i;
if (sv->id == sk) {
2018-01-17 18:04:04 +01:00
if (u->skill_size - i - 1 > 0) {
memmove(sv, sv + 1, (u->skill_size - i - 1) * sizeof(skill));
}
if (--u->skill_size == 0) {
free(u->skills);
u->skills = NULL;
}
return;
}
2010-08-08 10:06:34 +02:00
}
}
skill *add_skill(unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
skill *sv;
int i;
2018-11-17 22:01:15 +01:00
assert(u);
for (i = 0; i != u->skill_size; ++i) {
sv = u->skills + i;
if (sv->id >= sk) break;
}
2018-11-17 22:01:15 +01:00
sv = realloc(u->skills, (1 + u->skill_size) * sizeof(skill));
assert(sv);
u->skills = sv;
sv = u->skills + i;
if (i < u->skill_size) {
assert(sv->id != sk);
memmove(sv + 1, sv, sizeof(skill) * (u->skill_size - i));
}
++u->skill_size;
sv->level = 0;
sv->weeks = 1;
sv->old = 0;
sv->id = sk;
if (sk == SK_MAGIC && u->faction && !fval(u->faction, FFL_NPC)) {
assert(u->number <= 1);
assert(max_magicians(u->faction) >= u->number);
}
return sv;
2010-08-08 10:06:34 +02:00
}
skill *unit_skill(const unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
skill *sv = u->skills;
while (sv != u->skills + u->skill_size && sv->id <= sk) {
if (sv->id == sk) {
return sv;
}
++sv;
}
return NULL;
2010-08-08 10:06:34 +02:00
}
bool has_skill(const unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
skill *sv = u->skills;
while (sv != u->skills + u->skill_size && sv->id <= sk) {
if (sv->id == sk) {
return (sv->level > 0);
}
++sv;
2010-08-08 10:06:34 +02:00
}
return false;
2010-08-08 10:06:34 +02:00
}
static int item_invis(const unit *u) {
const struct resource_type *rring = get_resourcetype(R_RING_OF_INVISIBILITY);
const struct resource_type *rsphere = get_resourcetype(R_SPHERE_OF_INVISIBILITY);
return (rring ? i_get(u->items, rring->itype) : 0)
+ (rsphere ? i_get(u->items, rsphere->itype) * 100 : 0);
}
static int att_modification(const unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
double result = 0;
if (u->attribs) {
curse *c;
attrib *a;
c = get_curse(u->attribs, &ct_worse);
if (c != NULL) {
result += curse_geteffect(c);
}
a = a_find(u->attribs, &at_curse);
while (a && a->type == &at_curse) {
c = (curse *)a->data.v;
if (c->type == &ct_skillmod && c->data.i == sk) {
result += curse_geteffect(c);
break;
}
a = a->next;
2010-08-08 10:06:34 +02:00
}
}
/* TODO hier kann nicht mit get/iscursed gearbeitet werden, da nur der
* jeweils erste vom Typ C_GBDREAM zurueckgegen wird, wir aber alle
* durchsuchen und aufaddieren muessen */
if (u->region && u->region->attribs) {
int bonus = 0, malus = 0;
attrib *a = a_find(u->region->attribs, &at_curse);
while (a && a->type == &at_curse) {
curse *c = (curse *)a->data.v;
if (c->magician && curse_active(c) && c->type == &ct_gbdream) {
int effect = curse_geteffect_int(c);
bool allied = alliedunit(c->magician, u->faction, HELP_GUARD);
if (allied) {
if (effect > bonus) bonus = effect;
}
else {
if (effect < malus) malus = effect;
}
}
a = a->next;
}
result = result + bonus + malus;
}
return (int)result;
2010-08-08 10:06:34 +02:00
}
static int terrain_mod(const race * rc, skill_t sk, const region *r)
{
static int rc_cache, t_cache;
static const race *rc_dwarf, *rc_insect, *rc_elf;
static const terrain_type *t_mountain, *t_desert, *t_swamp;
const struct terrain_type *terrain = r->terrain;
if (terrain_changed(&t_cache)) {
t_mountain = get_terrain(terrainnames[T_MOUNTAIN]);
t_desert = get_terrain(terrainnames[T_DESERT]);
t_swamp = get_terrain(terrainnames[T_SWAMP]);
}
if (rc_changed(&rc_cache)) {
rc_elf = get_race(RC_ELF);
rc_dwarf = get_race(RC_DWARF);
rc_insect = get_race(RC_INSECT);
}
if (rc == rc_dwarf) {
if (sk == SK_TACTICS) {
if (terrain == t_mountain || fval(terrain, ARCTIC_REGION))
return 1;
}
}
else if (rc == rc_insect) {
if (terrain == t_mountain || fval(terrain, ARCTIC_REGION)) {
return -1;
}
else if (terrain == t_desert || terrain == t_swamp) {
return 1;
}
}
else if (rc == rc_elf) {
if (r_isforest(r)) {
if (sk == SK_PERCEPTION || sk == SK_STEALTH) {
return 1;
}
else if (sk == SK_TACTICS) {
return 2;
}
}
}
return 0;
}
static int rc_skillmod(const struct race *rc, skill_t sk)
{
if (!skill_enabled(sk)) {
return 0;
}
return rc->bonus[sk];
}
int get_modifier(const unit * u, skill_t sk, int level, const region * r, bool noitem)
2010-08-08 10:06:34 +02:00
{
const struct race *rc = u_race(u);
int bskill = level;
int skill = bskill;
2010-08-08 10:06:34 +02:00
if (r && sk == SK_STEALTH) {
plane *pl = rplane(r);
if (pl && fval(pl, PFL_NOSTEALTH)) {
return 0;
}
2012-05-26 17:20:26 +02:00
}
2010-08-08 10:06:34 +02:00
skill += rc_skillmod(rc, sk);
if (skill > 0) {
if (r) {
skill += terrain_mod(rc, sk, r);
}
skill += att_modification(u, sk);
if (u->attribs) {
skill = skillmod(u, r, sk, skill);
}
2017-04-02 20:17:39 +02:00
}
if (fval(u, UFL_HUNGER)) {
if (sk == SK_SAILING && skill > 2) {
skill = skill - 1;
}
else {
skill = skill / 2;
}
}
return skill - bskill;
2010-08-08 10:06:34 +02:00
}
int eff_skill(const unit * u, const skill *sv, const region *r)
2010-08-08 10:06:34 +02:00
{
assert(u);
if (!r) r = u->region;
if (sv && sv->level > 0) {
int mlevel = sv->level + get_modifier(u, sv->id, sv->level, r, false);
if (mlevel > 0) {
return mlevel;
2010-08-08 10:06:34 +02:00
}
}
return 0;
2010-08-08 10:06:34 +02:00
}
2018-07-05 20:06:32 +02:00
int effskill_study(const unit * u, skill_t sk)
2010-08-08 10:06:34 +02:00
{
skill *sv = unit_skill(u, sk);
if (sv && sv->level > 0) {
int mlevel = sv->level;
2018-07-05 20:06:32 +02:00
mlevel += get_modifier(u, sv->id, sv->level, u->region, true);
if (mlevel > 0)
return mlevel;
}
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int invisible(const unit * target, const unit * viewer)
2010-08-08 10:06:34 +02:00
{
if (viewer && viewer->faction == target->faction)
return 0;
else {
int hidden = item_invis(target);
if (hidden) {
2019-01-24 16:34:07 +01:00
if (hidden > target->number) hidden = target->number;
if (viewer) {
const resource_type *rtype = get_resourcetype(R_AMULET_OF_TRUE_SEEING);
hidden -= i_get(viewer->items, rtype->itype);
}
}
return hidden;
2010-08-08 10:06:34 +02:00
}
}
/** remove the unit from memory.
* this frees all memory that's only accessible through the unit,
* and you should already have called uunhash and removed the unit from the
* region.
2010-08-08 10:06:34 +02:00
*/
2011-03-07 08:02:35 +01:00
void free_unit(unit * u)
2010-08-08 10:06:34 +02:00
{
2018-11-17 22:01:15 +01:00
struct reservation **pres = &u->reservations;
assert(!u->region);
free(u->_name);
free_order(u->thisorder);
free_orders(&u->orders);
2018-11-17 22:01:15 +01:00
while (*pres) {
struct reservation *res = *pres;
*pres = res->next;
2018-11-17 22:01:15 +01:00
free(res);
}
if (u->skills) {
free(u->skills);
2018-11-17 22:01:15 +01:00
u->skills = NULL;
}
while (u->items) {
item *it = u->items->next;
u->items->next = NULL;
i_free(u->items);
u->items = it;
}
2018-11-17 22:01:15 +01:00
while (u->attribs) {
a_remove(&u->attribs, u->attribs);
}
2010-08-08 10:06:34 +02:00
}
static int newunitid(void)
{
int random_unit_no;
int start_random_no;
random_unit_no = 1 + (rng_int() % MAX_UNIT_NR);
start_random_no = random_unit_no;
while (ufindhash(random_unit_no) || dfindhash(random_unit_no)
|| forbiddenid(random_unit_no)) {
random_unit_no++;
if (random_unit_no == MAX_UNIT_NR + 1) {
random_unit_no = 1;
}
if (random_unit_no == start_random_no) {
random_unit_no = (int)MAX_UNIT_NR + 1;
}
}
return random_unit_no;
}
2011-03-07 08:02:35 +01:00
static void createunitid(unit * u, int id)
2010-08-08 10:06:34 +02:00
{
if (id <= 0 || id > MAX_UNIT_NR || ufindhash(id) || dfindhash(id)
|| forbiddenid(id))
u->no = newunitid();
else
u->no = id;
uhash(u);
2010-08-08 10:06:34 +02:00
}
void default_name(const unit *u, char name[], int len) {
const char * result;
const struct locale * lang = u->faction ? u->faction->locale : default_locale;
if (lang) {
2016-08-30 08:51:05 +02:00
const char * prefix;
prefix = LOC(lang, "unitdefault");
if (!prefix) {
prefix = parameters[P_UNIT];
}
2016-08-30 08:51:05 +02:00
result = prefix;
}
else {
result = parameters[P_UNIT];
}
snprintf(name, len, "%s %s", result, itoa36(u->no));
}
2011-03-07 08:02:35 +01:00
void name_unit(unit * u)
2010-08-08 10:06:34 +02:00
{
2016-12-31 20:03:50 +01:00
const race *rc = u_race(u);
if (rc->name_unit) {
rc->name_unit(u);
}
else if (u->faction->flags & FFL_NPC) {
unit_setname(u, NULL);
}
else {
char name[32];
default_name(u, name, sizeof(name));
unit_setname(u, name);
}
2010-08-08 10:06:34 +02:00
}
unit *unit_create(int id)
{
unit *u = (unit *)calloc(1, sizeof(unit));
createunitid(u, id);
return u;
}
2010-08-08 10:06:34 +02:00
/** creates a new unit.
*
* @param dname: name, set to NULL to get a default.
* @param creator: unit to inherit stealth, group, building, ship, etc. from
*/
2011-03-07 08:02:35 +01:00
unit *create_unit(region * r, faction * f, int number, const struct race *urace,
int id, const char *dname, unit * creator)
2010-08-08 10:06:34 +02:00
{
unit *u = unit_create(id);
2010-08-08 10:06:34 +02:00
assert(urace);
u_setrace(u, urace);
u->irace = NULL;
if (f) {
assert(faction_alive(f));
u_setfaction(u, f);
2010-08-08 10:06:34 +02:00
}
set_number(u, number);
2010-08-08 10:06:34 +02:00
/* zuerst in die Region setzen, da zb Drachennamen den Regionsnamen
* enthalten */
if (r)
move_unit(u, r, NULL);
2010-08-08 10:06:34 +02:00
/* u->race muss bereits gesetzt sein, wird fuer default-hp gebraucht */
/* u->region auch */
if (number > 0) {
u->hp = unit_max_hp(u) * number;
}
2010-08-08 10:06:34 +02:00
if (dname) {
u->_name = str_strdup(dname);
}
else if (urace->name_unit || playerrace(urace)) {
name_unit(u);
}
2010-08-08 10:06:34 +02:00
if (creator) {
attrib *a;
2010-08-08 10:06:34 +02:00
/* erbt Kampfstatus und Tarnung */
unit_setstatus(u, creator->status);
u->irace = creator->irace;
2010-08-08 10:06:34 +02:00
/* erbt Gebaeude/Schiff */
if (creator->region == r) {
if (creator->building) {
u_set_building(u, creator->building);
}
if (creator->ship && fval(u_race(u), RCF_CANSAIL)) {
u_set_ship(u, creator->ship);
}
}
2010-08-08 10:06:34 +02:00
/* Tarnlimit wird vererbt */
if (fval(creator, UFL_STEALTH)) {
attrib *a = a_find(creator->attribs, &at_stealth);
if (a) {
int stealth = a->data.i;
a = a_add(&u->attribs, a_new(&at_stealth));
a->data.i = stealth;
}
}
2010-08-08 10:06:34 +02:00
/* Temps von parteigetarnten Einheiten sind wieder parteigetarnt */
if (fval(creator, UFL_ANON_FACTION)) {
fset(u, UFL_ANON_FACTION);
}
/* Daemonentarnung */
set_racename(&u->attribs, get_racename(creator->attribs));
if (fval(u_race(u), RCF_SHAPESHIFT) && fval(u_race(creator), RCF_SHAPESHIFT)) {
u->irace = creator->irace;
}
2010-08-08 10:06:34 +02:00
/* Gruppen */
2018-10-26 22:12:43 +02:00
if (creator->faction == f) {
group *g = get_group(creator);
if (g) {
set_group(u, g);
}
}
if (creator->attribs) {
faction *otherf = get_otherfaction(creator);
if (otherf) {
a_add(&u->attribs, make_otherfaction(otherf));
}
}
2010-08-08 10:06:34 +02:00
a = a_add(&u->attribs, a_new(&at_creator));
a->data.v = creator;
}
2010-08-08 10:06:34 +02:00
return u;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int maxheroes(const struct faction *f)
2010-08-08 10:06:34 +02:00
{
int nsize = f->num_people;
if (nsize == 0)
return 0;
else {
int nmax = (int)(log10(nsize / 50.0) * 20);
return (nmax < 0) ? 0 : nmax;
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int countheroes(const struct faction *f)
2010-08-08 10:06:34 +02:00
{
const unit *u = f->units;
int n = 0;
2010-08-08 10:06:34 +02:00
while (u) {
if (fval(u, UFL_HERO))
n += u->number;
u = u->nextF;
}
return n;
2010-08-08 10:06:34 +02:00
}
/** Returns the raw unit name, like "Frodo", or "Seeschlange" */
2011-03-07 08:02:35 +01:00
const char *unit_getname(const unit * u)
2010-08-08 10:06:34 +02:00
{
if (!u->_name) {
const struct locale * lang = u->faction ? u->faction->locale : default_locale;
const char *rcname = rc_name_s(u->_race, u->number == 1 ? NAME_SINGULAR : NAME_PLURAL);
return LOC(lang, rcname);
}
return u->_name;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void unit_setname(unit * u, const char *name)
2010-08-08 10:06:34 +02:00
{
free(u->_name);
if (name && name[0])
u->_name = str_strdup(name);
else
u->_name = NULL;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
const char *unit_getinfo(const unit * u)
2010-08-08 10:06:34 +02:00
{
if (u->display_id > 0) {
return dbstring_load(u->display_id, NULL);
}
return NULL;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void unit_setinfo(unit * u, const char *info)
2010-08-08 10:06:34 +02:00
{
if (info) {
u->display_id = dbstring_save(info);
}
else {
u->display_id = 0;
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int unit_getid(const unit * u)
2010-08-08 10:06:34 +02:00
{
return u->no;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void unit_setid(unit * u, int id)
2010-08-08 10:06:34 +02:00
{
unit *nu = findunit(id);
if (nu == NULL) {
uunhash(u);
u->no = id;
uhash(u);
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int unit_gethp(const unit * u)
2010-08-08 10:06:34 +02:00
{
return u->hp;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void unit_sethp(unit * u, int hp)
2010-08-08 10:06:34 +02:00
{
u->hp = hp;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
status_t unit_getstatus(const unit * u)
2010-08-08 10:06:34 +02:00
{
return u->status;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void unit_setstatus(unit * u, status_t status)
2010-08-08 10:06:34 +02:00
{
u->status = status;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int unit_getweight(const unit * u)
2010-08-08 10:06:34 +02:00
{
return weight(u);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int unit_getcapacity(const unit * u)
2010-08-08 10:06:34 +02:00
{
return walkingcapacity(u);
2010-08-08 10:06:34 +02:00
}
void renumber_unit(unit *u, int no) {
if (no == 0) no = newunitid();
uunhash(u);
if (!ualias(u)) {
attrib *a = a_add(&u->attribs, a_new(&at_alias));
2016-11-17 21:08:35 +01:00
a->data.i = -u->no;
}
u->no = no;
uhash(u);
}
2011-03-07 08:02:35 +01:00
void unit_addorder(unit * u, order * ord)
2010-08-08 10:06:34 +02:00
{
order **ordp = &u->orders;
while (*ordp)
ordp = &(*ordp)->next;
*ordp = ord;
u->faction->lastorders = turn;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int unit_max_hp(const unit * u)
2010-08-08 10:06:34 +02:00
{
int h;
2016-09-23 20:36:57 +02:00
static int config;
2017-03-31 21:37:30 +02:00
static bool rule_stamina;
h = u_race(u)->hitpoints;
2010-08-08 10:06:34 +02:00
2016-09-23 20:36:57 +02:00
if (config_changed(&config)) {
rule_stamina = config_get_int("rules.stamina", 1) != 0;
2016-09-23 20:36:57 +02:00
}
2017-03-31 21:37:30 +02:00
if (rule_stamina) {
double p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2;
h += (int)(h * p + 0.5);
}
/* der healing curse veraendert die maximalen hp */
if (u->region && u->region->attribs) {
curse *c = get_curse(u->region->attribs, &ct_healing);
if (c) {
h = (int)(h * (1.0 + (curse_geteffect(c) / 100)));
}
2010-08-08 10:06:34 +02:00
}
return h;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void scale_number(unit * u, int n)
2010-08-08 10:06:34 +02:00
{
if (n == u->number) {
return;
2010-08-08 10:06:34 +02:00
}
if (u->number > 0) {
if (n > 0) {
const attrib *a = a_find(u->attribs, &at_effect);
u->hp = (long long)u->hp * n / u->number;
for (; a && a->type == &at_effect; a = a->next) {
effect_data *data = (effect_data *)a->data.v;
data->value = (long long)data->value * n / u->number;
}
}
else {
u->hp = 0;
}
}
if (u->number == 0 || n == 0) {
skill_t sk;
for (sk = 0; sk < MAXSKILLS; sk++) {
remove_skill(u, sk);
}
2010-08-08 10:06:34 +02:00
}
set_number(u, n);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
const struct race *u_irace(const struct unit *u)
2010-08-08 10:06:34 +02:00
{
if (u->irace) {
return u->irace;
}
return u->_race;
2010-08-08 10:06:34 +02:00
}
const struct race *u_race(const struct unit *u)
{
return u->_race;
}
void u_setrace(struct unit *u, const struct race *rc)
{
assert(rc);
if (!u->faction) {
u->_race = rc;
}
else {
int n = 0;
if (count_unit(u)) {
--n;
}
u->_race = rc;
if (count_unit(u)) {
++n;
}
if (n != 0) {
u->faction->num_units += n;
u->faction->num_people += n * u->number;
}
}
}
int effskill(const unit * u, skill_t sk, const region *r)
{
assert(u);
if (skill_enabled(sk)) {
skill *sv = u->skills;
while (sv != u->skills + u->skill_size) {
if (sv->id == sk) {
return eff_skill(u, sv, r);
}
++sv;
}
}
return 0;
}
void remove_empty_units_in_region(region * r)
{
unit **up = &r->units;
while (*up) {
unit *u = *up;
if (u->number) {
faction *f = u->faction;
if (f == NULL || !faction_alive(f)) {
set_number(u, 0);
}
}
2017-05-24 08:52:19 +02:00
if (u->number == 0) {
remove_unit(up, u);
}
if (*up == u)
up = &u->next;
}
}
void remove_empty_units(void)
{
region *r;
for (r = regions; r; r = r->next) {
remove_empty_units_in_region(r);
}
}
typedef char name[OBJECTIDSIZE + 1];
static name idbuf[8];
static int nextbuf = 0;
/** Puts human-readable unit name, with number, like "Frodo (hobb)" into buffer */
char *write_unitname(const unit * u, char *buffer, size_t size)
{
const char * name = unit_getname(u);
snprintf(buffer, size, "%s (%s)", name, itoa36(u->no));
buffer[size - 1] = 0;
return buffer;
}
/** Returns human-readable unit name, with number, like "Frodo (hobb)" */
const char *unitname(const unit * u)
{
char *ubuf = idbuf[(++nextbuf) % 8];
return write_unitname(u, ubuf, sizeof(idbuf[0]));
}
bool unit_name_equals_race(const unit *u) {
if (u->_name) {
char sing[32], plur[32];
const struct locale *lang = u->faction->locale;
rc_name(u->_race, NAME_SINGULAR, sing, sizeof(sing));
rc_name(u->_race, NAME_PLURAL, plur, sizeof(plur));
if (strcmp(u->_name, sing) == 0 || strcmp(u->_name, plur) == 0 ||
strcmp(u->_name, LOC(lang, sing)) == 0 ||
strcmp(u->_name, LOC(lang, plur)) == 0) {
return true;
}
}
return false;
}
static int read_newunitid(const faction * f, const region * r)
{
int n;
unit *u2;
n = getid();
if (n == 0)
return -1;
u2 = findnewunit(r, f, n);
if (u2)
return u2->no;
return -1;
}
int read_unitid(const faction * f, const region * r)
{
char token[16];
const char *s = gettoken(token, sizeof(token));
if (!s || *s == 0 || !isalnum(*s)) {
return -1;
}
if (isparam(s, f->locale, P_TEMP)) {
return read_newunitid(f, r);
}
return atoi36((const char *)s);
}
int getunit(const region * r, const faction * f, unit **uresult)
{
unit *u2 = NULL;
int n = read_unitid(f, r);
int result = GET_NOTFOUND;
if (n == 0) {
result = GET_PEASANTS;
}
else if (n > 0) {
u2 = findunit(n);
if (u2 != NULL && u2->region == r) {
/* there used to be a 'u2->flags & UFL_ISNEW || u2->number>0' condition
* here, but it got removed because of a bug that made units disappear:
* http://eressea.upb.de/mantis/bug_view_page.php?bug_id=0000172
*/
result = GET_UNIT;
}
else {
u2 = NULL;
}
}
if (uresult) {
*uresult = u2;
}
return result;
}
bool has_horses(const unit * u)
{
item *itm = u->items;
for (; itm; itm = itm->next) {
if (itm->type->flags & ITF_ANIMAL)
return true;
}
return false;
}
#define MAINTENANCE 10
int maintenance_cost(const struct unit *u)
{
if (u == NULL)
return MAINTENANCE;
return u_race(u)->maintenance * u->number;
}
static skill_t limited_skills[] = { SK_ALCHEMY, SK_HERBALISM, SK_MAGIC, SK_SPY, SK_TACTICS, NOSKILL };
bool is_limited_skill(skill_t sk)
{
int i;
for (i = 0; limited_skills[i] != NOSKILL; ++i) {
if (sk == limited_skills[i]) {
return true;
}
}
return false;
}
bool has_limited_skills(const struct unit * u)
{
int i;
for (i = 0; i != u->skill_size; ++i) {
skill *sv = u->skills + i;
if (is_limited_skill(sv->id)) {
return true;
}
}
return false;
}
double u_heal_factor(const unit * u)
{
const race * rc = u_race(u);
if (rc->healing > 0) {
return rc->healing / 100.0;
}
if (r_isforest(u->region)) {
static int rc_cache;
static const race *rc_elf;
if (rc_changed(&rc_cache)) {
rc_elf = get_race(RC_ELF);
}
if (rc == rc_elf) {
static int cache;
static double elf_regen;
if (config_changed(&cache)) {
elf_regen = config_get_flt("healing.forest", 1.0);
}
return elf_regen;
}
}
return 1.0;
}
2019-09-18 20:55:44 +02:00
void unit_convert_race(unit *u, const race *rc, const char *rcname)
{
if (rc && u->_race != rc) {
u_setrace(u, rc);
}
if (rcname && strcmp(rcname, u->_race->_name) != 0) {
set_racename(&u->attribs, rcname);
}
}