AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Dynamic Array help (https://forums.alliedmods.net/showthread.php?t=88449)

TheDOO 03-24-2009 20:51

Dynamic Array help
 
Ok, well right now I have a plugin that stores damage that is done to an entity by a player into an array.

Right now the array is defined as:
Code:

gDamageDone[ MAX_PLAYERS + 1][ 4381 ]
I would like not having to declare a large array like that, so I want the 2nd dimension to be dynamic, depending on the amount of entities in the current map.

Now I understand that I would probably use CellArray, but I am unsure how to go about doing this and still use the array like I do, example:
Code:

public Event_MonsterTakeDamage( idEnt, idInflictor, idAttacker, Float:damage, damageBits )
{
    ... stuff here
   
    gDamageDone[ idAttacker ][ idEnt ] += damage
}


ot_207 03-25-2009 03:23

Re: Dynamic Array help
 
I haven't used the dynamic arrays yet. But I will just give you a typ.
gDamageDone[ MAX_PLAYERS + 1][ 4381 ] ->
gDamageDone[ MAX_PLAYERS + 1][ 1126 ]

The engine supports something like 1126 entities. You don't need 4381 o.O.

ConnorMcLeod 03-25-2009 03:40

Re: Dynamic Array help
 
Quote:

Originally Posted by ot_207 (Post 788587)
The engine supports something like 1126 entities.

Depend on -edict param or -edict_num or -max_edict can't remember, search for a post by orangutanz about this.

Arkshine 03-25-2009 06:59

Re: Dynamic Array help
 
The switch name is : -num_edicts

Quote:

The engine supports something like 1126 entities. You don't need 4381 o.O.
Max entities is calculated like that : max_ents = 900 + 15 * ( max_slots - 1 )
You can define a custom and higher number with the swith : -num_edicts

ot_207 03-25-2009 08:14

Re: Dynamic Array help
 
Quote:

Originally Posted by arkshine (Post 788633)
The switch name is : -num_edicts

Max entities is calculated like that : max_ents = 900 + 15 * ( max_slots - 1 )
You can define a custom and higher number with the swith : -num_edicts

Ok didn't know that. :mrgreen:

TheDOO 03-25-2009 17:17

Re: Dynamic Array help
 
Yeah my -num_edicts is at 4096 then I used:
Code:

global_get(glb_maxEntities)
To get the 4381 that I declared the array as. But I know there must be some way to make it dynamic. Or is just declaring this large array fine as long as it's global?

ot_207 03-25-2009 19:42

Re: Dynamic Array help
 
I think that we need a diferent aproach.
I suggest doing something like this:

PHP Code:

new Float:player_attacked_entities[MAX_PLAYERS][100][2]
player_attacked_entities[id][slot_number][0] for entity id that has been attacked
player_attacked_entities
[id][slot_number][1the damage done to that entity 

What do you want to do exactly?

TheDOO 03-25-2009 20:41

Re: Dynamic Array help
 
Quote:

Originally Posted by ot_207 (Post 789111)
I think that we need a diferent aproach.
I suggest doing something like this:

PHP Code:

new Float:player_attacked_entities[MAX_PLAYERS][100][2]
player_attacked_entities[id][slot_number][0] for entity id that has been attacked
player_attacked_entities
[id][slot_number][1the damage done to that entity 

What do you want to do exactly?

Basically right now I am putting the damage done to a certain entity into the array: ( called in a Ham_TakeDamage hook )
Code:

    gDamageDone[ idAttacker ][ idEnt ] += damage;
Later on I call a function when the entity dies that contains this:
Code:

    ... stuff here
   
    new player, numPlayers;
    static players[ MAX_PLAYERS ];
   
    get_players( players, numPlayers );
   
    for ( new i = 0; i < numPlayers; i++ )
    {
          player = players[ i ];
         
          if ( gDamageDone[ player ][ idVictim ] > 0 )
          {
              ArrayPushCell( playerAssisted, player );
                   
              gDamageDone[ player ][ idVictim ] = 0;
          }
      }

I mainly just get all the players that did damage to the specific entity that died, do some calculations with them, then zero the damage done to that entity because the entity ID can be used by a different entity later on.

MeRcyLeZZ 03-31-2009 23:22

Re: Dynamic Array help
 
1 Attachment(s)
Using dynamic arrays for this would be kinda unefficient:
Quote:

Originally Posted by cellarray.inc
/**
* These arrays are intended to be used for a form of global storage without
* requiring a #define that needs to be increased each time a person needs more
* storage.
* These are not designed to be used as a replacement for normal arrays, as
* normal arrays are faster and should be used whenever possible.
*/

If you don't care about it though, try this:
Code:
#include <amxmodx> #include <hamsandwich> #define MAX_PLAYERS 32 new Array:gEntityIndex[MAX_PLAYERS + 1] // Attacked entity's index new Array:gDamageDone[MAX_PLAYERS + 1] // Damage done to attacked entity public plugin_init() {     // Replace "player" with your entity's classname     RegisterHam( Ham_TakeDamage, "player", "Event_MonsterTakeDamage" )         // Create arrays for each player     for ( new player = 1; player <= get_maxplayers(); player++ )     {         gEntityIndex[player] = ArrayCreate( 1, 1 )         gDamageDone[player] = ArrayCreate( 1, 1 )     } } public Event_MonsterTakeDamage( idEnt, idInflictor, idAttacker, Float:damage, damageBits ) {     new Float:totaldamage     new index = -1     new size = ArraySize( gEntityIndex[idAttacker] )         // Check whether entity's data already exists on attacker's array     for ( new i = 0; i < size; i++ )         if ( idEnt == ArrayGetCell( gEntityIndex[idAttacker], i ) )             index = i;         // If index is still -1 at this point, there's no data for this entity yet     if ( index == -1 )     {         // Allocate new data         ArrayPushCell( gEntityIndex[idAttacker], idEnt )         ArrayPushCell( gDamageDone[idAttacker], damage )                 // Set index and totaldamage for following checks         index = size;         totaldamage = damage;     }     else     {         // Modify existent data         new Float:olddamage = ArrayGetCell( gDamageDone[idAttacker], index )         ArraySetCell( gDamageDone[idAttacker], index, olddamage + damage)                 // Set totaldamage for following checks         totaldamage = olddamage + damage;     }         //server_print("DEBUG: idEnt %d idAttacker %d arrayindex %d totaldmg %f", idEnt, idAttacker, index, totaldamage)         // Print message to player when entity's damage reaches 500.0     if (totaldamage >= 500.0)     {         client_print(idAttacker, print_center, "OMG! You've done a total of 500 dmg to ent %d!", idEnt)                 // Reset damage data         ArraySetCell( gDamageDone[idAttacker], index, 0.0)                 // Use this instead if you want the data to be erased (to save memory)         //ArrayDeleteItem( gEntityIndex[idAttacker], index )         //ArrayDeleteItem( gDamageDone[idAttacker], index )     } }

Dores 04-01-2009 14:23

Re: Dynamic Array help
 
@MeRcyLeZZ: You should create a var to store get_maxplayers()'s value because the loop will execute it every time the loop's condition is being checked.


All times are GMT -4. The time now is 09:00.

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