diff --git a/src/kernel/building.c b/src/kernel/building.c index 60b588912..0056fba1e 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -83,15 +83,28 @@ const building_type *bt_find(const char *name) return bt_find_i(name); } +static int bt_changes = 1; + +bool bt_changed(int *cache) +{ + assert(cache); + if (*cache != bt_changes) { + *cache = bt_changes; + return true; + } + return false; +} + void bt_register(building_type * type) { if (type->init) { type->init(type); } ql_push(&buildingtypes, (void *)type); + ++bt_changes; } -void free_buildingtype(void *ptr) { +static void free_buildingtype(void *ptr) { building_type *btype = (building_type *)ptr; free_construction(btype->construction); free(btype->maintenance); @@ -103,6 +116,7 @@ void free_buildingtypes(void) { ql_foreach(buildingtypes, free_buildingtype); ql_free(buildingtypes); buildingtypes = 0; + ++bt_changes; } building_type *bt_get_or_create(const char *name) diff --git a/src/kernel/building.h b/src/kernel/building.h index 587197276..dad9ffdde 100644 --- a/src/kernel/building.h +++ b/src/kernel/building.h @@ -84,6 +84,7 @@ extern "C" { extern struct attrib_type at_building_action; building_type *bt_get_or_create(const char *name); + bool bt_changed(int *cache); const building_type *bt_find(const char *name); void free_buildingtypes(void); void register_buildings(void); diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 2f6fa6e34..755e9319f 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -17,16 +17,20 @@ static void test_register_building(CuTest * tc) { building_type *btype; + int cache = 0; test_cleanup(); btype = (building_type *)calloc(sizeof(building_type), 1); btype->_name = _strdup("herp"); + CuAssertIntEquals(tc, true, bt_changed(&cache)); + CuAssertIntEquals(tc, false, bt_changed(&cache)); bt_register(btype); + CuAssertIntEquals(tc, true, bt_changed(&cache)); CuAssertPtrNotNull(tc, bt_find("herp")); - // free(btype->_name); - // free(btype); + free_buildingtypes(); + CuAssertIntEquals(tc, true, bt_changed(&cache)); test_cleanup(); }