From db7ed24b3e4b43a0c2722f8bcb5f35e52dbf7f07 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 23 May 2012 12:42:14 -0700 Subject: [PATCH] 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). --- src/kernel.vcxproj | 4 ++++ src/kernel.vcxproj.filters | 12 ++++++++++ src/kernel/magic_test.c | 34 ++++++++++++++++++++++++++++ src/kernel/spellbook.c | 40 +++++++++++++++++++++++++++++++++ src/kernel/spellbook.h | 44 +++++++++++++++++++++++++++++++++++++ src/kernel/spellbook_test.c | 40 +++++++++++++++++++++++++++++++++ src/tests.c | 4 ++++ 7 files changed, 178 insertions(+) create mode 100644 src/kernel/magic_test.c create mode 100644 src/kernel/spellbook.c create mode 100644 src/kernel/spellbook.h create mode 100644 src/kernel/spellbook_test.c diff --git a/src/kernel.vcxproj b/src/kernel.vcxproj index 8c46343d9..6db6ea17b 100644 --- a/src/kernel.vcxproj +++ b/src/kernel.vcxproj @@ -106,6 +106,7 @@ + @@ -125,6 +126,8 @@ + + @@ -221,6 +224,7 @@ + diff --git a/src/kernel.vcxproj.filters b/src/kernel.vcxproj.filters index f385b7c1f..f09da757a 100644 --- a/src/kernel.vcxproj.filters +++ b/src/kernel.vcxproj.filters @@ -310,6 +310,15 @@ kernel + + kernel + + + kernel + + + kernel + @@ -603,5 +612,8 @@ attributes + + kernel + \ No newline at end of file diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c new file mode 100644 index 000000000..c24c727c6 --- /dev/null +++ b/src/kernel/magic_test.c @@ -0,0 +1,34 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +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; +} diff --git a/src/kernel/spellbook.c b/src/kernel/spellbook.c new file mode 100644 index 000000000..00dd6de7a --- /dev/null +++ b/src/kernel/spellbook.c @@ -0,0 +1,40 @@ +#include +#include +#include + +#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; +} diff --git a/src/kernel/spellbook.h b/src/kernel/spellbook.h new file mode 100644 index 000000000..71eb427d2 --- /dev/null +++ b/src/kernel/spellbook.h @@ -0,0 +1,44 @@ +/* +Copyright (c) 1998-2012, Enno Rehling + Katja Zedel + +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 diff --git a/src/kernel/spellbook_test.c b/src/kernel/spellbook_test.c new file mode 100644 index 000000000..c4adad24d --- /dev/null +++ b/src/kernel/spellbook_test.c @@ -0,0 +1,40 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + + +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; +} diff --git a/src/tests.c b/src/tests.c index 46e5c4304..c73b2bb18 100644 --- a/src/tests.c +++ b/src/tests.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include @@ -50,9 +52,11 @@ int RunAllTests(void) /* kernel */ CuSuiteAddSuite(suite, get_curse_suite()); CuSuiteAddSuite(suite, get_item_suite()); + CuSuiteAddSuite(suite, get_magic_suite()); CuSuiteAddSuite(suite, get_move_suite()); CuSuiteAddSuite(suite, get_reports_suite()); CuSuiteAddSuite(suite, get_ship_suite()); + CuSuiteAddSuite(suite, get_spellbook_suite()); CuSuiteAddSuite(suite, get_building_suite()); CuSuiteAddSuite(suite, get_spell_suite()); CuSuiteAddSuite(suite, get_battle_suite());