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 :(

Spoiler

default veh (play):

Mr.K executed command: getElementParent(getPedOccupiedVehicle(me))
Command results: userdata: 073142E8 [element:map]

script of mine:
Mr.K executed command: getElementParent(getPedOccupiedVehicle(me))
Command results: userdata: 054A33D8 [element:map]

default script (freeroam):
Mr.K executed command: getElementParent(getPedOccupiedVehicle(me))
Command results: userdata: 054A4760 [element:map]

 

The parents of these maps are the resourced that created the veh (in order: play, [my script], freeroam)

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
4 minutes ago, 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 

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
Just now, 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

 

 

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
2 minutes ago, 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

 

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
3 minutes ago, 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!

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
4 hours ago, lordhenry said:

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

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

Spoiler

50 4B 03 04 14 00 00 00 08 00 F8 A1 70 49 47 03 AE 5A 0C 01 00 00 CF 01 00 00 08 00 00 00 6D 65 74 61 2E 78 6D 6C 75 51 41 4E C3 30 10 3C 53 A9 7F 18 F9 52 90 AA 14 EE 0D 07 AE 08 D1 43 C5 7D 71 B6 D8 34 B5 23 EF BA 6D 7E 8F 93 34 27 E0 66 8D 67 66 67 67 B7 27 56 7A 5E 2E EE B6 3E 1C 22 28 AB 8B A9 36 6F A9 7A 35 38 73 12 1F 43 6D 9E AA 47 83 40 27 AE CD AE A5 BE C0 F8 60 E7 6D CB 82 3D 7D B6 6C D0 B0 D8 E4 3B 1D F9 7B E7 05 89 25 E6 64 19 8E 42 33 30 39 28 27 1F BE D6 E0 AB D7 F2 C0 79 36 29 0C 74 A3 73 31 22 75 E0 73 61 CB 7A FC E0 6B 17 93 0A AC 63 7B DC 71 F3 6E 6D EE 3C 37 B7 08 F7 EA 78 4A F5 00 8D F8 62 45 41 66 6F 38 5E 09 7C A8 F0 42 E2 2D B5 6D 0F AF 05 FA CE A2 20 5C 62 3A 52 8A 39 34 83 78 10 1E C8 0E 0E A4 88 A1 A4 A7 39 98 97 21 5B 83 3E 66 58 0A 2B FD 63 14 2E 34 0E 33 D8 8C A5 4E 9D 40 92 AD 8D 70 2A 85 56 6D 26 03 ED 3B 9E 11 33 51 A7 2D 71 C8 C1 4E 25 FE B3 EE 2F 31 96 8B ED E6 76 C6 1F 50 4B 03 04 14 00 00 00 08 00 14 A2 70 49 49 9E F7 3C 8A 01 00 00 73 04 00 00 0A 00 00 00 73 65 72 76 65 72 2E 6C 75 61 95 93 DF 4E 83 30 14 C6 AF 25 E1 1D 4E B8 DA 92 2E 3E 81 17 1A 17 BD DB 32 8D BB 30 C6 D4 F6 30 EA BA 96 B4 65 93 18 DF DD 52 18 1B 6C D9 9F 1B 68 E1 F4 7C DF EF C0 27 35 A3 12 72 E4 76 C2 58 91 0B E4 6F 98 09 26 D1 C2 1D FC FE C5 51 1C A5 85 62 4E 68 05 2C 43 B6 9C A8 17 47 8D 1B B8 0C 67 68 75 61 18 0E E3 E8 26 D5 06 3E 89 7F 38 95 B4 44 03 42 41 4E 85 B1 83 05 BA B1 C4 15 2A 67 1F CA D7 32 C7 01 24 79 A8 49 86 43 E0 DA 9F BD 39 26 FF DE F6 FA F0 4E 7C 97 29 F2 5E CD 00 76 7A 95 07 54 3C 8E C2 85 72 3E 5E 7B C9 67 AA B8 44 E3 25 B5 DA BA 0D F6 13 02 A6 D9 CF B4 76 A4 C3 46 C0 99 02 09 24 99 58 64 C9 B0 33 03 AD BC F8 58 39 34 DB DE AD 07 62 91 3A F2 4D D9 12 79 65 A7 1E AD 7F DD D8 F5 18 B5 62 1C 5D 40 BC 3B 77 82 A9 A9 08 7E 2A A4 80 72 60 F1 3C CF 8F 70 57 E2 5C 8B 93 52 69 2F 21 F1 4E 7A 20 3B 6F 2D 87 D4 9B 04 FA 1C B5 D6 A3 37 9D B5 2C DA 51 79 BF 5A 69 02 4B 21 43 87 FA 3E 47 9A 6B 45 E0 4B F3 32 0F 5F DC 3A A4 D2 65 1D D0 E6 D7 BA 9A F3 14 67 5D 39 A7 5E 8F EF 81 1E 9A EF C0 06 D6 38 1A 8D 9E D0 55 CE 60 5D 8B 87 75 DE 24 CE DE 6E A8 F5 C1 EB 27 F6 48 70 5A CB 15 AF 48 F7 60 BD B8 EF D4 24 B6 13 B0 6A AD AA B4 1A 74 85 51 70 66 16 DB 3C 6E CB 77 83 F9 07 50 4B 01 02 1F 00 14 00 00 00 08 00 F8 A1 70 49 47 03 AE 5A 0C 01 00 00 CF 01 00 00 08 00 24 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 6D 65 74 61 2E 78 6D 6C 0A 00 20 00 00 00 00 00 01 00 18 00 D5 7B 51 D6 3D 40 D2 01 7B 2E CC 6E 3D 40 D2 01 77 A8 20 F9 15 38 D2 01 50 4B 01 02 1F 00 14 00 00 00 08 00 14 A2 70 49 49 9E F7 3C 8A 01 00 00 73 04 00 00 0A 00 24 00 00 00 00 00 00 00 20 00 00 00 32 01 00 00 73 65 72 76 65 72 2E 6C 75 61 0A 00 20 00 00 00 00 00 01 00 18 00 FD 94 C1 F5 3D 40 D2 01 5F 8E 0B D8 3D 40 D2 01 A9 1D 21 F9 15 38 D2 01 50 4B 05 06 00 00 00 00 02 00 02 00 B6 00 00 00 E4 02 00 00 00 00

It's the hex of the zip file xD 

Link to comment
  • Other Languages Moderators
On 16/11/2016 at 4:57 PM, 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

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

Spoiler
Quote

It is also possible to add finer priority control by appending a positive or negative number to the priority string. For example (in priority order for reference): "high+4" "high" "high-1" "normal-6" "normal-7" "low+1" "low" "low-1" Important note: Anything bound to a specific element will be run before other handlers that are bound to something higher in the element tree (like root) This means that "high+10" bound to root won't trigger before "normal" bound directly to an element.

and also i read somewhere (i can't find where otherwise i would be quoting, sry) there's no guaranteed order executing 2 handlers with the same priority.

 

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...