<commit_before>-- This module handles an encounter in the quest system.
-- An encounter is an event where the player should encounter something after a while.
-- The module takes care of scheduling the event, tringgering the event, clean up etc.
-- The user supplies the module with information about what the encounter is about.
--
-- Parameters
-- taskName - need to be the name of the class inheriting the Encounter class to get the observers and events to work.
-- minimumTimeUntilEncounter - the minimum time until the encounter should happen in ms.
-- maximumTimeUntilEncounter - the maximum time until the encounter should happen in ms.
-- encounterDespawnTime - time in ms from the moment the encounter is spawned until it should be despawned.
-- customSpawnBehaviour - function that is called when the encounter has been spawned with the pCreatureObject
--                        and a list of pointers to the creature objects of the spawned objects
-- spawnObjectList - a list with one element per object that should be spawned in the encounter.
--					 It uses the format from the spawn mobile unit in utils.
--                   Each element should define a table with the following parameters:
--                    - template - the template of the mobile to spawn.
--				      - minimumDistance - the minimum distance for the mobile to spawn at from the center reference point.
--				      - maximumDistance - the maximum distance for the mobile to spawn at from the center reference point.
--				      - referencePoint - index of the spawned mobile to use as center reference point for this mobile.
--									     0 = use provided coordinates, 1 = use the spawn position of the first mobile in the mobileList, etc.
--									     This value must be lower than the current list item number.
--                    - followPlayer - boolean value indicating if the spawned object should be set to follow the player or not.

local Task = require("quest.tasks.task")
local PersistentEvent = require("quest.tasks.persistent_event")
local ObjectManager = require("managers.object.object_manager")
local SpawnMobiles = require("utils.spawn_mobiles")
local Logger = require("utils.logger")

Encounter = Task:new {
	-- Task properties
	taskName = "",
	-- Encounter properties
	minimumTimeUntilEncounter = 0,
	maximumTimeUntilEncounter = 0,
	encounterDespawnTime = 0,
	despawnMessage = "",
	spawnObjectList = {},
	onEncounterSpawned = nil,
	isEncounterFinished = nil,
	onEncounterInRange = nil,
	inRangeValue = 16,
	spawnTask = nil,
	despawnTask = nil
}

-- Setup persistent events
function Encounter:setupSpawnAndDespawnEvents()
	if self.spawnTask == nil then
		self.spawnTask = PersistentEvent:new {
			-- Task properties
			taskName = self.taskName .. "_spawn_task",
			taskFinish = nil,
			-- PersistentEvent properties
			minimumTimeUntilEvent = self.minimumTimeUntilEncounter,
			maximumTimeUntilEvent = self.maximumTimeUntilEncounter,
			handlerName = self.taskName,
			handlerFunction = "handleSpawnEvent",
			-- Spawn task properties
			encounter = self
		}
	end

	if self.despawnTask == nil then
		self.despawnTask = PersistentEvent:new {
			-- Task properties
			taskName = self.taskName .. "_despawn_task",
			taskFinish = nil,
			-- PersistentEvent properties
			minimumTimeUntilEvent = self.encounterDespawnTime,
			maximumTimeUntilEvent = self.encounterDespawnTime,
			handlerName = self.taskName,
			handlerFunction = "handleDespawnEvent",
			-- Spawn task properties
			encounter = self
		}
	end
end

-- Start the encounter.
-- @param pCreatureObject pointer to the creature object of the player.
function Encounter:taskStart(pCreatureObject)
	Logger:log("Starting spawn task in " .. self.taskName, LT_INFO)
	self:setupSpawnAndDespawnEvents()
	self.spawnTask:start(pCreatureObject)
	return true
end

-- Check if the player is online.
-- @param pCreatureObject pointer to the creature object of the player who should be checked if online.
-- @return true if the player is online.
function Encounter:isPlayerOnline(pCreatureObject)
	return ObjectManager.withCreaturePlayerObject(pCreatureObject, function(playerObject)
		return playerObject:isOnline()
	end) == true
end

-- Check if the player is in a building or not.
-- @param pCreatureObject pointer to the creature object of the player that should be checked for being in a building or not.
-- @return true if the player is in a building.
function Encounter:isPlayerInABuilding(pCreatureObject)
	return not SceneObject(pCreatureObject):getParentID() == 0
end

-- Check if the player is in a NPC city.
-- @param pCreatureObject pointer to the creature object of the player who should be checked for being in a NPC city.
-- return true if the player is within a NPC city.
function Encounter:isPlayerInNpcCity(pCreatureObject)
	if (pCreatureObject == nil or SceneObject(pCreatureObject):getZoneName() == "") then
		return false
	end
