Altaric

Caravane PalOS

Jul 26th, 2021
1,011
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. print("Hello.")
  2. print(" ")
  3. event.ignoreAll()
  4. DEBUG_ACTIVE=false
  5. BELTSPEED=470
  6. PIPESPEED=600
  7. ZOOM = 1.6
  8. VERSION="Caravan PalOS v1.1"
  9. THRESHOLD = 1  -- how many stacks of each item ?
  10. THRESHOLDS = {}
  11. THRESHOLDS["Concrete"] = 32
  12. THRESHOLDS["Cable"] = 2
  13. THRESHOLDS["Iron Plate"] = 3
  14. THRESHOLDS["Copper Sheet"] = 2
  15. THRESHOLDS["Plastic"] = 2
  16. THRESHOLDS["Motor"] = 2
  17. THRESHOLDS["Modular Frame"] = 2
  18. THRESHOLDS["Encased Industrial Beam"] = 1
  19. THRESHOLDS["Caterium Ingot"] = 0
  20. -- THRESHOLDS["Motor"] = 3
  21.  
  22.  
  23. -- Some basic functions toolbox
  24. raise = computer.panic
  25. -- String padding
  26. function pad(str, len)  return (' '):rep(len-#str)..str end  
  27. -- Sorted iterator
  28. function spairs(t)    local keys = {}    for k in pairs(t) do keys[#keys+1] = k end    table.sort(keys)    local i = 0
  29.     return function()        i = i + 1        if keys[i] then            return keys[i], t[keys[i]]        end    end end
  30. -- Does a string startrs with another ?
  31. function string.starts(String,Start)
  32.    return string.sub(String,1,string.len(Start))==Start
  33. end
  34. -- Debug print
  35. function debug(text, force) if (DEBUG_ACTIVE or force==1) then print(text) end  end
  36. -- GPU functions
  37. function gprint(s)
  38.     print(s)
  39.     if screen then
  40.         gpu:setText(1+math.floor(math.floor(gpu_line/h)*w/2), gpu_line % h, s)
  41.         gpu_line=gpu_line+1
  42.     end
  43. end
  44. function gflush()
  45.     if screen then
  46.         gpu_line = 0
  47.         gpu:setBackground(0,0,0,0)
  48.         gpu:fill(0,0,w,h," ")
  49.         gpu:setForeground(1,1,1,1) 
  50.     end
  51. end
  52.  
  53. -- Hook network components
  54. components = component.findComponent("")
  55. debug(#components .. " components in network")
  56. splitters = {}
  57. mergers = {}
  58. mall = {}
  59. for n, c in pairs(components) do
  60.   comp = component.proxy(c)
  61.   type = comp:getType().name
  62.   debug(n..": "..type)
  63.   if     (type=="CodeableSplitter_C") then table.insert(splitters, comp)
  64.   elseif (type=="CodeableMerger_C")then  table.insert(mergers, comp)
  65.   elseif type=="Build_Screen_C" then screen=comp
  66.   elseif type=="Build_TrainDockingStation_C" then freight=comp table.insert(mall, freight:getInventories()[1])
  67.   elseif type=="Build_TrainStation_C" then station=comp
  68.   elseif type=="LargeControlPannel" then board=comp
  69.   elseif type=="LargeVerticalControlPannel" then board=comp
  70.   end
  71. end
  72. print(#splitters.." codeable splitters found.")
  73. print(#mergers.." codeable mergers found.")
  74. -- assert(screen ~= nil, "No Screen found.")
  75. assert(freight ~= nil, "No freight found.")
  76. -- assert(station ~= nil, "No station found.")
  77. -- assert(container ~= nil, "No codeable merger found.")
  78. trains = freight:getTrackGraph():getTrains()
  79. for _, i in pairs(trains) do
  80.     if i:getName() == "Caravane" then train = i end
  81. end
  82. assert(train ~= nil, "'Caravane' train not found.")
  83. for _, i in pairs(train:getVehicles()) do
  84.     for _, j in pairs(i:getInventories()) do table.insert(mall, j) end
  85.     event.listen(i)
  86. end
  87. event.listen(freight)
  88.  
  89. print("Mall has "..#mall.." inventories")
  90. -- mall:flush()
  91.  
  92. -- I/O Pannel
  93. -- board = component.proxy(component.findComponent("Keybd"))[1]
  94. if board then
  95.   event.listen(board)
  96.   modules = board.getModules(board)
  97.  
  98.   -- Register control panel buttons as events
  99.   panels = {}
  100.   for n, m in pairs(modules) do
  101.     event.listen(m)
  102.     debug("Listening to module " .. n .. " " .. m.internalName)  
  103.     -- if m:getType().name == "ModuleTextDisplay_C" then table.insert(panels, m) end
  104.     if m:getType().name == "ModuleScreen_C" then screen = m end
  105.   end
  106. end
  107.  
  108. -- containerConnector = container:getFactoryConnectors()[1]
  109. -- if (containerConnector.direction ~= 0) then containerConnector = container:getFactoryConnectors()[2] end
  110. -- if (containerConnector.direction ~= 0) then containerConnector = container:getFactoryConnectors()[3] end
  111. -- if (containerConnector.direction ~= 0) then raise("Wrong connector direction!") end
  112. --event.listen(containerConnector)
  113.  
  114. transit = {}
  115.  
  116. for n, m in pairs(splitters) do
  117.   i = m:getInput(0)
  118.   if i.type then
  119.     print(m.nick.." has "..i.type.name)
  120.   end
  121. --  event.listen(m:getFactoryConnectors()[2])
  122. end
  123. print("")
  124.  
  125. -- Index the mall inventory and add what's on the belts
  126. function getTotal()
  127.     total = {}
  128.     print("Mall has "..#mall.." inventories")
  129.     for _, inv in pairs(mall) do
  130.         for i = 0, inv.size-1, 1 do
  131.             s = inv:getStack(i)
  132.             if (s.count>0) then    
  133.                 t = s.item.type.name
  134.                 total[t] = (total[t] or 0) + s.count
  135.             end
  136.         end
  137.     end
  138.     for n, c in pairs(transit) do total[n] = (total[n] or 0) end
  139.     for k, i in spairs(total) do
  140.         gprint(pad(""..i, 4).." "..pad(""..(transit[k] or 0), 2).." | "..k)
  141.     end
  142.     for n, c in pairs(transit) do
  143.         total[n] = (total[n] or 0) + c
  144.     end
  145. end
  146.  
  147. -- GPU Stuff
  148. gpu = computer.getPCIDevices(findClass("FINComputerGPU"))[1]
  149. if gpu and screen then
  150.     -- event.listen(gpu)
  151.     gpu:bindScreen(screen)
  152.     w,h = screen:getSize()
  153.     gpu:setSize(math.floor(22*w/ZOOM), math.floor(10*h/ZOOM))
  154.     w,h = gpu:getSize()
  155.     debug("Screen size= "..w.."x"..h)
  156. else
  157.     debug("No GPU found.")
  158. end
  159.  
  160. longUpdate = 3
  161. event.clear()
  162. while true do
  163.     -- Game load check
  164.     if (#mall < 3) then computer.beep(0) computer.reset() end
  165.    
  166.     -- Manage events
  167.     if longUpdate < 0 then e, sender, i, j = event.pull(5) else e = " " end
  168.     while e ~= nil do
  169.         e, sender, i, j = event.pull(0.126)
  170.         if e then lastEvent = e.." from "..sender:getType().name.." "..(i or "").." "..(j or "") computer.beep(1) end
  171.     end
  172.     -- longUpdate = longUpdate -1
  173.    
  174.     gflush()
  175.     gprint(VERSION.." "..(lastEvent or ""))
  176.     gprint("Freight status: "..freight.status)
  177.  
  178.     -- Transfer from transit to mall
  179.     for input = 0, 2, 1 do
  180.         for _, container in pairs(mergers) do
  181.             i = container:getInput(input) while i.type do
  182.                 debug(""..input..i.type.name)
  183.                 if container:transferItem(input) then
  184.                     transit[i.type.name] = math.max((transit[i.type.name] or 0) - 1, 0)
  185.                     longUpdate = 3
  186.                 end
  187.                 i = container:getInput(input)
  188.             end
  189.         end
  190.     end
  191.  
  192.     getTotal()
  193.  
  194.     if freight.status ~= 0 then     goto continue   end -- If the station is not idle, do not pull items.
  195.    
  196.     for n, m in pairs(splitters) do
  197.         i = m:getInput(0)  -- Right side
  198.         if i.type then
  199.             t = i.type.name
  200.             debug(i.type.name.." Got "..(total[t] or 0).." Need ".. i.type.max*(THRESHOLDS[t] or THRESHOLD))
  201.             if ((total[t] or 0) < (i.type.max*(THRESHOLDS[t] or THRESHOLD) -10)) or (((transit[t] or 0) == 0) and ((total[t] or 0) < i.type.max*(THRESHOLDS[t] or THRESHOLD)-2 )) then
  202.                 -- debug("Trying to push")
  203.                 if (m:transferItem(0) or m:transferItem(2)) then   -- send left or right
  204.                     transit[t] = (transit[t] or 0) + 1
  205.                     longUpdate = 3
  206.                     -- debug("pushed")
  207.                 end
  208.             else
  209.                 m:transferItem(1) -- send middle
  210.             end
  211.         end
  212.     end
  213.     ::continue::
  214.     if screen then gpu:flush() end
  215. end
  216.  
RAW Paste Data