Creative: Make the placenode registration check for non-player placers
[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                 local inv = puncher:get_inventory()
113                 if not (creative and creative.is_enabled_for
114                                 and creative.is_enabled_for(puncher:get_player_name()))
115                                 or not inv:contains_item("main", "boats:boat") then
116                         local leftover = inv:add_item("main", "boats:boat")
117                         -- if no room in inventory add a replacement boat to the world
118                         if not leftover:is_empty() then
119                                 minetest.add_item(self.object:getpos(), leftover)
120                         end
121                 end
122                 -- delay remove to ensure player is detached
123                 minetest.after(0.1, function()
124                         self.object:remove()
125                 end)
126         end
127 end
128
129
130 function boat.on_step(self, dtime)
131         self.v = get_v(self.object:getvelocity()) * get_sign(self.v)
132         if self.driver then
133                 local ctrl = self.driver:get_player_control()
134                 local yaw = self.object:getyaw()
135                 if ctrl.up then
136                         self.v = self.v + 0.1
137                 elseif ctrl.down then
138                         self.v = self.v - 0.1
139                 end
140                 if ctrl.left then
141                         if self.v < 0 then
142                                 self.object:setyaw(yaw - (1 + dtime) * 0.03)
143                         else
144                                 self.object:setyaw(yaw + (1 + dtime) * 0.03)
145                         end
146                 elseif ctrl.right then
147                         if self.v < 0 then
148                                 self.object:setyaw(yaw + (1 + dtime) * 0.03)
149                         else
150                                 self.object:setyaw(yaw - (1 + dtime) * 0.03)
151                         end
152                 end
153         end
154         local velo = self.object:getvelocity()
155         if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
156                 self.object:setpos(self.object:getpos())
157                 return
158         end
159         local s = get_sign(self.v)
160         self.v = self.v - 0.02 * s
161         if s ~= get_sign(self.v) then
162                 self.object:setvelocity({x = 0, y = 0, z = 0})
163                 self.v = 0
164                 return
165         end
166         if math.abs(self.v) > 5 then
167                 self.v = 5 * get_sign(self.v)
168         end
169
170         local p = self.object:getpos()
171         p.y = p.y - 0.5
172         local new_velo
173         local new_acce = {x = 0, y = 0, z = 0}
174         if not is_water(p) then
175                 local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
176                 if (not nodedef) or nodedef.walkable then
177                         self.v = 0
178                         new_acce = {x = 0, y = 1, z = 0}
179                 else
180                         new_acce = {x = 0, y = -9.8, z = 0}
181                 end
182                 new_velo = get_velocity(self.v, self.object:getyaw(),
183                         self.object:getvelocity().y)
184                 self.object:setpos(self.object:getpos())
185         else
186                 p.y = p.y + 1
187                 if is_water(p) then
188                         local y = self.object:getvelocity().y
189                         if y >= 5 then
190                                 y = 5
191                         elseif y < 0 then
192                                 new_acce = {x = 0, y = 20, z = 0}
193                         else
194                                 new_acce = {x = 0, y = 5, z = 0}
195                         end
196                         new_velo = get_velocity(self.v, self.object:getyaw(), y)
197                         self.object:setpos(self.object:getpos())
198                 else
199                         new_acce = {x = 0, y = 0, z = 0}
200                         if math.abs(self.object:getvelocity().y) < 1 then
201                                 local pos = self.object:getpos()
202                                 pos.y = math.floor(pos.y) + 0.5
203                                 self.object:setpos(pos)
204                                 new_velo = get_velocity(self.v, self.object:getyaw(), 0)
205                         else
206                                 new_velo = get_velocity(self.v, self.object:getyaw(),
207                                         self.object:getvelocity().y)
208                                 self.object:setpos(self.object:getpos())
209                         end
210                 end
211         end
212         self.object:setvelocity(new_velo)
213         self.object:setacceleration(new_acce)
214 end
215
216
217 minetest.register_entity("boats:boat", boat)
218
219
220 minetest.register_craftitem("boats:boat", {
221         description = "Boat",
222         inventory_image = "boats_inventory.png",
223         wield_image = "boats_wield.png",
224         wield_scale = {x = 2, y = 2, z = 1},
225         liquids_pointable = true,
226         groups = {flammable = 2},
227
228         on_place = function(itemstack, placer, pointed_thing)
229                 local under = pointed_thing.under
230                 local node = minetest.get_node(under)
231                 local udef = minetest.registered_nodes[node.name]
232                 if udef and udef.on_rightclick and
233                                 not (placer and placer:get_player_control().sneak) then
234                         return udef.on_rightclick(under, node, placer, itemstack,
235                                 pointed_thing) or itemstack
236                 end
237
238                 if pointed_thing.type ~= "node" then
239                         return itemstack
240                 end
241                 if not is_water(pointed_thing.under) then
242                         return itemstack
243                 end
244                 pointed_thing.under.y = pointed_thing.under.y + 0.5
245                 boat = minetest.add_entity(pointed_thing.under, "boats:boat")
246                 if boat then
247                         boat:setyaw(placer:get_look_horizontal())
248                         if not (creative and creative.is_enabled_for
249                                         and creative.is_enabled_for(placer:get_player_name())) then
250                                 itemstack:take_item()
251                         end
252                 end
253                 return itemstack
254         end,
255 })
256
257
258 minetest.register_craft({
259         output = "boats:boat",
260         recipe = {
261                 {"",           "",           ""          },
262                 {"group:wood", "",           "group:wood"},
263                 {"group:wood", "group:wood", "group:wood"},
264         },
265 })
266
267 minetest.register_craft({
268         type = "fuel",
269         recipe = "boats:boat",
270         burntime = 20,
271 })