aboutsummaryrefslogtreecommitdiff
path: root/advtrains
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2018-05-28 20:05:19 +0200
committerorwell96 <orwell@bleipb.de>2018-06-14 17:39:42 +0200
commit254c32e304a4f279bf99e460727daf1b78050c47 (patch)
tree106c2b6853adb39674886c3c32da0568c3405a27 /advtrains
parent24b0639c5f057ccb0fccc3c65be923bee1b571db (diff)
downloadadvtrains-254c32e304a4f279bf99e460727daf1b78050c47.tar.gz
advtrains-254c32e304a4f279bf99e460727daf1b78050c47.tar.bz2
advtrains-254c32e304a4f279bf99e460727daf1b78050c47.zip
Fix yaw calculations, track placement orientation and speed up direction lookup functions
Diffstat (limited to 'advtrains')
-rw-r--r--advtrains/debugitems.lua25
-rw-r--r--advtrains/helpers.lua64
-rw-r--r--advtrains/init.lua1
-rw-r--r--advtrains/trackplacer.lua40
-rw-r--r--advtrains/wagons.lua4
5 files changed, 77 insertions, 57 deletions
diff --git a/advtrains/debugitems.lua b/advtrains/debugitems.lua
index dcc95d9..e672308 100644
--- a/advtrains/debugitems.lua
+++ b/advtrains/debugitems.lua
@@ -26,3 +26,28 @@ minetest.register_tool("advtrains:tunnelborer",
end,
}
)
+
+minetest.register_chatcommand("atyaw",
+ {
+ params = "angledeg conn1 conn2",
+ description = "",
+ func = function(name, param)
+ local angledegs, conn1s, conn2s = string.match(param, "^(%S+)%s(%S+)%s(%S+)$")
+ if angledegs and conn1s and conn2s then
+ local angledeg, conn1, conn2 = angledegs+0,conn1s+0,conn2s+0
+ local yaw = angledeg*math.pi/180
+ local yaw1 = advtrains.dir_to_angle(conn1)
+ local yaw2 = advtrains.dir_to_angle(conn2)
+ local adiff1 = advtrains.minAngleDiffRad(yaw, yaw1)
+ local adiff2 = advtrains.minAngleDiffRad(yaw, yaw2)
+
+ atdebug("yaw1",atfloor(yaw1*180/math.pi))
+ atdebug("yaw2",atfloor(yaw2*180/math.pi))
+ atdebug("dif1",atfloor(adiff1*180/math.pi))
+ atdebug("dif2",atfloor(adiff2*180/math.pi))
+
+ minetest.chat_send_all(advtrains.yawToAnyDir(yaw))
+ return true, advtrains.yawToDirection(yaw, conn1, conn2)
+ end
+ end,
+})
diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua
index dc923a5..e060920 100644
--- a/advtrains/helpers.lua
+++ b/advtrains/helpers.lua
@@ -1,42 +1,47 @@
--advtrains by orwell96, see readme.txt
-advtrains.dir_trans_tbl={
- [0]={x=0, z=1},
- [1]={x=1, z=2},
- [2]={x=1, z=1},
- [3]={x=2, z=1},
- [4]={x=1, z=0},
- [5]={x=2, z=-1},
- [6]={x=1, z=-1},
- [7]={x=1, z=-2},
- [8]={x=0, z=-1},
- [9]={x=-1, z=-2},
- [10]={x=-1, z=-1},
- [11]={x=-2, z=-1},
- [12]={x=-1, z=0},
- [13]={x=-2, z=1},
- [14]={x=-1, z=1},
- [15]={x=-1, z=2},
+local dir_trans_tbl={
+ [0]={x=0, z=1, y=0},
+ [1]={x=1, z=2, y=0},
+ [2]={x=1, z=1, y=0},
+ [3]={x=2, z=1, y=0},
+ [4]={x=1, z=0, y=0},
+ [5]={x=2, z=-1, y=0},
+ [6]={x=1, z=-1, y=0},
+ [7]={x=1, z=-2, y=0},
+ [8]={x=0, z=-1, y=0},
+ [9]={x=-1, z=-2, y=0},
+ [10]={x=-1, z=-1, y=0},
+ [11]={x=-2, z=-1, y=0},
+ [12]={x=-1, z=0, y=0},
+ [13]={x=-2, z=1, y=0},
+ [14]={x=-1, z=1, y=0},
+ [15]={x=-1, z=2, y=0},
}
+local dir_angle_tbl={}
+for d,v in pairs(dir_trans_tbl) do
+ local uvec = vector.normalize(v)
+ dir_angle_tbl[d] = math.atan2(-uvec.x, uvec.z)
+end
+
+
+function advtrains.dir_to_angle(dir)
+ return dir_angle_tbl[dir] or error("advtrains: in helpers.lua/dir_to_angle() given dir="..(dir or "nil"))
+end
+
function advtrains.dirCoordSet(coord, dir)
- local x,z
- if advtrains.dir_trans_tbl[dir] then
- x,z=advtrains.dir_trans_tbl[dir].x, advtrains.dir_trans_tbl[dir].z
- else
- error("advtrains: in helpers.lua/dirCoordSet() given dir="..(dir or "nil"))
- end
- return {x=coord.x+x, y=coord.y, z=coord.z+z}
+ return vector.add(coord, advtrains.dirToCoord(dir))
end
advtrains.pos_add_dir = advtrains.dirCoordSet
function advtrains.pos_add_angle(pos, ang)
-- 0 is +Z -> meaning of sin/cos swapped
- return vector.add(pos, {x=math.sin(ang), y=0, z=math.cos(ang)})
+ return vector.add(pos, {x = -math.cos(ang), y = 0, z = math.sin(ang)})
end
function advtrains.dirToCoord(dir)
- return advtrains.dirCoordSet({x=0, y=0, z=0}, dir)
+ return dir_trans_tbl[dir] or error("advtrains: in helpers.lua/dir_to_vector() given dir="..(dir or "nil"))
end
advtrains.dir_to_vector = advtrains.dirToCoord
@@ -86,7 +91,7 @@ function advtrains.yawToAnyDir(yaw)
local min_conn, min_diff=0, 10
for conn, vec in pairs(advtrains.dir_trans_tbl) do
local yaw1 = advtrains.dir_to_angle(conn)
- local diff = advtrains.minAngleDiffRad(yaw, yaw1)
+ local diff = math.abs(advtrains.minAngleDiffRad(yaw, yaw1))
if diff < min_diff then
min_conn = conn
min_diff = diff
@@ -107,11 +112,6 @@ function advtrains.yawToClosestConn(yaw, conns)
return min_connid
end
-function advtrains.dir_to_angle(dir)
- local uvec = vector.normalize(advtrains.dirToCoord(dir))
- return math.atan2(uvec.x, uvec.z)
-end
-
local pi, pi2 = math.pi, 2*math.pi
function advtrains.minAngleDiffRad(r1, r2)
while r1>pi2 do
diff --git a/advtrains/init.lua b/advtrains/init.lua
index 21af66d..d68b188 100644
--- a/advtrains/init.lua
+++ b/advtrains/init.lua
@@ -427,5 +427,6 @@ minetest.register_chatcommand("at_reroute",
end,
})
+
local tot=(os.clock()-lot)*1000
minetest.log("action", "[advtrains] Loaded in "..tot.."ms")
diff --git a/advtrains/trackplacer.lua b/advtrains/trackplacer.lua
index c61bbb4..7a4894e 100644
--- a/advtrains/trackplacer.lua
+++ b/advtrains/trackplacer.lua
@@ -194,32 +194,28 @@ function tp.placetrack(pos, nnpref, placer, itemstack, pointed_thing, yaw)
if k1~=k2 then
local dconn1 = tr.double_conn_1
local dconn2 = tr.double_conn_2
- if not (advtrains.yawToDirection((math.pi/2) - yaw, k1, k2) == k1) then
+ if not (advtrains.yawToDirection(yaw, conn1, conn2) == conn1) then
dconn1 = tr.double_conn_2
dconn2 = tr.double_conn_1
end
+ -- Checks are made this way round so that dconn1 has priority (this will make arrows of atc rails
+ -- point in the right direction)
+ local using
+ if (dconn2[conn1.."_"..conn2]) then
+ using = dconn2[conn1.."_"..conn2]
+ end
if (dconn1[conn1.."_"..conn2]) then
- local using = dconn1[conn1.."_"..conn2]
- tp.bend_rail(p_railpos[conn1], conn1, nnpref)
- tp.bend_rail(p_railpos[conn2], conn2, nnpref)
- advtrains.ndb.swap_node(pos, using)
- local nname=using.name
- if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then
- minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing)
- end
- return
+ using = dconn1[conn1.."_"..conn2]
end
- if (dconn2[conn1.."_"..conn2]) then
- local using = dconn2[conn1.."_"..conn2]
- tp.bend_rail(p_railpos[conn1], conn1, nnpref)
- tp.bend_rail(p_railpos[conn2], conn2, nnpref)
- advtrains.ndb.swap_node(pos, using)
- local nname=using.name
- if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then
- minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing)
- end
- return
+
+ tp.bend_rail(p_railpos[conn1], conn1, nnpref)
+ tp.bend_rail(p_railpos[conn2], conn2, nnpref)
+ advtrains.ndb.swap_node(pos, using)
+ local nname=using.name
+ if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then
+ minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing)
end
+ return
end
end
end
@@ -229,7 +225,7 @@ function tp.placetrack(pos, nnpref, placer, itemstack, pointed_thing, yaw)
for ix, p_rail in ipairs(p_rails) do
local sconn1 = tr.single_conn_1
local sconn2 = tr.single_conn_2
- if not (advtrains.yawToDirection((math.pi/2) - yaw, p_rail, (p_rail+8)%16) == p_rail) then
+ if not (advtrains.yawToDirection(yaw, p_rail, (p_rail+8)%16) == p_rail) then
sconn1 = tr.single_conn_2
sconn2 = tr.single_conn_1
end
@@ -285,7 +281,7 @@ function tp.register_track_placer(nnprefix, imgprefix, dispname)
if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to
and minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable then
-- minetest.chat_send_all(nnprefix)
- local yaw = placer:get_look_horizontal() + (math.pi/2)
+ local yaw = placer:get_look_horizontal()
tp.placetrack(pos, nnprefix, placer, itemstack, pointed_thing, yaw)
if not minetest.settings:get_bool("creative_mode") then
itemstack:take_item()
diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua
index 254e6a8..9ee44b2 100644
--- a/advtrains/wagons.lua
+++ b/advtrains/wagons.lua
@@ -10,8 +10,6 @@
advtrains.wagons = {}
advtrains.wagon_prototypes = {}
--- TODO: That yaw thing is still not fixed. seems like minetest itself obeys the counterclockwise system...
-
--
function advtrains.create_wagon(wtype, owner)
local new_id=advtrains.random_id()
@@ -1092,7 +1090,7 @@ function advtrains.register_wagon(sysname_p, prototype, desc, inv_img, nincreati
return itemstack
end
local tconns=advtrains.get_track_connections(node.name, node.param2)
- local yaw = placer:get_look_horizontal() + (math.pi/2)
+ local yaw = placer:get_look_horizontal()
local plconnid = advtrains.yawToClosestConn(yaw, tconns)
local prevpos = advtrains.get_adjacent_rail(pointed_thing.under, tconns, plconnid, prototype.drives_on)