Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » Find nearest vehicle?
Find nearest vehicle? [message #446647] Sat, 07 May 2011 04:51 Go to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
is there a way to find the nearest vehicle?

in C++


Owner of kambot TT server

kambot.freeforums.org
Re: Find nearest vehicle? [message #446651 is a reply to message #446647] Sat, 07 May 2011 08:19 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
Something like this should do the trick. Thumbs Up

GameObject * getClosestVehicle(GameObject *sourceObject)
{
	GenericSLNode *x = SmartGameObjList->HeadNode;
	float closestDist = FLT_MAX;
	Vector3 sourceDist = Commands->Get_Position(sourceObject);
	GameObject *closestVeh = NULL;
	
	while (x)
	{
		GameObject *o = (GameObject *)x->NodeData;
		if (o && o != sourceObject && Is_Vehicle(o))
		{
			Vector3 TargetPos = Commands->Get_Position(o);
			float distance = Commands->Get_Distance(sourceDist, TargetPos);
			if (distance < closestDist)
			{
				closestDist = distance;
				closestVeh = o;
			}
		}
		x = x->NodeNext;
	}
	
	return closestVeh;
}


NOTE: This code might not be 100% because I wrote it down quickly in Notepad.


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Sun, 08 May 2011 13:00]

Report message to a moderator

Re: Find nearest vehicle? [message #446653 is a reply to message #446647] Sat, 07 May 2011 09:42 Go to previous messageGo to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
WOW thnx i wondered if there was an existing code already you just made my work easy Very Happy

only one small linking error beacause you initialise closestVeh but dont assign anythin to it

edit:
ok that didnt work out as i hoped to Sad


Owner of kambot TT server

kambot.freeforums.org

[Updated on: Sat, 07 May 2011 10:23]

Report message to a moderator

Re: Find nearest vehicle? [message #446665 is a reply to message #446647] Sat, 07 May 2011 15:07 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
You mean it does not work, or that it does not do what you want?
I'm fairly sure this gets the closest vehicle 0o

Also, if it does not work, please tell me what it is that does not work and don't just say that it doesn't work.


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Sat, 07 May 2011 15:08]

Report message to a moderator

Re: Find nearest vehicle? [message #446666 is a reply to message #446647] Sat, 07 May 2011 15:13 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
initalizing but not using using a variable should link correctly, unless you have 'treat warnings as errors' turned on in your compiler (most compilers warn if variables aren't used).

EDIT: closestVeh is assigned to 'o' in the inner if-loop. So the compiler shouldn't be giving that error.


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases

[Updated on: Sat, 07 May 2011 15:17]

Report message to a moderator

Re: Find nearest vehicle? [message #446667 is a reply to message #446647] Sat, 07 May 2011 15:15 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
What he is saying is that if *no* vehicles are on the map it returns a variable whose value is not defined. It's a simple fix to initialise it to NULL.

Nothing to do with linker errors....


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Sat, 07 May 2011 15:16]

Report message to a moderator

Re: Find nearest vehicle? [message #446668 is a reply to message #446647] Sat, 07 May 2011 16:36 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
Ok I added ' = NULL' Razz
Though that should not be a problem that prevents this code from working.


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg
Re: Find nearest vehicle? [message #446681 is a reply to message #446647] Sun, 08 May 2011 02:25 Go to previous messageGo to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
It didnt work because it does find the nearest vehicle wich is, if your in a vehicle, yourself

and i meant it didnt work as i hoped to, and i didnt really had found out why yet, but with some adjustements i got it to work Wink


Owner of kambot TT server

kambot.freeforums.org

[Updated on: Sun, 08 May 2011 02:28]

Report message to a moderator

Re: Find nearest vehicle? [message #446682 is a reply to message #446647] Sun, 08 May 2011 03:02 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
if (o && Is_Vehicle(o) && o != sourceObject)



Re: Find nearest vehicle? [message #446685 is a reply to message #446681] Sun, 08 May 2011 04:06 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
robbyke wrote on Sun, 08 May 2011 11:25

It didnt work because it does find the nearest vehicle wich is, if your in a vehicle, yourself

reborn wrote on Sun, 08 May 2011 12:02

if (o && Is_Vehicle(o) && o != sourceObject)


Right! I did not account for that Blush
Fixed the code above to inlude that check Thumbs Up


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg
Re: Find nearest vehicle? [message #446686 is a reply to message #446647] Sun, 08 May 2011 04:06 Go to previous messageGo to next message
saberhawk
Messages: 1068
Registered: January 2006
Location: ::1
Karma: 0
General (1 Star)
Also,
float closestDist = FLT_MAX;
Re: Find nearest vehicle? [message #446690 is a reply to message #446647] Sun, 08 May 2011 05:41 Go to previous message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
so what linker error did you get

Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Previous Topic: attach vehicle
Next Topic: modifying weapons&ammos with serverside scripts.dll
Goto Forum:
  


Current Time: Wed Dec 18 08:57:13 MST 2024

Total time taken to generate the page: 0.00983 seconds