adding spellbooks.

spellbooks will take the level and magic school out of the spell definitions, and aggregate a list of spells with their minimum levels for learning from. factions will have a list of books they can learn from (different in E2 and E3 already).
This commit is contained in:
Enno Rehling 2012-05-23 12:42:14 -07:00
parent eea6bdb888
commit db7ed24b3e
7 changed files with 178 additions and 0 deletions

View File

@ -106,6 +106,7 @@
<ClCompile Include="kernel\item.c" /> <ClCompile Include="kernel\item.c" />
<ClCompile Include="kernel\item_test.c" /> <ClCompile Include="kernel\item_test.c" />
<ClCompile Include="kernel\magic.c" /> <ClCompile Include="kernel\magic.c" />
<ClCompile Include="kernel\magic_test.c" />
<ClCompile Include="kernel\message.c" /> <ClCompile Include="kernel\message.c" />
<ClCompile Include="kernel\move.c" /> <ClCompile Include="kernel\move.c" />
<ClCompile Include="kernel\move_test.c" /> <ClCompile Include="kernel\move_test.c" />
@ -125,6 +126,8 @@
<ClCompile Include="kernel\ship_test.c" /> <ClCompile Include="kernel\ship_test.c" />
<ClCompile Include="kernel\skill.c" /> <ClCompile Include="kernel\skill.c" />
<ClCompile Include="kernel\spell.c" /> <ClCompile Include="kernel\spell.c" />
<ClCompile Include="kernel\spellbook.c" />
<ClCompile Include="kernel\spellbook_test.c" />
<ClCompile Include="kernel\spell_test.c" /> <ClCompile Include="kernel\spell_test.c" />
<ClCompile Include="kernel\sqlite.c" /> <ClCompile Include="kernel\sqlite.c" />
<ClCompile Include="kernel\teleport.c" /> <ClCompile Include="kernel\teleport.c" />
@ -221,6 +224,7 @@
<ClInclude Include="kernel\ship.h" /> <ClInclude Include="kernel\ship.h" />
<ClInclude Include="kernel\skill.h" /> <ClInclude Include="kernel\skill.h" />
<ClInclude Include="kernel\spell.h" /> <ClInclude Include="kernel\spell.h" />
<ClInclude Include="kernel\spellbook.h" />
<ClInclude Include="kernel\spellid.h" /> <ClInclude Include="kernel\spellid.h" />
<ClInclude Include="kernel\teleport.h" /> <ClInclude Include="kernel\teleport.h" />
<ClInclude Include="kernel\terrain.h" /> <ClInclude Include="kernel\terrain.h" />

View File

@ -310,6 +310,15 @@
<ClCompile Include="kernel\item_test.c"> <ClCompile Include="kernel\item_test.c">
<Filter>kernel</Filter> <Filter>kernel</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="kernel\magic_test.c">
<Filter>kernel</Filter>
</ClCompile>
<ClCompile Include="kernel\spellbook.c">
<Filter>kernel</Filter>
</ClCompile>
<ClCompile Include="kernel\spellbook_test.c">
<Filter>kernel</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="kernel\alchemy.h"> <ClInclude Include="kernel\alchemy.h">
@ -603,5 +612,8 @@
<ClInclude Include="attributes\targetregion.h"> <ClInclude Include="attributes\targetregion.h">
<Filter>attributes</Filter> <Filter>attributes</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="kernel\spellbook.h">
<Filter>kernel</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

34
src/kernel/magic_test.c Normal file
View File

@ -0,0 +1,34 @@
#include <platform.h>
#include <kernel/types.h>
#include <kernel/magic.h>
#include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <util/quicklist.h>
#include <util/language.h>
#include <cutest/CuTest.h>
#include <tests.h>
void test_updatespells(CuTest * tc)
{
struct faction * f;
spell * sp;
spellbook * book = 0;
test_cleanup();
f = test_create_faction(0);
sp = create_spell("testspell", 0);
CuAssertPtrNotNull(tc, sp);
spellbook_add(&book, sp, 1);
update_spellbook(f, 1);
}
CuSuite *get_magic_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_updatespells);
return suite;
}

40
src/kernel/spellbook.c Normal file
View File

