add wallTool
[df_scaffold.git] / tlib.lua
blob9855d3792ab90e04f6904b17cfd7be51a812b19a
1 turtle = {}
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}
8 end
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)
15 end
17 local function parse_coord(c)
18 end
20 -- can include ~ + - along with num and ,
21 local function parse_relative_coord(c)
22 end
24 function turtle.ordercoord(c)
25 if c.x == nil then
26 return {x = c[1], y = c[2], z = c[3]}
27 else
28 return c
29 end
30 end
32 -- x or {x,y,z} or {x=x,y=y,z=z}
33 function turtle.optcoord(x, y, z)
34 if y and z then
35 return turtle.coord(x, y, z)
36 else
37 return turtle.ordercoord(x)
38 end
39 end
41 -- swap x and y if x > y
42 local function swapg(x, y)
43 if x > y then
44 return y, x
45 else
46 return x, y
47 end
48 end
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)
55 return c1, c2
56 end
58 -- converts a coordinate to a system where 0,0 is the southwestern corner of the world
59 function turtle.zeroidx(c)
60 local side = 30912
61 return turtle.coord(c.x + side, c.y + side, c.z + side)
62 end
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)
74 return c1, rel
75 end
77 function turtle.cadd(c1, c2)
78 return turtle.coord(c1.x + c2.x, c1.y + c2.y, c1.z + c2.z)
79 end
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))
85 end
87 local function between(x, y, z) -- x is between y and z (inclusive)
88 return y <= x and x <= z
89 end
91 function turtle.getdir() --
92 local rot = minetest.localplayer:get_yaw() % 360
93 if between(rot, 315, 360) or between(rot, 0, 45) then
94 return "north"
95 elseif between(rot, 135, 225) then
96 return "south"
97 elseif between(rot, 225, 315) then
98 return "east"
99 elseif between(rot, 45, 135) then
100 return "west"
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)
118 local f = coord.x
119 local y = coord.y
120 local r = coord.z
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)