techage_modpack/hyperloop/booking.lua

85 lines
2.4 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
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 SP = function(pos) if pos then return minetest.pos_to_string(pos) end end
2024-08-25 20:49:57 +03:00
--local P = minetest.string_to_pos
--local M = minetest.get_meta
2020-05-31 23:31:18 +03:00
local S = hyperloop.S
2024-08-25 20:49:57 +03:00
--local NS = hyperloop.NS
2020-05-31 23:31:18 +03:00
local tBlockingTime = {}
local tBookings = {} -- open bookings: tBookings[SP(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, S("Station data is corrupted. Please rebuild the station!"))
return false
elseif Stations:get(arrival_pos) == nil then
hyperloop.chat(player, S("Station data is corrupted. Please rebuild the station!"))
return false
end
2024-08-25 20:49:57 +03:00
2020-05-31 23:31:18 +03:00
if (tBlockingTime[SP(departure_pos)] or 0) > minetest.get_gametime() then
hyperloop.chat(player, S("Station is still blocked. Please try again in a few seconds!"))
return false
elseif (tBlockingTime[SP(arrival_pos)] or 0) > minetest.get_gametime() then
hyperloop.chat(player, S("Station is still blocked. Please try again in a few seconds!"))
return false
end
2024-08-25 20:49:57 +03:00
2020-05-31 23:31:18 +03:00
-- place a reservation for 20 seconds to start the trip
tBlockingTime[SP(departure_pos)] = minetest.get_gametime() + 20
tBlockingTime[SP(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
2024-08-25 20:49:57 +03:00
2020-05-31 23:31:18 +03:00
tBlockingTime[SP(departure_pos)] = minetest.get_gametime() + seconds
tBlockingTime[SP(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
2024-08-25 20:49:57 +03:00
2020-05-31 23:31:18 +03:00
return (tBlockingTime[SP(pos)] or 0) > minetest.get_gametime()
end
function hyperloop.set_arrival(departure_pos, arrival_pos)
tBookings[SP(departure_pos)] = arrival_pos
2024-08-25 20:49:57 +03:00
end
2020-05-31 23:31:18 +03:00
function hyperloop.get_arrival(departure_pos)
-- Return and delete the arrival pos
local arrival_pos = tBookings[SP(departure_pos)]
tBookings[SP(departure_pos)] = nil
return arrival_pos
end