station blocking/reservation added
This commit is contained in:
parent
08728f5365
commit
2fb43296fb
40
booking.lua
40
booking.lua
@ -15,12 +15,21 @@
|
||||
|
||||
|
||||
function hyperloop.update_all_booking_machines()
|
||||
if hyperloop.debugging then
|
||||
print("update_all_booking_machines")
|
||||
end
|
||||
local t = minetest.get_us_time()
|
||||
for _, dataset in pairs(hyperloop.tAllStations) do
|
||||
if dataset.booking_pos ~= nil then
|
||||
local pos = minetest.string_to_pos(dataset.booking_pos)
|
||||
minetest.registered_nodes["hyperloop:booking"].update(pos)
|
||||
break--------------------------TODO
|
||||
end
|
||||
end
|
||||
t = minetest.get_us_time() - t
|
||||
if hyperloop.debugging then
|
||||
print("time="..t)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -104,19 +113,23 @@ minetest.register_node("hyperloop:booking", {
|
||||
meta:set_string("station_name", station_name)
|
||||
meta:set_string("infotext", "Station: "..station_name)
|
||||
meta:set_string("formspec", formspec(station_name))
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
else
|
||||
minetest.chat_send_player(player:get_player_name(), "Error: Invalid station name!")
|
||||
end
|
||||
-- destination selected?
|
||||
elseif fields.button ~= nil then
|
||||
local station_name = meta:get_string("station_name")
|
||||
-- place booking
|
||||
local idx = tonumber(fields.button)
|
||||
local destination = get_station_list(station_name)[idx]
|
||||
hyperloop.booking[station_name] = destination
|
||||
-- open the pod door
|
||||
hyperloop.open_pod_door(station_name)
|
||||
-- place booking of not already blocked
|
||||
if hyperloop.reserve(station_name, destination) then
|
||||
hyperloop.booking[station_name] = destination
|
||||
-- open the pod door
|
||||
hyperloop.open_pod_door(station_name)
|
||||
else
|
||||
minetest.chat_send_player(player:get_player_name(), "Station is still blocked. Please try again in a view seconds!")
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
@ -130,9 +143,6 @@ minetest.register_node("hyperloop:booking", {
|
||||
end,
|
||||
|
||||
update = function(pos)
|
||||
if hyperloop.debugging then
|
||||
print("Booking machine update")
|
||||
end
|
||||
local meta = minetest.get_meta(pos)
|
||||
local station_name = meta:get_string("station_name")
|
||||
local stations = get_station_list(station_name)
|
||||
@ -145,5 +155,17 @@ minetest.register_node("hyperloop:booking", {
|
||||
is_ground_content = false,
|
||||
})
|
||||
|
||||
|
||||
minetest.register_abm({
|
||||
nodenames = {"hyperloop:booking"},
|
||||
interval = 10.0, -- Run every 10 seconds
|
||||
chance = 1,
|
||||
action = function(pos, node, active_object_count, active_object_count_wider)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local station_name = meta:get_string("station_name") or nil
|
||||
if station_name ~= nil and hyperloop.tAllStations[station_name] ~= nil then
|
||||
local stations = get_station_list(station_name)
|
||||
meta:set_string("formspec", formspec(station_name, stations))
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
|
4
door.lua
4
door.lua
@ -80,6 +80,7 @@ minetest.register_node("hyperloop:doorTopPassive", {
|
||||
},
|
||||
paramtype2 = "facedir",
|
||||
diggable = false,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
groups = {cracky=1, not_in_creative_inventory=1},
|
||||
is_ground_content = false,
|
||||
})
|
||||
@ -111,6 +112,7 @@ minetest.register_node("hyperloop:doorTopActive", {
|
||||
paramtype2 = "facedir",
|
||||
diggable = false,
|
||||
light_source = 2,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
groups = {cracky=1, not_in_creative_inventory=1},
|
||||
is_ground_content = false,
|
||||
})
|
||||
@ -133,6 +135,7 @@ minetest.register_node("hyperloop:doorBottom", {
|
||||
},
|
||||
paramtype2 = "facedir",
|
||||
diggable = false,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
groups = {cracky=1, not_in_creative_inventory=1},
|
||||
is_ground_content = false,
|
||||
})
|
||||
@ -149,6 +152,7 @@ minetest.register_node("hyperloop:doorframe", {
|
||||
"hyperloop_skin.png",
|
||||
},
|
||||
paramtype2 = "facedir",
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
groups = {cracky=1},
|
||||
is_ground_content = false,
|
||||
})
|
||||
|
4
info.lua
4
info.lua
@ -60,7 +60,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
meta:set_string("short_info", fields.short_info)
|
||||
meta:set_string("long_info", fields.long_info)
|
||||
meta:set_string("infotext", fields.long_info)
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
else
|
||||
minetest.chat_send_player(player:get_player_name(), "Error: Invalid station name!")
|
||||
end
|
||||
@ -88,7 +88,7 @@ minetest.register_node("hyperloop:info", {
|
||||
if hyperloop.tAllStations[station_name] ~= nil
|
||||
and hyperloop.tAllStations[station_name]["info"] ~= nil then
|
||||
hyperloop.tAllStations[station_name]["info"] = nil
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
end
|
||||
end,
|
||||
|
||||
|
@ -43,7 +43,8 @@ local function store_routes(pos, owner)
|
||||
-- store list
|
||||
local spos = minetest.pos_to_string(pos)
|
||||
if hyperloop.tAllStations[station_name] == nil then
|
||||
hyperloop.tAllStations[station_name] = {pos=spos, routes=tRoutes}
|
||||
-- add a new station
|
||||
hyperloop.tAllStations[station_name] = {pos=spos, routes=tRoutes, time_blocked=0}
|
||||
else
|
||||
hyperloop.tAllStations[station_name].routes = tRoutes
|
||||
end
|
||||
@ -67,7 +68,7 @@ minetest.register_node("hyperloop:junction", {
|
||||
meta:set_string("infotext", "Station "..default_name(pos))
|
||||
meta:set_string("formspec", formspec)
|
||||
store_routes(pos, placer)
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
end,
|
||||
|
||||
on_receive_fields = function(pos, formname, fields, player)
|
||||
@ -93,7 +94,7 @@ minetest.register_node("hyperloop:junction", {
|
||||
meta:set_string("station_name", station_name)
|
||||
meta:set_string("infotext", "Station '"..station_name.."'")
|
||||
store_routes(pos, player)
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
end,
|
||||
|
||||
on_destruct = function(pos)
|
||||
@ -102,7 +103,7 @@ minetest.register_node("hyperloop:junction", {
|
||||
local station_name = meta:get_string("station_name")
|
||||
if hyperloop.tAllStations[station_name] ~= nil then
|
||||
hyperloop.tAllStations[station_name] = nil
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
end
|
||||
end,
|
||||
|
||||
|
3
map.lua
3
map.lua
@ -27,7 +27,8 @@ local function station_list_as_string(pos)
|
||||
return x.distance < y.distance
|
||||
end)
|
||||
if hyperloop.debugging then
|
||||
print(dump(sortedList))
|
||||
print("tAllStations="..dump(sortedList))
|
||||
print("tWifi="..dump(hyperloop.tWifi))
|
||||
end
|
||||
local tRes = {"(player distance: station name (position) seat/machine/owner => directly connected with)\n\n"}
|
||||
local seat, machine, owner
|
||||
|
13
seat.lua
13
seat.lua
@ -130,6 +130,7 @@ local function on_start_travel(pos, node, clicker)
|
||||
local dest_pos = minetest.string_to_pos(dataSet.pos)
|
||||
local dest_meta = minetest.get_meta(dest_pos)
|
||||
local dest_name = dest_meta:get_string("station_name")
|
||||
|
||||
-- seat is on top of the station block
|
||||
dest_pos = vector.add(dest_pos, {x=0,y=1,z=0})
|
||||
dest_meta = minetest.get_meta(dest_pos)
|
||||
@ -150,9 +151,19 @@ local function on_start_travel(pos, node, clicker)
|
||||
-- activate display
|
||||
local dist = hyperloop.distance(pos, dest_pos)
|
||||
local text = "Destination: | "..dest_name.." | Distance: | "..dist.." m | Arrival in: | "
|
||||
local atime = 10 + math.floor(dist/200)
|
||||
local atime
|
||||
if dist < 1000 then
|
||||
atime = 10 + math.floor(dist/100) -- 10..20 sec
|
||||
elseif dist < 10000 then
|
||||
atime = 20 + math.floor(dist/300) -- 23..53 sec
|
||||
else
|
||||
atime = 40 + math.floor(dist/600) -- 56..240 sec (120.000m)
|
||||
end
|
||||
hyperloop.enter_display(pos, dest_facedir, text..atime.." sec")
|
||||
|
||||
-- block departure and arrival stations
|
||||
hyperloop.block(station_name, dest_name, atime+10)
|
||||
|
||||
-- store some data
|
||||
meta:set_int("arrival_time", atime)
|
||||
meta:set_string("lcd_text", text)
|
||||
|
55
utils.lua
55
utils.lua
@ -101,13 +101,6 @@ function hyperloop.distance(pos1, pos2)
|
||||
return math.abs(pos1.x - pos2.x) + math.abs(pos1.y - pos2.y) + math.abs(pos1.z - pos2.z) - 2
|
||||
end
|
||||
|
||||
function hyperloop.dbg_nodes(nodes)
|
||||
print("Nodes:")
|
||||
for _,node in ipairs(nodes) do
|
||||
print("name:"..node.name)
|
||||
end
|
||||
end
|
||||
|
||||
-- Return true if both blocks given bei string-positions are nearby
|
||||
function hyperloop.nearby(pos1, pos2)
|
||||
pos1 = minetest.string_to_pos(pos1)
|
||||
@ -222,7 +215,53 @@ function hyperloop.get_networks()
|
||||
end
|
||||
|
||||
|
||||
-- Store and read the RingList to / from a file
|
||||
-------------------------------------------------------------------------------
|
||||
---- Station reservation/blocking
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
-- reserve departure and arrival stations for some time
|
||||
function hyperloop.reserve(departure, arrival)
|
||||
if hyperloop.tAllStations[departure] == nil then
|
||||
return false
|
||||
elseif hyperloop.tAllStations[arrival] == nil then
|
||||
return false
|
||||
else
|
||||
local t1 = hyperloop.tAllStations[departure].time_blocked or 0
|
||||
local t2 = hyperloop.tAllStations[arrival].time_blocked or 0
|
||||
|
||||
if t1 > minetest.get_gametime() then
|
||||
return false
|
||||
elseif t2 > minetest.get_gametime() then
|
||||
return false
|
||||
else
|
||||
-- place a reservation for 20 seconds to start the trip
|
||||
hyperloop.tAllStations[departure].time_blocked = minetest.get_gametime() + 20
|
||||
hyperloop.tAllStations[arrival].time_blocked = minetest.get_gametime() + 20
|
||||
if hyperloop.debugging then
|
||||
print(departure.." and ".. arrival.." stations are reserved")
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- block the already reserved stations
|
||||
function hyperloop.block(departure, arrival, seconds)
|
||||
if hyperloop.tAllStations[departure] == nil then
|
||||
return false
|
||||
elseif hyperloop.tAllStations[arrival] == nil then
|
||||
return false
|
||||
else
|
||||
hyperloop.tAllStations[departure].time_blocked = minetest.get_gametime() + seconds
|
||||
hyperloop.tAllStations[arrival].time_blocked = minetest.get_gametime() + seconds
|
||||
if hyperloop.debugging then
|
||||
print(departure.." and ".. arrival.." stations are blocked")
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
-- Store and read the station list to / from a file
|
||||
-- so that upcoming actions are remembered when the game
|
||||
-- is restarted
|
||||
hyperloop.tAllStations = hyperloop.file2table("hyperloop_station_list")
|
||||
|
78
wifi.lua
78
wifi.lua
@ -40,7 +40,7 @@
|
||||
|
||||
local function search_head(pos)
|
||||
local res, nodes = hyperloop.scan_neighbours(pos)
|
||||
if res == 1 then -- one neighbor available?
|
||||
if res == 1 or res == 4 then -- one neighbor available?
|
||||
return nodes[1]
|
||||
end
|
||||
end
|
||||
@ -49,7 +49,7 @@ local function read_peer_pos(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
return meta:get_string("peer")
|
||||
end
|
||||
|
||||
|
||||
local function wifi_register(pos, channel)
|
||||
if hyperloop.tWifi[channel] == nil then
|
||||
hyperloop.tWifi[channel] = pos
|
||||
@ -61,10 +61,21 @@ local function wifi_register(pos, channel)
|
||||
end
|
||||
end
|
||||
|
||||
local function wifi_unregister(pos)
|
||||
-- delete channel registration
|
||||
local meta = minetest.get_meta(pos)
|
||||
local channel = meta:get_string("channel")
|
||||
if channel ~= nil and hyperloop.tWifi[channel] ~= nil
|
||||
and vector.equals(hyperloop.tWifi[channel], pos) then
|
||||
hyperloop.tWifi[channel] = nil
|
||||
end
|
||||
end
|
||||
|
||||
local function wifi_update(pos, peer_pos)
|
||||
local rmt_head_pos1 -- own remote tube head
|
||||
local local_head -- local tube head node
|
||||
-- determine remote tube head via local tube head
|
||||
minetest.forceload_block(pos) ---###########################################
|
||||
local_head = search_head(pos)
|
||||
rmt_head_pos1 = read_peer_pos(local_head.pos)
|
||||
if rmt_head_pos1 == nil then
|
||||
@ -72,9 +83,6 @@ local function wifi_update(pos, peer_pos)
|
||||
end
|
||||
-- store peer_pos and tube head pos locally
|
||||
minetest.get_meta(pos):set_string("wifi_peer", peer_pos)
|
||||
minetest.get_meta(pos):set_string("peer", rmt_head_pos1)
|
||||
-- degrade head tube to link tube
|
||||
hyperloop.degrade_tupe_node(local_head)
|
||||
return rmt_head_pos1
|
||||
end
|
||||
|
||||
@ -95,12 +103,29 @@ local function wifi_pairing(pos, peer_pos)
|
||||
hyperloop.update_head_node(rmt_head_pos2, rmt_head_pos1)
|
||||
-- store peer_pos and tube head pos locally
|
||||
minetest.get_meta(pos):set_string("wifi_peer", peer_pos)
|
||||
minetest.get_meta(pos):set_string("peer", rmt_head_pos1)
|
||||
if hyperloop.debugging then
|
||||
print("wifi_pairing meta="..dump(minetest.get_meta(pos):to_table()))
|
||||
end
|
||||
-- degrade head tube to link tube
|
||||
hyperloop.degrade_tupe_node(local_head)
|
||||
--hyperloop.degrade_tupe_node(local_head)
|
||||
return true
|
||||
end
|
||||
|
||||
-- Place the wifi node as head of a tube chain
|
||||
local function place_wifi_node(pos, head_node)
|
||||
local peer_pos = minetest.get_meta(head_node.pos):get_string("peer")
|
||||
-- update self
|
||||
hyperloop.update_head_node(minetest.pos_to_string(pos), peer_pos)
|
||||
-- update peer
|
||||
hyperloop.update_head_node(peer_pos, minetest.pos_to_string(pos))
|
||||
-- degrade head tube to link tube
|
||||
hyperloop.degrade_tupe_node(head_node)
|
||||
if hyperloop.debugging then
|
||||
print("wifi meta="..dump(minetest.get_meta(pos):to_table()))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
minetest.register_node("hyperloop:tube_wifi1", {
|
||||
description = "Hyperloop WiFi Tube",
|
||||
inventory_image = "hyperloop_tube_wifi_inventory.png",
|
||||
@ -118,32 +143,36 @@ minetest.register_node("hyperloop:tube_wifi1", {
|
||||
},
|
||||
|
||||
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
||||
local res, nodes = hyperloop.scan_neighbours(pos)
|
||||
if res == 1 and nodes[1].name == "hyperloop:tube1" then
|
||||
local formspec = "size[5,4]"..
|
||||
"field[0.5,0.5;3,1;channel;Insert channel ID;myName:myChannel]" ..
|
||||
"button_exit[1,2;2,1;exit;Save]"
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("formspec", formspec)
|
||||
else
|
||||
local node = minetest.get_node(pos)
|
||||
hyperloop.remove_node(pos, node)
|
||||
return itemstack
|
||||
local head_node = search_head(pos)
|
||||
if head_node ~= nil then
|
||||
if head_node.name == "hyperloop:tube1" then
|
||||
local formspec = "size[5,4]"..
|
||||
"field[0.5,0.5;3,1;channel;Insert channel ID;myName:myChannel]" ..
|
||||
"button_exit[1,2;2,1;exit;Save]"
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("formspec", formspec)
|
||||
place_wifi_node(pos, head_node)
|
||||
else
|
||||
local node = minetest.get_node(pos)
|
||||
hyperloop.remove_node(pos, node)
|
||||
return itemstack
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
|
||||
on_receive_fields = function(pos, formname, fields, player)
|
||||
if fields.channel == nil then
|
||||
return
|
||||
end
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("channel", fields.channel)
|
||||
meta:set_string("formspec", nil)
|
||||
local peer_pos = wifi_register(pos, fields.channel)
|
||||
if peer_pos then
|
||||
if wifi_pairing(pos, peer_pos, true) ~= nil then
|
||||
if peer_pos ~= nil then
|
||||
if wifi_pairing(pos, peer_pos) then
|
||||
minetest.chat_send_player(player:get_player_name(),
|
||||
"WiFi pairing completed!")
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
end
|
||||
end
|
||||
end,
|
||||
@ -154,10 +183,13 @@ minetest.register_node("hyperloop:tube_wifi1", {
|
||||
peer = minetest.string_to_pos(peer)
|
||||
if peer ~= nil then
|
||||
hyperloop.upgrade_node(peer)
|
||||
else -- no pairing so far
|
||||
-- delete channel registration
|
||||
wifi_unregister(pos)
|
||||
end
|
||||
-- unpair local wifi node
|
||||
hyperloop.upgrade_node(pos)
|
||||
hyperloop.update_all_booking_machines()
|
||||
--hyperloop.update_all_booking_machines()
|
||||
end,
|
||||
|
||||
paramtype2 = "facedir",
|
||||
|
Loading…
x
Reference in New Issue
Block a user