Shadriu

Edited control.lua

Oct 16th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.23 KB | None | 0 0
  1. config = require "config"
  2.  
  3. local refresh_rate = 60
  4.  
  5. local look_offset = 0.5
  6. local look_distance = 1
  7. local assembler_surroundings_check_distance = 2
  8. local function get_assembler(entity)
  9.     local position = entity.position
  10.     local direction = entity.direction
  11.    
  12.     local area
  13.    
  14.     if direction == defines.direction.north then
  15.         area = {{position.x - look_offset, position.y - look_distance}, {position.x + look_offset, position.y - look_distance}}
  16.     elseif direction == defines.direction.west then
  17.         area = {{position.x - look_distance - 0.1, position.y - look_offset}, {position.x - look_distance + 0.1, position.y + look_offset}}
  18.     elseif direction == defines.direction.south then
  19.         area = {{position.x - look_offset, position.y + look_distance}, {position.x + look_offset, position.y + look_distance}}
  20.     elseif direction == defines.direction.east then
  21.         area = {{position.x + look_distance - 0.1, position.y - look_offset}, {position.x + look_distance + 0.1, position.y + look_offset}}
  22.     end
  23.    
  24.     return entity.surface.find_entities_filtered{area = area, type = "assembling-machine"}[1]
  25. end
  26.  
  27. local function get_recipe_from_wire(combinator, wire, recipe)
  28.     local ings = {}
  29.     if recipe then
  30.         for _, r in pairs(recipe.ingredients) do
  31.             ings[r.name] = r.amount
  32.         end
  33.     end
  34.    
  35.     local res
  36.     local n = 0
  37.     local cn = nil
  38.     --CHECK
  39.     if combinator.valid then
  40.         cn = combinator.get_circuit_network(wire, defines.circuit_connector_id.combinator_input)
  41.     end
  42.     local signals = {}
  43.     if cn then signals = cn.signals end
  44.     for _, signal in pairs(signals) do
  45.         local s = signal.signal.name
  46.         local c = ings[s] or 0
  47.        
  48.         if config.special_cases_r[s] then s = config.special_cases_r[s] end
  49.         if combinator.force.recipes[s] and combinator.force.recipes[s].enabled and signal.count - c > n then
  50.             res = s
  51.             n = signal.count - c
  52.         end
  53.     end
  54.    
  55.     return res, n
  56. end
  57.  
  58. local function get_recipe(combinator, recipe)
  59.     s1, c1 = get_recipe_from_wire(combinator, defines.wire_type.red, recipe)
  60.     s2, c2 = get_recipe_from_wire(combinator, defines.wire_type.green, recipe)
  61.    
  62.     if c2 > c1 then return s2 end
  63.     return s1
  64. end
  65.  
  66. local function find_in_global(combinator)
  67.     for i = 0, refresh_rate - 1 do
  68.         for ei, c in pairs(global.combinators[i]) do
  69.             if c.entity == combinator then return c, i, ei end
  70.         end
  71.     end
  72. end
  73.  
  74. local function on_built(event)
  75.     local entity = event.created_entity
  76.     --crafting-combinator
  77.     if entity.name == "crafting-combinator" then
  78.         local ei
  79.         local n
  80.         for i = 0, refresh_rate - 1 do
  81.             if not n or n >= #global.combinators[i] then
  82.                 ei = i
  83.                 n = #global.combinators[i]
  84.             end
  85.         end
  86.         entity.rotatable = true
  87.         table.insert(global.combinators[ei], {entity = entity, assembler = get_assembler(entity)})
  88.     end
  89.     if entity.type == "assembling-machine" then
  90.         local area = {
  91.             {entity.position.x - assembler_surroundings_check_distance, entity.position.y - assembler_surroundings_check_distance},
  92.             {entity.position.x + assembler_surroundings_check_distance, entity.position.y + assembler_surroundings_check_distance}
  93.         }
  94.         local combinators = entity.surface.find_entities_filtered{area = area, name = "crafting-combinator"}
  95.         for _, combinator in pairs(combinators) do
  96.             find_in_global(combinator).assembler = get_assembler(combinator)
  97.         end
  98.     end
  99.    
  100.     --recipe-combinator
  101.     if entity.name == "recipe-combinator" then
  102.         local ei
  103.         local n
  104.         for i = 0, refresh_rate - 1 do
  105.             if not n or n >= #global.recipe_combinators[i] then
  106.                 ei = i
  107.                 n = #global.recipe_combinators[i]
  108.             end
  109.         end
  110.        
  111.         entity.operable = false
  112.        
  113.         res = {entity = entity}
  114.         table.insert(global.recipe_combinators[ei], res)
  115.     end
  116. end
  117.  
  118. local function on_tick(event)
  119.     for _, combinator in pairs(global.combinators[event.tick % refresh_rate]) do
  120.         if combinator.assembler and combinator.assembler.valid then
  121.             combinator.assembler.recipe = get_recipe(combinator.entity)
  122.         end
  123.     end
  124.    
  125.     for _, combinator in pairs(global.recipe_combinators[event.tick % refresh_rate]) do
  126.         local recipe = get_recipe(combinator.entity, combinator.recipe)
  127.         local params = {}
  128.         if recipe then
  129.             recipe = combinator.entity.force.recipes[recipe]
  130.             combinator.recipe = recipe
  131.             for i, ing in pairs(recipe.ingredients) do
  132.                 local r = 0
  133.                 if tonumber(ing.amount) % 1 > 0 then r = 1 end
  134.                 table.insert(params, {signal = {type = ing.type, name = ing.name}, count = math.floor(tonumber(ing.amount)) + r, index = i})
  135.             end
  136.         end
  137.  
  138.         --CHECK
  139.         if combinator.entity.valid then
  140.             combinator.entity.get_or_create_control_behavior().parameters = {enabled = true, parameters = params}
  141.         end
  142.     end
  143. end
  144.  
  145. local function on_rotated(event)
  146.     local entity = event.entity
  147.     if entity.name == "crafting-combinator" then
  148.         find_in_global(entity).assembler = get_assembler(entity)
  149.     end
  150. end
  151.  
  152. local function on_destroyed(event)
  153.     local entity = event.entity
  154.     --crafting-combinator
  155.     if entity.name == "crafting-combinator" then
  156.         for i = 0, refresh_rate - 1 do
  157.             for ei, combinator in pairs(global.combinators[i]) do
  158.                 if combinator.entity == entity then
  159.                     table.remove(global.combinators[i], ei)
  160.                     return
  161.                 end
  162.             end
  163.         end
  164.     end
  165.     if entity.type == "assembling-machine" then
  166.         local area = {
  167.             {entity.position.x - assembler_surroundings_check_distance, entity.position.y - assembler_surroundings_check_distance},
  168.             {entity.position.x + assembler_surroundings_check_distance, entity.position.y + assembler_surroundings_check_distance}
  169.         }
  170.         local combinators = entity.surface.find_entities_filtered{area = area, name = "crafting-combinator"}
  171.         for _, combinator in pairs(combinators) do
  172.             find_in_global(combinator).assembler = get_assembler(combinator)
  173.         end
  174.     end
  175.    
  176.     --recipe-combinator
  177.     if entity.name == "recipe-combinator" then
  178.         for i = 0, refresh_rate - 1 do
  179.             for ei, v in pairs(global.recipe_combinators) do
  180.                 if v.entity == entity then
  181.                     table.remove(global.recipe_combinators[i], ei)
  182.                     break
  183.                 end
  184.             end
  185.         end
  186.     end
  187. end
  188.  
  189. script.on_init(function()
  190.     global.combinators = global.combinators or {}
  191.     global.recipe_combinators = global.recipe_combinators or {}
  192.     for i = 0, refresh_rate - 1 do
  193.         global.combinators[i] = global.combinators[i] or {}
  194.         global.recipe_combinators[i] = global.recipe_combinators[i] or {}
  195.     end
  196. end)
  197.  
  198. script.on_configuration_changed(function(data)
  199.     global.combinators = global.combinators or {}
  200.     global.recipe_combinators = global.recipe_combinators or {}
  201.     for i = 0, refresh_rate - 1 do
  202.         global.combinators[i] = global.combinators[i] or {}
  203.         global.recipe_combinators[i] = global.recipe_combinators[i] or {}
  204.     end
  205.    
  206.     if data.mod_changes["crafting_combinator"] then
  207.         for _, force in pairs(game.forces) do
  208.             if force.technologies["circuit-network"].researched then
  209.                 force.recipes["crafting-combinator"].enabled = true
  210.                 force.recipes["recipe-combinator"].enabled = true
  211.             end
  212.         end
  213.     end
  214. end)
  215.  
  216. script.on_event(defines.events.on_built_entity, on_built)
  217. script.on_event(defines.events.on_robot_built_entity, on_built)
  218.  
  219. script.on_event(defines.events.on_preplayer_mined_item, on_destroyed)
  220. script.on_event(defines.events.on_robot_pre_mined, on_destroyed)
  221. script.on_event(defines.events.on_entity_died, on_destroyed)
  222.  
  223. script.on_event(defines.events.on_tick, on_tick)
  224.  
  225. script.on_event(defines.events.on_player_rotated_entity, on_rotated)
Advertisement
Add Comment
Please, Sign In to add comment