Removed debugging code
[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 if not self.selfdriving then
76 minetest.chat_send_player(name,"Press E to save path and enter automatic mode")
77 end
78 self.driver = clicker
79 clicker:set_attach(self.object, "",
80 {x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
81 default.player_attached[name] = true
82 minetest.after(0.2, function()
83 default.player_set_animation(clicker, "sit" , 30)
84 end)
85 clicker:set_look_horizontal(self.object:getyaw())
86 end
87 end
88
89
90 function boat.on_activate(self, staticdata, dtime_s)
91 self.object:set_armor_groups({immortal = 1})
92 local data = {}
93 if staticdata then
94 data = minetest.deserialize(staticdata)
95 if not data then
96 return
97 end
98
99 self.v = data.v
100 self.instructions = data.instr
101 self.selfdriving = data.sdr
102 self.dnext = data.dn
103 self.current = data.cur
104 end
105 self.last_v = self.v
106 end
107
108
109 function boat.get_staticdata(self)
110 data = {v = self.v, instr = self.instructions, cur = self.current, sdr = self.selfdriving, dn = self.dnext}
111 return minetest.serialize(data)
112 end
113
114
115 function boat.on_punch(self, puncher)
116 if not puncher or not puncher:is_player() or self.removed then
117 return
118 end
119 if self.driver and puncher == self.driver then
120 self.driver = nil
121 puncher:set_detach()
122 default.player_attached[puncher:get_player_name()] = false
123 end
124 if not self.driver then
125 self.removed = true
126 local inv = puncher:get_inventory()
127 if not (creative and creative.is_enabled_for
128 and creative.is_enabled_for(puncher:get_player_name()))
129 or not inv:contains_item("main", "advboats:boat") then
130 local leftover = inv:add_item("main", "advboats:boat")
131 -- if no room in inventory add a replacement boat to the world
132 if not leftover:is_empty() then
133 minetest.add_item(self.object:getpos(), leftover)
134 end
135 end
136 -- delay remove to ensure player is detached
137 minetest.after(0.1, function()
138 self.object:remove()
139 end)
140 end
141 end
142 minetest.register_entity("advboats:mark", {
143 initial_properties = {
144 visual = "cube",
145 visual_size = {x=1.1, y=1.1},
146 textures = {"areas_pos1.png", "areas_pos1.png",
147 "areas_pos1.png", "areas_pos1.png",
148 "areas_pos1.png", "areas_pos1.png"},
149 collisionbox = {-0.55, -0.55, -0.55, 0.55, 0.55, 0.55},
150 },
151 on_punch = function(self, hitter)
152 self.object:remove()
153 end,
154 })
155
156
157
158 function boat.round_pos(self)
159 -- Round boat's position to the nearest integer location
160 local pos = self.object:getpos()
161 pos.x = math.floor(pos.x+0.5)
162 pos.z = math.floor(pos.z+0.5)
163 self.object:setpos(pos)
164 -- minetest.add_entity(pos, "advboats:mark")
165 end
166 boat.get_instr_pos = function (instruction)
167 return {x=instruction[1], y=instruction[2], z=instruction[3]}
168 end
169
170 function boat.save_pos(self)
171 local pos = self.object:getpos()
172 local o = math.floor(self.object:getyaw()*4/math.pi+0.5)%8
173 local s = self.v
174 -- minetest.chat_send_all(pos.x.." "..pos.y.." "..pos.z.." "..o.." "..s)
175 if not self.instructions then
176 self.instructions = {}
177 end
178 local last = self.instructions[#self.instructions]
179 self.instructions[#self.instructions+1] = {pos.x, pos.y, pos.z, o, s}
180 if not last then
181 return
182 end
183 local lastpos = self.get_instr_pos(last)
184 -- minetest.chat_send_all("Last position:"..lastpos.x..","..lastpos.y..","..lastpos.z..", Distance to last position: "..vector.distance(pos,lastpos))
185
186 end
187
188 -- Instruction format:
189 -- {x, y, z, o, s}
190 -- x,y,z : Coords (int)
191 -- o: orientation in multiples of pi/4 (0 to 7)
192 -- s: target speed in m/s
193
194 function boat.selfdriving_step(self, dtime)
195 if not self.instructions or #self.instructions == 1 then
196 return
197 end
198 if not self.current then
199 self.current = 1
200 local instr = self.instructions[1]
201 local curpos = self.get_instr_pos(instr)
202 self.object:setpos(curpos)
203 local nextpos = self.instructions[self.current+1]
204 nextpos = self.get_instr_pos(nextpos)
205 self.object:setpos(curpos)
206 self.object:setyaw(core.dir_to_yaw(vector.direction(curpos,nextpos)))
207 self.v = instr[5]
208 self.dnext = vector.distance(curpos,nextpos)
209 end
210 if self.dnext < 0 then
211 self.current = self.current + 1
212 if self.current == #self.instructions then
213 local instr = self.instructions[self.current]
214 self.current = 0
215 local nextpos = self.instructions[1]
216 nextpos = self.get_instr_pos(nextpos)
217 local curpos = self.get_instr_pos(instr)
218 self.object:setyaw(core.dir_to_yaw(vector.direction(curpos,nextpos)))
219 self.dnext = vector.distance(curpos,nextpos)
220 return
221 end
222
223 local instr = self.instructions[self.current]
224 local curpos = self.get_instr_pos(instr)
225 local nextpos = self.instructions[self.current+1]
226 if not nextpos then
227 nextpos = self.instructions[1]
228 end
229 nextpos = self.get_instr_pos(nextpos)
230 self.object:setpos(curpos)
231 self.object:setyaw(math.pi/4*instr[4])
232 self.v = instr[5]
233 self.dnext = vector.distance(curpos,nextpos)
234 end
235 self.dnext = self.dnext - math.abs(self.v*dtime)
236 -- minetest.chat_send_all(self.dnext)
237 end
238
239 function boat.on_step(self, dtime)
240 self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
241 if self.driver and not self.selfdriving then
242 local ctrl = self.driver:get_player_control()
243 local yaw = self.object:getyaw()
244 if ctrl.aux1 then
245 self.selfdriving = true
246 -- minetest.chat_send_all("Boat is now selfdriving")
247 end
248 if ctrl.up then
249 self.v = self.v + 0.1
250 elseif ctrl.down then
251 self.v = self.v - 0.1
252 end
253 if ctrl.left and not self.pressed then
254 self.pressed = true
255 if self.v < 0 then
256 self.object:setyaw(yaw-math.pi/4)
257 else
258 self.object:setyaw(yaw+math.pi/4)
259 end
260 self:round_pos()
261 self:save_pos()
262 elseif ctrl.right and not self.pressed then
263 self.pressed = true
264 if self.v < 0 then
265 self.object:setyaw(yaw+math.pi/4)
266 else
267 self.object:setyaw(yaw-math.pi/4)
268 end
269 self:round_pos()
270 self:save_pos()
271 elseif not ctrl.right and not ctrl.left then
272 self.pressed = false
273 end
274 end
275 if self.selfdriving then
276 self:selfdriving_step(dtime)
277 end
278 local velo = self.object:getvelocity()
279 if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
280 self.object:setpos(self.object:getpos())
281 return
282 end
283 local s = get_sign(self.v)
284 -- self.v = self.v - 0.02 * s
285 if s ~= get_sign(self.v) then
286 self.object:setvelocity({x = 0, y = 0, z = 0})
287 self.v = 0
288 return
289 end
290 if math.abs(self.v) > 5 then
291 self.v = 5 * get_sign(self.v)
292 end
293
294 local p = self.object:getpos()
295 p.y = p.y - 0.5
296 local new_velo
297 local new_acce = {x = 0, y = 0, z = 0}
298 if not is_water(p) then
299 local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
300 if (not nodedef) or nodedef.walkable then
301 self.v = 0
302 new_acce = {x = 0, y = 1, z = 0}
303 else
304 new_acce = {x = 0, y = -9.8, z = 0}
305 end
306 new_velo = get_velocity(self.v, self.object:getyaw(),
307 self.object:getvelocity().y)
308 self.object:setpos(self.object:getpos())
309 else
310 p.y = p.y + 1
311 if is_water(p) then
312 local y = self.object:getvelocity().y
313 if y >= 5 then
314 y = 5
315 elseif y < 0 then
316 new_acce = {x = 0, y = 20, z = 0}
317 else
318 new_acce = {x = 0, y = 5, z = 0}
319 end
320 new_velo = get_velocity(self.v, self.object:getyaw(), y)
321 self.object:setpos(self.object:getpos())
322 else
323 new_acce = {x = 0, y = 0, z = 0}
324 if math.abs(self.object:getvelocity().y) < 1 then
325 local pos = self.object:getpos()
326 pos.y = math.floor(pos.y) + 0.5
327 self.object:setpos(pos)
328 new_velo = get_velocity(self.v, self.object:getyaw(), 0)
329 else
330 new_velo = get_velocity(self.v, self.object:getyaw(),
331 self.object:getvelocity().y)
332 self.object:setpos(self.object:getpos())
333 end
334 end
335 end
336 self.object:setvelocity(new_velo)
337 self.object:setacceleration(new_acce)
338 end
339
340
341 minetest.register_entity("advboats:boat", boat)
342
343
344 minetest.register_craftitem("advboats:boat", {
345 description = "Advanced Boat",
346 inventory_image = "advboats_inventory.png",
347 wield_image = "advboats_wield.png",
348 wield_scale = {x = 2, y = 2, z = 1},
349 liquids_pointable = true,
350 groups = {flammable = 2},
351
352 on_place = function(itemstack, placer, pointed_thing)
353 local under = pointed_thing.under
354 local node = minetest.get_node(under)
355 local udef = minetest.registered_nodes[node.name]
356 if udef and udef.on_rightclick and
357 not (placer and placer:get_player_control().sneak) then
358 return udef.on_rightclick(under, node, placer, itemstack,
359 pointed_thing) or itemstack
360 end
361
362 if pointed_thing.type ~= "node" then
363 return itemstack
364 end
365 if not is_water(pointed_thing.under) then
366 return itemstack
367 end
368 pointed_thing.under.y = pointed_thing.under.y + 0.5
369 boat = minetest.add_entity(pointed_thing.under, "advboats:boat")
370 if boat then
371 -- boat:setyaw(placer:get_look_horizontal())
372 if not (creative and creative.is_enabled_for
373 and creative.is_enabled_for(placer:get_player_name())) then
374 itemstack:take_item()
375 end
376 end
377 return itemstack
378 end,
379 })
380
381
382 minetest.register_craft({
383 output = "advboats:boat",
384 recipe = {
385 {"", "", "" },
386 {"group:wood", "", "group:wood"},
387 {"group:wood", "group:wood", "group:wood"},
388 },
389 })
390
391 minetest.register_craft({
392 type = "fuel",
393 recipe = "advboats:boat",
394 burntime = 20,
395 })