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

View File

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

View File

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

View File

@ -23,7 +23,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* kernel includes */
#include "item.h"
#include "unit.h"
#include "faction.h"
#include "race.h"
#include "spellbook.h"
/* util includes */
#include <util/quicklist.h>
@ -43,13 +45,9 @@ equipment *create_equipment(const char *eqname)
struct equipment *eq = *eqp;
int i = eq ? strcmp(eq->name, eqname) : 1;
if (i > 0) {
eq = malloc(sizeof(equipment));
eq = (equipment *)calloc(1, sizeof(equipment));
eq->name = strdup(eqname);
eq->next = *eqp;
eq->items = NULL;
eq->spells = NULL;
eq->subsets = NULL;
eq->callback = NULL;
memset(eq->skills, 0, sizeof(eq->skills));
*eqp = eq;
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) {
ql_set_insert(&eq->spells, sp);
if (eq) {
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) {
quicklist *ql = eq->spells;
if (ql) {
int qi;
if (eq->spellbook) {
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)) {
spell *sp = (spell *) ql_get(ql, qi);
add_spell(&m->spells, sp);
add_spellname(m, sp);
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
add_spell(&m->spells, sbe->sp);
add_spellname(m, sbe->sp);
}
}
}

View File

@ -43,7 +43,7 @@ extern "C" {
char *name;
struct itemdata *items;
char *skills[MAXSKILLS];
struct quicklist *spells;
struct spellbook *spellbook;
struct subset *subsets;
struct equipment *next;
void (*callback) (const struct equipment *, struct unit *);
@ -56,7 +56,7 @@ extern "C" {
const struct item_type *itype, const char *value);
extern void equipment_setskill(struct equipment *eq, skill_t sk,
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,
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 <= UNIT_MAXSIZE);
#ifndef NDEBUG
assert(u->faction || count == 0);
#endif
if (count == 0) {
u->flags &= ~(UFL_HERO);
}
if (u->faction) {
if (playerrace(u->race)) {
if (u->faction && playerrace(u->race)) {
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");
}
}
boolean learn_skill(unit * u, skill_t sk, double chance)
@ -1415,15 +1406,22 @@ void name_unit(unit * u)
}
} else {
char name[32];
const char * result;
const struct locale * lang = u->faction ? u->faction->locale : default_locale;
if (lang) {
static const char * prefix[MAXLOCALES];
int i = locale_index(u->faction->locale);
int i = locale_index(lang);
if (!prefix[i]) {
prefix[i] = LOC(u->faction->locale, "unitdefault");
prefix[i] = LOC(lang, "unitdefault");
if (!prefix[i]) {
prefix[i] = parameters[P_UNIT];
}
}
snprintf(name, sizeof(name), "%s %s", prefix[i], itoa36(u->no));
result = prefix[i];
} else {
result = parameters[P_UNIT];
}
snprintf(name, sizeof(name), "%s %s", result, itoa36(u->no));
unit_setname(u, name);
}
}
@ -1436,9 +1434,10 @@ void name_unit(unit * u)
unit *create_unit(region * r, faction * f, int number, const struct race *urace,
int id, const char *dname, unit * creator)
{
unit *u = calloc(1, sizeof(unit));
unit *u = (unit *)calloc(1, sizeof(unit));
assert(urace);
if (f) {
assert(f->alive);
u_setfaction(u, f);
@ -1449,6 +1448,7 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
addlist(&u->orders, deford);
}
}
}
u_seteffstealth(u, -1);
u->race = urace;
u->irace = NULL;
@ -1473,8 +1473,6 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
} else {
u->name = strdup(dname);
}
if (count_unit(u))
f->no_units++;
if (creator) {
attrib *a;

View File

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

View File

@ -17,6 +17,7 @@
#include <kernel/spell_test.c>
#include <kernel/curse_test.c>
#include <kernel/battle_test.c>
#include <kernel/equipment_test.c>
#include <kernel/reports_test.c>
#include <kernel/spellbook_test.c>
#include <gamecode/laws_test.c>
@ -51,6 +52,7 @@ int RunAllTests(void)
CuSuiteAddSuite(suite, get_umlaut_suite());
/* kernel */
CuSuiteAddSuite(suite, get_curse_suite());
CuSuiteAddSuite(suite, get_equipment_suite());
CuSuiteAddSuite(suite, get_item_suite());
CuSuiteAddSuite(suite, get_magic_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)
{
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;
}