Merge pull request #878 from ennorehling/develop

region_list optimization, list formatting
This commit is contained in:
Enno Rehling 2019-09-28 21:01:53 +02:00 committed by GitHub
commit 8345ee11dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 166 additions and 182 deletions

View File

@ -5291,6 +5291,7 @@
<message name="pest" section="events">
<type>
<arg name="dead" type="int"/>
<arg name="peasants" type="resource"/>
</type>
</message>
<message name="error131" section="errors">

View File

@ -2370,7 +2370,7 @@ msgid "sp_confusion_effect_0"
msgstr "\"$unit($mage) stimmt einen seltsamen Gesang an. Ein plötzlicher Tumult entsteht, der sich jedoch schnell wieder legt.\""
msgid "pest"
msgstr "\"Hier wütete die Pest, und $int($dead) Bauern starben.\""
msgstr "\"Hier wütete die Pest, und $int($dead) $resource($peasants,$dead) $if($eq($dead,1), \"starb\", \"starben\").\""
msgid "wormhole_exit"
msgstr "\"$unit($unit) reist durch ein Wurmloch nach $region($region).\""

View File

@ -2370,7 +2370,7 @@ msgid "sp_confusion_effect_0"
msgstr "\"$unit($mage) intones a mysterious chant. There is a sudden hubbub, but order is restored quickly.\""
msgid "pest"
msgstr "\"The region is visited by the plague and $int($dead) peasants died.\""
msgstr "\"The region is visited by the plague and $int($dead) $resource($peasants,$dead) died.\""
msgid "wormhole_exit"
msgstr "\"$unit($unit) travels through a wormhole to $region($region).\""

View File

@ -571,7 +571,7 @@ msgid "balloon"
msgstr "Ballon"
msgid "nr_schemes_template"
msgstr "Schemen der Regionen {0} sind erkennbar."
msgstr "Schemen von {0} sind erkennbar."
msgid "list_two"
msgstr "{0} und {1}"

View File

@ -15,16 +15,20 @@ end
function test_fetch_astral()
local r = region.create(0, 0, "plain")
local ra = r:get_astral('fog')
local rb = region.create(ra.x + 1, ra.y, 'fog')
local f = faction.create("human")
local u1 = unit.create(f, r, 1)
local u2 = unit.create(f, r, 1)
local u3 = unit.create(f, rb, 1)
rb.plane = ra.plane
u1.magic = "gray"
u1:set_skill("magic", 6)
u1.aura = 0
u1:add_spell("fetch_astral")
u1:clear_orders()
u1:add_order("ZAUBERE Ruf~der~Realitaet " .. itoa36(u2.id))
u1:add_order("ZAUBERE Ruf~der~Realitaet " .. itoa36(u2.id) .. " " .. itoa36(u3.id))
process_orders()
assert_equal(1, f:count_msg_type('missing_components_list'), 'no components')
@ -33,10 +37,11 @@ function test_fetch_astral()
assert_equal(12, u1.aura)
assert_equal(1, f:count_msg_type('spellfail_astralonly'), 'astral space')
u2.region = u2.region:get_astral('fog')
u2.region = ra
process_orders()
assert_equal(0, u1.aura)
assert_equal(u1.region, u2.region)
assert_equal(r, u2.region)
assert_equal(rb, u3.region)
end
function test_pull_astral()

View File

@ -85,3 +85,30 @@ function test_recruit_in_desert()
assert_equal('winter', get_season(get_turn()))
assert_equal(2, u.number)
end
function test_ride_through_mountain()
local r1 = region.create(1, 0, "plain")
local r2 = region.create(2, 0, "mountain")
local r3 = region.create(3, 0, "plain")
local f = faction.create("insect")
local u = unit.create(f, r1, 1)
u.name="Hercules"
u:add_order('NACH O O')
u:add_item('horse', 1)
u:set_skill('riding', 1, true)
process_orders()
assert_equal(r3, u.region)
end
function test_ride_from_mountain()
local r1 = region.create(1, 0, "mountain")
local r2 = region.create(2, 0, "plain")
local r3 = region.create(3, 0, "plain")
local f = faction.create("insect")
local u = unit.create(f, r1, 1)
u:add_order('NACH O O')
u:add_item('horse', 1)
u:set_skill('riding', 1, true)
process_orders()
assert_equal(r2, u.region)
end

View File

