forked from github/server
make region:set_resource do the right thing
also for adamantium
This commit is contained in:
parent
bdf7411954
commit
17145eaf10
|
@ -0,0 +1,52 @@
|
|||
require "lunit"
|
||||
|
||||
module("tests.e2.adamantium", package.seeall, lunit.testcase )
|
||||
|
||||
function setup()
|
||||
eressea.free_game()
|
||||
eressea.settings.set("nmr.timeout", "0")
|
||||
eressea.settings.set("rules.food.flags", "4")
|
||||
eressea.settings.set("rules.ship.storms", "0")
|
||||
eressea.settings.set("rules.encounters", "0")
|
||||
end
|
||||
|
||||
local function _test_create_adamantium()
|
||||
eressea.settings.set("rules.terraform.all", "1")
|
||||
local r = region.create(0,0, "mountain")
|
||||
local f1 = faction.create("human", "noreply19@eressea.de", "de")
|
||||
local u1 = unit.create(f1, r, 1)
|
||||
r:set_resource("adamantium", 50)
|
||||
assert_equal(50, r:get_resource("adamantium"))
|
||||
return r, u1
|
||||
end
|
||||
|
||||
function test_adamantium1()
|
||||
local r, u1 = _test_create_adamantium()
|
||||
|
||||
u1:add_item("money", 1000)
|
||||
u1:set_skill("mining", 14)
|
||||
u1:clear_orders()
|
||||
u1:add_order("MACHEN Adamantium")
|
||||
|
||||
process_orders()
|
||||
assert_equal(0, u1:get_item("adamantium"))
|
||||
end
|
||||
|
||||
function test_adamantium2()
|
||||
local r, u1 = _test_create_adamantium()
|
||||
|
||||
u1:add_item("money", 1000)
|
||||
u1:set_skill("mining", 15)
|
||||
u1:clear_orders()
|
||||
u1:add_order("MACHEN Adamantium")
|
||||
|
||||
local b = building.create(r, "mine")
|
||||
b.size = 10
|
||||
u1.building = b
|
||||
local adamantium = r:get_resource("adamantium")
|
||||
|
||||
process_orders()
|
||||
assert_equal(1, u1:get_item("adamantium"))
|
||||
assert_equal(adamantium - 1, r:get_resource("adamantium"))
|
||||
end
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
require 'tests.e2.adamantium'
|
||||
require 'tests.e2.undead'
|
||||
require 'tests.e2.shiplanding'
|
||||
require 'tests.e2.e2features'
|
||||
|
|
|
@ -349,7 +349,7 @@ static int tolua_region_get_resourcelevel(lua_State * L)
|
|||
if (rtype != NULL) {
|
||||
const rawmaterial *rm;
|
||||
for (rm = r->resources; rm; rm = rm->next) {
|
||||
if (rm->type->rtype == rtype) {
|
||||
if (rm->rtype == rtype) {
|
||||
lua_pushinteger(L, rm->level);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1085,7 +1085,7 @@ leveled_allocation(const resource_type * rtype, region * r, allocation * alist)
|
|||
avail = MIN(avail, nreq);
|
||||
if (need > 0) {
|
||||
int use = 0;
|
||||
for (al = alist; al; al = al->next)
|
||||
for (al = alist; al; al = al->next) {
|
||||
if (!fval(al, AFL_DONE)) {
|
||||
if (avail > 0) {
|
||||
int want = required(al->want - al->get, al->save);
|
||||
|
@ -1100,9 +1100,13 @@ leveled_allocation(const resource_type * rtype, region * r, allocation * alist)
|
|||
al->get = MIN(al->want, al->get + x * al->save.sa[1] / al->save.sa[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (use) {
|
||||
rawmaterial_type *raw = rmt_get(rm->rtype);
|
||||
if (raw && raw->use) {
|
||||
assert(use <= rm->amount);
|
||||
rm->type->use(rm, r, use);
|
||||
raw->use(rm, r, use);
|
||||
}
|
||||
}
|
||||
assert(avail == 0 || nreq == 0);
|
||||
}
|
||||
|
|
|
@ -383,14 +383,14 @@ static void test_make_item(CuTest *tc) {
|
|||
itype->construction->materials = 0;
|
||||
rtype->flags |= RTF_LIMITED;
|
||||
rmt_create(rtype);
|
||||
add_resource(u->region, 1, 300, 150, rtype);
|
||||
u->region->resources->amount = 300; /* there are 300 stones at level 1 */
|
||||
add_resource(u->region, 1, 300, 150, rtype); /* there are 300 stones at level 1 */
|
||||
CuAssertIntEquals(tc, 300, region_getresource(u->region, rtype));
|
||||
set_level(u, SK_ALCHEMY, 10);
|
||||
|
||||
make_item(u, itype, 10);
|
||||
split_allocations(u->region);
|
||||
CuAssertIntEquals(tc, 11, get_item(u, itype));
|
||||
CuAssertIntEquals(tc, 290, u->region->resources->amount); /* used 10 stones to make 10 stones */
|
||||
CuAssertIntEquals(tc, 290, region_getresource(u->region, rtype)); /* used 10 stones to make 10 stones */
|
||||
|
||||
rtype->modifiers = calloc(2, sizeof(resource_mod));
|
||||
rtype->modifiers[0].flags = RMF_SAVEMATERIAL;
|
||||
|
@ -400,18 +400,18 @@ static void test_make_item(CuTest *tc) {
|
|||
make_item(u, itype, 10);
|
||||
split_allocations(u->region);
|
||||
CuAssertIntEquals(tc, 21, get_item(u, itype));
|
||||
CuAssertIntEquals(tc, 284, u->region->resources->amount); /* 60% saving = 6 stones make 10 stones */
|
||||
CuAssertIntEquals(tc, 284, region_getresource(u->region, rtype)); /* 60% saving = 6 stones make 10 stones */
|
||||
|
||||
make_item(u, itype, 1);
|
||||
split_allocations(u->region);
|
||||
CuAssertIntEquals(tc, 22, get_item(u, itype));
|
||||
CuAssertIntEquals(tc, 283, u->region->resources->amount); /* no free lunches */
|
||||
CuAssertIntEquals(tc, 283, region_getresource(u->region, rtype)); /* no free lunches */
|
||||
|
||||
rtype->modifiers[0].value = frac_make(1, 2);
|
||||
make_item(u, itype, 6);
|
||||
split_allocations(u->region);
|
||||
CuAssertIntEquals(tc, 28, get_item(u, itype));
|
||||
CuAssertIntEquals(tc, 280, u->region->resources->amount); /* 50% saving = 3 stones make 6 stones */
|
||||
CuAssertIntEquals(tc, 280, region_getresource(u->region, rtype)); /* 50% saving = 3 stones make 6 stones */
|
||||
|
||||
rtype->modifiers[0].flags = RMF_REQUIREDBUILDING;
|
||||
rtype->modifiers[0].race = NULL;
|
||||
|
|
|
@ -826,7 +826,7 @@ void region_setresource(region * r, const resource_type * rtype, int value)
|
|||
{
|
||||
rawmaterial *rm = r->resources;
|
||||
while (rm) {
|
||||
if (rm->type->rtype == rtype) {
|
||||
if (rm->rtype == rtype) {
|
||||
rm->amount = value;
|
||||
break;
|
||||
}
|
||||
|
@ -840,15 +840,28 @@ void region_setresource(region * r, const resource_type * rtype, int value)
|
|||
else if (rtype == get_resourcetype(R_HORSE))
|
||||
rsethorses(r, value);
|
||||
else {
|
||||
rawmaterial *rm;
|
||||
if (r->terrain->production) {
|
||||
int i;
|
||||
for (i = 0; r->terrain->production[i].type; ++i) {
|
||||
const terrain_production *production = r->terrain->production + i;
|
||||
if (production->type == rtype) {
|
||||
add_resource(r, 1, value, dice_rand(production->divisor), rtype);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* adamantium etc are not usually terraformed: */
|
||||
for (rm = r->resources; rm; rm = rm->next) {
|
||||
if (rm->rtype == rtype) {
|
||||
rm->amount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!rm) {
|
||||
add_resource(r, 1, value, 150, rtype);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -856,7 +869,7 @@ int region_getresource(const region * r, const resource_type * rtype)
|
|||
{
|
||||
const rawmaterial *rm;
|
||||
for (rm = r->resources; rm; rm = rm->next) {
|
||||
if (rm->type->rtype == rtype) {
|
||||
if (rm->rtype == rtype) {
|
||||
return rm->amount;
|
||||
}
|
||||
}
|
||||
|
@ -1047,8 +1060,8 @@ void terraform_region(region * r, const terrain_type * terrain)
|
|||
if (terrain->production != NULL) {
|
||||
int i;
|
||||
for (i = 0; terrain->production[i].type; ++i) {
|
||||
if (rm->type->rtype == terrain->production[i].type) {
|
||||
rtype = rm->type->rtype;
|
||||
if (rm->rtype == terrain->production[i].type) {
|
||||
rtype = rm->rtype;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <platform.h>
|
||||
|
||||
#include "region.h"
|
||||
#include "resources.h"
|
||||
#include "building.h"
|
||||
#include "unit.h"
|
||||
#include "terrain.h"
|
||||
|
@ -52,10 +53,36 @@ static void test_region_get_owner(CuTest *tc) {
|
|||
test_cleanup();
|
||||
}
|
||||
|
||||
static void test_region_getset_resource(CuTest *tc) {
|
||||
region *r;
|
||||
item_type *itype;
|
||||
|
||||
test_setup();
|
||||
init_resources();
|
||||
itype = test_create_itemtype("iron");
|
||||
itype->construction = calloc(1, sizeof(construction));
|
||||
rmt_create(itype->rtype);
|
||||
r = test_create_region(0, 0, NULL);
|
||||
|
||||
region_setresource(r, itype->rtype, 50);
|
||||
CuAssertIntEquals(tc, 50, region_getresource(r, itype->rtype));
|
||||
|
||||
region_setresource(r, get_resourcetype(R_HORSE), 10);
|
||||
CuAssertIntEquals(tc, 10, region_getresource(r, get_resourcetype(R_HORSE)));
|
||||
CuAssertIntEquals(tc, 10, rhorses(r));
|
||||
|
||||
region_setresource(r, get_resourcetype(R_PEASANT), 10);
|
||||
CuAssertIntEquals(tc, 10, region_getresource(r, get_resourcetype(R_PEASANT)));
|
||||
CuAssertIntEquals(tc, 10, rpeasants(r));
|
||||
|
||||
test_cleanup();
|
||||
}
|
||||
|
||||
CuSuite *get_region_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_terraform);
|
||||
SUITE_ADD_TEST(suite, test_region_getset_resource);
|
||||
SUITE_ADD_TEST(suite, test_region_get_owner);
|
||||
return suite;
|
||||
}
|
||||
|
|
|
@ -36,8 +36,10 @@ void update_resources(region * r)
|
|||
{
|
||||
struct rawmaterial *res = r->resources;
|
||||
while (res) {
|
||||
if (res->type->update)
|
||||
res->type->update(res, r);
|
||||
struct rawmaterial_type *raw = rmt_get(res->rtype);
|
||||
if (raw && raw->update) {
|
||||
raw->update(res, r);
|
||||
}
|
||||
res = res->next;
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +57,7 @@ static void update_resource(struct rawmaterial *res, double modifier)
|
|||
assert(res->amount > 0);
|
||||
}
|
||||
|
||||
void
|
||||
struct rawmaterial *
|
||||
add_resource(region * r, int level, int base, int divisor,
|
||||
const resource_type * rtype)
|
||||
{
|
||||
|
@ -66,12 +68,11 @@ const resource_type * rtype)
|
|||
rm->level = level;
|
||||
rm->startlevel = level;
|
||||
rm->base = base;
|
||||
rm->amount = base;
|
||||
rm->divisor = divisor;
|
||||
rm->flags = 0;
|
||||
rm->type = rmt_get(rtype);
|
||||
assert(rm->type);
|
||||
update_resource(rm, 1.0);
|
||||
rm->type->terraform(rm, r);
|
||||
rm->rtype = rtype;
|
||||
return rm;
|
||||
}
|
||||
|
||||
void terraform_resources(region * r)
|
||||
|
@ -88,7 +89,7 @@ void terraform_resources(region * r)
|
|||
const resource_type *rtype = production->type;
|
||||
|
||||
for (rm = r->resources; rm; rm = rm->next) {
|
||||
if (rm->type->rtype == rtype)
|
||||
if (rm->rtype == rtype)
|
||||
break;
|
||||
}
|
||||
if (rm) {
|
||||
|
@ -96,9 +97,16 @@ void terraform_resources(region * r)
|
|||
}
|
||||
|
||||
if (terraform_all || chance(production->chance)) {
|
||||
add_resource(r, dice_rand(production->startlevel),
|
||||
rawmaterial *rm;
|
||||
rawmaterial_type *raw;
|
||||
rm = add_resource(r, dice_rand(production->startlevel),
|
||||
dice_rand(production->base), dice_rand(production->divisor),
|
||||
production->type);
|
||||
update_resource(rm, 1.0);
|
||||
raw = rmt_get(rm->rtype);
|
||||
if (raw && raw->terraform) {
|
||||
raw->terraform(rm, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -143,7 +151,7 @@ static int visible_default(const rawmaterial * res, int skilllevel)
|
|||
/* resources are visible, if skill equals minimum skill to mine them
|
||||
* plus current level of difficulty */
|
||||
{
|
||||
const struct item_type *itype = res->type->rtype->itype;
|
||||
const struct item_type *itype = res->rtype->itype;
|
||||
if (res->level <= 1
|
||||
&& res->level + itype->construction->minskill <= skilllevel + 1) {
|
||||
assert(res->amount > 0);
|
||||
|
@ -167,7 +175,7 @@ static void use_default(rawmaterial * res, const region * r, int amount)
|
|||
int i;
|
||||
|
||||
for (i = 0; r->terrain->production[i].type; ++i) {
|
||||
if (res->type->rtype == r->terrain->production[i].type)
|
||||
if (res->rtype == r->terrain->production[i].type)
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -179,8 +187,9 @@ static void use_default(rawmaterial * res, const region * r, int amount)
|
|||
struct rawmaterial *rm_get(region * r, const struct resource_type *rtype)
|
||||
{
|
||||
struct rawmaterial *rm = r->resources;
|
||||
while (rm && rm->type->rtype != rtype)
|
||||
while (rm && rm->rtype != rtype) {
|
||||
rm = rm->next;
|
||||
}
|
||||
return rm;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ extern "C" {
|
|||
};
|
||||
|
||||
typedef struct rawmaterial {
|
||||
const struct rawmaterial_type *type;
|
||||
const struct resource_type *rtype;
|
||||
#ifdef LOMEM
|
||||
int amount:16;
|
||||
int level:8;
|
||||
|
@ -68,8 +68,8 @@ extern "C" {
|
|||
struct rawmaterial_type *rmt_find(const char *str);
|
||||
struct rawmaterial_type *rmt_get(const struct resource_type *);
|
||||
|
||||
void add_resource(struct region *r, int level, int base, int divisor,
|
||||
const struct resource_type *rtype);
|
||||
struct rawmaterial *add_resource(struct region *r, int level,
|
||||
int base, int divisor, const struct resource_type *rtype);
|
||||
struct rawmaterial_type *rmt_create(struct resource_type *rtype);
|
||||
|
||||
extern int(*res_limit_fun)(const struct region *, const struct resource_type *);
|
||||
|
|
|
@ -954,11 +954,11 @@ static region *readregion(struct gamedata *data, int x, int y)
|
|||
if (strcmp(name, "end") == 0)
|
||||
break;
|
||||
res = malloc(sizeof(rawmaterial));
|
||||
res->type = rmt_find(name);
|
||||
if (res->type == NULL) {
|
||||
res->rtype = rt_find(name);
|
||||
if (!res->rtype || !res->rtype->raw) {
|
||||
log_error("invalid resourcetype %s in data.", name);
|
||||
}
|
||||
assert(res->type != NULL);
|
||||
assert(res->rtype);
|
||||
READ_INT(data->store, &n);
|
||||
res->level = n;
|
||||
READ_INT(data->store, &n);
|
||||
|
@ -1067,7 +1067,7 @@ void writeregion(struct gamedata *data, const region * r)
|
|||
WRITE_INT(data->store, rhorses(r));
|
||||
|
||||
while (res) {
|
||||
WRITE_TOK(data->store, res->type->rtype->_name);
|
||||
WRITE_TOK(data->store, res->rtype->_name);
|
||||
WRITE_INT(data->store, res->level);
|
||||
WRITE_INT(data->store, res->amount);
|
||||
WRITE_INT(data->store, res->startlevel);
|
||||
|
|
|
@ -81,20 +81,6 @@ const terrain_type *random_terrain(const terrain_type * terrains[],
|
|||
return terrain;
|
||||
}
|
||||
|
||||
int seed_adamantium(region * r, int base)
|
||||
{
|
||||
const resource_type *rtype = rt_find("adamantium");
|
||||
rawmaterial *rm;
|
||||
for (rm = r->resources; rm; rm = rm->next) {
|
||||
if (rm->type->rtype == rtype)
|
||||
break;
|
||||
}
|
||||
if (!rm) {
|
||||
add_resource(r, 1, base, 150, rtype);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int count_demand(const region * r)
|
||||
{
|
||||
struct demand *dmd;
|
||||
|
|
|
@ -95,8 +95,7 @@ static void test_report_region(CuTest *tc) {
|
|||
|
||||
mstream_init(&out);
|
||||
r = test_create_region(0, 0, 0);
|
||||
add_resource(r, 1, 100, 10, rt_stone);
|
||||
r->resources->amount = 135;
|
||||
add_resource(r, 1, 135, 10, rt_stone);
|
||||
CuAssertIntEquals(tc, 1, r->resources->level);
|
||||
r->land->peasants = 5;
|
||||
r->land->horses = 7;
|
||||
|
|
|
@ -448,12 +448,13 @@ const faction * viewer, bool see_unit)
|
|||
rawmaterial *res = r->resources;
|
||||
while (res) {
|
||||
int maxskill = 0;
|
||||
const item_type *itype = resource2item(res->type->rtype);
|
||||
const item_type *itype = resource2item(res->rtype);
|
||||
int minskill = itype->construction->minskill;
|
||||
skill_t skill = itype->construction->skill;
|
||||
int level = res->level + minskill - 1;
|
||||
int visible = -1;
|
||||
if (res->type->visible == NULL) {
|
||||
rawmaterial_type *raw = rmt_get(res->rtype);
|
||||
if (raw->visible == NULL) {
|
||||
visible = res->amount;
|
||||
level = res->level + minskill - 1;
|
||||
}
|
||||
|
@ -464,7 +465,7 @@ const faction * viewer, bool see_unit)
|
|||
int s = effskill(u, skill, 0);
|
||||
if (s > maxskill) {
|
||||
maxskill = s;
|
||||
visible = res->type->visible(res, maxskill);
|
||||
visible = raw->visible(res, maxskill);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -472,7 +473,7 @@ const faction * viewer, bool see_unit)
|
|||
if (level >= 0 && visible >= 0) {
|
||||
if (n >= size)
|
||||
return -1;
|
||||
report_resource(result + n, res->type->rtype, visible, level);
|
||||
report_resource(result + n, res->rtype, visible, level);
|
||||
n++;
|
||||
}
|
||||
res = res->next;
|
||||
|
|
Loading…
Reference in New Issue