2012-06-03 22:39:42 +02:00
|
|
|
#include <platform.h>
|
2014-07-06 08:06:51 +02:00
|
|
|
#include <kernel/config.h>
|
2012-05-27 17:38:17 +02:00
|
|
|
#include "economy.h"
|
|
|
|
|
2014-07-06 05:14:11 +02:00
|
|
|
#include <util/message.h>
|
2015-02-16 20:39:50 +01:00
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/messages.h>
|
|
|
|
#include <kernel/order.h>
|
|
|
|
#include <kernel/pool.h>
|
2014-07-06 05:14:11 +02:00
|
|
|
#include <kernel/race.h>
|
2012-05-27 17:38:17 +02:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/ship.h>
|
2014-07-06 05:14:11 +02:00
|
|
|
#include <kernel/terrain.h>
|
2015-02-16 20:39:50 +01:00
|
|
|
#include <kernel/unit.h>
|
|
|
|
|
|
|
|
#include <util/language.h>
|
2012-05-27 17:38:17 +02:00
|
|
|
|
2012-05-31 04:17:08 +02:00
|
|
|
#include <CuTest.h>
|
2012-05-27 17:38:17 +02:00
|
|
|
#include <tests.h>
|
2015-11-14 15:32:31 +01:00
|
|
|
#include <assert.h>
|
2012-05-27 17:38:17 +02:00
|
|
|
|
|
|
|
static void test_give_control_building(CuTest * tc)
|
|
|
|
{
|
2014-07-06 05:14:11 +02:00
|
|
|
unit *u1, *u2;
|
|
|
|
building *b;
|
|
|
|
struct faction *f;
|
|
|
|
region *r;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
f = test_create_faction(0);
|
2015-10-13 22:31:03 +02:00
|
|
|
r = test_create_region(0, 0, 0);
|
2014-07-06 05:14:11 +02:00
|
|
|
b = test_create_building(r, 0);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u_set_building(u1, b);
|
|
|
|
u2 = test_create_unit(f, r);
|
|
|
|
u_set_building(u2, b);
|
|
|
|
CuAssertPtrEquals(tc, u1, building_owner(b));
|
|
|
|
give_control(u1, u2);
|
|
|
|
CuAssertPtrEquals(tc, u2, building_owner(b));
|
|
|
|
test_cleanup();
|
2012-05-27 17:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_give_control_ship(CuTest * tc)
|
|
|
|
{
|
2014-07-06 05:14:11 +02:00
|
|
|
unit *u1, *u2;
|
|
|
|
ship *sh;
|
|
|
|
struct faction *f;
|
|
|
|
region *r;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
f = test_create_faction(0);
|
2015-10-13 22:31:03 +02:00
|
|
|
r = test_create_region(0, 0, 0);
|
2014-07-06 05:14:11 +02:00
|
|
|
sh = test_create_ship(r, 0);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u_set_ship(u1, sh);
|
|
|
|
u2 = test_create_unit(f, r);
|
|
|
|
u_set_ship(u2, sh);
|
|
|
|
CuAssertPtrEquals(tc, u1, ship_owner(sh));
|
|
|
|
give_control(u1, u2);
|
|
|
|
CuAssertPtrEquals(tc, u2, ship_owner(sh));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-07-06 06:12:34 +02:00
|
|
|
struct steal {
|
2014-07-06 05:14:11 +02:00
|
|
|
struct unit *u;
|
|
|
|
struct region *r;
|
|
|
|
struct faction *f;
|
2014-07-06 06:12:34 +02:00
|
|
|
};
|
2014-07-06 05:14:11 +02:00
|
|
|
|
2014-11-03 08:33:07 +01:00
|
|
|
static void setup_steal(struct steal *env, struct terrain_type *ter, struct race *rc) {
|
2014-07-06 06:12:34 +02:00
|
|
|
env->r = test_create_region(0, 0, ter);
|
|
|
|
env->f = test_create_faction(rc);
|
|
|
|
env->u = test_create_unit(env->f, env->r);
|
2014-07-06 05:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_steal_okay(CuTest * tc) {
|
2014-07-06 06:12:34 +02:00
|
|
|
struct steal env;
|
2014-07-06 05:14:11 +02:00
|
|
|
race *rc;
|
2014-11-03 08:33:07 +01:00
|
|
|
struct terrain_type *ter;
|
2014-07-06 05:14:11 +02:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
ter = test_create_terrain("plain", LAND_REGION);
|
|
|
|
rc = test_create_race("human");
|
|
|
|
rc->flags = 0;
|
2014-07-06 06:12:34 +02:00
|
|
|
setup_steal(&env, ter, rc);
|
|
|
|
CuAssertPtrEquals(tc, 0, check_steal(env.u, 0));
|
2014-07-06 05:14:11 +02:00
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_steal_nosteal(CuTest * tc) {
|
2014-07-06 06:12:34 +02:00
|
|
|
struct steal env;
|
2014-07-06 05:14:11 +02:00
|
|
|
race *rc;
|
|
|
|
terrain_type *ter;
|
|
|
|
message *msg;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
ter = test_create_terrain("plain", LAND_REGION);
|
|
|
|
rc = test_create_race("human");
|
|
|
|
rc->flags = RCF_NOSTEAL;
|
2014-07-06 06:12:34 +02:00
|
|
|
setup_steal(&env, ter, rc);
|
|
|
|
CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0));
|
2014-07-06 05:14:11 +02:00
|
|
|
msg_release(msg);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_steal_ocean(CuTest * tc) {
|
2014-07-06 06:12:34 +02:00
|
|
|
struct steal env;
|
2014-07-06 05:14:11 +02:00
|
|
|
race *rc;
|
|
|
|
terrain_type *ter;
|
|
|
|
message *msg;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
ter = test_create_terrain("ocean", SEA_REGION);
|
|
|
|
rc = test_create_race("human");
|
2014-07-06 06:12:34 +02:00
|
|
|
setup_steal(&env, ter, rc);
|
|
|
|
CuAssertPtrNotNull(tc, msg = check_steal(env.u, 0));
|
2014-07-06 05:14:11 +02:00
|
|
|
msg_release(msg);
|
|
|
|
test_cleanup();
|
2012-05-27 17:38:17 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 20:39:50 +01:00
|
|
|
static struct unit *create_recruiter(void) {
|
|
|
|
region *r;
|
|
|
|
faction *f;
|
|
|
|
unit *u;
|
|
|
|
const resource_type* rtype;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
r=findregion(0, 0);
|
|
|
|
rsetpeasants(r, 999);
|
|
|
|
f = test_create_faction(rc_find("human"));
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
rtype = get_resourcetype(R_SILVER);
|
|
|
|
change_resource(u, rtype, 1000);
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_heroes_dont_recruit(CuTest * tc) {
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
|
|
|
|
u = create_recruiter();
|
|
|
|
fset(u, UFL_HERO);
|
2015-10-13 15:45:38 +02:00
|
|
|
unit_addorder(u, create_order(K_RECRUIT, default_locale, "1"));
|
2015-02-16 20:39:50 +01:00
|
|
|
|
|
|
|
economics(u->region);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 1, u->number);
|
2015-08-17 19:35:07 +02:00
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error_herorecruit"));
|
2015-02-16 20:39:50 +01:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_normals_recruit(CuTest * tc) {
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
|
|
|
|
u = create_recruiter();
|
2015-10-13 15:45:38 +02:00
|
|
|
unit_addorder(u, create_order(K_RECRUIT, default_locale, "1"));
|
2015-02-16 20:39:50 +01:00
|
|
|
|
|
|
|
economics(u->region);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 2, u->number);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:32:31 +01:00
|
|
|
typedef struct request {
|
|
|
|
struct request *next;
|
|
|
|
struct unit *unit;
|
|
|
|
struct order *ord;
|
|
|
|
int qty;
|
|
|
|
int no;
|
|
|
|
union {
|
|
|
|
bool goblin; /* stealing */
|
|
|
|
const struct luxury_type *ltype; /* trading */
|
|
|
|
} type;
|
|
|
|
} request;
|
|
|
|
|
|
|
|
static void test_tax_cmd(CuTest *tc) {
|
|
|
|
order *ord;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
unit *u;
|
|
|
|
item_type *sword, *silver;
|
|
|
|
request *taxorders = 0;
|
|
|
|
|
|
|
|
|
|
|
|
test_cleanup();
|
2015-11-26 18:48:21 +01:00
|
|
|
config_set("taxing.perlevel", "20");
|
2015-11-14 15:32:31 +01:00
|
|
|
test_create_world();
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
|
|
|
|
ord = create_order(K_TAX, f->locale, "");
|
|
|
|
assert(ord);
|
|
|
|
|
|
|
|
tax_cmd(u, ord, &taxorders);
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error48"));
|
|
|
|
test_clear_messages(u->faction);
|
|
|
|
|
|
|
|
silver = get_resourcetype(R_SILVER)->itype;
|
|
|
|
|
|
|
|
sword = it_get_or_create(rt_get_or_create("sword"));
|
|
|
|
new_weapontype(sword, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 1);
|
|
|
|
i_change(&u->items, sword, 1);
|
|
|
|
set_level(u, SK_MELEE, 1);
|
|
|
|
|
|
|
|
tax_cmd(u, ord, &taxorders);
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error_no_tax_skill"));
|
|
|
|
test_clear_messages(u->faction);
|
|
|
|
|
|
|
|
set_level(u, SK_TAXING, 1);
|
|
|
|
tax_cmd(u, ord, &taxorders);
|
|
|
|
CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "error_no_tax_skill"));
|
|
|
|
CuAssertPtrNotNull(tc, taxorders);
|
|
|
|
|
2015-12-07 17:59:50 +01:00
|
|
|
rsetmoney(r, 11);
|
2015-11-14 15:32:31 +01:00
|
|
|
expandtax(r, taxorders);
|
|
|
|
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "income"));
|
2015-11-14 17:14:38 +01:00
|
|
|
/* taxing is in multiples of 10 */
|
|
|
|
CuAssertIntEquals(tc, 10, i_get(u->items, silver));
|
2015-11-14 15:32:31 +01:00
|
|
|
test_clear_messages(u->faction);
|
|
|
|
i_change(&u->items, silver, -i_get(u->items, silver));
|
|
|
|
|
2015-12-07 17:59:50 +01:00
|
|
|
rsetmoney(r, 1000);
|
2015-11-14 15:32:31 +01:00
|
|
|
taxorders = 0;
|
|
|
|
tax_cmd(u, ord, &taxorders);
|
|
|
|
expandtax(r, taxorders);
|
|
|
|
CuAssertIntEquals(tc, 20, i_get(u->items, silver));
|
|
|
|
test_clear_messages(u->faction);
|
|
|
|
|
|
|
|
free_order(ord);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2016-08-21 20:01:30 +02:00
|
|
|
static void test_maintain_buildings(CuTest *tc) {
|
|
|
|
region *r;
|
|
|
|
building *b;
|
|
|
|
building_type *btype;
|
|
|
|
unit *u;
|
|
|
|
maintenance *req;
|
|
|
|
item_type *itype;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
btype = test_create_buildingtype("Hort");
|
|
|
|
btype->maxsize = 10;
|
|
|
|
r = test_create_region(0, 0, 0);
|
|
|
|
u = test_create_unit(test_create_faction(0), r);
|
|
|
|
b = test_create_building(r, btype);
|
|
|
|
itype = test_create_itemtype("money");
|
|
|
|
b->size = btype->maxsize;
|
|
|
|
u_set_building(u, b);
|
|
|
|
|
|
|
|
// this building has no upkeep, it just works:
|
|
|
|
b->flags = 0;
|
2016-08-21 20:04:51 +02:00
|
|
|
maintain_buildings(r);
|
2016-08-21 20:01:30 +02:00
|
|
|
CuAssertIntEquals(tc, BLD_MAINTAINED, fval(b, BLD_MAINTAINED));
|
|
|
|
|
|
|
|
req = calloc(2, sizeof(maintenance));
|
|
|
|
req[0].number = 100;
|
|
|
|
req[0].rtype = itype->rtype;
|
|
|
|
btype->maintenance = req;
|
|
|
|
|
|
|
|
// we cannot afford to pay:
|
|
|
|
b->flags = 0;
|
2016-08-21 20:04:51 +02:00
|
|
|
maintain_buildings(r);
|
2016-08-21 20:01:30 +02:00
|
|
|
CuAssertIntEquals(tc, 0, fval(b, BLD_MAINTAINED));
|
|
|
|
|
|
|
|
// we can afford to pay:
|
|
|
|
i_change(&u->items, itype, 100);
|
|
|
|
b->flags = 0;
|
2016-08-21 20:04:51 +02:00
|
|
|
maintain_buildings(r);
|
2016-08-21 20:01:30 +02:00
|
|
|
CuAssertIntEquals(tc, BLD_MAINTAINED, fval(b, BLD_MAINTAINED));
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u->items, itype));
|
|
|
|
|
|
|
|
// this building has no owner, it doesn't work:
|
|
|
|
u_set_building(u, NULL);
|
|
|
|
b->flags = 0;
|
2016-08-21 20:04:51 +02:00
|
|
|
maintain_buildings(r);
|
2016-08-21 20:01:30 +02:00
|
|
|
CuAssertIntEquals(tc, 0, fval(b, BLD_MAINTAINED));
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2012-05-27 17:38:17 +02:00
|
|
|
CuSuite *get_economy_suite(void)
|
|
|
|
{
|
2014-07-06 05:14:11 +02:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_give_control_building);
|
|
|
|
SUITE_ADD_TEST(suite, test_give_control_ship);
|
|
|
|
SUITE_ADD_TEST(suite, test_steal_okay);
|
|
|
|
SUITE_ADD_TEST(suite, test_steal_ocean);
|
|
|
|
SUITE_ADD_TEST(suite, test_steal_nosteal);
|
2015-02-16 20:39:50 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_normals_recruit);
|
|
|
|
SUITE_ADD_TEST(suite, test_heroes_dont_recruit);
|
2015-11-14 15:32:31 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_tax_cmd);
|
2016-08-21 20:01:30 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_maintain_buildings);
|
2014-07-06 05:14:11 +02:00
|
|
|
return suite;
|
2012-05-27 17:38:17 +02:00
|
|
|
}
|