Merge branch 'master' into develop

This commit is contained in:
Enno Rehling 2018-11-04 09:10:54 +01:00
commit dac6ef660d
14 changed files with 352 additions and 59 deletions

View File

@ -1058,6 +1058,21 @@ function test_give_silver()
assert_equal(10, u:get_item("money"))
end
function test_build_castle_one_stage()
local r = region.create(0, 0, 'plain')
local f = faction.create('human')
local u = unit.create(f, r, 2)
u:add_item('stone', 4)
u:set_skill('building', 1)
u:add_order('MACHE BURG')
process_orders()
assert_equal(2, u.building.size)
assert_equal(2, u:get_item('stone'))
end
function test_build_castle()
local r = region.create(0, 0, "plain")
local f = create_faction('human')

View File

@ -29,6 +29,21 @@ function test_castle_names()
assert_equal("citadel", b:get_typename(6250))
end
function test_build_castle_one_stage()
local r = region.create(0, 0, 'plain')
local f = faction.create('human')
local u = unit.create(f, r, 2)
u:add_item('stone', 4)
u:set_skill('building', 1)
u:add_order('MACHE BURG')
process_orders()
assert_equal(2, u.building.size)
assert_equal(2, u:get_item('stone'))
end
function test_build_castle_stages()
local r = region.create(0,0, "plain")
local f = faction.create("human")

View File

@ -28,15 +28,17 @@ static int cmp_scholars(const void *lhs, const void *rhs)
return (int)a->sk - (int)b->sk;
}
int autostudy_init(scholar scholars[], int max_scholars, region *r)
int autostudy_init(scholar scholars[], int max_scholars, unit **units)
{
unit *u;
unit *unext = NULL, *u = *units;
faction *f = u->faction;
int nscholars = 0;
for (u = r->units; u; u = u->next) {
while (u) {
keyword_t kwd = init_order(u->thisorder, u->faction->locale);
if (kwd == K_AUTOSTUDY) {
if (long_order_allowed(u)) {
if (f == u->faction) {
scholar * st = scholars + nscholars;
skill_t sk = getskill(u->faction->locale);
if (check_student(u, u->thisorder, sk)) {
@ -49,8 +51,15 @@ int autostudy_init(scholar scholars[], int max_scholars, region *r)
}
}
}
else if (!unext) {
unext = u;
}
}
}
u = u->next;
}
*units = unext;
scholars[nscholars].u = NULL;
if (nscholars > 0) {
qsort(scholars, nscholars, sizeof(scholar), cmp_scholars);
}
@ -145,15 +154,23 @@ void autostudy_run(scholar scholars[], int nscholars)
}
}
#define MAXSCHOLARS 128
#define MAXSCHOLARS 512
void do_autostudy(region *r)
{
static int max_scholars;
unit *units = r->units;
scholar scholars[MAXSCHOLARS];
int i, nscholars = autostudy_init(scholars, MAXSCHOLARS, r);
while (units) {
int i, nscholars = autostudy_init(scholars, MAXSCHOLARS, &units);
if (nscholars > max_scholars) {
stats_count("automate.max_scholars", nscholars - max_scholars);
max_scholars = nscholars;
}
autostudy_run(scholars, nscholars);
for (i = 0; i != nscholars; ++i) {
int days = STUDYDAYS * scholars[i].learn;
learn_skill(scholars[i].u, scholars[i].sk, days);
}
}
}

View File

