Raised This Month: $ Target: $400
 0% 

Dynamic Array help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TheDOO
Member
Join Date: Nov 2007
Old 03-24-2009 , 20:51   Dynamic Array help
Reply With Quote #1

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
}

Last edited by TheDOO; 03-24-2009 at 20:56.
TheDOO is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 03-25-2009 , 03:23   Re: Dynamic Array help
Reply With Quote #2

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.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 03-25-2009 , 03:40   Re: Dynamic Array help
Reply With Quote #3

Quote:
Originally Posted by ot_207 View Post
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.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 03-25-2009 , 06:59   Re: Dynamic Array help
Reply With Quote #4

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
Arkshine is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 03-25-2009 , 08:14   Re: Dynamic Array help
Reply With Quote #5

Quote:
Originally Posted by arkshine View Post
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.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
TheDOO
Member
Join Date: Nov 2007
Old 03-25-2009 , 17:17   Re: Dynamic Array help
Reply With Quote #6

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?
TheDOO is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 03-25-2009 , 19:42   Re: Dynamic Array help
Reply With Quote #7

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?
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
TheDOO
Member
Join Date: Nov 2007
Old 03-25-2009 , 20:41   Re: Dynamic Array help
Reply With Quote #8

Quote:
Originally Posted by ot_207 View Post
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.
TheDOO is offline
MeRcyLeZZ
Veteran Member
Join Date: Dec 2007
Old 03-31-2009 , 23:22   Re: Dynamic Array help
Reply With Quote #9

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 )     } }
Attached Files
File Type: sma Get Plugin or Get Source (dnyamicentdamage.sma - 505 views - 2.1 KB)
__________________
MeRcyLeZZ is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 04-01-2009 , 14:23   Re: Dynamic Array help
Reply With Quote #10

@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.
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ
Dores 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 09:00.


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