SHARE
TWEET

control.lua

Kryzeth Aug 12th, 2017 212 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. script.on_event(defines.events.on_tick, function(event)
  2.     leech()
  3. end)
  4.  
  5. script.on_event(defines.events.on_built_entity, function(event)
  6.     if not string.find(event.created_entity.name, "burner") then return end
  7.     if not string.find(event.created_entity.name, "inserter") then return end
  8.   if not global.burner then
  9.     global.burner = {}
  10.   end
  11.   local burner = event.created_entity
  12.   table.insert(global.burner, burner)
  13.   check_burner(burner)
  14. end)
  15.  
  16. script.on_event(defines.events.on_robot_built_entity, function(event)
  17.     if not string.find(event.created_entity.name, "burner") then return end
  18.     if not string.find(event.created_entity.name, "inserter") then return end
  19.   if not global.burner then
  20.     global.burner = {}
  21.   end
  22.   local burner = event.created_entity
  23.   table.insert(global.burner, burner)
  24.   check_burner(burner)
  25. end)
  26.  
  27. function leech()
  28.   if not fuel_list then
  29.   --I recalculate the fuel list everytime because adding or removing mod/items can fuck it all up and this doesn't take very long
  30.    fuel_list = {}
  31.     for k, v in pairs (game.item_prototypes) do    
  32.       if v.fuel_value > 0 then
  33.         table.insert(fuel_list,v)
  34.       end
  35.     end
  36.   end
  37.   if global.burner_index == nil then
  38.   global.burner_index = 1
  39.   end
  40.   if global.burner == nil then return end
  41.   if global.burner[global.burner_index] == nil then return end
  42.   check_burner(global.burner[global.burner_index])
  43.   if global.burner_index >= #global.burner then
  44.     global.burner_index = 1
  45.   else
  46.     global.burner_index = global.burner_index + 1
  47.   end
  48. end
  49.  
  50.  
  51. function check_burner(burner)
  52.   if not burner then return end
  53.   if not burner.valid then
  54.   table.remove(global.burner, global.burner_index)
  55.   global.burner_index = global.burner_index -1
  56.   return
  57.   end
  58.   local send_to_target = false
  59.   if burner.drop_target ~= nil then
  60.     if burner.drop_target.get_fuel_inventory() ~= nil then
  61.       if burner.drop_target.get_fuel_inventory().get_item_count() < 1 then
  62.         send_to_target = true
  63.       end
  64.     end
  65.   end
  66.   if burner.pickup_target == nil then return end
  67.   if burner.get_item_count() < 1 or send_to_target then
  68.     leeched = burner.pickup_target
  69.     if leeched == nil then return end
  70.     if burner.held_stack.valid_for_read == false then          
  71.       for j, v in pairs (fuel_list) do
  72.         if leeched.get_item_count(v.name) > 0 then                
  73.           burner.held_stack.set_stack({name = v.name, count = 1})      
  74.           leeched.remove_item({name = v.name, count = 1})
  75.           return
  76.         end
  77.       end
  78.     end
  79.   end
  80. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top