5 local function is_water(pos)
6 local nn = minetest.get_node(pos).name
7 return minetest.get_item_group(nn, "water") ~= 0
11 local function get_sign(i)
15 return i / math.abs(i)
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}
27 local function get_v(v)
28 return math.sqrt(v.x ^ 2 + v.z ^ 2)
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},
41 mesh = "boats_boat.obj",
42 textures = {"default_wood.png"},
51 function boat.on_rightclick(self, clicker)
52 if not clicker or not clicker:is_player() then
55 local name = clicker:get_player_name()
56 if self.driver and clicker == self.driver then
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()
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
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)
82 clicker:set_look_horizontal(self.object:getyaw())
87 function boat.on_activate(self, staticdata, dtime_s)
88 self.object:set_armor_groups({immortal = 1})
90 self.v = tonumber(staticdata)
96 function boat.get_staticdata(self)
97 return tostring(self.v)
101 function boat.on_punch(self, puncher)
102 if not puncher or not puncher:is_player() or self.removed then
105 if self.driver and puncher == self.driver then
108 default.player_attached[puncher:get_player_name()] = false
110 if not self.driver then
112 -- delay remove to ensure player is detached
113 minetest.after(0.1, function()
116 if not minetest.setting_getbool("creative_mode") then
117 local inv = puncher:get_inventory()
118 if inv:room_for_item("main", "boats:boat") then
119 inv:add_item("main", "boats:boat")
121 minetest.add_item(self.object:getpos(), "boats:boat")
128 function boat.on_step(self, dtime)
129 self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
131 local ctrl = self.driver:get_player_control()
132 local yaw = self.object:getyaw()
134 self.v = self.v + 0.1
135 elseif ctrl.down then
136 self.v = self.v - 0.1
140 self.object:setyaw(yaw - (1 + dtime) * 0.03)
142 self.object:setyaw(yaw + (1 + dtime) * 0.03)
144 elseif ctrl.right then
146 self.object:setyaw(yaw + (1 + dtime) * 0.03)
148 self.object:setyaw(yaw - (1 + dtime) * 0.03)
152 local velo = self.object:getvelocity()
153 if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
154 self.object:setpos(self.object:getpos())
157 local s = get_sign(self.v)
158 self.v = self.v - 0.02 * s
159 if s ~= get_sign(self.v) then
160 self.object:setvelocity({x = 0, y = 0, z = 0})
164 if math.abs(self.v) > 5 then
165 self.v = 5 * get_sign(self.v)
168 local p = self.object:getpos()
171 local new_acce = {x = 0, y = 0, z = 0}
172 if not is_water(p) then
173 local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
174 if (not nodedef) or nodedef.walkable then
176 new_acce = {x = 0, y = 1, z = 0}
178 new_acce = {x = 0, y = -9.8, z = 0}
180 new_velo = get_velocity(self.v, self.object:getyaw(),
181 self.object:getvelocity().y)
182 self.object:setpos(self.object:getpos())
186 local y = self.object:getvelocity().y
190 new_acce = {x = 0, y = 20, z = 0}
192 new_acce = {x = 0, y = 5, z = 0}
194 new_velo = get_velocity(self.v, self.object:getyaw(), y)
195 self.object:setpos(self.object:getpos())
197 new_acce = {x = 0, y = 0, z = 0}
198 if math.abs(self.object:getvelocity().y) < 1 then
199 local pos = self.object:getpos()
200 pos.y = math.floor(pos.y) + 0.5
201 self.object:setpos(pos)
202 new_velo = get_velocity(self.v, self.object:getyaw(), 0)
204 new_velo = get_velocity(self.v, self.object:getyaw(),
205 self.object:getvelocity().y)
206 self.object:setpos(self.object:getpos())
210 self.object:setvelocity(new_velo)
211 self.object:setacceleration(new_acce)
215 minetest.register_entity("boats:boat", boat)
218 minetest.register_craftitem("boats:boat", {
219 description = "Boat",
220 inventory_image = "boats_inventory.png",
221 wield_image = "boats_wield.png",
222 wield_scale = {x = 2, y = 2, z = 1},
223 liquids_pointable = true,
224 groups = {flammable = 2},
226 on_place = function(itemstack, placer, pointed_thing)
227 if pointed_thing.type ~= "node" then
230 if not is_water(pointed_thing.under) then
233 pointed_thing.under.y = pointed_thing.under.y + 0.5
234 boat = minetest.add_entity(pointed_thing.under, "boats:boat")
235 boat:setyaw(placer:get_look_horizontal())
236 if not minetest.setting_getbool("creative_mode") then
237 itemstack:take_item()
244 minetest.register_craft({
245 output = "boats:boat",
248 {"group:wood", "", "group:wood"},
249 {"group:wood", "group:wood", "group:wood"},
253 minetest.register_craft({
255 recipe = "boats:boat",