encapsulate r->land->peasants, money, horses, herbs, morale

This commit is contained in:
Steffen Mecke 2015-12-07 17:59:50 +01:00
parent 723c3df7b2
commit 1f4c521ac8
14 changed files with 68 additions and 44 deletions

View file

@ -77,7 +77,7 @@ void herbsearch(unit * u, int max)
herbsfound = ntimespprob(effsk * u->number,
(double)rherbs(r) / 100.0F, -0.01F);
herbsfound = _min(herbsfound, max);
rsetherbs(r, rherbs(r) - herbsfound);
rsetherbs(r, (short) (rherbs(r) - herbsfound));
if (herbsfound) {
produceexp(u, SK_HERBALISM, u->number);

View file

@ -1380,7 +1380,7 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr)
}
}
if (r->land && r->land->ownership) {
fprintf(F, "%d;morale\n", r->land->morale);
fprintf(F, "%d;morale\n", region_get_morale(r));
}
}

View file

@ -2298,7 +2298,7 @@ static void plant(unit * u, int raw)
/* Alles ok. Abziehen. */
use_pooled(u, rt_water, GET_DEFAULT, 1);
use_pooled(u, itype->rtype, GET_DEFAULT, n);
rsetherbs(r, rherbs(r) + planted);
rsetherbs(r, (short) (rherbs(r) + planted));
ADDMSG(&u->faction->msgs, msg_message("plant", "unit region amount herb",
u, r, planted, itype->rtype));
}

View file

