AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   Feedback request (https://forums.alliedmods.net/showthread.php?t=308815)

hts 07-04-2018 15:06

Feedback request
 
Hey, did not figure out better place to post this.

I made my first AMX Mod X plugin for my server since I could not find plugin like this and it seems to be working OK, I guess I just wanted feedback that am I doing everything correct, properly and so on.

Code:

#include <amxmodx>
#include <csx>
#include <cstrike>

/* Changes kill rewards to match CSGO */
/* BUGS: HUD icon still shows +300 even if reward is more or less */
/* TODO: CVAR control for rewards, make other economy (round win, loss, by exploding bomb, by killing enemies) match CSGO too */

#define PLUGIN "Weapon Rewards Plugin"
#define VERSION "1.0"
#define AUTHOR "hts"

public plugin_init()
{
        register_plugin(PLUGIN,VERSION,AUTHOR)
        register_event("DeathMsg", "event_death", "a" )
}

public get_award( wpn_id )
{
        new kill_award = 0
       
        switch( wpn_id )
        {
                case CSW_P228:
                        kill_award = 300
                case CSW_SCOUT:
                        kill_award = 300
                case CSW_HEGRENADE:
                        kill_award = 300
                case CSW_XM1014:
                        kill_award = 900
                case CSW_MAC10:
                        kill_award = 600
                case CSW_AUG:
                        kill_award = 300
                case CSW_SMOKEGRENADE:
                        kill_award = 300
                case CSW_ELITE:
                        kill_award = 300
                case CSW_FIVESEVEN:
                        kill_award = 300
                case CSW_UMP45:
                        kill_award = 600
                case CSW_SG550:
                        kill_award = 100
                case CSW_GALIL:
                        kill_award = 600
                case CSW_FAMAS:
                        kill_award = 300
                case CSW_USP:
                        kill_award = 300
                case CSW_GLOCK18:
                        kill_award = 300
                case CSW_AWP:
                        kill_award = 100
                case CSW_MP5NAVY:
                        kill_award = 600
                case CSW_M249:
                        kill_award = 300
                case CSW_M3:
                        kill_award = 900
                case CSW_M4A1:
                        kill_award = 300
                case CSW_TMP:
                        kill_award = 600
                case CSW_G3SG1:
                        kill_award = 100
                case CSW_FLASHBANG:
                        kill_award = 300
                case CSW_DEAGLE:
                        kill_award = 300
                case CSW_SG552:
                        kill_award = 300
                case CSW_AK47:
                        kill_award = 300
                case CSW_KNIFE:
                        kill_award = 1500
                case CSW_P90:
                        kill_award = 300
                default:
                        return PLUGIN_CONTINUE
        }
       
        return kill_award
}

public event_death()
{
        new killer = read_data(1)
        new wpn_id = get_user_weapon(killer)
        new award = get_award( wpn_id )
       
        if( award > 0 )
        {
                new cur_money = cs_get_user_money( killer );
                new num_money = 0;
               
                if( cur_money + award >= 16000 )
                {
                        num_money = 16000;
                }
                else
                {
                        num_money = cur_money + award;
                        num_money -= 300; // CS 1.6 gives 300 dollars by default for every weapon, we don't want it in this case.
                }
               
                cs_set_user_money( killer, num_money );
        }
       
        return PLUGIN_CONTINUE
}

I realized later that same syntax as in C++:
Code:

case 1:
case 2:
case 3:
  return same_value_for_these;

would probably work for Pawn too. Am I correct? Cba to compile / upload to server again since I've got work in the morning. :D

I should also probably check killer / weapon_id variable values too before doing anything, dunno how well AMX Mod X (or Metamod?) handles mistakes like this but I will definately check their values in the future.

kristi 07-04-2018 20:22

Re: Feedback request
 
Quote:

Originally Posted by hts (Post 2601026)
same syntax as in C++:
Code:

case 1:
case 2:
case 3:
  return same_value_for_these;

would probably work for Pawn too. Am I correct?

Nope. In Pawn we do it like this:
PHP Code:

case 123:
{
    
// code


or
PHP Code:

case 123: return same_value_for_these


maqi 07-04-2018 20:40

Re: Feedback request
 
Quote:

Originally Posted by hts (Post 2601026)
Code:

case 1:
case 2:
case 3:
  return same_value_for_these;


You can't do this because in pawn you don't manually break the switch statement.

Anyhow, a few tips, if I may:

You don't want to use such a long switch with so many cases for something as simple as this. Simply create a constant array, something like this,

Code:
new const AWARD_MONEY[31] = { 0, 300, 500, 200, 100, 0, 200, 500 /* etc up to 31*/ } ;

Check https://wiki.alliedmods.net/CS_Weapons_Information on how to index the array ( 0th element shouldn't be used in this case )

Then simply
Code:
new award = AWARD_MONEY[wpn_id];
You don't need that whole function at all :D ( Always check if wpn_id is valid [ 1 > 30 ] so you don't run into index out of bounds errors )

Also, you don't need the num_money variable at all in this case, once you define the cur_money, work with it ( You kinda do anyway )

And the most important tip, post this kind of questions in scripting help in the future :D

hts 07-04-2018 23:17

Re: Feedback request
 
Quote:

Originally Posted by kristi (Post 2601065)
Nope. In Pawn we do it like this:
PHP Code:

case 123:
{
    
// code


or
PHP Code:

case 123: return same_value_for_these


Great, thanks!

Quote:

Originally Posted by maqi (Post 2601067)
Code:
new const AWARD_MONEY[31] = { 0, 300, 500, 200, 100, 0, 200, 500 /* etc up to 31*/ } ;

Check https://wiki.alliedmods.net/CS_Weapons_Information on how to index the array ( 0th element shouldn't be used in this case )

Then simply
Code:
new award = AWARD_MONEY[wpn_id];
You don't need that whole function at all :D ( Always check if wpn_id is valid [ 1 > 30 ] so you don't run into index out of bounds errors )

Also, you don't need the num_money variable at all in this case, once you define the cur_money, work with it ( You kinda do anyway )

And the most important tip, post this kind of questions in scripting help in the future :D

Didn't even think of using array, that probably might be optimized as little bit faster and best part of all, less code.

Thanks to both of you


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

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