created alpha

This commit is contained in:
Monica Moniot
2022-11-04 01:00:28 -04:00
parent ebcb906f27
commit 190741715d
10 changed files with 41 additions and 47 deletions

View File

@@ -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

View File

@@ -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 = "/"

View File

@@ -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 = {}

View File

@@ -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

View File

@@ -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)

View File

@@ -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)