forked from github/server
add a function to compare size of occupied castles
This commit is contained in:
parent
dcdb5354d9
commit
044953e4ab
|
@ -335,7 +335,18 @@ const building_type *findbuildingtype(const char *name,
|
||||||
return (const building_type *)type.v;
|
return (const building_type *)type.v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int building_protection(const building * b, const unit * u, building_bonus bonus)
|
int cmp_castle_size(const building * b, const building * a)
|
||||||
|
{
|
||||||
|
if (!b || !b->type->protection || !building_owner(b)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!a || !a->type->protection || !building_owner(a)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return b->size - a->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int building_protection(const building * b, const unit * u, building_bonus bonus)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int bsize = buildingeffsize(b, false);
|
int bsize = buildingeffsize(b, false);
|
||||||
|
|
|
@ -85,6 +85,8 @@ extern "C" {
|
||||||
extern struct selist *buildingtypes;
|
extern struct selist *buildingtypes;
|
||||||
extern struct attrib_type at_building_action;
|
extern struct attrib_type at_building_action;
|
||||||
|
|
||||||
|
int cmp_castle_size(const struct building * b, const struct building * a);
|
||||||
|
int building_protection(const struct building * b, const struct unit * u, building_bonus bonus);
|
||||||
building_type *bt_get_or_create(const char *name);
|
building_type *bt_get_or_create(const char *name);
|
||||||
bool bt_changed(int *cache);
|
bool bt_changed(int *cache);
|
||||||
const building_type *bt_find(const char *name);
|
const building_type *bt_find(const char *name);
|
||||||
|
|
|
@ -493,9 +493,35 @@ static void test_building_type(CuTest *tc) {
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_cmp_castle_size(CuTest *tc) {
|
||||||
|
region *r;
|
||||||
|
building *b1, *b2;
|
||||||
|
building_type *bt_castle;
|
||||||
|
unit *u1, *u2;
|
||||||
|
|
||||||
|
test_setup();
|
||||||
|
bt_castle = test_create_buildingtype("castle");
|
||||||
|
bt_castle->protection = building_protection;
|
||||||
|
r = test_create_region(0, 0, 0);
|
||||||
|
b1 = test_create_building(r, bt_castle);
|
||||||
|
b2 = test_create_building(r, bt_castle);
|
||||||
|
u1 = test_create_unit(test_create_faction(0), r);
|
||||||
|
u_set_building(u1, b1);
|
||||||
|
u2 = test_create_unit(test_create_faction(0), r);
|
||||||
|
u_set_building(u2, b2);
|
||||||
|
b1->size = 5;
|
||||||
|
b2->size = 10;
|
||||||
|
CuAssertTrue(tc, cmp_castle_size(b1, b2)<0);
|
||||||
|
CuAssertTrue(tc, cmp_castle_size(b2, b1)>0);
|
||||||
|
CuAssertTrue(tc, cmp_castle_size(b1, NULL)>0);
|
||||||
|
CuAssertTrue(tc, cmp_castle_size(NULL, b1)<0);
|
||||||
|
test_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
CuSuite *get_building_suite(void)
|
CuSuite *get_building_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
CuSuite *suite = CuSuiteNew();
|
||||||
|
SUITE_ADD_TEST(suite, test_cmp_castle_size);
|
||||||
SUITE_ADD_TEST(suite, test_register_building);
|
SUITE_ADD_TEST(suite, test_register_building);
|
||||||
SUITE_ADD_TEST(suite, test_btype_defaults);
|
SUITE_ADD_TEST(suite, test_btype_defaults);
|
||||||
SUITE_ADD_TEST(suite, test_building_set_owner);
|
SUITE_ADD_TEST(suite, test_building_set_owner);
|
||||||
|
|
|
@ -1324,10 +1324,18 @@ struct message *msg)
|
||||||
|
|
||||||
struct faction *region_get_owner(const struct region *r)
|
struct faction *region_get_owner(const struct region *r)
|
||||||
{
|
{
|
||||||
assert(rule_region_owners());
|
if (r->land) {
|
||||||
if (r->land && r->land->ownership) {
|
if (rule_region_owners()) {
|
||||||
|
if (r->land->ownership) {
|
||||||
return r->land->ownership->owner;
|
return r->land->ownership->owner;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
building *b = largestbuilding(r, cmp_castle_size, false);
|
||||||
|
unit * u = b ? building_owner(b) : NULL;
|
||||||
|
return u ? u->faction : NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include <platform.h>
|
#include <platform.h>
|
||||||
|
|
||||||
#include "region.h"
|
#include "region.h"
|
||||||
|
#include "building.h"
|
||||||
|
#include "unit.h"
|
||||||
#include "terrain.h"
|
#include "terrain.h"
|
||||||
#include "item.h"
|
#include "item.h"
|
||||||
|
|
||||||
|
@ -31,9 +33,32 @@ void test_terraform(CuTest *tc) {
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_region_get_owner(CuTest *tc) {
|
||||||
|
region *r;
|
||||||
|
building *b1, *b2;
|
||||||
|
building_type *bt_castle;
|
||||||
|
unit *u1, *u2;
|
||||||
|
|
||||||
|
test_setup();
|
||||||
|
bt_castle = test_create_buildingtype("castle");
|
||||||
|
bt_castle->protection = building_protection;
|
||||||
|
r = test_create_region(0, 0, 0);
|
||||||
|
b1 = test_create_building(r, bt_castle);
|
||||||
|
b2 = test_create_building(r, bt_castle);
|
||||||
|
b1->size = 5;
|
||||||
|
b2->size = 10;
|
||||||
|
u1 = test_create_unit(test_create_faction(0), r);
|
||||||
|
u_set_building(u1, b1);
|
||||||
|
u2 = test_create_unit(test_create_faction(0), r);
|
||||||
|
u_set_building(u2, b2);
|
||||||
|
CuAssertPtrEquals(tc, u2->faction, region_get_owner(r));
|
||||||
|
test_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
CuSuite *get_region_suite(void)
|
CuSuite *get_region_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
CuSuite *suite = CuSuiteNew();
|
||||||
SUITE_ADD_TEST(suite, test_terraform);
|
SUITE_ADD_TEST(suite, test_terraform);
|
||||||
|
SUITE_ADD_TEST(suite, test_region_get_owner);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue