Refactor(AmbulanceJob): Optimisations and Fixes

This commit is contained in:
Mycroft
2022-03-02 15:57:46 +00:00
parent 98513594ea
commit 72eaf601e8
7 changed files with 76 additions and 116 deletions
+7 -29
View File
@@ -7,41 +7,19 @@ ESX Ambulance Job is an plugin for ESX with features:
## Requirements
* Auto mode
- [esx_skin](https://github.com/ESX-Org/esx_skin)
- [esx_vehicleshop](https://github.com/ESX-Org/esx_vehicleshop)
`Required`:
* Player management (boss actions)
- [esx_society](https://github.com/ESX-Org/esx_society)
- [esx_skin](https://github.com/esx-framework/esx-legacy/tree/main/%5Besx%5D/esx_skin)
- [esx_vehicleshop](https://github.com/esx-framework/esx-legacy/tree/main/%5Besx_addons%5D/esx_vehicleshop)
## Download & Installation
`Optional`:
### Using [fvm](https://github.com/qlaffont/fvm-installer)
```
fvm install --save --folder=esx esx-org/esx_ambulancejob
```
- [esx_society](https://github.com/esx-framework/esx-legacy/tree/main/%5Besx_addons%5D/esx_society)
### Using Git
```
cd resources
git clone https://github.com/ESX-Org/esx_ambulancejob [esx]/esx_ambulancejob
```
## Legal
### Manually
- Download https://github.com/ESX-Org/esx_ambulancejob/archive/master.zip
- Put it in the `[esx]` directory
## Installation
- Import `esx_ambulancejob.sql` in your database
- If you want player management you have to set `Config.EnablePlayerManagement` to `true` in `config.lua`
- Add this in your `server.cfg`:
```
start esx_ambulancejob
```
# Legal
### License
esx_ambulancejob - ambulance script for fivem
Copyright (C) 2015-2022 Jérémie N'gadi
+45 -51
View File
@@ -6,7 +6,7 @@ isInShopMenu = false
function OpenAmbulanceActionsMenu()
local elements = {{label = _U('cloakroom'), value = 'cloakroom'}}
if Config.EnablePlayerManagement and ESX.PlayerData.job.grade_name == 'boss' then
if Config.EnablePlayerManagement and ESX.GetPlayerData().job.grade_name == 'boss' then
table.insert(elements, {label = _U('boss_actions'), value = 'boss_actions'})
end
@@ -182,11 +182,11 @@ end
-- Draw markers & Marker logic
CreateThread(function()
while true do
Wait(0)
local sleep = 1500
if ESX.PlayerData.job and ESX.PlayerData.job.name == 'ambulance' then
if ESX.GetPlayerData().job and ESX.GetPlayerData().job.name == 'ambulance' then
local playerCoords = GetEntityCoords(PlayerPedId())
local letSleep, isInMarker, hasExited = true, false, false
local isInMarker, hasExited = false, false
local currentHospital, currentPart, currentPartNum
for hospitalNum,hospital in pairs(Config.Hospitals) do
@@ -195,8 +195,9 @@ CreateThread(function()
local distance = #(playerCoords - v)
if distance < Config.DrawDistance then
sleep = 0
DrawMarker(Config.Marker.type, v, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Config.Marker.x, Config.Marker.y, Config.Marker.z, Config.Marker.r, Config.Marker.g, Config.Marker.b, Config.Marker.a, false, false, 2, Config.Marker.rotate, nil, nil, false)
letSleep = false
if distance < Config.Marker.x then
isInMarker, currentHospital, currentPart, currentPartNum = true, hospitalNum, 'AmbulanceActions', k
@@ -209,8 +210,9 @@ CreateThread(function()
local distance = #(playerCoords - v)
if distance < Config.DrawDistance then
sleep = 0
DrawMarker(Config.Marker.type, v, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Config.Marker.x, Config.Marker.y, Config.Marker.z, Config.Marker.r, Config.Marker.g, Config.Marker.b, Config.Marker.a, false, false, 2, Config.Marker.rotate, nil, nil, false)
letSleep = false
if distance < Config.Marker.x then
isInMarker, currentHospital, currentPart, currentPartNum = true, hospitalNum, 'Pharmacy', k
@@ -223,8 +225,9 @@ CreateThread(function()
local distance = #(playerCoords - v.Spawner)
if distance < Config.DrawDistance then
sleep = 0
DrawMarker(v.Marker.type, v.Spawner, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Marker.x, v.Marker.y, v.Marker.z, v.Marker.r, v.Marker.g, v.Marker.b, v.Marker.a, false, false, 2, v.Marker.rotate, nil, nil, false)
letSleep = false
if distance < v.Marker.x then
isInMarker, currentHospital, currentPart, currentPartNum = true, hospitalNum, 'Vehicles', k
@@ -237,8 +240,9 @@ CreateThread(function()
local distance = #(playerCoords - v.Spawner)
if distance < Config.DrawDistance then
sleep = 0
DrawMarker(v.Marker.type, v.Spawner, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Marker.x, v.Marker.y, v.Marker.z, v.Marker.r, v.Marker.g, v.Marker.b, v.Marker.a, false, false, 2, v.Marker.rotate, nil, nil, false)
letSleep = false
if distance < v.Marker.x then
isInMarker, currentHospital, currentPart, currentPartNum = true, hospitalNum, 'Helicopters', k
@@ -251,8 +255,9 @@ CreateThread(function()
local distance = #(playerCoords - v.From)
if distance < Config.DrawDistance then
sleep = 0
DrawMarker(v.Marker.type, v.From, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Marker.x, v.Marker.y, v.Marker.z, v.Marker.r, v.Marker.g, v.Marker.b, v.Marker.a, false, false, 2, v.Marker.rotate, nil, nil, false)
letSleep = false
if distance < v.Marker.x then
isInMarker, currentHospital, currentPart, currentPartNum = true, hospitalNum, 'FastTravelsPrompt', k
@@ -280,41 +285,8 @@ CreateThread(function()
HasAlreadyEnteredMarker = false
TriggerEvent('esx_ambulancejob:hasExitedMarker', LastHospital, LastPart, LastPartNum)
end
if letSleep then
Wait(500)
end
else
Wait(500)
end
end
end)
-- Fast travels
CreateThread(function()
while true do
Wait(0)
local playerCoords, letSleep = GetEntityCoords(PlayerPedId()), true
for hospitalNum,hospital in pairs(Config.Hospitals) do
-- Fast Travels
for k,v in ipairs(hospital.FastTravels) do
local distance = #(playerCoords - v.From)
if distance < Config.DrawDistance then
DrawMarker(v.Marker.type, v.From, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Marker.x, v.Marker.y, v.Marker.z, v.Marker.r, v.Marker.g, v.Marker.b, v.Marker.a, false, false, 2, v.Marker.rotate, nil, nil, false)
letSleep = false
if distance < v.Marker.x then
FastTravel(v.To.coords, v.To.heading)
end
end
end
end
if letSleep then
Wait(500)
end
Wait(sleep)
end
end)
@@ -355,9 +327,10 @@ end)
-- Key Controls
CreateThread(function()
while true do
Wait(0)
local sleep = 1500
if CurrentAction then
sleep = 0
ESX.ShowHelpNotification(CurrentActionMsg)
if IsControlJustReleased(0, 38) then
@@ -375,17 +348,38 @@ CreateThread(function()
CurrentAction = nil
end
elseif ESX.PlayerData.job and ESX.PlayerData.job.name == 'ambulance' and not ESX.PlayerData.dead then
if IsControlJustReleased(0, 167) then
OpenMobileAmbulanceActionsMenu()
end
else
Wait(500)
end
local playerCoords, letSleep = GetEntityCoords(PlayerPedId()), true
for hospitalNum,hospital in pairs(Config.Hospitals) do
-- Fast Travels
for k,v in ipairs(hospital.FastTravels) do
local distance = #(playerCoords - v.From)
if distance < Config.DrawDistance then
sleep = 0
DrawMarker(v.Marker.type, v.From, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.Marker.x, v.Marker.y, v.Marker.z, v.Marker.r, v.Marker.g, v.Marker.b, v.Marker.a, false, false, 2, v.Marker.rotate, nil, nil, false)
if distance < v.Marker.x then
FastTravel(v.To.coords, v.To.heading)
end
end
end
end
Wait(sleep)
end
end)
RegisterCommand("ambulance", function(src)
if ESX.GetPlayerData().job and ESX.GetPlayerData().job.name == 'ambulance' and not ESX.GetPlayerData().dead then
OpenMobileAmbulanceActionsMenu()
end
end)
RegisterKeyMapping("ambulance", "Open Ambulance Actions Menu", "k")
RegisterNetEvent('esx_ambulancejob:putInVehicle')
AddEventHandler('esx_ambulancejob:putInVehicle', function()
local playerPed = PlayerPedId()
+16 -30
View File
@@ -4,22 +4,15 @@ isDead, isSearched, medic = false, false, 0
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
ESX.PlayerData = xPlayer
ESX.PlayerLoaded = true
end)
RegisterNetEvent('esx:onPlayerLogout')
AddEventHandler('esx:onPlayerLogout', function()
ESX.PlayerLoaded = false
ESX.PlayerData = {}
firstSpawn = true
end)
RegisterNetEvent('esx:setJob')
AddEventHandler('esx:setJob', function(job)
ESX.PlayerData.job = job
end)
AddEventHandler('esx:onPlayerSpawn', function()
isDead = false
@@ -55,37 +48,30 @@ CreateThread(function()
AddTextComponentSubstringPlayerName(_U('blip_hospital'))
EndTextCommandSetBlipName(blip)
end
end)
-- Disable most inputs when dead
CreateThread(function()
while true do
Wait(0)
while true do
local Sleep = 1500
if isDead then
Sleep = 0
DisableAllControlActions(0)
EnableControlAction(0, 47, true)
EnableControlAction(0, 245, true)
EnableControlAction(0, 38, true)
else
Wait(500)
end
end
end)
CreateThread(function()
while true do
Wait(0)
if isDead and isSearched then
local playerPed = PlayerPedId()
local ped = GetPlayerPed(GetPlayerFromServerId(medic))
isSearched = false
AttachEntityToEntity(playerPed, ped, 11816, 0.54, 0.54, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
Wait(1000)
DetachEntity(playerPed, true, false)
ClearPedTasksImmediately(playerPed)
if isSearched then
local playerPed = PlayerPedId()
local ped = GetPlayerPed(GetPlayerFromServerId(medic))
isSearched = false
AttachEntityToEntity(playerPed, ped, 11816, 0.54, 0.54, 0.0, 0.0, 0.0, 0.0, false, false, false, false, 2, true)
Wait(1000)
DetachEntity(playerPed, true, false)
ClearPedTasksImmediately(playerPed)
end
end
Wait(Sleep)
end
end)
@@ -280,14 +280,14 @@ end
CreateThread(function()
while true do
Wait(0)
sleep = 1500
if isInShopMenu then
sleep = 0
DisableControlAction(0, 75, true) -- Disable exit vehicle
DisableControlAction(27, 75, true) -- Disable exit vehicle
else
Wait(500)
end
Wait(sleep)
end
end)
+1 -1
View File
@@ -1,6 +1,6 @@
Config = {}
Config.DrawDistance = 20.0 -- How close do you need to be in order for the markers to be drawn (in GTA units).
Config.DrawDistance = 10.0 -- How close do you need to be in order for the markers to be drawn (in GTA units).
Config.Marker = {type = 1, x = 1.5, y = 1.5, z = 0.5, r = 102, g = 0, b = 102, a = 100, rotate = false}
@@ -1,5 +1,3 @@
USE `es_extended`;
INSERT INTO `addon_account` (name, label, shared) VALUES
('society_ambulance', 'Ambulance', 1)
;
@@ -1,8 +1,12 @@
local playersHealing, deadPlayers = {}, {}
if GetResourceState("esx_phone") ~= 'missing' then
TriggerEvent('esx_phone:registerNumber', 'ambulance', _U('alert_ambulance'), true, true)
end
if GetResourceState("esx_society") ~= 'missing' then
TriggerEvent('esx_society:registerSociety', 'ambulance', 'Ambulance', 'society_ambulance', 'society_ambulance', 'society_ambulance', {type = 'public'})
end
RegisterNetEvent('esx_ambulancejob:revive')
AddEventHandler('esx_ambulancejob:revive', function(playerId)