extract contact logic to a module

add missing tests for contact to units.
add an attribute for contacting a faction.
This commit is contained in:
Enno Rehling 2018-11-04 15:53:27 +01:00
parent bf04d95651
commit 0fd7906d41
17 changed files with 160 additions and 88 deletions

View File

@ -97,6 +97,7 @@ set (ERESSEA_SRC
automate.c automate.c
battle.c battle.c
chaos.c chaos.c
contact.c
creport.c creport.c
direction.c direction.c
donations.c donations.c
@ -214,6 +215,7 @@ set(TESTS_SRC
alchemy.test.c alchemy.test.c
automate.test.c automate.test.c
battle.test.c battle.test.c
contact.test.c
creport.test.c creport.test.c
direction.test.c direction.test.c
donations.test.c donations.test.c

View File

@ -5,6 +5,7 @@
#include "bind_process.h" #include "bind_process.h"
#include "battle.h" #include "battle.h"
#include "contact.h"
#include "economy.h" #include "economy.h"
#include "laws.h" #include "laws.h"
#include "magic.h" #include "magic.h"

80
src/contact.c Normal file
View File

@ -0,0 +1,80 @@
#ifdef _MSC_VER
#include <platform.h>
#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;
}

10
src/contact.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <stdbool.h>
struct unit;
struct faction;
bool ucontact(const struct unit *u, const struct unit *u2);
void usetcontact(struct unit *u, const struct unit *c);

36
src/contact.test.c Normal file
View File

@ -0,0 +1,36 @@
#include "contact.h"
#include "tests.h"
#include <CuTest.h>
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;
}

View File

@ -16,6 +16,7 @@
#include <kernel/config.h> #include <kernel/config.h>
#include "give.h" #include "give.h"
#include "contact.h"
#include "economy.h" #include "economy.h"
#include "laws.h" #include "laws.h"

View File

@ -1,6 +1,8 @@
#include <platform.h> #include <platform.h>
#include "give.h" #include "give.h"
#include "contact.h"
#include "economy.h" #include "economy.h"
#include <kernel/ally.h> #include <kernel/ally.h>

View File

@ -20,6 +20,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
#include "guard.h" #include "guard.h"
#include "contact.h"
#include "laws.h" #include "laws.h"
#include "monsters.h" #include "monsters.h"

View File

@ -22,7 +22,6 @@ struct critbit_tree;
static void test_read_unitid(CuTest *tc) { static void test_read_unitid(CuTest *tc) {
unit *u; unit *u;
order *ord; order *ord;
attrib *a;
struct locale *lang; struct locale *lang;
struct terrain_type *t_plain; 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 */ /* note that the english order is FIGHT, not COMBAT, so this is a poor example */
t_plain = test_create_terrain("plain", LAND_REGION); t_plain = test_create_terrain("plain", LAND_REGION);
u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, t_plain)); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, t_plain));
a = a_add(&u->attribs, a_new(&at_alias)); usetalias(u, atoi36("42"));
a->data.i = atoi36("42"); /* this unit is also TEMP 42 */
ord = create_order(K_GIVE, lang, "TEMP 42"); ord = create_order(K_GIVE, lang, "TEMP 42");
init_order_depr(ord); init_order_depr(ord);
@ -66,7 +64,6 @@ static void test_read_unitid(CuTest *tc) {
static void test_getunit(CuTest *tc) { static void test_getunit(CuTest *tc) {
unit *u, *u2; unit *u, *u2;
order *ord; order *ord;
attrib *a;
struct region *r; struct region *r;
struct locale *lang; struct locale *lang;
struct terrain_type *t_plain; 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 */ /* note that the english order is FIGHT, not COMBAT, so this is a poor example */
t_plain = test_create_terrain("plain", LAND_REGION); t_plain = test_create_terrain("plain", LAND_REGION);
u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, t_plain)); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, t_plain));
a = a_add(&u->attribs, a_new(&at_alias)); /* This unit is also TEMP 42: */
a->data.i = atoi36("42"); /* this unit is also TEMP 42 */ usetalias(u, atoi36("42"));
r = test_create_region(1, 0, t_plain); r = test_create_region(1, 0, t_plain);
ord = create_order(K_GIVE, lang, itoa36(u->no)); ord = create_order(K_GIVE, lang, itoa36(u->no));

View File

