3 local mod_prefix
= minetest
.get_modpath(minetest
.get_current_modname())
6 function turtle
.coord(x
, y
, z
)
7 return {x
= x
, y
= y
, z
= z
}
10 turtle
.pos1
= turtle
.coord(0, 0, 0)
11 turtle
.pos2
= turtle
.coord(0, 0, 0)
13 local function format_coord(c
)
14 return tostring(c
.x
) .. " " .. tostring(c
.y
) .. " " .. tostring(c
.z
)
17 local function parse_coord(c
)
20 -- can include ~ + - along with num and ,
21 local function parse_relative_coord(c
)
24 function turtle
.ordercoord(c
)
26 return {x
= c
[1], y
= c
[2], z
= c
[3]}
32 -- x or {x,y,z} or {x=x,y=y,z=z}
33 function turtle
.optcoord(x
, y
, z
)
35 return turtle
.coord(x
, y
, z
)
37 return turtle
.ordercoord(x
)
41 -- swap x and y if x > y
42 local function swapg(x
, y
)
50 -- swaps coordinates around such that (matching ords of) c1 < c2 and the overall cuboid is the same shape
51 function turtle
.rectify(c1
, c2
)
52 c1
.x
, c2
.x
= swapg(c1
.x
, c2
.x
)
53 c1
.y
, c2
.y
= swapg(c1
.y
, c2
.y
)
54 c1
.z
, c2
.z
= swapg(c1
.z
, c2
.z
)
58 -- converts a coordinate to a system where 0,0 is the southwestern corner of the world
59 function turtle
.zeroidx(c
)
61 return turtle
.coord(c
.x
+ side
, c
.y
+ side
, c
.z
+ side
)
64 -- swaps coords and subtracts such that c1 == {0, 0, 0} and c2 is the distance from c1
65 -- returns rectified c1/c2 and the relativized version
66 function turtle
.relativize(c1
, c2
)
67 c1
, c2
= turtle
.rectify(c1
, c2
)
69 local c1z
= turtle
.zeroidx(c1
)
70 local c2z
= turtle
.zeroidx(c2
)
72 local rel
= turtle
.coord(c2z
.x
- c1z
.x
, c2z
.y
- c1z
.y
, c2z
.z
- c1z
.z
)
77 function turtle
.cadd(c1
, c2
)
78 return turtle
.coord(c1
.x
+ c2
.x
, c1
.y
+ c2
.y
, c1
.z
+ c2
.z
)
81 function turtle
.relcoord(x
, y
, z
)
82 local pos
= minetest
.localplayer
:get_pos()
83 if pos
.y
> -5000 then pos
.y
=pos
.y
-1 end
84 return turtle
.cadd(pos
, turtle
.optcoord(x
, y
, z
))
87 local function between(x
, y
, z
) -- x is between y and z (inclusive)
88 return y
<= x
and x
<= z
91 function turtle
.getdir() --
92 local rot
= minetest
.localplayer
:get_yaw() % 360
93 if between(rot
, 315, 360) or between(rot
, 0, 45) then
95 elseif between(rot
, 135, 225) then
97 elseif between(rot
, 225, 315) then
99 elseif between(rot
, 45, 135) then
103 function turtle
.setdir(dir
) --
104 if dir
== "north" then
105 minetest
.localplayer
:set_yaw(0)
106 elseif dir
== "south" then
107 minetest
.localplayer
:set_yaw(180)
108 elseif dir
== "east" then
109 minetest
.localplayer
:set_yaw(270)
110 elseif dir
== "west" then
111 minetest
.localplayer
:set_yaw(90)
115 function turtle
.dircoord(f
, y
, r
)
116 local dir
=turtle
.getdir()
117 local coord
= turtle
.optcoord(f
, y
, r
)
121 local lp
=minetest
.localplayer
:get_pos()
122 if dir
== "north" then
123 return turtle
.relcoord(r
, y
, f
)
124 elseif dir
== "south" then
125 return turtle
.relcoord(-r
, y
, -f
)
126 elseif dir
== "east" then
127 return turtle
.relcoord(f
, y
, -r
)
128 elseif dir
== "west" then
129 return turtle
.relcoord(-f
, y
, r
)
132 return turtle
.relcoord(0, 0, 0)