From 1f4c521ac82d48d9ec57127d0f6444fbd26e127e Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Mon, 7 Dec 2015 17:59:50 +0100 Subject: [PATCH] encapsulate r->land->peasants, money, horses, herbs, morale --- src/alchemy.c | 2 +- src/creport.c | 2 +- src/economy.c | 2 +- src/economy.test.c | 4 ++-- src/give.test.c | 4 ++-- src/gmtool.c | 5 ++--- src/kernel/item.c | 4 ++-- src/kernel/region.c | 42 ++++++++++++++++++++++++++++++++---------- src/kernel/region.h | 5 +++-- src/kernel/save.c | 7 ++----- src/modules/autoseed.c | 6 +++--- src/monsters.c | 6 +++--- src/morale.c | 19 ++++++++++++------- src/report.c | 4 ++-- 14 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index ace6a62dd..a1dd86c0a 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -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); diff --git a/src/creport.c b/src/creport.c index 80ce1ba91..9b0182252 100644 --- a/src/creport.c +++ b/src/creport.c @@ -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)); } } diff --git a/src/economy.c b/src/economy.c index 0a360c094..6311d0c3a 100644 --- a/src/economy.c +++ b/src/economy.c @@ -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)); } diff --git a/src/economy.test.c b/src/economy.test.c index 8cf337f4f..577c7931a 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -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); diff --git a/src/give.test.c b/src/give.test.c index c6c3aafc8..119c5f1a9 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -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(); } diff --git a/src/gmtool.c b/src/gmtool.c index 92080b63b..36f9fe753 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -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]); } diff --git a/src/kernel/item.c b/src/kernel/item.c index 1b312c655..aa975fb8e 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -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) { diff --git a/src/kernel/region.c b/src/kernel/region.c index 3abbdf883..4b26cf8c9 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -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 { diff --git a/src/kernel/region.h b/src/kernel/region.h index 210ded0e1..61235d5b2 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -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); diff --git a/src/kernel/save.c b/src/kernel/save.c index f14c610ff..0fae07bb4 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -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 diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index f0af4582f..e8855683e 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -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; diff --git a/src/monsters.c b/src/monsters.c index ab63cbb31..b48cb7c82 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -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; diff --git a/src/morale.c b/src/morale.c index c0a20f372..8dc22ccb5 100644 --- a/src/morale.c +++ b/src/morale.c @@ -29,6 +29,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include +#include + 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); } } diff --git a/src/report.c b/src/report.c index 9125cebdb..6e13e834f 100644 --- a/src/report.c +++ b/src/report.c @@ -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);