diff --git a/.vscode/launch.json b/.vscode/launch.json index 3e8c87c..7fd5b23 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -29,7 +29,6 @@ "debugadapter": true, "flib": true, "cybersyn": true, - "creative-mod": true, }, "disableExtraMods": true }, diff --git a/cybersyn/TODO b/cybersyn/TODO index 3756855..9a58a0e 100644 --- a/cybersyn/TODO +++ b/cybersyn/TODO @@ -1,5 +1,5 @@ close gui when the combinator is destroyed -play close sound when gui is closed +do not play close sound when a different gui is opened improve localization support space elevator do hardcore testing diff --git a/cybersyn/locale/en/base.cfg b/cybersyn/locale/en/base.cfg index ddccf25..59ef511 100644 --- a/cybersyn/locale/en/base.cfg +++ b/cybersyn/locale/en/base.cfg @@ -1,34 +1,34 @@ [mod-setting-name] -cybersyn-ticks-per-second=Dispatcher ticks per second +cybersyn-ticks-per-second=Dispatcher updates per second cybersyn-request-threshold=Default requester threshold cybersyn-provide-threshold=Default provider threshold cybersyn-network-flag=Default network flags [mod-setting-description] -cybersyn-ticks-per-second=How many times per second to check all stations for possible deliveries. This value will be rounded up to a divisor of 60. -cybersyn-request-threshold=When a requester threshold signal is not recieved by a station it will default to this value. -cybersyn-provide-threshold=When a provider threshold signal is not recieved by a station it will default to this value. -cybersyn-network-flag=Choose the default set of networks a station will service when no network signal is provided. This integer is interpretted bit-wise to give 32 possible networks to choose from. +cybersyn-ticks-per-second=How many times per second the dispather should check for new deliveries. Deliveries are made one at a time per update. This value will be rounded up to a divisor of 60. +cybersyn-request-threshold=The default request threshold when a request threshold signal is not given to a station. Huge values will prevent stations from taking requests from the network unless an explicit threshold is set. +cybersyn-provide-threshold=The default provide threshold when a provide threshold signal is not given to a station. Huge values will prevent stations from providing to the network unless an explicit threshold is set. +cybersyn-network-flag=The default set of networks a station will service when no network signal is given to a station. This integer is interpretted bit-wise to give 32 possible network flags to choose from. [item-name] cybersyn-combinator=Cybernetic combinator [item-description] -cybersyn-combinator=Cybernetic combinator +cybersyn-combinator=Place next to a train stop to add it to the cybersyn network. Adjacent stations can now request or provide items or fluids by train. Has 4 different operation modes. [entity-name] cybersyn-combinator=Cybernetic combinator cybersyn-combinator-output=NA [entity-description] -cybersyn-combinator=Cybersyn depot +cybersyn-combinator=Place next to a train stop to add it to the cybersyn network. Has 4 different operation modes. cybersyn-combinator-output=NA [technology-name] cybersyn-train-network=Cybernetic train network [technology-description] -cybersyn-train-network=Cybernetic train network +cybersyn-train-network=Train station controllers capable of coordinating the inputs and outputs of an entire economy. [virtual-signal-name] cybersyn-priority=Station priority @@ -42,11 +42,11 @@ lost-train=A train has become lost nonempty-train=A train has parked in a depot while still containing items; it cannot be dispatched until it is empty [cybersyn-gui] -operation=Combinator type -network=Network -comb1=Primary controller -comb2=Secondary station control -depot=Depot -wagon-manifest=Wagon combinator-title=Cybernetic combinator -auto-description=Station automatically decides which trains in the network it can service +operation=Mode +comb1=Primary station control +comb2=Optional station control +depot=Depot control +wagon-manifest=Wagon control +network=Network +auto-description=Station automatically decides which trains in the network it can service (all trains are allowed by default) diff --git a/cybersyn/scripts/central-planning.lua b/cybersyn/scripts/central-planning.lua index 5ae7ce6..375b3fa 100644 --- a/cybersyn/scripts/central-planning.lua +++ b/cybersyn/scripts/central-planning.lua @@ -85,11 +85,7 @@ function set_combinator_output(map_data, comb, signals) local out = map_data.to_output[comb.unit_number] if out.valid then out.get_or_create_control_behavior().parameters = signals - else - --TODO: error logging? end - else - --TODO: error logging? end end @@ -101,7 +97,7 @@ local function set_comb2(map_data, station) local signals = {} for item_name, count in pairs(deliveries) do local i = #signals + 1 - local item_type = game.item_prototypes[item_name].type + local item_type = game.item_prototypes[item_name].type--NOTE: this is expensive signals[i] = {index = i, signal = {type = item_type, name = item_name}, count = -count} end set_combinator_output(map_data, station.entity_comb2, signals) @@ -124,7 +120,6 @@ function remove_manifest(map_data, station, manifest, sign) end ---@param map_data MapData ----@param station Station ---@param signal SignalID local function get_thresholds(map_data, station, signal) local comb2 = station.entity_comb2 @@ -229,10 +224,8 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p ---@type string local item_name = v.signal.name local item_count = v.count - --local item_type = v.signal.type local effective_item_count = item_count + (r_station.deliveries[item_name] or 0) - local r_threshold, p_threshold = get_thresholds(map_data, r_station, v.signal) - if -effective_item_count >= r_threshold then + if effective_item_count < 0 and item_count < 0 then requests[item_name] = -effective_item_count end end @@ -245,8 +238,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p local item_count = v.count local item_type = v.signal.type local effective_item_count = item_count + (p_station.deliveries[item_name] or 0) - local r_threshold, p_threshold = get_thresholds(map_data, p_station, v.signal) - if effective_item_count >= p_threshold then + if effective_item_count > 0 and item_count > 0 then local r = requests[item_name] if r then local item = {name = item_name, type = item_type, count = min(r, effective_item_count)} @@ -312,6 +304,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p local item_network_name = network_name..":"..item.name local r_stations = economy.all_r_stations[item_network_name] local p_stations = economy.all_p_stations[item_network_name] + --NOTE: one of these will be redundant for i, id in ipairs(r_stations) do if id == r_station_id then table.remove(r_stations, i) @@ -422,9 +415,10 @@ local function tick_poll_station(map_data, mod_settings) if item_type == "virtual" then if item_name == SIGNAL_PRIORITY then station.priority = item_count - elseif item_name == REQUEST_THRESHOLD then + elseif item_name == REQUEST_THRESHOLD and item_count ~= 0 then + --NOTE: thresholds must be >0 or they will cause a crash station.r_threshold = abs(item_count) - elseif item_name == PROVIDE_THRESHOLD then + elseif item_name == PROVIDE_THRESHOLD and item_count ~= 0 then station.p_threshold = abs(item_count) elseif item_name == LOCKED_SLOTS then station.locked_slots = max(item_count, 0) @@ -444,7 +438,7 @@ local function tick_poll_station(map_data, mod_settings) local effective_item_count = item_count + (station.deliveries[item_name] or 0) local r_threshold, p_threshold = get_thresholds(map_data, station, v.signal) - if -effective_item_count >= r_threshold then + if -effective_item_count >= r_threshold and -item_count >= r_threshold then local item_network_name = station.network_name..":"..item_name local stations = all_r_stations[item_network_name] if stations == nil then @@ -454,7 +448,7 @@ local function tick_poll_station(map_data, mod_settings) all_names[#all_names + 1] = v.signal end stations[#stations + 1] = station_id - elseif effective_item_count >= p_threshold then + elseif effective_item_count >= p_threshold and item_count >= p_threshold then local item_network_name = station.network_name..":"..item_name local stations = all_p_stations[item_network_name] if stations == nil then @@ -462,6 +456,8 @@ local function tick_poll_station(map_data, mod_settings) all_p_stations[item_network_name] = stations end stations[#stations + 1] = station_id + else + signals[k] = nil end end end diff --git a/cybersyn/scripts/constants.lua b/cybersyn/scripts/constants.lua index d8e1de2..4e15cdd 100644 --- a/cybersyn/scripts/constants.lua +++ b/cybersyn/scripts/constants.lua @@ -11,6 +11,7 @@ LOCKED_SLOTS = "cybersyn-locked-slots" COMBINATOR_NAME = "cybersyn-combinator" COMBINATOR_OUT_NAME = "cybersyn-combinator-output" +COMBINATOR_CLOSE_SOUND = "entity-close/cybersyn-combinator" OPERATION_DEFAULT = "*" OPERATION_PRIMARY_IO = "/" diff --git a/cybersyn/scripts/global.lua b/cybersyn/scripts/global.lua index 15cdae4..d6a2ae9 100644 --- a/cybersyn/scripts/global.lua +++ b/cybersyn/scripts/global.lua @@ -32,7 +32,7 @@ ---@field public is_all boolean ---@field public accepted_layouts TrainClass ---@field public layout_pattern string? ----@field public tick_signals Signal[]? --transient +---@field public tick_signals {[uint]: Signal}? --transient ---@class Depot ---@field public priority int --transient @@ -69,15 +69,10 @@ ---@field public p_threshold int ---@field public network_flag int - ---TODO: only init once and move settings code ---@type CybersynModSettings mod_settings = {} -mod_settings.tps = settings.global["cybersyn-ticks-per-second"].value -mod_settings.r_threshold = settings.global["cybersyn-request-threshold"].value -mod_settings.p_threshold = settings.global["cybersyn-provide-threshold"].value -mod_settings.network_flag = settings.global["cybersyn-network-flag"].value +--TODO: guarantee this only inits once global.total_ticks = 0 global.tick_state = STATE_INIT global.tick_data = {} diff --git a/cybersyn/scripts/gui.lua b/cybersyn/scripts/gui.lua index edb05df..a31938b 100644 --- a/cybersyn/scripts/gui.lua +++ b/cybersyn/scripts/gui.lua @@ -118,7 +118,7 @@ local function on_gui_closed(event) if rootgui[COMBINATOR_NAME] then rootgui[COMBINATOR_NAME].destroy() - --TODO: play close sound to player + player.play_sound({path = COMBINATOR_CLOSE_SOUND}) end end @@ -133,7 +133,7 @@ function register_gui_actions() if msg[1] == "close" then if rootgui[COMBINATOR_NAME] then rootgui[COMBINATOR_NAME].destroy() - --TODO: play close sound to player + player.play_sound({path = COMBINATOR_CLOSE_SOUND}) end elseif msg[1] == "drop-down" then local element = event.element diff --git a/cybersyn/scripts/layout.lua b/cybersyn/scripts/layout.lua index afbc365..24cba3e 100644 --- a/cybersyn/scripts/layout.lua +++ b/cybersyn/scripts/layout.lua @@ -244,7 +244,6 @@ function set_r_wagon_combs(map_data, station, train) local stack = inv[stack_i] if stack.valid_for_read then local i = #signals + 1 - --TODO: does this work or do we need to aggregate signals? signals[i] = {index = i, signal = {type = stack.type, name = stack.name}, count = -stack.count} end end @@ -406,7 +405,6 @@ local function reset_station_layout(map_data, station, forbidden_entity) if supports_fluid then layout_pattern = layout_pattern..STATION_LAYOUT_ALL else - --TODO: needs to allow misc wagons as well layout_pattern = layout_pattern..STATION_LAYOUT_NOT_FLUID end pattern_length = #layout_pattern @@ -456,7 +454,7 @@ end ---@param rail LuaEntity ---@param forbidden_entity LuaEntity? function update_station_from_rail(map_data, rail, forbidden_entity) - --TODO: search further or better? + --NOTE: should we search further or better? it would be more expensive local entity = rail.get_rail_segment_entity(defines.rail_direction.back, false) if entity and entity.valid and entity.name == "train-stop" then local station = map_data.stations[entity.unit_number] @@ -485,7 +483,6 @@ end ---@param inserter LuaEntity ---@param forbidden_entity LuaEntity? function update_station_from_inserter(map_data, inserter, forbidden_entity) - --TODO: check if correct local surface = inserter.surface local rail = surface.find_entity("straight-rail", inserter.pickup_position) diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index ef7ec91..03345bb 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -720,6 +720,12 @@ local filter_comb = { {filter = "type", type = "arithmetic-combinator"}, } local function register_events() + + mod_settings.tps = settings.global["cybersyn-ticks-per-second"].value --[[@as int]] + mod_settings.r_threshold = settings.global["cybersyn-request-threshold"].value--[[@as int]] + mod_settings.p_threshold = settings.global["cybersyn-provide-threshold"].value--[[@as int]] + mod_settings.network_flag = settings.global["cybersyn-network-flag"].value--[[@as int]] + --NOTE: I have no idea if this correctly registers all events once in all situations flib_event.register(defines.events.on_built_entity, on_built, filter_built) flib_event.register(defines.events.on_robot_built_entity, on_built, filter_built) diff --git a/cybersyn/settings.lua b/cybersyn/settings.lua index ddc7cbb..dadd6e2 100644 --- a/cybersyn/settings.lua +++ b/cybersyn/settings.lua @@ -14,7 +14,7 @@ data:extend({ name = "cybersyn-request-threshold", order = "ab", setting_type = "runtime-global", - default_value = 1000000000, + default_value = 2000000000, minimum_value = 1, maximum_value = 2147483647, }, @@ -23,7 +23,7 @@ data:extend({ name = "cybersyn-provide-threshold", order = "ac", setting_type = "runtime-global", - default_value = 1000000000, + default_value = 2000000000, minimum_value = 1, maximum_value = 2147483647, },