2014-05-03 12:09:17 +02:00
|
|
|
--
|
|
|
|
-- Helper functions
|
|
|
|
--
|
|
|
|
|
|
|
|
local function is_water(pos)
|
2014-07-01 14:25:43 +02:00
|
|
|
local nn = minetest.get_node(pos).name
|
2014-05-03 12:09:17 +02:00
|
|
|
return minetest.get_item_group(nn, "water") ~= 0
|
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
local function get_sign(i)
|
|
|
|
if i == 0 then
|
|
|
|
return 0
|
|
|
|
else
|
2014-07-09 09:47:51 +02:00
|
|
|
return i / math.abs(i)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
local function get_velocity(v, yaw, y)
|
2014-07-09 09:47:51 +02:00
|
|
|
local x = -math.sin(yaw) * v
|
|
|
|
local z = math.cos(yaw) * v
|
|
|
|
return {x = x, y = y, z = z}
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
local function get_v(v)
|
2014-07-09 09:47:51 +02:00
|
|
|
return math.sqrt(v.x ^ 2 + v.z ^ 2)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Boat entity
|
|
|
|
--
|
|
|
|
|
|
|
|
local boat = {
|
|
|
|
physical = true,
|
2016-06-14 22:16:22 +02:00
|
|
|
-- Warning: Do not change the position of the collisionbox top surface,
|
|
|
|
-- lowering it causes the boat to fall through the world if underwater
|
|
|
|
collisionbox = {-0.5, -0.35, -0.5, 0.5, 0.3, 0.5},
|
2014-05-03 12:09:17 +02:00
|
|
|
visual = "mesh",
|
2016-02-01 18:30:45 +01:00
|
|
|
mesh = "boats_boat.obj",
|
2014-05-03 12:09:17 +02:00
|
|
|
textures = {"default_wood.png"},
|
2014-07-06 09:53:54 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
driver = nil,
|
|
|
|
v = 0,
|
2014-07-01 14:25:43 +02:00
|
|
|
last_v = 0,
|
2018-07-03 20:52:28 +02:00
|
|
|
removed = false,
|
|
|
|
auto = false
|
2014-05-03 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-07-06 10:39:16 +02:00
|
|
|
function boat.on_rightclick(self, clicker)
|
2014-05-03 12:09:17 +02:00
|
|
|
if not clicker or not clicker:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local name = clicker:get_player_name()
|
|
|
|
if self.driver and clicker == self.driver then
|
|
|
|
self.driver = nil
|
2018-07-03 20:52:28 +02:00
|
|
|
self.auto = false
|
2014-05-03 12:09:17 +02:00
|
|
|
clicker:set_detach()
|
2017-08-13 13:47:01 +02:00
|
|
|
player_api.player_attached[name] = false
|
|
|
|
player_api.set_animation(clicker, "stand" , 30)
|
2018-07-01 21:44:03 +02:00
|
|
|
local pos = clicker:get_pos()
|
2015-08-17 00:24:41 +02:00
|
|
|
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
|
|
|
|
minetest.after(0.1, function()
|
2018-07-01 21:44:03 +02:00
|
|
|
clicker:set_pos(pos)
|
2015-08-17 00:24:41 +02:00
|
|
|
end)
|
2014-05-03 12:09:17 +02:00
|
|
|
elseif not self.driver then
|
2015-10-28 16:41:15 +01:00
|
|
|
local attach = clicker:get_attach()
|
|
|
|
if attach and attach:get_luaentity() then
|
|
|
|
local luaentity = attach:get_luaentity()
|
|
|
|
if luaentity.driver then
|
|
|
|
luaentity.driver = nil
|
|
|
|
end
|
|
|
|
clicker:set_detach()
|
|
|
|
end
|
2014-05-03 12:09:17 +02:00
|
|
|
self.driver = clicker
|
2015-08-17 00:24:41 +02:00
|
|
|
clicker:set_attach(self.object, "",
|
2017-08-13 13:47:01 +02:00
|
|
|
{x = 0.5, y = 1, z = -3}, {x = 0, y = 0, z = 0})
|
|
|
|
player_api.player_attached[name] = true
|
2014-05-03 12:09:17 +02:00
|
|
|
minetest.after(0.2, function()
|
2017-08-13 13:47:01 +02:00
|
|
|
player_api.set_animation(clicker, "sit" , 30)
|
2014-05-03 12:09:17 +02:00
|
|
|
end)
|
2018-07-01 21:44:03 +02:00
|
|
|
clicker:set_look_horizontal(self.object:get_yaw())
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-07-06 10:39:16 +02:00
|
|
|
function boat.on_activate(self, staticdata, dtime_s)
|
2014-07-09 09:47:51 +02:00
|
|
|
self.object:set_armor_groups({immortal = 1})
|
2014-05-03 12:09:17 +02:00
|
|
|
if staticdata then
|
|
|
|
self.v = tonumber(staticdata)
|
|
|
|
end
|
2014-07-01 14:25:43 +02:00
|
|
|
self.last_v = self.v
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-07-09 09:47:51 +02:00
|
|
|
function boat.get_staticdata(self)
|
|
|
|
return tostring(self.v)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2015-10-28 16:41:15 +01:00
|
|
|
function boat.on_punch(self, puncher)
|
2014-07-06 11:12:48 +02:00
|
|
|
if not puncher or not puncher:is_player() or self.removed then
|
|
|
|
return
|
|
|
|
end
|
2014-10-03 03:20:31 +02:00
|
|
|
if self.driver and puncher == self.driver then
|
|
|
|
self.driver = nil
|
|
|
|
puncher:set_detach()
|
2017-08-13 13:47:01 +02:00
|
|
|
player_api.player_attached[puncher:get_player_name()] = false
|
2014-10-03 03:20:31 +02:00
|
|
|
end
|
|
|
|
if not self.driver then
|
|
|
|
self.removed = true
|
2016-11-29 03:28:55 +01:00
|
|
|
local inv = puncher:get_inventory()
|
2017-03-29 21:02:26 +02:00
|
|
|
if not (creative and creative.is_enabled_for
|
|
|
|
and creative.is_enabled_for(puncher:get_player_name()))
|
2016-11-29 03:28:55 +01:00
|
|
|
or not inv:contains_item("main", "boats:boat") then
|
|
|
|
local leftover = inv:add_item("main", "boats:boat")
|
|
|
|
-- if no room in inventory add a replacement boat to the world
|
|
|
|
if not leftover:is_empty() then
|
2018-07-01 21:44:03 +02:00
|
|
|
minetest.add_item(self.object:get_pos(), leftover)
|
2016-11-29 03:28:55 +01:00
|
|
|
end
|
|
|
|
end
|
2014-10-03 03:20:31 +02:00
|
|
|
-- delay remove to ensure player is detached
|
|
|
|
minetest.after(0.1, function()
|
|
|
|
self.object:remove()
|
|
|
|
end)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-07-06 10:39:16 +02:00
|
|
|
function boat.on_step(self, dtime)
|
2018-07-01 21:44:03 +02:00
|
|
|
self.v = get_v(self.object:get_velocity()) * get_sign(self.v)
|
2014-05-03 12:09:17 +02:00
|
|
|
if self.driver then
|
2018-07-03 20:52:28 +02:00
|
|
|
local driver_name = self.driver:get_player_name()
|
2014-05-03 12:09:17 +02:00
|
|
|
local ctrl = self.driver:get_player_control()
|
2018-07-03 20:52:28 +02:00
|
|
|
if ctrl.up and ctrl.down then
|
|
|
|
if not self.auto then
|
|
|
|
self.auto = true
|
|
|
|
minetest.chat_send_player(driver_name,
|
|
|
|
"[boats] Cruise on")
|
|
|
|
end
|
2014-10-04 05:44:23 +02:00
|
|
|
elseif ctrl.down then
|
2018-07-07 22:00:31 +02:00
|
|
|
self.v = self.v - dtime * 1.8
|
2018-07-03 20:52:28 +02:00
|
|
|
if self.auto then
|
|
|
|
self.auto = false
|
|
|
|
minetest.chat_send_player(driver_name,
|
|
|
|
"[boats] Cruise off")
|
|
|
|
end
|
|
|
|
elseif ctrl.up or self.auto then
|
2018-07-07 22:00:31 +02:00
|
|
|
self.v = self.v + dtime * 1.8
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
if ctrl.left then
|
2018-07-07 22:00:31 +02:00
|
|
|
if self.v < -0.001 then
|
|
|
|
self.object:set_yaw(self.object:get_yaw() - dtime * 0.9)
|
2014-05-03 12:09:17 +02:00
|
|
|
else
|
2018-07-07 22:00:31 +02:00
|
|
|
self.object:set_yaw(self.object:get_yaw() + dtime * 0.9)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
2014-10-04 05:44:23 +02:00
|
|
|
elseif ctrl.right then
|
2018-07-07 22:00:31 +02:00
|
|
|
if self.v < -0.001 then
|
|
|
|
self.object:set_yaw(self.object:get_yaw() + dtime * 0.9)
|
2014-05-03 12:09:17 +02:00
|
|
|
else
|
2018-07-07 22:00:31 +02:00
|
|
|
self.object:set_yaw(self.object:get_yaw() - dtime * 0.9)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-01 21:44:03 +02:00
|
|
|
local velo = self.object:get_velocity()
|
2014-08-07 12:14:27 +02:00
|
|
|
if self.v == 0 and velo.x == 0 and velo.y == 0 and velo.z == 0 then
|
2018-07-01 21:44:03 +02:00
|
|
|
self.object:set_pos(self.object:get_pos())
|
2014-07-01 14:25:43 +02:00
|
|
|
return
|
|
|
|
end
|
2014-05-03 12:09:17 +02:00
|
|
|
local s = get_sign(self.v)
|
2018-07-07 22:00:31 +02:00
|
|
|
self.v = self.v - dtime * 0.6 * s
|
2014-05-03 12:09:17 +02:00
|
|
|
if s ~= get_sign(self.v) then
|
2018-07-01 21:44:03 +02:00
|
|
|
self.object:set_velocity({x = 0, y = 0, z = 0})
|
2014-05-03 12:09:17 +02:00
|
|
|
self.v = 0
|
|
|
|
return
|
|
|
|
end
|
2016-04-15 11:43:10 +02:00
|
|
|
if math.abs(self.v) > 5 then
|
|
|
|
self.v = 5 * get_sign(self.v)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
2014-07-06 09:53:54 +02:00
|
|
|
|
2018-07-01 21:44:03 +02:00
|
|
|
local p = self.object:get_pos()
|
2014-07-09 09:47:51 +02:00
|
|
|
p.y = p.y - 0.5
|
2016-06-26 13:34:14 +02:00
|
|
|
local new_velo
|
2014-07-09 09:47:51 +02:00
|
|
|
local new_acce = {x = 0, y = 0, z = 0}
|
2014-05-03 12:09:17 +02:00
|
|
|
if not is_water(p) then
|
2018-02-26 19:47:18 +01:00
|
|
|
local nodedef = minetest.registered_nodes[minetest.get_node(p).name]
|
|
|
|
if (not nodedef) or nodedef.walkable then
|
2014-05-03 12:09:17 +02:00
|
|
|
self.v = 0
|
2014-08-07 12:14:27 +02:00
|
|
|
new_acce = {x = 0, y = 1, z = 0}
|
|
|
|
else
|
2014-10-04 05:44:23 +02:00
|
|
|
new_acce = {x = 0, y = -9.8, z = 0}
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
2018-07-01 21:44:03 +02:00
|
|
|
new_velo = get_velocity(self.v, self.object:get_yaw(),
|
|
|
|
self.object:get_velocity().y)
|
|
|
|
self.object:set_pos(self.object:get_pos())
|
2014-05-03 12:09:17 +02:00
|
|
|
else
|
2014-07-09 09:47:51 +02:00
|
|
|
p.y = p.y + 1
|
2014-05-03 12:09:17 +02:00
|
|
|
if is_water(p) then
|
2018-07-01 21:44:03 +02:00
|
|
|
local y = self.object:get_velocity().y
|
2016-04-15 11:43:10 +02:00
|
|
|
if y >= 5 then
|
|
|
|
y = 5
|
2014-10-04 05:44:23 +02:00
|
|
|
elseif y < 0 then
|
|
|
|
new_acce = {x = 0, y = 20, z = 0}
|
|
|
|
else
|
|
|
|
new_acce = {x = 0, y = 5, z = 0}
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
2018-07-01 21:44:03 +02:00
|
|
|
new_velo = get_velocity(self.v, self.object:get_yaw(), y)
|
|
|
|
self.object:set_pos(self.object:get_pos())
|
2014-05-03 12:09:17 +02:00
|
|
|
else
|
2014-07-09 09:47:51 +02:00
|
|
|
new_acce = {x = 0, y = 0, z = 0}
|
2018-07-01 21:44:03 +02:00
|
|
|
if math.abs(self.object:get_velocity().y) < 1 then
|
|
|
|
local pos = self.object:get_pos()
|
2014-07-09 09:47:51 +02:00
|
|
|
pos.y = math.floor(pos.y) + 0.5
|
2018-07-01 21:44:03 +02:00
|
|
|
self.object:set_pos(pos)
|
|
|
|
new_velo = get_velocity(self.v, self.object:get_yaw(), 0)
|
2014-05-03 12:09:17 +02:00
|
|
|
else
|
2018-07-01 21:44:03 +02:00
|
|
|
new_velo = get_velocity(self.v, self.object:get_yaw(),
|
|
|
|
self.object:get_velocity().y)
|
|
|
|
self.object:set_pos(self.object:get_pos())
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-01 21:44:03 +02:00
|
|
|
self.object:set_velocity(new_velo)
|
|
|
|
self.object:set_acceleration(new_acce)
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
minetest.register_entity("boats:boat", boat)
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
minetest.register_craftitem("boats:boat", {
|
|
|
|
description = "Boat",
|
2016-02-01 18:30:45 +01:00
|
|
|
inventory_image = "boats_inventory.png",
|
|
|
|
wield_image = "boats_wield.png",
|
2014-07-09 09:47:51 +02:00
|
|
|
wield_scale = {x = 2, y = 2, z = 1},
|
2014-05-03 12:09:17 +02:00
|
|
|
liquids_pointable = true,
|
2016-10-24 20:34:00 +02:00
|
|
|
groups = {flammable = 2},
|
2014-07-06 09:53:54 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
on_place = function(itemstack, placer, pointed_thing)
|
2017-02-12 01:23:43 +01:00
|
|
|
local under = pointed_thing.under
|
|
|
|
local node = minetest.get_node(under)
|
|
|
|
local udef = minetest.registered_nodes[node.name]
|
|
|
|
if udef and udef.on_rightclick and
|
2017-10-01 15:41:58 +02:00
|
|
|
not (placer and placer:is_player() and
|
|
|
|
placer:get_player_control().sneak) then
|
2017-02-12 01:23:43 +01:00
|
|
|
return udef.on_rightclick(under, node, placer, itemstack,
|
|
|
|
pointed_thing) or itemstack
|
|
|
|
end
|
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
if pointed_thing.type ~= "node" then
|
2016-07-01 22:43:02 +02:00
|
|
|
return itemstack
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
|
|
|
if not is_water(pointed_thing.under) then
|
2016-07-01 22:43:02 +02:00
|
|
|
return itemstack
|
2014-05-03 12:09:17 +02:00
|
|
|
end
|
2014-07-09 09:47:51 +02:00
|
|
|
pointed_thing.under.y = pointed_thing.under.y + 0.5
|
2016-11-15 22:08:12 +01:00
|
|
|
boat = minetest.add_entity(pointed_thing.under, "boats:boat")
|
2016-12-21 08:38:27 +01:00
|
|
|
if boat then
|
2017-10-01 15:41:58 +02:00
|
|
|
if placer then
|
2018-07-01 21:44:03 +02:00
|
|
|
boat:set_yaw(placer:get_look_horizontal())
|
2017-10-01 15:41:58 +02:00
|
|
|
end
|
|
|
|
local player_name = placer and placer:get_player_name() or ""
|
|
|
|
if not (creative and creative.is_enabled_for and
|
|
|
|
creative.is_enabled_for(player_name)) then
|
2016-12-21 08:38:27 +01:00
|
|
|
itemstack:take_item()
|
|
|
|
end
|
2014-07-01 14:25:43 +02:00
|
|
|
end
|
2014-05-03 12:09:17 +02:00
|
|
|
return itemstack
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2015-08-17 00:24:41 +02:00
|
|
|
|
2014-05-03 12:09:17 +02:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "boats:boat",
|
|
|
|
recipe = {
|
2014-07-09 09:47:51 +02:00
|
|
|
{"", "", "" },
|
|
|
|
{"group:wood", "", "group:wood"},
|
2014-05-03 12:09:17 +02:00
|
|
|
{"group:wood", "group:wood", "group:wood"},
|
|
|
|
},
|
|
|
|
})
|
Add and edit fuel registrations
Edited fuels: fences, wooden ladder.
New fuels:
boat, paper, book, dry shrub, stick, vessels shelf, wooden tools,
wooden door, trapdoor, fence gates, farming crops, stairs, slabs, beds.
2016-10-30 11:29:58 +01:00
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "fuel",
|
|
|
|
recipe = "boats:boat",
|
|
|
|
burntime = 20,
|
|
|
|
})
|