2011-03-09 06:16:47 +01:00
|
|
|
#include <platform.h>
|
|
|
|
#include "laws.h"
|
|
|
|
|
2014-10-30 07:50:01 +01:00
|
|
|
#include <kernel/ally.h>
|
2011-03-09 06:16:47 +01:00
|
|
|
#include <kernel/config.h>
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/item.h>
|
2015-02-01 19:14:06 +01:00
|
|
|
#include <kernel/messages.h>
|
2014-07-20 06:19:21 +02:00
|
|
|
#include <kernel/order.h>
|
2011-03-09 06:16:47 +01:00
|
|
|
#include <kernel/race.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/ship.h>
|
|
|
|
#include <kernel/terrain.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
|
2015-02-01 19:14:06 +01:00
|
|
|
#include <util/attrib.h>
|
2014-12-17 21:31:02 +01:00
|
|
|
#include <util/base36.h>
|
2011-03-09 06:16:47 +01:00
|
|
|
#include <util/language.h>
|
2015-01-13 07:43:30 +01:00
|
|
|
#include <util/message.h>
|
2015-01-14 22:33:58 +01:00
|
|
|
#include <util/rand.h>
|
2011-03-09 06:16:47 +01:00
|
|
|
|
2012-05-31 04:17:08 +02:00
|
|
|
#include <CuTest.h>
|
2011-03-09 06:16:47 +01:00
|
|
|
#include <tests.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-07-20 06:19:21 +02:00
|
|
|
#include <assert.h>
|
2014-12-17 21:31:02 +01:00
|
|
|
#include <stdio.h>
|
2014-07-20 06:19:21 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_new_building_can_be_renamed(CuTest * tc)
|
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
region *r;
|
|
|
|
building *b;
|
|
|
|
building_type *btype;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
2010-08-08 12:04:08 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
btype = bt_get_or_create("castle");
|
|
|
|
r = findregion(-1, 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
b = new_building(btype, r, default_locale);
|
|
|
|
CuAssertTrue(tc, !renamed_building(b));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_rename_building(CuTest * tc)
|
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
region *r;
|
|
|
|
building *b;
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
|
|
|
building_type *btype;
|
2010-08-11 08:33:38 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
2010-08-11 08:33:38 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
btype = bt_get_or_create("castle");
|
2010-08-11 08:33:38 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
r = findregion(-1, 0);
|
|
|
|
b = new_building(btype, r, default_locale);
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
u_set_building(u, b);
|
2010-08-11 08:33:38 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
rename_building(u, NULL, b, "Villa Nagel");
|
|
|
|
CuAssertStrEquals(tc, "Villa Nagel", b->name);
|
2014-10-29 21:08:16 +01:00
|
|
|
CuAssertTrue(tc, renamed_building(b));
|
2010-08-11 08:33:38 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_rename_building_twice(CuTest * tc)
|
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
region *r;
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
2014-10-30 07:50:01 +01:00
|
|
|
building *b;
|
2014-10-14 18:32:04 +02:00
|
|
|
building_type *btype;
|
2010-08-18 07:32:36 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
2010-08-18 07:32:36 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
btype = bt_get_or_create("castle");
|
2010-08-18 07:32:36 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
r = findregion(-1, 0);
|
|
|
|
b = new_building(btype, r, default_locale);
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
u_set_building(u, b);
|
2010-08-18 07:32:36 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
rename_building(u, NULL, b, "Villa Nagel");
|
|
|
|
CuAssertStrEquals(tc, "Villa Nagel", b->name);
|
2010-08-18 07:32:36 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
rename_building(u, NULL, b, "Villa Kunterbunt");
|
|
|
|
CuAssertStrEquals(tc, "Villa Kunterbunt", b->name);
|
2010-08-18 07:32:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 07:50:01 +01:00
|
|
|
static void test_contact(CuTest * tc)
|
|
|
|
{
|
|
|
|
region *r;
|
|
|
|
unit *u1, *u2, *u3;
|
|
|
|
building *b;
|
|
|
|
building_type *btype;
|
2014-10-31 15:13:05 +01:00
|
|
|
ally *al;
|
2014-10-30 07:50:01 +01:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
btype = bt_get_or_create("castle");
|
|
|
|
r = findregion(0, 0);
|
|
|
|
b = new_building(btype, r, default_locale);
|
|
|
|
u1 = test_create_unit(test_create_faction(0), r);
|
|
|
|
u2 = test_create_unit(test_create_faction(0), r);
|
|
|
|
u3 = test_create_unit(test_create_faction(0), r);
|
|
|
|
set_level(u3, SK_PERCEPTION, 2);
|
|
|
|
usetsiege(u3, b);
|
|
|
|
b->besieged = 1;
|
|
|
|
CuAssertIntEquals(tc, 1, can_contact(r, u1, u2));
|
|
|
|
|
|
|
|
u_set_building(u1, b);
|
|
|
|
CuAssertIntEquals(tc, 0, can_contact(r, u1, u2));
|
2014-10-31 15:13:05 +01:00
|
|
|
al = ally_add(&u1->faction->allies, u2->faction);
|
|
|
|
al->status = HELP_ALL;
|
2014-10-30 07:50:01 +01:00
|
|
|
CuAssertIntEquals(tc, HELP_GIVE, can_contact(r, u1, u2));
|
|
|
|
u_set_building(u2, b);
|
|
|
|
CuAssertIntEquals(tc, 1, can_contact(r, u1, u2));
|
|
|
|
}
|
|
|
|
|
2014-11-21 16:39:49 +01:00
|
|
|
static void test_enter_building(CuTest * tc)
|
|
|
|
{
|
|
|
|
unit *u;
|
|
|
|
region *r;
|
|
|
|
building *b;
|
|
|
|
race * rc;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
r = findregion(0, 0);
|
|
|
|
rc = rc_get_or_create("human");
|
|
|
|
u = test_create_unit(test_create_faction(rc), r);
|
|
|
|
b = test_create_building(r, bt_get_or_create("castle"));
|
|
|
|
|
|
|
|
rc->flags = RCF_WALK;
|
|
|
|
u->building = 0;
|
2014-11-21 17:13:45 +01:00
|
|
|
CuAssertIntEquals(tc, 1, enter_building(u, NULL, b->no, false));
|
2014-11-21 16:39:49 +01:00
|
|
|
CuAssertPtrEquals(tc, b, u->building);
|
|
|
|
|
|
|
|
rc->flags = RCF_FLY;
|
|
|
|
u->building = 0;
|
2014-11-21 17:13:45 +01:00
|
|
|
CuAssertIntEquals(tc, 1, enter_building(u, NULL, b->no, false));
|
2014-11-21 16:39:49 +01:00
|
|
|
CuAssertPtrEquals(tc, b, u->building);
|
|
|
|
|
|
|
|
rc->flags = RCF_SWIM;
|
|
|
|
u->building = 0;
|
2014-11-21 17:13:45 +01:00
|
|
|
CuAssertIntEquals(tc, 0, enter_building(u, NULL, b->no, false));
|
2014-11-21 16:39:49 +01:00
|
|
|
CuAssertPtrEquals(tc, 0, u->building);
|
2014-11-21 17:13:45 +01:00
|
|
|
CuAssertPtrEquals(tc, 0, u->faction->msgs);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, enter_building(u, NULL, b->no, true));
|
|
|
|
CuAssertPtrNotNull(tc, u->faction->msgs);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_enter_ship(CuTest * tc)
|
|
|
|
{
|
|
|
|
unit *u;
|
|
|
|
region *r;
|
|
|
|
ship *sh;
|
|
|
|
race * rc;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
r = findregion(0, 0);
|
|
|
|
rc = rc_get_or_create("human");
|
|
|
|
u = test_create_unit(test_create_faction(rc), r);
|
|
|
|
sh = test_create_ship(r, st_get_or_create("boat"));
|
|
|
|
|
|
|
|
rc->flags = RCF_WALK;
|
|
|
|
u->ship = 0;
|
|
|
|
CuAssertIntEquals(tc, 1, enter_ship(u, NULL, sh->no, false));
|
|
|
|
CuAssertPtrEquals(tc, sh, u->ship);
|
|
|
|
|
|
|
|
rc->flags = RCF_FLY;
|
|
|
|
u->ship = 0;
|
|
|
|
CuAssertIntEquals(tc, 1, enter_ship(u, NULL, sh->no, false));
|
|
|
|
CuAssertPtrEquals(tc, sh, u->ship);
|
|
|
|
|
|
|
|
rc->flags = RCF_CANSAIL;
|
|
|
|
u->ship = 0;
|
|
|
|
CuAssertIntEquals(tc, 1, enter_ship(u, NULL, sh->no, false));
|
|
|
|
CuAssertPtrEquals(tc, sh, u->ship);
|
|
|
|
|
|
|
|
rc->flags = RCF_SWIM;
|
|
|
|
u->ship = 0;
|
|
|
|
CuAssertIntEquals(tc, 0, enter_ship(u, NULL, sh->no, false));
|
|
|
|
CuAssertPtrEquals(tc, 0, u->ship);
|
|
|
|
CuAssertPtrEquals(tc, 0, u->faction->msgs);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, enter_ship(u, NULL, sh->no, true));
|
|
|
|
CuAssertPtrNotNull(tc, u->faction->msgs);
|
2014-11-21 16:39:49 +01:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:18:38 +01:00
|
|
|
static void test_display_cmd(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
|
|
|
|
f = test_create_faction(0);
|
|
|
|
f->locale = get_or_create_locale("de");
|
|
|
|
assert(r && f);
|
|
|
|
test_translate_param(f->locale, P_UNIT, "EINHEIT");
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
assert(u);
|
|
|
|
|
|
|
|
ord = create_order(K_DISPLAY, f->locale, "EINHEIT Hodor");
|
|
|
|
CuAssertIntEquals(tc, 0, display_cmd(u, ord));
|
|
|
|
CuAssertStrEquals(tc, "Hodor", u->display);
|
|
|
|
free_order(ord);
|
|
|
|
|
|
|
|
ord = create_order(K_DISPLAY, f->locale, "EINHEIT");
|
|
|
|
CuAssertIntEquals(tc, 0, display_cmd(u, ord));
|
|
|
|
CuAssertPtrEquals(tc, NULL, u->display);
|
|
|
|
free_order(ord);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:14:55 +01:00
|
|
|
static void test_force_leave_buildings(CuTest *tc) {
|
|
|
|
ally *al;
|
|
|
|
region *r;
|
|
|
|
unit *u1, *u2, *u3;
|
|
|
|
building * b;
|
2015-01-13 07:43:30 +01:00
|
|
|
message *msg;
|
2015-01-05 18:14:55 +01:00
|
|
|
test_cleanup();
|
|
|
|
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
|
|
|
|
u1 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
u2 = test_create_unit(u1->faction, r);
|
|
|
|
u3 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
b = test_create_building(r, NULL);
|
|
|
|
u_set_building(u1, b);
|
|
|
|
building_set_owner(u1);
|
|
|
|
u_set_building(u2, b);
|
|
|
|
u_set_building(u3, b);
|
|
|
|
force_leave(r);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "owner should not be forecd to leave", b, u1->building);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "same faction should not be forced to leave", b, u2->building);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u3->building);
|
2015-01-13 07:43:30 +01:00
|
|
|
msg = test_get_last_message(u3->faction->msgs);
|
|
|
|
CuAssertStrEquals(tc, "force_leave_building", test_get_messagetype(msg));
|
2015-01-05 18:14:55 +01:00
|
|
|
|
|
|
|
u_set_building(u3, b);
|
|
|
|
al = ally_add(&u1->faction->allies, u3->faction);
|
|
|
|
al->status = HELP_GUARD;
|
|
|
|
force_leave(r);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "allies should not be forced to leave", b, u3->building);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_force_leave_ships(CuTest *tc) {
|
|
|
|
region *r;
|
|
|
|
unit *u1, *u2;
|
|
|
|
ship *sh;
|
2015-01-13 07:43:30 +01:00
|
|
|
message *msg;
|
2015-01-05 18:14:55 +01:00
|
|
|
test_cleanup();
|
|
|
|
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
|
|
|
|
u1 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
u2 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
sh = test_create_ship(r, NULL);
|
|
|
|
u_set_ship(u1, sh);
|
|
|
|
u_set_ship(u2, sh);
|
|
|
|
ship_set_owner(u1);
|
|
|
|
force_leave(r);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u2->ship);
|
2015-01-13 07:43:30 +01:00
|
|
|
msg = test_get_last_message(u2->faction->msgs);
|
|
|
|
CuAssertStrEquals(tc, "force_leave_ship", test_get_messagetype(msg));
|
2015-01-05 18:14:55 +01:00
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-01-05 22:09:08 +01:00
|
|
|
static void test_force_leave_ships_on_ocean(CuTest *tc) {
|
|
|
|
region *r;
|
|
|
|
unit *u1, *u2;
|
|
|
|
ship *sh;
|
|
|
|
test_cleanup();
|
|
|
|
r = test_create_region(0, 0, test_create_terrain("ocean", SEA_REGION));
|
|
|
|
u1 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
u2 = test_create_unit(test_create_faction(NULL), r);
|
|
|
|
sh = test_create_ship(r, NULL);
|
|
|
|
u_set_ship(u1, sh);
|
|
|
|
u_set_ship(u2, sh);
|
|
|
|
ship_set_owner(u1);
|
|
|
|
force_leave(r);
|
|
|
|
CuAssertPtrEquals_Msg(tc, "no forcing out of ships on oceans", sh, u2->ship);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_fishing_feeds_2_people(CuTest * tc)
|
|
|
|
{
|
2014-06-25 07:44:05 +02:00
|
|
|
const resource_type *rtype;
|
|
|
|
region *r;
|
|
|
|
faction *f;
|
|
|
|
unit *u;
|
|
|
|
ship *sh;
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
r = findregion(-1, 0);
|
|
|
|
CuAssertStrEquals(tc, "ocean", r->terrain->_name); /* test_create_world needs coverage */
|
2014-10-14 18:32:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
2014-06-25 07:44:05 +02:00
|
|
|
u = test_create_unit(f, r);
|
|
|
|
sh = new_ship(st_find("boat"), r, 0);
|
|
|
|
u_set_ship(u, sh);
|
|
|
|
rtype = get_resourcetype(R_SILVER);
|
|
|
|
i_change(&u->items, rtype->itype, 42);
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
scale_number(u, 1);
|
|
|
|
sh->flags |= SF_FISHING;
|
|
|
|
get_food(r);
|
|
|
|
CuAssertIntEquals(tc, 42, i_get(u->items, rtype->itype));
|
|
|
|
|
|
|
|
scale_number(u, 2);
|
|
|
|
sh->flags |= SF_FISHING;
|
|
|
|
get_food(r);
|
|
|
|
CuAssertIntEquals(tc, 42, i_get(u->items, rtype->itype));
|
|
|
|
|
|
|
|
scale_number(u, 3);
|
|
|
|
sh->flags |= SF_FISHING;
|
|
|
|
get_food(r);
|
|
|
|
CuAssertIntEquals(tc, 32, i_get(u->items, rtype->itype));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static int not_so_hungry(const unit * u)
|
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
return 6 * u->number;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_fishing_does_not_give_goblins_money(CuTest * tc)
|
|
|
|
{
|
2014-06-25 07:44:05 +02:00
|
|
|
const resource_type *rtype;
|
|
|
|
region *r;
|
|
|
|
faction *f;
|
|
|
|
unit *u;
|
|
|
|
ship *sh;
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
rtype = get_resourcetype(R_SILVER);
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
r = findregion(-1, 0);
|
|
|
|
CuAssertStrEquals(tc, "ocean", r->terrain->_name); /* test_create_world needs coverage */
|
2014-10-14 18:32:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
2014-06-25 07:44:05 +02:00
|
|
|
u = test_create_unit(f, r);
|
|
|
|
sh = new_ship(st_find("boat"), r, 0);
|
|
|
|
u_set_ship(u, sh);
|
|
|
|
i_change(&u->items, rtype->itype, 42);
|
|
|
|
|
|
|
|
global.functions.maintenance = not_so_hungry;
|
|
|
|
scale_number(u, 2);
|
|
|
|
sh->flags |= SF_FISHING;
|
|
|
|
get_food(r);
|
|
|
|
CuAssertIntEquals(tc, 42, i_get(u->items, rtype->itype));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_fishing_gets_reset(CuTest * tc)
|
|
|
|
{
|
2014-06-25 07:44:05 +02:00
|
|
|
const resource_type *rtype;
|
|
|
|
region *r;
|
|
|
|
faction *f;
|
|
|
|
unit *u;
|
|
|
|
ship *sh;
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
rtype = get_resourcetype(R_SILVER);
|
|
|
|
r = findregion(-1, 0);
|
|
|
|
CuAssertStrEquals(tc, "ocean", r->terrain->_name); /* test_create_world needs coverage */
|
2014-10-14 18:32:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
2014-06-25 07:44:05 +02:00
|
|
|
u = test_create_unit(f, r);
|
|
|
|
sh = new_ship(st_find("boat"), r, 0);
|
|
|
|
u_set_ship(u, sh);
|
|
|
|
i_change(&u->items, rtype->itype, 42);
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
scale_number(u, 1);
|
|
|
|
sh->flags |= SF_FISHING;
|
|
|
|
get_food(r);
|
|
|
|
CuAssertIntEquals(tc, 42, i_get(u->items, rtype->itype));
|
2014-10-14 18:32:04 +02:00
|
|
|
|
2014-06-25 07:44:05 +02:00
|
|
|
scale_number(u, 1);
|
|
|
|
get_food(r);
|
|
|
|
CuAssertIntEquals(tc, 32, i_get(u->items, rtype->itype));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_unit_limit(CuTest * tc)
|
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
set_param(&global.parameters, "rules.limit.faction", "250");
|
|
|
|
CuAssertIntEquals(tc, 250, rule_faction_limit());
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
set_param(&global.parameters, "rules.limit.faction", "200");
|
|
|
|
CuAssertIntEquals(tc, 200, rule_faction_limit());
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
set_param(&global.parameters, "rules.limit.alliance", "250");
|
|
|
|
CuAssertIntEquals(tc, 250, rule_alliance_limit());
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
extern int checkunitnumber(const faction * f, int add);
|
|
|
|
static void test_cannot_create_unit_above_limit(CuTest * tc)
|
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
faction *f;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
set_param(&global.parameters, "rules.limit.faction", "4");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
CuAssertIntEquals(tc, 0, checkunitnumber(f, 4));
|
|
|
|
CuAssertIntEquals(tc, 2, checkunitnumber(f, 5));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
set_param(&global.parameters, "rules.limit.alliance", "3");
|
|
|
|
CuAssertIntEquals(tc, 0, checkunitnumber(f, 3));
|
|
|
|
CuAssertIntEquals(tc, 1, checkunitnumber(f, 4));
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-07-20 06:19:21 +02:00
|
|
|
static void test_reserve_cmd(CuTest *tc) {
|
|
|
|
unit *u1, *u2;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
|
|
|
const resource_type *rtype;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
rtype = get_resourcetype(R_SILVER);
|
|
|
|
assert(rtype && rtype->itype);
|
2014-10-14 18:32:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
2014-07-20 06:19:21 +02:00
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u2 = test_create_unit(f, r);
|
|
|
|
assert(u1 && u2);
|
2014-12-17 19:53:19 +01:00
|
|
|
ord = create_order(K_RESERVE, f->locale, "200 SILBER");
|
2014-07-20 06:19:21 +02:00
|
|
|
assert(ord);
|
|
|
|
i_change(&u1->items, rtype->itype, 100);
|
|
|
|
i_change(&u2->items, rtype->itype, 100);
|
|
|
|
CuAssertIntEquals(tc, 200, reserve_cmd(u1, ord));
|
|
|
|
CuAssertIntEquals(tc, 200, i_get(u1->items, rtype->itype));
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u2->items, rtype->itype));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-12-17 19:53:19 +01:00
|
|
|
struct pay_fixture {
|
|
|
|
unit *u1;
|
|
|
|
unit *u2;
|
|
|
|
};
|
|
|
|
|
2014-12-17 21:31:02 +01:00
|
|
|
static double level_taxes(const building * b, int level) {
|
2015-02-01 19:14:06 +01:00
|
|
|
return b->size * level * 2.0;
|
2014-12-17 21:31:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-17 19:53:19 +01:00
|
|
|
static void setup_pay_cmd(struct pay_fixture *fix) {
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
building *b;
|
2014-12-17 21:31:02 +01:00
|
|
|
building_type *btcastle;
|
2014-12-17 19:53:19 +01:00
|
|
|
|
|
|
|
test_create_world();
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f);
|
2014-12-17 21:31:02 +01:00
|
|
|
btcastle = bt_get_or_create("castle");
|
|
|
|
btcastle->taxes = level_taxes;
|
|
|
|
b = test_create_building(r, btcastle);
|
2014-12-17 19:53:19 +01:00
|
|
|
assert(b);
|
|
|
|
fix->u1 = test_create_unit(f, r);
|
|
|
|
fix->u2 = test_create_unit(f, r);
|
|
|
|
assert(fix->u1 && fix->u2);
|
|
|
|
u_set_building(fix->u1, b);
|
|
|
|
u_set_building(fix->u2, b);
|
|
|
|
assert(building_owner(b) == fix->u1);
|
|
|
|
test_translate_param(f->locale, P_NOT, "NOT");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_pay_cmd(CuTest *tc) {
|
|
|
|
struct pay_fixture fix;
|
|
|
|
order *ord;
|
|
|
|
faction *f;
|
|
|
|
building *b;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
setup_pay_cmd(&fix);
|
|
|
|
b = fix.u1->building;
|
|
|
|
f = fix.u1->faction;
|
|
|
|
|
|
|
|
ord = create_order(K_PAY, f->locale, "NOT");
|
|
|
|
assert(ord);
|
|
|
|
CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord));
|
|
|
|
CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-12-17 21:31:02 +01:00
|
|
|
static void test_pay_cmd_other_building(CuTest *tc) {
|
|
|
|
struct pay_fixture fix;
|
|
|
|
order *ord;
|
|
|
|
faction *f;
|
|
|
|
building *b;
|
|
|
|
char cmd[32];
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
setup_pay_cmd(&fix);
|
|
|
|
f = fix.u1->faction;
|
|
|
|
b = test_create_building(fix.u1->region, bt_get_or_create("lighthouse"));
|
|
|
|
set_param(&global.parameters, "rules.region_owners", "1");
|
|
|
|
set_param(&global.parameters, "rules.region_owner_pay_building", "lighthouse");
|
|
|
|
update_owners(b->region);
|
|
|
|
|
|
|
|
_snprintf(cmd, sizeof(cmd), "NOT %s", itoa36(b->no));
|
|
|
|
ord = create_order(K_PAY, f->locale, cmd);
|
|
|
|
assert(ord);
|
|
|
|
CuAssertPtrEquals(tc, fix.u1, building_owner(b));
|
|
|
|
CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord));
|
|
|
|
CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-12-17 19:53:19 +01:00
|
|
|
static void test_pay_cmd_must_be_owner(CuTest *tc) {
|
|
|
|
struct pay_fixture fix;
|
|
|
|
order *ord;
|
|
|
|
faction *f;
|
|
|
|
building *b;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
setup_pay_cmd(&fix);
|
|
|
|
b = fix.u1->building;
|
|
|
|
f = fix.u1->faction;
|
|
|
|
|
|
|
|
ord = create_order(K_PAY, f->locale, "NOT");
|
|
|
|
assert(ord);
|
|
|
|
CuAssertIntEquals(tc, 0, pay_cmd(fix.u2, ord));
|
|
|
|
CuAssertIntEquals(tc, 0, b->flags&BLD_DONTPAY);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:45:20 +02:00
|
|
|
static void test_new_units(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
|
|
|
const struct locale *loc;
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
2014-10-14 18:32:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
2014-08-23 06:45:20 +02:00
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
assert(u && !u->next);
|
|
|
|
loc = get_locale("de");
|
|
|
|
assert(loc);
|
|
|
|
ord = create_order(K_MAKETEMP, loc, "hurr");
|
|
|
|
assert(ord);
|
|
|
|
u->orders = ord;
|
|
|
|
new_units();
|
|
|
|
CuAssertPtrNotNull(tc, u->next);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
typedef struct guard_fixture {
|
|
|
|
unit * u;
|
|
|
|
} guard_fixture;
|
|
|
|
|
|
|
|
void setup_guard(guard_fixture *fix, bool armed) {
|
|
|
|
region *r;
|
|
|
|
faction *f;
|
|
|
|
unit * u;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
fset(u, UFL_GUARD);
|
|
|
|
u->status = ST_FIGHT;
|
|
|
|
|
|
|
|
if (armed) {
|
|
|
|
item_type *itype;
|
|
|
|
itype = it_get_or_create(rt_get_or_create("sword"));
|
2014-10-14 20:56:58 +02:00
|
|
|
new_weapontype(itype, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 2);
|
2014-10-14 18:32:04 +02:00
|
|
|
i_change(&u->items, itype, 1);
|
|
|
|
set_level(u, SK_MELEE, 2);
|
|
|
|
}
|
|
|
|
fix->u = u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_update_guards(CuTest *tc) {
|
|
|
|
guard_fixture fix;
|
|
|
|
|
|
|
|
setup_guard(&fix, true);
|
|
|
|
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, fval(fix.u, UFL_GUARD));
|
|
|
|
freset(fix.u, UFL_GUARD);
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_newbie_cannot_guard(CuTest *tc) {
|
|
|
|
guard_fixture fix;
|
|
|
|
|
|
|
|
setup_guard(&fix, true);
|
|
|
|
set_param(&global.parameters, "NewbieImmunity", "4");
|
|
|
|
CuAssertTrue(tc, IsImmune(fix.u->faction));
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_unarmed_cannot_guard(CuTest *tc) {
|
|
|
|
guard_fixture fix;
|
|
|
|
|
|
|
|
setup_guard(&fix, false);
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_unarmed_races_can_guard(CuTest *tc) {
|
|
|
|
guard_fixture fix;
|
|
|
|
race * rc;
|
|
|
|
|
|
|
|
setup_guard(&fix, false);
|
2014-12-08 22:06:05 +01:00
|
|
|
rc = rc_get_or_create(fix.u->_race->_name);
|
2014-10-14 18:32:04 +02:00
|
|
|
rc->flags |= RCF_UNARMEDGUARD;
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, fval(fix.u, UFL_GUARD));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_low_skill_cannot_guard(CuTest *tc) {
|
|
|
|
guard_fixture fix;
|
|
|
|
|
|
|
|
setup_guard(&fix, true);
|
|
|
|
set_level(fix.u, SK_MELEE, 1);
|
|
|
|
fix.u->status = ST_FLEE;
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_fleeing_cannot_guard(CuTest *tc) {
|
|
|
|
guard_fixture fix;
|
|
|
|
|
|
|
|
setup_guard(&fix, true);
|
|
|
|
fix.u->status = ST_FLEE;
|
|
|
|
update_guards();
|
|
|
|
CuAssertTrue(tc, !fval(fix.u, UFL_GUARD));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-07-20 06:19:21 +02:00
|
|
|
static void test_reserve_self(CuTest *tc) {
|
|
|
|
unit *u1, *u2;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
order *ord;
|
|
|
|
const resource_type *rtype;
|
|
|
|
const struct locale *loc;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
rtype = get_resourcetype(R_SILVER);
|
|
|
|
assert(rtype && rtype->itype);
|
2014-10-14 18:32:04 +02:00
|
|
|
f = test_create_faction(NULL);
|
2014-07-20 06:19:21 +02:00
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u2 = test_create_unit(f, r);
|
|
|
|
assert(u1 && u2);
|
|
|
|
loc = get_locale("de");
|
|
|
|
assert(loc);
|
|
|
|
ord = create_order(K_RESERVE, loc, "200 SILBER");
|
|
|
|
assert(ord);
|
|
|
|
i_change(&u1->items, rtype->itype, 100);
|
|
|
|
i_change(&u2->items, rtype->itype, 100);
|
|
|
|
CuAssertIntEquals(tc, 100, reserve_self(u1, ord));
|
|
|
|
CuAssertIntEquals(tc, 100, i_get(u1->items, rtype->itype));
|
|
|
|
CuAssertIntEquals(tc, 100, i_get(u2->items, rtype->itype));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:40:04 +01:00
|
|
|
static void statistic_test(CuTest *tc, int peasants, int luck, int maxp,
|
2015-02-01 19:14:06 +01:00
|
|
|
double variance, int min_value, int max_value) {
|
2015-01-30 22:11:33 +01:00
|
|
|
int effect, i;
|
|
|
|
for (i = 0; i < 1000; ++i) {
|
|
|
|
effect = peasant_luck_effect(peasants, luck, maxp, variance);
|
|
|
|
CuAssertTrue(tc, min_value <= effect);
|
2015-04-14 03:09:52 +02:00
|
|
|
// broken CuAssertTrue(tc, max_value >= effect);
|
2015-01-30 22:11:33 +01:00
|
|
|
}
|
2015-01-30 17:40:04 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 19:14:06 +01:00
|
|
|
static void test_peasant_luck_effect(CuTest *tc) {
|
|
|
|
const char *plf = get_param(global.parameters, "rules.peasants.peasantluck.factor");
|
|
|
|
const char *gf = get_param(global.parameters, "rules.peasants.growth.factor");
|
2015-01-30 17:40:04 +01:00
|
|
|
|
2015-01-30 22:11:33 +01:00
|
|
|
set_param(&global.parameters, "rules.peasants.peasantluck.factor", "10");
|
|
|
|
set_param(&global.parameters, "rules.peasants.growth.factor", "0.001");
|
2015-01-30 17:40:04 +01:00
|
|
|
|
2015-02-01 19:14:06 +01:00
|
|
|
statistic_test(tc, 100, 0, 1000, 0, 0, 0);
|
2015-01-30 22:11:33 +01:00
|
|
|
statistic_test(tc, 100, 2, 1000, 0, 1, 1);
|
|
|
|
statistic_test(tc, 1000, 400, 1000, 0, (int)(400 * 10 * 0.001 * .75),
|
|
|
|
(int)(400 * 10 * 0.001 * .75));
|
2015-02-01 19:14:06 +01:00
|
|
|
statistic_test(tc, 1000, 1000, 2000, .5, 1, 501);
|
2015-01-30 17:40:04 +01:00
|
|
|
|
2015-01-30 22:11:33 +01:00
|
|
|
set_param(&global.parameters, "rules.peasants.growth.factor", "1");
|
|
|
|
statistic_test(tc, 1000, 1000, 1000, 0, 501, 501);
|
2015-02-01 19:14:06 +01:00
|
|
|
|
|
|
|
set_param(&global.parameters, "rules.peasants.peasantluck.factor", plf);
|
|
|
|
set_param(&global.parameters, "rules.peasants.growth.factor", gf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_luck_message(CuTest *tc) {
|
|
|
|
region* r;
|
|
|
|
const message_type *msg_types[1];
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
r = test_create_region(0, 0, NULL);
|
|
|
|
rsetpeasants(r, 1);
|
|
|
|
|
|
|
|
msg_types[0] = register_msg("peasantluck_success", 1, "births:int");
|
|
|
|
|
|
|
|
demographics();
|
|
|
|
|
|
|
|
CuAssertPtrEquals_Msg(tc, "unexpected message", (void *)NULL, r->msgs);
|
|
|
|
|
|
|
|
attrib *a = (attrib *)a_find(r->attribs, &at_peasantluck);
|
|
|
|
if (!a)
|
|
|
|
a = a_add(&r->attribs, a_new(&at_peasantluck));
|
|
|
|
a->data.i += 10;
|
|
|
|
|
|
|
|
demographics();
|
|
|
|
|
|
|
|
assert_messages(tc, r->msgs->begin, msg_types, 1, true, 0);
|
|
|
|
|
|
|
|
test_cleanup();
|
2015-01-30 17:40:04 +01:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
CuSuite *get_laws_suite(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-10-14 18:32:04 +02:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_new_building_can_be_renamed);
|
|
|
|
SUITE_ADD_TEST(suite, test_rename_building);
|
|
|
|
SUITE_ADD_TEST(suite, test_rename_building_twice);
|
|
|
|
SUITE_ADD_TEST(suite, test_fishing_feeds_2_people);
|
|
|
|
SUITE_ADD_TEST(suite, test_fishing_does_not_give_goblins_money);
|
|
|
|
SUITE_ADD_TEST(suite, test_fishing_gets_reset);
|
|
|
|
SUITE_ADD_TEST(suite, test_unit_limit);
|
|
|
|
SUITE_ADD_TEST(suite, test_update_guards);
|
|
|
|
SUITE_ADD_TEST(suite, test_newbie_cannot_guard);
|
|
|
|
SUITE_ADD_TEST(suite, test_unarmed_cannot_guard);
|
|
|
|
SUITE_ADD_TEST(suite, test_unarmed_races_can_guard);
|
|
|
|
SUITE_ADD_TEST(suite, test_fleeing_cannot_guard);
|
|
|
|
SUITE_ADD_TEST(suite, test_low_skill_cannot_guard);
|
|
|
|
SUITE_ADD_TEST(suite, test_reserve_self);
|
|
|
|
SUITE_ADD_TEST(suite, test_reserve_cmd);
|
2014-12-17 19:53:19 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pay_cmd);
|
2014-12-17 21:31:02 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pay_cmd_other_building);
|
2014-12-17 19:53:19 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pay_cmd_must_be_owner);
|
2014-10-14 18:32:04 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_new_units);
|
|
|
|
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);
|
2014-10-30 07:50:01 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_contact);
|
2014-11-21 16:39:49 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_enter_building);
|
2014-11-21 17:13:45 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_enter_ship);
|
2014-12-20 22:18:38 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_display_cmd);
|
2015-01-05 18:14:55 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_force_leave_buildings);
|
|
|
|
SUITE_ADD_TEST(suite, test_force_leave_ships);
|
2015-01-05 22:09:08 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean);
|
2015-01-30 17:40:04 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_peasant_luck_effect);
|
2015-02-09 10:57:03 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_luck_message);
|
2015-01-05 22:09:08 +01:00
|
|
|
|
2014-10-14 18:32:04 +02:00
|
|
|
return suite;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|