@ -37,7 +37,7 @@ typedef struct scholar {
void do_autostudy(struct region *r);
int autostudy_init(scholar scholars[], int max_scholars, struct region *r);
int autostudy_init(scholar scholars[], int max_scholars, struct unit **units);
void autostudy_run(scholar scholars[], int nscholars);
#endif

View File

@ -17,7 +17,7 @@
static void test_autostudy_init(CuTest *tc) {
scholar scholars[4];
unit *u1, *u2, *u3, *u4;
unit *u1, *u2, *u3, *u4, *u5, *ulist;
faction *f;
region *r;
@ -35,10 +35,13 @@ static void test_autostudy_init(CuTest *tc) {
set_level(u2, SK_ENTERTAINMENT, 2);
u3 = test_create_unit(f, r);
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
u4 = test_create_unit(test_create_faction(NULL), r);
u4 = test_create_unit(f, r);
u4->thisorder = create_order(K_AUTOSTUDY, f->locale, "Dudelidu");
u5 = test_create_unit(test_create_faction(NULL), r);
u5->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
scholars[3].u = NULL;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r));
ulist = r->units;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, &ulist));
CuAssertPtrNotNull(tc, test_find_messagetype(u4->faction->msgs, "error77"));
CuAssertPtrEquals(tc, u2, scholars[0].u);
CuAssertIntEquals(tc, 2, scholars[0].level);
@ -53,12 +56,20 @@ static void test_autostudy_init(CuTest *tc) {
CuAssertIntEquals(tc, 0, scholars[2].learn);
CuAssertIntEquals(tc, SK_PERCEPTION, scholars[2].sk);
CuAssertPtrEquals(tc, NULL, scholars[3].u);
CuAssertPtrEquals(tc, u5, ulist);
CuAssertIntEquals(tc, 1, autostudy_init(scholars, 4, &ulist));
CuAssertPtrEquals(tc, u5, scholars[0].u);
CuAssertIntEquals(tc, 0, scholars[0].level);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, SK_PERCEPTION, scholars[0].sk);
CuAssertPtrEquals(tc, NULL, scholars[1].u);
CuAssertPtrEquals(tc, NULL, ulist);
test_teardown();
}
static void test_autostudy_run(CuTest *tc) {
scholar scholars[4];
unit *u1, *u2, *u3;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
@ -76,7 +87,9 @@ static void test_autostudy_run(CuTest *tc) {
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
set_number(u3, 15);
scholars[3].u = NULL;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r));
ulist = r->units;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, &ulist));
CuAssertPtrEquals(tc, NULL, ulist);
autostudy_run(scholars, 3);
CuAssertIntEquals(tc, 0, scholars[0].learn);
CuAssertIntEquals(tc, 20, scholars[1].learn);
@ -86,7 +99,7 @@ static void test_autostudy_run(CuTest *tc) {
static void test_autostudy_run_noteachers(CuTest *tc) {
scholar scholars[4];
unit *u1, *u2, *u3;
unit *u1, *u2, *u3, *ulist;
faction *f;
region *r;
@ -104,7 +117,9 @@ static void test_autostudy_run_noteachers(CuTest *tc) {
u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]);
set_number(u3, 15);
scholars[3].u = NULL;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, r));
ulist = r->units;
CuAssertIntEquals(tc, 3, autostudy_init(scholars, 4, &ulist));
CuAssertPtrEquals(tc, NULL, ulist);
autostudy_run(scholars, 3);
CuAssertIntEquals(tc, 2, scholars[0].learn);
CuAssertIntEquals(tc, 10, scholars[1].learn);

View File

