Add lua tests for ROUTE.

This commit is contained in:
Enno Rehling 2017-12-10 20:54:02 +01:00
parent 686bbdbf69
commit 2ca236e68c
4 changed files with 53 additions and 9 deletions

View file

@ -1112,3 +1112,25 @@ function test_build_castle()
assert_equal(1, u.building.size) assert_equal(1, u.building.size)
assert_equal(u.building.name, "Burg") assert_equal(u.building.name, "Burg")
end end
function test_route()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE O W P")
process_orders()
assert_equal("ROUTE West PAUSE Ost", u:get_order(0))
assert_equal(r2, u.region)
end
function test_route_pause()
local r1 = region.create(0, 0, "plain")
local r2 = region.create(1, 0, "plain")
local f = faction.create("human", "route@example.com")
local u = unit.create(f, r1, 1)
u:add_order("ROUTE P O W")
process_orders()
assert_equal("ROUTE P O W", u:get_order(0))
assert_equal(r1, u.region)
end

View file

@ -718,6 +718,30 @@ static int tolua_unit_set_region(lua_State * L)
return 0; return 0;
} }
static int tolua_unit_get_order(lua_State * L)
{
unit *self = (unit *)tolua_tousertype(L, 1, 0);
int index = (int)tolua_tonumber(L, 2, -1);
order *ord = NULL;
if (index < 0) {
ord = self->thisorder;
}
else {
int i;
ord = self->orders;
for (i = 0; ord && i != index; ++i) {
ord = ord->next;
}
}
if (ord) {
char buffer[1024];
get_command(ord, self->faction->locale, buffer, sizeof(buffer));
lua_pushstring(L, buffer);
return 1;
}
return 0;
}
static int tolua_unit_add_order(lua_State * L) static int tolua_unit_add_order(lua_State * L)
{ {
unit *self = (unit *)tolua_tousertype(L, 1, 0); unit *self = (unit *)tolua_tousertype(L, 1, 0);
@ -972,6 +996,7 @@ void tolua_unit_open(lua_State * L)
tolua_variable(L, TOLUA_CAST "weight", tolua_unit_get_weight, 0); tolua_variable(L, TOLUA_CAST "weight", tolua_unit_get_weight, 0);
tolua_variable(L, TOLUA_CAST "capacity", tolua_unit_get_capacity, 0); tolua_variable(L, TOLUA_CAST "capacity", tolua_unit_get_capacity, 0);
tolua_function(L, TOLUA_CAST "get_order", tolua_unit_get_order);
tolua_function(L, TOLUA_CAST "add_order", tolua_unit_add_order); tolua_function(L, TOLUA_CAST "add_order", tolua_unit_add_order);
tolua_function(L, TOLUA_CAST "clear_orders", tolua_unit_clear_orders); tolua_function(L, TOLUA_CAST "clear_orders", tolua_unit_clear_orders);
tolua_function(L, TOLUA_CAST "get_curse", tolua_unit_get_curse); tolua_function(L, TOLUA_CAST "get_curse", tolua_unit_get_curse);

View file

@ -64,7 +64,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/bsdstring.h>
#include <util/event.h> #include <util/event.h>
#include <util/goodies.h> #include <util/goodies.h>
#include <util/language.h> #include <util/language.h>
@ -248,6 +247,7 @@ void add_recruits(unit * u, int number, int wanted)
if (number > 0) { if (number > 0) {
unit *unew; unit *unew;
char equipment[64]; char equipment[64];
int len;
if (u->number == 0) { if (u->number == 0) {
set_number(u, number); set_number(u, number);
@ -258,10 +258,10 @@ void add_recruits(unit * u, int number, int wanted)
unew = create_unit(r, u->faction, number, u_race(u), 0, NULL, u); unew = create_unit(r, u->faction, number, u_race(u), 0, NULL, u);
} }
strlcpy(equipment, "new_", sizeof(equipment)); len = snprintf(equipment, sizeof(equipment), "new_%s", u_race(u)->_name);
strlcat(equipment, u_race(u)->_name, sizeof(equipment)); if (len > 0 && (size_t)len < sizeof(equipment)) {
equip_unit(unew, get_equipment(equipment)); equip_unit(unew, get_equipment(equipment));
}
if (unew != u) { if (unew != u) {
transfermen(unew, u, unew->number); transfermen(unew, u, unew->number);
remove_unit(&r->units, unew); remove_unit(&r->units, unew);

View file

@ -33,7 +33,6 @@
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/bsdstring.h>
#include <util/goodies.h> #include <util/goodies.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
@ -166,9 +165,7 @@ newfaction *read_newfactions(const char *filename)
break; break;
} }
if (password[0] == '\0') { if (password[0] == '\0') {
size_t sz; snprintf(password, sizeof(password), "%s%s", itoa36(rng_int()), itoa36(rng_int()));
sz = strlcpy(password, itoa36(rng_int()), sizeof(password));
sz += strlcat(password, itoa36(rng_int()), sizeof(password));
} }
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
if (strcmp(faction_getemail(f), email) == 0 && f->age < MINAGE_MULTI) { if (strcmp(faction_getemail(f), email) == 0 && f->age < MINAGE_MULTI) {