require ("util")
require ("__base__.prototypes.entity.pipecovers")
require ("circuit-connector-sprites")
require ("__base__.prototypes.entity.assemblerpipes")

--Control lua handles p lab rejecting items of normal, "", nil, or common quality
local simulations = require("__base__.prototypes.factoriopedia-simulations")
local hit_effects = require("__base__.prototypes.entity.hit-effects")
local sounds = require("__base__.prototypes.entity.sounds")
local combustion_furnace_anim_speed = 0.5
local combustion_furnace_uppertank_shift = {1, -0.8}

--Controls other building stuff
function electric_mining_drill_smoke()
  return
  {
    priority = "high",
    filename = "__base__/graphics/entity/electric-mining-drill/electric-mining-drill-smoke.png",
    line_length = 6,
    width = 48,
    height = 72,
    frame_count = 30,
	repeat_count = 2,
    animation_speed = electric_drill_animation_speed,
    shift = util.by_pixel(0, 3),
    scale = 1
  }
end
function electric_mining_drill_smoke_front()
  return
  {
    priority = "high",
    filename = "__base__/graphics/entity/electric-mining-drill/electric-mining-drill-smoke-front.png",
    line_length = 6,
    width = 148,
    height = 132,
    frame_count = 30,
	repeat_count = 2,
    animation_speed = electric_drill_animation_speed,
--    shift = util.by_pixel(-3, 9),
    shift = util.by_pixel(0, 3),
    scale = 1
  }
end

function make_4way_animation_from_spritesheet(animation)
    local function make_animation_layer(idx, anim)
      local frame_count = anim.frame_count or 1
      local start_frame = frame_count * idx
      local x = 0
      local y = 0
      if anim.line_length then
        y = anim.height * math.floor(start_frame / (anim.line_length or 1))
        if anim.line_length > frame_count then
          error("single line must not contain animations for multiple directions when line_lenght is specified: " .. anim.filename)
        end
      else
        x = idx * anim.width
        
      end
      return
      {
        filename = anim.filename,
        priority = anim.priority or "high",
        flags = anim.flags,
        x = x,
        y = y,
        width = anim.width,
        height = anim.height,
        frame_count = anim.frame_count,
        line_length = anim.line_length,
        repeat_count = anim.repeat_count,
        shift = anim.shift,
        draw_as_shadow = anim.draw_as_shadow,
        draw_as_glow = anim.draw_as_glow,
        draw_as_light = anim.draw_as_light,
        apply_runtime_tint = anim.apply_runtime_tint,
        tint_as_overlay = anim.tint_as_overlay or false,
        animation_speed = anim.animation_speed,
        scale = anim.scale or 1,
        tint = anim.tint,
        blend_mode = anim.blend_mode,
        load_in_minimal_mode = anim.load_in_minimal_mode,
        premul_alpha = anim.premul_alpha,
        generate_sdf = anim.generate_sdf
      }
    end
  
    local function make_animation(idx)
      if animation.layers then
        local tab = { layers = {} }
        for k,v in ipairs(animation.layers) do
          table.insert(tab.layers, make_animation_layer(idx, v))
        end
        return tab
      else
        return make_animation_layer(idx, animation)
      end
    end
  
    return
    {
      north = make_animation(0),
      east = make_animation(1),
      south = make_animation(2),
      west = make_animation(3)
    }
end
local function spritesheet_variations(count, line_length, base)
  local variations = {}
  for i = 1, count do
    local new = table.deepcopy(base)
    new.x = ((i-1) % line_length) * base.width
    new.y = math.floor((i-1) / line_length) * base.height
    table.insert(variations, new)
  end
  return variations
