173 lines
		
	
	
		
			No EOL
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			173 lines
		
	
	
		
			No EOL
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| local spawnedAnimals = {}
 | |
| local spawnedPersons = {}
 | |
| local spawnedProps = {}
 | |
| local isPedsSpawned = false
 | |
| local count = 0
 | |
| 
 | |
| ---Detete all things (Persons, Animals, Object). On resource stop.
 | |
| ---@param resourceName string
 | |
| ---@return string? count
 | |
| local function deleteAll(resourceName)
 | |
|     if GetCurrentResourceName() ~= resourceName then        
 | |
|         return
 | |
|     end
 | |
| 
 | |
|     for k, v in pairs(spawnedPersons) do
 | |
|         DeletePed(v)
 | |
|         count = count + 1
 | |
|     end
 | |
| 
 | |
|     for k, v in pairs(spawnedAnimals) do
 | |
|         DeletePed(v)
 | |
|         count = count + 1
 | |
|     end
 | |
| 
 | |
|     for k, v in pairs(spawnedProps) do
 | |
|         DeleteObject(v)
 | |
|         count = count + 1
 | |
|     end
 | |
| 
 | |
|     return print(('[Resource Stopped] Delete %s Persons, Animals, Props'):format(count))
 | |
| end
 | |
| 
 | |
| ---Load a model. Check if model is valid. If it is already loaded, it immediately returns the hash value.
 | |
| ---@param hash number
 | |
| ---@return number? hash
 | |
| local function requestModel(hash)
 | |
|     if not tonumber(hash) then
 | |
|         return print(('[RequestModel Function] That value: %s its not number/hash. Use backtics ``'):format(hash))
 | |
|     end
 | |
| 
 | |
|     if not IsModelValid(hash) then
 | |
|         return print(('[RequestModel Function] Attempted to load invalid model %s'):format(hash))
 | |
|     end
 | |
| 
 | |
|     if HasModelLoaded(hash) then
 | |
|         return hash
 | |
|     end
 | |
| 
 | |
|     RequestModel(hash)
 | |
|     while not HasModelLoaded(hash) do
 | |
|         Wait(50)
 | |
|     end
 | |
| 
 | |
|     return hash
 | |
| end
 | |
| 
 | |
| ---Load a dict. Check if animDict is valid. If it is already loaded, it immediately returns the animDict value.
 | |
| ---@param animDict string
 | |
| ---@return string? animDict
 | |
| local function requestAnimDict(animDict)
 | |
|     if type(animDict) ~= 'string' then
 | |
|         return print(('[RequestAnimDict Function] Expected animDict to have type string (received %s)'):format(type(animDict)))
 | |
|     end
 | |
| 
 | |
|     if not DoesAnimDictExist(animDict) then
 | |
|         return print(('[RequestAnimDict Function] Attempted to load invalid animDict %s'):format(animDict))
 | |
|     end
 | |
| 
 | |
|     if HasAnimDictLoaded(animDict) then 
 | |
|         return animDict 
 | |
|     end
 | |
| 
 | |
|     RequestAnimDict(animDict)
 | |
|     while not HasAnimDictLoaded(animDict) do
 | |
|         Wait(50)
 | |
|     end
 | |
| 
 | |
|     return animDict
 | |
| end
 | |
| 
 | |
| ---Spawn Persons, Animal and object. Save that things in 3 tables.
 | |
| local function spawnPeds()
 | |
|     if isPedsSpawned then
 | |
|         return
 | |
|     end
 | |
| 
 | |
|     isPedsSpawned = true
 | |
| 
 | |
|     for i = 1, #Config.Animals, 1 do
 | |
|         local Animal = Config.Animals[i]
 | |
| 
 | |
|         requestModel(Animal.animalHash)
 | |
| 
 | |
|         local createdAnimal = CreatePed('ANIMAL', Animal.animalHash, Animal.animalCoords.x, Animal.animalCoords.y, Animal.animalCoords.z, Animal.animalCoords.w, false, false)
 | |
| 
 | |
|         FreezeEntityPosition(createdAnimal, Animal.animalSettings.FreezeAnimal)
 | |
| 
 | |
|         SetEntityInvincible(createdAnimal, Animal.animalSettings.Invincible)
 | |
|         SetPedDiesWhenInjured(createdAnimal, not Animal.animalSettings.Invincible)
 | |
