Skip to content

Commit c993e14

Browse files
committedJul 9, 2014
Add spaces around operators in boat mod code, fix a problem with boat staticdata, fix a crash that can occur with boat going over unknown nodes.
1 parent 955f3cf commit c993e14

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed
 

‎mods/boats/init.lua

+37-36
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ local function get_sign(i)
1212
if i == 0 then
1313
return 0
1414
else
15-
return i/math.abs(i)
15+
return i / math.abs(i)
1616
end
1717
end
1818

1919
local function get_velocity(v, yaw, y)
20-
local x = -math.sin(yaw)*v
21-
local z = math.cos(yaw)*v
22-
return {x=x, y=y, z=z}
20+
local x = -math.sin(yaw) * v
21+
local z = math.cos(yaw) * v
22+
return {x = x, y = y, z = z}
2323
end
2424

2525
local function get_v(v)
26-
return math.sqrt(v.x^2+v.z^2)
26+
return math.sqrt(v.x ^ 2 + v.z ^ 2)
2727
end
2828

2929
--
@@ -32,7 +32,7 @@ end
3232

3333
local boat = {
3434
physical = true,
35-
collisionbox = {-0.6,-0.4,-0.6, 0.6,0.3,0.6},
35+
collisionbox = {-0.6, -0.4, -0.6, 0.6, 0.3, 0.6},
3636
visual = "mesh",
3737
mesh = "boat.x",
3838
textures = {"default_wood.png"},
@@ -55,25 +55,25 @@ function boat.on_rightclick(self, clicker)
5555
default.player_set_animation(clicker, "stand" , 30)
5656
elseif not self.driver then
5757
self.driver = clicker
58-
clicker:set_attach(self.object, "", {x=0,y=11,z=-3}, {x=0,y=0,z=0})
58+
clicker:set_attach(self.object, "", {x = 0, y = 11, z = -3}, {x = 0, y = 0, z = 0})
5959
default.player_attached[name] = true
6060
minetest.after(0.2, function()
6161
default.player_set_animation(clicker, "sit" , 30)
6262
end)
63-
self.object:setyaw(clicker:get_look_yaw()-math.pi/2)
63+
self.object:setyaw(clicker:get_look_yaw() - math.pi / 2)
6464
end
6565
end
6666

6767
function boat.on_activate(self, staticdata, dtime_s)
68-
self.object:set_armor_groups({immortal=1})
68+
self.object:set_armor_groups({immortal = 1})
6969
if staticdata then
7070
self.v = tonumber(staticdata)
7171
end
7272
self.last_v = self.v
7373
end
7474

75-
function boat.get_staticdata()
76-
return tostring(v)
75+
function boat.get_staticdata(self)
76+
return tostring(self.v)
7777
end
7878

7979
function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, direction)
@@ -85,7 +85,7 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d
8585

