Merge pull request #180 from stm2/spy_expand_april

expand spy messages, rebased
This commit is contained in:
Enno Rehling 2015-05-04 20:30:40 +02:00
commit 5f0a02ecb9
6 changed files with 125 additions and 5 deletions

View File

@ -3466,6 +3466,7 @@
</message> </message>
<message name="spyreport_mage" section="events"> <message name="spyreport_mage" section="events">
<type> <type>
<arg name="spy" type="unit"/>
<arg name="target" type="unit"/> <arg name="target" type="unit"/>
<arg name="type" type="string"/> <arg name="type" type="string"/>
</type> </type>
@ -3474,6 +3475,7 @@
</message> </message>
<message name="spyreport_skills" section="events"> <message name="spyreport_skills" section="events">
<type> <type>
<arg name="spy" type="unit"/>
<arg name="target" type="unit"/> <arg name="target" type="unit"/>
<arg name="skills" type="string"/> <arg name="skills" type="string"/>
</type> </type>
@ -3482,6 +3484,7 @@
</message> </message>
<message name="spyreport_items" section="events"> <message name="spyreport_items" section="events">
<type> <type>
<arg name="spy" type="unit"/>
<arg name="target" type="unit"/> <arg name="target" type="unit"/>
<arg name="items" type="items"/> <arg name="items" type="items"/>
</type> </type>

View File

@ -178,6 +178,7 @@ set(TESTS_SRC
move.test.c move.test.c
skill.test.c skill.test.c
upkeep.test.c upkeep.test.c
spy.test.c
${ATTRIBUTES_TESTS} ${ATTRIBUTES_TESTS}
${UTIL_TESTS} ${UTIL_TESTS}
${KERNEL_TESTS} ${KERNEL_TESTS}

View File

@ -147,6 +147,7 @@ message *msg_message(const char *name, const char *sig, ...)
const message_type *mtype = mt_find(name); const message_type *mtype = mt_find(name);
char paramname[64]; char paramname[64];
const char *ic = sig; const char *ic = sig;
int argnum=0;
variant args[16]; variant args[16];
memset(args, 0, sizeof(args)); memset(args, 0, sizeof(args));
@ -183,6 +184,7 @@ message *msg_message(const char *name, const char *sig, ...)
else { else {
assert(!"unknown variant type"); assert(!"unknown variant type");
} }
argnum++;
} }
else { else {
log_error("invalid parameter %s for message type %s\n", paramname, mtype->name); log_error("invalid parameter %s for message type %s\n", paramname, mtype->name);
@ -192,6 +194,10 @@ message *msg_message(const char *name, const char *sig, ...)
ic++; ic++;
} }
va_end(vargs); va_end(vargs);
if (argnum != mtype->nparameters) {
log_error("not enough parameters for message type %s\n", mtype->name);
assert(!"program aborted.");
}
return msg_create(mtype, args); return msg_create(mtype, args);
} }

View File

@ -61,12 +61,12 @@ void spy_message(int spy, const unit * u, const unit * target)
const char *str = report_kampfstatus(target, u->faction->locale); const char *str = report_kampfstatus(target, u->faction->locale);
ADDMSG(&u->faction->msgs, msg_message("spyreport", "spy target status", u, ADDMSG(&u->faction->msgs, msg_message("spyreport", "spy target status", u,
target, str)); target, str));
if (spy > 20) { if (spy > 20) {
sc_mage *mage = get_mage(target); sc_mage *mage = get_mage(target);
/* for mages, spells and magic school */ /* for mages, spells and magic school */
if (mage) { if (mage) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_mage", "target type", ADDMSG(&u->faction->msgs, msg_message("spyreport_mage", "spy target type", u,
target, magic_school[mage->magietyp])); target, magic_school[mage->magietyp]));
} }
} }
@ -75,7 +75,7 @@ void spy_message(int spy, const unit * u, const unit * target)
if (fv && fv != target->faction) { if (fv && fv != target->faction) {
/* true faction */ /* true faction */
ADDMSG(&u->faction->msgs, msg_message("spyreport_faction", ADDMSG(&u->faction->msgs, msg_message("spyreport_faction",
"target faction", target, target->faction)); "spy target faction", u, target, target->faction));
add_seen_faction(u->faction, target->faction); add_seen_faction(u->faction, target->faction);
} }
} }
@ -103,12 +103,12 @@ void spy_message(int spy, const unit * u, const unit * target)
} }
} }
if (found) { if (found) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_skills", "target skills", ADDMSG(&u->faction->msgs, msg_message("spyreport_skills", "spy target skills", u,
target, buf)); target, buf));
} }
if (target->items) { if (target->items) {
ADDMSG(&u->faction->msgs, msg_message("spyreport_items", "target items", ADDMSG(&u->faction->msgs, msg_message("spyreport_items", "spy target items", u,
target, target->items)); target, target->items));
} }
} }

