Raised This Month: $51 Target: $400
 12% 

enum and array question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
tristen620
Junior Member
Join Date: Feb 2012
Old 02-02-2014 , 21:51   enum and array question
Reply With Quote #1

I haven't ever benchmarked or speed tested any sourcepawn stuff so I would like to know if it would be a better idea to either..
A) Create a large set of global arrays for different types of information.
-or-
B) Create enum's for the various sets of information like the following...

If anyone has links to information I can consume or experience in doing something similar a comment would be appreciated, thank you.

Code:
#define MAXCHARACTERS 12

enum RACES
{
  NONE,
  ELF,
  DRAGONBORN,
  DWARF,
  GOBLIN,
  HUMAN,
  ORC,
};

enum CLASSES
{
  NONE,
  BARD,
  SORCERER,
  CLERIC,
  ROGUE,
  WARRIOR,
  BARBARIAN,
};

enum characterStats
{
  Float:DEX,
  Float:HASTE,
  Float:INT,
  Float:RESIST,
  Float:STA,
  Float:STR,
  Float:ACC,
  Float:ARMOR,
  Float:ARMOR_IGNORE,
  Float:ATTACK_SPEED,
  Float:BLOCK_AMOUNT,
  Float:BLOCK_RATE,
  Float:CRIT,
  Float:DAMAGE,
  Float:HPREGEN,
  Float:MAXHP,
  Float:MAXMP,
  Float:MOVESPEED,
  Float:MP_REGEN,
  Float:PARRY_AMOUNT,
  Float:PARRY_RATE,
  Float:RESIST_MAGICAL,
  Float:RESIST_PHYSICAL,
  Float:SPELL_DAMAGE,
  Float:WEAPONSPEED,
};

enum characterList
{
  characterId[MAXCHARACTERS],
  level[MAXCHARACTERS],
  race[MAXCHARACTERS],
};

enum equipmentTypes
{
  NONE,
  LIGHT,
  MEDIUM,
  HEAVY,
};

enum handedness
{
  NONE,
  ONE,
  TWO,
  SHIELD,
  OFFHAND,
};

enum rarity
{
  COMMON,
  UNCOMMON,
  RARE,
  EPIC,
  LEGENDARY,
  INVALUABLE,
};
enum equipmentData
{
  equipmentId,
  equipmentType:type,
  handedness:hand,
  rarity:quality,
  Float:DEX,
  Float:HASTE,
  Float:INT,
  Float:RESIST,
  Float:STA,
  Float:STR,
  Float:ACC,
  Float:ARMOR,
  Float:ARMOR_IGNORE,
  Float:ATTACK_SPEED,
  Float:BLOCK_AMOUNT,
  Float:BLOCK_RATE,
  Float:CRIT,
  Float:DAMAGE,
  Float:HP_MAX,
  Float:HP_REGEN,
  Float:MOVESPEED,
  Float:MP_MAX,
  Float:MP_REGEN,
  Float:PARRY_AMOUNT,
  Float:PARRY_RATE,
  Float:RESIST_MAGICAL,
  Float:RESIST_PHYSICAL,
  Float:SPELL_DAMAGE,
  Float:WEAPONSPEED,
};

enum equipmentSlots
{
  HELMET[equipmentData],
  CHEST[equipmentData],
  PANTS[equipmentData],
  BOOTS[equipmentData],
  ACCESSORY1[equipmentData],
  ACCESSORY2[equipmentData],
  MELEELEFT[equipmentData],
  MELEERIGHT[equipmentData],
};

enum activeCharacter
{
  characterId,
  level,
  class[CLASSES],
  RACES:race,
  baseStats[characterStats],
  bonusStats[characterStats],
  equipment[equipmentSlots],
};

enum playerData
{
  playerId,
  charList[characterList],
  character[activeCharacter],
  Float:currentHp,
  Float:currentMp,
  mastery,
};

new users[MAXPLAYERS+1][playerData];
tristen620 is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 02-02-2014 , 22:20   Re: enum and array question
Reply With Quote #2

