mirror of
https://github.com/Xevion/project-cybersyn.git
synced 2025-12-16 18:12:50 -06:00
added priorities
This commit is contained in:
104
main.lua
104
main.lua
@@ -1,71 +1,87 @@
|
|||||||
|
local function get_item_amount(station, item_id)
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_distance(stations, r_station_i, p_station_i)
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
local function send_train_between(stations, r_station_i, p_station_i)
|
local function send_train_between(stations, r_station_i, p_station_i)
|
||||||
stations[r_station_i].last_p_station_i = p_station_i
|
stations[r_station_i].last_p_station_i = p_station_i
|
||||||
stations[p_station_i].last_r_station_i = r_station_i
|
stations[p_station_i].last_r_station_i = r_station_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
station:
|
||||||
|
deliveries_total: int
|
||||||
|
train_limit: int
|
||||||
|
requester_limit: int
|
||||||
|
provider_limit: int
|
||||||
|
priority: int
|
||||||
|
]]
|
||||||
|
|
||||||
local function tick(stations)
|
local function tick(stations, all_items)
|
||||||
--psuedocode
|
for _, item_id in pairs(all_items) do
|
||||||
|
|
||||||
local r_stations = {}
|
local r_stations = {}
|
||||||
|
|
||||||
for station_i, station in ipairs(stations) do
|
|
||||||
local r_item = station.item
|
|
||||||
if -r_item.amount >= station.requester_limit then
|
|
||||||
r_stations[#r_stations + 1] = station_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local p_stations = {}
|
local p_stations = {}
|
||||||
|
|
||||||
for station_i, station in ipairs(stations) do
|
for station_i, station in pairs(stations) do
|
||||||
local p_item = station.item
|
if station.deliveries_total < station.train_limit then
|
||||||
if p_item.amount >= station.provider_limit then
|
local item_amount = get_item_amount(station, item_id)
|
||||||
p_stations[#p_stations + 1] = station_i
|
local delivery_amount = station.delivery_amount
|
||||||
|
|
||||||
|
if -(item_amount + delivery_amount) >= station.requester_limit then
|
||||||
|
table.insert(r_stations, station_i)
|
||||||
|
elseif item_amount + delivery_amount >= station.provider_limit then
|
||||||
|
table.insert(p_stations, station_i)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--we do not dispatch more than one train per station per tick
|
--we do not dispatch more than one train per station per tick
|
||||||
|
|
||||||
if #r_stations > 0 and #p_stations > 0 then
|
if #r_stations > 0 and #p_stations > 0 then
|
||||||
if #r_stations <= #p_stations then
|
if #r_stations <= #p_stations then
|
||||||
local last_p_station_i = stations[r_stations[1]].last_p_station_i
|
--backpressure, prioritize locality
|
||||||
local i = 1
|
for i, r_station_i in ipairs(r_stations) do
|
||||||
while true do
|
local best = 1
|
||||||
if i > #p_stations then
|
local best_dist = math.huge
|
||||||
i = 1
|
local highest_prior = -math.huge
|
||||||
break
|
for j, p_station_i in ipairs(p_stations) do
|
||||||
elseif p_stations[i] > last_p_station_i then
|
local d = get_distance(stations, r_station_i, p_station_i)
|
||||||
break
|
local prior = stations[p_station_i].priority
|
||||||
else
|
if prior > highest_prior then
|
||||||
i = i + 1
|
best = j
|
||||||
|
best_dist = d
|
||||||
|
highest_prior = prior
|
||||||
|
elseif prior == highest_prior and d < best_dist then
|
||||||
|
best = j
|
||||||
|
best_dist = d
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for j = 1, #r_stations do
|
send_train_between(stations, r_station_i, p_stations[best])
|
||||||
send_train_between(stations, r_stations[j], p_stations[i])
|
table.remove(p_stations, best)
|
||||||
|
|
||||||
i = i%#p_stations + 1
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local last_r_station_i = stations[p_stations[1]].last_r_station_i
|
--prioritize round robin
|
||||||
local i = 1
|
for j, p_station_i in ipairs(p_stations) do
|
||||||
while true do
|
local best = 1
|
||||||
if i > #r_stations then
|
local highest_prior = -math.huge
|
||||||
i = 1
|
local last_r_station_i = stations[p_station_i].last_r_station_i
|
||||||
break
|
for i, r_station_i in ipairs(r_stations) do
|
||||||
elseif r_stations[i] > last_r_station_i then
|
local prior = stations[r_station_i].priority
|
||||||
break
|
if r_stations[i] > last_r_station_i then
|
||||||
else
|
prior = prior + .5
|
||||||
i = i + 1
|
end
|
||||||
|
if prior > highest_prior then
|
||||||
|
best = i
|
||||||
|
highest_prior = prior
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for j = 1, #p_stations do
|
send_train_between(stations, r_stations[best], p_station_i)
|
||||||
send_train_between(stations, r_stations[i], p_stations[j])
|
table.remove(r_stations, best)
|
||||||
|
end
|
||||||
i = i%#p_stations + 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user