AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   First plugin, need feedback (https://forums.alliedmods.net/showthread.php?t=229571)

2called-chaos 11-10-2013 02:37

First plugin, need feedback
 
Hey guys,

I just build my first plugin and it works! I know that there is a plugin doing exactly that but I couldn't find it anymore so I thought it would be a good first project :)

Would bee cool if someone more experienced could tell me if this would be considered good code. I'm normally ruby programmer where everything is an object and pawn doesn't even know the concept. That's also the reason why I did not put in any semicolons :)

Thanks in advance!

And as I like gists much more because you can comment on lines and stuff: https://gist.github.com/2called-chao...ed2c09f1d3d7e9

Changes I made after feedback: https://gist.github.com/2called-chao...d7e9/revisions

PHP Code:

/*
 *
 *  Author:   2called-chaos
 *  Date:     10-November-2013
 *
 *
 *  Description: "Flashes" user on each spawn or round start with it's team color.
 *               Especially on deathmatch it's sometimes surprising if you got teambalanced.
 *
 *  Cvars:
 *
 *      amx_who2shoot          1    Enable plugin
 *      amx_who2shoot_spawn    1    flash 1 = on every spawn; 0 = only on round start
 *      amx_who2shoot_stay     0.1  Duration layer is shown before fading it out
 *      amx_who2shoot_duration 0.3  Duration of the fade effect in seconds
 *      amx_who2shoot_alpha    150  Max. alpha of the layer (0 = invisible, 255 = solid)
 *
 *
 *  Requirements: AMXModX
 *                cstrike
 *                HamSandwich
 *                Screenfade Util (https://forums.alliedmods.net/showthread.php?t=87623)
 *
 */

#include <amxmodx>
#include <cstrike>
#include <hamsandwich>
#include <screenfade_util>

// Layer color CT (R, G, B)
#define CT_LAYER_COLOR_R 110
#define CT_LAYER_COLOR_G 156
#define CT_LAYER_COLOR_B 190

// Layer color T (R, G, B)
#define T_LAYER_COLOR_R 255
#define T_LAYER_COLOR_G 63
#define T_LAYER_COLOR_B 63

// Globals
new g_enabledg_flash_on_spawng_flash_stayg_flash_durationg_flash_alpha
new bool:g_round_started

public plugin_init()
{
    
register_plugin("who2shoot""1.0""2called-chaos")
    
g_round_started true

    
// events
    
register_event("HLTV" "event_new_round" "a" "1=0" "2=0")
    
RegisterHam(Ham_Spawn"player""event_player_spawned"1)
    
register_logevent("reset_round_started"2"1=Round_Start")
    
register_logevent("reset_round_started"2"1=Round_End")

    
// cvars
    
g_enabled        register_cvar("amx_who2shoot""1")
    
g_flash_on_spawn register_cvar("amx_who2shoot_spawn""1")
    
g_flash_stay     register_cvar("amx_who2shoot_stay""0.1")
    
g_flash_duration register_cvar("amx_who2shoot_duration""0.3")
    
g_flash_alpha    register_cvar("amx_who2shoot_alpha""180")
}

// ==========
// = EVENTS =
// ==========

// new round
public event_new_round()
{
    
g_round_started true
    
return PLUGIN_CONTINUE
}

// flash player on spawn
public event_player_spawned(id)
{
    
// plugin disabled
    
if (!get_pcvar_num(g_enabled))
        return 
PLUGIN_CONTINUE

    
// we don't flash on every spawn and round has not been started recently
    
if (!get_pcvar_num(g_flash_on_spawn) && !g_round_started)
        return 
PLUGIN_CONTINUE

    flash_user_with_team_color
(id)
    return 
PLUGIN_CONTINUE
}

// reset round started
public reset_round_started()
{
    
g_round_started false
    
return PLUGIN_CONTINUE
}



// ============
// = FLASHING =
// ============
flash_user_with_team_color(id)
{
    
// plugin disabled
    
if (!get_pcvar_num(g_enabled))
        return 
PLUGIN_CONTINUE

    
// user dead
    
if (!is_user_alive(id))
        return 
PLUGIN_CONTINUE

    
// set color
    
new color[3]
    switch(
cs_get_user_team(id))
    {
        case 
CS_TEAM_T:
        {
            
color[0] = T_LAYER_COLOR_R
            color
[1] = T_LAYER_COLOR_G
            color
[2] = T_LAYER_COLOR_B
        
}

        case 
CS_TEAM_CT:
        {
            
color[0] = CT_LAYER_COLOR_R
            color
[1] = CT_LAYER_COLOR_G
            color
[2] = CT_LAYER_COLOR_B
        
}

        default: 
// Spec or unassigned
            
return PLUGIN_CONTINUE
    
}

    
// actual fade
    
UTIL_ScreenFade(idcolorget_pcvar_float(g_flash_duration), get_pcvar_float(g_flash_stay), get_pcvar_num(g_flash_alpha), FFADE_INtrue)
    return 
PLUGIN_CONTINUE


My next plugin will be a C4 respawner... I block round ends but allow bombs (gungame) and after detonation or defuse one random player (prefer CTs & Bots) gets c4 (and maybe drops it).

2called-chaos 11-10-2013 03:38

Re: First plugin, need feedback
 
I didn't really understand the returns to be honest. I've read this post and concluded to always return PLUGIN_CONTINUE except in plugin_init and if I want to prevent a chat command from being displayed in chat.

2called-chaos 11-10-2013 04:10

Re: First plugin, need feedback
 
Thanks, I think I understand it! So basically no function has to return PLUGIN_HANDLED if I would nest or combine the if conditions?

PHP Code:

public event_player_spawned(id)
{
    if (
get_pcvar_num(g_enabled) && ( get_pcvar_num(g_flash_on_spawn) || g_round_started ))
      
flash_user_with_team_color(id)



ConnorMcLeod 11-10-2013 05:09

Re: First plugin, need feedback
 
Returns values are not related to conditions.

Some functions can be blocked, then you use returns values depending if you want to block or not.
Some callbacks are sent after something has been executed, and you can't block anything, so returns values have no effects in it.

2called-chaos 11-10-2013 05:58

Re: First plugin, need feedback
 
Yeah sure they are not related. I couldn't find the information what pawn return by default if you have no explicit return in your function. To illustrate my thoughts (assuming no return would be NULL and the constants would represent strings rather than integers):

PHP Code:

# the caller (amx)
result callback()
if (
result  ==  "success")
    
continue_event
else if (result == "handled")
    
block_event
else i_dont_know 

I guess an empty function is the same as return 0 so I only HAVE TO return something if I want to stop something, am I right? Or is it the case that depending on what you handle you HAVE TO return either one or nothing?

Thank you very much so far!

ConnorMcLeod 11-10-2013 06:19

Re: First plugin, need feedback
 
Some forwards can be blocked (client_command, ...) some can not be blocked (plugin_init, client_connect ...)
Event (callbacks from register_event) can not be blocked
Commands (register_clcmd, register_concmd) can be blocked
Menus (register_menu) can be blocked

Things that can't be blocked don't need returns values, if you need to exit functions, just use 'return' with no value.
In things that can be blocked, you should use return values, though when you don't return any value, PLUGIN_CONTINUE is assumed.

On specific modules (hamsandwich and fakemate) functions hooks, you need to use specific modules return values (HAM_IGNORED, HAM_HANDLED, HAM_OVERRIDE, HAM_SUPERCEDE, FMRED_IGNORED, FMRED_HANDLED, FMRED_OVERRIDE, FMRED_SUPERCEDE)

2called-chaos 11-10-2013 12:57

Re: First plugin, need feedback
 
Thank you for the clarification. I think these changes should do it then. Not sure if I should return HAM_HANDLED in the event_player_spawned function but default would be HAM_IGNORED anyway.

https://gist.github.com/2called-chao...d7e9/revisions

Thanks for the help!

akcaliberg 11-11-2013 12:21

Re: First plugin, need feedback
 
FMRES_*


All times are GMT -4. The time now is 23:17.

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