equipment sets that include spells now have levels on them and store in a spellbook.

add some tests for equipment.
This commit is contained in:
Enno Rehling 2012-05-24 23:50:27 -07:00
parent 8ecda7b203
commit f5b35a9a2b
9 changed files with 136 additions and 72 deletions

View file

@ -32,9 +32,9 @@
<set name="songdragon_familiar"> <set name="songdragon_familiar">
<skill name="magic" level="1"/> <skill name="magic" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="flee"/> <spell name="flee" level="2"/>
<spell name="sleep"/> <spell name="sleep" level="7"/>
<spell name="frighten"/> <spell name="frighten" level="8"/>
</set> </set>
<set name="nymph_familiar"> <set name="nymph_familiar">
@ -48,10 +48,10 @@
<skill name="entertainment" level="1"/> <skill name="entertainment" level="1"/>
<skill name="perception" level="1"/> <skill name="perception" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="seduction"/> <spell name="seduction" level="6"/>
<spell name="calm_monster"/> <spell name="calm_monster" level="6"/>
<spell name="song_of_confusion"/> <spell name="song_of_confusion" level="4"/>
<spell name="appeasement"/> <spell name="appeasement" level="1"/>
</set> </set>
<set name="unicorn_familiar"> <set name="unicorn_familiar">
@ -59,12 +59,12 @@
<skill name="stealth" level="1"/> <skill name="stealth" level="1"/>
<skill name="perception" level="1"/> <skill name="perception" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="resist_magic"/> <spell name="resist_magic" level="3"/>
<spell name="song_of_peace"/> <spell name="song_of_peace" level="12"/>
<spell name="calm_monster"/> <spell name="calm_monster" level="6"/>
<spell name="heroic_song"/> <spell name="heroic_song" level="5"/>
<spell name="song_of_healing"/> <spell name="song_of_healing" level="2"/>
<spell name="appeasement"/> <spell name="appeasement" level="1"/>
</set> </set>
<set name="direwolf_familiar"> <set name="direwolf_familiar">
@ -75,9 +75,9 @@
<set name="ghost_familiar"> <set name="ghost_familiar">
<skill name="magic" level="1"/> <skill name="magic" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="steal_aura"/> <spell name="steal_aura" level="6"/>
<spell name="frighten"/> <spell name="frighten" level="8"/>
<spell name="summonundead"/> <spell name="summonundead" level="6"/>
</set> </set>
<set name="imp_familiar"> <set name="imp_familiar">
@ -87,9 +87,9 @@
<skill name="perception" level="1"/> <skill name="perception" level="1"/>
<skill name="taxation" level="1"/> <skill name="taxation" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="steal_aura"/> <spell name="steal_aura" level="6"/>
<spell name="shapeshift"/> <spell name="shapeshift" level="3"/>
<spell name="seduction"/> <spell name="seduction" level="6"/>
</set> </set>
<set name="dreamcat_familiar"> <set name="dreamcat_familiar">
@ -99,16 +99,16 @@
<skill name="perception" level="1"/> <skill name="perception" level="1"/>
<skill name="taxation" level="1"/> <skill name="taxation" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="shapeshift"/> <spell name="shapeshift" level="3"/>
<spell name="transferauratraum"/> <spell name="transferauratraum" level="3"/>
</set> </set>
<set name="fairy_familiar"> <set name="fairy_familiar">
<skill name="magic" level="1"/> <skill name="magic" level="1"/>
<!-- spells --> <!-- spells -->
<spell name="appeasement"/> <spell name="appeasement" level="1"/>
<spell name="calm_monster"/> <spell name="calm_monster" level="6"/>
<spell name="seduction"/> <spell name="seduction" level="6"/>
</set> </set>
<set name="owl_familiar"> <set name="owl_familiar">

View file

@ -101,6 +101,7 @@
<ClCompile Include="kernel\connection.c" /> <ClCompile Include="kernel\connection.c" />
<ClCompile Include="kernel\curse.c" /> <ClCompile Include="kernel\curse.c" />
<ClCompile Include="kernel\equipment.c" /> <ClCompile Include="kernel\equipment.c" />
<ClCompile Include="kernel\equipment_test.c" />
<ClCompile Include="kernel\faction.c" /> <ClCompile Include="kernel\faction.c" />
<ClCompile Include="kernel\group.c" /> <ClCompile Include="kernel\group.c" />
<ClCompile Include="kernel\item.c" /> <ClCompile Include="kernel\item.c" />