@ -430,7 +430,7 @@ unit *findnewunit(const region * r, const faction * f, int n)
/*********************/ /*********************/
/* at_alias */ /* at_alias */
/*********************/ /*********************/
attrib_type at_alias = { static attrib_type at_alias = {
"alias", "alias",
DEFAULT_INIT, DEFAULT_INIT,
DEFAULT_FINALIZE, DEFAULT_FINALIZE,
@ -451,6 +451,11 @@ int ualias(const unit * u)
return a->data.i; 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) int a_readprivate(variant *var, void *owner, gamedata *data)
{ {
struct storage *store = data->store; struct storage *store = data->store;
@ -517,58 +522,6 @@ void usetprivate(unit * u, const char *str)
a->data.v = str_strdup(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 ** init & cleanup module
**/ **/

View File

@ -120,15 +120,14 @@ extern "C" {
} unit; } unit;
extern struct attrib_type at_creator; 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_potionuser;
extern struct attrib_type at_contact;
extern struct attrib_type at_effect; extern struct attrib_type at_effect;
extern struct attrib_type at_private; extern struct attrib_type at_private;
extern struct attrib_type at_showskchange; extern struct attrib_type at_showskchange;
int ualias(const struct unit *u); int ualias(const struct unit *u);
void usetalias(unit *u, int alias);
int weight(const struct unit *u); int weight(const struct unit *u);
void renumber_unit(struct unit *u, int no); void renumber_unit(struct unit *u, int no);
@ -141,9 +140,6 @@ extern "C" {
const char *uprivate(const struct unit *u); const char *uprivate(const struct unit *u);
void usetprivate(struct unit *u, const char *c); 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, struct unit *findnewunit(const struct region *r, const struct faction *f,
int alias); int alias);

View File

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "alchemy.h" #include "alchemy.h"
#include "automate.h" #include "automate.h"
#include "battle.h" #include "battle.h"
#include "contact.h"
#include "economy.h" #include "economy.h"
#include "market.h" #include "market.h"
#include "morale.h" #include "morale.h"
@ -912,21 +913,6 @@ void demographics(void)
immigration(); 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) int leave_cmd(unit * u, struct order *ord)
{ {
region *r = u->region; 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); u2 = create_unit(u->region, u->faction, 0, u->faction->race, alias, s, u);
fset(u2, UFL_ISNEW); fset(u2, UFL_ISNEW);
a_add(&u2->attribs, a_new(&at_alias))->data.i = alias; usetalias(u2, alias);
sh = leftship(u); sh = leftship(u);
if (sh) { if (sh) {
set_leftship(u2, sh); set_leftship(u2, sh);

View File

@ -22,11 +22,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#endif #endif
#include "magic.h" #include "magic.h"
#include "skill.h" #include "contact.h"
#include "study.h"
#include "helpers.h" #include "helpers.h"
#include "laws.h" #include "laws.h"
#include "skill.h"
#include "spells.h" #include "spells.h"
#include "study.h"
#include <triggers/timeout.h> #include <triggers/timeout.h>
#include <triggers/shock.h> #include <triggers/shock.h>

View File

@ -44,18 +44,19 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "kernel/terrainid.h" #include "kernel/terrainid.h"
#include "kernel/unit.h" #include "kernel/unit.h"
#include "move.h" #include "alchemy.h"
#include "contact.h"
#include "guard.h" #include "guard.h"
#include "laws.h" #include "laws.h"
#include "lighthouse.h"
#include "monsters.h"
#include "move.h"
#include "piracy.h"
#include "reports.h" #include "reports.h"
#include "study.h" #include "study.h"
#include "spy.h" #include "spy.h"
#include "alchemy.h"
#include "travelthru.h" #include "travelthru.h"
#include "vortex.h" #include "vortex.h"
#include "monsters.h"
#include "lighthouse.h"
#include "piracy.h"
#include <spells/flyingship.h> #include <spells/flyingship.h>
#include <spells/unitcurse.h> #include <spells/unitcurse.h>

View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "move.h" #include "move.h"
#include "contact.h"
#include "lighthouse.h" #include "lighthouse.h"
#include <kernel/attrib.h> #include <kernel/attrib.h>

View File

@ -17,6 +17,7 @@
#include "spells.h" #include "spells.h"
#include "contact.h"
#include "guard.h" #include "guard.h"
#include "reports.h" #include "reports.h"
#include "spy.h" #include "spy.h"

View File

@ -125,6 +125,7 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(automate); ADD_SUITE(automate);
ADD_SUITE(battle); ADD_SUITE(battle);
ADD_SUITE(calendar); ADD_SUITE(calendar);
ADD_SUITE(contact);
ADD_SUITE(creport); ADD_SUITE(creport);
ADD_SUITE(donations); ADD_SUITE(donations);
ADD_SUITE(economy); ADD_SUITE(economy);