Rudimentary routing system
[advboats.git] / init.lua
1 --
2 -- Helper functions
3 --
4
5 local function is_water(pos)
6 local nn = minetest.get_node(pos).name
7 return minetest.get_item_group(nn, "water") ~= 0
8 end
9
10
11 local function get_sign(i)
12 if i == 0 then
13 return 0
14 else
15 return i / math.abs(i)
16 end
17 end
18
19
20 local function get_velocity(v, yaw, y)
21 local x = -math.sin(yaw) * v
22 local z = math.cos(yaw) * v
23 return {x = x, y = y, z = z}
24 end
25
26
27 local function get_v(v)
28 return math.sqrt(v.x ^ 2 + v.z ^ 2)
29 end
30
31 --
32 -- Boat entity
33 --
34
35 local boat = {
36 physical = true,
37 -- Warning: Do not change the position of the collisionbox top surface,
38 -- lowering it causes the boat to fall through the world if underwater
39 collisionbox = {-0.5, -0.35, -0.5, 0.5, 0.3, 0.5},
40 visual = "mesh",
41 mesh = "boats_boat.obj",
42 textures = {"default_wood.png"},
43
44 driver = nil,
45 v = 0,
46 last_v = 0,
47 removed = false
48 }
49
50
51 function boat.on_rightclick(self, clicker)
52 if not clicker or not clicker:is_player() then
53 return
54 end
55 local name = clicker:get_player_name()
56 if self.driver and clicker == self.driver then
57 self.driver = nil
58 clicker:set_detach()
59 default.player_attached[name] = false
60 default.player_set_animation(clicker, "stand" , 30)
61 local pos = clicker:getpos()
62 pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
63 minetest.after(0.1, function()
64 clicker:setpos(pos)
65 end)
66 elseif not self.driver then
67 local attach = clicker:get_attach()
68 if attach and attach:get_luaentity() then
69 local luaentity = attach:get_luaentity()
70 if luaentity.driver then
71 luaentity.driver = nil
72 end
73 clicker:set_detach()
74 end
75 self.driver = clicker
76 clicker:set_attach(self.object, "",
77 {x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
78 default.player_attached[name] = true
79 minetest.after(0.2, function()
80 default.player_set_animation(clicker, "sit" , 30)
81 end)
82 clicker:set_look_horizontal(self.object:getyaw())
83 end
84 end
85
86
87 function boat.on_activate(self, staticdata, dtime_s)
88 self.object:set_armor_groups({immortal = 1})
89 if staticdata then
90 self.v = tonumber(staticdata)
91 end
92 self.last_v = self.v
93 end
94
95
96 function boat.get_staticdata(self)
97 return tostring(self.v)
98 end
99
100
101 function boat.on_punch(self, puncher)
102 if not puncher or not puncher:is_player() or self.removed then
103 return
104 end
105 if self.driver and puncher == self.driver then
106 self.driver = nil
107 puncher:set_detach()
108 default.player_attached[puncher:get_player_name()] = false
109 end
110 if not self.driver then
111 self.removed = true
112 local inv = puncher:get_inventory()
113 if not (creative and creative.is_enabled_for
114 and creative.is_enabled_for(puncher:get_player_name()))
115 or not inv:contains_item("main", "advboats:boat") then
116 local leftover = inv:add_item("main", "advboats:boat")
117 -- if no room in inventory add a replacement boat to the world
118 if not leftover:is_empty() then
119 minetest.add_item(self.object:getpos(), leftover)
120 end
121 end
122 -- delay remove to ensure player is detached
123 minetest.after(0.1, function()
124 self.object:remove()
125 end)
126 end
127 end
128 minetest.register_entity("advboats:mark", {
129 initial_properties = {
130 visual = "cube",
131 visual_size = {x=1.1, y=1.1},
132 textures = {"areas_pos1.png", "areas_pos1.png",
133 "areas_pos1.png", "areas_pos1.png",
134 "areas_pos1.png", "areas_pos1.png"},
135 collisionbox = {-0.55, -0.55, -0.55, 0.55, 0.55, 0.55},
136 },
137 on_punch = function(self, hitter)
138 self.object:remove()
139 end,
140 })
141
142
143
144 function boat.round_pos(self)
145 -- Round boat's position to the nearest integer location
146 local pos = self.object:getpos()
147 pos.x = math.floor(pos.x+0.5)
148 pos.z = math.floor(pos.z+0.5)
149 self.object:setpos(pos)
150 minetest.add_entity(pos, "advboats:mark")
151 end
152 boat.get_instr_pos = function (instruction)
153 return {x=instruction[1], y=instruction[2], z=instruction[3]}
154 end
155
156 function boat.save_pos(self)
157 local pos = self.object:getpos()
158 local o = math.floor(self.object:getyaw()*4/math.pi+0.5)%8
159 local s = self.v
160 minetest.chat_send_all(pos.x.." "..pos.y.." "..pos.z.." "..o.." "..s)
161 if not self.instructions then
162 self.instructions = {}
163 end
164 local last = self.instructions[#self.instructions]
165 self.instructions[#self.instructions+1] = {pos.x, pos.y, pos.z, o, s}
166 if not last then
167 return
168 end
169 local lastpos = self.get_instr_pos(last)
170 minetest.chat_send_all("Last position:"..lastpos.x..","..lastpos.y..","..lastpos.z..", Distance to last position: "..vector.distance(pos,lastpos))
171
172 end
173
174 -- Instruction format:
175 -- {x, y, z, o, s}
176 -- x,y,z : Coords (int)
177 -- o: orientation in multiples of pi/4 (0 to 7)
178 -- s: target speed in m/s
179
180 function boat.selfdriving_step(self, dtime)
181 if not self.instructions or #self.instructions == 1 then
182 return
183 end
184 if not self.current then
185 self.current = 1
186 local instr = self.instructions[1]
187 local curpos = self.get_instr_pos(instr)
188 self.object:setpos(curpos)
189 self.dnext = -1
190 local nextpos = self.instructions[self.current+2]
191 nextpos = self.get_instr_pos(nextpos)
192 self.object:setpos(nextpos)
193 self.object:setyaw(math.pi/4*instr[4])
194 self.v = instr[5]
195 self.dnext = vector.distance(curpos,nextpos)
196 end
197 if self.dnext < 0 then
198 self.current = self.current + 1
199 if self.current == #self.instructions then
200 self.selfdriving = false
201 self.v = 0
202 return
203 end
204
205 local instr = self.instructions[self.current]
206 local curpos = self.get_instr_pos(instr)
207 local nextpos = self.instructions[self.current+1]
208 if not nextpos then
209 nextpos = self.instructions[1]
210 end
211 nextpos = self.get_instr_pos(nextpos)
212 self.object:setpos(curpos)
213 self.object:setyaw(math.pi/4*instr[4])
214 self.v = instr[5]
215 self.dnext = vector.distance(curpos,nextpos)
216 end
217 self.dnext = self.dnext - math.abs(self.v*dtime)
218 minetest.chat_send_all(self.dnext)
219 end
220
221 function boat.on_step(self, dtime)
222 self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
223 if self.driver and not self.selfdriving then
224 local ctrl = self.driver:get_player_control()
225 local yaw = self.object:getyaw()
226 if ctrl.aux1 then
227 self.selfdriving = true
228 minetest.chat_send_all("Boat is now selfdriving")
229 end
230 if ctrl.up then
231 self.v = self.v + 0.1
232 elseif ctrl.down then
233 self.v = self.v - 0.1
234 end
235 if ctrl.left and not self.pressed then
236 self.pressed = true
237 if self.v < 0 then
238 self.object:setyaw(yaw-math.pi/4)
239 else
240 self.object:setyaw(yaw+math.pi/4)
241 end
242 self:round_pos()
243 self:save_pos()
244 elseif ctrl.right and not self.pressed then
245 self.pressed = true
246 if self.v < 0 then
247 self.object:setyaw(yaw+math.pi/4)
248 else
249 self.object:setyaw(yaw-math.pi/4)
250 end
251 self:round_pos()
252 self:save_pos()
253 elseif not ctrl.right and not ctrl.left then
254 self.pressed = false
255 end
256 end
257 if self.selfdriving then
258 self:selfdriving_step(dtime)
259 end
260 local velo = self.object:getvelocity()
261 if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
262 self.object:setpos(self.object:getpos())
263 return
264 end
265 local s = get_sign(self.v)
266 -- self.v = self.v - 0.02 * s
267 if s ~= get_sign(self.v) then
268 self.object:setvelocity({x = 0, y = 0, z = 0})
269 self.v = 0
270 return
271 end
272 if math.abs(self.v) > 5 then
273 self.v = 5 * get_sign(self.v)
274 end
275
276 local p = self.object:getpos()
277 p.y = p.y - 0.5
278 local new_velo
279 local new_acce = {x = 0, y = 0, z = 0}
280 if not is_water(p) then
281 local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
282 if (not nodedef) or nodedef.walkable then
283 self.v = 0
284 new_acce = {x = 0, y = 1, z = 0}
285 else
286 new_acce = {x = 0, y = -9.8, z = 0}
287 end
288 new_velo = get_velocity(self.v, self.object:getyaw(),
289 self.object:getvelocity().y)
290 self.object:setpos(self.object:getpos())
291 else
292 p.y = p.y + 1
293 if is_water(p) then
294 local y = self.object:getvelocity().y
295 if y >= 5 then
296 y = 5
297 elseif y < 0 then
298 new_acce = {x = 0, y = 20, z = 0}
299 else
300 new_acce = {x = 0, y = 5, z = 0}
301 end
302 new_velo = get_velocity(self.v, self.object:getyaw(), y)
303 self.object:setpos(self.object:getpos())
304 else
305 new_acce = {x = 0, y = 0, z = 0}
306 if math.abs(self.object:getvelocity().y) < 1 then
307 local pos = self.object:getpos()
308 pos.y = math.floor(pos.y) + 0.5
309 self.object:setpos(pos)
310 new_velo = get_velocity(self.v, self.object:getyaw(), 0)
311 else
312 new_velo = get_velocity(self.v, self.object:getyaw(),
313 self.object:getvelocity().y)
314 self.object:setpos(self.object:getpos())
315 end
316 end
317 end
318 self.object:setvelocity(new_velo)
319 self.object:setacceleration(new_acce)
320 end
321
322
323 minetest.register_entity("advboats:boat", boat)
324
325
326 minetest.register_craftitem("advboats:boat", {
327 description = "Boat",
328 inventory_image = "advboats_inventory.png",
329 wield_image = "advboats_wield.png",
330 wield_scale = {x = 2, y = 2, z = 1},
331 liquids_pointable = true,
332 groups = {flammable = 2},
333
334 on_place = function(itemstack, placer, pointed_thing)
335 local under = pointed_thing.under
336 local node = minetest.get_node(under)
337 local udef = minetest.registered_nodes[node.name]
338 if udef and udef.on_rightclick and
339 not (placer and placer:get_player_control().sneak) then
340 return udef.on_rightclick(under, node, placer, itemstack,
341 pointed_thing) or itemstack
342 end
343
344 if pointed_thing.type ~= "node" then
345 return itemstack
346 end
347 if not is_water(pointed_thing.under) then
348 return itemstack
349 end
350 pointed_thing.under.y = pointed_thing.under.y + 0.5
351 boat = minetest.add_entity(pointed_thing.under, "advboats:boat")
352 if boat then
353 -- boat:setyaw(placer:get_look_horizontal())
354 if not (creative and creative.is_enabled_for
355 and creative.is_enabled_for(placer:get_player_name())) then
356 itemstack:take_item()
357 end
358 end
359 return itemstack
360 end,
361 })
362
363
364 minetest.register_craft({
365 output = "advboats:boat",
366 recipe = {
367 {"", "", "" },
368 {"group:wood", "", "group:wood"},
369 {"group:wood", "group:wood", "group:wood"},
370 },
371 })
372
373 minetest.register_craft({
374 type = "fuel",
375 recipe = "advboats:boat",
376 burntime = 20,
377 })