local no_cooldown = true

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

local variable_comp = EntityGetFirstComponent(GetUpdatedEntityID(), "VariableStorageComponent")

-- birth scene starts after random amount of time
SetRandomSeed(GameGetFrameNum(), variable_comp)

if no_cooldown == true and GameGetFrameNum() - ComponentGetValue2(variable_comp, "value_string") > Random(30, 60) * 60 then
	local kobold_id = EntityGetParent(GetUpdatedEntityID())

	if ComponentGetValue2(EntityGetFirstComponent(kobold_id, "DamageModelComponent"), "is_on_fire") == false then
		local entity_to_create = ComponentGetValue2(variable_comp, "name")
		local birth_scene = EntityCreateNew("birth")

		EntityAddComponent2(birth_scene, "VariableStorageComponent", {
			name="birth_entity",
			value_string=entity_to_create,
		})

		local kobold_x, kobold_y = EntityGetTransform(kobold_id)

		if Random(0, 1) == 1 then
			EntitySetTransform(birth_scene, kobold_x, kobold_y, 0, 1, 1)
		else
			EntitySetTransform(birth_scene, kobold_x, kobold_y, 0, -1, 1)
		end

		EntityAddChild(birth_scene, kobold_id)

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

		EntityKill(GetUpdatedEntityID())

		if IsPlayer(kobold_id) == true then
			GamePrintImportant("you are about to give birth to something!", "", "mods/lewd_noita/files/sex_logic/ui_msg.png")
			GamePlaySound("data/audio/Desktop/event_cues.bank", "event_cues/heartbeat/create", kobold_x, kobold_y)
		end
	end
end