the performance is almost exactly the same. One problem is, from what people said, it impossible to put array into enum, but it is not true. The only questions now is are you ready for a brain fuck and some pain?

PHP Code:
public OnPluginStart()
{
    
decl String:buffer_index[35], String:buffer[4][8];
    
Format(buffer_index35"%d+%d+%d+%d"playerData:characteractiveCharacter:equipmentequipmentSlots:HELMETequipmentData:DEX);
    
ExplodeString(buffer_index"+"buffer48);
    new 
index;
    for (new 
i4i++)
    {
        
index += StringToInt(buffer[i]);
    }
    
users[0][index] = 5.0;
    
    
PrintToChatAll("player 0 active character helmet dexterity equal to %d"users[0][index]);

By the way if you really wanna keep the method of putting array into enums (that are not strings) you will need to create functions doing the similar thing above to get your indexes so the code will be readable and yes this is slower than standard array because you need to find the index of the value you looking for into you user variable. But we can't talk about a big issue of performance.

You asked for it.

EDIT: Hope you don't give up cause it a fun world to play with! Just hope the compiler would get over it and let us doing math with enum members.

Last edited by Mathias.; 02-02-2014 at 22:32.
Mathias. is offline
tristen620
Junior Member
Join Date: Feb 2012
Old 02-02-2014 , 22:46   Re: enum and array question
Reply With Quote #3

Code:
main()
{
  new client=1; //just an example
  users[client][playerId]=1;
    users[client][character][baseStats][DEX]=3.0;
  users[client][charList][characterId][charNum]=99;
    return;
}
It allows access to deep members on the web compiler AND it does not contain strings or make use of them for storing information.

Additionally I won't actually be directly using these all over the place, it is mainly so that if needed in two lines I can erase any portion of a players data or the entire thing, the information will be written either from storage in an SQL DB or updated to be current at player spawn in the case of things like currentHp.

Code:
main()
{
  new empty[playerData];
  users[client]=empty;
  /** a player's data has been entirely reset, no need for iterative loops and making sure I didn't miss anything.
    * plus, if i add new stats I don't have to update code in a million places, my enums take care of it with single lines.
    */
}

I suppose I have answered my own question in a way, I'd be willing to sacrifice a little speed for future compatibility.
tristen620 is offline
tristen620
Junior Member
Join Date: Feb 2012
Old 02-02-2014 , 22:53   Re: enum and array question
Reply With Quote #4

Quote:
Originally Posted by Black-Rabbit View Post
EDIT: Hope you don't give up cause it a fun world to play with! Just hope the compiler would get over it and let us doing math with enum members.
Just noticed your edit, my enumerator set does allow for accessing members computing things and re-storing them without issue, web compiler doesn't mind.

Code:
main(client)
{
  new client=1;
  users[client][character][baseStats][MAXHP] =10;
  users[client][character][baseStats][MAXHP]*=2.3;
  users[client][currentHP]=users[client][character][baseStats][MAXHP];
}
Yes, I realize this is a LOT to type to get this simple information but this will be in functions like: GetMaxHP, SetMaxHP, GetCurrentHP, SetCurrentHP, etc.

*edit* not new to coding or sourcepawn but uninformed enough to know i should ask a question.

Last edited by tristen620; 02-02-2014 at 22:58.
tristen620 is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 02-02-2014 , 23:10   Re: enum and array question
Reply With Quote #5

Quote:
Originally Posted by tristen620 View Post
Just noticed your edit, my enumerator set does allow for accessing members computing things and re-storing them without issue, web compiler doesn't mind.

Code:
main(client)
{
  new client=1;
  users[client][character][baseStats][MAXHP] =10;
  users[client][character][baseStats][MAXHP]*=2.3;
  users[client][currentHP]=users[client][character][baseStats][MAXHP];
}
Yes, I realize this is a LOT to type to get this simple information but this will be in functions like: GetMaxHP, SetMaxHP, GetCurrentHP, SetCurrentHP, etc.

*edit* not new to coding or sourcepawn but uninformed enough to know i should ask a question.
Are you kidding me? This is working, ffs I feel so bad. We learn every days, wow seriously nice, i didnt knew about that. Well to answer your question, in this case this do not slower down the process.
Mathias. is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 02-03-2014 , 15:21   Re: enum and array question
Reply With Quote #6

The only expensive operation about these static arrays is initialization. Usually that's done when the plugin is loading (if it's a global variable) and isn't noticeable.

