PHP Code:
new iTimerHUDSync
new Float:fPlayerTimer[33]
new iTimerSetCount[33]
new iPlayerCP_Count[33]
public fwButtonUse(button, player, type, Float:value)
{
if( !get_pcvar_num(cvTimerOn) )
return HAM_IGNORED
new szTarget[32]
pev(button, pev_target, szTarget, 31)
// Check if this button is the timer's end/start. Look for the words "counter","clock" and "timer"
// If they are found,check if it's the start or the end timer
if( containi(szTarget, "counter") != -1 || containi(szTarget, "clock") != -1 || containi(szTarget, "timer") != -1 )
{
if( containi(szTarget, "start") != -1 || containi(szTarget, "on") != -1 )
TimerStart(player)
else if( containi(szTarget, "stop") != -1 || containi(szTarget, "off") != -1 )
TimerEnd(player)
}
else
{
new szTargetName[32]
pev(button, pev_targetname, szTargetName, 31)
if( containi(szTargetName, "counter") != -1 || containi(szTargetName, "clock") != -1 || containi(szTargetName, "timer") != -1 )
{
if( containi(szTargetName, "start") != -1 || containi(szTargetName, "on") != -1 )
TimerStart(player)
else if( containi(szTargetName, "stop") != -1 || containi(szTargetName, "off") != -1 )
TimerEnd(player)
}
}
return HAM_IGNORED
}
public fwTimerThink(ent)
{
if( get_pcvar_num(cvTimerOn) )
{
new Float:fNow = get_gametime()
static Float:fTime
new iHours, iMinutes, iSeconds
for (new id = 1; id < iMaxPlayers; id++)
{
if( !fPlayerTimer[id] || !is_user_alive(id))
continue
// Subtracting the current time with the one stored we get the elapsed time
fTime = fNow - fPlayerTimer[id]
iSeconds = floatround(fTime, floatround_floor)
iHours = iSeconds / 3600
iMinutes = (iSeconds / 60) - (iHours * 60)
iSeconds = iSeconds % 60
if( iSeconds == 60)
{
iSeconds = 0
iMinutes++
}
if( iMinutes == 60)
{
iMinutes = 0
iHours++
}
set_hudmessage(0, 100, 200, TIMER_X, TIMER_Y)
ShowSyncHudMsg(id, iTimerHUDSync, "%d:%02d:%02d:%02d", iHours, iMinutes, iSeconds, floatround(floatfract(fTime) * 100, floatround_floor) )
}
set_pev(ent, pev_nextthink, fNow + TIMER_INTERVAL)
}
}
TimerInit()
{
// Timer already initialized,return
if( fm_find_ent(iMaxPlayers, "env_kz_timer") )
return false
// Initialize HudSyncObj
iTimerHUDSync = CreateHudSyncObj()
// Create entity
new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target") )
if( !pev_valid(ent) )
return false
// Set appropriate classname
set_pev(ent, pev_classname, "env_kz_timer")
// Manually call the timer function,it will set up the next think
fwTimerThink(ent)
// Register it's think function
RegisterHamFromEntity(Ham_Think, ent, "fwTimerThink")
// Spawn the entity
dllfunc(DLLFunc_Spawn, ent)
return true
}
TimerStart(id)
{
if(!get_pcvar_num(cvTimerOn) || !is_user_alive(id))
return
new iLimit = get_pcvar_num(cvTimerLimit)
// Player has no timer, or he's within the allowed reset limit
if( !fPlayerTimer[id] || (!iLimit || iTimerSetCount[id] < iLimit) )
{
// Store the timer's start time
fPlayerTimer[id] = get_gametime()
// First one,initialize everything
if( !iTimerHUDSync)
TimerInit()
// Increase the timer set count
iTimerSetCount[id]++
}
else
{
client_print(id, print_chat, "[KZ] Timer failed - you reached the limit of %d starts", iLimit)
}
}
TimerEnd(id)
{
if(!fPlayerTimer[id] || !is_user_alive(id))
return
// Get current time
new Float:fTime = get_gametime()
fTime -= fPlayerTimer[id]
// Format the time to a user-friendly display
new iSeconds = floatround(fTime, floatround_floor)
new iHours = iSeconds / 3600
new iMinutes = (iSeconds / 60) - (iHours * 60)
iSeconds = iSeconds % 60
if( iSeconds == 60)
{
iSeconds = 0
iMinutes++
}
if( iMinutes == 60)
{
iMinutes = 0
iHours++
}
// Get user name
new szUserName[32]
get_user_name(id, szUserName, 31)
client_print(0, print_chat, "[KZ] %s finished the map in %d:%02d:%02d:%02d, with %d checkpoints. Congratulations!", szUserName, iHours, iMinutes, iSeconds, floatround(floatfract(fTime) * 100), iPlayerCP_Count[id])
if(iPlayerCP_Count[id] <= get_pcvar_num(cvHookCPs))
{
client_print(id,print_chat,"[KZ] You were granted with the hook! Bind a key to +hook to use it!")
iPlayerHook_On[id] = true
}
// Reset user vars
fPlayerTimer[id] = 0.0
iTimerSetCount[id] = 0
ClearSyncHud(id, iTimerHUDSync)
CheckPointReset(id)
}
// ---------------------------------------------------------- //