Skip to content

Commit

Permalink
add virtual train signals and support for it in the dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvn committed Oct 28, 2021
1 parent 4ff9d0c commit 019469e
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 36 deletions.
138 changes: 138 additions & 0 deletions data-final-fixes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,138 @@
local flib = require('__flib__.data-util')

local icon_encoded_position = { { icon = "__LogisticTrainNetwork__/graphics/icons/encoded-position.png", icon_size = 64, tint = {r=1, g=1, b=1, a=1} } }
local icon_empty = { { icon = "__core__/graphics/empty.png", icon_size = 1, tint = {r=1, g=1, b=1, a=1} } }
local proto_empty = { icons = icon_empty }

local function is_hidden(proto)
if proto.flags then
for _,flag in pairs(proto.flags) do
if (flag == "hidden") then
return true
end
end
end
return false
end

local function scaled_icon(icon, second)
local size = icon.icon_size or 32
local shift = icon.shift or {x = 0, y = 0}
-- empty icon scales things up 32 times bigger and we want half size
local scale = (icon.scale or 32 / size) / 64
local dx
if second then
dx = 0.25
else
dx = -0.25
end
return {
icon = icon.icon,
icon_size = size,
icon_mipmaps = icon.icon_mipmaps or 0,
tint = icon.tint,
scale = scale,
-- empty icon scales things up 32 times bigger and we want half size
shift = {
x = shift.x / 64 + dx,
y = shift.y / 64,
},
}
end

