Add subnet feature

master
Joachim Stolberg 2021-03-15 20:29:19 +01:00
parent 8b80d36618
commit 5b7d4eeca7
5 changed files with 105 additions and 51 deletions

View File

@ -11,8 +11,7 @@ It is the fast and modern way of travelling.
* It can be used even on small servers without lagging
* No configuration or programming of the tube network is necessary (only the station names have to be entered)
**![See Wiki Page for more info](https://github.com/joe7575/Minetest-Hyperloop/wiki)**
**[See Wiki Page for more info](https://github.com/joe7575/Minetest-Hyperloop/wiki)**
![screenshot](https://github.com/joe7575/Minetest-Hyperloop/blob/master/screenshot.png)
@ -33,9 +32,9 @@ The mod includes many different kind of blocks:
..and more.
Browse on: ![GitHub](https://github.com/joe7575/Minetest-Hyperloop)
Browse on: [GitHub](https://github.com/joe7575/Minetest-Hyperloop)
Download: ![GitHub](https://github.com/joe7575/Minetest-Hyperloop/archive/master.zip)
Download: [GitHub](https://github.com/joe7575/Minetest-Hyperloop/archive/master.zip)
## Migration from v1 to v2
@ -59,7 +58,7 @@ has some risks. Therefore:
## Introduction
**![See Wiki Page for more info](https://github.com/joe7575/Minetest-Hyperloop/wiki)**
**[See Wiki Page for more info](https://github.com/joe7575/Minetest-Hyperloop/wiki)**
## Configuration
@ -68,23 +67,25 @@ The following can be changed in the minetest menu (Settings -> Advanced Settings
* "WiFi block crafting enabled" - To enable the crafting of WiFi blocks (default: false)
* "free tube placement enabled" - If enabled Hyperloop Tubes and Elevator Shafts can be build in all directions (default: true)
When this option is disabled, Hyperloop tubes can only be built in the horizontal direction and elevator shafts in the vertical direction.
* "enable building of subnets" - If enabled the ticket block has an additional field for specifying a subnet name. Stations with the same subnet name (optional) represent an isolated subnet within the Hyperloop network.
Example for 'minetest.conf':
```LUA
hyperloop_wifi_enabled = true
hyperloop_wifi_crafting_enabled = false
hyperloop_free_tube_placement_enabled = true
hyperloop_wifi_enabled = true -- WiFi block enabled
hyperloop_wifi_crafting_enabled = false -- WiFi block crafting enabled
hyperloop_free_tube_placement_enabled = true -- free tube placement enabled
hyperloop_subnet_enabled = true -- enable building of subnets
```
## Dependencies
tubelib2 (![GitHub](https://github.com/joe7575/tubelib2))
tubelib2 ([GitHub](https://github.com/joe7575/tubelib2))
default
intllib
optional: worldedit, techage
# License
Copyright (C) 2017,2020 Joachim Stolberg
Copyright (C) 2017,2021 Joachim Stolberg
Code: Licensed under the GNU LGPL version 2.1 or later. See LICENSE.txt and http://www.gnu.org/licenses/lgpl-2.1.txt
Textures: CC0
Display: Derived from the work of kaeza, sofar and others (digilines) LGPLv2.1+

View File

@ -75,35 +75,72 @@ local function remove_junctions(sortedList)
return tbl
end
local function station_list_as_string(pos)
-- Generate a distance sorted list of all connected stations
local sortedList = Stations:station_list(pos, pos, "dist")
-- Delete the own station from list
table.remove(sortedList, 1)
local function filter_subnet(sortedList, subnet)
if hyperloop.subnet_enabled then
if subnet == "" then
subnet = nil
end
local tbl = {}
for idx,item in ipairs(sortedList) do
if item.subnet == subnet then
tbl[#tbl+1] = item
end
end
return tbl
end
return sortedList
end
-- Used to update the station list for booking machine
-- and teleport list.
local function station_list_as_string(pos, subnet)
local meta = M(pos)
-- Generate a name sorted list of all connected stations
local sortedList = Stations:station_list(pos, pos, "name")
-- remove all junctions from the list
sortedList = remove_junctions(sortedList)
-- use subnet pattern to reduce the list
sortedList = filter_subnet(sortedList, subnet)
-- store the list for later use
store_station_list(pos, sortedList)
-- Generate the formspec string
return generate_string(sortedList)
end
local naming_formspec = nil
local function naming_formspec(pos)
local meta = minetest.get_meta(pos)
local formspec = "size[6,4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0,0;"..S("Please enter the station name to\nwhich this booking machine belongs.").."]" ..
"field[0.5,1.5;5,1;name;"..S("Station name")..";MyTown]" ..
"field[0.5,2.7;5,1;info;"..S("Additional station information")..";]" ..
"button_exit[2,3.6;2,1;exit;Save]"
meta:set_string("formspec", formspec)
meta:set_int("change_counter", 0)
if hyperloop.subnet_enabled then
naming_formspec = function(pos)
local meta = M(pos)
local formspec = "size[7,5.4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0,0;"..S("Please enter the station name to\nwhich this booking machine belongs.").."]" ..
"field[0.2,1.5;7.1,1;name;"..S("Station name")..";MyTown]" ..
"field[0.2,2.7;7.1,1;info;"..S("Additional station information")..";]" ..
"field[0.2,3.9;7.1,1;subnet;"..S("Subnet name (optional)")..";]" ..
"button_exit[2.5,4.7;2,1;exit;Save]"
meta:set_string("formspec", formspec)
meta:set_int("change_counter", 0)
end
else
naming_formspec = function(pos)
local meta = M(pos)
local formspec = "size[7,4.4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0,0;"..S("Please enter the station name to\nwhich this booking machine belongs.").."]" ..
"field[0.2,1.5;7.1,1;name;"..S("Station name")..";MyTown]" ..
"field[0.2,2.7;7.1,1;info;"..S("Additional station information")..";]" ..
"button_exit[2.5,3.7;2,1;exit;Save]"
meta:set_string("formspec", formspec)
meta:set_int("change_counter", 0)
end
end
local function booking_machine_update(pos)
local meta = M(pos)
local sStationPos = meta:get_string("sStationPos")
@ -111,25 +148,19 @@ local function booking_machine_update(pos)
local station_pos = P(sStationPos)
local counter = meta:get_int("change_counter") or 0
local changed, newcounter = Stations:changed(counter)
if changed then
meta:set_string("formspec", station_list_as_string(station_pos))
if changed or not tStationList[sStationPos] then
local subnet = meta:get_string("subnet")
meta:set_string("formspec", station_list_as_string(station_pos, subnet))
meta:set_int("change_counter", newcounter)
end
if not tStationList[sStationPos] then
local sortedList = Stations:station_list(station_pos, station_pos, "dist")
-- Delete the own station from list
table.remove(sortedList, 1)
-- remove all junctions from the list
sortedList = remove_junctions(sortedList)
-- store the list for later use
store_station_list(station_pos, sortedList)
end
end
end
local function on_rightclick(pos)
booking_machine_update(pos)
end
local function on_receive_fields(pos, formname, fields, player)
booking_machine_update(pos)
-- station name entered?
if fields.name ~= nil then
local station_name = string.trim(fields.name)
@ -142,17 +173,25 @@ local function on_receive_fields(pos, formname, fields, player)
hyperloop.chat(player, S("Station has already a booking machine!"))
return
end
-- add subnet name if available
local subnet = string.trim(fields.subnet or "")
if subnet == "" then
subnet = nil
end
-- store meta and generate station formspec
Stations:update(stationPos, {
name = station_name,
booking_pos = pos,
booking_info = string.trim(fields.info),
subnet = subnet,
})
local meta = M(pos)
meta:set_string("sStationPos", SP(stationPos))
meta:set_string("infotext", "Station: "..station_name)
meta:set_string("formspec", station_list_as_string(stationPos))
meta:set_string("subnet", string.trim(fields.subnet or ""))
meta:set_int("change_counter", 0) -- force update
booking_machine_update(pos)
else
hyperloop.chat(player, S("Invalid station name!"))
end
@ -233,6 +272,7 @@ minetest.register_node("hyperloop:booking", {
on_rotate = screwdriver.disallow,
on_receive_fields = on_receive_fields,
on_destruct = on_destruct,
on_rightclick = on_rightclick,
paramtype = 'light',
light_source = 2,
@ -279,10 +319,4 @@ minetest.register_node("hyperloop:booking_ground", {
})
minetest.register_lbm({
label = "[Hyperloop] Booking machine update",
name = "hyperloop:update",
nodenames = {"hyperloop:booking", "hyperloop:booking_ground"},
run_at_every_load = true,
action = booking_machine_update
})

View File

@ -66,7 +66,8 @@ else
hyperloop.wifi_enabled = minetest.settings:get_bool("hyperloop_wifi_enabled")
hyperloop.wifi_crafting_enabled = minetest.settings:get_bool("hyperloop_wifi_crafting_enabled")
hyperloop.free_tube_placement_enabled = minetest.settings:get_bool("hyperloop_free_tube_placement_enabled", true)
hyperloop.subnet_enabled = minetest.settings:get_bool("hyperloop_subnet_enabled")
dofile(minetest.get_modpath("hyperloop") .. "/network.lua")
dofile(minetest.get_modpath("hyperloop") .. "/data_base.lua")
dofile(minetest.get_modpath("hyperloop") .. "/booking.lua")

View File

@ -96,6 +96,15 @@ local function sort_based_on_distance(tStations, pos)
return lStations
end
-- Return a list with sorted stations
local function sort_based_on_name(tStations, pos)
local lStations = table_to_list(table.copy(tStations))
-- Add distance
lStations = add_distance_to_list(lStations, pos)
table.sort(lStations, function(a,b) return a.name < b.name end)
return lStations
end
--
-- Class Network
@ -233,8 +242,12 @@ function Network:station_list(pos, station_pos, sorted)
end
if sorted == "dist" then
lStations = sort_based_on_distance(tStations, pos)
else
elseif sorted == "level" then
lStations = sort_based_on_level(tStations)
else
-- delete own station from list
tStations[S(station_pos)] = nil
lStations = sort_based_on_name(tStations, pos)
end
return lStations
end
@ -263,4 +276,4 @@ end
function Network:serialize()
return minetest.serialize(self)
end

View File

@ -8,3 +8,8 @@ hyperloop_wifi_crafting_enabled (WiFi block crafting enabled) bool false
# If disabled, connected stations have to be on one level,
# typically underground.
hyperloop_free_tube_placement_enabled (free tube placement enabled) bool false
# The ticket block has an additional field for specifying a subnet name.
# Stations with the same subnet name (optional) represent an isolated
# subnet within the Hyperloop network.
hyperloop_subnet_enabled (enable building of subnets) bool false