Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / GetClosestNPC

Author
Message
Krast0r
20
Years of Service
User Offline
Joined: 25th Jun 2004
Location: UK
Posted: 8th Aug 2004 08:09
Hi,
I am trying to make a function that would find the nearest NPC (Non-Player Character) to the location of the player. Here is an example:

function GetClosestNPC(x#,y#,z#)
code to get closest npc
return nearestnpc$

so when I wanted to know the nearest NPC of a player I can just GetClosestNPC(playerpositionx#,playerpositiony#,playerpositionz#). I thought about having static, or limited movement NPC's within a square so i could check for the nearest co-ordinates, but is there a better way? Any help would be very greatly appreciated.

Thanks,
Krast0r

In theory, theory and practice are the same, but in practice they are not.
[email protected] - That's Right! I'm a Gmail Beta Tester!
Powersoft
21
Years of Service
User Offline
Joined: 1st Aug 2003
Location: United Kingdom
Posted: 8th Aug 2004 22:30
for x=0-500 to 500
for y=0-500 to 500
for z=0-500 to 500
position object [object no of a random object],x,y,z
'check for a collision
'if the collision is with a npc object do stuff
next z
next y
next x


Create or Play? You choose!
Arkheii
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: QC, Philippines
Posted: 9th Aug 2004 20:25
@Powersoft: OMG... LOL! That would be slow as hell!

Well, in a situation where you really have to find the closest entity, you really have no choice but to search through all of them and make a distance comparison. The only way to speed it up would be to divide the searches if possible, like GetClosestFriendlyNPC(), GetClosestEnemyNPC(), GetClosestNeutralNPC(), etc. so if you know that you are in battle mode, you would only use GetClosestEnemyNPC(), and so on.

Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 9th Aug 2004 22:13
there should be a better idea ....
cut your map in tiles ... the NPC tiles depend on his position.
So you check a value that tell which npc are in the nearests tiles . so you calculate distance between the nearest ones ...
no need to check all NPC present in the entire map.

Powersoft
21
Years of Service
User Offline
Joined: 1st Aug 2003
Location: United Kingdom
Posted: 10th Aug 2004 04:34
i didnt say it would be fast..... just a slow... no wait... very slow alternative


Create or Play? You choose!
Krast0r
20
Years of Service
User Offline
Joined: 25th Jun 2004
Location: UK
Posted: 11th Aug 2004 01:51
Yea, that's the kind of thought process I went through. I may well just use static NPC's so I know their positions all the time, i'm probably going to remake my game using Torque or any otehr game engine I can afford Thanks guy's for your suggestions, i'll try them out to see which works best in my program!

In theory, theory and practice are the same, but in practice they are not.
[email protected] - That's Right! I'm a Gmail Beta Tester!
Powersoft
21
Years of Service
User Offline
Joined: 1st Aug 2003
Location: United Kingdom
Posted: 11th Aug 2004 01:54
write the npc to an array holding the positions


Create or Play? You choose!
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Aug 2004 06:36 Edited at: 11th Aug 2004 06:37
This might help ya.

http://forum.thegamecreators.com/?m=forum_view&t=36650&b=7

"eureka" - Archimedes

Login to post a reply

Server time is: 2025-05-25 13:47:40
Your offset time is: 2025-05-25 13:47:40