2018-09-03 06:56:16 -07:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Airshuttle
|
|
|
|
==========
|
|
|
|
|
|
|
|
Copyright (C) 2018 Joachim Stolberg
|
|
|
|
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2018-09-15 08:34:06 -07:00
|
|
|
local DBG = function(...) end
|
|
|
|
--local DBG = print
|
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
-- used to detect a server restart to re-initialize the launcher node
|
|
|
|
local ServerRestart = {}
|
|
|
|
|
2018-09-03 06:56:16 -07:00
|
|
|
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",
|
2018-09-14 12:11:54 -07:00
|
|
|
visual_size = {x = 2, y = 2},
|
2018-09-03 06:56:16 -07:00
|
|
|
textures = {"airshuttle:airshuttle_nodebox"},
|
|
|
|
},
|
|
|
|
|
|
|
|
-- Custom fields
|
2018-09-14 12:11:54 -07:00
|
|
|
driver = nil, -- drivers name
|
2018-09-03 06:56:16 -07:00
|
|
|
removed = false,
|
2018-09-03 15:40:55 -07:00
|
|
|
speedH = 0,
|
|
|
|
speedV = 0,
|
2018-09-14 12:11:54 -07:00
|
|
|
rot = 0, -- rotation
|
2018-09-07 12:21:47 -07:00
|
|
|
timer = 0,
|
2018-09-03 06:56:16 -07:00
|
|
|
on_trip = false,
|
|
|
|
dest_pos = nil,
|
|
|
|
start_pos = nil,
|
2018-09-03 15:40:55 -07:00
|
|
|
dest_approach = false,
|
2018-09-03 06:56:16 -07:00
|
|
|
}
|
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
local function landed(self)
|
|
|
|
if self.speedH < 0.1 then
|
|
|
|
local pos = self.object:getpos()
|
|
|
|
pos.y = pos.y - 1.5
|
|
|
|
if minetest.get_node(pos).name ~= "air" then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function remove_airshuttle(self)
|
|
|
|
self.removed = true
|
|
|
|
self.on_trip = false
|
|
|
|
if self.pos then
|
|
|
|
local meta = minetest.get_meta(self.pos)
|
|
|
|
meta:set_int("busy", 0)
|
|
|
|
end
|
|
|
|
-- delay remove to ensure player is detached
|
|
|
|
minetest.after(0.1, function()
|
|
|
|
self.object:remove()
|
|
|
|
end)
|
2018-09-15 08:34:06 -07:00
|
|
|
DBG("airshuttle removed")
|
2018-09-14 12:11:54 -07:00
|
|
|
end
|
|
|
|
|
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-14 12:11:54 -07:00
|
|
|
if self.driver and name == self.driver and landed(self) then
|
2018-09-03 06:56:16 -07:00
|
|
|
-- Detach
|
|
|
|
self.driver = nil
|
|
|
|
self.on_trip = false
|
|
|
|
clicker:set_detach()
|
|
|
|
default.player_attached[name] = false
|
2018-09-14 12:11:54 -07:00
|
|
|
default.player_set_animation(clicker, "stand" , 30)
|
2018-09-15 08:34:06 -07:00
|
|
|
clicker:set_attribute("airshuttle_start_pos", nil)
|
2018-09-14 12:11:54 -07:00
|
|
|
local pos = clicker:getpos()
|
|
|
|
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
|
2018-09-03 06:56:16 -07:00
|
|
|
minetest.after(0.1, function()
|
|
|
|
clicker:setpos(pos)
|
|
|
|
end)
|
2018-09-14 12:11:54 -07:00
|
|
|
remove_airshuttle(self)
|
2018-09-15 08:34:06 -07:00
|
|
|
minetest.log("action", clicker:get_player_name().." detaches from airshuttle at "..
|
|
|
|
minetest.pos_to_string(pos))
|
2018-09-03 06:56:16 -07:00
|
|
|
elseif not self.driver then
|
|
|
|
-- Attach
|
2018-09-14 12:11:54 -07:00
|
|
|
self.on_trip = true
|
|
|
|
local spos = minetest.pos_to_string(clicker:get_pos())
|
|
|
|
clicker:set_attribute("airshuttle_start_pos", spos)
|
|
|
|
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
|
|
|
|
self.driver = clicker:get_player_name()
|
|
|
|
clicker:set_attach(self.object, "",
|
|
|
|
{x = 0, y = 6, z = 0}, {x = 0, y = 0, z = 0})
|
|
|
|
default.player_attached[name] = true
|
|
|
|
minetest.after(0.2, function()
|
|
|
|
default.player_set_animation(clicker, "stand" , 30)
|
|
|
|
end)
|
|
|
|
clicker:set_look_horizontal(self.object:getyaw())
|
2018-09-15 08:34:06 -07:00
|
|
|
minetest.log("action", clicker:get_player_name().." attaches to airshuttle at "..
|
|
|
|
minetest.pos_to_string(clicker:getpos()))
|
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
|
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
if self.on_trip then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-03 06:56:16 -07:00
|
|
|
local name = puncher:get_player_name()
|
2018-09-14 12:11:54 -07:00
|
|
|
if self.driver and name == self.driver then
|
|
|
|
self.driver = nil
|
|
|
|
puncher:set_detach()
|
|
|
|
default.player_attached[name] = false
|
|
|
|
end
|
2018-09-03 06:56:16 -07:00
|
|
|
if not self.driver then
|
2018-09-14 12:11:54 -07:00
|
|
|
remove_airshuttle(self)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function AirshuttleEntity.on_step(self, dtime)
|
2018-09-07 12:21:47 -07:00
|
|
|
self.timer = (self.timer or 0) + dtime
|
2018-09-14 12:11:54 -07:00
|
|
|
if 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
|
2018-09-15 08:34:06 -07:00
|
|
|
return
|
2018-09-03 15:40:55 -07:00
|
|
|
end
|
|
|
|
|
2018-09-15 08:34:06 -07:00
|
|
|
if airshuttle.player_gone(self.driver) then
|
2018-09-14 12:11:54 -07:00
|
|
|
remove_airshuttle(self)
|
2018-09-15 08:34:06 -07:00
|
|
|
return
|
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
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
if airshuttle.remote_control(self) then
|
2018-09-03 06:56:16 -07:00
|
|
|
self.object:setpos(self.object:getpos())
|
2018-09-14 12:11:54 -07:00
|
|
|
self.object:setvelocity(get_velocity(self.speedH, self.object:getyaw(), self.speedV))
|
|
|
|
self.object:setyaw(self.object:getyaw() + 1 * self.rot)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
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-13 13:03:47 -07:00
|
|
|
groups = {not_in_creative_inventory = 1},
|
2018-09-03 06:56:16 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
-- 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-13 13:03:47 -07:00
|
|
|
groups = {cracky = 3, oddly_breakable_by_hand = 3, not_in_creative_inventory = 1},
|
2018-09-03 06:56:16 -07:00
|
|
|
})
|
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
local function place_shuttle(pos, owner, facedir, route_id)
|
|
|
|
pos.y = pos.y + 1
|
|
|
|
local airshuttle_entity = minetest.add_entity(pos, "airshuttle:airshuttle")
|
|
|
|
if airshuttle_entity 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)
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
2018-09-14 12:11:54 -07:00
|
|
|
pos.y = pos.y - 1
|
|
|
|
self.owner = owner
|
|
|
|
self.pos = table.copy(pos)
|
|
|
|
self.route_id = route_id
|
2018-09-03 06:56:16 -07:00
|
|
|
end
|
|
|
|
end
|
2018-09-03 15:40:55 -07:00
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
minetest.register_node("airshuttle:launcher", {
|
|
|
|
description = "AirShuttle Launcher",
|
|
|
|
drawtype = "node",
|
|
|
|
tiles = {"airshuttle_launcher.png"},
|
|
|
|
|
|
|
|
-- switch ON/OFF
|
|
|
|
on_rightclick = function (pos, node, clicker)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local busy = meta:get_int("busy")
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
local route_id = meta:get_int("route_id")
|
|
|
|
if busy == 0 or ServerRestart[route_id] == nil then
|
|
|
|
ServerRestart[route_id] = true
|
|
|
|
place_shuttle(pos, owner, node.param2, route_id)
|
|
|
|
meta:set_int("busy", 1)
|
2018-09-26 01:58:05 -07:00
|
|
|
minetest.get_node_timer(pos):start(60*10)
|
2018-09-14 12:11:54 -07:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2018-09-26 01:58:05 -07:00
|
|
|
on_timer = function(pos, elapsed)
|
|
|
|
print("timer")
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_int("busy", 0)
|
|
|
|
return false
|
|
|
|
end,
|
|
|
|
|
2018-09-14 12:11:54 -07:00
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local route_id = airshuttle.get_next_id(placer:get_player_name())
|
|
|
|
if route_id then
|
|
|
|
meta:set_int("route_id", route_id)
|
|
|
|
meta:set_int("busy", 0)
|
|
|
|
meta:set_string("owner", placer:get_player_name())
|
|
|
|
meta:set_string("infotext", "AirShuttle Launcher (ID "..route_id..")")
|
2018-09-15 08:34:06 -07:00
|
|
|
minetest.log("action", placer:get_player_name().." places airshuttle:launcher at "..
|
|
|
|
minetest.pos_to_string(pos))
|
2018-09-14 12:11:54 -07:00
|
|
|
else
|
|
|
|
minetest.chat_send_player(placer:get_player_name(), "[AirShuttle] Number of Launcher exceeded!")
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
|
|
|
local route_id = tonumber(oldmetadata["fields"]["route_id"])
|
|
|
|
airshuttle.delete_id(digger:get_player_name(), route_id)
|
|
|
|
end,
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
is_ground_content = false,
|
|
|
|
groups = {cracky = 1},
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
|
|
|
})
|