@ -0,0 +1,40 @@
#include <platform.h>
#include <kernel/config.h>
#include <util/quicklist.h>
#include "spellbook.h"
void spellbook_add(spellbook **sbp, struct spell * sp, int level)
{
spellbook_entry * sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry));
sbe->sp = sp;
sbe->level = level;
ql_push(sbp, sbe);
}
void spellbook_free(spellbook *sb)
{
quicklist *ql;
int qi;
for (qi = 0, ql = sb; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
free(sbe);
}
ql_free(sb);
}
int spellbook_foreach(spellbook *sb, int (*callback)(spellbook_entry *, void *), void * data)
{
quicklist *ql;
int qi;
for (qi = 0, ql = sb; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
int result = callback(sbe, data);
if (result) {
return result;
}
}
return 0;
}

44
src/kernel/spellbook.h Normal file
View File

@ -0,0 +1,44 @@
/*
Copyright (c) 1998-2012, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#ifndef H_KRNL_SPELLBOOK_H
#define H_KRNL_SPELLBOOK_H
#ifdef __cplusplus
extern "C" {
#endif
struct spell;
typedef struct spellbook_entry {
struct spell * sp;
int level;
} spellbook_entry;
typedef struct quicklist spellbook;
spellbook * school_books[MAXMAGIETYP];
void spellbook_add(spellbook **sbp, struct spell * sp, int level);
int spellbook_foreach(spellbook *sb, int (*callback)(spellbook_entry *, void *), void * data);
void spellbook_free(spellbook *sb);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,40 @@
#include <platform.h>
#include <kernel/types.h>
#include <kernel/magic.h>
#include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <util/quicklist.h>
#include <util/language.h>
#include <cutest/CuTest.h>
#include <tests.h>
int count_spell_cb(spellbook_entry * sbe, void * ptr)
{
int * counter = (int *)ptr;
++*counter;
return 0;
}
void test_spellbook(CuTest * tc)
{
spell * sp;
spellbook * sb = 0;
int counter = 0;
sp = create_spell("testspell", 0);
spellbook_add(&sb, sp, 1);
CuAssertPtrNotNull(tc, sb);
spellbook_foreach(sb, count_spell_cb, &counter);
CuAssertIntEquals(tc, 1, counter);
spellbook_free(sb);
}
CuSuite *get_spellbook_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_spellbook);
return suite;
}

View File

@ -9,6 +9,7 @@
#include <util/functions_test.c> #include <util/functions_test.c>
#include <util/quicklist_test.c> #include <util/quicklist_test.c>
#include <util/umlaut_test.c> #include <util/umlaut_test.c>
#include <kernel/magic_test.c>
#include <kernel/move_test.c> #include <kernel/move_test.c>
#include <kernel/item_test.c> #include <kernel/item_test.c>
#include <kernel/ship_test.c> #include <kernel/ship_test.c>
@ -17,6 +18,7 @@
#include <kernel/curse_test.c> #include <kernel/curse_test.c>
#include <kernel/battle_test.c> #include <kernel/battle_test.c>
#include <kernel/reports_test.c> #include <kernel/reports_test.c>
#include <kernel/spellbook_test.c>
#include <gamecode/laws_test.c> #include <gamecode/laws_test.c>
#include <gamecode/market_test.c> #include <gamecode/market_test.c>
@ -50,9 +52,11 @@ int RunAllTests(void)
/* kernel */ /* kernel */
CuSuiteAddSuite(suite, get_curse_suite()); CuSuiteAddSuite(suite, get_curse_suite());
CuSuiteAddSuite(suite, get_item_suite()); CuSuiteAddSuite(suite, get_item_suite());
CuSuiteAddSuite(suite, get_magic_suite());
CuSuiteAddSuite(suite, get_move_suite()); CuSuiteAddSuite(suite, get_move_suite());
CuSuiteAddSuite(suite, get_reports_suite()); CuSuiteAddSuite(suite, get_reports_suite());
CuSuiteAddSuite(suite, get_ship_suite()); CuSuiteAddSuite(suite, get_ship_suite());
CuSuiteAddSuite(suite, get_spellbook_suite());
CuSuiteAddSuite(suite, get_building_suite()); CuSuiteAddSuite(suite, get_building_suite());
CuSuiteAddSuite(suite, get_spell_suite()); CuSuiteAddSuite(suite, get_spell_suite());
CuSuiteAddSuite(suite, get_battle_suite()); CuSuiteAddSuite(suite, get_battle_suite());