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