// File: /workspace/github_c_makefile_star2_O2/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 $0x358, %rsp            # imm = 0x358
movq %rdi, %r14
movb $0x0, 0x50(%rsp)
callq 0x2080
movl %eax, %ebx
callq 0x2080
pushq $0x3
popq %r12
cltd
idivl %r12d
movl %edx, %ebp
movq %r14, %rdi
callq 0xc860
leaq 0x18946(%rip), %rcx     # 0x1fa80
movq (%rcx), %rcx
cltq
imulq $0x26a8, %rax, %r13     # imm = 0x26A8
cmpl $0x0, 0x2818(%rcx,%r13)
je 0x7237
movq %r14, 0x38(%rsp)
cvtsi2ss %ebx, %xmm0
divss 0xc19a(%rip), %xmm0     # 0x13300
movl 0xf2e60(%rcx), %r15d
movl %ebp, 0x1b0(%rsp)
movss 0xc188(%rip), %xmm1     # 0x13304
ucomiss %xmm0, %xmm1
pushq $0x5
popq %rcx
jae 0x7258
movq %r13, %r14
movss 0xc175(%rip), %xmm1     # 0x13308
ucomiss %xmm0, %xmm1
setb %al
movl %eax, 0x10(%rsp)
jae 0x7266
movl $0x373003, %r13d        # imm = 0x373003
movss 0xc15b(%rip), %xmm1     # 0x1330c
ucomiss %xmm0, %xmm1
jae 0x7270
movss 0xc14e(%rip), %xmm1     # 0x13310
ucomiss %xmm0, %xmm1
jae 0x768f
movl $0x323003, %r13d        # imm = 0x323003
movss 0xc13b(%rip), %xmm1     # 0x13314
ucomiss %xmm0, %xmm1
movl %ecx, %r12d
jae 0x7270
movss 0xc12b(%rip), %xmm1     # 0x13318
ucomiss %xmm0, %xmm1
jae 0x76d2
movss 0xc11e(%rip), %xmm1     # 0x1331c
ucomiss %xmm0, %xmm1
jae 0x76e1
movss 0xc111(%rip), %xmm1     # 0x13320
ucomiss %xmm0, %xmm1
jae 0x76f1
cmpl $0x3, %r15d
jg 0x7724
callq 0x2080
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %edx, %ebp
movq %r14, %r12
jmp 0x776b
leaq 0xc0(%r14), %rdx
leaq 0xc21b(%rip), %rsi      # 0x13460
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rcx
jmp 0x7663
pushq $0x1
popq %r12
movl $0x0, 0x10(%rsp)
jmp 0x7282
pushq $0x2
popq %r12
movl $0x303103, %r13d        # imm = 0x303103
leaq 0x50(%rsp), %rdi
callq 0x20c0
movl %r13d, 0x50(%rsp,%rax)
movq %r14, %r13
movl %r12d, 0x1b8(%rsp)
movl %r12d, %eax
leaq 0x15abc(%rip), %rcx     # 0x1cd50
movq (%rcx,%rax,8), %rsi
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x2410
cmpl $0x2, %ebp
je 0x7348
cmpl $0x1, %ebp
je 0x73d1
testl %ebp, %ebp
jne 0x769e
callq 0x2080
leaq 0x187b5(%rip), %r14     # 0x1fa80
movq (%r14), %rcx
leal (%r15,%r15), %ebx
movl 0x2808(%rcx,%r13), %ecx
imull %ebx, %ecx
imull %r12d, %ecx
cltd
idivl %ecx
movl %edx, %ebp
incl %ebp
callq 0x2080
movq (%r14), %rcx
leaq 0x50(%rsp), %r14
imull 0x2808(%rcx,%r13), %ebx
imull %r12d, %ebx
cltd
idivl %ebx
incl %edx
movq %rdx, 0x20(%rsp)
callq 0x2080
cltd
idivl %ebp
incl %edx
movq %rdx, 0x28(%rsp)
callq 0x2080
cltd
movl %ebp, 0xc(%rsp)
idivl %ebp
incl %edx
movq %rdx, 0x18(%rsp)
callq 0x2080
pushq $0xc
popq %rcx
cltd
idivl %ecx
movslq %edx, %rax
leaq 0xc37d(%rip), %rcx      # 0x136c0
jmp 0x7455
callq 0x2080
leaq 0x1872c(%rip), %r14     # 0x1fa80
movq (%r14), %rcx
leal (%r15,%r15), %ebp
movl 0x2808(%rcx,%r13), %ecx
imull %ebp, %ecx
imull %r12d, %ecx
cltd
idivl %ecx
movl %edx, %ebx
incl %ebx
callq 0x2080
cltd
idivl %ebx
incl %edx
movq %rdx, 0x20(%rsp)
callq 0x2080
cltd
movq %rbx, 0x28(%rsp)
idivl %ebx
incl %edx
movl %edx, 0xc(%rsp)
callq 0x2080
movq (%r14), %rcx
leaq 0x50(%rsp), %r14
imull 0x2808(%rcx,%r13), %ebp
imull %r12d, %ebp
cltd
idivl %ebp
incl %edx
movq %rdx, 0x18(%rsp)
callq 0x2080
cltd
pushq $0x5
popq %rcx
idivl %ecx
movslq %edx, %rax
leaq 0xc338(%rip), %rcx      # 0x13704
jmp 0x7455
callq 0x2080
leaq 0x186a3(%rip), %r14     # 0x1fa80
movq (%r14), %rcx
leal (%r15,%r15), %ebp
movl 0x2808(%rcx,%r13), %ecx
imull %ebp, %ecx
imull %r12d, %ecx
cltd
idivl %ecx
movl %edx, %ebx
incl %ebx
callq 0x2080
cltd
idivl %ebx
incl %edx
movq %rdx, 0x20(%rsp)
callq 0x2080
cltd
movq %rbx, 0x28(%rsp)
idivl %ebx
incl %edx
movl %edx, 0xc(%rsp)
callq 0x2080
movq (%r14), %rcx
leaq 0x50(%rsp), %r14
imull 0x2808(%rcx,%r13), %ebp
imull %r12d, %ebp
cltd
idivl %ebp
incl %edx
movq %rdx, 0x18(%rsp)
callq 0x2080
cltd
pushq $0x5
popq %rcx
idivl %ecx
movslq %edx, %rax
leaq 0xc29b(%rip), %rcx      # 0x136f0
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c0
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x2410
movl 0x10(%rsp), %eax
xorl %ebx, %ebx
testb %al, %al
je 0x74aa
callq 0x2080
leaq 0x185f1(%rip), %rcx     # 0x1fa80
movq (%rcx), %rcx
imull 0x2808(%rcx,%r13), %r15d
imull %r12d, %r15d
addl %r15d, %r15d
cltd
idivl %r15d
movl %edx, %ebx
incl %ebx
movq %r14, %rdi
callq 0x20c0
movw $0xf, 0x50(%rsp,%rax)
movq %r14, %rdi
callq 0x20c0
movb $0x0, 0x1(%rax,%r14)
leaq 0x124(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x21c0
movl 0xc(%rsp), %eax
movl %eax, 0x70(%r15)
movq 0x28(%rsp), %rax
movl %eax, 0x68(%r15)
movq 0x20(%rsp), %rax
movl %eax, 0x6c(%r15)
movq 0x18(%rsp), %rax
movl %eax, 0x74(%r15)
movl %ebx, 0x64(%r15)
movsd 0xbe27(%rip), %xmm0     # 0x13330
movups %xmm0, 0x78(%r15)
andl $0x0, 0x88(%r15)
andl $0x0, 0x90(%r15)
movb 0x98(%r15), %al
andb $-0x20, %al
orb $0x9, %al
movb %al, 0x98(%r15)
movq %r13, 0x10(%rsp)
addq $0x330, %r13            # imm = 0x330
leaq 0xbff5(%rip), %r12      # 0x13538
leaq 0xc16e(%rip), %rbx      # 0x136b8
xorl %r15d, %r15d
cmpq $0x19, %r15
je 0x7600
leaq 0x18522(%rip), %r14     # 0x1fa80
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 0x759a
movq %rbx, %rdi
callq 0x2340
incq %r15
addq $0x9c, %r13
jmp 0x754d
movq 0x10(%rsp), %r15
leaq (%r14,%r15), %rax
addq $0x1a0, %rax            # imm = 0x1A0
cmpl $0x0, 0x2678(%rax)
jle 0x7645
leaq 0x124(%rsp), %rbx
movl $0x9c, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2170
leaq 0x184ab(%rip), %rax     # 0x1fa80
movq (%rax), %rax
movl 0x2818(%rax,%r15), %ecx
decl %ecx
movl %ecx, 0x2818(%rax,%r15)
leaq 0xbf51(%rip), %rdi      # 0x13542
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2040
movq 0x38(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
subq $0x8, %rsp
leaq 0xbf7a(%rip), %rsi      # 0x13591
leaq 0x1c8(%rsp), %rbx
leaq 0x58(%rsp), %r8
movq %rbx, %rdi
movl 0x14(%rsp), %r9d
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x2050
addq $0x20, %rsp
jmp 0x766a
movq 0x38(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
leaq 0xbf03(%rip), %rsi      # 0x1355b
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2050
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xbd04
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x4
popq %r12
movl $0x363003, %r13d        # imm = 0x363003
jmp 0x7270
leaq 0xbe64(%rip), %rdi      # 0x13509
callq 0x2190
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x7479
pushq $0x6
popq %r12
movl $0x353003, %r13d        # imm = 0x353003
jmp 0x7270
pushq $0x7
popq %r12
movl $0x333103, %r13d        # imm = 0x333103
movw $0x2, %bx
jmp 0x7710
leaq 0x50(%rsp), %rdi
callq 0x20c0
movw $0x2, 0x50(%rsp,%rax)
pushq $0x8
popq %r12
movl $0x343003, %r13d        # imm = 0x343003
movw $0x1f, %bx
leaq 0x50(%rsp), %rdi
callq 0x20c0
movw %bx, 0x50(%rsp,%rax)
jmp 0x7270
cmpl $0x5, %r15d
movq %r14, %r12
ja 0x7736
callq 0x2080
pushq $0x3
jmp 0x7765
cmpl $0x7, %r15d
ja 0x7745
callq 0x2080
pushq $0x4
jmp 0x7765
cmpl $0x9, %r15d
ja 0x7754
callq 0x2080
pushq $0x6
jmp 0x7765
callq 0x2080
cmpl $0xa, %r15d
jne 0x7763
pushq $0x7
jmp 0x7765
pushq $0x8
popq %rcx
cltd
idivl %ecx
movl %edx, %ebp
callq 0x2080
pushq $0x4
popq %rcx
cltd
idivl %ecx
leal 0x1(%rdx), %r15d
testl %edx, %edx
jle 0x7788
movl $0x737061, 0x33(%rsp)   # imm = 0x737061
jmp 0x778f
movw $0x70, 0x34(%rsp)
movl $0x61726373, 0x30(%rsp) # imm = 0x61726373
movslq %ebp, %r14
leaq 0xbf77(%rip), %rax      # 0x13718
movslq (%rax,%r14,4), %rsi
addq %rax, %rsi
leaq 0x46(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c0
movslq %r15d, %r8
leaq 0x182c1(%rip), %rax     # 0x1fa80
addq (%rax), %r12
addq %r8, 0x2798(%r12,%r14,8)
movq 0x38(%rsp), %rcx
leaq 0xc0(%rcx), %rdx
subq $0x8, %rsp
leaq 0x38(%rsp), %r10
leaq 0xbd03(%rip), %rsi      # 0x134e9
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %r9
xorl %eax, %eax
pushq %r10
callq 0x2050
popq %rcx
popq %rdx
movq %r14, %rdi
callq 0x20c0
movq %r14, %rdi
jmp 0x7675