@@ -156,6 +156,7 @@ function OnTick(event)
156
156
update_interval = tick - global .tick_interval_start ,
157
157
provided_by_stop = global .Dispatcher .Provided_by_Stop ,
158
158
requests_by_stop = global .Dispatcher .Requests_by_Stop ,
159
+ new_deliveries = global .Dispatcher .new_Deliveries ,
159
160
deliveries = global .Dispatcher .Deliveries ,
160
161
available_trains = global .Dispatcher .availableTrains ,
161
162
})
@@ -171,6 +172,7 @@ function OnTick(event)
171
172
global .Dispatcher .Requests = {}
172
173
global .Dispatcher .Provided_by_Stop = {}
173
174
global .Dispatcher .Requests_by_Stop = {}
175
+ global .Dispatcher .new_Deliveries = {}
174
176
end
175
177
end
176
178
276
278
277
279
---- ProcessRequest ----
278
280
279
- -- return a list ordered priority > #active_deliveries > item-count of {entity, network_id, priority, activeDeliveryCount, item, count, providing_threshold, providing_threshold_stacks, min_carriages, max_carriages, locked_slots}
281
+ -- returns the string "number1|number2" in consistent order: the smaller number is always placed first
282
+ local function sorted_pair (number1 , number2 )
283
+ return (number1 < number2 ) and (number1 .. ' |' .. number2 ) or (number2 .. ' |' .. number1 )
284
+ end
285
+
286
+ -- return a list of matching { entity1, entity2, network_id } each connecting the two surfaces. The list will be empty if surface1 == surface2 and it will be nil if there are no matching connections. The second return value will be the number of entries in the list.
287
+ local function find_surface_connections (surface1 , surface2 , force , network_id )
288
+ if surface1 == surface2 then return {}, 0 end
289
+
290
+ local surface_pair_key = sorted_pair (surface1 .index , surface2 .index )
291
+ local surface_connections = global .ConnectedSurfaces [surface_pair_key ]
292
+ if not surface_connections then return nil end
293
+
294
+ local matching_connections = {}
295
+ local count = 0
296
+ for entity_pair_key , connection in pairs (surface_connections ) do
297
+ if connection .entity1 .valid and connection .entity2 .valid then
298
+ if btest (network_id , connection .network_id )
299
+ and connection .entity1 .force == force and connection .entity2 .force == force then
300
+ count = count + 1
301
+ matching_connections [count ] = connection
302
+ end
303
+ else
304
+ if debug_log then log (" removing invalid surface connection " .. entity_pair_key .. " between surfaces " .. surface_pair_key ) end
305
+ surface_connections [entity_pair_key ] = nil
306
+ end
307
+ end
308
+
309
+ if count > 0 then
310
+ return matching_connections , count
311
+ else
312
+ return nil , nil
313
+ end
314
+ end
315
+
316
+ -- return a list ordered priority > #active_deliveries > item-count of {entity, network_id, priority, activeDeliveryCount, item, count, providing_threshold, providing_threshold_stacks, min_carriages, max_carriages, locked_slots, surface_connections}
280
317
local function getProviders (requestStation , item , req_count , min_length , max_length )
281
318
local stations = {}
282
319
local providers = global .Dispatcher .Provided [item ]
@@ -294,28 +331,34 @@ local function getProviders(requestStation, item, req_count, min_length, max_len
294
331
-- log("DEBUG: comparing 0x"..format("%x", band(requestStation.network_id)).." & 0x"..format("%x", band(stop.network_id)).." = 0x"..format("%x", band(matched_networks)) )
295
332
296
333
if stop .entity .force == force
297
- and stop .entity .surface == surface
298
334
and matched_networks ~= 0
299
335
-- and count >= stop.providing_threshold
300
336
and (stop .min_carriages == 0 or max_length == 0 or stop .min_carriages <= max_length )
301
- and (stop .max_carriages == 0 or min_length == 0 or stop .max_carriages >= min_length ) then -- check if provider can actually service trains from requester
337
+ and (stop .max_carriages == 0 or min_length == 0 or stop .max_carriages >= min_length ) then
338
+ -- check if provider can accept more trains
302
339
local activeDeliveryCount = # stop .active_deliveries
303
- local from_network_id_string = format (" 0x%x" , band (stop .network_id ))
304
340
if activeDeliveryCount and (stop .max_trains == 0 or activeDeliveryCount < stop .max_trains ) then
305
- if debug_log then log (" found " .. count .. " (" .. tostring (stop .providing_threshold ).. " )" .. " /" .. req_count .. " " .. item .. " at " .. stop .entity .backer_name .. " {" .. from_network_id_string .. " }, priority: " .. stop .provider_priority .. " , active Deliveries: " .. activeDeliveryCount .. " min_carriages: " .. stop .min_carriages .. " , max_carriages: " .. stop .max_carriages .. " , locked Slots: " .. stop .locked_slots ) end
306
- stations [# stations + 1 ] = {
307
- entity = stop .entity ,
308
- network_id = matched_networks ,
309
- priority = stop .provider_priority ,
310
- activeDeliveryCount = activeDeliveryCount ,
311
- item = item ,
312
- count = count ,
313
- providing_threshold = stop .providing_threshold ,
314
- providing_threshold_stacks = stop .providing_threshold_stacks ,
315
- min_carriages = stop .min_carriages ,
316
- max_carriages = stop .max_carriages ,
317
- locked_slots = stop .locked_slots ,
318
- }
341
+ -- check if surface transition is possible
342
+ local surface_connections , surface_connections_count = find_surface_connections (surface , stop .entity .surface , force , matched_networks )
343
+ if surface_connections then -- for same surfaces surface_connections = {}
344
+ local from_network_id_string = format (" 0x%x" , band (stop .network_id ))
345
+ if debug_log then log (" found " .. count .. " (" .. tostring (stop .providing_threshold ).. " )" .. " /" .. req_count .. " " .. item .. " at " .. stop .entity .backer_name .. " {" .. from_network_id_string .. " }, priority: " .. stop .provider_priority .. " , active Deliveries: " .. activeDeliveryCount .. " , min_carriages: " .. stop .min_carriages .. " , max_carriages: " .. stop .max_carriages .. " , locked Slots: " .. stop .locked_slots .. " , #surface_connections: " .. (surface_connections_count )) end
346
+ stations [# stations + 1 ] = {
347
+ entity = stop .entity ,
348
+ network_id = matched_networks ,
349
+ priority = stop .provider_priority ,
350
+ activeDeliveryCount = activeDeliveryCount ,
351
+ item = item ,
352
+ count = count ,
353
+ providing_threshold = stop .providing_threshold ,
354
+ providing_threshold_stacks = stop .providing_threshold_stacks ,
355
+ min_carriages = stop .min_carriages ,
356
+ max_carriages = stop .max_carriages ,
357
+ locked_slots = stop .locked_slots ,
358
+ surface_connections = surface_connections ,
359
+ surface_connections_count = surface_connections_count ,
360
+ }
361
+ end
319
362
end
320
363
end
321
364
end
@@ -324,12 +367,16 @@ local function getProviders(requestStation, item, req_count, min_length, max_len
324
367
sort (stations , function (a , b )
325
368
if a .priority ~= b .priority then -- sort by priority, will result in train queues if trainlimit is not set
326
369
return a .priority > b .priority
370
+ elseif a .surface_connections_count ~= b .surface_connections_count then -- sort providers without surface transition to top
371
+ return min (a .surface_connections_count , 1 ) < min (b .surface_connections_count , 1 )
327
372
elseif a .activeDeliveryCount ~= b .activeDeliveryCount then -- sort by #deliveries
328
373
return a .activeDeliveryCount < b .activeDeliveryCount
329
374
else
330
375
return a .count > b .count -- finally sort by item count
331
376
end
332
377
end )
378
+
379
+ if debug_log then log (" (getProviders) sorted providers: " .. serpent .block (stations )) end
333
380
return stations
334
381
end
335
382
@@ -511,11 +558,6 @@ function ProcessRequest(reqIndex, request)
511
558
local matched_network_id_string = format (" 0x%x" , band (providerData .network_id ))
512
559
513
560
if message_level >= 3 then printmsg ({" ltn-message.provider-found" , from_gps , tostring (providerData .priority ), tostring (providerData .activeDeliveryCount ), providerData .count , " [" .. itype .. " =" .. iname .. " ]" }, requestForce , true ) end
514
- -- if debug_log then
515
- -- for n, provider in pairs (providers) do
516
- -- log("Provider["..n.."] "..provider.entity.backer_name..": Priority "..tostring(provider.priority)..", "..tostring(provider.activeDeliveryCount).." deliveries, "..tostring(provider.count).." "..item.." available.")
517
- -- end
518
- -- end
519
561
520
562
-- limit deliverySize to count at provider
521
563
local deliverySize = count
@@ -628,14 +670,18 @@ function ProcessRequest(reqIndex, request)
628
670
schedule .records [# schedule .records + 1 ] = NewScheduleRecord (depot .entity .backer_name , " inactivity" , depot_inactivity )
629
671
630
672
-- make train go to specific stations by setting a temporary waypoint on the rail the station is connected to
631
- if from_rail and from_rail_direction then
673
+ -- schedules cannot have temporary stops on a different surface, those need to be added when the delivery is updated with a train on a different surface
674
+ if from_rail and from_rail_direction
675
+ and depot .entity .surface == from_rail .surface then
632
676
schedule .records [# schedule .records + 1 ] = NewTempScheduleRecord (from_rail , from_rail_direction )
633
677
else
634
678
if debug_log then log (" (ProcessRequest) Warning: creating schedule without temporary stop for provider." ) end
635
679
end
636
680
schedule .records [# schedule .records + 1 ] = NewScheduleRecord (from , " item_count" , " ≥" , loadingList )
637
681
638
- if to_rail and to_rail_direction then
682
+ if to_rail and to_rail_direction
683
+ and depot .entity .surface == to_rail .surface
684
+ and (from_rail and to_rail .surface == from_rail .surface ) then
639
685
schedule .records [# schedule .records + 1 ] = NewTempScheduleRecord (to_rail , to_rail_direction )
640
686
else
641
687
if debug_log then log (" (ProcessRequest) Warning: creating schedule without temporary stop for requester." ) end
@@ -680,6 +726,7 @@ function ProcessRequest(reqIndex, request)
680
726
681
727
if debug_log then log (" " .. loadingListItem .. " , " .. loadingList [i ].count .. " in " .. loadingList [i ].stacks .. " stacks " ) end
682
728
end
729
+ global .Dispatcher .new_Deliveries [# global .Dispatcher .new_Deliveries + 1 ] = selectedTrain .id
683
730
global .Dispatcher .Deliveries [selectedTrain .id ] = {
684
731
force = requestForce ,
685
732
train = selectedTrain ,
@@ -689,6 +736,7 @@ function ProcessRequest(reqIndex, request)
689
736
to = to ,
690
737
to_id = toID ,
691
738
network_id = providerData .network_id ,
739
+ surface_connections = providerData .surface_connections ,
692
740
shipment = shipment }
693
741
global .Dispatcher .availableTrains_total_capacity = global .Dispatcher .availableTrains_total_capacity - global .Dispatcher .availableTrains [selectedTrain .id ].capacity
694
742
global .Dispatcher .availableTrains_total_fluid_capacity = global .Dispatcher .availableTrains_total_fluid_capacity - global .Dispatcher .availableTrains [selectedTrain .id ].fluid_capacity
@@ -712,18 +760,7 @@ function ProcessRequest(reqIndex, request)
712
760
end
713
761
end
714
762
715
- script .raise_event (on_delivery_created_event , {
716
- train_id = selectedTrain .id ,
717
- train = selectedTrain ,
718
- from = from ,
719
- from_id = fromID ,
720
- to = to ,
721
- to_id = toID ,
722
- shipment = shipment
723
- })
724
-
725
- -- return train ID = delivery ID
726
- return selectedTrain .id
763
+ return selectedTrain .id -- deliveries are indexed by train.id
727
764
end
728
765
729
766
0 commit comments