|
Veteran Member
Join Date: Jul 2006
Location: France (95)
|

09-08-2008
, 15:41
Re: Detecting a button press (not a key press)
|
#10
|
Code:
#if defined _kz_buttons_included
#endinput
#endif
#define _kz_buttons_included
#pragma reqlib kz_buttons
#if !defined AMXMODX_NOAUTOLOAD
#pragma loadlib kz_buttons
#endif
#define START 0
#define STOP 1
#define BUTTONS 2
forward client_use_button(index, button)
PHP Code:
/* Copyright © 2008, ConnorMcLeod
Kz Buttons is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Kz Buttons; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <amxmodx> #include <fakemeta> #include <hamsandwich>
#define PLUGIN "Kz Buttons" #define AUTHOR "ConnorMcLeod" #define VERSION "0.0.1"
#define MAX_PLAYERS 32
#define START 0 #define STOP 1 #define BUTTONS 2
#define MIN_DELAY 1.0
new g_iButtons[BUTTONS] new Float:g_flLastUse[MAX_PLAYERS+1][BUTTONS]
new g_iSpawnForwardId new g_iMaxPlayers
new g_iButtonForwardId new g_iReturn
public plugin_precache() { register_plugin( PLUGIN, VERSION, AUTHOR ) g_iSpawnForwardId = register_forward(FM_Spawn, "Forward_Spawn") }
public Forward_Spawn(iEnt) { if( !pev_valid(iEnt) ) return
new counter_start[][] = {"counter_start", "clockstartbutton", "firsttimerelay", "multi_start"} new counter_stop[][] = {"counter_off", "clockstop", "clockstopbutton", "multi_stop"}
new szTarget[17] pev(iEnt, pev_target, szTarget, 16)
for(new i; i<4; i++) { if( equal(szTarget, counter_start[i]) ) { g_iButtons[START] = iEnt } else if( equal(szTarget, counter_stop[i]) ) { g_iButtons[STOP] = iEnt } } }
public plugin_init() { unregister_forward(FM_Spawn, g_iSpawnForwardId) if(!g_iButtons[START] || !g_iButtons[STOP]) { pause("ad") return }
RegisterHam(Ham_Use, "func_button", "Ham_Use_button") g_iButtonForwardId = CreateMultiForward("client_use_button", ET_IGNORE, FP_CELL, FP_CELL) g_iMaxPlayers = get_maxplayers() }
public plugin_natives() { register_library("kz_buttons") }
public client_putinserver(id) { g_flLastUse[id][START] = 0.0 g_flLastUse[id][STOP] = 0.0 }
public Ham_Use_button(iEnt, id, idactivator, use_type, Float:flValue) { if( !(1 <= id <= g_iMaxPlayers) || use_type != 2 || flValue != 1.0 ) { return HAM_IGNORED }
static iButton
if( iEnt == g_iButtons[START] ) { iButton = START } else if( iEnt == g_iButtons[STOP] ) { iButton = STOP } else { return HAM_IGNORED }
static Float:flTime global_get(glb_time, flTime)
if( g_flLastUse[id][iButton] + MIN_DELAY > flTime ) { return HAM_IGNORED }
g_flLastUse[id][iButton] = flTime
ExecuteForward(g_iButtonForwardId, g_iReturn, id, iButton)
return HAM_IGNORED }
Then, you can make a plugin like this :
PHP Code:
#include <amxmodx>
#include "kz_buttons.inc"
#define PLUGIN "Kz Buttons Test" #define AUTHOR "ConnorMcLeod" #define VERSION "0.0.1"
public plugin_init() { register_plugin( PLUGIN, VERSION, AUTHOR ) }
public client_use_button(id, iButton) { client_print(id, print_chat, "You have just pressed the %s button", iButton ? "stop" : "start") }
or
PHP Code:
#include <amxmodx> #include <fakemeta>
#include "kz_buttons.inc"
#define PLUGIN "Kz Buttons Test" #define AUTHOR "ConnorMcLeod" #define VERSION "0.0.1"
#define MAX_PLAYERS 32
new g_iMaxPlayers
new Float:g_flStartTime[MAX_PLAYERS+1]
public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) register_forward(FM_EmitSound, "EmitSound") g_iMaxPlayers = get_maxplayers() }
public client_putinserver(id) { g_flStartTime[id] = 0.0 }
public client_use_button(id, iButton) { if( iButton == START ) { g_flStartTime[id] = get_gametime() client_print(id, print_chat, "Timer started !!!") } else if( g_flStartTime[id] ) { new szName[32] get_user_name(id, szName, 31)
new iTime = floatround(get_gametime() - g_flStartTime[id]) if( iTime > 3600 ) { new iTime2 = iTime%3600 client_print(id, print_center, "%d:%02d:%02d", iTime/3600, iTime2/60, iTime2%60) } else { client_print(id, print_center, "%d:%02d", iTime/60, iTime%60) } g_flStartTime[id] = 0.0 } }
public EmitSound(id, osef, szSound[]) { if( !(1 <= id <= g_iMaxPlayers) ) return FMRES_IGNORED
//common/wpn_denyselect.wav if( szSound[0] != 'c' || szSound[7] != 'w' || szSound[11] != 'd') return FMRES_IGNORED
if(g_flStartTime[id]) { new iTime = floatround(get_gametime() - g_flStartTime[id]) if( iTime > 3600 ) { static iTime2 iTime2 = iTime%3600 client_print(id, print_center, "%d:%02d:%02d", iTime/3600, iTime2/60, iTime2%60) } else { client_print(id, print_center, "%d:%02d", iTime/60, iTime%60) }
return FMRES_HANDLED } return FMRES_IGNORED }
__________________
Last edited by ConnorMcLeod; 09-08-2008 at 15:48.
|
|