From 0fd7906d41ca22f4275b55fc23eae6cc556a0565 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 4 Nov 2018 15:53:27 +0100 Subject: [PATCH] extract contact logic to a module add missing tests for contact to units. add an attribute for contacting a faction. --- src/CMakeLists.txt | 2 + src/bind_process.c | 1 + src/contact.c | 80 ++++++++++++++++++++++++++++++++++++++++ src/contact.h | 10 +++++ src/contact.test.c | 36 ++++++++++++++++++ src/give.c | 1 + src/give.test.c | 2 + src/guard.c | 2 + src/kernel/config.test.c | 10 ++--- src/kernel/unit.c | 59 +++-------------------------- src/kernel/unit.h | 8 +--- src/laws.c | 18 +-------- src/magic.c | 5 ++- src/move.c | 11 +++--- src/move.test.c | 1 + src/spells.c | 1 + src/test_eressea.c | 1 + 17 files changed, 160 insertions(+), 88 deletions(-) create mode 100644 src/contact.c create mode 100644 src/contact.h create mode 100644 src/contact.test.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 96f67ebd0..bd0ed2bff 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -97,6 +97,7 @@ set (ERESSEA_SRC automate.c battle.c chaos.c + contact.c creport.c direction.c donations.c @@ -214,6 +215,7 @@ set(TESTS_SRC alchemy.test.c automate.test.c battle.test.c + contact.test.c creport.test.c direction.test.c donations.test.c diff --git a/src/bind_process.c b/src/bind_process.c index f9f99284e..6a02ac7a5 100755 --- a/src/bind_process.c +++ b/src/bind_process.c @@ -5,6 +5,7 @@ #include "bind_process.h" #include "battle.h" +#include "contact.h" #include "economy.h" #include "laws.h" #include "magic.h" diff --git a/src/contact.c b/src/contact.c new file mode 100644 index 000000000..2ca5fc5be --- /dev/null +++ b/src/contact.c @@ -0,0 +1,80 @@ +#ifdef _MSC_VER +#include +#endif + +#include "contact.h" + +#include "kernel/attrib.h" +#include "kernel/faction.h" +#include "kernel/order.h" +#include "kernel/unit.h" + +static attrib_type at_contact_unit = { + "contact_unit", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + NO_WRITE, + NO_READ +}; + +static attrib_type at_contact_faction = { + "contact_faction", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + NO_WRITE, + NO_READ +}; + +void usetcontact(unit * u, const unit * u2) +{ + attrib *a = a_find(u->attribs, &at_contact_unit); + while (a && a->type == &at_contact_unit) { + if (a->data.v == u2) { + return; + } + a = a->next; + } + a_add(&u->attribs, a_new(&at_contact_unit))->data.v = (void *)u2; +} + +bool ucontact(const unit * u, const unit * u2) +/* Prueft, ob u den Kontaktiere-Befehl zu u2 gesetzt hat. */ +{ + attrib *a; + + if (u->faction == u2->faction) { + return true; + } + /* Explizites KONTAKTIERE */ + for (a = u->attribs; a; a = a->next) { + if (a->type == &at_contact_unit) { + if (u2 == a->data.v) { + return true; + } + } + else if (a->type == &at_contact_faction) { + if (u2->faction == a->data.v) { + return true; + } + } + } + return false; +} + +int contact_cmd(unit * u, order * ord) +{ + unit *u2; + int n; + + init_order(ord, u->faction->locale); + n = read_unitid(u->faction, u->region); + u2 = findunit(n); + + if (u2 != NULL) { + usetcontact(u, u2); + } + return 0; +} + diff --git a/src/contact.h b/src/contact.h new file mode 100644 index 000000000..d3f751887 --- /dev/null +++ b/src/contact.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +struct unit; +struct faction; + +bool ucontact(const struct unit *u, const struct unit *u2); +void usetcontact(struct unit *u, const struct unit *c); + diff --git a/src/contact.test.c b/src/contact.test.c new file mode 100644 index 000000000..4374eb259 --- /dev/null +++ b/src/contact.test.c @@ -0,0 +1,36 @@ +#include "contact.h" + +#include "tests.h" +#include + +struct region; +struct unit; +struct faction; + +static void test_contact(CuTest *tc) { + struct unit *u1, *u2, *u3; + struct region *r; + struct faction *f; + + test_setup(); + r = test_create_plain(0, 0); + f = test_create_faction(NULL); + u1 = test_create_unit(f, r); + u2 = test_create_unit(f, r); + u3 = test_create_unit(test_create_faction(NULL), r); + CuAssertTrue(tc, ucontact(u1, u1)); + CuAssertTrue(tc, ucontact(u1, u2)); + CuAssertTrue(tc, !ucontact(u1, u3)); + usetcontact(u1, u3); + CuAssertTrue(tc, ucontact(u1, u3)); + CuAssertTrue(tc, !ucontact(u2, u3)); + test_teardown(); +} + +CuSuite *get_contact_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_contact); + + return suite; +} diff --git a/src/give.c b/src/give.c index 4b1dbb0ac..98ceff6bf 100644 --- a/src/give.c +++ b/src/give.c @@ -16,6 +16,7 @@ #include #include "give.h" +#include "contact.h" #include "economy.h" #include "laws.h" diff --git a/src/give.test.c b/src/give.test.c index f3164470c..f8009874c 100644 --- a/src/give.test.c +++ b/src/give.test.c @@ -1,6 +1,8 @@ #include #include "give.h" + +#include "contact.h" #include "economy.h" #include diff --git a/src/guard.c b/src/guard.c index 468dd99bc..437980b34 100644 --- a/src/guard.c +++ b/src/guard.c @@ -20,6 +20,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include "guard.h" + +#include "contact.h" #include "laws.h" #include "monsters.h" diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index f5d29877a..cc476c6c3 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -22,7 +22,6 @@ struct critbit_tree; static void test_read_unitid(CuTest *tc) { unit *u; order *ord; - attrib *a; struct locale *lang; struct terrain_type *t_plain; @@ -31,8 +30,7 @@ static void test_read_unitid(CuTest *tc) { /* note that the english order is FIGHT, not COMBAT, so this is a poor example */ t_plain = test_create_terrain("plain", LAND_REGION); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, t_plain)); - a = a_add(&u->attribs, a_new(&at_alias)); - a->data.i = atoi36("42"); /* this unit is also TEMP 42 */ + usetalias(u, atoi36("42")); ord = create_order(K_GIVE, lang, "TEMP 42"); init_order_depr(ord); @@ -66,7 +64,6 @@ static void test_read_unitid(CuTest *tc) { static void test_getunit(CuTest *tc) { unit *u, *u2; order *ord; - attrib *a; struct region *r; struct locale *lang; struct terrain_type *t_plain; @@ -76,8 +73,9 @@ static void test_getunit(CuTest *tc) { /* note that the english order is FIGHT, not COMBAT, so this is a poor example */ t_plain = test_create_terrain("plain", LAND_REGION); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, t_plain)); - a = a_add(&u->attribs, a_new(&at_alias)); - a->data.i = atoi36("42"); /* this unit is also TEMP 42 */ + /* This unit is also TEMP 42: */ + usetalias(u, atoi36("42")); + r = test_create_region(1, 0, t_plain); ord = create_order(K_GIVE, lang, itoa36(u->no)); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 8b2248d39..637a83a53 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -430,7 +430,7 @@ unit *findnewunit(const region * r, const faction * f, int n) /*********************/ /* at_alias */ /*********************/ -attrib_type at_alias = { +static attrib_type at_alias = { "alias", DEFAULT_INIT, DEFAULT_FINALIZE, @@ -451,6 +451,11 @@ int ualias(const unit * u) return a->data.i; } +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) { struct storage *store = data->store; @@ -517,58 +522,6 @@ void usetprivate(unit * u, const char *str) a->data.v = str_strdup(str); } -/*********************/ -/* at_target */ -/*********************/ -attrib_type at_target = { - "target", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - NO_WRITE, - NO_READ -}; - -/*********************/ -/* at_contact */ -/*********************/ -attrib_type at_contact = { - "contact", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - NO_WRITE, - NO_READ -}; - -void usetcontact(unit * u, const unit * u2) -{ - attrib *a = a_find(u->attribs, &at_contact); - while (a && a->type == &at_contact && a->data.v != u2) - a = a->next; - if (a && a->type == &at_contact) - return; - a_add(&u->attribs, a_new(&at_contact))->data.v = (void *)u2; -} - -bool ucontact(const unit * u, const unit * u2) -/* Prueft, ob u den Kontaktiere-Befehl zu u2 gesetzt hat. */ -{ - attrib *ru; - if (u->faction == u2->faction) - return true; - - /* Explizites KONTAKTIERE */ - for (ru = a_find(u->attribs, &at_contact); ru && ru->type == &at_contact; - ru = ru->next) { - if (((unit *)ru->data.v) == u2) { - return true; - } - } - - return false; -} - /*** ** init & cleanup module **/ diff --git a/src/kernel/unit.h b/src/kernel/unit.h index e08ca1ef3..169a5ff30 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -120,15 +120,14 @@ extern "C" { } unit; extern struct attrib_type at_creator; - extern struct attrib_type at_alias; - extern struct attrib_type at_target; extern struct attrib_type at_potionuser; - extern struct attrib_type at_contact; extern struct attrib_type at_effect; extern struct attrib_type at_private; extern struct attrib_type at_showskchange; int ualias(const struct unit *u); + void usetalias(unit *u, int alias); + int weight(const struct unit *u); void renumber_unit(struct unit *u, int no); @@ -141,9 +140,6 @@ extern "C" { const char *uprivate(const struct unit *u); void usetprivate(struct unit *u, const char *c); - bool ucontact(const struct unit *u, const struct unit *u2); - void usetcontact(struct unit *u, const struct unit *c); - struct unit *findnewunit(const struct region *r, const struct faction *f, int alias); diff --git a/src/laws.c b/src/laws.c index 57c5adfc2..e8b6fee17 100644 --- a/src/laws.c +++ b/src/laws.c @@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "alchemy.h" #include "automate.h" #include "battle.h" +#include "contact.h" #include "economy.h" #include "market.h" #include "morale.h" @@ -912,21 +913,6 @@ void demographics(void) immigration(); } -int contact_cmd(unit * u, order * ord) -{ - unit *u2; - int n; - - init_order_depr(ord); - n = read_unitid(u->faction, u->region); - u2 = findunit(n); - - if (u2 != NULL) { - usetcontact(u, u2); - } - return 0; -} - int leave_cmd(unit * u, struct order *ord) { region *r = u->region; @@ -2855,7 +2841,7 @@ void maketemp_cmd(unit *u, order **olist) } u2 = create_unit(u->region, u->faction, 0, u->faction->race, alias, s, u); fset(u2, UFL_ISNEW); - a_add(&u2->attribs, a_new(&at_alias))->data.i = alias; + usetalias(u2, alias); sh = leftship(u); if (sh) { set_leftship(u2, sh); diff --git a/src/magic.c b/src/magic.c index 469361c51..3f1d58ae6 100644 --- a/src/magic.c +++ b/src/magic.c @@ -22,11 +22,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #endif #include "magic.h" -#include "skill.h" -#include "study.h" +#include "contact.h" #include "helpers.h" #include "laws.h" +#include "skill.h" #include "spells.h" +#include "study.h" #include #include diff --git a/src/move.c b/src/move.c index 545883f8e..8088ddd13 100644 --- a/src/move.c +++ b/src/move.c @@ -44,18 +44,19 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "kernel/terrainid.h" #include "kernel/unit.h" -#include "move.h" +#include "alchemy.h" +#include "contact.h" #include "guard.h" #include "laws.h" +#include "lighthouse.h" +#include "monsters.h" +#include "move.h" +#include "piracy.h" #include "reports.h" #include "study.h" #include "spy.h" -#include "alchemy.h" #include "travelthru.h" #include "vortex.h" -#include "monsters.h" -#include "lighthouse.h" -#include "piracy.h" #include #include diff --git a/src/move.test.c b/src/move.test.c index 2c91165e2..b240171d5 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -2,6 +2,7 @@ #include #include "move.h" +#include "contact.h" #include "lighthouse.h" #include diff --git a/src/spells.c b/src/spells.c index dadce89c6..7d745147f 100644 --- a/src/spells.c +++ b/src/spells.c @@ -17,6 +17,7 @@ #include "spells.h" +#include "contact.h" #include "guard.h" #include "reports.h" #include "spy.h" diff --git a/src/test_eressea.c b/src/test_eressea.c index 12c658f91..fc5c49d1c 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -125,6 +125,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(automate); ADD_SUITE(battle); ADD_SUITE(calendar); + ADD_SUITE(contact); ADD_SUITE(creport); ADD_SUITE(donations); ADD_SUITE(economy);