
local ord={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r"}

local omni_fluids_prefixes = {"omniston-solvation-","omnic-hydrolyzation-","stone-omnisolvent-"}

script.on_event(defines.events.on_player_created, function(event)
local iteminsert = game.players[event.player_index].insert
iteminsert{name="burner-omnitractor-1", count=1}
end)

local start_with = function(a,b)
	return string.sub(a,1,string.len(b)) == b
end
local start_within_table = function(a,b)
	return string.sub(a,1,string.len(b)) == b
end

script.on_event(defines.events.on_player_crafted_item, function(event)
	log("was crafted: "..event.recipe.name)
end)



script.on_event(defines.events.on_research_finished, function(event)
	local tech = event.research
	if start_with(tech.name,"omni-sorting-") and not start_with(tech.name,"omni-sorting-electric-")then
		local length = string.len(tech.name)
		local lvl = 0
		local material = ""
		if string.sub(tech.name,length-1,length-1) == "-" then
			lvl = tonumber(string.sub(tech.name,length,length))
			material = string.sub(tech.name,string.len("omni-sorting-")+1,length-2)
		else
			lvl = tonumber(string.sub(tech.name,length-1,length))	
			material = string.sub(tech.name,string.len("omni-sorting-")+1,length-3)	
		end
		if lvl > 1 then
			for i=1,5 do
				for _,surface in pairs(game.surfaces) do
					for _,entity in pairs(surface.find_entities_filtered{force=event.research.force, name="omnitractor-"..i}) do
						if entity.recipe and entity.recipe.name == material.."-extraction-"..ord[lvl-1] then
							entity.recipe=material.."-extraction-"..ord[lvl]
						end
					end
				end
			end
			for i=1,lvl-1 do
				tech.force.recipes[material.."-extraction-"..ord[i]].enabled = false	
			end
		end
	end
	--tech: "omni-impure-"..omnisource[i].ore.name.."-extraction-"..l
	--Recipe: "impure-"..omnisource[i].ore.name.."-extraction-"..l
	if start_with(tech.name,"omni-impure-") then
		local length = string.len(tech.name)
		local lvl = 0
		local material = ""
		if string.sub(tech.name,length-1,length-1) == "-" then
			lvl = tonumber(string.sub(tech.name,length,length))
			material = string.sub(tech.name,string.len("omni-impure-")+1,length-1-string.len("-extraction-"))
		else
			lvl = tonumber(string.sub(tech.name,length-1,length))	
			material = string.sub(tech.name,string.len("omni-impure-")+1,length-2-string.len("-extraction-"))	
		end
		if lvl > 1 then
			for i=1,5 do
				for _,surface in pairs(game.surfaces) do
					for _,entity in pairs(surface.find_entities_filtered{force=event.research.force, name="omnitractor-"..i}) do
						if entity.recipe and entity.recipe.name == "impure-"..material.."-extraction-"..lvl-1 then
							entity.recipe="impure-"..material.."-extraction-"..lvl
						end
					end
				end
			end
			for _,surface in pairs(game.surfaces) do
				for _,entity in pairs(surface.find_entities_filtered{force=event.research.force, name="burner-omnitractor-1"}) do
					if entity.recipe and entity.recipe.name == "impure-"..material.."-extraction-"..lvl-1 then
						entity.recipe="impure-"..material.."-extraction-"..lvl
					end
				end
			end
			tech.force.recipes["impure-"..material.."-extraction-"..lvl-1].enabled = false
		end
	end
	for _,str in pairs(omni_fluids_prefixes) do
		if start_with(tech.name,str) then
			local length = string.len(tech.name)
			local lvl = 0
			local material = ""
			if string.sub(tech.name,length-1,length-1) == "-" then
				lvl = tonumber(string.sub(tech.name,length,length))
			else
				lvl = tonumber(string.sub(tech.name,length-1,length))
			end
			local fluid_buildings = {"chemical-plant","chemical-plant-2","chemical-plant-3","chemical-plant-3"}
			if game.active_mods["omnimatter_crystal"] then fluid_buildings = {"omni-plant"} end
			if lvl > 1 then
				for _,building in pairs(fluid_buildings) do
					for _,surface in pairs(game.surfaces) do
						for _,entity in pairs(surface.find_entities_filtered{force=event.research.force, name=building}) do
							if entity.recipe and entity.recipe.name == str..ord[lvl-1] then
								entity.recipe=str..ord[lvl]
							end
						end
					end
				end
				tech.force.recipes[str..ord[lvl-1]].enabled = false
			end
		end
	end
	--"distillation-"..f.fluid.name.."-"..ord[i]
	if start_with(tech.name,"distillation-") then
		local length = string.len(tech.name)
		local lvl = 0
		local material = ""
		if string.sub(tech.name,length-1,length-1) == "-" then
			lvl = tonumber(string.sub(tech.name,length,length))
			material = string.sub(tech.name,string.len("distillation-")+1,length-2)
		else
			lvl = tonumber(string.sub(tech.name,length-1,length))	
			material = string.sub(tech.name,string.len("distillation-")+1,length-3)	
		end
		local fluid_buildings = {"chemical-plant","chemical-plant-2","chemical-plant-3","chemical-plant-3"}
		if game.active_mods["omnimatter_crystal"] then fluid_buildings = {"omni-plant"} end
		if lvl > 1 then
			for _,building in pairs(fluid_buildings) do
				for _,surface in pairs(game.surfaces) do
					for _,entity in pairs(surface.find_entities_filtered{force=event.research.force, name=building}) do
						if entity.recipe and entity.recipe.name == "distillation-"..material.."-"..ord[lvl-1] then
							entity.recipe="distillation-"..material.."-"..ord[lvl]
						end
					end
				end
			end
			tech.force.recipes["distillation-"..material.."-"..ord[lvl-1]].enabled = false
		end
	end
end)
