From 1ad2f4a2640f8a03c3c493ee236287c9095c442c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 24 Jul 2009 21:41:59 +0000 Subject: [PATCH] MORALE -1 when giving command of building --- src/common/gamecode/economy.c | 28 ++++++++++++++++++++++------ src/common/gamecode/laws.c | 8 +++++--- src/common/kernel/region.c | 12 +++++++----- src/common/kernel/region.h | 2 +- src/eressea/tolua/bind_region.c | 2 +- src/scripts/tests.lua | 32 +++++++++++++++++++++++++++++++- 6 files changed, 67 insertions(+), 17 deletions(-) diff --git a/src/common/gamecode/economy.c b/src/common/gamecode/economy.c index fe1e0950e..5961678ca 100644 --- a/src/common/gamecode/economy.c +++ b/src/common/gamecode/economy.c @@ -634,6 +634,27 @@ recruit(unit * u, struct order * ord, request ** recruitorders) addlist(recruitorders, o); } +static void +give_control(unit * u, unit * u2) +{ + if (u->building && u->faction!=u2->faction) { + region * r = u->region; + faction * f = region_get_owner(r); + if (f==u->faction) { + building * b = largestbuilding(r, &is_owner_building, false); + if (b==u->building) { + int morale = region_get_morale(r); + region_set_owner(r, u2->faction, turn); + if (morale>0) { + region_set_morale(r, morale-1, turn); + } + } + } + } + freset(u, UFL_OWNER); + fset(u2, UFL_OWNER); +} + static void give_cmd(unit * u, order * ord) { @@ -704,12 +725,7 @@ give_cmd(unit * u, order * ord) cmistake(u, ord, 49, MSG_EVENT); return; } - if (!alliedunit(u2, u->faction, HELP_GIVE) && !ucontact(u2, u)) { - cmistake(u, ord, 40, MSG_EVENT); - return; - } - freset(u, UFL_OWNER); - fset(u2, UFL_OWNER); + give_control(u, u2); msg = msg_message("givecommand", "unit recipient", u, u2); add_message(&u->faction->msgs, msg); diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index ec6adc4ef..1d84684d2 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -3098,12 +3098,14 @@ static void age_region(region * r) ch *= 1.2; /* 20% improvement */ } if (chance(ch)) { - ++r->land->morale; - r->land->ownership->morale_turn = turn; + region_set_morale(r, r->land->morale+1, turn); } } if (!r->land->ownership->owner && r->land->moraleland->morale = (short)MIN(r->land->morale, MORALE_DEFAULT); + short m = (short)MIN(r->land->morale, MORALE_DEFAULT); + if (m!=r->land->morale) { + region_set_morale(r, m, turn); + } } } } diff --git a/src/common/kernel/region.c b/src/common/kernel/region.c index 944d75f44..3dda60caa 100644 --- a/src/common/kernel/region.c +++ b/src/common/kernel/region.c @@ -1246,8 +1246,8 @@ terraform_region(region * r, const terrain_type * terrain) int mnr = 0; r->land = calloc(1, sizeof(land_region)); - r->land->morale = MORALE_DEFAULT; r->land->ownership = NULL; + region_set_morale(r, MORALE_DEFAULT, -1); region_setname(r, makename()); for (d=0;d!=MAXDIRECTIONS;++d) { region * nr = rconnect(r, d); @@ -1456,13 +1456,12 @@ region_set_owner(struct region * r, struct faction * owner, int turn) if (r->land) { if (!r->land->ownership) { r->land->ownership = malloc(sizeof(region_owner)); - r->land->morale = MORALE_DEFAULT; + region_set_morale(r, MORALE_DEFAULT, turn); } else if (r->land->ownership->owner) { - r->land->morale = MORALE_TAKEOVER; + region_set_morale(r, MORALE_TAKEOVER, turn); } r->land->ownership->owner = owner; r->land->ownership->since_turn = turn; - r->land->ownership->morale_turn = turn; } } @@ -1500,10 +1499,13 @@ int region_get_morale(const region * r) return r->land?r->land->morale:-1; } -void region_set_morale(region * r, int morale) +void region_set_morale(region * r, int morale, int turn) { if (r->land) { r->land->morale = (short)morale; + if (turn>=0 && r->land->ownership) { + r->land->ownership->morale_turn = turn; + } } } diff --git a/src/common/kernel/region.h b/src/common/kernel/region.h index a883b2432..8c845b0a8 100644 --- a/src/common/kernel/region.h +++ b/src/common/kernel/region.h @@ -258,7 +258,7 @@ struct region * r_connect(const struct region *, direction_t dir); void free_regions(void); int region_get_morale(const region * r); -void region_set_morale(region * r, int morale); +void region_set_morale(region * r, int morale, int turn); void write_region_reference(const struct region * r, struct storage * store); variant read_region_reference(struct storage * store); diff --git a/src/eressea/tolua/bind_region.c b/src/eressea/tolua/bind_region.c index fa5f26855..c97ead3aa 100644 --- a/src/eressea/tolua/bind_region.c +++ b/src/eressea/tolua/bind_region.c @@ -194,7 +194,7 @@ static int tolua_region_get_morale(lua_State* L) static int tolua_region_set_morale(lua_State* L) { region* r = (region*)tolua_tousertype(L, 1, 0); - region_set_morale(r, (int)tolua_tonumber(L, 2, 0)); + region_set_morale(r, (int)tolua_tonumber(L, 2, 0), turn); return 0; } diff --git a/src/scripts/tests.lua b/src/scripts/tests.lua index 8043d7cda..41a7da28b 100644 --- a/src/scripts/tests.lua +++ b/src/scripts/tests.lua @@ -302,6 +302,34 @@ local function test_owners() assert(r.owner==u1.faction) end +local function test_morale() + free_game() + local r = region.create(0, 0, "plain") + local f1 = faction.create("enno@eressea.de", "human", "de") + local u1 = unit.create(f1, r, 1) + local f2 = faction.create("enno@eressea.de", "human", "de") + local u2 = unit.create(f2, r, 1) + + local b = building.create(r, "castle") + b.size = 10 + u1.building = b + u2.building = b + update_owners() + assert(r.morale==2) + r.morale = 5 + assert(r.owner==u1.faction) + u1:clear_orders() + u1:add_order("GIB " .. itoa36(u2.id) .. " KOMMANDO") + process_orders() + u1:clear_orders() + assert(r.owner==u2.faction) + assert(r.morale==4) + u2.building = nil + update_owners() + assert(r.owner==u1.faction) + assert(r.morale==0) +end + local function test_recruit() free_game() local r = region.create(0, 0, "plain") @@ -645,14 +673,16 @@ tests = { ["upkeep"] = test_upkeep, ["id"] = test_id, ["work"] = test_work, + ["morale"] = test_morale, ["owners"] = test_owners, ["market"] = test_market } mytests = { -- ["blessed"] = test_blessed -- foiled by peasantgrowth + ["morale"] = test_morale } fail = 0 -for k, v in pairs(tests) do +for k, v in pairs(mytests) do local status, err = pcall(v) if not status then fail = fail + 1