@ -224,7 +224,7 @@ static void test_tax_cmd(CuTest *tc) {
r->land->money = 11;
rsetmoney(r, 11);
expandtax(r, taxorders);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "income"));
/* taxing is in multiples of 10 */
@ -232,7 +232,7 @@ static void test_tax_cmd(CuTest *tc) {
test_clear_messages(u->faction);
i_change(&u->items, silver, -i_get(u->items, silver));
r->land->money = 1000;
rsetmoney(r, 1000);
taxorders = 0;
tax_cmd(u, ord, &taxorders);
expandtax(r, taxorders);

View file

@ -64,7 +64,7 @@ static void test_give_unit_to_peasants(CuTest * tc) {
rsetpeasants(env.r, 0);
give_unit(env.src, NULL, NULL);
CuAssertIntEquals(tc, 0, env.src->number);
CuAssertIntEquals(tc, 1, env.r->land->peasants);
CuAssertIntEquals(tc, 1, rpeasants(env.r));
test_cleanup();
}
@ -251,7 +251,7 @@ static void test_give_peasants(CuTest * tc) {
msg = disband_men(1, env.src, NULL);
CuAssertStrEquals(tc, "give_person_peasants", (const char*)msg->parameters[0].v);
CuAssertIntEquals(tc, 0, env.src->number);
CuAssertIntEquals(tc, 1, env.r->land->peasants);
CuAssertIntEquals(tc, 1, rpeasants(env.r));
msg_release(msg);
test_cleanup();
}

View file

@ -385,9 +385,8 @@ static void paint_info_region(window * wnd, const state * st)
line++;
mvwprintw(win, line++, 1, "%s, age %d", r->terrain->_name, r->age);
if (r->land) {
mvwprintw(win, line++, 1, "$:%6d P:%5d", r->land->money,
r->land->peasants);
mvwprintw(win, line++, 1, "H:%6d %s:%5d", r->land->horses,
mvwprintw(win, line++, 1, "$:%6d P:%5d", rmoney(r), rpeasants(r));
mvwprintw(win, line++, 1, "H:%6d %s:%5d", rhorses(r),
(r->flags & RF_MALLORN) ? "M" : "T",
r->land->trees[1] + r->land->trees[2]);
}

View file

@ -98,8 +98,8 @@ static int res_changehp(unit * u, const resource_type * rtype, int delta)
static int res_changepeasants(unit * u, const resource_type * rtype, int delta)
{
assert(rtype != NULL && u->region->land);
u->region->land->peasants += delta;
return u->region->land->peasants;
rsetpeasants(u->region, rpeasants(u->region) + delta);
return rpeasants(u->region);
}
static int golem_factor(const unit *u, const resource_type *rtype) {

View file

@ -595,26 +595,30 @@ bool is_coastregion(region * r)
int rpeasants(const region * r)
{
return ((r)->land ? (r)->land->peasants : 0);
return r->land ? r->land->peasants : 0;
}
void rsetpeasants(region * r, int value)
{
if (r->land) r->land->peasants = value;
else assert(value>=0);
if (r->land) {
assert(value >= 0);
r->land->peasants = value;
} else
assert(value == 0);
}
int rmoney(const region * r)
{
return ((r)->land ? (r)->land->money : 0);
return r->land ? r->land->money : 0;
}
void rsethorses(const region * r, int value)
{
assert(value >= 0);
if (r->land)
if (r->land) {
assert(value >= 0);
r->land->horses = value;
} else
assert(value == 0);
}
int rhorses(const region * r)
@ -624,10 +628,28 @@ int rhorses(const region * r)
void rsetmoney(region * r, int value)
{
if (r->land) r->land->money = value;
else assert(value >= 0);
if (r->land) {
assert(value >= 0);
r->land->money = value;
} else
assert(value == 0);
}
short rherbs(const struct region *r)
{
return r->land?r->land->herbs:0;
}
void rsetherbs(const struct region *r, short value)
{
if (r->land) {
assert(value >= 0);
r->land->herbs = (short)(value);
} else
assert(value == 0);
}
void r_setdemand(region * r, const luxury_type * ltype, int value)
{
struct demand *d, **dp = &r->land->demands;
@ -1325,7 +1347,7 @@ faction *update_owners(region * r)
else if (f || new_owner->faction != region_get_last_owner(r)) {
alliance *al = region_get_alliance(r);
if (al && new_owner->faction->alliance == al) {
int morale = _max(0, r->land->morale - MORALE_TRANSFER);
int morale = _max(0, region_get_morale(r) - MORALE_TRANSFER);
region_set_morale(r, morale, turn);
}
else {

View file

@ -201,13 +201,14 @@ extern "C" {
int rhorses(const struct region *r);
void rsethorses(const struct region *r, int value);
short rherbs(const struct region *r);
void rsetherbs(const struct region *r, short value);
#define rbuildings(r) ((r)->buildings)
#define rherbtype(r) ((r)->land?(r)->land->herbtype:0)
#define rsetherbtype(r, value) if ((r)->land) (r)->land->herbtype=(value)
#define rherbs(r) ((r)->land?(r)->land->herbs:0)
#define rsetherbs(r, value) if ((r)->land) ((r)->land->herbs=(short)(value))
bool r_isforest(const struct region *r);

View file

@ -973,10 +973,7 @@ static region *readregion(struct gamedata *data, int x, int y)
read_items(data->store, &r->land->items);
if (data->version >= REGIONOWNER_VERSION) {
READ_INT(data->store, &n);
r->land->morale = (short)n;
if (r->land->morale < 0) {
r->land->morale = 0;
}
region_set_morale(r, _max(0, (short) n), -1);
read_owner(data, &r->land->ownership);
}
}
@ -1039,7 +1036,7 @@ void writeregion(struct gamedata *data, const region * r)
write_items(data->store, r->land->items);
WRITE_SECTION(data->store);
#if RELEASE_VERSION>=REGIONOWNER_VERSION
WRITE_INT(data->store, r->land->morale);
WRITE_INT(data->store, region_get_morale(r));
write_owner(data, r->land->ownership);
WRITE_SECTION(data->store);
#endif

View file

@ -842,7 +842,7 @@ int region_quality(const region * r, region * rn[])
/* nobody likes volcanoes */
result -= 2000;
}
result += rn[n]->land->peasants;
result += rpeasants(rn[n]);
}
}
return result;
@ -1007,10 +1007,10 @@ int build_island_e3(newfaction ** players, int x, int y, int numfactions, int mi
terraform_region(r, newterrain(T_HIGHLAND));
prepare_starting_region(r);
}
r->land->money = 50000; /* 2% = 1000 silver */
rsetmoney(r, 50000); /* 2% = 1000 silver */
}
else if (r->land) {
r->land->money *= 4;
rsetmoney(r, rmoney(r) *4);
}
}
return nfactions;

View file

@ -657,8 +657,8 @@ static order *plan_dragon(unit * u)
order *long_order = NULL;
if (ta == NULL) {
move |= (r->land == 0 || r->land->peasants == 0); /* when no peasants, move */
move |= (r->land == 0 || r->land->money == 0); /* when no money, move */
move |= (rpeasants(r) == 0); /* when no peasants, move */
move |= (rmoney(r) == 0); /* when no money, move */
}
move |= chance(0.04); /* 4% chance to change your mind */
@ -950,7 +950,7 @@ void spawn_undead(void)
continue;
/* Chance 0.1% * chaosfactor */
if (r->land && unburied > r->land->peasants / 20
if (r->land && unburied > rpeasants(r) / 20
&& rng_int() % 10000 < (100 + 100 * chaosfactor(r))) {
message *msg;
unit *u;

View file

@ -29,6 +29,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/rand.h>
#include <assert.h>
static double rc_popularity(const struct race *rc)
{
int pop = get_param_int(rc->parameters, "morale", MORALE_AVERAGE);
@ -36,6 +38,9 @@ static double rc_popularity(const struct race *rc)
}
void morale_update(region *r) {
int morale = region_get_morale(r);
assert(r->land);
if (r->land->ownership && r->land->ownership->owner) {
int stability = turn - r->land->ownership->morale_turn;
int maxmorale = MORALE_DEFAULT;
@ -44,24 +49,24 @@ void morale_update(region *r) {
int bsize = buildingeffsize(b, false);
maxmorale = (int)(0.5 + b->type->taxes(b, bsize + 1) / MORALE_TAX_FACTOR);
}
if (r->land->morale < maxmorale) {
if (morale < maxmorale) {
if (stability > MORALE_COOLDOWN && r->land->ownership->owner
&& r->land->morale < MORALE_MAX) {
&& morale < MORALE_MAX) {
double ch = rc_popularity(r->land->ownership->owner->race);
if (is_cursed(r->attribs, C_GENEROUS, 0)) {
ch *= 1.2; /* 20% improvement */
}
if (stability >= MORALE_AVERAGE * 2 || chance(ch)) {
region_set_morale(r, r->land->morale + 1, turn);
region_set_morale(r, morale + 1, turn);
}
}
}
else if (r->land->morale > maxmorale) {
region_set_morale(r, r->land->morale - 1, turn);
else if (morale > maxmorale) {
region_set_morale(r, morale - 1, turn);
}
}
else if (r->land->morale > MORALE_DEFAULT) {
region_set_morale(r, r->land->morale - 1, turn);
else if (morale > MORALE_DEFAULT) {
region_set_morale(r, morale - 1, turn);
}
}

View file

@ -1019,7 +1019,7 @@ static void describe(stream *out, const seen_region * sr, faction * f)
if (r->land->ownership) {
const char *str =
LOC(f->locale, mkname("morale", itoa10(r->land->morale)));
LOC(f->locale, mkname("morale", itoa10(region_get_morale(r))));
bytes = _snprintf(bufp, size, " %s", str);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
@ -1354,7 +1354,7 @@ static void statistics(stream *out, const region * r, const faction * f)
}
if (r->land->ownership) {
m = msg_message("nr_stat_morale", "morale", r->land->morale);
m = msg_message("nr_stat_morale", "morale", region_get_morale(r));
nr_render(m, f->locale, buf, sizeof(buf), f);
paragraph(out, buf, 2, 2, 0);
msg_release(m);