View Single Post
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