View file

@ -319,6 +319,9 @@
<ClCompile Include="kernel\spellbook_test.c"> <ClCompile Include="kernel\spellbook_test.c">
<Filter>kernel</Filter> <Filter>kernel</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="kernel\equipment_test.c">
<Filter>kernel</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="kernel\alchemy.h"> <ClInclude Include="kernel\alchemy.h">

View file

@ -23,7 +23,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* kernel includes */ /* kernel includes */
#include "item.h" #include "item.h"
#include "unit.h" #include "unit.h"
#include "faction.h"
#include "race.h" #include "race.h"
#include "spellbook.h"
/* util includes */ /* util includes */
#include <util/quicklist.h> #include <util/quicklist.h>
@ -43,13 +45,9 @@ equipment *create_equipment(const char *eqname)
struct equipment *eq = *eqp; struct equipment *eq = *eqp;
int i = eq ? strcmp(eq->name, eqname) : 1; int i = eq ? strcmp(eq->name, eqname) : 1;
if (i > 0) { if (i > 0) {
eq = malloc(sizeof(equipment)); eq = (equipment *)calloc(1, sizeof(equipment));
eq->name = strdup(eqname); eq->name = strdup(eqname);
eq->next = *eqp; eq->next = *eqp;
eq->items = NULL;
eq->spells = NULL;
eq->subsets = NULL;
eq->callback = NULL;
memset(eq->skills, 0, sizeof(eq->skills)); memset(eq->skills, 0, sizeof(eq->skills));
*eqp = eq; *eqp = eq;
break; break;
@ -85,10 +83,13 @@ void equipment_setskill(equipment * eq, skill_t sk, const char *value)
} }
} }
void equipment_addspell(equipment * eq, spell * sp) void equipment_addspell(equipment * eq, spell * sp, int level)
{ {
if (eq != NULL) { if (eq) {
ql_set_insert(&eq->spells, sp); if (!eq->spellbook) {
eq->spellbook = create_spellbook(0);
}
spellbook_add(eq->spellbook, sp, level);
} }
} }
@ -140,16 +141,18 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask)
} }
if (mask & EQUIP_SPELLS) { if (mask & EQUIP_SPELLS) {
quicklist *ql = eq->spells; if (eq->spellbook) {
if (ql) {
int qi;
sc_mage *m = get_mage(u); sc_mage *m = get_mage(u);
quicklist * ql = eq->spellbook->spells;
int qi;
assert(m || !"trying to equip spells on a non-mage!"); if (!m) {
m = create_mage(u, u->faction?u->faction->magiegebiet:M_GRAY);
}
for (qi = 0; ql; ql_advance(&ql, &qi, 1)) { for (qi = 0; ql; ql_advance(&ql, &qi, 1)) {
spell *sp = (spell *) ql_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
add_spell(&m->spells, sp); add_spell(&m->spells, sbe->sp);
add_spellname(m, sp); add_spellname(m, sbe->sp);
} }
} }
} }

View file

@ -43,7 +43,7 @@ extern "C" {
char *name; char *name;
struct itemdata *items; struct itemdata *items;
char *skills[MAXSKILLS]; char *skills[MAXSKILLS];
struct quicklist *spells; struct spellbook *spellbook;
struct subset *subsets; struct subset *subsets;
struct equipment *next; struct equipment *next;
void (*callback) (const struct equipment *, struct unit *); void (*callback) (const struct equipment *, struct unit *);
@ -56,7 +56,7 @@ extern "C" {
const struct item_type *itype, const char *value); const struct item_type *itype, const char *value);
extern void equipment_setskill(struct equipment *eq, skill_t sk, extern void equipment_setskill(struct equipment *eq, skill_t sk,
const char *value); const char *value);
extern void equipment_addspell(struct equipment *eq, struct spell *sp); extern void equipment_addspell(struct equipment *eq, struct spell *sp, int level);
extern void equipment_setcallback(struct equipment *eq, extern void equipment_setcallback(struct equipment *eq,
void (*callback) (const struct equipment *, struct unit *)); void (*callback) (const struct equipment *, struct unit *));