8686
self.removed = true
8787
-- delay remove to ensure player is detached
88-
minetest.after(0.1,function()
88+
minetest.after(0.1, function()
8989
self.object:remove()
9090
end)
9191
if not minetest.setting_getbool("creative_mode") then
@@ -94,28 +94,28 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d
9494
end
9595

9696
function boat.on_step(self, dtime)
97-
self.v = get_v(self.object:getvelocity())*get_sign(self.v)
97+
self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
9898
if self.driver then
9999
local ctrl = self.driver:get_player_control()
100100
local yaw = self.object:getyaw()
101101
if ctrl.up then
102-
self.v = self.v+0.1
102+
self.v = self.v + 0.1
103103
end
104104
if ctrl.down then
105-
self.v = self.v-0.08
105+
self.v = self.v - 0.08
106106
end
107107
if ctrl.left then
108108
if ctrl.down then
109-
self.object:setyaw(yaw-math.pi/120-dtime*math.pi/120)
109+
self.object:setyaw(yaw - math.pi / 120 - dtime * math.pi / 120)
110110
else
111-
self.object:setyaw(yaw+math.pi/120+dtime*math.pi/120)
111+
self.object:setyaw(yaw + math.pi / 120 + dtime * math.pi / 120)
112112
end
113113
end
114114
if ctrl.right then
115115
if ctrl.down then
116-
self.object:setyaw(yaw+math.pi/120+dtime*math.pi/120)
116+
self.object:setyaw(yaw + math.pi / 120 + dtime * math.pi / 120)
117117
else
118-
self.object:setyaw(yaw-math.pi/120-dtime*math.pi/120)
118+
self.object:setyaw(yaw - math.pi / 120 - dtime*math.pi/120)
119119
end
120120
end
121121
end
@@ -124,43 +124,44 @@ function boat.on_step(self, dtime)
124124
return
125125
end
126126
local s = get_sign(self.v)
127-
self.v = self.v - 0.02*s
127+
self.v = self.v - 0.02 * s
128128
if s ~= get_sign(self.v) then
129-
self.object:setvelocity({x=0, y=0, z=0})
129+
self.object:setvelocity({x = 0, y = 0, z = 0})
130130
self.v = 0
131131
return
132132
end
133133
if math.abs(self.v) > 4.5 then
134-
self.v = 4.5*get_sign(self.v)
134+
self.v = 4.5 * get_sign(self.v)
135135
end
136136

137137
local p = self.object:getpos()
138-
p.y = p.y-0.5
139-
local new_velo = {x=0,y=0,z=0}
140-
local new_acce = {x=0,y=0,z=0}
138+
p.y = p.y - 0.5
139+
local new_velo = {x = 0, y = 0, z = 0}
140+
local new_acce = {x = 0, y = 0, z = 0}
141141
if not is_water(p) then
142-
if minetest.registered_nodes[minetest.env:get_node(p).name].walkable then
142+
local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
143+
if (not nodedef) or nodedef.walkable then
143144
self.v = 0
144145
end
145-
new_acce = {x=0, y=-10, z=0}
146+
new_acce = {x = 0, y = -10, z = 0}
146147
new_velo = get_velocity(self.v, self.object:getyaw(), self.object:getvelocity().y)
147148
else
148-
p.y = p.y+1
149+
p.y = p.y + 1
149150
if is_water(p) then
150-
new_acce = {x=0, y=3, z=0}
151+
new_acce = {x = 0, y = 3, z = 0}
151152
local y = self.object:getvelocity().y
152153
if y > 2 then
153154
y = 2
154155
end
155156
if y < 0 then
156-
self.object:setacceleration({x=0, y=10, z=0})
157+
self.object:setacceleration({x = 0, y = 10, z = 0})
157158
end
158159
new_velo = get_velocity(self.v, self.object:getyaw(), y)
159160
else
160-
new_acce = {x=0, y=0, z=0}
161+
new_acce = {x = 0, y = 0, z = 0}
161162
if math.abs(self.object:getvelocity().y) < 1 then
162163
local pos = self.object:getpos()
163-
pos.y = math.floor(pos.y)+0.5
164+
pos.y = math.floor(pos.y) + 0.5
164165
self.object:setpos(pos)
165166
new_velo = get_velocity(self.v, self.object:getyaw(), 0)
166167
else
@@ -179,7 +180,7 @@ minetest.register_craftitem("boats:boat", {
179180
description = "Boat",
180181
inventory_image = "boat_inventory.png",
181182
wield_image = "boat_wield.png",
182-
wield_scale = {x=2, y=2, z=1},
183+
wield_scale = {x = 2, y = 2, z = 1},
183184
liquids_pointable = true,
184185

185186
on_place = function(itemstack, placer, pointed_thing)
@@ -189,7 +190,7 @@ minetest.register_craftitem("boats:boat", {
189190
if not is_water(pointed_thing.under) then
190191
return
191192
end
192-
pointed_thing.under.y = pointed_thing.under.y+0.5
193+
pointed_thing.under.y = pointed_thing.under.y + 0.5
193194
minetest.add_entity(pointed_thing.under, "boats:boat")
194195
if not minetest.setting_getbool("creative_mode") then
195196
itemstack:take_item()
@@ -201,8 +202,8 @@ minetest.register_craftitem("boats:boat", {
201202
minetest.register_craft({
202203
output = "boats:boat",
203204
recipe = {
204-
{"", "", ""},
205-
{"group:wood", "", "group:wood"},
205+
{"", "", "" },
206+
{"group:wood", "", "group:wood"},
206207
{"group:wood", "group:wood", "group:wood"},
207208
},
208209
})

0 commit comments

Comments
 (0)
Please sign in to comment.