From 2426e5d7f5a3a1108e5d2bf98fad83a6b8c6d705 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 24 Jan 2010 19:56:45 +0000 Subject: [PATCH] http://bugs.eressea.de/view.php?id=1697 http://bugs.eressea.de/view.php?id=1703 - coordinate transform repairs --- src/common/kernel/plane.c | 23 +++++++++++++++---- src/eressea/tolua/bind_faction.c | 39 +++++++++++++++++++++++++++++--- src/scripts/tests/common.lua | 39 +++++++++++++++++++++++++++++++- src/scripts/tests/e3a.lua | 5 +--- 4 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/common/kernel/plane.c b/src/common/kernel/plane.c index d7152d345..8483a14dc 100644 --- a/src/common/kernel/plane.c +++ b/src/common/kernel/plane.c @@ -193,19 +193,34 @@ plane_center_y(const plane *pl) void adjust_coordinates(const faction *f, int *x, int *y, const plane * pl, const region * r) { - int nx = *x - plane_center_x(pl); - int ny = *y - plane_center_y(pl); + int nx = *x; + int ny = *y; if (f) { + nx -= ursprung_x(f, pl, r); + ny -= ursprung_y(f, pl, r); + } + if (pl) { + nx -= plane_center_x(pl); + ny -= plane_center_y(pl); + } + + if (pl) { int width = plane_width(pl); int height = plane_height(pl); int width_2 = width/2; int height_2 = height/2; - nx -= ursprung_x(f, pl, r); + if (nx<0) nx = (width-(-nx)%width); if (nx>width_2) nx -= width; - ny -= ursprung_y(f, pl, r); + if (ny<0) ny = (height-(-ny)%height); if (ny>height_2) ny -= height; } + + assert(!pl || nx<=pl->maxx - plane_center_x(pl)); + assert(!pl || nx>=pl->minx - plane_center_x(pl)); + assert(!pl || ny<=pl->maxy - plane_center_y(pl)); + assert(!pl || ny>=pl->miny - plane_center_y(pl)); + *x = nx; *y = ny; } diff --git a/src/eressea/tolua/bind_faction.c b/src/eressea/tolua/bind_faction.c index c9080d763..5bb69b59b 100644 --- a/src/eressea/tolua/bind_faction.c +++ b/src/eressea/tolua/bind_faction.c @@ -20,7 +20,9 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include +#include #include @@ -251,6 +253,35 @@ tolua_faction_set_policy(lua_State* L) return 0; } +static int +tolua_faction_normalize(lua_State* L) +{ + faction * f = (faction *)tolua_tousertype(L, 1, 0); + region * r = (region * )tolua_tousertype(L, 2, 0); + if (r) { + plane * pl = rplane(r); + int nx = r->x, ny = r->y; + pnormalize(&nx, &ny, pl); + adjust_coordinates(f, &nx, &ny, pl, r); + tolua_pushnumber(L, (lua_Number)nx); + tolua_pushnumber(L, (lua_Number)ny); + return 2; + } + return 0; +} + +static int +tolua_faction_set_origin(lua_State* L) +{ + faction * f = (faction *)tolua_tousertype(L, 1, 0); + region * r = (region *)tolua_tousertype(L, 2, 0); + plane * pl = rplane(r); + int id = pl?pl->id:0; + + set_ursprung(f, id, r->x - plane_center_x(pl), r->y - plane_center_y(pl)); + return 0; +} + static int tolua_faction_get_origin(lua_State* L) { @@ -498,9 +529,11 @@ tolua_faction_open(lua_State* L) tolua_variable(L, TOLUA_CAST "flags", tolua_faction_get_flags, NULL); tolua_variable(L, TOLUA_CAST "lastturn", tolua_faction_get_lastturn, tolua_faction_set_lastturn); - tolua_function(L, TOLUA_CAST "set_policy", tolua_faction_set_policy); - tolua_function(L, TOLUA_CAST "get_policy", tolua_faction_get_policy); - tolua_function(L, TOLUA_CAST "get_origin", tolua_faction_get_origin); + tolua_function(L, TOLUA_CAST "set_policy", &tolua_faction_set_policy); + tolua_function(L, TOLUA_CAST "get_policy", &tolua_faction_get_policy); + tolua_function(L, TOLUA_CAST "get_origin", &tolua_faction_get_origin); + tolua_function(L, TOLUA_CAST "set_origin", &tolua_faction_set_origin); + tolua_function(L, TOLUA_CAST "normalize", &tolua_faction_normalize); tolua_function(L, TOLUA_CAST "add_item", tolua_faction_add_item); tolua_variable(L, TOLUA_CAST "items", tolua_faction_get_items, NULL); diff --git a/src/scripts/tests/common.lua b/src/scripts/tests/common.lua index eaa0c0e14..a258e8e7c 100644 --- a/src/scripts/tests/common.lua +++ b/src/scripts/tests/common.lua @@ -1,5 +1,9 @@ require "lunit" +function setup() + free_game() +end + function one_unit(r, f) local u = unit.create(f, r, 1) u:add_item("money", u.number * 100) @@ -45,7 +49,6 @@ function test_fleeing_units_can_be_transported() end function test_plane() - free_game() local pl = plane.create(0, -3, -3, 7, 7) local nx, ny = plane.normalize(pl, 4, 4) assert_equal(nx, -3, "normalization failed") @@ -507,6 +510,40 @@ function test_mallorn() assert(u3:get_item("mallorn")==1) end +function test_coordinate_translation() + local pl = plane.create(1, 500, 500, 1001, 1001) -- astralraum + local pe = plane.create(1, -8761, 3620, 23, 23) -- eternath + local r = region.create(1000, 1000, "plain") + local f = faction.create("noreply@eressea.de", "human", "de") + assert_not_equal(nil, r) + assert_equal(r.x, 1000) + assert_equal(r.y, 1000) + local nx, ny = plane.normalize(pl, r.x, r.y) + assert_equal(nx, 1000) + assert_equal(ny, 1000) + local r1 = region.create(500, 500, "plain") + f:set_origin(r1) + nx, ny = f:normalize(r1) + assert_equal(0, nx) + assert_equal(0, ny) + local r0 = region.create(0, 0, "plain") + nx, ny = f:normalize(r0) + assert_equal(0, nx) + assert_equal(0, ny) + nx, ny = f:normalize(r) + assert_equal(500, nx) + assert_equal(500, ny) + local rn = region.create(1010, 1010, "plain") + nx, ny = f:normalize(rn) + assert_equal(-491, nx) + assert_equal(-491, ny) + + local re = region.create(-8760, 3541, "plain") -- eternath + nx, ny = f:normalize(rn) + assert_equal(-491, nx) + assert_equal(-491, ny) +end + function test_control() free_game() local u1, u2 = two_units(region.create(0, 0, "plain"), two_factions()) diff --git a/src/scripts/tests/e3a.lua b/src/scripts/tests/e3a.lua index 79dee9d85..c6404c758 100644 --- a/src/scripts/tests/e3a.lua +++ b/src/scripts/tests/e3a.lua @@ -219,7 +219,7 @@ function test_market() end end -function jest_market_gives_items() +function test_market_gives_items() free_game() local r for x = -1, 1 do for y = -1, 1 do @@ -326,7 +326,6 @@ function test_alliance() end function test_morale() - free_game() local r = region.create(0, 0, "plain") assert_equal(1, r.morale) local f1 = faction.create("noreply@eressea.de", "human", "de") @@ -355,7 +354,6 @@ function test_morale() end function test_canoe_passes_through_land() - free_game() local f = faction.create("noreply@eressea.de", "human", "de") local src = region.create(0, 0, "ocean") local land = region.create(1, 0, "plain") @@ -376,7 +374,6 @@ function test_canoe_passes_through_land() end function test_give_only_a_third_of_items() - free_game() local u1, u2 = two_units(region.create(0, 0, "plain"), two_factions()) local r = u2.region u1.faction.age = 10