For regular access we're talking about nano seconds, so it's nothing to worry about. If it's too slow, it's the algorithm that must be improved. What's more important is that the code is readable and easy to maintain.

This solution works great if you need high performance: http://forums.alliedmods.net/showthread.php?t=157406

Note that there are few dimensions. Three or fewer dimensions might be optimal in my opinion. If it's deeper you might want to consider multiple data structures with references to eachother to flatten the big old structure.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 02-03-2014 at 15:27.
rhelgeby is offline
Send a message via MSN to rhelgeby
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 02-03-2014 , 16:43   Re: enum and array question
Reply With Quote #7

Quote:
Originally Posted by rhelgeby View Post
The only expensive operation about these static arrays is initialization. Usually that's done when the plugin is loading (if it's a global variable) and isn't noticeable.

For regular access we're talking about nano seconds, so it's nothing to worry about. If it's too slow, it's the algorithm that must be improved. What's more important is that the code is readable and easy to maintain.

This solution works great if you need high performance: http://forums.alliedmods.net/showthread.php?t=157406

Note that there are few dimensions. Three or fewer dimensions might be optimal in my opinion. If it's deeper you might want to consider multiple data structures with references to eachother to flatten the big old structure.
I have a question for you. I'm currently converting my member to a valid vec buffer because no functions with Float:vec[3] parameters work directly with my enum array:

PHP Code:
#include <sourcemod>

enum vector
{
    
Float:x,
    
Float:y,
    
Float:z
}

/* return structures point vector to a valid sourcemod vector */

stock MemberToVec(const member[], Float:vec[3])
{
    for (new 
i3i++)
    {
        
vec[i] = Float:member[i];
    }
}

enum struct_weapon
{
    
m_pos[vector]
};

new 
myvar[struct_weapon];

public 
OnPluginStart()
{
    
myvar[m_pos][x] = 5.231521;
    
myvar[m_pos][y] = 2.325124;
    
myvar[m_pos][z] = 1.831457;

    new 
Float:pos[vector];
    
MemberToVec(myvar[m_pos], pos);
    
PrintToChatAll("vec lenght: %f"GetVectorLength(pos));

Is there a way to do it?

Last edited by Mathias.; 02-03-2014 at 16:49.
Mathias. is offline
tristen620
Junior Member
Join Date: Feb 2012
Old 02-04-2014 , 01:00   Re: enum and array question
Reply With Quote #8

What is your goal usage for the code above, very likely it has become over thought or over complicated. Fill me in and I may be able to help ya out
tristen620 is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 02-04-2014 , 19:00   Re: enum and array question
Reply With Quote #9

I want to be able to use my m_pos member directly into a function that support Float:vec[3] parameters but it always throw errors saying that the array is wrong or too small. So I had to create a sort of buffer function to store it up but what is nice about it is when you use vector in a single array it work just fine and u can use the tag x, y, z instead of 0, 1, 2

EDIT: oh just for example if you wanted to save player position. My buffer will work but if it could work without it, it would be nice.

by the way here is the reverse solution for storing a vector array into a structure array (yes I call them structures):

PHP Code:
// VecToMember(pos, Float:myvar[m_pos]);
stock VecToMember(const Float:vec[3], Float:member[])
{
    for (new 
i3i++)
    {
        
member[i] = vec[i];
    }

Yes you have to cast your structure array to Float to make it work.

Last edited by Mathias.; 02-04-2014 at 19:08.
Mathias. 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 10:42.


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