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

#define problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
raydan
Senior Member
Join Date: Aug 2006
Old 12-07-2007 , 02:07   #define problem
Reply With Quote #1

Code:
#define amerknife           1
#define bar                 2
#define weapon_bazooka      3
#define c96                 4
#define colt                5
#define frag_us             6
#define frag_ger            7
#define garand              8
#define riflegren_us        9
#define k98                 10
#define riflegren_ger       11
#define k98_scoped          12
#define m1carbine           13
#define mg42                14
#define mp40                15
#define mp44                16
#define p38                 17
#define pschreck            18
#define spade               19
#define spring              20
#define thompson            21
new Float:gWeaponKnockBack[MAX_WEAPON];
gWeaponKnockBack[mg42] = 1.0; // and more
 
public Action:ev_player_hurt(Handle:event,const String:name[],bool:dontBroadcast)
{
GetEventString(event,"weapon",weapon,65);
PrintToServer("%f",gWeaponKnockBack[weapon]); // how to get it?
}
raydan is offline
lamdacore
Member
Join Date: Aug 2005
Old 12-07-2007 , 02:26   Re: #define problem
Reply With Quote #2

you have to fill an array with the weapon strings, too. Now you can string compare these with the weapon you will get from GetEventString().

Code:
decl String:gWeaponStr[MAX_WEAPON][32];
strcopy(gWeaponSt[mg42], 32, "mg42");

if (!strcmp(weapon, gWeaponSt[mg42], 32)
{
  PrintToServer("%f",gWeaponKnockBack[mg42]);
}
lamdacore is offline
raydan
Senior Member
Join Date: Aug 2006
Old 12-07-2007 , 04:24   Re: #define problem
Reply With Quote #3

Quote:
Originally Posted by lamdacore View Post
you have to fill an array with the weapon strings, too. Now you can string compare these with the weapon you will get from GetEventString().

Code:
decl String:gWeaponStr[MAX_WEAPON][32];
strcopy(gWeaponSt[mg42], 32, "mg42");
 
if (!strcmp(weapon, gWeaponSt[mg42], 32)
{
  PrintToServer("%f",gWeaponKnockBack[mg42]);
}

it need to use many "if" to compare
raydan is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 12-07-2007 , 06:04   Re: #define problem
Reply With Quote #4

Defines are eliminated at compile time.

I'd do something like this:
(this is basically lambdacore's idea with a loop)


Code:
new const gWeaponNames[MAX_WEAPON][] = { "", // 0 "amerknife", // 1 "bar", //2 "tesT" } getweaponid(const weapname[]) { new i; for (i = 0; i < MAX_WEAPON; ++i) { if (strcmp(weapname, gWeaponNames[i]) == 0) return i; } return -1; // not found }


You'd have to watch out for MAX_WEAPON to be one id higher than the highest used weapon id.
__________________
hello, i am pm
PM is offline
pRED*
Join Date: Dec 2006
Old 12-07-2007 , 15:34   Re: #define problem
Reply With Quote #5

Hmm... I wonder if Bail could make some 'trie' natives available for coding as that would be much more efficient in situations like this..
pRED* is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 12-07-2007 , 16:55   Re: #define problem
Reply With Quote #6

You could use a simple hash algorithm, ie.

Code:
#define chars_per_cell (cellbits / 8)

compute_hash(const input[])
{
	new res = 0xFFFFFFFF;

	for (new i = 0; input[i]; ++i)
	{
		res ^= input[i] << ((i % chars_per_cell)*8));
		++i;
	}
	return res;
}
(i hope that this won't produce collisions for your data )

Then you could either pre-compute the hash table or compute them on plugin load:
Code:
new const gWeaponNames[MAX_WEAPON][] = { "", // 0
   "amerknife", // 1
   "bar", //2
   "something"
   ...
};

new gWeaponHashes[MAX_WEAPON];

// on plugin load:
for (new i = 0; i < MAX_WEAPON; ++i)
   gWeaponHashes[i] = compute_hash(gWeaponNames[i]);

// To retreive the index:
getweaponid(const weapname[])
{
   new hash = compute_hash(weapname);
   for (new i = 0; i < MAX_WEAPON; ++i)
   {
      if (gWeaponHashes[i] == hash)
         return i;
   }
   return -1;  // not found
}
Or something similar.
__________________
hello, i am pm
PM is offline
raydan
Senior Member
Join Date: Aug 2006
Old 12-07-2007 , 23:34   Re: #define problem
Reply With Quote #7

i am using keyvalue to do that,,b ut i don't know use keyvalue is fast or not
this is keyvalue file
Code:
"zm_weapon"
{
 "30cal" "0.5"  //weapon_30cal
 "amerknife" "1"  //weapon_amerknife
 "bar" "1"  //weapon_bar
 "bazooka" "5"  //weapon_bazooka
}
Code:
weapon_kv = CreateKeyValues("zm_weapon");
 FileToKeyValues(weapon_kv, "cfg/weapon.cfg");
 
stock Float:GetWeaponKnockBack(String:weapon[])
{
 return KvGetFloat(weapon_kv,weapon,1.0);
}
raydan is offline
pRED*
Join Date: Dec 2006
Old 12-08-2007 , 10:51   Re: #define problem
Reply With Quote #8

Kv's are quite slow. You'd be better to parse it into an array when you load the kv.
pRED* is offline
BAILOPAN
Join Date: Jan 2004
Old 12-08-2007 , 11:08   Re: #define problem
Reply With Quote #9

KeyValues aren't bad for small configuration sets. But I agree that if you can extract configuration data into a faster structure, it will be better.

Supporting tries (fast string-based lookups) in SourceMod scripts wouldn't be too hard. Someone should file a feature request.
__________________
egg
BAILOPAN 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 03:07.


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