User:Darkfrei/Steam Cooling

From Official Factorio Wiki
< User:Darkfrei
Revision as of 16:51, 16 February 2020 by Darkfrei (talk | contribs) (Created page with "<syntaxhighlight lang="lua"> local supported_types = {'storage-tank', 'pipe'} function is_value_in_table (value, tabl) for i, v in pairs (tabl) do if v == value then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
local supported_types = {'storage-tank', 'pipe'}

function is_value_in_table (value, tabl)
  for i, v in pairs (tabl) do
    if v == value then
      return true
    end
  end
  return false
end


function cool_storage_tank (entity, dt)
  local fluidbox = entity.fluidbox
  if fluidbox then
    for i = 1, #fluidbox do
      local fluid = fluidbox[i]
      if fluid then
        local name = fluid.name
        local temp = fluid.temperature
        local amount = fluid.amount
        local min_temp = game.fluid_prototypes[name].default_temperature
        local delta_temp = (0.99 ^ (dt/amount)) * (temp-min_temp)
        
        fluid.temperature = min_temp + delta_temp
        fluidbox[i] = fluid -- why?
      end
    end
  end
end


function cool_pipe (entity, dt)
  local fluidbox = entity.fluidbox
  if fluidbox then
    for i = 1, #fluidbox do
      local fluid = fluidbox[i]
      if fluid then
        local name = fluid.name
        local temp = fluid.temperature
        local amount = fluid.amount
        local min_temp = game.fluid_prototypes[name].default_temperature
        local delta_temp = (0.99 ^ (dt/amount)) * (temp-min_temp)
        
        fluid.temperature = min_temp + delta_temp
        fluidbox[i] = fluid -- why?
      end
    end
  end
end


function main_handler (handler, dt) -- dt is time in ticks
  -- game.print ('ok: '..dt)
  local entity = handler.entity
  if entity and entity.valid then
    if entity.type == 'storage-tank' then
      cool_storage_tank (entity, dt)
    elseif entity.type == 'pipe' then
      cool_pipe (entity, dt)
    end
    
    
    return true
  else
    -- must be deleted
    return false
  end
end


function on_tick () -- I love this function / darkfrei
  -- multiple handlers per tick
  local handlers_per_tick = handlers_per_tick_global_setting or 1
  if not global.handlers then
    global.handlers = {}
    return -- nothing to do in this tick
  end
  local id = global.next_handler_id or 1
  local n = 0
  while n < handlers_per_tick do
    local handler = global.handlers[id+n]
    n = n + 1
    if handler then
      -- do the function
      -----
      local max_id = #global.handlers -- how many ticks it needs to update? It's delta time!
      
      local valid = main_handler (handler, max_id/handlers_per_tick) -- returns false or nil if it was something wrong
      -----
      if not valid then
        
        if id < max_id then
          global.handlers[id] = global.handlers[max_id]
          global.handlers[max_id] = nil
        else -- id => max_id; the last one
          global.handlers[id] = nil
        end
      end
    else -- no handler, earlier was the last one
      global.next_handler_id = 1
      return
    end
  end
  -- the next tick next handlers
  global.next_handler_id = id + n
end


script.on_event(defines.events.on_tick, on_tick)


function onBuildHandler (entity) 
  local handler = {entity = entity}
  if not global.handlers then global.handlers = {} end
  table.insert(global.handlers, handler)
  game.print (#global.handlers)
end


script.on_event(defines.events.on_built_entity, function(event)
  local entity_type = event.created_entity.type
  if is_value_in_table (entity_type, supported_types) then 
    onBuildHandler (event.created_entity) 
  end
end)


script.on_event(defines.events.on_robot_built_entity, function(event)
  local entity_type = event.created_entity.type
  if is_value_in_table (entity_type, supported_types) then 
    onBuildHandler (event.created_entity) 
  end
end)