Raised This Month: $ Target: $400
 0% 

Most accurate way to make a stopwatch/timer?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-21-2008 , 16:33   Re: Most accurate way to make a stopwatch/timer?
Reply With Quote #8

Part of the code from the KZ Plugin i was working on:
PHP Code:
new iTimerHUDSync
new Float:fPlayerTimer[33]
new 
iTimerSetCount[33]

new 
iPlayerCP_Count[33]

public 
fwButtonUse(buttonplayertypeFloat:value)
{
    if( !
get_pcvar_num(cvTimerOn) )
        return 
HAM_IGNORED

    
new szTarget[32]
    
pev(buttonpev_targetszTarget31)

    
// 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") != -|| containi(szTarget"clock") != -|| containi(szTarget"timer") != -)
    {
        if( 
containi(szTarget"start") != -|| containi(szTarget"on") != -)
            
TimerStart(player)
        else if( 
containi(szTarget"stop") != -|| containi(szTarget"off") != -)
            
TimerEnd(player)
    }
    else
    {
        new 
szTargetName[32]
        
pev(buttonpev_targetnameszTargetName31)

        if( 
containi(szTargetName"counter") != -|| containi(szTargetName"clock") != -|| containi(szTargetName"timer") != -)
        {
            if( 
containi(szTargetName"start") != -|| containi(szTargetName"on") != -)
                
TimerStart(player)
            else if( 
containi(szTargetName"stop") != -|| containi(szTargetName"off") != -)
                
TimerEnd(player)
        }
    }

    return 
HAM_IGNORED
}


public 
fwTimerThink(ent)
{
    if( 
get_pcvar_num(cvTimerOn) )
    {
        new 
Float:fNow get_gametime()
        static 
Float:fTime
        
        
new iHoursiMinutesiSeconds

        
for (new id 1id iMaxPlayersid++)
        {
            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(fTimefloatround_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(0100200TIMER_XTIMER_Y)

            
ShowSyncHudMsg(idiTimerHUDSync"%d:%02d:%02d:%02d"iHoursiMinutesiSecondsfloatround(floatfract(fTime) * 100floatround_floor) )
        }

        
set_pev(entpev_nextthinkfNow 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_CreateNamedEntityengfunc(EngFunc_AllocString"info_target") )
    if( !
pev_valid(ent) )
        return 
false

    
// Set appropriate classname
    
set_pev(entpev_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_Thinkent"fwTimerThink")

    
// Spawn the entity
    
dllfunc(DLLFunc_Spawnent)

    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(idprint_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(fTimefloatround_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(idszUserName31)

    
client_print(0print_chat"[KZ] %s finished the map in %d:%02d:%02d:%02d, with %d checkpoints. Congratulations!"szUserNameiHoursiMinutesiSecondsfloatround(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
(idiTimerHUDSync)

    
CheckPointReset(id)
}
// ---------------------------------------------------------- // 
danielkza is offline
 



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:12.


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