local function scale_icons(icons, proto, second)
if proto.icon then
local icon = {
icon = proto.icon,
icon_size = proto.icon_size,
icon_mipmaps = proto.icon_mipmaps,
tint = {r=1, g=1, b=1, a=1}
}
icons[#icons+1] = scaled_icon(icon, second)
elseif proto.icons then
for _, v in pairs(proto.icons) do
icons[#icons+1] = scaled_icon(v, second)
end
end
end

local function train_signal(loco, wagon, order, suffix)
local wagoncount = 0
if not is_hidden(wagon) then
wagoncount = wagoncount + 1
-- scale icons
local icons = {}
scale_icons(icons, loco, false)
scale_icons(icons, wagon, true)
-- create signal
local localised_name
local loco_name = loco.localized_name or loco.name
local wagon_name = wagon.localized_name or wagon.name

if loco.type == "virtual-signal" then
if wagon.type == "virtual-signal" then
localised_name = {"virtual-signal-name.ltn-train-any-any", suffix}
else
localised_name = {"virtual-signal-name.ltn-train-any", wagon_name}
end
else
if wagon.type == "virtual-signal" then
localised_name = {"virtual-signal-name.ltn-train-any", loco_name}
else
localised_name = {"virtual-signal-name.ltn-train", loco_name, wagon_name}
end
end

local signal = {
type = "virtual-signal",
name = "ltn-train-"..suffix.."-"..loco.name.."-"..wagon.name,
icons = flib.create_icons(proto_empty, icons) or empty_icon,
icon_size = nil,
subgroup = "ltn-train-signal-"..suffix,
order = order,
localised_name = localised_name,
}
data:extend({signal})
end
end

local function train_signals(loco, lococount)
if not is_hidden(loco) then
-- add cargo wagons to train
local wagoncount = 0
for _, wagon in pairs(data.raw["cargo-wagon"]) do
wagoncount = wagoncount + 1
local order = "a-cargo-"..string.format("%02d", lococount)..string.format("%02d", wagoncount)
train_signal(loco, wagon, order, "cargo")
end
if wagoncount > 1 then
local order = "a-cargo-"..string.format("%02d", lococount).."00"
train_signal(loco, data.raw["virtual-signal"]["ltn-position-any-cargo-wagon"], oder, "cargo")
end
-- add fluid wagons to train
wagoncount = 0
for _, wagon in pairs(data.raw["fluid-wagon"]) do
wagoncount = wagoncount + 1
local order = "a-fluid-"..string.format("%02d", lococount)..string.format("%02d", wagoncount)
train_signal(loco, wagon, order, "fluid")
end
if wagoncount > 1 then
local order = "a-cargo-"..string.format("%02d", lococount).."00"
train_signal(loco, data.raw["virtual-signal"]["ltn-position-any-fluid-wagon"], order, "fluid")
end
-- add fluid wagons to train
wagoncount = 0
for _, wagon in pairs(data.raw["artillery-wagon"]) do
wagoncount = wagoncount + 1
local order = "a-artillery-"..string.format("%02d", lococount)..string.format("%02d", wagoncount)
train_signal(loco, wagon, order, "artillery")
end
if wagoncount > 1 then
local order = "a-artillery-"..string.format("%02d", lococount).."00"
train_signal(loco, data.raw["virtual-signal"]["ltn-position-any-artillery-wagon"], order, "artillery")
end
end
end

local lococount = 0
for _, loco in pairs(data.raw["locomotive"]) do
Expand All @@ -21,6 +153,10 @@ for _, loco in pairs(data.raw["locomotive"]) do
localised_name = {"virtual-signal-name.ltn-position", loco.localised_name or {"entity-name." .. loco.name}}
}
data:extend({signal})
train_signals(loco, lococount)
end
if lococount > 1 then
train_signals(data.raw["virtual-signal"]["ltn-position-any-locomotive"], 0)
end

local wagoncount = 0
Expand Down Expand Up @@ -87,3 +223,5 @@ end
data.raw["constant-combinator"]["logistic-train-stop-output"].item_slot_count = 4 + lococount + wagoncount + wagoncount_fluid + wagoncount_artillery + itemcount + fluidcount
log("[LTN] found "..tostring(itemcount).." items, "..tostring(fluidcount).." fluids, "..tostring(lococount).." locomotives, "..tostring(wagoncount + wagoncount_fluid + wagoncount_artillery).." wagons")

for _, loco in pairs(data.raw["locomotive"]) do
end
4 changes: 4 additions & 0 deletions locale/en/base.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ ltn-provider-stack-threshold=Provide Stack Threshold
ltn-provider-priority=Provide Priority
ltn-locked-slots=Locked Slots per Wagon
ltn-disable-warnings=Disable Warning Messages
ltn-train=Train with __1__ and __2__
ltn-train-any=Any train with __1__
ltn-train-any-any=Any __1__ train

[ltn-message]
error-duplicated-unit-number=[LTN] Error: Duplicated unit_number __1__.
Expand All @@ -64,6 +67,7 @@ warning-dispatcher-disabled=[LTN] Warning: Dispatcher disabled. No deliveries wi
empty-depot-item=[LTN] No train to transport items found in depots. Skipping item processing.
empty-depot-fluid=[LTN] No train to transport fluids found in depots. Skipping fluid processing.
empty-depot-artillery=[LTN] No artillery train found in depots. Skipping artillery processing.
no-provider-found=[LTN] Requester __1__: No station supplying __2__ found in networks __3__.
provider-found=[LTN] Provider __1__: priority: __2__, deliveries: __3__, available: __4__ __5__.
no-train-found=[LTN] No train to transport from __1__ to __2__ in networks __3__ with length between __4__ and __5__ found in Depot.
Expand Down
18 changes: 18 additions & 0 deletions prototypes/signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ data:extend({
group = "signals",
order = "ltn4[ltn-position-signal-artillery-wagon]"
},
{
type = "item-subgroup",
name = "ltn-train-signal-cargo",
group = "signals",
order = "ltn4[ltn-train-signal-cargo]"
},
{
type = "item-subgroup",
name = "ltn-train-signal-fluid",
group = "signals",
order = "ltn4[ltn-train-signal-fluid]"
},
{
type = "item-subgroup",
name = "ltn-train-signal-artillery",
group = "signals",
order = "ltn4[ltn-train-signal-artillery]"
},
{
type = "virtual-signal",
name = "ltn-position-any-locomotive",
Expand Down
7 changes: 6 additions & 1 deletion script/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ match_string = "([^,]+),([^,]+)"
btest = bit32.btest
band = bit32.band
ceil = math.ceil
sort = table.sort
sort = table.sort

-- train signals types
TRAIN_CARGO = "ltn-train-cargo"
TRAIN_FLUID = "ltn-train-fluid"
TRAIN_ARTILLERY = "ltn-train-artillery"
Loading

0 comments on commit 019469e

Please sign in to comment.