AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [HOWTO] Finding a Random Origin (Scanning) (https://forums.alliedmods.net/showthread.php?t=49829)

Hawk552 01-13-2007 18:12

[HOWTO] Finding a Random Origin (Scanning)
 
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.

Cheap_Suit 01-15-2007 06:51

Re: Finding a Random Origin (Scanning)
 
WOW! I needed his! Thnx

regalis 05-13-2007 11:03

Re: [HOWTO] Finding a Random Origin (Scanning)
 
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!!! :)

VEN 05-13-2007 11:57

Re: [HOWTO] Finding a Random Origin (Scanning)
 
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.

_Master_ 05-14-2007 05:13

Re: [HOWTO] Finding a Random Origin (Scanning)
 
Recursion... might be a bit costly if a valid point is not found in the first 2-3 attempts.

regalis 05-14-2007 05:42

Re: [HOWTO] Finding a Random Origin (Scanning)
 
Quote:

Originally Posted by _Master_ (Post 476476)
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?

Hawk552 05-14-2007 06:44

Re: [HOWTO] Finding a Random Origin (Scanning)
 
Quote:

Originally Posted by _Master_ (Post 476476)
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.

Zenith77 05-14-2007 11:42

Re: [HOWTO] Finding a Random Origin (Scanning)
 
Quote:

Originally Posted by regalis (Post 476480)
Do you think a while loop would be more suitable?

How would that help?

regalis 05-14-2007 12:24

Re: [HOWTO] Finding a Random Origin (Scanning)
 
Quote:

Originally Posted by Zenith77 (Post 476559)
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...

_Master_ 05-15-2007 15:30

Re: [HOWTO] Finding a Random Origin (Scanning)
 
Quote:

Originally Posted by regalis (Post 476480)
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 :D)


All times are GMT -4. The time now is 17:42.

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