2018-12-20 18:52:00 +01:00

86 lines
2.4 KiB
Lua

--[[
Hyperloop Mod
=============
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
Station reservation/blocking and trip booking
]]--
-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta
-- Load support for intllib.
local MP = minetest.get_modpath("hyperloop")
local I, NS = dofile(MP.."/intllib.lua")
local tBlockingTime = {}
local tBookings = {} -- open bookings: tBookings[S(departure_pos)] = arrival_pos
local Stations = hyperloop.Stations
-- Reserve departure and arrival stations for some time
function hyperloop.reserve(departure_pos, arrival_pos, player)
if Stations:get(departure_pos) == nil then
hyperloop.chat(player, I("Station data is corrupted. Please rebuild the station!"))
return false
elseif Stations:get(arrival_pos) == nil then
hyperloop.chat(player, I("Station data is corrupted. Please rebuild the station!"))
return false
end
if (tBlockingTime[S(departure_pos)] or 0) > minetest.get_gametime() then
hyperloop.chat(player, I("Station is still blocked. Please try again in a few seconds!"))
return false
elseif (tBlockingTime[S(arrival_pos)] or 0) > minetest.get_gametime() then
hyperloop.chat(player, I("Station is still blocked. Please try again in a few seconds!"))
return false
end
-- place a reservation for 20 seconds to start the trip
tBlockingTime[S(departure_pos)] = minetest.get_gametime() + 20
tBlockingTime[S(arrival_pos)] = minetest.get_gametime() + 20
return true
end
-- block the already reserved stations
function hyperloop.block(departure_pos, arrival_pos, seconds)
if Stations:get(departure_pos) == nil then
return false
elseif Stations:get(arrival_pos) == nil then
return false
end
tBlockingTime[S(departure_pos)] = minetest.get_gametime() + seconds
tBlockingTime[S(arrival_pos)] = minetest.get_gametime() + seconds
return true
end
-- check if station is blocked
function hyperloop.is_blocked(pos)
if not pos then return false end
if Stations:get(pos) == nil then
return false
end
return (tBlockingTime[S(pos)] or 0) > minetest.get_gametime()
end
function hyperloop.set_arrival(departure_pos, arrival_pos)
tBookings[S(departure_pos)] = arrival_pos
end
function hyperloop.get_arrival(departure_pos)
-- Return and delete the arrival pos
local arrival_pos = tBookings[S(departure_pos)]
tBookings[S(departure_pos)] = nil
return arrival_pos
end