|         SetPedCanPlayAmbientAnims(createdAnimal, Animal.animalSettings.Invincible)
 | |
|         SetPedCanRagdollFromPlayerImpact(createdAnimal, not Animal.animalSettings.Invincible)
 | |
| 
 | |
|         SetBlockingOfNonTemporaryEvents(createdAnimal, Animal.animalSettings.BlockingOfNonTemporaryEvents)
 | |
| 
 | |
|         SetEntityAsMissionEntity(createdAnimal, true, true)
 | |
|         SetModelAsNoLongerNeeded(Animal.animalHash)
 | |
| 
 | |
|         if Animal.animalScenario then
 | |
|             TaskStartScenarioInPlace(createdAnimal, Animal.animalScenario, -1, true)
 | |
|         end
 | |
| 
 | |
|         spawnedAnimals[i] = createdAnimal
 | |
|     end
 | |
| 
 | |
|     for i = 1, #Config.Persons, 1 do
 | |
|         local Person = Config.Persons[i]
 | |
| 
 | |
|         requestModel(Person.pedHash)
 | |
| 
 | |
|         local createdPed = CreatePed('ANIMAL', Person.pedHash, Person.pedCoords.x, Person.pedCoords.y, Person.pedCoords.z, Person.pedCoords.w, false, false)
 | |
| 
 | |
|         FreezeEntityPosition(createdPed, Person.pedSettings.FreezePerson or false)
 | |
| 
 | |
|         if Person.pedSettings.Invincible then
 | |
|             SetEntityInvincible(createdPed, true)
 | |
|             SetPedDiesWhenInjured(createdPed, false)
 | |
|             SetPedCanPlayAmbientAnims(createdPed, true)
 | |
|             SetPedCanRagdollFromPlayerImpact(createdPed, false)
 | |
|         end
 | |
| 
 | |
|         SetBlockingOfNonTemporaryEvents(createdPed, Person.pedSettings.BlockingOfNonTemporaryEvents)
 | |
| 
 | |
|         SetEntityAsMissionEntity(createdPed, true, true)
 | |
|         SetModelAsNoLongerNeeded(Person.pedHash)
 | |
| 
 | |
|         if Person.pedAnimation.animDict and Person.pedAnimation.animName and Person.pedAnimation.animFlag then
 | |
|             requestAnimDict(Person.pedAnimation.animDict)
 | |
| 
 | |
|             TaskPlayAnim(createdPed, Person.pedAnimation.animDict, Person.pedAnimation.animName, 2.0, 2.0, -1, Person.pedAnimation.animFlag, 0, false, false, false)
 | |
|         end
 | |
| 
 | |
|         if Person.pedProp.propHash and Person.pedProp.propBone and Person.pedProp.propPlacement then
 | |
|             local pedCoords = GetEntityCoords(createdPed)
 | |
|             local x, y, z = table.unpack(pedCoords)
 | |
|             local xPos, yPos, zPos, xRot, yRot, zRot = table.unpack(Person.pedProp.propPlacement)
 | |
| 
 | |
|             requestModel(Person.pedProp.propHash)
 | |
| 
 | |
|             local createdProp = CreateObject(Person.pedProp.propHash, x, y, z+0.2,  true,  true, true)
 | |
| 
 | |
|             AttachEntityToEntity(createdProp, createdPed, GetPedBoneIndex(createdPed, Person.pedProp.propBone), xPos, yPos, zPos, xRot, yRot, zRot, true, true, false, true, 1, true)
 | |
|             SetModelAsNoLongerNeeded(Person.pedProp.propHash)
 | |
| 
 | |
|             spawnedProps[i] = createdProp
 | |
|         end
 | |
| 
 | |
|         spawnedPersons[i] = createdPed
 | |
|     end
 | |
| end
 | |
| 
 | |
| ---@param resourceName string
 | |
| ---@return function? spawnPeds
 | |
| local function spawnPedsOnStart(resourceName)
 | |
|     if GetCurrentResourceName() ~= resourceName then
 | |
|         return
 | |
|     end
 | |
| 
 | |
|     return spawnPeds()
 | |
| end
 | |
| 
 | |
| AddEventHandler('playerSpawned', spawnPeds)
 | |
| AddEventHandler('onResourceStart', spawnPedsOnStart)
 | |
| AddEventHandler('onResourceStop', deleteAll) | 
