From 73da2564614b012971aa979210bb69ff1094d403 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 5 Jan 2015 18:14:55 +0100 Subject: [PATCH] Bug 2059: building owners kick out anyone they don't HELP GUARD. --- src/laws.c | 75 +++++++++++++++++++++++++++++++------------------ src/laws.h | 1 + src/laws.test.c | 47 +++++++++++++++++++++++++++++++ src/tests.c | 4 +-- 4 files changed, 98 insertions(+), 29 deletions(-) diff --git a/src/laws.c b/src/laws.c index 0c01f0471..8a29db9c0 100755 --- a/src/laws.c +++ b/src/laws.c @@ -4269,6 +4269,26 @@ static void enter_2(region * r) do_enter(r, 1); } +static bool help_enter(unit *uo, unit *u) { + return uo->faction == u->faction || alliedunit(uo, u->faction, HELP_GUARD); +} + +void force_leave(region *r) { + unit *u; + for (u = r->units; u; u = u->next) { + unit *uo = NULL; + if (u->building) { + uo = building_owner(u->building); + } + if (u->ship) { + uo = ship_owner(u->ship); + } + if (uo && !help_enter(uo, u)) { + leave(u, false); + } + } +} + static void maintain_buildings_1(region * r) { maintain_buildings(r, false); @@ -4306,71 +4326,72 @@ void init_processor(void) int p; p = 10; - add_proc_global(p, &new_units, "Neue Einheiten erschaffen"); + add_proc_global(p, new_units, "Neue Einheiten erschaffen"); p += 10; add_proc_unit(p, update_long_order, "Langen Befehl aktualisieren"); add_proc_order(p, K_BANNER, banner_cmd, 0, NULL); - add_proc_order(p, K_EMAIL, &email_cmd, 0, NULL); - add_proc_order(p, K_PASSWORD, &password_cmd, 0, NULL); - add_proc_order(p, K_SEND, &send_cmd, 0, NULL); - add_proc_order(p, K_GROUP, &group_cmd, 0, NULL); + add_proc_order(p, K_EMAIL, email_cmd, 0, NULL); + add_proc_order(p, K_PASSWORD, password_cmd, 0, NULL); + add_proc_order(p, K_SEND, send_cmd, 0, NULL); + add_proc_order(p, K_GROUP, group_cmd, 0, NULL); p += 10; - add_proc_order(p, K_QUIT, &quit_cmd, 0, NULL); - add_proc_order(p, K_URSPRUNG, &origin_cmd, 0, NULL); - add_proc_order(p, K_ALLY, &ally_cmd, 0, NULL); - add_proc_order(p, K_PREFIX, &prefix_cmd, 0, NULL); - add_proc_order(p, K_SETSTEALTH, &setstealth_cmd, 0, NULL); - add_proc_order(p, K_STATUS, &status_cmd, 0, NULL); - add_proc_order(p, K_COMBATSPELL, &combatspell_cmd, 0, NULL); - add_proc_order(p, K_DISPLAY, &display_cmd, 0, NULL); - add_proc_order(p, K_NAME, &name_cmd, 0, NULL); - add_proc_order(p, K_GUARD, &guard_off_cmd, 0, NULL); - add_proc_order(p, K_RESHOW, &reshow_cmd, 0, NULL); + add_proc_order(p, K_QUIT, quit_cmd, 0, NULL); + add_proc_order(p, K_URSPRUNG, origin_cmd, 0, NULL); + add_proc_order(p, K_ALLY, ally_cmd, 0, NULL); + add_proc_order(p, K_PREFIX, prefix_cmd, 0, NULL); + add_proc_order(p, K_SETSTEALTH, setstealth_cmd, 0, NULL); + add_proc_order(p, K_STATUS, status_cmd, 0, NULL); + add_proc_order(p, K_COMBATSPELL, combatspell_cmd, 0, NULL); + add_proc_order(p, K_DISPLAY, display_cmd, 0, NULL); + add_proc_order(p, K_NAME, name_cmd, 0, NULL); + add_proc_order(p, K_GUARD, guard_off_cmd, 0, NULL); + add_proc_order(p, K_RESHOW, reshow_cmd, 0, NULL); if (get_param_int(global.parameters, "rules.alliances", 0) == 1) { p += 10; - add_proc_global(p, &alliance_cmd, NULL); + add_proc_global(p, alliance_cmd, NULL); } p += 10; add_proc_region(p, do_contact, "Kontaktieren"); - add_proc_order(p, K_MAIL, &mail_cmd, 0, "Botschaften"); + add_proc_order(p, K_MAIL, mail_cmd, 0, "Botschaften"); p += 10; /* all claims must be done before we can USE */ add_proc_region(p, &enter_1, "Betreten (1. Versuch)"); /* for GIVE CONTROL */ - add_proc_order(p, K_USE, &use_cmd, 0, "Benutzen"); + add_proc_order(p, K_USE, use_cmd, 0, "Benutzen"); p += 10; /* in case it has any effects on alliance victories */ - add_proc_order(p, K_GIVE, &give_control_cmd, 0, "GIB KOMMANDO"); + add_proc_order(p, K_GIVE, give_control_cmd, 0, "GIB KOMMANDO"); p += 10; /* in case it has any effects on alliance victories */ - add_proc_order(p, K_LEAVE, &leave_cmd, 0, "Verlassen"); + add_proc_order(p, K_LEAVE, leave_cmd, 0, "Verlassen"); p += 10; - add_proc_region(p, &enter_1, "Betreten (2. Versuch)"); /* to allow a buildingowner to enter the castle pre combat */ + add_proc_region(p, enter_1, "Betreten (2. Versuch)"); /* to allow a buildingowner to enter the castle pre combat */ p += 10; - add_proc_region(p, &do_battle, "Attackieren"); + add_proc_region(p, do_battle, "Attackieren"); if (!keyword_disabled(K_BESIEGE)) { p += 10; - add_proc_region(p, &do_siege, "Belagern"); + add_proc_region(p, do_siege, "Belagern"); } p += 10; /* can't allow reserve before siege (weapons) */ - add_proc_region(p, &enter_1, "Betreten (3. Versuch)"); /* to claim a castle after a victory and to be able to DESTROY it in the same turn */ + add_proc_region(p, enter_1, "Betreten (3. Versuch)"); /* to claim a castle after a victory and to be able to DESTROY it in the same turn */ if (get_param_int(global.parameters, "rules.reserve.twophase", 0)) { add_proc_order(p, K_RESERVE, &reserve_self, 0, "RESERVE (self)"); p += 10; } add_proc_order(p, K_RESERVE, &reserve_cmd, 0, "RESERVE (all)"); add_proc_order(p, K_CLAIM, &claim_cmd, 0, NULL); - add_proc_unit(p, &follow_unit, "Folge auf Einheiten setzen"); + add_proc_unit(p, follow_unit, "Folge auf Einheiten setzen"); p += 10; /* rest rng again before economics */ - add_proc_region(p, &economics, "Zerstoeren, Geben, Rekrutieren, Vergessen"); + add_proc_region(p, force_leave, "kick non-allies out of buildings/ships"); + add_proc_region(p, economics, "Zerstoeren, Geben, Rekrutieren, Vergessen"); add_proc_order(p, K_PROMOTION, &promotion_cmd, 0, "Heldenbefoerderung"); p += 10; diff --git a/src/laws.h b/src/laws.h index f5209ac3f..27232982b 100755 --- a/src/laws.h +++ b/src/laws.h @@ -105,6 +105,7 @@ extern "C" { bool seefaction(const struct faction *f, const struct region *r, const struct unit *u, int modifier); int armedmen(const struct unit *u, bool siege_weapons); + void force_leave(struct region *r); #ifdef __cplusplus diff --git a/src/laws.test.c b/src/laws.test.c index 9f91ffc77..ec1174a17 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -226,6 +226,51 @@ static void test_display_cmd(CuTest *tc) { test_cleanup(); } +static void test_force_leave_buildings(CuTest *tc) { + ally *al; + region *r; + unit *u1, *u2, *u3; + building * b; + test_cleanup(); + r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION)); + u1 = test_create_unit(test_create_faction(NULL), r); + u2 = test_create_unit(u1->faction, r); + u3 = test_create_unit(test_create_faction(NULL), r); + b = test_create_building(r, NULL); + u_set_building(u1, b); + building_set_owner(u1); + u_set_building(u2, b); + u_set_building(u3, b); + force_leave(r); + CuAssertPtrEquals_Msg(tc, "owner should not be forecd to leave", b, u1->building); + CuAssertPtrEquals_Msg(tc, "same faction should not be forced to leave", b, u2->building); + CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u3->building); + + u_set_building(u3, b); + al = ally_add(&u1->faction->allies, u3->faction); + al->status = HELP_GUARD; + force_leave(r); + CuAssertPtrEquals_Msg(tc, "allies should not be forced to leave", b, u3->building); + test_cleanup(); +} + +static void test_force_leave_ships(CuTest *tc) { + region *r; + unit *u1, *u2; + ship *sh; + test_cleanup(); + r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION)); + u1 = test_create_unit(test_create_faction(NULL), r); + u2 = test_create_unit(test_create_faction(NULL), r); + sh = test_create_ship(r, NULL); + u_set_ship(u1, sh); + u_set_ship(u2, sh); + ship_set_owner(u1); + force_leave(r); + CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u2->ship); + test_cleanup(); +} + static void test_fishing_feeds_2_people(CuTest * tc) { const resource_type *rtype; @@ -649,5 +694,7 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_enter_building); SUITE_ADD_TEST(suite, test_enter_ship); SUITE_ADD_TEST(suite, test_display_cmd); + SUITE_ADD_TEST(suite, test_force_leave_buildings); + SUITE_ADD_TEST(suite, test_force_leave_ships); return suite; } diff --git a/src/tests.c b/src/tests.c index 79ff5ad38..bf2aa4ef3 100644 --- a/src/tests.c +++ b/src/tests.c @@ -81,14 +81,14 @@ test_create_terrain(const char * name, unsigned int flags) building * test_create_building(region * r, const building_type * btype) { - building * b = new_building(btype?btype:bt_find("castle"), r, default_locale); + building * b = new_building(btype?btype:bt_get_or_create("castle"), r, default_locale); b->size = b->type->maxsize>0?b->type->maxsize:1; return b; } ship * test_create_ship(region * r, const ship_type * stype) { - ship * s = new_ship(stype?stype:st_find("boat"), r, default_locale); + ship * s = new_ship(stype?stype:st_get_or_create("boat"), r, default_locale); s->size = s->type->construction?s->type->construction->maxsize:1; return s; }