// File: /workspace/github_c_makefile_star2_O1/Ziddykins[P]Dawn/src/items.c
// Function: generate_drop
void generate_drop(struct Message *message) {
    char out[MAX_MESSAGE_BUFFER];
    const char *rarity[]  = {NULL, "Common ", "Uncommon ", "Rare ", "Mythical ", "Epic ", "Legendary ", "Godly ", "Forsaken "};
    const char *weapons[] = {"Sword", "Mace", "Scimitar", "Axe", "Club", "Staff", "Wand", "Pike", "Polearm", "Lance", "Spear", "Floppy Dildo"};
    const char *shields[] = {"Tower Shield", "Round Shield", "Small Shield", "Large Shield", "Medium Shield"};
    const char *armor[]   = {"Leather Armor", "Breastplate Armor", "Fullplate Armor", "Chainmail Armor", "Cloth Armor"};
    char item_name[200];
    item_name[0] = '\0';

    float rarity_chance = rand()/(RAND_MAX/100.0f);
    int type_chance     = rand()%MAX_ITEM_TYPE;
    int p_index = get_pindex(message->sender_nick);
    int drop_level = dawn->global_monster.drop_level;
    int str, def, intel, mdef, hp;
    str = def = intel = mdef = hp = 0;

    if (dawn->players[p_index].available_slots == 0) {
        sprintf(out, "PRIVMSG %s :%s has found an item, but has no more room in his inventory!\r\n",
                message->receiver, message->sender_nick);
        add_msg(out, strlen(out));
        return;
    }

    struct Inventory item_dropped;
    item_dropped.type = type_chance;

    if (rarity_chance <= COMMON) {
        item_dropped.rarity = 1;
    } else if (rarity_chance <= UNCOMMON) {
        strcat(item_name, IRC_CYAN);
        item_dropped.rarity = 2;
    } else if (rarity_chance <= RARE) {
        strcat(item_name, IRC_ORANGE);
        item_dropped.rarity = 3;
    } else if (rarity_chance <= MYTHICAL) {
        strcat(item_name, IRC_PURPLE);
        item_dropped.rarity = 4;
    } else if (rarity_chance <= EPIC) {
        strcat(item_name, IRC_DBLUE);
        item_dropped.rarity = 5;
    } else if (rarity_chance <= LEGENDARY) {
        strcat(item_name, IRC_BROWN);
        item_dropped.rarity = 6;
    } else if (rarity_chance <= GODLY) {
        strcat(item_name, IRC_BOLD);
        strcat(item_name, IRC_PINK);
        item_dropped.rarity = 7;
    } else if (rarity_chance <= FORSAKEN) {
        strcat(item_name, IRC_BOLD);
        strcat(item_name, IRC_UNDRL);
        strcat(item_name, IRC_RED);
        item_dropped.rarity = 8;
    } else {
        enum material {WOOD, LEATHER, STONE, ORE, BRONZE, MAIL, STEEL, DIAMOND};
        char *mat_types[] = {"wood", "leather", "stone", "ore", "bronze", "mail", "steel", "diamond"};
        char plural[7], mat_name[10];
        int type, amount;

        if (drop_level < 4)       type = rand() % 2;
        else if (drop_level < 6)  type = rand() % 3;
        else if (drop_level < 8)  type = rand() % 4;
        else if (drop_level < 10) type = rand() % 6;
        else if (drop_level < 11) type = rand() % 7;
        else                      type = rand() % MAX_MATERIAL_TYPE;

        amount = 1 + rand() % 4;
        amount > 1 ? strcpy(plural, "scraps") : strcpy(plural, "scrap");
        strcpy(mat_name, mat_types[type]);
        dawn->players[p_index].materials[type] += amount;
/*
        switch (type) {
            case WOOD:    dawn->players[p_index].wood    += amount; break;
            case LEATHER: dawn->players[p_index].leather += amount; break;
            case STONE:   dawn->players[p_index].stone   += amount; break;
            case ORE:     dawn->players[p_index].ore     += amount; break;
            case BRONZE:  dawn->players[p_index].bronze  += amount; break;
            case MAIL:    dawn->players[p_index].mail    += amount; break;
            case STEEL:   dawn->players[p_index].steel   += amount; break;
            case DIAMOND: dawn->players[p_index].diamond += amount; break;
        }
*/
        sprintf(out, "PRIVMSG %s :%s finds %d %s %s\r\n",
                message->receiver, message->sender_nick, amount, mat_name, plural);
        add_msg(out, strlen(out));

        return;
    }

    strcat(item_name, rarity[item_dropped.rarity]);

    switch (type_chance) {
        case 0:
        {
            char weapon_type[48];
            str = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            intel = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            def   = 1 + rand() % str;
            mdef  = 1 + rand() % str;
            strcpy(weapon_type, weapons[rand()%MAX_WEAPON_TYPE]);
            strcat(item_name, weapon_type);
            break;
        }
        case 1:
        {
            char shield_type[48];
            def = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            intel = 1 + rand() % def;
            str   = 1 + rand() % def;
            mdef = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            strcpy(shield_type, shields[rand()%MAX_SHIELD_TYPE]);
            strcat(item_name, shield_type);
            break;
        }
        case 2:
        {
            char armor_type[48];
            def = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            intel = 1 + rand() % def;
            str   = 1 + rand() % def;
            mdef = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
            strcpy(armor_type, armor[rand()%MAX_ARMOR_TYPE]);
            strcat(item_name, armor_type);
            break;
        }
        default:
            PRINTERR("ITEM ERROR")
    }

    if (item_dropped.rarity >= 3) {
        hp = 1 + rand() % ((drop_level * dawn->players[p_index].level) * 2 * item_dropped.rarity);
    }

        
    strcat(item_name, IRC_NORMAL);
    //strcpy(item_name, item_name); programming drunk. not even once.
    size_t len = strlen(item_name);
    item_name[len+1] = '\0';

    strcpy(item_dropped.name, item_name);

    item_dropped.attr_strength     = str;
    item_dropped.attr_defense      = def;
    item_dropped.attr_intelligence = intel;
    item_dropped.attr_mdef         = mdef;
    item_dropped.attr_health = hp;
    item_dropped.req_level = 1;
    item_dropped.weight = 5;
    item_dropped.socket_one = 0;
    item_dropped.socket_two = 0;
    item_dropped.socket_three = 0;
    item_dropped.attr_mana = 0;

    //see inventory.h
    item_dropped.bitfield &= ~(1 << RUSTED);     //Not rusted
    item_dropped.bitfield &= ~(1 << EQUIPPED);   //Not equipped
    item_dropped.bitfield |=  (1 << EQUIPPABLE); //Equippable
    item_dropped.bitfield |=  (1 << NEW);        //New
    item_dropped.bitfield &= ~(1 << FAVORITE);  //Not favorite


    for (int i=0; i<MAX_INVENTORY_SLOTS; i++) {
        printf("%d. '%s'\n", i, dawn->players[p_index].inventory[i].name);
        if (strlen(dawn->players[p_index].inventory[i].name) < 2) {
            if (dawn->players[p_index].available_slots > 0) {
                dawn->players[p_index].inventory[i] = item_dropped;
                dawn->players[p_index].available_slots--;
                printf("%s got %s %d slots left\n", message->sender_nick, item_dropped.name, dawn->players[p_index].available_slots);
                break;
            } else {
                sprintf(out, "PRIVMSG %s :%s, you have no room in your inventory!\r\n",
                        message->receiver, message->sender_nick);
                add_msg(out, strlen(out));
                return;
            }
        } else {
            printf("wat?\n");
        }
    }

    sprintf(out, "PRIVMSG %s :%s has found a %s [S: %u - D: %u I: %u - MD: %u] on the corpse of the monster!\r\n",
            message->receiver, message->sender_nick, item_name, str, def, intel, mdef);
    add_msg(out, strlen(out));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
movq %rdi, %r14
movb $0x0, 0xe0(%rsp)
callq 0x2080
movl %eax, %ebx
callq 0x2080
movl %eax, %ebp
movq %r14, %rdi
callq 0xd89c
leaq 0x19af7(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
cltq
imulq $0x26a8, %rax, %r13     # imm = 0x26A8
cmpl $0x0, 0x2818(%rcx,%r13)
je 0x808a
movq %r14, 0x8(%rsp)
cvtsi2ss %ebx, %xmm0
divss 0xc34b(%rip), %xmm0     # 0x14300
movslq %ebp, %rbx
imulq $0x55555556, %rbx, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
leal (%rax,%rax,2), %eax
subl %eax, %ebx
movl 0xf2e60(%rcx), %r15d
movl %ebx, 0xd0(%rsp)
movl $0x1, %ebp
movss 0xc318(%rip), %xmm1     # 0x14304
ucomiss %xmm0, %xmm1
jae 0x815c
movss 0xc30b(%rip), %xmm1     # 0x14308
ucomiss %xmm0, %xmm1
jae 0x80ab
movss 0xc2fe(%rip), %xmm1     # 0x1430c
ucomiss %xmm0, %xmm1
jae 0x80bb
movss 0xc2f1(%rip), %xmm1     # 0x14310
ucomiss %xmm0, %xmm1
jae 0x80c8
movss 0xc2e4(%rip), %xmm1     # 0x14314
ucomiss %xmm0, %xmm1
jae 0x80d5
movss 0xc2d7(%rip), %xmm1     # 0x14318
ucomiss %xmm0, %xmm1
jae 0x80e2
movss 0xc2ca(%rip), %xmm1     # 0x1431c
ucomiss %xmm0, %xmm1
jae 0x80ef
movss 0xc2bd(%rip), %xmm1     # 0x14320
ucomiss %xmm0, %xmm1
jae 0x8104
cmpl $0x3, %r15d
jg 0x85e6
callq 0x2080
movl %eax, %ebx
shrl $0x1f, %eax
addl %ebx, %eax
andl $-0x2, %eax
jmp 0x8629
leaq 0xc0(%r14), %rdx
leaq 0xc3c8(%rip), %rsi      # 0x14460
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rcx
jmp 0x858c
movl $0x2, %ebp
movl $0x303103, %r12d        # imm = 0x303103
jmp 0x8147
movl $0x3, %ebp
movl $0x373003, %r12d        # imm = 0x373003
jmp 0x8147
movl $0x4, %ebp
movl $0x363003, %r12d        # imm = 0x363003
jmp 0x8147
movl $0x5, %ebp
movl $0x323003, %r12d        # imm = 0x323003
jmp 0x8147
movl $0x6, %ebp
movl $0x353003, %r12d        # imm = 0x353003
jmp 0x8147
movq %r13, %r14
movl $0x7, %ebp
movl $0x333103, %r12d        # imm = 0x333103
movw $0x2, %r13w
jmp 0x812e
movq %r13, %r14
leaq 0xe0(%rsp), %rdi
callq 0x20c0
movw $0x2, 0xe0(%rsp,%rax)
movl $0x8, %ebp
movl $0x343003, %r12d        # imm = 0x343003
movw $0x1f, %r13w
leaq 0xe0(%rsp), %rdi
callq 0x20c0
movw %r13w, 0xe0(%rsp,%rax)
movq %r14, %r13
leaq 0xe0(%rsp), %rdi
callq 0x20c0
movl %r12d, 0xe0(%rsp,%rax)
movl %ebp, 0xd8(%rsp)
movl %ebp, %eax
leaq 0x16be4(%rip), %rcx     # 0x1ed50
movq (%rcx,%rax,8), %rsi
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
callq 0x2410
cmpl $0x2, %ebx
je 0x823c
cmpl $0x1, %ebx
je 0x82db
testl %ebx, %ebx
jne 0x85b8
callq 0x2080
leaq 0x198da(%rip), %r14     # 0x21a80
movq (%r14), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull 0xd8(%rsp), %ecx
cltd
idivl %ecx
movl %edx, %ebp
incl %ebp
callq 0x2080
movq (%r14), %rcx
imull 0x2808(%rcx,%r13), %ebx
imull 0xd8(%rsp), %ebx
cltd
idivl %ebx
incl %edx
movq %rdx, 0x10(%rsp)
callq 0x2080
cltd
idivl %ebp
incl %edx
movq %rdx, 0x18(%rsp)
callq 0x2080
cltd
movl %ebp, 0x4(%rsp)
idivl %ebp
movl %edx, %r14d
incl %r14d
callq 0x2080
cltq
imulq $0x2aaaaaab, %rax, %rcx # imm = 0x2AAAAAAB
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x21, %rcx
addl %edx, %ecx
shll $0x2, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
cltq
leaq 0xc489(%rip), %rcx      # 0x146c0
jmp 0x8375
callq 0x2080
leaq 0x19838(%rip), %r14     # 0x21a80
movq (%r14), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull 0xd8(%rsp), %ecx
cltd
idivl %ecx
movl %edx, %ebp
incl %ebp
callq 0x2080
cltd
idivl %ebp
incl %edx
movq %rdx, 0x10(%rsp)
callq 0x2080
cltd
movq %rbp, 0x18(%rsp)
idivl %ebp
incl %edx
movl %edx, 0x4(%rsp)
callq 0x2080
movq (%r14), %rcx
imull 0x2808(%rcx,%r13), %ebx
imull 0xd8(%rsp), %ebx
cltd
idivl %ebx
movl %edx, %r14d
incl %r14d
callq 0x2080
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
cltq
leaq 0xc42e(%rip), %rcx      # 0x14704
jmp 0x8375
callq 0x2080
leaq 0x19799(%rip), %r14     # 0x21a80
movq (%r14), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull 0xd8(%rsp), %ecx
cltd
idivl %ecx
movl %edx, %ebp
incl %ebp
callq 0x2080
cltd
idivl %ebp
incl %edx
movq %rdx, 0x10(%rsp)
callq 0x2080
cltd
movq %rbp, 0x18(%rsp)
idivl %ebp
incl %edx
movl %edx, 0x4(%rsp)
callq 0x2080
movq (%r14), %rcx
imull 0x2808(%rcx,%r13), %ebx
imull 0xd8(%rsp), %ebx
cltd
idivl %ebx
movl %edx, %r14d
incl %r14d
callq 0x2080
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
cltq
leaq 0xc37b(%rip), %rcx      # 0x146f0
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c0
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2410
xorl %ebp, %ebp
cmpl $0x3, 0xd8(%rsp)
movl $0x0, %ebx
jl 0x83d9
callq 0x2080
leaq 0x196c7(%rip), %rcx     # 0x21a80
movq (%rcx), %rcx
imull 0x2808(%rcx,%r13), %r15d
imull 0xd8(%rsp), %r15d
addl %r15d, %r15d
cltd
idivl %r15d
movl %edx, %ebx
incl %ebx
movq %r12, %rdi
callq 0x20c0
movw $0xf, 0xe0(%rsp,%rax)
movq %r12, %rdi
callq 0x20c0
movb $0x0, 0x1(%rax,%r12)
leaq 0x44(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x21c0
movl 0x4(%rsp), %eax
movl %eax, 0x70(%r15)
movq 0x18(%rsp), %rax
movl %eax, 0x68(%r15)
movq 0x10(%rsp), %rax
movl %eax, 0x6c(%r15)
movq %r14, 0x30(%rsp)
movl %r14d, 0x74(%r15)
movl %ebx, 0x64(%r15)
movsd 0xbef8(%rip), %xmm0     # 0x14330
movups %xmm0, 0x78(%r15)
movl %ebp, 0x88(%r15)
movl %ebp, 0x90(%r15)
movb 0x98(%r15), %al
andb $-0x20, %al
orb $0x9, %al
movb %al, 0x98(%r15)
movq %r13, 0x28(%rsp)
addq $0x330, %r13            # imm = 0x330
leaq 0xc0c8(%rip), %r12      # 0x14538
leaq 0xc241(%rip), %rbx      # 0x146b8
xorl %r15d, %r15d
leaq 0x195ff(%rip), %r14     # 0x21a80
movq (%r14), %rdx
addq %r13, %rdx
movq %r12, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x2040
movq (%r14), %r14
leaq (%r14,%r13), %rbp
movq %rbp, %rdi
callq 0x20c0
cmpq $0x1, %rax
jbe 0x84c3
movq %rbx, %rdi
callq 0x2330
incq %r15
addq $0x9c, %r13
cmpq $0x19, %r15
jne 0x847a
jmp 0x8526
movq 0x28(%rsp), %r15
leaq (%r14,%r15), %rax
addq $0x1a0, %rax            # imm = 0x1A0
cmpl $0x0, 0x2678(%rax)
jle 0x856e
leaq 0x44(%rsp), %rbx
movl $0x9c, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2170
leaq 0x19585(%rip), %rax     # 0x21a80
movq (%rax), %rax
movl 0x2818(%rax,%r15), %ecx
decl %ecx
movl %ecx, 0x2818(%rax,%r15)
leaq 0xc02b(%rip), %rdi      # 0x14542
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2040
movq 0x8(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
subq $0x8, %rsp
leaq 0xc054(%rip), %rsi      # 0x14591
leaq 0x1b8(%rsp), %rbx
leaq 0xe8(%rsp), %r8
movq %rbx, %rdi
movl 0xc(%rsp), %r9d
xorl %eax, %eax
pushq 0x38(%rsp)
pushq 0x20(%rsp)
pushq 0x30(%rsp)
callq 0x2050
addq $0x20, %rsp
jmp 0x8593
movq 0x8(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
leaq 0xbfda(%rip), %rsi      # 0x1455b
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2050
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xcd1c
addq $0x348, %rsp            # imm = 0x348
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbf4a(%rip), %rdi      # 0x14509
callq 0x2190
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x839c
cmpl $0x5, %r15d
jg 0x860d
callq 0x2080
movslq %eax, %rbx
imulq $0x55555556, %rbx, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
leal (%rax,%rax,2), %eax
jmp 0x8629
cmpl $0x7, %r15d
jg 0x86e0
callq 0x2080
movl %eax, %ebx
leal 0x3(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
andl $-0x4, %eax
subl %eax, %ebx
movq 0x8(%rsp), %r15
callq 0x2080
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
movl %eax, %edx
subl %ecx, %edx
negl %ecx
leal (%rax,%rcx), %ebp
incl %ebp
testl %edx, %edx
jle 0x8659
movl $0x737061, 0x23(%rsp)   # imm = 0x737061
jmp 0x8660
movw $0x70, 0x24(%rsp)
movl $0x61726373, 0x20(%rsp) # imm = 0x61726373
movslq %ebx, %r14
leaq 0xc0a6(%rip), %rax      # 0x14718
movslq (%rax,%r14,4), %rsi
addq %rax, %rsi
leaq 0x3a(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c0
movslq %ebp, %r8
leaq 0x193f0(%rip), %rax     # 0x21a80
addq (%rax), %r13
addq %r8, 0x2798(%r13,%r14,8)
leaq 0xc0(%r15), %rdx
subq $0x8, %rsp
leaq 0x28(%rsp), %r10
leaq 0xbe37(%rip), %rsi      # 0x144e9
leaq 0x1b8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rcx
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x2050
addq $0x10, %rsp
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
jmp 0x859e
cmpl $0x9, %r15d
jg 0x8709
callq 0x2080
movslq %eax, %rbx
imulq $0x2aaaaaab, %rbx, %rax # imm = 0x2AAAAAAB
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
addl %eax, %eax
jmp 0x8608
callq 0x2080
cmpl $0xa, %r15d
jne 0x873e
movslq %eax, %rcx
imulq $-0x6db6db6d, %rcx, %rbx # imm = 0x92492493
shrq $0x20, %rbx
addl %eax, %ebx
movl %ebx, %ecx
shrl $0x1f, %ecx
sarl $0x2, %ebx
addl %ecx, %ebx
leal (,%rbx,8), %ecx
subl %ecx, %ebx
addl %eax, %ebx
jmp 0x862b
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x8, %ecx
subl %ecx, %eax
movl %eax, %ebx
jmp 0x862b