end
data:extend({
--======================--
--=== Combustion Furnace ===--
--======================--
  {
    type = "recipe-category",
    name = "vesta-combustion",
  },
  {
    type = "assembling-machine",
    name = "vesta-combustion-furnace",
    icon = "__skewer_planet_vesta_assets__/graphics/icons/combustion_furnace.png",
    flags = {"placeable-neutral","placeable-player", "player-creation"},
    minable = {mining_time = 2, result = "vesta-combustion-furnace"},
    max_health = 350,
    corpse = "medium-remnants",
    dying_explosion = "medium-explosion",
    icon_draw_specification = {shift = {0, -0.3}},
    circuit_wire_max_distance = furnace_circuit_wire_max_distance,
    circuit_connector = circuit_connector_definitions["electric-furnace"],
    alert_icon_shift = util.by_pixel(0, -12),
    resistances =
    {
      {
        type = "fire",
        percent = 100
      },
      {
        type = "impact",
        percent = 100
      }
    },
    fluid_boxes =
    {
      {
        production_type = "output",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="output", direction = defines.direction.north, position = {0, -1} }},
        secondary_draw_orders = { north = -1, west = -1 }
      },
      {
        production_type = "output",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="output", direction = defines.direction.south, position = {0, 1} }},
        secondary_draw_orders = { north = -1, west = -1 }
      },
      {
        production_type = "input",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="input", direction = defines.direction.west, position = {-1, 0} }},
        secondary_draw_orders = { north = -1, west = -1 }
      },
      {
        production_type = "input",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="input", direction = defines.direction.east, position = {1, 0} }},
        secondary_draw_orders = { north = -1, west = -1 }
      }
    },
    fluid_boxes_off_when_no_fluid_recipe = true,
    impact_category = "metal",
    open_sound = sounds.electric_large_open,
    close_sound = sounds.electric_large_close,
    working_sound =
    {
		sound = { filename = "__skewer_planet_vesta_assets__/sounds/isotopic_centrifuge.ogg", volume = 0.85, },
		audible_distance_modifier = 1.5,
		fade_in_ticks = 4,
		fade_out_ticks = 20
    },
	collision_box = {{-1.2, -1.2}, {1.2, 1.2}},
	selection_box = {{-1.5, -1.5}, {1.5, 1.5}},
    drawing_box = {{-1, -1}, {1, 1}},
    damaged_trigger_effect = hit_effects.entity(),
    fast_replaceable_group = "furnace",
    graphics_set = {
        animation = {
			layers = {
				{
				filename = "__skewer_planet_vesta_assets__/graphics/entity/combustion-furnace/combustion_furnace_base.png",
				width = 239,
				height = 291,
				line_length = 1,
				repeat_count = 32,
				frame_count = 1,
				shift = {0, -0.5},
				scale = 0.5,
				animation_speed = combustion_furnace_anim_speed
				},
				{
				filename = "__skewer_planet_vesta_assets__/graphics/entity/combustion-furnace/combustion_furnace_tank_layer_top.png",
				width = 2816/8,
				height = 1028/4,
				line_length = 8,
				frame_count = 32,
				shift = combustion_furnace_uppertank_shift,
				scale = 0.5,
				animation_speed = combustion_furnace_anim_speed
				}
			}
		},
		working_visualisations = {
			{
				fadeout = true,
				animation = {
					layers = {
						{
						filename = "__skewer_planet_vesta_assets__/graphics/entity/combustion-furnace/electric-furnace-light.png",
						width = 202,
						height = 202,
						line_length = 1,
						repeat_count = 32,
						frame_count = 1,
						shift = {0, -0.05},
						scale = 0.5,
						blend_mode = "additive",
						draw_as_glow = true,
						animation_speed = combustion_furnace_anim_speed
						},
						{
						filename = "__skewer_planet_vesta_assets__/graphics/entity/combustion-furnace/combustion_furnace_tank_layer_top.png",
						width = 2816/8,
						height = 1028/4,
						line_length = 8,
						frame_count = 32,
						shift = combustion_furnace_uppertank_shift,
						scale = 0.5,
						animation_speed = combustion_furnace_anim_speed
						},
						{
						filename = "__skewer_planet_vesta_assets__/graphics/entity/combustion-furnace/combustion_furnace_heater.png",
						width = 480/8,
						height = 224/4,
						line_length = 8,
						frame_count = 32,
						shift = {0, 0.9},
						scale = 0.5,
						draw_as_glow = true,
						animation_speed = combustion_furnace_anim_speed
						},
--						{
--						filename = "__skewer_planet_vesta_assets__/graphics/entity/combustion-furnace/electric-furnace-ground-light.png",
--						width = 166,
--						height = 124,
--						line_length = 1,
--						repeat_count = 32,
--						frame_count = 1,
--						shift = {0, 2},
--						scale = 0.5,
--						blend_mode = "additive",
--						draw_as_glow = true,
--						animation_speed = combustion_furnace_anim_speed
--						},
					}
				},
			},
			{
				light = {
				type = "basic",
				intensity = 2,
				size = 15
				}
			}
		}
	},
    crafting_categories = {"vesta-combustion"},
    crafting_speed = 2,
    energy_source =
    {
      type = "electric",
      usage_priority = "secondary-input",
      emissions_per_minute = { pollution = 4 },
    },
    energy_usage = "50kW",
    module_slots = 4,
    effect_receiver = { base_effect = { productivity = 0.7 }},
    allowed_effects = {"consumption", "speed", "productivity", "pollution"}
  },