View file

@ -0,0 +1,56 @@
#include <platform.h>
#include <kernel/types.h>
#include <kernel/equipment.h>
#include <kernel/item.h>
#include <kernel/unit.h>
#include <kernel/magic.h>
#include <kernel/skill.h>
#include <kernel/spell.h>
#include <util/quicklist.h>
#include <cutest/CuTest.h>
#include <tests.h>
void test_equipment(CuTest * tc)
{
equipment * eq;
unit * u;
const item_type * it_horses;
const char * names[] = {"horse", "horse_p"};
spell *sp;
sc_mage * mage;
test_cleanup();
skill_enabled[SK_MAGIC] = 1;
it_horses = test_create_itemtype(names);
CuAssertPtrNotNull(tc, it_horses);
sp = create_spell("testspell", 0);
CuAssertPtrNotNull(tc, sp);
CuAssertPtrEquals(tc, 0, get_equipment("herpderp"));
eq = create_equipment("herpderp");
CuAssertPtrEquals(tc, eq, get_equipment("herpderp"));
equipment_setitem(eq, it_horses, "1");
equipment_setskill(eq, SK_MAGIC, "5");
equipment_addspell(eq, sp, 1);
u = test_create_unit(0, 0);
equip_unit_mask(u, eq, EQUIP_ALL);
CuAssertIntEquals(tc, 1, i_get(u->items, it_horses));
CuAssertIntEquals(tc, 5, get_level(u, SK_MAGIC));
mage = get_mage(u);
CuAssertPtrNotNull(tc, mage);
CuAssertPtrNotNull(tc, mage->spells);
CuAssertTrue(tc, u_hasspell(mage, sp));
}
CuSuite *get_equipment_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_equipment);
return suite;
}

View file