<commit_msg>[fixed] Encounters spawning while the player is in a building - mantis 6709<commit_after>-- This module handles an encounter in the quest system.
-- An encounter is an event where the player should encounter something after a while.
-- The module takes care of scheduling the event, tringgering the event, clean up etc.
-- The user supplies the module with information about what the encounter is about.
--
-- Parameters
-- taskName - need to be the name of the class inheriting the Encounter class to get the observers and events to work.
-- minimumTimeUntilEncounter - the minimum time until the encounter should happen in ms.
-- maximumTimeUntilEncounter - the maximum time until the encounter should happen in ms.
-- encounterDespawnTime - time in ms from the moment the encounter is spawned until it should be despawned.
-- customSpawnBehaviour - function that is called when the encounter has been spawned with the pCreatureObject
--                        and a list of pointers to the creature objects of the spawned objects
-- spawnObjectList - a list with one element per object that should be spawned in the encounter.
--					 It uses the format from the spawn mobile unit in utils.
--                   Each element should define a table with the following parameters:
--                    - template - the template of the mobile to spawn.
--				      - minimumDistance - the minimum distance for the mobile to spawn at from the center reference point.
--				      - maximumDistance - the maximum distance for the mobile to spawn at from the center reference point.
--				      - referencePoint - index of the spawned mobile to use as center reference point for this mobile.
--									     0 = use provided coordinates, 1 = use the spawn position of the first mobile in the mobileList, etc.
--									     This value must be lower than the current list item number.
--                    - followPlayer - boolean value indicating if the spawned object should be set to follow the player or not.

local Task = require("quest.tasks.task")
local PersistentEvent = require("quest.tasks.persistent_event")
local ObjectManager = require("managers.object.object_manager")
local SpawnMobiles = require("utils.spawn_mobiles")
local Logger = require("utils.logger")

Encounter = Task:new {
	-- Task properties
	taskName = "",
	-- Encounter properties
	minimumTimeUntilEncounter = 0,
	maximumTimeUntilEncounter = 0,
	encounterDespawnTime = 0,
	despawnMessage = "",
	spawnObjectList = {},
	onEncounterSpawned = nil,
	isEncounterFinished = nil,
	onEncounterInRange = nil,
	inRangeValue = 16,
	spawnTask = nil,
	despawnTask = nil
}

-- Setup persistent events
function Encounter:setupSpawnAndDespawnEvents()
	if self.spawnTask == nil then
		self.spawnTask = PersistentEvent:new {
			-- Task properties
			taskName = self.taskName .. "_spawn_task",
			taskFinish = nil,
			-- PersistentEvent properties
			minimumTimeUntilEvent = self.minimumTimeUntilEncounter,
			maximumTimeUntilEvent = self.maximumTimeUntilEncounter,
			handlerName = self.taskName,
			handlerFunction = "handleSpawnEvent",
			-- Spawn task properties
			encounter = self
		}
	end

	if self.despawnTask == nil then
		self.despawnTask = PersistentEvent:new {
			-- Task properties
			taskName = self.taskName .. "_despawn_task",
			taskFinish = nil,
			-- PersistentEvent properties
			minimumTimeUntilEvent = self.encounterDespawnTime,
			maximumTimeUntilEvent = self.encounterDespawnTime,
			handlerName = self.taskName,
			handlerFunction = "handleDespawnEvent",
			-- Spawn task properties
			encounter = self
		}
	end
end

-- Start the encounter.
-- @param pCreatureObject pointer to the creature object of the player.
function Encounter:taskStart(pCreatureObject)
	Logger:log("Starting spawn task in " .. self.taskName, LT_INFO)
	self:setupSpawnAndDespawnEvents()
	self.spawnTask:start(pCreatureObject)
	return true
end

-- Check if the player is online.
-- @param pCreatureObject pointer to the creature object of the player who should be checked if online.
-- @return true if the player is online.
function Encounter:isPlayerOnline(pCreatureObject)
	return ObjectManager.withCreaturePlayerObject(pCreatureObject, function(playerObject)
		return playerObject:isOnline()
	end) == true
end

-- Check if the player is in a building or not.
-- @param pCreatureObject pointer to the creature object of the player that should be checked for being in a building or not.
-- @return true if the player is in a building.
function Encounter:isPlayerInABuilding(pCreatureObject)
	return SceneObject(pCreatureObject):getParentID() ~= 0
end

-- Check if the player is in a NPC city.
-- @param pCreatureObject pointer to the creature object of the player who should be checked for being in a NPC city.
-- return true if the player is within a NPC city.
function Encounter:isPlayerInNpcCity(pCreatureObject)
	if (pCreatureObject == nil or SceneObject(pCreatureObject):getZoneName() == "") then
		return false
	end

{'fraction_non_alphanumeric': 0.06784630940343782, 'fraction_numerical': 0.0020222446916076846, 'mean_word_length': 3.6633663366336635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4619384', 'n_tokens_mistral': 2805, 'n_tokens_neox': 2622, 'n_words': 1090}