// File: /workspace/github_c_makefile_star2_O3/schroeder-lvb[P]sissm/src/piantirush.c
// Function: piantirushPeriodicCB
int piantirushPeriodicCB( char *strIn )
{
    static int lastFastState = -1;
    int currFastState;
    int playersAlive;
    static int lastLivePlayerCount = -1;
    int currLivePlayerCount;
    long noProtectSecsL;

    currFastState = (int) p2pGetF( "piantirush.p2p.fast", 0.0 ) ;
 
    // if last state is "slow" (default) and admin wishes to run "fast"
    // (antirush-disable), then immediately disable the slow rate via
    // the RCON
    // 
    if ( (lastFastState == 0) && (currFastState != 0) ) {
        _captureSpeedForceNext();
        _captureSpeed( 0 );
    }
    lastFastState = currFastState;

    // if we are running "fast" mode then turn on lcoal mute so that this plugin 
    // stays silent.
    //
    if ( currFastState ) { _localMute = 1;  _fastMode = 1; }
    else                 { _localMute = 0;  _fastMode = 0; }

    // if enabled process territorial antirush check.
    // This periodic processing increments the offender 'time' as well
    // as handle kicking upon allowed time expiry
    //
    if ( (piantirushConfig.autoKickEarlyBreach) ) {
        _capRusherPeriodicCheck();
    }

    // Process exemption of auto-kick on LMS conditions
    //
    playersAlive = apiBPPlayerCount();

    if ( playersAlive > _peakAlivePlayer ) {           // keep track of peak# of players
        _peakAlivePlayer = playersAlive;
    }
 
    if ( kickArmed && (_peakAlivePlayer > piantirushConfig.autoKickExemptLMS) ) {              
        if ( playersAlive <= piantirushConfig.autoKickExemptLMS )  {
            _localSay( piantirushConfig.autoKickExemptMsg );

            if ( NULL != strstr( rosterGetObjectiveType(), "WeaponCache" ))  {
                alarmCancel( rPtr );  // cancel any pending 'aa' request
                _localSay( piantirushConfig.destroyOkPrompt );
                kickArmed = 0;                    // kick safety : disable kick
                alarmCancel( aPtr );
                _captureSpeed( 0 );
           }

           else if ( NULL != strstr( rosterGetObjectiveType(), "Captur" ))  {
               alarmCancel( bPtr );   // cancel any pending 'aa' request  
               _localSay( piantirushConfig.breachOkPrompt );
                kickArmed = 0;                    // kick safety : disable kick
               alarmReset( aPtr, 2L );
               _capRusherClear();

           }
        }
    }
  
    // Useful information to print to the screen or log file - to determine
    // number of live players.  Only log changes to cut down on clutter.
    //
    currLivePlayerCount = apiBPPlayerCount();
    if ( lastLivePlayerCount  != currLivePlayerCount ) {
        logPrintf( LOG_LEVEL_INFO, "piantirush", "Change in live player count: %d -> %d", 
            lastLivePlayerCount, currLivePlayerCount );
        lastLivePlayerCount = currLivePlayerCount;
    }

    // State machine to handle kickArmed2 interlock that is compatible
    // with wave-respawn option (where number of players alive may go 'up' 
    // at any time.  Kickarmed2 is initialized to 2 at start of the objective. It is
    // primed when live players exceeds threshold.  After prime it is allowed to 
    // disarm when LMS condition occurs, and it is latched at this state until start 
    // of next objective.
    // 
    switch ( kickArmed2 ) {
    case 2:   // armed because objective has just begun
        if ( currLivePlayerCount > piantirushConfig.autoKickExemptLMS ) 
            kickArmed2 = 1;
        break;
    case 1:   // armed because minimum alive count exceeded the exemption threshold
        if ( currLivePlayerCount <= piantirushConfig.autoKickExemptLMS ) 
            kickArmed2 = 0;
        break;
    default:  // =0 disarmed because we are below minimum exemption threshold
        ;;    // do nothing, wait for synthetic objective event to set it back to "2"
        break;
    }

#if 0
    // Exception processing - regardless of the condition if min player exemption is set,
    // do not kick anyone as long as # player falls below the threshold.
    //
    if ( 0 != piantirushConfig.autoKickExemptLMS  ) {              
        kickArmed2 = 1;
        if ( currLivePlayerCount <= piantirushConfig.autoKickExemptLMS ) kickArmed2 = 0;
    }
    else 
        kickArmed2 = 1;
#endif

    // Service the "no kick" protection window countdown in seconds.  If it reaches 0L
    // then nokick protection becomes disabled.
    //
    noProtectSecsL =  p2pGetL( "piantirush.p2p.nokickprotect", 0L ) ;
    if ( noProtectSecsL != 0L ) {
         if ( --noProtectSecsL < 0L ) { 
             noProtectSecsL = 0L;
             logPrintf( LOG_LEVEL_INFO, "piantirush", "Nokick Protection window is now deactivated." );
         }
         else {
             logPrintf( LOG_LEVEL_INFO, "piantirush", "Nokick Protect window is active: %ld", noProtectSecsL );
         }
         p2pSetL( "piantirush.p2p.nokickprotect", noProtectSecsL );
    }

    // Service the "no wait" speedup feature (admins/moderators) 
    //
    if ( 0L != p2pGetL( "piantirush.p2p.SigNoWait", 0L )) {
        p2pSetL( "piantirush.p2p.SigNoWait", 0L );  // clear the semaphore 
        if ( alarmStatus( aPtr ) > 2L ) {
            _capRusherClear();
            alarmReset( aPtr, 2L );
        }
    }

    // If the server goes empty, make sure Training Mode and AutoKick Exemption is cleared.
    //
    if ( 0 == apiPlayersGetCount() ) {
        p2pSetL( "piantirush.p2p.training", 0L );
        p2pSetL( "piantirush.p2p.nokickprotect", 0L );
        p2pSetL( "piantirush.p2p.SigNoWait", 0L );
    }

    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
leaq 0x55e3(%rip), %rdi      # 0x1b965
xorpd %xmm0, %xmm0
callq 0x18162
cvttsd2si %xmm0, %ebx
cmpl $0x0, 0x13b3e(%rip)     # 0x29ed4
sete %al
testl %ebx, %ebx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x163b6
movl $0xffffffff, 0x13b35(%rip) # imm = 0xFFFFFFFF
xorl %edi, %edi
callq 0x15ce2
testl %ebx, %ebx
setne 0x209aa9(%rip)          # 0x21fe68
setne 0x209aa6(%rip)          # 0x21fe6c
movl %ebx, 0x13b08(%rip)     # 0x29ed4
cmpl $0x0, 0x2054f5(%rip)    # 0x21b8c8
je 0x1655b
cmpl $0x0, 0x13b00(%rip)     # 0x29ee0
je 0x1655b
testb $0x1, 0x209a53(%rip)    # 0x21fe40
je 0x1655b
movl 0x205cd7(%rip), %eax    # 0x21c0d0
movl 0x205cd5(%rip), %ecx    # 0x21c0d4
xorl %r15d, %r15d
leaq 0x209a87(%rip), %r12    # 0x21fe90
cmpb $0x0, (%r15,%r12)
je 0x1644d
cmpl $0x0, 0x88(%r15,%r12)
je 0x16432
movl 0x84(%r15,%r12), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x84(%r15,%r12)
cmpl %eax, %edx
jg 0x16462
testl %ecx, %ecx
je 0x1644d
movl 0x80(%r15,%r12), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x80(%r15,%r12)
cmpl %ecx, %edx
jg 0x164c9
addq $0x8c, %r15
cmpq $0x2300, %r15           # imm = 0x2300
jne 0x16409
jmp 0x1655b
leaq (%r15,%r12), %rbx
addq $0x40, %rbx
movq %rbx, %rdi
callq 0x14e14
testq %rax, %rax
je 0x1655b
movq %rsp, %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %rax, %rsi
callq 0xf384
leaq 0x206443(%rip), %rdx    # 0x21c8d8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x16a27
testl %eax, %eax
jne 0x16538
leaq 0x92bd(%rip), %rdi      # 0x1f76c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x16799
leaq 0x8612(%rip), %rsi      # 0x1ead2
leaq 0x92c7(%rip), %rdx      # 0x1f78e
jmp 0x16526
leaq (%r15,%r12), %rbx
addq $0x40, %rbx
movq %rbx, %rdi
callq 0x14e14
testq %rax, %rax
je 0x1655b
movq %rsp, %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %rax, %rsi
callq 0xf384
leaq 0x205fe0(%rip), %rdx    # 0x21c4d8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x16a27
testl %eax, %eax
jne 0x16538
leaq 0x92a5(%rip), %rdi      # 0x1f7b3
movq %rbx, %rsi
xorl %eax, %eax
callq 0x16799
leaq 0x85b3(%rip), %rsi      # 0x1ead2
leaq 0x92bd(%rip), %rdx      # 0x1f7e3
movq %rsp, %r8
movl $0x1, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1a3e3
xorl %eax, %eax
movb %al, (%r15,%r12)
movb %al, 0x40(%r15,%r12)
movl $0x0, 0x88(%r15,%r12)
movq $0x0, 0x80(%r15,%r12)
callq 0x11a3c
movl 0x13976(%rip), %ecx     # 0x29edc
cmpl %ecx, %eax
jle 0x16572
movl %eax, 0x1396c(%rip)     # 0x29edc
movl %eax, %ecx
cmpb $0x1, 0x2098c7(%rip)    # 0x21fe40
jne 0x16644
movl 0x206f53(%rip), %edx    # 0x21d4d8
cmpl %edx, %ecx
setle %cl
cmpl %edx, %eax
setg %al
orb %cl, %al
jne 0x16644
leaq 0x206f3e(%rip), %rdi    # 0x21d4dc
xorl %eax, %eax
callq 0x16799
callq 0x14563
leaq 0x857a(%rip), %rsi      # 0x1eb2b
movq %rax, %rdi
callq 0x83b0
testq %rax, %rax
je 0x165f4
movq 0x2098ab(%rip), %rdi    # 0x21fe70
callq 0xfcb9
leaq 0x203eef(%rip), %rdi    # 0x21a4c0
xorl %eax, %eax
callq 0x16799
movb $0x0, 0x209861(%rip)    # 0x21fe40
movq 0x20933a(%rip), %rdi    # 0x21f920
callq 0xfcb9
xorl %edi, %edi
callq 0x15ce2
jmp 0x16644
callq 0x14563
leaq 0x8537(%rip), %rsi      # 0x1eb37
movq %rax, %rdi
callq 0x83b0
testq %rax, %rax
je 0x16644
movq 0x209864(%rip), %rdi    # 0x21fe78
callq 0xfcb9
leaq 0x2052ac(%rip), %rdi    # 0x21b8cc
xorl %eax, %eax
callq 0x16799
movb $0x0, 0x209812(%rip)    # 0x21fe40
movq 0x2092eb(%rip), %rdi    # 0x21f920
movl $0x2, %esi
callq 0xfc98
callq 0x16115
callq 0x11a3c
movl %eax, %ebx
movl 0x13887(%rip), %ecx     # 0x29ed8
cmpl %eax, %ecx
je 0x16678
leaq 0x8476(%rip), %rsi      # 0x1ead2
leaq 0x84db(%rip), %rdx      # 0x1eb3e
movl $0x2, %edi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1a3e3
movl %ebx, 0x13860(%rip)     # 0x29ed8
movl 0x13862(%rip), %eax     # 0x29ee0
cmpl $0x1, %eax
je 0x166d5
cmpl $0x2, %eax
jne 0x1669b
movl $0x1, %eax
cmpl 0x206e45(%rip), %ebx    # 0x21d4d8
jle 0x1669b
movl %eax, 0x13845(%rip)     # 0x29ee0
leaq 0x58ca(%rip), %rdi      # 0x1bf6c
xorl %esi, %esi
callq 0x181e1
testq %rax, %rax
je 0x1670c
jle 0x166e1
movq %rax, %rbx
decq %rbx
leaq 0x8415(%rip), %rsi      # 0x1ead2
leaq 0x84cd(%rip), %rdx      # 0x1eb91
movl $0x2, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1a3e3
jmp 0x166fd
xorl %eax, %eax
cmpl 0x206dfb(%rip), %ebx    # 0x21d4d8
jle 0x16695
jmp 0x1669b
leaq 0x83ea(%rip), %rsi      # 0x1ead2
leaq 0x8475(%rip), %rdx      # 0x1eb64
xorl %ebx, %ebx
movl $0x2, %edi
xorl %eax, %eax
callq 0x1a3e3
leaq 0x5868(%rip), %rdi      # 0x1bf6c
movq %rbx, %rsi
callq 0x180a5
leaq 0x5876(%rip), %rdi      # 0x1bf89
xorl %esi, %esi
callq 0x181e1
testq %rax, %rax
je 0x16755
leaq 0x5863(%rip), %rdi      # 0x1bf89
xorl %esi, %esi
callq 0x180a5
movq 0x2091ec(%rip), %rdi    # 0x21f920
callq 0xfcc3
cmpq $0x3, %rax
jl 0x16755
callq 0x16115
movq 0x2091d5(%rip), %rdi    # 0x21f920
movl $0x2, %esi
callq 0xfc98
callq 0x13853
testl %eax, %eax
jne 0x16788
leaq 0x51a5(%rip), %rdi      # 0x1b90a
xorl %esi, %esi
callq 0x180a5
leaq 0x57f9(%rip), %rdi      # 0x1bf6c
xorl %esi, %esi
callq 0x180a5
leaq 0x5808(%rip), %rdi      # 0x1bf89
xorl %esi, %esi
callq 0x180a5
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r14
popq %r15
retq