--====================--
--=== Electrolyzer ===--
--====================--
  {
    type = "recipe-category",
    name = "vesta-zap",
  },
  {
    type = "assembling-machine",
    name = "vesta-electrolyzer",
    icon = "__skewer_planet_vesta_assets__/graphics/icons/electrolyzer.png",
    flags = {"placeable-neutral","placeable-player", "player-creation"},
    minable = {mining_time = 1.5, result = "vesta-electrolyzer"},
    max_health = 150,
    corpse = "medium-remnants",
    dying_explosion = "medium-explosion",
    icon_draw_specification = {shift = {0, -0.3}},
    circuit_wire_max_distance = furnace_circuit_wire_max_distance,
    circuit_connector = circuit_connector_definitions["electric-furnace"],
    alert_icon_shift = util.by_pixel(0, -12),
    resistances =
    {
      {
        type = "fire",
        percent = 100
      },
      {
        type = "impact",
        percent = 100
      }
    },
    fluid_boxes =
    {
      {
        production_type = "output",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="output", direction = defines.direction.north, position = {0, -1} }},
        secondary_draw_orders = { north = -1 }
      },
      {
        production_type = "input",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="input", direction = defines.direction.south, position = {0, 1} }},
        secondary_draw_orders = { north = -1 }
      },
      {
        production_type = "input",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="input", direction = defines.direction.west, position = {-1, 0} }},
        secondary_draw_orders = { north = -1 }
      },
      {
        production_type = "input",
        pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 1000,
        pipe_connections = {{ flow_direction="input", direction = defines.direction.east, position = {1, 0} }},
        secondary_draw_orders = { north = -1 }
      }
    },
    fluid_boxes_off_when_no_fluid_recipe = true,
    impact_category = "metal",
    open_sound = sounds.electric_large_open,
    close_sound = sounds.electric_large_close,
    working_sound =
    {
		sound = { filename = "__skewer_planet_vesta_assets__/sounds/metallurgic_infuser.ogg", volume = 0.85, },
		audible_distance_modifier = 1.5,
		fade_in_ticks = 4,
		fade_out_ticks = 20
    },
	collision_box = {{-1.2, -1.2}, {1.2, 1.2}},
	selection_box = {{-1.5, -1.5}, {1.5, 1.5}},
    drawing_box = {{-1, -1}, {1, 1}},
    damaged_trigger_effect = hit_effects.entity(),
    fast_replaceable_group = "assembling-machine",
    graphics_set = {
        animation = {
			layers = {
				{
				filename = "__skewer_planet_vesta_assets__/graphics/entity/electrolysis/electrolyzer.png",
				width = 202,
				height = 221,
				line_length = 1,
				repeat_count = 19,
				frame_count = 1,
				shift = {0, -0.35},
				scale = 0.50,
				animation_speed = 0.75
				}
			}
		},
		working_visualisations = {
			{
				fadeout = true,
				animation = {
					layers = {
						{
						filename = "__skewer_planet_vesta_assets__/graphics/entity/electrolysis/working.png",
						width = 202,
						height = 221,
						line_length = 1,
						repeat_count = 19,
						frame_count = 1,
						shift = {0, -0.5},
						scale = 0.50,
						animation_speed = 0.5,
						blend_mode = "additive",
						draw_as_glow = true
						},
						{
						filename = "__skewer_planet_vesta_assets__/graphics/entity/electrolysis/charge_flip.png",
						width = 1200/8,
						height = 1446/3,
						line_length = 8,
						frame_count = 19,
						shift = {0, -2},
						scale = 0.5,
						blend_mode = "additive",
						draw_as_glow = true,
						animation_speed = 0.5
						},
						{
						filename = "__skewer_planet_vesta_assets__/graphics/entity/electrolysis/charge.png",
						width = 1200/8,
						height = 1446/3,
						line_length = 8,
						frame_count = 19,
						shift = {0, -2},
						scale = 0.5,
						blend_mode = "additive",
						draw_as_glow = true,
						animation_speed = 0.5
						}
					}
				},
			},
			{
				light = {
				type = "basic",
				intensity = 2,
				size = 15
				}
			}
		}
	},
    crafting_categories = {"vesta-zap"},
    crafting_speed = 1.5,
    energy_source =
    {
      type = "electric",
      usage_priority = "secondary-input",
      emissions_per_minute = { pollution = 4 },
    },
    energy_usage = "500kW",
    module_slots = 3,
    allowed_effects = {"consumption", "speed", "productivity", "pollution"},
    effect_receiver = { base_effect = { productivity = 0.25 }},
  },
  
 
