-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.lua
190 lines (163 loc) · 5.82 KB
/
commands.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
turtlebot.commands = {};
local commands = turtlebot.commands
turtlebot.directions = {
left = 1, right = 2, forward = 3, backward = 4, up = 5, down = 6,
left_down = 7, right_down = 8, forward_down = 9, backward_down = 10,
left_up = 11, right_up = 12, forward_up = 13, backward_up = 14
}
local directions = turtlebot.directions
turtlebot.opposite_direction = {
[directions.left] = directions.right, [directions.right] = directions.left,
[directions.forward] = directions.backward, [directions.backward] = directions.forward,
[directions.up] = directions.down, [directions.down] = directions.up,
[directions.left_down] = directions.right_up, [directions.right_down] = directions.left_up,
[directions.forward_down] = directions.backward_up, [directions.backward_down] = directions.forward_up,
[directions.left_up] = directions.right_down, [directions.right_up] = directions.left_down,
[directions.forward_up] = directions.backward_down, [directions.backward_up] = directions.forward_down
}
turtlebot.plant_table = {
["farming:seed_barley"] = "farming:barley_1",
["farming:beans"] = "farming:beanpole_1", -- so it works with farming redo mod
["farming:blueberries"] = "farming:blueberry_1",
["farming:carrot"] = "farming:carrot_1",
["farming:cocoa_beans"] = "farming:cocoa_1",
["farming:coffee_beans"] = "farming:coffee_1",
["farming:corn"] = "farming:corn_1",
["farming:seed_cotton"] = "farming:cotton_1",
["farming:cucumber"] = "farming:cucumber_1",
["farming:grapes"] = "farming:grapes_1",
["farming:melon_slice"] = "farming:melon_1",
["farming:potato"] = "farming:potato_1",
["farming:pumpkin_slice"] = "farming:pumpkin_1",
["farming:raspberries"] = "farming:raspberry_1",
["farming:rhubarb"] = "farming:rhubarb_1",
["farming:tomato"] = "farming:tomato_1",
["farming:seed_wheat"] = "farming:wheat_1"
}
local pi = math.pi;
local function tick(pos) -- needed for plants to start growing: minetest 0.4.14 farming
minetest.get_node_timer(pos):start(math.random(166, 286))
end
local function pos_in_dir(obj, dir) -- position after we move in specified direction
local yaw = obj:getyaw();
local pos = obj:getpos();
if dir == directions.left then
yaw = yaw + pi/2;
elseif dir == directions.right then
yaw = yaw - pi/2;
elseif dir == directions.forward then
elseif dir == directions.backward then
yaw = yaw+pi;
elseif dir == directions.up then
pos.y = pos.y + 1
elseif dir == directions.down then
pos.y = pos.y - 1
elseif dir == directions.left_down then
yaw = yaw + pi/2;
pos.y = pos.y - 1
elseif dir == directions.right_down then
yaw = yaw - pi/2;
pos.y = pos.y - 1
elseif dir == directions.forward_down then
pos.y = pos.y - 1
elseif dir == directions.backward_down then
yaw = yaw + pi;
pos.y = pos.y - 1
elseif dir == directions.left_up then
yaw = yaw + pi/2;
pos.y = pos.y + 1
elseif dir == directions.right_up then
yaw = yaw - pi/2;
pos.y = pos.y + 1
elseif dir == directions.forward_up then
pos.y = pos.y + 1
elseif dir == directions.backward_up then
yaw = yaw + pi;
pos.y = pos.y + 1
end
if dir ~= directions.up and dir ~= directions.down then
pos.x = pos.x - math.sin(yaw)
pos.z = pos.z + math.cos(yaw)
end
return pos
end
commands.move = function(spawnPos, dir)
local turtle = turtlebot.get(spawnPos);
local obj = turtle.obj;
local pos = pos_in_dir(obj, dir)
-- can move through walkable nodes
if minetest.registered_nodes[minetest.get_node(pos).name].walkable then return end
-- up; no levitation!
if minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == "air" and
minetest.get_node({x=pos.x,y=pos.y-2,z=pos.z}).name == "air" then
minetest.chat_send_player(turtle.owner, "Turtles can't fly")
return false
end
obj:moveto(pos, true)
return true
end
commands.turn = function (spawnPos, angle)
local obj = turtlebot.get(spawnPos).obj;
local yaw;
-- more precise turns by 1 degree resolution
local mult = math.pi/180;
local yaw = obj:getyaw();
yaw = math.floor((yaw+angle)/mult+0.5)*mult;
obj:setyaw(yaw);
end
commands.dig = function(spawnPos, dir)
local obj = turtlebot.get(spawnPos).obj;
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
if minetest.is_protected(pos, luaent.owner) then return false end
local nodename = minetest.get_node(pos).name;
if nodename == "air" or nodename=="ignore" then return false end
local spos = obj:get_luaentity().spawnpos;
local inv = minetest.get_meta(spos):get_inventory();
minetest.set_node(pos,{name = "air"})
--DS: sounds
local sounds = minetest.registered_nodes[nodename].sounds
if sounds then
local sound = sounds.dug
if sound then
minetest.sound_play(sound,{pos=pos, max_hear_distance = 10})
end
end
return true
end
commands.read_node = function(spawnPos, dir)
local obj = turtlebot.get(spawnPos).obj;
local pos = pos_in_dir(obj, dir)
return minetest.get_node(pos).name or ""
end
commands.place = function(spawnPos, nodename, param2, dir)
local obj = turtlebot.get(spawnPos).obj;
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
if minetest.is_protected(pos, luaent.owner) then return false end
if minetest.get_node(pos).name ~= "air" then return false end
local spos = obj:get_luaentity().spawnpos;
--DS
local registered_node = minetest.registered_nodes[nodename];
if registered_node then
local sounds = registered_node.sounds
if sounds then
local sound = sounds.place
if sound then
minetest.sound_play(sound,{pos=pos, max_hear_distance = 10})
end
end
end
local plantName = turtlebot.plant_table[nodename];
if plantName then
minetest.set_node(pos, {name = plantName})
tick(pos); -- needed for seeds to grow
else -- normal place
if param2 then
minetest.set_node(pos, {name = nodename, param2 = param2})
else
minetest.set_node(pos, {name = nodename})
end
end
return true
end