2018-09-03 06:56:16 -07:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Airshuttle
|
|
|
|
==========
|
|
|
|
|
|
|
|
Copyright (C) 2018 Joachim Stolberg
|
|
|
|
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local function get_sign(i)
|
|
|
|
if i == 0 then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return i / math.abs(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_velocity(v, yaw, y)
|
|
|
|
local x = -math.sin(yaw) * v
|
|
|
|
local z = math.cos(yaw) * v
|
|
|
|
return {x = x, y = y, z = z}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_v(v)
|
|
|
|
return math.sqrt(v.x ^ 2 + v.z ^ 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Airshuttle entity
|
|
|
|
--
|
|
|
|
local AirshuttleEntity = {
|
|
|
|
initial_properties = {
|
|
|
|
physical = true,
|
|
|
|
collide_with_objects = false, -- Workaround fix for a MT engine bug
|
|
|
|
collisionbox = {-0.85, -0.40, -0.85, 0.85, 0.85, 0.85},
|
|
|
|
visual = "wielditem",
|
|
|
|
visual_size = {x = 2, y = 2}, -- Scale up of nodebox is these * 1.5
|
|
|
|
textures = {"airshuttle:airshuttle_nodebox"},
|
|
|
|
},
|
|
|
|
|
|
|
|
-- Custom fields
|
|
|
|
driver = nil,
|
|
|
|
removed = false,
|
2018-09-03 15:40:55 -07:00
|
|
|
speedH = 0,
|
|
|
|
speedV = 0,
|
2018-09-03 06:56:16 -07:00
|
|
|
rot = 0,
|
2018-09-07 12:21:47 -07:00
|
|
|
timer = 0,
|
2018-09-03 06:56:16 -07:00
|
|
|
auto = false,
|
|
|
|
on_trip = false,
|
|
|
|
dest_pos = nil,
|
|
|
|
start_pos = nil,
|
2018-09-03 15:40:55 -07:00
|
|
|
dest_approach = false,
|
2018-09-07 12:21:47 -07:00
|
|
|
remote_controlled = false,
|
2018-09-03 06:56:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function AirshuttleEntity.on_rightclick(self, clicker)
|
|
|
|
if not clicker or not clicker:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local name = clicker:get_player_name()
|
2018-09-03 15:40:55 -07:00
|
|
|
if self.driver and name == self.driver and self.speedH < 0.1 then
|
2018-09-03 06:56:16 -07:00
|
|
|
local pos = self.object:getpos()
|
|
|
|
pos.y = pos.y - 1.5
|
|
|
|
if minetest.get_node(pos).name == "air" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Detach
|
|
|
|
self.driver = nil
|
|
|
|
self.auto = false
|
|
|
|
self.on_trip = false
|
|
|
|
clicker:set_detach()
|
|
|
|
default.player_attached[name] = false
|
|
|
|
pos = clicker:getpos()
|
|
|
|
minetest.after(0.1, function()
|
|
|
|
clicker:setpos(pos)
|
|
|
|
end)
|
2018-09-03 15:40:55 -07:00
|
|
|
print("remove_airshuttle")
|
2018-09-07 12:21:47 -07:00
|
|
|
airshuttle.remove_airshuttle(self)
|
2018-09-03 06:56:16 -07:00
|
|
|
elseif not self.driver then
|
|
|
|
-- Attach
|
2018-09-07 12:21:47 -07:00
|
|
|
airshuttle.start_fly(self, clicker, nil)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function AirshuttleEntity.on_activate(self, staticdata, dtime_s)
|
|
|
|
self.object:set_armor_groups({immortal = 1})
|
|
|
|
end
|
|
|
|
|
|
|
|
function AirshuttleEntity.on_punch(self, puncher)
|
|
|
|
if not puncher or not puncher:is_player() or self.removed then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = puncher:get_player_name()
|
|
|
|
if not self.driver then
|
|
|
|
if name == self.owner
|
|
|
|
or self.owner == ""
|
|
|
|
or self.owner == nil
|
|
|
|
or minetest.check_player_privs(name, "server") then
|
2018-09-07 12:21:47 -07:00
|
|
|
airshuttle.remove_airshuttle(self)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function AirshuttleEntity.on_step(self, dtime)
|
2018-09-07 12:21:47 -07:00
|
|
|
self.timer = (self.timer or 0) + dtime
|
|
|
|
if self.remote_controlled and self.timer < 0.3 then
|
2018-09-03 06:56:16 -07:00
|
|
|
return
|
|
|
|
end
|
2018-09-07 12:21:47 -07:00
|
|
|
self.timer = 0
|
2018-09-03 06:56:16 -07:00
|
|
|
|
2018-09-03 15:40:55 -07:00
|
|
|
if not self.on_trip then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-09-07 12:21:47 -07:00
|
|
|
if not self.driver then
|
|
|
|
airshuttle.remove_airshuttle(self)
|
|
|
|
return false
|
|
|
|
end
|
2018-09-03 15:40:55 -07:00
|
|
|
|
2018-09-07 12:21:47 -07:00
|
|
|
local driver_objref = minetest.get_player_by_name(self.driver)
|
|
|
|
if not driver_objref then
|
|
|
|
airshuttle.remove_airshuttle(self)
|
|
|
|
return false
|
2018-09-03 15:40:55 -07:00
|
|
|
end
|
2018-09-07 12:21:47 -07:00
|
|
|
|
2018-09-03 15:40:55 -07:00
|
|
|
self.speedH = get_v(self.object:getvelocity()) * get_sign(self.speedH)
|
|
|
|
self.speedV = self.object:getvelocity().y
|
2018-09-03 06:56:16 -07:00
|
|
|
|
|
|
|
-- Controls
|
2018-09-07 12:21:47 -07:00
|
|
|
if self.remote_controlled then
|
2018-09-03 06:56:16 -07:00
|
|
|
airshuttle.remote_control(self)
|
|
|
|
dtime = 0
|
|
|
|
else
|
2018-09-07 12:21:47 -07:00
|
|
|
if not airshuttle.user_control(self, driver_objref) then return end
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Early return for stationary vehicle
|
2018-09-03 15:40:55 -07:00
|
|
|
if self.speedH == 0 and self.rot == 0 and self.speedV == 0 then
|
2018-09-03 06:56:16 -07:00
|
|
|
self.object:setpos(self.object:getpos())
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local new_acce = {x = 0, y = 0, z = 0}
|
2018-09-07 12:21:47 -07:00
|
|
|
-- -- Bouyancy in liquids
|
|
|
|
-- local p = self.object:getpos()
|
|
|
|
-- p.y = p.y - 1.5
|
|
|
|
-- local def = minetest.registered_nodes[minetest.get_node(p).name]
|
|
|
|
-- if def and (def.liquidtype == "source" or def.liquidtype == "flowing") then
|
|
|
|
-- new_acce = {x = 0, y = 10, z = 0}
|
|
|
|
-- end
|
|
|
|
self.object:setpos(self.object:getpos())
|
2018-09-03 15:40:55 -07:00
|
|
|
self.object:setvelocity(get_velocity(self.speedH, self.object:getyaw(), self.speedV))
|
2018-09-03 06:56:16 -07:00
|
|
|
self.object:setacceleration(new_acce)
|
|
|
|
self.object:setyaw(self.object:getyaw() + (1 + dtime) * self.rot)
|
|
|
|
end
|
|
|
|
|
2018-09-03 15:40:55 -07:00
|
|
|
|
2018-09-03 06:56:16 -07:00
|
|
|
minetest.register_entity("airshuttle:airshuttle", AirshuttleEntity)
|
|
|
|
|
|
|
|
|
|
|
|
-- Craftitem
|
|
|
|
|
|
|
|
minetest.register_craftitem("airshuttle:airshuttle", {
|
2018-09-03 15:40:55 -07:00
|
|
|
description = "AirShuttle",
|
2018-09-08 12:05:13 -07:00
|
|
|
inventory_image = "airshuttle_launcher.png",
|
2018-09-03 06:56:16 -07:00
|
|
|
liquids_pointable = true,
|
2018-09-07 12:21:47 -07:00
|
|
|
--groups = {not_in_creative_inventory = 1},
|
2018-09-03 15:40:55 -07:00
|
|
|
|
2018-09-03 06:56:16 -07:00
|
|
|
on_place = function(itemstack, placer, pointed_thing)
|
|
|
|
local under = pointed_thing.under
|
|
|
|
local node = minetest.get_node(under)
|
|
|
|
local udef = minetest.registered_nodes[node.name]
|
|
|
|
|
|
|
|
-- Run any on_rightclick function of pointed node instead
|
|
|
|
if udef and udef.on_rightclick and
|
|
|
|
not (placer and placer:is_player() and
|
|
|
|
placer:get_player_control().sneak) then
|
|
|
|
return udef.on_rightclick(under, node, placer, itemstack,
|
|
|
|
pointed_thing) or itemstack
|
|
|
|
end
|
|
|
|
|
|
|
|
if pointed_thing.type ~= "node" then
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
|
|
|
pointed_thing.under.y = pointed_thing.under.y + 1
|
|
|
|
local airshuttle = minetest.add_entity(pointed_thing.under,
|
|
|
|
"airshuttle:airshuttle")
|
|
|
|
if airshuttle then
|
|
|
|
if placer then
|
|
|
|
airshuttle:setyaw(placer:get_look_horizontal())
|
2018-09-07 12:21:47 -07:00
|
|
|
local self = airshuttle:get_luaentity()
|
|
|
|
self.owner = placer and placer:get_player_name() or ""
|
|
|
|
self.pos = table.copy(pointed_thing.under)
|
2018-09-03 15:40:55 -07:00
|
|
|
itemstack:take_item()
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
-- Nodebox for entity wielditem visual
|
|
|
|
|
|
|
|
minetest.register_node("airshuttle:airshuttle_nodebox", {
|
2018-09-03 15:40:55 -07:00
|
|
|
description = "AirShuttle",
|
2018-09-03 06:56:16 -07:00
|
|
|
tiles = { -- Top, base, right, left, front, back
|
|
|
|
"airshuttle_top.png",
|
|
|
|
"airshuttle_base.png",
|
|
|
|
"airshuttle_right.png",
|
|
|
|
"airshuttle_left.png",
|
|
|
|
"airshuttle_front.png",
|
|
|
|
"airshuttle_back.png",
|
|
|
|
},
|
|
|
|
paramtype = "light",
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
-- Widmin, heimin, lenmin, widmax, heimax, lenmax
|
|
|
|
{-6/24, -5/24, -12/24, 6/24, 6/24, 12/24}, -- Envelope
|
|
|
|
{-1/48, 6/24, -15/24, 1/48, 12/24, -3/24}, -- Top fin
|
|
|
|
{-12/24, 0/24, -15/24, -6/24, 1/24, -3/24}, -- Left fin
|
|
|
|
{ 6/24, 0/24, -15/24, 12/24, 1/24, -3/24}, -- Right fin
|
|
|
|
},
|
|
|
|
},
|
2018-09-07 12:21:47 -07:00
|
|
|
groups = {cracky = 3, oddly_breakable_by_hand = 3},
|
2018-09-03 06:56:16 -07:00
|
|
|
})
|
|
|
|
|
2018-09-08 12:05:13 -07:00
|
|
|
|
2018-09-07 12:21:47 -07:00
|
|
|
function airshuttle.remove_airshuttle(self)
|
2018-09-03 15:40:55 -07:00
|
|
|
if not self.removed then
|
2018-09-03 06:56:16 -07:00
|
|
|
self.removed = true
|
2018-09-03 15:40:55 -07:00
|
|
|
self.on_trip = false
|
|
|
|
if self.pos then
|
|
|
|
local meta = minetest.get_meta(self.pos)
|
|
|
|
meta:set_int("busy", 0)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
2018-09-03 15:40:55 -07:00
|
|
|
minetest.after(0.1, function()
|
|
|
|
self.object:remove()
|
|
|
|
end)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
end
|
2018-09-03 15:40:55 -07:00
|
|
|
|
2018-09-07 12:21:47 -07:00
|
|
|
function airshuttle.start_fly(self, player, route_id)
|
2018-09-03 15:40:55 -07:00
|
|
|
local attach = player:get_attach()
|
|
|
|
local name = player and player:get_player_name() or ""
|
|
|
|
if attach and attach:get_luaentity() then
|
|
|
|
local luaentity = attach:get_luaentity()
|
|
|
|
if luaentity.driver then
|
|
|
|
luaentity.driver = nil
|
|
|
|
end
|
|
|
|
player:set_detach()
|
|
|
|
end
|
2018-09-07 12:21:47 -07:00
|
|
|
if route_id then
|
|
|
|
self.route_id = route_id
|
|
|
|
self.remote_controlled = true
|
|
|
|
else
|
|
|
|
self.remote_controlled = false
|
|
|
|
end
|
2018-09-03 15:40:55 -07:00
|
|
|
self.on_trip = true
|
2018-09-07 12:21:47 -07:00
|
|
|
self.driver = name
|
2018-09-03 15:40:55 -07:00
|
|
|
player:set_attach(self.object, "",
|
|
|
|
{x = 0, y = 6, z = 0}, {x = 0, y = 0, z = 0})
|
|
|
|
default.player_attached[name] = true
|
|
|
|
player:set_look_horizontal(self.object:getyaw())
|
|
|
|
end
|
|
|
|
|
2018-09-07 12:21:47 -07:00
|
|
|
function airshuttle.place_shuttle(pos, player, owner, facedir, route_id)
|
2018-09-03 15:40:55 -07:00
|
|
|
pos.y = pos.y + 1
|
|
|
|
local airshuttle_entity = minetest.add_entity(pos, "airshuttle:airshuttle")
|
|
|
|
if airshuttle_entity and player then
|
|
|
|
local self = airshuttle_entity:get_luaentity()
|
|
|
|
if facedir then
|
|
|
|
local dir = minetest.facedir_to_dir(facedir)
|
|
|
|
local yaw = minetest.dir_to_yaw(dir)
|
|
|
|
airshuttle_entity:set_yaw(yaw)
|
|
|
|
player:set_look_horizontal(yaw)
|
|
|
|
else
|
|
|
|
airshuttle_entity:set_yaw(player:get_look_horizontal())
|
|
|
|
end
|
|
|
|
self.owner = owner
|
|
|
|
pos.y = pos.y - 1
|
|
|
|
self.pos = table.copy(pos)
|
2018-09-07 12:21:47 -07:00
|
|
|
airshuttle.start_fly(self, player, route_id)
|
2018-09-03 15:40:55 -07:00
|
|
|
end
|
|
|
|
end
|