--===================--
--=== Supermagnet ===--
--===================--
  {
    type = "recipe-category",
    name = "magnetizing",
  },
  {
    type = "assembling-machine",
    name = "supermagnet",
    icon = "__skewer_planet_vesta_assets__/graphics/icons/supermagnet.png",
    flags = {"placeable-neutral","placeable-player", "player-creation"},
    minable = {mining_time = 0.2, result = "supermagnet"},
    max_health = 2000,
    corpse = "medium-remnants",
    dying_explosion = "medium-explosion",
    icon_draw_specification = {shift = {0, -0.3}},
    circuit_wire_max_distance = assembling_machine_circuit_wire_max_distance,
    circuit_connector = circuit_connector_definitions["assembling-machine"],
    alert_icon_shift = util.by_pixel(0, -12),
    resistances =
    {
      {
        type = "fire",
        percent = 100
      },
      {
        type = "impact",
        percent = 100
      }
    },
    fluid_boxes_off_when_no_fluid_recipe = false,
    fluid_boxes =
    {
      {
        production_type = "input",
        --pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 100,
        pipe_connections = {{ flow_direction="input", direction = defines.direction.north, position = {-1, -1} }},
        secondary_draw_orders = { north = -1 }
      },
      {
        production_type = "output",
        --pipe_picture = assembler2pipepictures(),
        pipe_covers = pipecoverspictures(),
        volume = 100,
        pipe_connections = {{ flow_direction="output", direction = defines.direction.north, position = {1, -1} }},
        secondary_draw_orders = { north = -1 }
      },
    },
    open_sound = sounds.machine_open,
    close_sound = sounds.machine_close,
    impact_category = "metal-large",
    working_sound =
    {
      sound = { filename = "__skewer_planet_vesta_assets__/sounds/chargepad.ogg", volume = 0.85 },
      audible_distance_modifier = 0.5,
      fade_in_ticks = 4,
      fade_out_ticks = 20
    },
	collision_box = {{-1.2, -1.2}, {1.2, 1.2}},
	selection_box = {{-1.5, -1.5}, {1.5, 1.5}},
    drawing_box = {{-1, -1}, {1, 1}},
    damaged_trigger_effect = hit_effects.entity(),
--	drawing_box_vertical_extension = 0.2,
    fast_replaceable_group = "assembling-machine",
    graphics_set = {
		animation = {
			north = {
				layers = {
					{
					filename = "__skewer_planet_vesta_assets__/graphics/entity/supermagnet/north.png",
					width = 192,
					height = 256,
					line_length = 1,
					repeat_count = 64,
					shift = {0, 0},
					animation_speed = 0.75,
					scale = 0.5,
					run_mode = "forward"
					}
				}
			},
			south = {
				layers = {
					{
					filename = "__skewer_planet_vesta_assets__/graphics/entity/supermagnet/south.png",
					width = 192,
					height = 256,
					line_length = 1,
					repeat_count = 64,
					shift = {0, 0},
					animation_speed = 0.75,
					scale = 0.5,
					run_mode = "forward"
					}
				}
			},
			west = {
				layers = {
					{
					filename = "__skewer_planet_vesta_assets__/graphics/entity/supermagnet/east.png",
					width = 192,
					height = 256,
					line_length = 1,
					repeat_count = 64,
					shift = {0, 0},
					animation_speed = 0.75,
					scale = 0.5,
					run_mode = "forward"
					}
				}
			},
			east = {
				layers = {
					{
					filename = "__skewer_planet_vesta_assets__/graphics/entity/supermagnet/west.png",
					width = 192,
					height = 256,
					line_length = 1,
					repeat_count = 64,
					shift = {0, 0},
					animation_speed = 0.75,
					scale = 0.5,
					run_mode = "forward"
					},
				}
			},
		},
		working_visualisations = {
		{
		animation = 
			{
				filename = "__skewer_planet_vesta_assets__/graphics/entity/supermagnet/glow.png",
				priority = "extra-high",
				size = {268,258},
				width = 268,
				height = 258,
				shift = {0,-0.5},
				scale = 0.4,
				line_length = 8,
				frame_count = 64,
				draw_as_glow = true,
				blend_mode = "additive"
			}
		},
			{
				light = {
				type = "basic",
				intensity = 3,
				size = 20
				}
			}
		}
	},
    crafting_categories = {"magnetizing"},
    crafting_speed = 2,
    energy_source =
    {
      type = "electric",
      usage_priority = "secondary-input",
      emissions_per_minute = { pollution = 4 },
    },
    energy_usage = "5MW",
    module_slots = 8,
    allowed_effects = {"consumption", "speed", "productivity", "pollution"},
  },
})