109
src/spy.test.c Normal file
View File

@ -0,0 +1,109 @@
#include <platform.h>
#include <magic.h>
#include <kernel/types.h>
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/messages.h>
#include <util/attrib.h>
#include <util/message.h>
#include <util/crmessage.h>
#include <tests.h>
#include <attributes/otherfaction.h>
#include "spy.h"
#include <stdio.h>
#include <CuTest.h>
typedef enum {
M_BASE,
M_MAGE,
M_SKILLS,
M_FACTION,
M_ITEMS,
NUM_TYPES
} m_type;
typedef struct {
region *r;
unit *spy;
unit *victim;
const message_type *msg_types[NUM_TYPES];
} spy_fixture;
static void setup_spy(spy_fixture *fix) {
test_cleanup();
fix->r = test_create_region(0, 0, NULL);
fix->spy = test_create_unit(test_create_faction(NULL), fix->r);
fix->victim = test_create_unit(test_create_faction(NULL), fix->r);
fix->msg_types[M_BASE] = register_msg("spyreport", 3, "spy:unit", "target:unit", "status:string");
fix->msg_types[M_MAGE] = register_msg("spyreport_mage", 3, "spy:unit", "target:unit", "type:string");
fix->msg_types[M_SKILLS] = register_msg("spyreport_skills", 3, "spy:unit", "target:unit", "skills:string");
fix->msg_types[M_FACTION] = register_msg("spyreport_faction", 3, "spy:unit", "target:unit", "faction:faction");
fix->msg_types[M_ITEMS] = register_msg("spyreport_items", 3, "spy:unit", "target:unit", "items:items");
}
static void test_simple_spy_message(CuTest *tc) {
spy_fixture fix;
setup_spy(&fix);
spy_message(0, fix.spy, fix.victim);
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 1, true, M_BASE);
test_cleanup();
}
static void set_factionstealth(unit *u, faction *f) {
attrib *a = a_find(u->attribs, &at_otherfaction);
if (!a)
a = a_add(&u->attribs, make_otherfaction(f));
else
a->data.v = f;
}
static void test_all_spy_message(CuTest *tc) {
spy_fixture fix;
setup_spy(&fix);
enable_skill(SK_MAGIC, true);
set_level(fix.victim, SK_MINING, 2);
set_level(fix.victim, SK_MAGIC, 2);
create_mage(fix.victim, M_DRAIG);
set_factionstealth(fix.victim, fix.spy->faction);
item_type *itype;
itype = it_get_or_create(rt_get_or_create("sword"));
new_weapontype(itype, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 2);
i_change(&fix.victim->items, itype, 1);
spy_message(99, fix.spy, fix.victim);
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 5, true,
M_BASE,
M_MAGE,
M_FACTION,
M_SKILLS,
M_ITEMS);
test_cleanup();
}
CuSuite *get_spy_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_simple_spy_message);
SUITE_ADD_TEST(suite, test_all_spy_message);
return suite;
}

View File

@ -84,6 +84,7 @@ int RunAllTests(void)
RUN_TESTS(suite, upkeep); RUN_TESTS(suite, upkeep);
RUN_TESTS(suite, vortex); RUN_TESTS(suite, vortex);
RUN_TESTS(suite, wormhole); RUN_TESTS(suite, wormhole);
RUN_TESTS(suite, spy);
printf("\ntest summary: %d tests, %d failed\n", suite->count, suite->failCount); printf("\ntest summary: %d tests, %d failed\n", suite->count, suite->failCount);
log_flags = flags; log_flags = flags;