26486f4ed87cea9969e8316c6810499b3502da42
[oweals/minetest_game.git] / mods / boats / 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         if staticdata then
90                 self.v = tonumber(staticdata)
91         end
92         self.last_v = self.v
93 end
94
95
96 function boat.get_staticdata(self)
97         return tostring(self.v)
98 end
99
100
101 function boat.on_punch(self, puncher)
102         if not puncher or not puncher:is_player() or self.removed then
103                 return
104         end
105         if self.driver and puncher == self.driver then
106                 self.driver = nil
107                 puncher:set_detach()
108                 default.player_attached[puncher:get_player_name()] = false
109         end
110         if not self.driver then
111                 self.removed = true
112                 -- delay remove to ensure player is detached
113                 minetest.after(0.1, function()
114                         self.object:remove()
115                 end)
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")
120                         else
121                                 minetest.add_item(self.object:getpos(), "boats:boat")
122                         end
123                 end
124         end
125 end
126
127
128 function boat.on_step(self, dtime)
129         self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
130         if self.driver then
131                 local ctrl = self.driver:get_player_control()
132                 local yaw = self.object:getyaw()
133                 if ctrl.up then
134                         self.v = self.v + 0.1
135                 elseif ctrl.down then
136                         self.v = self.v - 0.1
137                 end
138                 if ctrl.left then
139                         if self.v < 0 then
140                                 self.object:setyaw(yaw - (1 + dtime) * 0.03)
141                         else
142                                 self.object:setyaw(yaw + (1 + dtime) * 0.03)
143                         end
144                 elseif ctrl.right then
145                         if self.v < 0 then
146                                 self.object:setyaw(yaw + (1 + dtime) * 0.03)
147                         else
148                                 self.object:setyaw(yaw - (1 + dtime) * 0.03)
149                         end
150                 end
151         end
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())
155                 return
156         end
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})
161                 self.v = 0
162                 return
163         end
164         if math.abs(self.v) > 5 then
165                 self.v = 5 * get_sign(self.v)
166         end
167
168         local p = self.object:getpos()
169         p.y = p.y - 0.5
170         local new_velo
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
175                         self.v = 0
176                         new_acce = {x = 0, y = 1, z = 0}
177                 else
178                         new_acce = {x = 0, y = -9.8, z = 0}
179                 end
180                 new_velo = get_velocity(self.v, self.object:getyaw(),
181                         self.object:getvelocity().y)
182                 self.object:setpos(self.object:getpos())
183         else
184                 p.y = p.y + 1
185                 if is_water(p) then
186                         local y = self.object:getvelocity().y
187                         if y >= 5 then
188                                 y = 5
189                         elseif y < 0 then
190                                 new_acce = {x = 0, y = 20, z = 0}
191                         else
192                                 new_acce = {x = 0, y = 5, z = 0}
193                         end
194                         new_velo = get_velocity(self.v, self.object:getyaw(), y)
195                         self.object:setpos(self.object:getpos())
196                 else
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)
203                         else
204                                 new_velo = get_velocity(self.v, self.object:getyaw(),
205                                         self.object:getvelocity().y)
206                                 self.object:setpos(self.object:getpos())
207                         end
208                 end
209         end
210         self.object:setvelocity(new_velo)
211         self.object:setacceleration(new_acce)
212 end
213
214
215 minetest.register_entity("boats:boat", boat)
216
217
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},
225
226         on_place = function(itemstack, placer, pointed_thing)
227                 if pointed_thing.type ~= "node" then
228                         return itemstack
229                 end
230                 if not is_water(pointed_thing.under) then
231                         return itemstack
232                 end
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()
238                 end
239                 return itemstack
240         end,
241 })
242
243
244 minetest.register_craft({
245         output = "boats:boat",
246         recipe = {
247                 {"",           "",           ""          },
248                 {"group:wood", "",           "group:wood"},
249                 {"group:wood", "group:wood", "group:wood"},
250         },
251 })
252
253 minetest.register_craft({
254         type = "fuel",
255         recipe = "boats:boat",
256         burntime = 20,
257 })