AreaEvent ported to AngelScript

The code below is identical to how Postal3Script's AreaEvent work.

Copy and paste them into your code, you can then call these functions with the filtered version, or without filtering!

Filtered version

void AreaEvent(CP3SObj@ center, string EventName, float Radius, array<string> Filter)
{
    Radius = Radius * 32;
    EventName.insert(0, "OnAE_");

    array<CP3SObj@> aNPC = engine.GetArrayOfEntitiesRadius(@center, Radius);
    for (uint i = 0; i < aNPC.length(); i++)
    {
        if ( aNPC[i] == null )
            continue;

        if ( aNPC[i] == @center )
            continue;

        bool bFire = false;
        for (uint j = 0; j < Filter.length(); j++)
        {
            string sFilter  = Filter[j]; sFilter.toLower();
            string Manner   = aNPC[i].GetManner(); Manner.toLower();
            string Faction  = aNPC[i].GetFaction(); Faction.toLower();
            string State    = aNPC[i].GetCurState(); State.toLower();

            if ( Manner == sFilter ||
                 Faction == sFilter ||
                 State == sFilter )
            {
                bFire = true;
                break;
            }
        }

        if (bFire)
            aNPC[i].FireEvent(EventName, center);
    }
}

Non-Filtered version

void AreaEvent(CP3SObj@ center, string EventName, float Radius)
{
    Radius = Radius * 32;
    EventName.insert(0, "OnAE_");

    array<CP3SObj@> aNPC = engine.GetArrayOfEntitiesRadius(@center, Radius);
    for (uint i = 0; i < aNPC.length(); i++)
    {
        if ( aNPC[i] == null )
            continue;

        if ( aNPC[i] == @center )
            continue;

        aNPC[i].FireEvent(EventName, center);
    }
}

Example (How to use)

Put the AreaEvent functions into a non-class AngelScript file, i.e. utils.as, and include it in your file you wish to use the functions from.

You can also just put it into a class, but it's generally a good idea to make your code clean.

For this example I'm going to assume I have already put the code into utils.as.

In the example below, all the objects will have OnAE_MyAreaEvent called on them, with the caller being the executioner. You can modify who the executioner is by using the first argument, so instead of self it could also be self.GetTarget(), of course if it exists in the first place ...

#include "Utils.as"

class MyClass : IPostal3Script
{
    void MyFunction()
    {
        CP3SObj @self = GetContextOwner();

        if ( @self == null )
            return;

        // There are 2 ways of using the Filtered version:
        // 1.) Putting the string array directly into the function
        AreaEvent( self, "MyAreaEvent", 8, {"Animals", "st_idle", "PussyCat"} );

        // 2.) Creating a string array, then putting the array into the function
        // Doing this is much more recommended, since you can easily swap out strings
        // and can also turn this into something dynamic
        array<string> Filter = 
        {
            "Animals",
            "st_idle",
            "PussyCat"
        };
        AreaEvent( self, "MyAreaEvent", 8, Filter );
        /////////////////////////////////////////////////////////////////////////

        // Non-filtered function will call this event on all nearby objects:
        AreaEvent( self, "MyAreaEvent", 8 );
    }
}