// File: /workspace/github_c_makefile_star2_O2/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 0x55cb(%rip), %rdi      # 0x1a965
xorpd %xmm0, %xmm0
callq 0x17102
cvttsd2si %xmm0, %ebx
cmpl $0x0, 0x14b26(%rip)     # 0x29ed4
sete %al
testl %ebx, %ebx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x153cb
orl $-0x1, 0x14b20(%rip)    # 0x29ee4
xorl %edi, %edi
callq 0x14d3e
testl %ebx, %ebx
setne 0x20aa94(%rip)          # 0x21fe68
setne 0x20aa91(%rip)          # 0x21fe6c
movl %ebx, 0x14af3(%rip)     # 0x29ed4
cmpl $0x0, 0x2064e0(%rip)    # 0x21b8c8
je 0x15569
cmpl $0x0, 0x14aeb(%rip)     # 0x29ee0
je 0x15569
testb $0x1, 0x20aa3e(%rip)    # 0x21fe40
je 0x15569
movl 0x206cc2(%rip), %eax    # 0x21c0d0
movl 0x206cc0(%rip), %ecx    # 0x21c0d4
xorl %r15d, %r15d
leaq 0x20aa72(%rip), %r12    # 0x21fe90
cmpq $0x2300, %r15           # imm = 0x2300
je 0x15569
cmpb $0x0, (%r15,%r12)
je 0x1546f
cmpl $0x0, 0x88(%r15,%r12)
je 0x15454
movl 0x84(%r15,%r12), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x84(%r15,%r12)
cmpl %eax, %edx
jg 0x15478
testl %ecx, %ecx
je 0x1546f
movl 0x80(%r15,%r12), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x80(%r15,%r12)
cmpl %ecx, %edx
jg 0x154df
addq $0x8c, %r15
jmp 0x1541e
leaq (%r15,%r12), %rbx
addq $0x40, %rbx
movq %rbx, %rdi
callq 0x13eb3
testq %rax, %rax
je 0x15569
movq %rsp, %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %rax, %rsi
callq 0xe97a
leaq 0x20742d(%rip), %rdx    # 0x21c8d8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15a1b
testl %eax, %eax
jne 0x1554c
leaq 0x928f(%rip), %rdi      # 0x1e754
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1579b
leaq 0x85e4(%rip), %rsi      # 0x1daba
leaq 0x9299(%rip), %rdx      # 0x1e776
jmp 0x1553c
leaq (%r15,%r12), %rbx
addq $0x40, %rbx
movq %rbx, %rdi
callq 0x13eb3
testq %rax, %rax
je 0x15569
movq %rsp, %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %rax, %rsi
callq 0xe97a
leaq 0x206fca(%rip), %rdx    # 0x21c4d8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x15a1b
testl %eax, %eax
jne 0x1554c
leaq 0x9277(%rip), %rdi      # 0x1e79b
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1579b
leaq 0x8585(%rip), %rsi      # 0x1daba
leaq 0x928f(%rip), %rdx      # 0x1e7cb
pushq $0x1
popq %rdi
movq %rsp, %r8
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1920b
xorl %eax, %eax
movb %al, (%r15,%r12)
movb %al, 0x40(%r15,%r12)
andl $0x0, 0x88(%r15,%r12)
andq $0x0, 0x80(%r15,%r12)
callq 0x10dd5
movl 0x14968(%rip), %ecx     # 0x29edc
cmpl %ecx, %eax
jle 0x15580
movl %eax, 0x1495e(%rip)     # 0x29edc
movl %eax, %ecx
cmpb $0x1, 0x20a8b9(%rip)    # 0x21fe40
jne 0x15650
movl 0x207f45(%rip), %edx    # 0x21d4d8
cmpl %edx, %ecx
setle %cl
cmpl %edx, %eax
setg %al
orb %cl, %al
jne 0x15650
leaq 0x207f30(%rip), %rdi    # 0x21d4dc
xorl %eax, %eax
callq 0x1579b
callq 0x13648
leaq 0x8554(%rip), %rsi      # 0x1db13
movq %rax, %rdi
callq 0x83c0
testq %rax, %rax
je 0x15602
movq 0x20a89d(%rip), %rdi    # 0x21fe70
callq 0xf1d2
leaq 0x204ee1(%rip), %rdi    # 0x21a4c0
xorl %eax, %eax
callq 0x1579b
movb $0x0, 0x20a853(%rip)    # 0x21fe40
movq 0x20a32c(%rip), %rdi    # 0x21f920
callq 0xf1d2
xorl %edi, %edi
callq 0x14d3e
jmp 0x15650
callq 0x13648
leaq 0x8511(%rip), %rsi      # 0x1db1f
movq %rax, %rdi
callq 0x83c0
testq %rax, %rax
je 0x15650
movq 0x20a856(%rip), %rdi    # 0x21fe78
callq 0xf1d2
leaq 0x20629e(%rip), %rdi    # 0x21b8cc
xorl %eax, %eax
callq 0x1579b
movb $0x0, 0x20a804(%rip)    # 0x21fe40
movq 0x20a2dd(%rip), %rdi    # 0x21f920
pushq $0x2
popq %rsi
callq 0xf1b1
callq 0x15143
callq 0x10dd5
movl %eax, %ebx
movl 0x1487b(%rip), %ecx     # 0x29ed8
cmpl %eax, %ecx
je 0x15682
leaq 0x8452(%rip), %rsi      # 0x1daba
leaq 0x84b7(%rip), %rdx      # 0x1db26
pushq $0x2
popq %rdi
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1920b
movl %ebx, 0x14856(%rip)     # 0x29ed8
movl 0x14858(%rip), %eax     # 0x29ee0
cmpl $0x1, %eax
je 0x1569f
cmpl $0x2, %eax
jne 0x156af
cmpl 0x207e40(%rip), %ebx    # 0x21d4d8
jle 0x156af
pushq $0x1
popq %rax
jmp 0x156a9
xorl %eax, %eax
cmpl 0x207e31(%rip), %ebx    # 0x21d4d8
jg 0x156af
movl %eax, 0x14831(%rip)     # 0x29ee0
leaq 0x58b6(%rip), %rdi      # 0x1af6c
xorl %esi, %esi
callq 0x17172
testq %rax, %rax
je 0x15710
jle 0x156e7
movq %rax, %rbx
decq %rbx
leaq 0x83e9(%rip), %rsi      # 0x1daba
leaq 0x84a1(%rip), %rdx      # 0x1db79
pushq $0x2
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1920b
jmp 0x15701
leaq 0x83cc(%rip), %rsi      # 0x1daba
leaq 0x8457(%rip), %rdx      # 0x1db4c
xorl %ebx, %ebx
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x1920b
leaq 0x5864(%rip), %rdi      # 0x1af6c
movq %rbx, %rsi
callq 0x17049
leaq 0x5872(%rip), %rdi      # 0x1af89
xorl %esi, %esi
callq 0x17172
testq %rax, %rax
je 0x15757
leaq 0x585f(%rip), %rdi      # 0x1af89
xorl %esi, %esi
callq 0x17049
movq 0x20a1e8(%rip), %rdi    # 0x21f920
callq 0xf1d9
cmpq $0x3, %rax
jl 0x15757
callq 0x15143
movq 0x20a1d1(%rip), %rdi    # 0x21f920
pushq $0x2
popq %rsi
callq 0xf1b1
callq 0x12ab4
testl %eax, %eax
jne 0x1578a
leaq 0x51a3(%rip), %rdi      # 0x1a90a
xorl %esi, %esi
callq 0x17049
leaq 0x57f7(%rip), %rdi      # 0x1af6c
xorl %esi, %esi
callq 0x17049
leaq 0x5806(%rip), %rdi      # 0x1af89
xorl %esi, %esi
callq 0x17049
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r14
popq %r15
retq