Raised This Month: $12 Target: $400
 3% 

[HOWTO] Finding a Random Origin (Scanning)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 01-13-2007 , 18:12   [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #1

Before you begin, this tutorial assumes that you are an intermediate to advanced level scripter. Most things to do with any implementations in AMXX will not be explained, and it will assume that you understand everything except for the way to do this itself (wouldn't really be a tutorial, would it?).

What I'm basically going to be explaining is how to find a random origin that is empty and can be used to put something at, do something with, or otherwise use in some way.

The most important thing to do is establish a refence point. What are you trying to do? Are you trying to find an origin around a person? Are you trying to find a random place on the map to drop something? For the purpose of this, we will assume the former.

This function can be abstracted out quite easily:

Code:
FindEmptyLoc(id,Float:Origin[3],&Num,Float:Radius) {     if(Num++ > 100)         return PLUGIN_CONTINUE         new Float:pOrigin[3]     pev(id,pev_origin,pOrigin)         for(new Count;Count < 2;Count++)         pOrigin[Count] += random_float(-Radius,Radius)         if(PointContents(pOrigin) != CONTENTS_EMPTY && PointContents(pOrigin) != CONTENTS_SKY)         return FindEmptyLoc(id,Origin,Num,Radius)         Origin = pOrigin         return PLUGIN_HANDLED }

This will scan around the user with a radius of Radius units for an empty location. If it doesn't find any, it will return 0, otherwise it will return 1. Num is simply a dummy variable to track how many times it has scanned around the user. It's best to set this to 0.

If you want to be sure the user can see it, you can run a trace_line (or TraceLine) between the user and the result. I assume you know how to do this and will not draw it out.

Here's an example implementation:

Code:
public MyFunc(id) {     new Float:Origin[3],Num     pev(id,pev_origin,Origin)     if(!FindEmptyLoc(id,Origin,Num,100.0))          return     new Ent = create_entity("info_target")     if(!Ent)          return         set_pev(Ent,pev_classname,"zomg")     set_pev(Ent,pev_model,"models/mymodel.mdl")     engfunc(EngFunc_SetOrigin,Ent,Origin) }

This will find an empty origin around the player, spawn an ent, then set its model and origin to that empty location.

Anyway, this may seem pretty simple, but a few people have asked me about how to do it, so I thought I'd write this up.

As always, if you have any questions, comments, or whatever else you can think of, feel free to post.
__________________

Last edited by Hawk552; 01-13-2007 at 18:59. Reason: Fixed a small typo
Hawk552 is offline
Send a message via AIM to Hawk552
Old 01-13-2007, 20:18
stupok
This message has been deleted by Hawk552.
Old 01-13-2007, 20:29
Hawk552
This message has been deleted by Hawk552.
Old 01-13-2007, 21:21
stupok
This message has been deleted by Hawk552.
Old 01-13-2007, 21:23
Hawk552
This message has been deleted by Hawk552.
Old 01-13-2007, 22:00
Charming
This message has been deleted by Hawk552.
Cheap_Suit
Veteran Member
Join Date: May 2004
Old 01-15-2007 , 06:51   Re: Finding a Random Origin (Scanning)
Reply With Quote #2

WOW! I needed his! Thnx
__________________
HDD fried, failed to backup files. Sorry folks, just don't have free time anymore. This is goodbye.
Cheap_Suit is offline
regalis
Veteran Member
Join Date: Jan 2007
Location: F*cking Germany
Old 05-13-2007 , 11:03   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #3

Nice Tutorial Hawk552 !!
I used this method in my new Plugin, but i have encountered that the Webcompiler have a problem with random_float()
Quote:
Originally Posted by Sawce
It looks like the webcompiler's float.inc wasn't updated for 1.76d.
...
...
Try taking all instances of -Float (such as -Radius like you do in that function) and replace it with this:

(-1 * Variable)

See if that fixes it.
Now i have made this changes and it works flawless with the Webcompiler...

Code:
FindEmptyLoc(id,Float:Origin[3],&Num,Float:Radius) {     if(Num++ > 100)         return PLUGIN_CONTINUE         new Float:pOrigin[3]     new Float:rnd;     pev(id,pev_origin,pOrigin)         for(new Count;Count < 2;Count++)     {         // This doesn't work with the Webcompiler...you have to do a workaround...         //pOrigin[Count] += random_float(-Radius,Radius)         rnd = random_float(0.0, Radius)         if(random(2))             pOrigin[Count] += rnd         else             pOrigin[Count] -= rnd     }     if(PointContents(pOrigin) != CONTENTS_EMPTY && PointContents(pOrigin) != CONTENTS_SKY)         return FindEmptyLoc(id,Origin,Num,Radius)         Origin = pOrigin         return PLUGIN_HANDLED }

Maybe you can improve my changes!?
Because i don't really understand the (-1 * Variable) thingy...
If i change random_float(-Radius, Radius) to random_float((-1 * Radius), Radius) where is the difference!?..0o

greetz regalis

PS: keep up the good work! Your tutorials are very helpfull!!!
__________________
regalis is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 05-13-2007 , 11:57   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #4

It's related to Float:operator-(Float:oper) stock function from float.inc issue that was fixed but they didn't updated webcompiler includes.

The difference is that -1 (or even -1.0) isn't pass to the operator because it's a constant value.
VEN is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 05-14-2007 , 05:13   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #5

Recursion... might be a bit costly if a valid point is not found in the first 2-3 attempts.
_Master_ is offline
regalis
Veteran Member
Join Date: Jan 2007
Location: F*cking Germany
Old 05-14-2007 , 05:42   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #6

Quote:
Originally Posted by _Master_ View Post
Recursion... might be a bit costly if a valid point is not found in the first 2-3 attempts.
Do you think a while loop would be more suitable?
__________________
regalis is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 05-14-2007 , 06:44   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #7

Quote:
Originally Posted by _Master_ View Post
Recursion... might be a bit costly if a valid point is not found in the first 2-3 attempts.
Agreed, I'll rewrite it later.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Zenith77
Veteran Member
Join Date: Aug 2005
Old 05-14-2007 , 11:42   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #8

Quote:
Originally Posted by regalis View Post
Do you think a while loop would be more suitable?
How would that help?
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
regalis
Veteran Member
Join Date: Jan 2007
Location: F*cking Germany
Old 05-14-2007 , 12:24   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #9

Quote:
Originally Posted by Zenith77 View Post
How would that help?
huh?
What helps whom?
The code is made recursive, and _master_ thought about the costs of this recursion...then i thought that maybe a while loop would be more suitable thats why i asked if that would be better...
__________________
regalis is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 05-15-2007 , 15:30   Re: [HOWTO] Finding a Random Origin (Scanning)
Reply With Quote #10

Quote:
Originally Posted by regalis View Post
Do you think a while loop would be more suitable?
Any recursive algorithm has an iterative implementation. The thing is that if there's no valid point it will loop forever. This won't be the case.
It all depends on how Hawk552 decides to implement that algorithm. (might I add that this issue has been dealt with before and handled - but I guess that neither Backtracking, Greedy, partitioning nor dynamic methods would help )

Last edited by _Master_; 05-15-2007 at 15:35.
_Master_ is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:27.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode