local kobold_x, kobold_y = EntityGetTransform(GetUpdatedEntityID())
local close_enemies = EntityGetInRadiusWithTag(kobold_x, kobold_y, 10, "enemy")

if #close_enemies > 0 then
	local attacker = nil

	-- prevent kobolds detecting themselves as enemies
	for _, enemy in ipairs(close_enemies) do
		if EntityGetName(enemy) ~= "$animal_kobold" then
			attacker = enemy
			break
		end
	end

	if attacker ~= nil then
		local no_cooldown = true

		if EntityGetAllChildren(GetUpdatedEntityID()) ~= nil then
			for i, ch in ipairs(EntityGetAllChildren(GetUpdatedEntityID())) do
				if EntityGetName(ch) == "sex_cooldown" then
					no_cooldown = false
					break
				end
			end
		end

		-- is_predator check for enemy works like enemies sex cooldown, they have it disabled for some time after sex
		if no_cooldown == true and
		ComponentGetValue2(EntityGetFirstComponent(GetUpdatedEntityID(), "DamageModelComponent"), "is_on_fire") == false and
		ComponentGetValue2(EntityGetFirstComponent(GetUpdatedEntityID(), "DamageModelComponent"), "mAirAreWeInWater") == false and
		ComponentGetValue2(EntityGetFirstComponent(attacker, "DamageModelComponent"), "is_on_fire") == false and
		ComponentGetValue2(EntityGetFirstComponent(attacker, "GenomeDataComponent"), "is_predator") == true and
		GameGetGameEffect(attacker, "CHARM") == 0 and
		GameGetGameEffect(attacker, "FROZEN") == 0 and
		GameGetGameEffect(attacker, "ELECTROCUTION") == 0 then

			-- for modified enemies that have base file and no entity name, like animals/crypt
			local fucker = GameTextGetTranslatedOrNot(EntityGetName(attacker))
			if fucker == "" then
				fucker = "an enemy"
			end

			-- "data/enemies_gfx/longleg.xml" -> "longleg"
			local enemy_name = string.match(ComponentGetValue2(EntityGetFirstComponent(attacker, "SpriteComponent"), "image_file"), "([^/]+)%.(.+)$")

			dofile_once("mods/lewd_noita/files/sex_logic/scene_table.lua")

			if scene_table[enemy_name] ~= nil then
				local sex_scene = EntityCreateNew(enemy_name)

				local enemy_x, enemy_y = EntityGetTransform(attacker)

				if enemy_x < kobold_x then
					EntitySetTransform(sex_scene, enemy_x, enemy_y, 0, 1, 1)
				else
					EntitySetTransform(sex_scene, enemy_x, enemy_y, 0, -1, 1)
				end

				if enemy_name == "tentacle_trap_left" or enemy_name == "tentacle_trap_right" then
					EntitySetTransform(sex_scene, enemy_x, enemy_y, 0, 1, 1)
				end

				EntityAddChild(sex_scene, GetUpdatedEntityID())

				EntityAddComponent2(sex_scene, "LuaComponent", {
					script_source_file="mods/lewd_noita/files/sex_logic/sex_scene.lua"
				})

				if IsPlayer(GetUpdatedEntityID()) == true then
					GamePrintImportant("oh no!", "You are caught by " .. fucker .. "!", "mods/lewd_noita/files/sex_logic/ui_msg.png")
					GamePlaySound("data/audio/Desktop/event_cues.bank", "event_cues/rune/create", kobold_x, kobold_y)
				end

				if enemy_name == "lurker" or enemy_name == "worm_tiny_head" or enemy_name == "tentacle_vase_blue" or enemy_name == "tentacle_vase_pink" then
					EntityKill(attacker)
				else
					EntityAddChild(sex_scene, attacker)
				end
			else
				if IsPlayer(GetUpdatedEntityID()) == true then
					GamePrint(fucker .. " doesn't want to fuck you. You are safe for now...")
				end
			end
		end
	end
end