@ -35,6 +35,7 @@
#include <util/lists.h>
#include <util/macros.h>
#include "util/path.h"
#include "util/rand.h"
#include "util/rng.h"
#include "util/unicode.h"
@ -43,6 +44,10 @@
#include "listbox.h"
#include "teleport.h"
#include <selist.h>
#include <storage.h>
#include <lua.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
@ -437,7 +442,10 @@ static void paint_info_region(window * wnd, const state * st)
line++;
umvwprintw(win, line++, 1, "%s, age %d", r->terrain->_name, r->age);
if (r->land) {
int iron = region_getresource_level(r, get_resourcetype(R_IRON));
int stone = region_getresource_level(r, get_resourcetype(R_STONE));
mvwprintw(win, line++, 1, "$:%6d P:%5d", rmoney(r), rpeasants(r));
mvwprintw(win, line++, 1, "S:%6d I:%5d", stone, iron);
mvwprintw(win, line++, 1, "H:%6d %s:%5d", rhorses(r),
(r->flags & RF_MALLORN) ? "M" : "T",
r->land->trees[1] + r->land->trees[2]);
@ -515,6 +523,33 @@ static void statusline(WINDOW * win, const char *str)
wnoutrefresh(win);
}
static void reset_resources(region *r, const struct terrain_type *terrain)
{
int i;
for (i = 0; terrain->production[i].type; ++i) {
rawmaterial *rm;
const terrain_production *production = terrain->production + i;
const resource_type *rtype = production->type;
for (rm = r->resources; rm; rm = rm->next) {
if (rm->rtype == rtype)
break;
}
if (rm) {
struct rawmaterial_type *rmt;
set_resource(rm,
dice_rand(production->startlevel),
dice_rand(production->base),
dice_rand(production->divisor));
rmt = rmt_get(rtype);
if (rmt && rmt->terraform) {
rmt->terraform(rm, r);
}
}
}
}
static void reset_region(region *r) {
unit **up = &r->units;
bool players = false;
@ -540,6 +575,7 @@ static void reset_region(region *r) {
}
if (r->land) {
init_region(r);
reset_resources(r, r->terrain);
}
}
}
@ -585,6 +621,69 @@ static void terraform_at(coordinate * c, const terrain_type * terrain)
}
}
static void selection_walk(selection * selected, void(*callback)(region *, void *), void *udata) {
int i;
for (i = 0; i != MAXTHASH; ++i) {
tag **tp = &selected->tags[i];
while (*tp) {
region *r;
tag *t = *tp;
int nx = t->coord.x, ny = t->coord.y;
plane *pl = t->coord.pl;
pnormalize(&nx, &ny, pl);
r = findregion(nx, ny);
if (r != NULL) {
callback(r, udata);
}
tp = &t->nexthash;
}
}
}
static void reset_levels_cb(region *r, void *udata) {
struct rawmaterial *res;
UNUSED_ARG(udata);
for (res = r->resources; res; res = res->next) {
if (res->level > 3) {
res->level = 1;
}
}
}
/**
* BUG 2506: reset drained mountains to level 1
*/
static void
fix_selection(selection * selected)
{
selection_walk(selected, reset_levels_cb, NULL);
}
static void
reset_selection(selection * selected)
{
int i;
for (i = 0; i != MAXTHASH; ++i) {
tag **tp = &selected->tags[i];
while (*tp) {
region *r;
tag *t = *tp;
int nx = t->coord.x, ny = t->coord.y;
plane *pl = t->coord.pl;
pnormalize(&nx, &ny, pl);
r = findregion(nx, ny);
if (r != NULL) {
reset_region(r);
}
tp = &t->nexthash;
}
}
}
static void
terraform_selection(selection * selected, const terrain_type * terrain)
{
@ -694,7 +793,7 @@ void highlight_region(region * r, int toggle)
}
}
void select_coordinate(struct selection *selected, int nx, int ny, int toggle)
void select_coordinate(struct selection *selected, int nx, int ny, bool toggle)
{
if (toggle)
tag_region(selected, nx, ny);
@ -702,7 +801,50 @@ void select_coordinate(struct selection *selected, int nx, int ny, int toggle)
untag_region(selected, nx, ny);
}
enum { MODE_MARK, MODE_SELECT, MODE_UNMARK, MODE_UNSELECT };
enum select_t { MODE_MARK, MODE_SELECT, MODE_UNMARK, MODE_UNSELECT };
static void select_island(state *st, int selectmode)
{
region *r;
int nx = st->cursor.x;
int ny = st->cursor.y;
pnormalize(&nx, &ny, st->cursor.pl);
r = findregion(nx, ny);
if (r && r->land) {
selist *ql, *stack = NULL;
int qi = 0;
selist_push(&stack, r);
for (ql = stack, qi = 0; ql; selist_advance(&ql, &qi, 1)) {
region *r = (region *)selist_get(ql, qi);
region *rnext[MAXDIRECTIONS];
int i;
fset(r, RF_MARK);
if (selectmode & MODE_SELECT) {
select_coordinate(st->selected, r->x, r->y,
selectmode == MODE_SELECT);
}
else {
highlight_region(r, selectmode == MODE_MARK);
}
get_neighbours(r, rnext);
for (i = 0; i != MAXDIRECTIONS; ++i) {
region *rn = rnext[i];
if (rn && rn->land && !fval(rn, RF_MARK)) {
selist_push(&stack, rn);
}
}
}
for (ql = stack, qi = 0; ql; selist_advance(&ql, &qi, 1)) {
region *r = (region *)selist_get(ql, qi);
freset(r, RF_MARK);
}
selist_free(stack);
}
}
static void select_regions(state * st, int selectmode)
{
@ -856,6 +998,11 @@ static void select_regions(state * st, int selectmode)
}
}
}
else if (findmode == 'i') {
sprintf(sbuffer, "%swand: ", status);
statusline(st->wnd_status->handle, sbuffer);
select_island(st, selectmode);
}
else if (findmode == 't') {
const struct terrain_type *terrain;
sprintf(sbuffer, "%sterrain: ", status);
@ -1186,7 +1333,12 @@ static void handlekey(state * st, int c)
statusline(st->wnd_status->handle, "tag-");
doupdate();
switch (getch()) {
case 'r':
reset_selection(st->selected);
break;
case 'f':
fix_selection(st->selected);
break;
case 't':
terraform_selection(st->selected, select_terrain(st, NULL));
st->modified = 1;

View File

@ -29,7 +29,7 @@ extern "C" {
const char *prompt);
void highlight_region(struct region *r, int on);
void select_coordinate(struct selection *selected, int x, int y, int on);
void select_coordinate(struct selection *selected, int x, int y, bool on);
void run_mapper(void);
extern int force_color;

View File

@ -527,28 +527,19 @@ int build_skill(unit *u, int basesk, int skill_mod) {
* of the first object have already been finished. return the
* actual size that could be built.
*/
int build(unit * u, const construction * ctype, int completed, int want, int skill_mod)
{
const construction *con = ctype;
int skills = INT_MAX; /* number of skill points remainig */
int basesk = 0;
static int build_limited(unit * u, const construction * con, int completed, int want, int basesk, int *skill_total) {
int skills = *skill_total;
int made = 0;
if (want <= 0)
if (want <= 0) {
return 0;
}
if (con == NULL) {
return ENOMATERIALS;
}
if (completed == con->maxsize) {
return ECOMPLETE;
}
if (con->skill != NOSKILL) {
basesk = effskill(u, con->skill, 0);
if (basesk == 0)
return ENEEDSKILL;
skills = build_skill(u, basesk, skill_mod);
}
for (; want > 0 && skills > 0;) {
int err, n;
@ -615,9 +606,27 @@ int build(unit * u, const construction * ctype, int completed, int want, int ski
want -= n;
completed = completed + n;
}
/* Nur soviel PRODUCEEXP wie auch tatsaechlich gemacht wurde */
produceexp(u, ctype->skill, (made < u->number) ? made : u->number);
*skill_total = skills;
return made;
}
int build(unit * u, const construction * con, int completed, int want, int skill_mod)
{
int skills = INT_MAX; /* number of skill points remainig */
int made, basesk = 0;
assert(con->skill != NOSKILL);
basesk = effskill(u, con->skill, 0);
if (basesk == 0) {
return ENEEDSKILL;
}
skills = build_skill(u, basesk, skill_mod);
made = build_limited(u, con, completed, want, basesk, &skills);
/* Nur soviel PRODUCEEXP wie auch tatsaechlich gemacht wurde */
if (made > 0) {
produceexp(u, con->skill, (made < u->number) ? made : u->number);
}
return made;
}
@ -686,7 +695,7 @@ static int build_failure(unit *u, order *ord, const building_type *btype, int wa
return err;
}
static int build_stages(unit *u, const building_type *btype, int built, int n) {
static int build_stages(unit *u, const building_type *btype, int built, int n, int basesk, int *skill_total) {
const building_stage *stage;
int made = 0;
@ -706,7 +715,7 @@ static int build_stages(unit *u, const building_type *btype, int built, int n) {
want = todo;
}
}
err = build(u, con, built, want, 0);
err = build_limited(u, con, built, want, basesk, skill_total);
if (err < 0) {
if (made == 0) {
/* could not make any part at all */
@ -744,10 +753,14 @@ build_building(unit * u, const building_type * btype, int id, int want, order *
const char *btname;
order *new_order = NULL;
const struct locale *lang = u->faction->locale;
int skills, basesk; /* number of skill points remainig */
assert(u->number);
assert(btype->stages && btype->stages->construction);
if (effskill(u, SK_BUILDING, 0) == 0) {
basesk = effskill(u, SK_BUILDING, 0);
skills = build_skill(u, basesk, 0);
if (skills == 0) {
cmistake(u, ord, 101, MSG_PRODUCE);
return 0;
}
@ -832,7 +845,7 @@ build_building(unit * u, const building_type * btype, int id, int want, order *
}
}
built = build_stages(u, btype, built, n);
built = build_stages(u, btype, built, n, basesk, &skills);
if (built < 0) {
return build_failure(u, ord, btype, want, built);

View File

@ -22,8 +22,9 @@
#include <CuTest.h>
#include <tests.h>
#include <stdlib.h>
#include <assert.h>
#include <stdlib.h>
#include <limits.h>
typedef struct build_fixture {
faction *f;
@ -57,6 +58,53 @@ static unit * setup_build(build_fixture *bf) {
return bf->u;
}
static building_type *setup_castle(item_type *it_stone) {
building_type *btype;
building_stage *stage;
construction * cons;
btype = test_create_buildingtype("castle");
stage = btype->stages = calloc(1, sizeof(building_stage));
cons = stage->construction = calloc(1, sizeof(construction));
cons->materials = calloc(2, sizeof(requirement));
cons->materials[0].number = 1;
cons->materials[0].rtype = it_stone->rtype;
cons->minskill = 1;
cons->maxsize = 2;
cons->reqsize = 1;
cons->skill = SK_BUILDING;
stage = stage->next = calloc(1, sizeof(building_stage));
cons = stage->construction = calloc(1, sizeof(construction));
cons->materials = calloc(2, sizeof(requirement));
cons->materials[0].number = 1;
cons->materials[0].rtype = it_stone->rtype;
cons->minskill = 1;
cons->maxsize = 8;
cons->reqsize = 1;
cons->skill = SK_BUILDING;
return btype;
}
static void test_build_building_stages(CuTest *tc) {
building_type *btype;
item_type *it_stone;
unit *u;
test_setup();
init_resources();
it_stone = test_create_itemtype("stone");
btype = setup_castle(it_stone);
u = test_create_unit(test_create_faction(NULL), test_create_plain(0, 0));
set_level(u, SK_BUILDING, 2);
i_change(&u->items, it_stone, 4);
build_building(u, btype, -1, INT_MAX, NULL);
CuAssertPtrNotNull(tc, u->building);
CuAssertIntEquals(tc, 2, u->building->size);
CuAssertIntEquals(tc, 2, i_get(u->items, it_stone));
test_teardown();
}
static void teardown_build(build_fixture *bf) {
free(bf->cons.materials);
test_teardown();
@ -395,6 +443,7 @@ CuSuite *get_build_suite(void)
SUITE_ADD_TEST(suite, test_build_with_ring);
SUITE_ADD_TEST(suite, test_build_with_potion);
SUITE_ADD_TEST(suite, test_build_building_success);
SUITE_ADD_TEST(suite, test_build_building_stages);
SUITE_ADD_TEST(suite, test_build_building_with_golem);
SUITE_ADD_TEST(suite, test_build_building_no_materials);
SUITE_ADD_TEST(suite, test_build_destroy_cmd);

View File

@ -829,7 +829,7 @@ void free_land(land_region * lr)
free(lr);
}
void region_setresource(region * r, const resource_type * rtype, int value)
void region_setresource(region * r, const struct resource_type *rtype, int value)
{
rawmaterial *rm = r->resources;
while (rm) {
@ -872,7 +872,18 @@ void region_setresource(region * r, const resource_type * rtype, int value)
}
}
int region_getresource(const region * r, const resource_type * rtype)
int region_getresource_level(const region * r, const struct resource_type * rtype)
{
const rawmaterial *rm;
for (rm = r->resources; rm; rm = rm->next) {
if (rm->rtype == rtype) {
return rm->level;
}
}
return -1;
}
int region_getresource(const region * r, const struct resource_type *rtype)
{
const rawmaterial *rm;
for (rm = r->resources; rm; rm = rm->next) {

View File

@ -263,6 +263,8 @@ extern "C" {
void region_setname(struct region *self, const char *name);
const char *region_getinfo(const struct region *self);
void region_setinfo(struct region *self, const char *name);
int region_getresource_level(const struct region * r,
const struct resource_type * rtype);
int region_getresource(const struct region *r,
const struct resource_type *rtype);
void region_setresource(struct region *r, const struct resource_type *rtype,

View File

@ -46,8 +46,6 @@ void update_resources(region * r)
}
}
extern int dice_rand(const char *s);
static void update_resource(struct rawmaterial *res, double modifier)
{
double amount = (res->level - res->startlevel) / 100.0 * res->divisor + 1;
@ -59,6 +57,15 @@ static void update_resource(struct rawmaterial *res, double modifier)
assert(res->amount > 0);
}
void set_resource(struct rawmaterial *rm, int level, int base, int divisor)
{
rm->level = level;
rm->startlevel = level;
rm->base = base;
rm->amount = base;
rm->divisor = divisor;
}
struct rawmaterial *
add_resource(region * r, int level, int base, int divisor,
const resource_type * rtype)
@ -67,13 +74,9 @@ const resource_type * rtype)
rm->next = r->resources;
r->resources = rm;
rm->level = level;
rm->startlevel = level;
rm->base = base;
rm->amount = base;
rm->divisor = divisor;
rm->flags = 0;
rm->rtype = rtype;
set_resource(rm, level, base, divisor);
return rm;
}

View File

@ -70,6 +70,7 @@ extern "C" {
const struct resource_type *);
struct rawmaterial_type *rmt_get(const struct resource_type *);
void set_resource(struct rawmaterial *rm, int level, int base, int divisor);
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);

View File

@ -26,7 +26,7 @@ extern "C" {
#endif
typedef struct skill {
skill_t id : 8;
int id : 8;
int level : 8;
int weeks : 8;
int old : 8;