Raised This Month: $ Target: $400
 0% 

[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 #7

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 #8

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 #9

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 #10

Recursion... might be a bit costly if a valid point is not found in the first 2-3 attempts.
_Master_ is offline
Reply



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 01:28.


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