• All, Gmail is currently rejecting messages from my host. I have a ticket in process, but it may take some time to resolve. Until further notice, do NOT use Gmail for your accounts. You will be unable to receive confirmations and two factor messages to login.

Count number of enemies within a distance

Toranks

Active member
I have a script that emulates a taunt behavior. Holding down A2 executes a freespecial with
Code:
@cmd taunt XX
that gives a little MP if there is an enemy close enough (50 pixels around in this code).

Now the question. How could I also count the number of enemies within that distance, if there is more than one?

C#:
void taunt(int add)
{
    void self = getlocalvar("self");
    void MP = getentityproperty(self,"mp");
    void target = findtarget(self);
    int x = getentityproperty(self, "x");
    int z = getentityproperty(self, "z");
    void enemy = getentityproperty(target,"type");

    if(target != NULL() && enemy == openborconstant("TYPE_ENEMY"))
    {
        float Tx = getentityproperty(target, "x");
        float Tz = getentityproperty(target, "z");
        float Disx = Tx - x;
        float Disz = Tz - z;


         if( Disx >= -50 && Disx <= 50 && Disz >= -50 && Disz <= 50)
         {
            changeentityproperty(self, "mp", MP+add);
         }
    }
}
 
I have a script that emulates a taunt behavior. Holding down A2 executes a freespecial with
Code:
@cmd taunt XX
that gives a little MP if there is an enemy close enough (50 pixels around in this code).

Now the question. How could I also count the number of enemies within that distance, if there is more than one?

C#:
void taunt(int add)
{
    void self = getlocalvar("self");
    void MP = getentityproperty(self,"mp");
    void target = findtarget(self);
    int x = getentityproperty(self, "x");
    int z = getentityproperty(self, "z");
    void enemy = getentityproperty(target,"type");

    if(target != NULL() && enemy == openborconstant("TYPE_ENEMY"))
    {
        float Tx = getentityproperty(target, "x");
        float Tz = getentityproperty(target, "z");
        float Disx = Tx - x;
        float Disz = Tz - z;


         if( Disx >= -50 && Disx <= 50 && Disz >= -50 && Disz <= 50)
         {
            changeentityproperty(self, "mp", MP+add);
         }
    }
}

The engine keeps a built in count how many of certain types of entities are on screen, but to find out the number in a custom range area, you'll need to run an enumeration and increment a counter yourself.

See this tutorial on enumeration. The only difference is instead of taking an action on an entity, you'll check range, and if the entity is within range, you add +1 to an integer value. That will give you a total count.


HTH,
DC
 
I'm honestly surprised that it worked for me the first time. I seem to learn quickly. Now instead of a sum, the add value is a multiplier. If there are 0 enemies, it is 0. For each enemy between 300 and 100 px, X MP, for each enemy closer than 100 px, 3X MP.

There goes the code.

C#:
void taunt(int add)
{
    void self = getlocalvar("self");
    void MP = getentityproperty(self,"mp");
    int x = getentityproperty(self, "x");
    int z = getentityproperty(self, "z");
    int i = 0;
    int enemies = 0;
    int entity_count = openborvariant("count_entities");
  

    for (i = 0; i < entity_count; ++i) { // openborconstant("MAX_ENTS")
            void ent = getentity(i);

        if (getentityproperty(ent, "exists") && getentityproperty(ent,"type") == openborconstant("TYPE_ENEMY"))
        {
            float Tx = getentityproperty(ent, "x");
            float Tz = getentityproperty(ent, "z");
            float Disx = Tx - x;
            float Disz = Tz - z;

             if( Disx >= -100 && Disx <= 100 && Disz >= -100 && Disz <= 100)
             {
                enemies = enemies+3;
             }
             else if( Disx >= -300 && Disx <= 300 && Disz >= -300 && Disz <= 300)
             {
                enemies = enemies+1;
             }
        }
    }
    changeentityproperty(self,"mp", MP+(add*enemies));
}


If you see something that could fail in specific cases, or a more efficient way, tell me. Thank you so much! I understood your tutorial very well.
 
Last edited:
Back
Top Bottom