Jump to content

How to differentiate a vehicle created by player from a vehicle created by map?


Lord Henry

Recommended Posts

  • Other Languages Moderators

Hello everyone.

I need to destroy a vehicle that is idle for 60 seconds (Freeroam already has this function, so this part is unnecessary to make.)

It starts counting when the vehicle becomes totally empty, (with no driver and no passengers), if someone enters in it before 60 seconds (as passenger or driver), the counting stops and restarts when the vehicle becomes empty again.) (Freeroam already works like this.)

BUT

If the car is from a .map, it need to be respawned to its original position on the map.

Else if the car was created by a player (F1, command, Admin Panel, scripts, etc) so it will be destroyed.

How can I differentiate the vehicles created by the .map from those created by the players?
 

Also, when the vehicle gets exploded:

if it is from a .map it will respawn after 10 seconds, else if was created by a player it will be destroyed after 10 seconds.

I have a little script that respawn all vehicles that have been exploded. But it respawns all the vehicles created by players too...
 

-- 1 second = 1000
function respawnExplodedVehicle()
	setTimer(respawnVehicle, 10000, 1, source)
end
addEventHandler("onVehicleExplode", getRootElement(), respawnExplodedVehicle)



 

Link to comment

I have just tested, forget what i said :(

  Reveal hidden contents

That was a work for onElementCreated if it had a parameter for the player that caused the element to be created... but it does not exist

Edited by LoPollo
Link to comment
  • Other Languages Moderators
  On 14/11/2016 at 22:26, Chainsaw said:

I imagine you can do this by setting variables when creating a vehicle, another way I can think of is to assign a tag following the ID in a specific way to the map vehicles and then get the string from getElementID

Example: 


id="Uranus 5"  

to


id="server_Uranus 5" 

and check if the vehicle belongs to map with  string.find 

Expand  

Let's use an example.
I have a map with a Patriot.

How will I differentiate that Patriot of the .map from a Patriot created by a player?

Edited by lordhenry
Link to comment

In your .map vehicles you have something like:

<vehicle id="vehicle (Buffalo) (3)" paintjob="3" interior="0" alpha="160" model="405" plate="INT6 8C7" dimension="0" posX="1802.0996" posY="-1932.990" posZ="13.2" rotX="0" rotY="0" rotZ="0" color="45,58,53,159,157,148,0,0,0,0,0,0"></vehicle>

Change to

<vehicle id="server_vehicle (Buffalo) (3)" paintjob="3" interior="0" alpha="160" model="405" plate="INT6 8C7" dimension="0" posX="1802.0996" posY="-1932.990" posZ="13.2" rotX="0" rotY="0" rotZ="0" color="45,58,53,159,157,148,0,0,0,0,0,0"></vehicle>

and in the script 

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle")
		local tag = '';
		if(tag = getElementID(element))
			if(string.find(tag, "server_"))
				return 1;
			end
		end
	end
end

 

if that function return 1 the vehicle from map

This is a form that I can think of, I do not know if there is an official way

 

 

Link to comment
  • Other Languages Moderators
  On 14/11/2016 at 22:34, Chainsaw said:

In your .map vehicles you have something like:


<vehicle id="vehicle (Buffalo) (3)" paintjob="3" interior="0" alpha="160" model="405" plate="INT6 8C7" dimension="0" posX="1802.0996" posY="-1932.990" posZ="13.2" rotX="0" rotY="0" rotZ="0" color="45,58,53,159,157,148,0,0,0,0,0,0"></vehicle>

Change to


<vehicle id="server_vehicle (Buffalo) (3)" paintjob="3" interior="0" alpha="160" model="405" plate="INT6 8C7" dimension="0" posX="1802.0996" posY="-1932.990" posZ="13.2" rotX="0" rotY="0" rotZ="0" color="45,58,53,159,157,148,0,0,0,0,0,0"></vehicle>

and in the script 


function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle")
		local tag = '';
		if(tag = getElementID(element))
			if(string.find(tag, "server_"))
				return 1;
			end
		end
	end
end

 

if that function return 1 the vehicle from map

This is a form that I can think of, I do not know if there is an official way

 

 

Expand  

The official way is all the ways that works, xD

Link to comment

addCommandHandler("test", function(player, command)
	local v = getPedOccupiedVehicle(player);

	if (v) then
		outputChatBox("This vehicle is from " .. (vehicleIsStatic(v)) and "map" or "player", player)
	end
end)

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle") then

		local tag = getElementID(element);

		if(tag and (string.find(tag, "server_"))) then	
			return 1;
		end
	end
end

 

Link to comment
  • Other Languages Moderators
  On 14/11/2016 at 22:44, Chainsaw said:

addCommandHandler("test", function(player, command)
	local v = getPedOccupiedVehicle(player);

	if (v) then
		outputChatBox("This vehicle is from " .. (vehicleIsStatic(v)) and "map" or "player", player)
	end
end)

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle") then

		local tag = getElementID(element);

		if(tag and (string.find(tag, "server_"))) then	
			return 1;
		end
	end
end

 

Expand  

This is a server sided script, right?

Link to comment

well, i did a test

920c4274a1d542fc901806aa62e8fa82.png

fc914cae797b476b970dad2f4435b3bd.png

code:

addCommandHandler("v", function(player, command)
	local v = getPedOccupiedVehicle(player);

	if (v) then
		local n = (not vehicleIsStatic(v)) and ("player") or ("map");

		outputChatBox("This vehicle is from " .. n)
	else 

		outputChatBox("you are't in a vehicle")
	end
end)

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle") then

		local tag = getElementID(element);

		if(tag and (string.find(tag, "server_"))) then	
			return 1;
		end
	end
end

 

And yes, server-side!

do not forget which this works with tag

940d0a67376648a58a9c87130b4b9210.png

Edited by Chainsaw
Link to comment
  • Other Languages Moderators
  On 14/11/2016 at 23:04, Chainsaw said:

well, i did a test

920c4274a1d542fc901806aa62e8fa82.png

fc914cae797b476b970dad2f4435b3bd.png

code:


addCommandHandler("v", function(player, command)
	local v = getPedOccupiedVehicle(player);

	if (v) then
		local n = (not vehicleIsStatic(v)) and ("player") or ("map");

		outputChatBox("This vehicle is from " .. n)
	else 

		outputChatBox("you are't in a vehicle")
	end
end)

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle") then

		local tag = getElementID(element);

		if(tag and (string.find(tag, "server_"))) then	
			return 1;
		end
	end
end

 

And yes, server-side!

Expand  

Cool!
Now try explode both of them but respawn only the map vehicle.
The player vehicle should disappear. (destroyed)

Edited by lordhenry
Link to comment
  • Other Languages Moderators

Done :D

function checkVehicle (thePlayer)
	local v = getPedOccupiedVehicle(thePlayer);

	if (v) then
		local n = (not vehicleIsStatic(v)) and ("player") or ("map");

		outputChatBox("This vehicle is from " .. n)
	else 

		outputChatBox("you are't in a vehicle")
	end
end
addEventHandler("onVehicleEnter", getRootElement(), checkVehicle)

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle") then

		local tag = getElementID(element);

		if(tag and (string.find(tag, "server_"))) then	
			return 1;
		end
	end
end

function respawnExplodedVehicle ()
 	if(not vehicleIsStatic(source)) then -- Not is vehicle from map
 		outputChatBox("The vehicle " .. getVehicleName(source) .. " was destroyed");
 		destroyElement(source)
	else
		setTimer(respawnVehicle, 10000, 1, source)
		outputChatBox("The vehicle " .. getVehicleName(source) .. " will respawn");
 	end
end
addEventHandler("onVehicleExplode", getRootElement(), respawnExplodedVehicle)

Test this.

Link to comment
  • Other Languages Moderators

There's still one problem.
The timer should stop when someone enters the vehicle (as driver or passenger) and restart when the vehicle goes empty (with no driver and no passenger).

Link to comment

store the timer in a table (or a variable but i doubt that you will use only 1 vehicle at time) and kill it when entering the "non static veh" and start it after exiting

PS: i don't understand all the login in the code... is it only a piece?

Could something like this work (the function isvehstatic is edited a bit)? check it all it's not tested

local timers = {}

function onVehicleEnterHandler(thePlayer, seat, jacked)
	local theVehicle = source
	if isTimer( timers[theVehicle] ) then 
		killTimer( timers[theVehicle] )
	end
end
addEventHandler( "onVehicleEnter", root, onVehicleEnterHandler)

function onVehicleExitHandler(thePlayer, seat, jacked)
	local theVehicle = source
	timers[theVehicle] = setTimer( isVehicleStatic(theVehicle) and respawnVehicle or destroyElement, 60000, 1, theVehicle )
end
addEventHandler( "onVehicleExit", root, onVehicleExitHandler)


function isVehicleStatic(theVehicle)
	if isElement( theVehicle ) and getElementType( theVehicle ) == "vehicle" then
		if getElementID(theVehicle):find("server_") then
			return true
		end
	end
	return false
end

 

Edited by LoPollo
i'm retarded tonight
Link to comment
  • Other Languages Moderators

Here is the full code.
 

function checkVehicle (thePlayer)
local v = getPedOccupiedVehicle(thePlayer);
	if (v) then
		local n = (not vehicleIsStatic(v)) and ("player") or ("map");
		--outputChatBox("This vehicle is from " .. n) --Tell the player if the vehicle was created from a map (server) or was created by a player.
	--else 
		--outputChatBox("You aren't in a vehicle!")
	end
end
addEventHandler("onVehicleEnter", getRootElement(), checkVehicle)

function vehicleIsStatic(element)
	if(getElementType(element) == "vehicle") then
		local tag = getElementID(element);
		if(tag and (string.find(tag, "server_"))) then	
			return 1;
		end
	end
end

function respawnExplodedVehicle ()
 	if(not vehicleIsStatic(source)) then -- Is not a vehicle from map
 		--outputChatBox("The vehicle " .. getVehicleName(source) .. " will be destroyed");--Tell the player that the vehicle was destroyed and will not respawn again.
 		setTimer(destroyElement, 10000, 1, source)
		--destroyElement(source)
	else
		setTimer(respawnVehicle, 10000, 1, source)
		--outputChatBox("The vehicle " .. getVehicleName(source) .. " will respawn");--Tell the player that the vehicle will respawn again where the map created it.
 	end
end
addEventHandler("onVehicleExplode", getRootElement(), respawnExplodedVehicle)

function respawnIdleVehicle ()
 	if(not vehicleIsStatic(source)) then -- Is not a vehicle from map
 		--outputChatBox("The idle vehicle " .. getVehicleName(source) .. " will be destroyed");
 		setTimer(destroyElement, 30000, 1, source)--This will always return a Warning to Console when the player exit a vehicle and explode it. Because the script will try to destroy the idle vehicle after the time but it doesn't exist anymore.
	else
		setTimer(respawnVehicle, 30000, 1, source)
		--outputChatBox("The idle vehicle " .. getVehicleName(source) .. " will respawn");
 	end
end
addEventHandler("onVehicleExit", getRootElement(), respawnIdleVehicle)

 

Link to comment
  • Other Languages Moderators

And this is the final version, I added a function to stop the timer when the vehicle is exploded and a timer of 10 seconds to respawn it or destroy it.

--[[You need to change all .map files with vehicles to make this script respawn them instead of destroy them.
Example:
<vehicle id="vehicle (Patriot) (2)" paintjob="3" interior="0"[...........]></vehicle>

Change only the 'vehicle id' to:
<vehicle id="server_vehicle (Patriot) (2)" paintjob="3" interior="0"[...........]></vehicle>
You need to do it with all vehicles in all your .map files. Open the .map files with notepad++

60000 = 60 seconds.
--]]
local timers = {}

function onVehicleEnterHandler(thePlayer, seat, jacked)--This stops the timer when a player enters the vehicle or the vehicle is exploded.
	local theVehicle = source
	if isTimer( timers[theVehicle] ) then 
		killTimer( timers[theVehicle] )
	end
end
addEventHandler( "onVehicleEnter", root, onVehicleEnterHandler)
addEventHandler( "onVehicleExplode", root, onVehicleEnterHandler)

function onVehicleExitHandler(thePlayer, seat, jacked)--This starts a 60s timer when the player exit the vehicle. 
	local theVehicle = source
	timers[theVehicle] = setTimer( isVehicleStatic(theVehicle) and respawnVehicle or destroyElement, 60000, 1, theVehicle )--This time must be lower than Freeroam setting 'MaxIdleTime' or it will return a warning to Console because it will try to destroy a vehicle that was already destroyed by Freeroam. I recommend that you set the Freeroam 'MaxIdleTime' to 70000.
end
addEventHandler( "onVehicleExit", root, onVehicleExitHandler)

function onVehicleExplodeHandler(thePlayer)--This starts a 10s timer when the vehicle is exploded. This happens after the original timer is stoped.
	local theVehicle = source
	timers[theVehicle] = setTimer( isVehicleStatic(theVehicle) and respawnVehicle or destroyElement, 10000, 1, theVehicle )--This time must be lower than Freeroam setting 'MaxIdleTime'.
end
addEventHandler( "onVehicleExplode", root, onVehicleExplodeHandler)

function isVehicleStatic(theVehicle)--This checks if the vehicle is from a map server or from a player.
	if isElement( theVehicle ) and getElementType( theVehicle ) == "vehicle" then
		if getElementID(theVehicle):find("server_") then
			return true
		end
	end
	return false
end

The unique problem happens if the player is killed while he's inside a vehicle. The vehicle doesn't respawn because the player didn't exited it. The freeroam also doesn't destroy it.

Edited by lordhenry
Link to comment
  On 16/11/2016 at 14:30, lordhenry said:

The unique problem happens if the player is killed while he's inside a vehicle.

Expand  

There's a workaround

i have written it in a hurry so test it before implementing it in a script

local pedsOccupiedVehicles = {}

function onVehEnterHandler(thePlayer,seat,jacked)
	local theVehicle = source
	pedsOccupiedVehicles[thePlayer] = theVehicle
end
addEventHandler( "onVehicleEnter", root, onVehEnterHandler)

function onVehExitHandler(thePlayer,seat,jacked)
	local theVehicle = source
	pedsOccupiedVehicles[thePlayer] = false
end
addEventHandler( "onVehicleExit", root, onVehExitHandler )

function onPlayerDeathHandler(totalAmmo, killer, killerWeapon, bodypart, stealth)
	local thePlayer = source
	pedsOccupiedVehicles[thePlayer] = false
end
addEventHandler( "onPlayerWasted", root, onPlayerDeathHandler)

function checkOnStart(theResource)
	for _,thePlayer in pairs(getElementsByType( "player")) do
		pedsOccupiedVehicles[thePlayer] = getPedOccupiedVehicle( thePlayer )
	end
end
addEventHandler( "onResourceStart", resourceRoot, checkOnStart )

I tried to be as clear as possible, let me know if it works

 

PS: maybe it's a good idea to make a resource with an exported function? setting the handler of death to low, and declaring an exported function that returns the veh for that player may be helpful, you wouldn't have to implement it in every script

Edited by LoPollo
Link to comment

Here's the resource

  Reveal hidden contents

It's the hex of the zip file xD 

Link to comment
  • Other Languages Moderators
  On 16/11/2016 at 18:57, LoPollo said:

There's a workaround

i have written it in a hurry so test it before implementing it in a script


local pedsOccupiedVehicles = {}

function onVehEnterHandler(thePlayer,seat,jacked)
	local theVehicle = source
	pedsOccupiedVehicles[thePlayer] = theVehicle
end
addEventHandler( "onVehicleEnter", root, onVehEnterHandler)

function onVehExitHandler(thePlayer,seat,jacked)
	local theVehicle = source
	pedsOccupiedVehicles[thePlayer] = false
end
addEventHandler( "onVehicleExit", root, onVehExitHandler )

function onPlayerDeathHandler(totalAmmo, killer, killerWeapon, bodypart, stealth)
	local thePlayer = source
	pedsOccupiedVehicles[thePlayer] = false
end
addEventHandler( "onPlayerWasted", root, onPlayerDeathHandler)

function checkOnStart(theResource)
	for _,thePlayer in pairs(getElementsByType( "player")) do
		pedsOccupiedVehicles[thePlayer] = getPedOccupiedVehicle( thePlayer )
	end
end
addEventHandler( "onResourceStart", resourceRoot, checkOnStart )

I tried to be as clear as possible, let me know if it works

Expand  

Nothing happens to vehicle.

Link to comment

As it should be: the goal of the quoted script is having a table used to workaround the issue you have, and the resource is this script with an exported function that read that table and tell you if the player was in a vehicle when he dead/was killed, returning the vehicle he was in if or false. Keep in mind the resource i posted as hex works by having an handler with high priority, so it's not reliable when used together with resources that have handlers of onPlayerWasted on "high" priority or higher, or similar situations*

 

* Read the spoiler

  Reveal hidden contents

 

Edited by LoPollo
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...