Skip to content

Commit 99d0442

Browse files
committedAug 18, 2015
Boats: Fix sinking through boat when detaching
By CProgrammerRU Also, by paramat: Slightly raise base of collision box Improve code style
1 parent b240499 commit 99d0442

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed
 

Diff for: ‎mods/boats/init.lua

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
--
32
-- Helper functions
43
--
@@ -8,6 +7,7 @@ local function is_water(pos)
87
return minetest.get_item_group(nn, "water") ~= 0
98
end
109

10+
1111
local function get_sign(i)
1212
if i == 0 then
1313
return 0
@@ -16,12 +16,14 @@ local function get_sign(i)
1616
end
1717
end
1818

19+
1920
local function get_velocity(v, yaw, y)
2021
local x = -math.sin(yaw) * v
2122
local z = math.cos(yaw) * v
2223
return {x = x, y = y, z = z}
2324
end
2425

26+
2527
local function get_v(v)
2628
return math.sqrt(v.x ^ 2 + v.z ^ 2)
2729
end
@@ -32,7 +34,7 @@ end
3234

3335
local boat = {
3436
physical = true,
35-
collisionbox = {-0.5, -0.4, -0.5, 0.5, 0.3, 0.5},
37+
collisionbox = {-0.5, -0.35, -0.5, 0.5, 0.3, 0.5},
3638
visual = "mesh",
3739
mesh = "boat.obj",
3840
textures = {"default_wood.png"},
@@ -43,6 +45,7 @@ local boat = {
4345
removed = false
4446
}
4547

48+
4649
function boat.on_rightclick(self, clicker)
4750
if not clicker or not clicker:is_player() then
4851
return
@@ -53,9 +56,15 @@ function boat.on_rightclick(self, clicker)
5356
clicker:set_detach()
5457
default.player_attached[name] = false
5558
default.player_set_animation(clicker, "stand" , 30)
59+
local pos = clicker:getpos()
60+
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
61+
minetest.after(0.1, function()
62+
clicker:setpos(pos)
63+
end)
5664
elseif not self.driver then
5765
self.driver = clicker
58-
clicker:set_attach(self.object, "", {x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
66+
clicker:set_attach(self.object, "",
67+
{x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
5968
default.player_attached[name] = true
6069
minetest.after(0.2, function()
6170
default.player_set_animation(clicker, "sit" , 30)
@@ -64,6 +73,7 @@ function boat.on_rightclick(self, clicker)
6473
end
6574
end
6675

76+
6777
function boat.on_activate(self, staticdata, dtime_s)
6878
self.object:set_armor_groups({immortal = 1})
6979
if staticdata then
@@ -72,11 +82,14 @@ function boat.on_activate(self, staticdata, dtime_s)
7282
self.last_v = self.v
7383
end
7484

85+
7586
function boat.get_staticdata(self)
7687
return tostring(self.v)
7788
end
7889

79-
function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, direction)
90+
91+
function boat.on_punch(self, puncher, time_from_last_punch,
92+
tool_capabilities, direction)
8093
if not puncher or not puncher:is_player() or self.removed then
8194
return
8295
end
@@ -97,6 +110,7 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d
97110
end
98111
end
99112

113+
100114
function boat.on_step(self, dtime)
101115
self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
102116
if self.driver then
@@ -149,7 +163,8 @@ function boat.on_step(self, dtime)
149163
else
150164
new_acce = {x = 0, y = -9.8, z = 0}
151165
end
152-
new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)
166+
new_velo = get_velocity(self.v, self.object:getyaw(),
167+
self.object:getvelocity().y)
153168
self.object:setpos(self.object:getpos())
154169
else
155170
p.y = p.y + 1
@@ -172,7 +187,8 @@ function boat.on_step(self, dtime)
172187
self.object:setpos(pos)
173188
new_velo = get_velocity(self.v, self.object:getyaw(), 0)
174189
else
175-
new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)
190+
new_velo = get_velocity(self.v, self.object:getyaw(),
191+
self.object:getvelocity().y)
176192
self.object:setpos(self.object:getpos())
177193
end
178194
end
@@ -181,8 +197,10 @@ function boat.on_step(self, dtime)
181197
self.object:setacceleration(new_acce)
182198
end
183199

200+
184201
minetest.register_entity("boats:boat", boat)
185202

203+
186204
minetest.register_craftitem("boats:boat", {
187205
description = "Boat",
188206
inventory_image = "boat_inventory.png",
@@ -206,6 +224,7 @@ minetest.register_craftitem("boats:boat", {
206224
end,
207225
})
208226

227+
209228
minetest.register_craft({
210229
output = "boats:boat",
211230
recipe = {
@@ -214,4 +233,3 @@ minetest.register_craft({
214233
{"group:wood", "group:wood", "group:wood"},
215234
},
216235
})
217-

0 commit comments

Comments
 (0)
Please sign in to comment.