// File: /workspace/github_c_makefile_star2_O2/QwazyWabbitWOS[P]lmctf60/g_tourney.c
// Function: Tourney_Think
void Tourney_Think(edict_t *ent)
{
    int     minutes;
    edict_t *player;
    int     i;
    char    message[MAX_INFO_STRING];

    ent->nextthink = level.time + 1;

    // If game paused, don't keep counting down
    if (GamePaused()) {
        return;
    }

    minutes = ent->count/60;

    if (matchstate == MATCH_OVER) {
        Time_Left = 0;
    } else if (ent->count < 60) {
        Time_Left = ent->count;
    } else {
        Time_Left = minutes + 1;
    }


    if (matchstate == MATCH_COUNTDOWN || matchstate == MATCH_RAILGUN_COUNTDOWN) {
        if(ent->count <= 10) {
            gi.sound (ent, CHAN_CTF, gi.soundindex (CTF_Countdown_Table[ent->count]), 1, ATTN_NONE, 0);
        }

        switch (ent->count) {
        case 60:
            ctf_BSafePrint(PRINT_HIGH, "60 seconds until match begins.\n");
            break;
        case 30:
            ctf_BSafePrint(PRINT_HIGH, "30 seconds until match begins.\n");
            break;
        case 15:
            if (matchstate == MATCH_RAILGUN_COUNTDOWN) {
                ctf_BSafePrint(PRINT_HIGH, "Prepare to annihilate your enemy...\n");

                gi.sound(ent, CHAN_CTF, gi.soundindex ("weapons/bfg__l1a.wav"), 1, ATTN_NONE, 0);
            } else {
                ctf_BSafePrint(PRINT_HIGH, "15 seconds until match begins.\n");
            }
            break;
        case 10:
            ctf_BSafePrint(PRINT_HIGH, "10 seconds until match begins.\n");
            break;
        case 0:
        //    gi.sound (ent, CHAN_CTF, gi.soundindex ("ctf/go.wav"), 1, ATTN_NONE, 0);
            Last_Guy = 0;
            Position_Count = 0;
            Match_Start(ent);
            break;
        }
    } else if (matchstate == MATCH_RAILGUN_INPLAY) {
        edict_t *cl_ent;

        if (++Position_Count == 6) {
            cl_ent = g_edicts + 1 + Last_Guy;

            while (cl_ent->health <= 0) {
                Last_Guy++;

                if (Last_Guy == game.maxclients) {
                    Last_Guy = 0;
                    break;
                }
            }

            cl_ent = g_edicts + 1 + Last_Guy;

            if(cl_ent->health > 0) {
                ForceCommand(cl_ent, "say I am %p");
            }

            Last_Guy++;
            Position_Count = 0;
        }


        if (ent->count == 0 || One_Man_Left()) {
            Railgun_Victor = Declare_Railgun_Victor();
            matchstate = MATCH_RAILGUN_OVER;
        } else if(ent->count <= 15) {
            sprintf(message, "%d\n", ent->count);
            ctf_BSafePrint(PRINT_HIGH, message);

            if (ent->count <= 10) {
                gi.sound (ent, CHAN_CTF, gi.soundindex (CTF_Countdown_Table[ent->count]), 1, ATTN_NONE, 0);
            }
        }

    } else if (matchstate == MATCH_INPLAY) {
        // Start the countdown if we hit the fraglimit
        if (ent->count > 10) {
            for (i=0 ; i<maxclients->value ; i++) {
                player = g_edicts + 1 + i;
                if (!player->inuse) {
                    continue;
                }

                if (fraglimit->value && stats_get(player, STATS_SCORE) >= fraglimit->value) {
                    // Fraglimit was hit
                    ent->count = 10;
                }
            }
        }

        if (ent->count <= 0) { // End match
            Match_End(ent);
            return;
        } else if (!(ent->count % 60)) {
            if (minutes > 1) {
                sprintf(message, "%d minutes until match ends.\n", minutes);
            } else if (minutes == 1) {
                sprintf(message, "%d minute until match ends.\n", minutes);
            } else {
                sprintf(message, "%d frags until match ends.\n", (int)fraglimit->value);
            }
            ctf_BSafePrint(PRINT_HIGH, message);
        } else if(ent->count <= 10) {
            gi.sound (ent, CHAN_CTF, gi.soundindex (CTF_Countdown_Table[ent->count]), 1, ATTN_NONE, 0);
        }
    } else if (matchstate == MATCH_OVER) {
        if (ent->count <= 0) {
            matchstate = MATCH_NONE;
            ent->think = G_FreeEdict;
            tourneyclock = NULL;
        }
    }

    ent->count--;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq 0x45238(%rip), %rax     # 0x66da0
movss 0x4(%rax), %xmm0
addss 0x2d3b3(%rip), %xmm0    # 0x4ef28
movss %xmm0, 0x200(%rdi)
movq 0x44f1c(%rip), %rax     # 0x66aa0
cmpl $0x0, (%rax)
jne 0x21f39
movq %rdi, %rbx
movl 0x290(%rdi), %ebp
pushq $0x3c
popq %rsi
movl %ebp, %eax
cltd
idivl %esi
movl %eax, %r8d
movq 0x44c30(%rip), %r15     # 0x667d8
movl (%r15), %eax
cmpl $0x4, %eax
jne 0x21be4
movq 0x45049(%rip), %rax     # 0x66c00
andl $0x0, (%rax)
testl %ebp, %ebp
jg 0x21f33
andl $0x0, (%r15)
movq 0x44863(%rip), %rax     # 0x66430
movq %rax, 0x210(%rbx)
movq 0x4540d(%rip), %rax     # 0x66fe8
andq $0x0, (%rax)
jmp 0x21f33
leal 0x1(%r8), %ecx
cmpl $0x3c, %ebp
cmovll %ebp, %ecx
movq 0x4500b(%rip), %rdx     # 0x66c00
movl %ecx, (%rdx)
addl $-0x2, %eax
cmpl $0x4, %eax
ja 0x21f33
leaq 0x2dd8a(%rip), %rcx     # 0x4f994
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0xa, %ebp
jg 0x21c53
movslq %ebp, %rax
movq 0x449de(%rip), %rcx     # 0x66600
movq 0x20(%rcx), %r14
movq 0x44ae3(%rip), %rdx     # 0x66710
movq (%rdx,%rax,8), %rdi
callq *0x48(%rcx)
pushq $0x5
popq %rsi
movss 0x2d2e9(%rip), %xmm0    # 0x4ef28
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %rbx, %rdi
movl %eax, %edx
callq *%r14
movl 0x290(%rbx), %ebp
testl %ebp, %ebp
je 0x21e97
cmpl $0xa, %ebp
je 0x21e33
cmpl $0xf, %ebp
je 0x21e4b
cmpl $0x1e, %ebp
je 0x21e3f
cmpl $0x3c, %ebp
jne 0x21f33
leaq 0x2de9b(%rip), %rsi     # 0x4fb21
jmp 0x21f2b
movq 0x4493e(%rip), %r14     # 0x665d0
movzwl (%r14), %eax
incl %eax
movw %ax, (%r14)
cmpw $0x6, %ax
jne 0x21d13
movq 0x448af(%rip), %rax     # 0x66558
movq (%rax), %rcx
leaq 0x4a8(%rcx), %rdi
movq 0x4515e(%rip), %r12     # 0x66e18
movswq (%r12), %rax
imulq $0x4a8, %rax, %rdx      # imm = 0x4A8
cmpl $0x0, 0x6fc(%rcx,%rdx)
jg 0x21cd8
andw $0x0, (%r12)
xorl %eax, %eax
movswq %ax, %rcx
imulq $0x4a8, %rcx, %rcx      # imm = 0x4A8
cmpl $0x0, 0x254(%rdi,%rcx)
jle 0x21d07
addq %rcx, %rdi
leaq 0x2dee4(%rip), %rsi     # 0x4fbdb
callq 0x18d20
movzwl (%r12), %eax
movl 0x290(%rbx), %ebp
incl %eax
movw %ax, (%r12)
andw $0x0, (%r14)
testl %ebp, %ebp
je 0x21d24
callq 0x183b0
testl %eax, %eax
je 0x21eb7
callq 0x18080
movq 0x44a58(%rip), %rcx     # 0x66788
movq %rax, (%rcx)
movl $0x7, (%r15)
jmp 0x21f33
movl %ebp, %ecx
cmpl $0xb, %ebp
jl 0x21de5
movq %r8, 0x8(%rsp)
movq 0x447da(%rip), %rax     # 0x66530
movq (%rax), %rax
movl $0x4a8, %r13d           # imm = 0x4A8
xorl %r15d, %r15d
movq 0x447ef(%rip), %r12     # 0x66558
movq 0x45090(%rip), %r14     # 0x66e00
cvtsi2ss %r15d, %xmm0
movss 0x20(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x21dd7
movq (%r12), %rdi
cmpl $0x0, 0x60(%rdi,%r13)
je 0x21dcb
movq (%r14), %rcx
movss 0x20(%rcx), %xmm0
ucomiss 0x2dbf6(%rip), %xmm0    # 0x4f990
jne 0x21d9e
jnp 0x21dcb
addq %r13, %rdi
pushq $0x3
popq %rsi
callq 0x18030
cvtsi2ss %rax, %xmm0
movq (%r14), %rax
ucomiss 0x20(%rax), %xmm0
movq 0x44774(%rip), %rax     # 0x66530
movq (%rax), %rax
jb 0x21dcb
movl $0xa, 0x290(%rbx)
addq $0x4a8, %r13            # imm = 0x4A8
incl %r15d
jmp 0x21d70
movl 0x290(%rbx), %ecx
pushq $0x3c
popq %rsi
movq 0x8(%rsp), %r8
testl %ecx, %ecx
jle 0x21e1a
movl %ecx, %eax
xorl %edx, %edx
divl %esi
testl %edx, %edx
je 0x21f0e
cmpl $0xa, %ecx
ja 0x21f33
movq 0x447f9(%rip), %rax     # 0x66600
movq 0x20(%rax), %r14
movl %ecx, %ecx
movq 0x448fc(%rip), %rdx     # 0x66710
movq (%rdx,%rcx,8), %rdi
jmp 0x21e76
movq %rbx, %rdi
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x18e70
leaq 0x2dd81(%rip), %rsi     # 0x4fbbb
jmp 0x21f2b
leaq 0x2dcfb(%rip), %rsi     # 0x4fb41
jmp 0x21f2b
cmpl $0x5, (%r15)
jne 0x21f24
leaq 0x2dd05(%rip), %rsi     # 0x4fb61
pushq $0x2
popq %rdi
callq 0x193f0
movq 0x44795(%rip), %rax     # 0x66600
movq 0x20(%rax), %r14
leaq 0x2dd10(%rip), %rdi     # 0x4fb86
callq *0x48(%rax)
pushq $0x5
popq %rsi
movss 0x2d0a4(%rip), %xmm0    # 0x4ef28
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %rbx, %rdi
movl %eax, %edx
callq *%r14
jmp 0x21f33
movq 0x44f7a(%rip), %rax     # 0x66e18
andw $0x0, (%rax)
movq 0x44727(%rip), %rax     # 0x665d0
andw $0x0, (%rax)
movq %rbx, %rdi
callq 0x18980
jmp 0x21f33
movl 0x290(%rbx), %edx
cmpl $0xf, %edx
jg 0x21f33
leaq 0x2f35b(%rip), %rsi     # 0x51224
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x198c0
pushq $0x2
popq %rdi
movq %r14, %rsi
callq 0x193f0
movslq 0x290(%rbx), %rax
cmpq $0xa, %rax
jg 0x21f33
movq 0x44709(%rip), %rcx     # 0x66600
movq 0x20(%rcx), %r14
movq 0x4480e(%rip), %rdx     # 0x66710
movq (%rdx,%rax,8), %rdi
callq *0x48(%rcx)
jmp 0x21e79
cmpl $0x78, %ebp
jl 0x21f4b
leaq 0x2dab6(%rip), %rsi     # 0x4f9d0
leaq 0x10(%rsp), %rdi
movl %r8d, %edx
jmp 0x21f7f
leaq 0x2dc70(%rip), %rsi     # 0x4fb9b
pushq $0x2
popq %rdi
callq 0x193f0
decl 0x290(%rbx)
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl $-0x3c, %ebp
cmpl $0x3b, %ebp
ja 0x21f64
leaq 0x2dc8d(%rip), %rsi     # 0x4fbe7
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
jmp 0x21f7f
movq 0x44e95(%rip), %rax     # 0x66e00
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x2dc8a(%rip), %rsi     # 0x4fc04
leaq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0x198c0
pushq $0x2
popq %rdi
leaq 0x10(%rsp), %rsi
jmp 0x21f2e