@ -1153,7 +1153,8 @@ static void destroy_items(troop dt) {
}
static void calculate_defense_type(troop at, troop dt, int type, bool missile,
const weapon_type **dwtype, int *defskill) {
const weapon_type **dwtype, int *defskill)
{
const weapon *weapon;
weapon = select_weapon(dt, false, true); /* missile=true to get the unmodified best weapon she has */
*defskill = weapon_effskill(dt, at, weapon, false, false);
@ -1253,7 +1254,7 @@ static int apply_magicshield(int reduced_damage, fighter *df,
}
}
}
return reduced_damage;
}
@ -1265,25 +1266,22 @@ terminate(troop dt, troop at, int type, const char *damage_formula, bool missile
unit *au = af->unit;
unit *du = df->unit;
battle *b = df->side->battle;
int armor_value;
const weapon_type *dwtype = NULL;
const weapon_type *awtype = NULL;
const armor_type *armor = NULL;
const armor_type *shield = NULL;
int reduced_damage, attskill = 0, defskill = 0;
bool magic = false;
int damage = dice_rand(damage_formula);
assert(du->number > 0);
++at.fighter->hits;
calculate_attack_type(at, dt, type, missile, &awtype, &attskill, &magic);
calculate_defense_type(at, dt, type, missile, &dwtype, &defskill);
if (is_riding(at) && (awtype == NULL || (fval(awtype, WTF_HORSEBONUS)
&& !fval(awtype, WTF_MISSILE)))) {
damage += CavalryBonus(au, dt, BONUS_DAMAGE);

View File

@ -260,7 +260,7 @@ static int tolua_faction_debug_messages(lua_State * L)
int i = 1;
if (f->msgs) {
mlist *ml;
for (ml = self->msgs->begin; ml; ml = ml->next, ++i) {
for (ml = f->msgs->begin; ml; ml = ml->next, ++i) {
char buf[120];
nr_render(ml->msg, default_locale, buf, sizeof(buf), NULL);
puts(buf);

View File

@ -1,8 +1,11 @@
#pragma once
struct lua_State;
#ifdef __cplusplus
extern "C" {
#endif
struct lua_State;
int tolua_factionlist_next(struct lua_State *L);
void tolua_faction_open(struct lua_State *L);

View File

@ -114,6 +114,14 @@ static int tolua_region_get_plane(lua_State * L)
return 1;
}
static int tolua_region_set_plane(lua_State * L)
{
region *r = (region *)tolua_tousertype(L, 1, NULL);
plane *pl = (plane *)tolua_tousertype(L, 2, NULL);
r->_plane = pl;
return 0;
}
static int tolua_region_get_terrain(lua_State * L)
{
region *self = (region *)tolua_tousertype(L, 1, NULL);
@ -768,7 +776,8 @@ void tolua_region_open(lua_State * L)
tolua_variable(L, TOLUA_CAST "id", tolua_region_get_id, NULL);
tolua_variable(L, TOLUA_CAST "x", tolua_region_get_x, NULL);
tolua_variable(L, TOLUA_CAST "y", tolua_region_get_y, NULL);
tolua_variable(L, TOLUA_CAST "plane", tolua_region_get_plane, NULL);
tolua_variable(L, TOLUA_CAST "plane", tolua_region_get_plane,
tolua_region_set_plane);
tolua_variable(L, TOLUA_CAST "name", tolua_region_get_name,
tolua_region_set_name);
tolua_variable(L, TOLUA_CAST "morale", tolua_region_get_morale,

View File

@ -1464,14 +1464,16 @@ static void cr_output_region(FILE * F, report_context * ctx, region * r)
cr_output_curses_compat(F, f, r, TYP_REGION);
cr_borders(r, f, r->seen.mode, F);
if (r->seen.mode >= seen_unit && is_astral(r)
&& !is_cursed(r->attribs, &ct_astralblock)) {
&& !is_cursed(r->attribs, &ct_astralblock))
{
/* Sonderbehandlung Teleport-Ebene */
region_list *rl = astralregions(r, inhabitable);
region *rl[MAX_SCHEMES];
int num = get_astralregions(r, inhabitable, rl);
if (rl) {
region_list *rl2 = rl;
while (rl2) {
region *r2 = rl2->data;
if (num > 0) {
int i;
for (i = 0; i != num; ++i) {
region *r2 = rl[i];
plane *plx = rplane(r2);
nx = r2->x;
@ -1480,9 +1482,7 @@ static void cr_output_region(FILE * F, report_context * ctx, region * r)
adjust_coordinates(f, &nx, &ny, plx);
fprintf(F, "SCHEMEN %d %d\n", nx, ny);
fprintf(F, "\"%s\";Name\n", rname(r2, f->locale));
rl2 = rl2->next;
}
free_regionlist(rl);
}
}

View File

@ -2181,6 +2181,21 @@ int email_cmd(unit * u, struct order *ord)
return 0;
}
bool password_wellformed(const char *password)
{
unsigned char *c = (unsigned char *)password;
int i;
if (!password || password[0]=='\0') {
return false;
}
for (i = 0; c[i] && i != PASSWORD_MAXSIZE; ++i) {
if (!isalnum(c[i])) {
return false;
}
}
return true;
}
int password_cmd(unit * u, struct order *ord)
{
char pwbuf[PASSWORD_MAXSIZE + 1];
@ -2194,19 +2209,11 @@ int password_cmd(unit * u, struct order *ord)
pwbuf[PASSWORD_MAXSIZE - 1] = '\0';
}
if (s && *s) {
unsigned char *c = (unsigned char *)pwbuf;
int i, r = 0;
for (i = 0; c[i] && i != PASSWORD_MAXSIZE; ++i) {
if (!isalnum(c[i])) {
c[i] = 'X';
++r;
}
}
if (r != 0) {
if (!s || !password_wellformed(s)) {
if (s) {
cmistake(u, ord, 283, MSG_EVENT);
}
password_generate(pwbuf, PASSWORD_MAXSIZE);
}
faction_setpassword(u->faction, password_hash(pwbuf, PASSWORD_DEFAULT));
ADDMSG(&u->faction->msgs, msg_message("changepasswd", "value", pwbuf));

View File

@ -48,6 +48,7 @@ extern "C" {
void sinkships(struct region * r);
void do_enter(struct region *r, bool is_final_attempt);
bool long_order_allowed(const struct unit *u);
bool password_wellformed(const char *password);
int password_cmd(struct unit *u, struct order *ord);
int banner_cmd(struct unit *u, struct order *ord);

View File

@ -49,37 +49,6 @@ static void test_new_building_can_be_renamed(CuTest * tc)
test_teardown();
}
static void test_password_cmd(CuTest * tc)
{
unit *u;
faction * f;
test_setup();
u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0));
u->thisorder = create_order(K_PASSWORD, f->locale, "abcdefgh");
password_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, faction_getpassword(f));
CuAssertTrue(tc, checkpasswd(f, "abcdefgh"));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
free_order(u->thisorder);
u->thisorder = create_order(K_PASSWORD, f->locale, "abc*de*");
password_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error283"));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
CuAssertTrue(tc, !checkpasswd(f, "abc*de*"));
CuAssertTrue(tc, checkpasswd(f, "abcXdeX"));
free_order(u->thisorder);
u->thisorder = create_order(K_PASSWORD, f->locale, "1234567890123456789012345678901234567890");
password_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error321"));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
CuAssertTrue(tc, checkpasswd(f, "1234567890123456789012345678901"));
test_teardown();
}
static void test_rename_building(CuTest * tc)
{
region *r;
@ -1949,6 +1918,60 @@ static void test_long_order_on_ocean(CuTest *tc) {
test_teardown();
}
static void test_password_cmd(CuTest *tc) {
unit *u;
message *msg;
faction * f;
CuAssertTrue(tc, password_wellformed("PASSword"));
CuAssertTrue(tc, password_wellformed("1234567"));
CuAssertTrue(tc, !password_wellformed("$password"));
CuAssertTrue(tc, !password_wellformed("no space"));
test_setup();
mt_create_error(283);
mt_create_error(321);
mt_create_va(mt_new("changepasswd", NULL), "value:string", MT_NEW_END);
u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0));
u->thisorder = create_order(K_PASSWORD, f->locale, "password1234", NULL);
password_cmd(u, u->thisorder);
CuAssertTrue(tc, checkpasswd(f, "password1234"));
CuAssertPtrNotNull(tc, msg = test_find_messagetype(f->msgs, "changepasswd"));
free_order(u->thisorder);
test_clear_messages(f);
u->thisorder = create_order(K_PASSWORD, f->locale, "bad-password", NULL);
password_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error283"));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
CuAssertTrue(tc, !checkpasswd(f, "password1234"));
free_order(u->thisorder);
test_clear_messages(f);
u->thisorder = create_order(K_PASSWORD, f->locale, "''", NULL);
password_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error283"));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
free_order(u->thisorder);
test_clear_messages(f);
u->thisorder = create_order(K_PASSWORD, f->locale, NULL);
password_cmd(u, u->thisorder);
CuAssertTrue(tc, !checkpasswd(f, "password1234"));
CuAssertPtrEquals(tc, NULL, test_find_messagetype(f->msgs, "error283"));
CuAssertPtrNotNull(tc, msg = test_find_messagetype(f->msgs, "changepasswd"));
free_order(u->thisorder);
test_clear_messages(f);
u->thisorder = create_order(K_PASSWORD, f->locale, "1234567890123456789012345678901234567890");
password_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error321"));
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
CuAssertTrue(tc, checkpasswd(f, "1234567890123456789012345678901"));
test_teardown();
}
static void test_peasant_migration(CuTest *tc) {
region *r1, *r2;
int rmax;

View File

@ -587,7 +587,8 @@ void plagues(region * r)
}
if (dead > 0) {
ADDMSG(&r->msgs, msg_message("pest", "dead", dead));
ADDMSG(&r->msgs, msg_message("pest", "peasants dead",
get_resourcetype(R_PEASANT), dead));
deathcounts(r, dead);
rsetpeasants(r, peasants - dead);
}

View File

@ -1062,8 +1062,6 @@ char *report_list(const struct locale *lang, char *buffer, size_t len, int argc,
return format_list(argc, argv, buffer, len, two, start, middle, end);
}
#define MAX_SCHEMES ((TP_RADIUS * 2 + 1) * (TP_RADIUS * 2 + 1) - 4)
static void report_region_schemes(struct stream *out, const region * r, faction * f) {
if (r->seen.mode >= seen_unit && is_astral(r) &&

View File

@ -1852,8 +1852,6 @@ static int sp_treewalkenter(castorder * co)
static int sp_treewalkexit(castorder * co)
{
region *rt;
region_list *rl, *rl2;
int tax, tay;
unit *u, *u2;
int remaining_cap;
int n;
@ -1885,23 +1883,7 @@ static int sp_treewalkexit(castorder * co)
/* Koordinaten setzen und Region loeschen fuer Ueberpruefung auf
* Gueltigkeit */
rt = pa->param[0]->data.r;
tax = rt->x;
tay = rt->y;
rl = astralregions(r, inhabitable);
rt = NULL;
rl2 = rl;
while (rl2) {
if (rl2->data->x == tax && rl2->data->y == tay) {
rt = rl2->data;
break;
}
rl2 = rl2->next;
}
free_regionlist(rl);
if (!rt) {
if (!rt || !inhabitable(rt) || r_standard_to_astral(rt) != r) {
cmistake(caster, co->order, 195, MSG_MAGIC);
return 0;
}
@ -5051,7 +5033,6 @@ int sp_pullastral(castorder * co)
{
region *rt, *ro;
unit *u, *u2;
region_list *rl, *rl2;
int remaining_cap;
int n, w;
region *r = co_get_region(co);
@ -5064,23 +5045,12 @@ int sp_pullastral(castorder * co)
case 1:
rt = r;
ro = pa->param[0]->data.r;
rl = astralregions(r, NULL);
rl2 = rl;
while (rl2 != NULL) {
region *r2 = rl2->data;
if (r2->x == ro->x && r2->y == ro->y) {
ro = r2;
break;
}
rl2 = rl2->next;
}
if (!rl2) {
if (r_astral_to_standard(r) != ro) {
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
"spellfail::nocontact", "target", rt));
free_regionlist(rl);
return 0;
}
free_regionlist(rl);
break;
default:
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
@ -5192,7 +5162,6 @@ int sp_pullastral(castorder * co)
int sp_leaveastral(castorder * co)
{
region *rt, *ro;
region_list *rl, *rl2;
unit *u, *u2;
int remaining_cap;
int n, w;
@ -5204,27 +5173,13 @@ int sp_leaveastral(castorder * co)
switch (getplaneid(r)) {
case 1:
ro = r;
rt = pa->param[0]->data.r;
if (!rt) {
if (!rt || r_standard_to_astral(rt) != r || !inhabitable(rt)) {
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
"spellfail::noway", ""));
return 0;
}
rl = astralregions(r, inhabitable);
rl2 = rl;
while (rl2 != NULL) {
if (rl2->data == rt)
break;
rl2 = rl2->next;
}
if (rl2 == NULL) {
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
"spellfail::noway", ""));
free_regionlist(rl);
return 0;
}
free_regionlist(rl);
ro = r;
break;
default:
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
@ -5328,7 +5283,6 @@ int sp_fetchastral(castorder * co)
spellparameter *pa = co->par;
double power = co->force;
int remaining_cap = (int)((power - 3) * 1500);
region_list *rtl = NULL;
region *rt = co_get_region(co); /* region to which we are fetching */
region *ro = NULL; /* region in which the target is */
@ -5350,21 +5304,13 @@ int sp_fetchastral(castorder * co)
if (u->region != ro) {
/* this can happen several times if the units are from different astral
* regions. Only possible on the intersections of schemes */
region_list *rfind;
if (!is_astral(u->region)) {
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
"spellfail_astralonly", ""));
continue;
}
if (rtl != NULL)
free_regionlist(rtl);
rtl = astralregions(u->region, NULL);
for (rfind = rtl; rfind != NULL; rfind = rfind->next) {
if (rfind->data == mage->region)
break;
}
if (rfind == NULL) {
if (r_standard_to_astral(mage->region) != u->region) {
/* the region r is not in the schemes of rt */
ADDMSG(&mage->faction->msgs, msg_feedback(mage, co->order,
"spellfail_distance", "target", u));
@ -5446,8 +5392,6 @@ int sp_fetchastral(castorder * co)
if (m)
msg_release(m);
}
if (rtl != NULL)
free_regionlist(rtl);
return cast_level;
}
@ -5545,11 +5489,12 @@ int sp_showastral(castorder * co)
/* ------------------------------------------------------------- */
int sp_viewreality(castorder * co)
{
region_list *rl, *rl2;
region *r = co_get_region(co);
unit *mage = co_get_caster(co);
int cast_level = co->level;
message *m;
region *rl[MAX_SCHEMES];
int num;
if (getplaneid(r) != 1) {
/* sprintf(buf, "Dieser Zauber kann nur im Astralraum gezaubert werden."); */
@ -5558,18 +5503,17 @@ int sp_viewreality(castorder * co)
return 0;
}
rl = astralregions(r, NULL);
/* Irgendwann mal auf Curses u/o Attribut umstellen. */
for (rl2 = rl; rl2; rl2 = rl2->next) {
region *rt = rl2->data;
if (!is_cursed(rt->attribs, &ct_astralblock)) {
set_observer(rt, mage->faction, co->level / 2, 2);
num = get_astralregions(r, NULL, rl);
if (num > 0) {
int i;
for (i = 0; i != num; ++i) {
region *rt = rl[i];
if (!is_cursed(rt->attribs, &ct_astralblock)) {
set_observer(rt, mage->faction, co->level / 2, 2);
}
}
}
free_regionlist(rl);
m = msg_message("viewreality_effect", "unit", mage);
r_addmessage(r, mage->faction, m);
msg_release(m);
@ -5620,11 +5564,7 @@ int sp_disruptastral(castorder * co)
continue;
if (r2->units != NULL) {
region_list *trl2;
trl = astralregions(rl2->data, inhabitable);
for (trl2 = trl; trl2; trl2 = trl2->next)
++inhab_regions;
inhab_regions = get_astralregions(r, inhabitable, NULL);
}
/* Nicht-Permanente Tore zerstoeren */

View File

@ -43,37 +43,6 @@ static region *tpregion(const region * r)
return rt;
}
region_list *astralregions(const region * r, bool(*valid) (const region *))
{
region_list *rlist = NULL;
int x, y;
assert(is_astral(r));
if (!is_astral(r)) {
log_error("astralregions was called with a non-astral region.\n");
return NULL;
}
r = r_astral_to_standard(r);
if (r == NULL)
return NULL;
for (x = -TP_RADIUS; x <= +TP_RADIUS; ++x) {
for (y = -TP_RADIUS; y <= +TP_RADIUS; ++y) {
region *rn;
int dist = koor_distance(0, 0, x, y);
int nx = r->x + x, ny = r->y + y;
if (dist > TP_RADIUS)
continue;
pnormalize(&nx, &ny, rplane(r));
rn = findregion(nx, ny);
if (rn != NULL && (valid == NULL || valid(rn)))
add_regionlist(&rlist, rn);
}
}
return rlist;
}
int get_astralregions(const region * r, bool(*valid) (const region *), region *result[])
{
@ -91,7 +60,10 @@ int get_astralregions(const region * r, bool(*valid) (const region *), region *r
pnormalize(&nx, &ny, rplane(r));
rn = findregion(nx, ny);
if (rn != NULL && (valid == NULL || valid(rn))) {
result[num++] = rn;
if (result) {
result[num] = rn;
}
++num;
}
}
}

View File

@ -8,6 +8,7 @@ extern "C" {
#endif
#define TP_RADIUS 2 /* Radius von Schemen */
#define MAX_SCHEMES ((TP_RADIUS * 2 + 1) * (TP_RADIUS * 2 + 1) - 4)
struct region;
struct region_list;
@ -15,8 +16,6 @@ extern "C" {
struct region *r_standard_to_astral(const struct region *r);
struct region *r_astral_to_standard(const struct region *);
struct region_list *astralregions(const struct region *rastral,
bool(*valid) (const struct region *));
struct region_list *all_in_range(const struct region *r, int n,
bool(*valid) (const struct region *));
bool inhabitable(const struct region *r);