@ -1103,22 +1103,13 @@ void set_number(unit * u, int count)
assert(count >= 0); assert(count >= 0);
assert(count <= UNIT_MAXSIZE); assert(count <= UNIT_MAXSIZE);
#ifndef NDEBUG
assert(u->faction || count == 0);
#endif
if (count == 0) { if (count == 0) {
u->flags &= ~(UFL_HERO); u->flags &= ~(UFL_HERO);
} }
if (u->faction) { if (u->faction && playerrace(u->race)) {
if (playerrace(u->race)) { u->faction->num_people += count - u->number;
u->faction->num_people += count - u->number;
}
u->number = (unsigned short)count;
} else if (u->number > 0) {
assert
(!"why doesn't this unit have a faction? this will fuck up num_people");
} }
u->number = (unsigned short)count;
} }
boolean learn_skill(unit * u, skill_t sk, double chance) boolean learn_skill(unit * u, skill_t sk, double chance)
@ -1415,15 +1406,22 @@ void name_unit(unit * u)
} }
} else { } else {
char name[32]; char name[32];
static const char * prefix[MAXLOCALES]; const char * result;
int i = locale_index(u->faction->locale); const struct locale * lang = u->faction ? u->faction->locale : default_locale;
if (!prefix[i]) { if (lang) {
prefix[i] = LOC(u->faction->locale, "unitdefault"); static const char * prefix[MAXLOCALES];
int i = locale_index(lang);
if (!prefix[i]) { if (!prefix[i]) {
prefix[i] = parameters[P_UNIT]; prefix[i] = LOC(lang, "unitdefault");
if (!prefix[i]) {
prefix[i] = parameters[P_UNIT];
}
} }
result = prefix[i];
} else {
result = parameters[P_UNIT];
} }
snprintf(name, sizeof(name), "%s %s", prefix[i], itoa36(u->no)); snprintf(name, sizeof(name), "%s %s", result, itoa36(u->no));
unit_setname(u, name); unit_setname(u, name);
} }
} }
@ -1436,17 +1434,19 @@ void name_unit(unit * u)
unit *create_unit(region * r, faction * f, int number, const struct race *urace, unit *create_unit(region * r, faction * f, int number, const struct race *urace,
int id, const char *dname, unit * creator) int id, const char *dname, unit * creator)
{ {
unit *u = calloc(1, sizeof(unit)); unit *u = (unit *)calloc(1, sizeof(unit));
assert(urace); assert(urace);
assert(f->alive); if (f) {
u_setfaction(u, f); assert(f->alive);
u_setfaction(u, f);
if (f->locale) { if (f->locale) {
order *deford = default_order(f->locale); order *deford = default_order(f->locale);
if (deford) { if (deford) {
set_order(&u->thisorder, NULL); set_order(&u->thisorder, NULL);
addlist(&u->orders, deford); addlist(&u->orders, deford);
}
} }
} }
u_seteffstealth(u, -1); u_seteffstealth(u, -1);
@ -1473,8 +1473,6 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
} else { } else {
u->name = strdup(dname); u->name = strdup(dname);
} }
if (count_unit(u))
f->no_units++;
if (creator) { if (creator) {
attrib *a; attrib *a;

View file

@ -1314,7 +1314,8 @@ static void add_spells(equipment * eq, xmlNodeSetPtr nsetItems)
if (!sp) { if (!sp) {
log_error("no spell '%s' in school '%s' for equipment-set '%s'\n", (const char *)propValue, magic_school[mtype], eq->name); log_error("no spell '%s' in school '%s' for equipment-set '%s'\n", (const char *)propValue, magic_school[mtype], eq->name);
} else { } else {
equipment_addspell(eq, sp); int level = xml_ivalue(node, "level", sp->level);
equipment_addspell(eq, sp, level);
} }
xmlFree(propValue); xmlFree(propValue);
} }
@ -1438,7 +1439,7 @@ static int parse_equipment(xmlDocPtr doc)
xmlXPathFreeObject(xpathResult); xmlXPathFreeObject(xpathResult);
xpathResult = xmlXPathEvalExpression(BAD_CAST "spell", xpath); xpathResult = xmlXPathEvalExpression(BAD_CAST "spell", xpath);
assert(!eq->spells); assert(!eq->spellbook);
add_spells(eq, xpathResult->nodesetval); add_spells(eq, xpathResult->nodesetval);
xmlXPathFreeObject(xpathResult); xmlXPathFreeObject(xpathResult);

View file

@ -17,6 +17,7 @@
#include <kernel/spell_test.c> #include <kernel/spell_test.c>
#include <kernel/curse_test.c> #include <kernel/curse_test.c>
#include <kernel/battle_test.c> #include <kernel/battle_test.c>
#include <kernel/equipment_test.c>
#include <kernel/reports_test.c> #include <kernel/reports_test.c>
#include <kernel/spellbook_test.c> #include <kernel/spellbook_test.c>
#include <gamecode/laws_test.c> #include <gamecode/laws_test.c>
@ -51,6 +52,7 @@ int RunAllTests(void)
CuSuiteAddSuite(suite, get_umlaut_suite()); CuSuiteAddSuite(suite, get_umlaut_suite());
/* kernel */ /* kernel */
CuSuiteAddSuite(suite, get_curse_suite()); CuSuiteAddSuite(suite, get_curse_suite());
CuSuiteAddSuite(suite, get_equipment_suite());
CuSuiteAddSuite(suite, get_item_suite()); CuSuiteAddSuite(suite, get_item_suite());
CuSuiteAddSuite(suite, get_magic_suite()); CuSuiteAddSuite(suite, get_magic_suite());
CuSuiteAddSuite(suite, get_move_suite()); CuSuiteAddSuite(suite, get_move_suite());
@ -101,7 +103,7 @@ struct faction *test_create_faction(const struct race *rc)
struct unit *test_create_unit(struct faction *f, struct region *r) struct unit *test_create_unit(struct faction *f, struct region *r)
{ {
unit *u = create_unit(r, f, 1, f->race, 0, 0, 0); unit *u = create_unit(r, f, 1, f?f->race:rc_find("human"), 0, 0, 0);
return u; return u;
} }