Merge pull request #873 from ennorehling/develop

mapper enhancement
This commit is contained in:
Enno Rehling 2019-09-02 20:59:41 +02:00 committed by GitHub
commit 99f7ddd1c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 6 deletions

View file

@ -580,12 +580,49 @@ static void reset_region(region *r) {
}
}
static void reset_cursor(state *st) {
static region * state_region(state *st) {
int nx = st->cursor.x;
int ny = st->cursor.y;
region *r;
pnormalize(&nx, &ny, st->cursor.pl);
if ((r = findregion(nx, ny)) != NULL) {
return findregion(nx, ny);
}
static void reset_area_cb(void *arg) {
region *r = (region *)arg;
r->age = 0;
freset(r, RF_MARK);
}
static void reset_area(state *st) {
region * r = state_region(st);
if (r != NULL) {
selist * ql = NULL;
int qi = 0, qlen = 0;
fset(r, RF_MARK);
selist_insert(&ql, qlen++, r);
while (qi != qlen) {
int i;
region *adj[MAXDIRECTIONS];
r = selist_get(ql, qi++);
get_neighbours(r, adj);
for (i = 0; i != MAXDIRECTIONS; ++i) {
region *rn = adj[i];
if (rn && !fval(rn, RF_MARK)) {
if ((rn->terrain->flags & FORBIDDEN_REGION) == 0) {
fset(rn, RF_MARK);
selist_insert(&ql, qlen++, rn);
}
}
}
}
selist_foreach(ql, reset_area_cb);
selist_free(ql);
}
}
static void reset_cursor(state *st) {
region * r = state_region(st);
if (r != NULL) {
reset_region(r);
}
}
@ -1072,6 +1109,16 @@ static void seed_player(state *st, const newfaction *player) {
}
}
static bool confirm(WINDOW * win, const char *q) {
int ch;
werase(win);
mvwaddstr(win, 0, 0, (char *)q);
wmove(win, 0, (int)(strlen(q) + 1));
ch = wgetch(win);
return (ch == 'y') || (ch == 'Y');
}
static void handlekey(state * st, int c)
{
window *wnd;
@ -1157,6 +1204,13 @@ static void handlekey(state * st, int c)
st->wnd_status->update |= 1;
st->wnd_map->update |= 1;
break;
case 'A': /* clear/reset area */
if (confirm(st->wnd_status->handle, "Are you sure you want to reset this entire area?")) {
reset_area(st);
st->modified = 1;
st->wnd_map->update |= 1;
}
break;
case 'c': /* clear/reset */
reset_cursor(st);
st->modified = 1;

View file

@ -1,4 +1,4 @@
#include <platform.h>
#include <kernel/ally.h>
#include <kernel/config.h>
#include <kernel/curse.h>
#include <kernel/item.h>
@ -652,6 +652,62 @@ static void test_get_modifier(CuTest *tc) {
test_teardown();
}
static void test_gift_items(CuTest *tc) {
unit *u, *u1, *u2;
region *r;
const resource_type *rtype;
test_setup();
init_resources();
r = test_create_plain(0, 0);
u = test_create_unit(test_create_faction(NULL), r);
rtype = get_resourcetype(R_SILVER);
region_setresource(r, rtype, 0);
i_change(&u->items, rtype->itype, 10);
gift_items(u, GIFT_FRIENDS | GIFT_PEASANTS | GIFT_SELF);
CuAssertIntEquals(tc, 10, region_getresource(r, rtype));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
region_setresource(r, get_resourcetype(R_HORSE), 0);
region_setresource(r, rtype, 0);
i_change(&u->items, rtype->itype, 10);
i_change(&u->items, get_resourcetype(R_HORSE)->itype, 20);
u1 = test_create_unit(test_create_faction(NULL), r);
u2 = test_create_unit(u1->faction, r);
gift_items(u, GIFT_FRIENDS | GIFT_PEASANTS | GIFT_SELF);
CuAssertIntEquals(tc, 20, region_getresource(r, get_resourcetype(R_HORSE)));
CuAssertIntEquals(tc, 10, region_getresource(r, rtype));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
CuAssertIntEquals(tc, 0, i_get(u1->items, rtype->itype));
CuAssertIntEquals(tc, 0, i_get(u2->items, rtype->itype));
region_setresource(r, rtype, 0);
i_change(&u->items, rtype->itype, 10);
ally_set(&u->faction->allies, u1->faction, HELP_MONEY);
ally_set(&u1->faction->allies, u->faction, HELP_GIVE);
gift_items(u, GIFT_FRIENDS | GIFT_PEASANTS | GIFT_SELF);
CuAssertIntEquals(tc, 0, region_getresource(r, rtype));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
CuAssertIntEquals(tc, 10, i_get(u1->items, rtype->itype));
CuAssertIntEquals(tc, 0, i_get(u2->items, rtype->itype));
i_change(&u1->items, rtype->itype, -10);
set_number(u1, 2);
u_setfaction(u2, test_create_faction(NULL));
ally_set(&u->faction->allies, u2->faction, HELP_MONEY);
ally_set(&u2->faction->allies, u->faction, HELP_GIVE);
region_setresource(r, rtype, 0);
i_change(&u->items, rtype->itype, 15);
ally_set(&u->faction->allies, u1->faction, HELP_MONEY);
ally_set(&u1->faction->allies, u->faction, HELP_GIVE);
gift_items(u, GIFT_FRIENDS | GIFT_PEASANTS | GIFT_SELF);
CuAssertIntEquals(tc, 0, region_getresource(r, rtype));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
CuAssertIntEquals(tc, 10, i_get(u1->items, rtype->itype));
CuAssertIntEquals(tc, 5, i_get(u2->items, rtype->itype));
test_teardown();
}
CuSuite *get_unit_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -683,5 +739,6 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_name_unit);
SUITE_ADD_TEST(suite, test_heal_factor);
SUITE_ADD_TEST(suite, test_get_modifier);
SUITE_ADD_TEST(suite, test_gift_items);
return suite;
}

View file

@ -40,8 +40,10 @@
static bool good_region(const region * r)
{
return (!fval(r, RF_CHAOTIC) && r->age > 30 && rplane(r) == NULL
&& r->units != NULL && r->land != NULL);
if (fval(r, RF_CHAOTIC) || r->age <= 100 || !r->units || !r->land || rplane(r)) {
return false;
}
return true;
}
static int cmp_age(const void *v1, const void *v2)