// NAME: Gourmet Guy COMPLETE
// GAMES: MP3_USA
// EXECUTION: Direct
// PARAM: Space|SpaceToFace

#include "ultra64.h"
u8 itemOdds[] = {98, 97, 94, 90, 75, 60, 50, 40, 30, 20, 10};
u8 itemIDsForRand[] = {18, 16, 15, 13, 12, 11, 10, 9, 6, 4, 1};

extern s16 GetCurrentPlayerIndex();
extern struct player *GetPlayerStruct(s32 player_index);
extern s32 PlayerHasEmptyItemSlot(s32 player_index);
char string[] = "Text 1\x0A\x0A \x0C  Item 1\x0D\x0A \x0C  Item 2\x0D\x0A \x0C  Item 3\x0D\x0A \x0C  ViewMap\x0D\x0A \x0C  Don\x5Ct Feed Him\x0D\x0A\x00";
char replacewithlamp[] = "Replace which item with a \x07magic lamp\x08\xC3\x0A\x0A \x0C  Item 1\x0D\x0A \x0C  Item 2\x0D\x0A \x0C  Item 3\x0D\x0A \x0C  View Map\x0D\x0A \x0C  Discard magic lamp\x0D\x00";
char noitems[] = "What a tease\x5C you have no food\x85\xFF\x00";

char initialStringFormatted[0x200];
char initialStringFormatters[] = "Why\x82 I smell such delicacies\xC2"
" You there\xC2\x0AWould you be so kind as to\x0Agive me a scrumptious snack\xC3\x0A\x0A"
"\x1A\x1A\x0C \x08 Feed him the\x07 first item\x0D\x0A"
"\x1A\x1A\x0C %s Feed him the\x03 second item\x0D\x0A"
"\x1A\x1A\x0C %s Feed him the\x06 third item\x0D\x0A"
"\x1A\x1A\x0C \x08 View Map\x0D\x0A"
"\x1A\x1A\x0C \x08 Don\x5Ct feed him\x0D\x00";



char mushroomText[] = "Mmm\x85" "That was \x03yummy\xC2" "\x08Here\x82" " take these coins\xC2\xFF";
char reverseMushroomText[] = "Wow\xC2" " This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x05astonishing\xC2\x0A" "\x08It tastes like \x05jelly\xC2\x0A" "\x08Here\xC2" "You can have a \x03goodie\xC2\xFF";
char poisonMushroomText[] = "\x04 Ackpthh\xC2" "\x08What was \x03THAT\xC3\xC2\x0A" "That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why it\x5Cs" " absolutely \x03OUTRAGEOUS\xC2\x0A" "\x08Why would"" you give me \x03THAT\xC3" " \x08Here\x85" "Take \x03this" "\x08and go away\x85\xFF";
char goldenMushroomText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x07glorious\xC2\x0A" "\x08It DOES taste like \x07gold\xC2\x0A" "\x08Here\xC2" " You deserve a \x03good reward\xC2\xFF";
char magicLampText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x06 phenomenal\xC2\x0A" "\x08It almost tastes" " like \x07magic\xC2\x0A" "\x08Here\xC2" " You deserve a \x03good reward\xC2\xFF";
char booBellText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x04 ethereal\xC2\x0A" "\x08Here\xC2" " Take \x07 25 Coins\xC2\xFF";
char skeletonKeyText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and texture\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x07 delectable\xC2\x0A" "\x08Here\x85" "Take \x07 15 Coins\xC2\xFF";
char barterBoxText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x04 exquisite\xC2\x0A" "\x08It almost tastes" " like \x03chocolate cake\xC2\x0A" "\x08 Here\xC2\x0A" " Take \x07 35 Coins \x08and a \x03goodie\xC2\xFF";
char booRepellantText[] = "\x04 Ackpthh\xC2" "\x08What was \x03THAT\xC3\xC2" "\x08That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why it\x5Cs" " absolutely \x03OUTRAGEOUS\xC2\x0A" "\x08 Why would" " you give me \x03that\xC3" "\x08Here\x85\x0A" " Take \x03this\x85\x85\x85" "\x08You\x5Cll need it\x85\xFF";
char bowserPhoneText[] = "\x04 Ackpthh\xC2" "\x08What was \x03THAT\xC3\xC2" "\x08That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why it\x5Cs" " absolutely \x03OUTRAGEOUS\xC2\x0A" "\x08 Why would" " you give me \x03that\xC3" "\x08Here\x85\x0A" " Take \x03this\x85\x85\x85" "\x08You\x5Cll need it\x85\xFF";
char wackyWatchText[] = "Wow\xC2" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply heavenly\xC2\x0A" "It almost tastes like \x03pizza\xC2\x0A" "\x08 Here\xC2\x0A" " Take"  "\x07 50 Coins \x08and a \x03goodie\xC2\xFF";
char luckyLampText[] = "Wow\xC2" "That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x06 scrumptious\xC2\x0A" "\x08 It almost tastes like \x06cotton candy\xC2\x0A" "\x08Here\xC2\x0A" " Take \x07 25 Coins \x08and a \x03goodie\xC2\xFF";
char plunderChestText[] = "\x04 Ackpthh\xC2\x0A" "\x08What was \x03THAT\xC3\xC2\x0A" "That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why it\x5Cs" " absolutely \x03OUTRAGEOUS\xC2\x0A" "\x08Why would you" " give me \x03 that\xC3\x0A""\x08Here\x85\x0A" "Take \x03this\x85\x85\x85" "\x08You\x5Cll need it\x85\xFF";
char cellularShopperText[] = "\x04 Ackpthh\xC2" "\x08What was \x03THAT\xC3\xC2\x0A" "That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why it\x5Cs" " absolutely \x03OUTRAGEOUS\xC2\x0A" "\x08Why would you" " give me \x03 that\xC3\x0A""\x08Here\x85\x0A" "Take \x03this\x85\x85\x85" "\x08You\x5Cll need it\x85\xFF";
char luckyCoinText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x04 ethereal\xC2\x0A" "\x08Here\xC2" " Take"  "\x07 25 Coins \x08and a \x03goodie\xC2\xFF";
char duelingGloveText[] = "Oh wow\xC2\x0A" "You brought me a \x03handkercheif\xC2\x0A" "\x08Why \x07Thank you\xC2" "\x08Here\xC2" " Take"  "\x07 15 Coins \x08and a \x03goodie\xC2\xFF";
char warpBlockText[] = "Wow\xC2\x0A" "This combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why" " it\x5Cs" " simply \x04 ethereal\xC2\x0A" "\x08Here\xC2" " Take"  "\x07 15 Coins \x08and a \x03goodie\xC2\xFF";
char koopaCardText[] = "\x04 Ackpthh\xC2" "\x08What was \x03THAT\xC3\xC2" "\x08That combination" " of flavor" " and textures\x85\x85\x85\x0A" "Why it\x5Cs" " absolutely \x03OUTRAGEOUS\xC2\x0A" "\x08Why would" " you give"" me \x03that\xC3" "\x08Here\x85\x0A" "Take \x03this\x85\x85\x85" "\x08You\x5Cll need it\x85\xFF";
char invalidItemText[] = "FEED ME\xC2\xC2\xC2\xFF";

enum item {
    ITEM_NONE = 0xFF,
    ITEM_MUSHROOM = 0x00,
    ITEM_SKELETON_KEY = 0x01,
    ITEM_POISON_MUSHROOM = 0x02,
    ITEM_REVERSE_MUSHROOM = 0x03,
    ITEM_CELLULAR_SHOPPER = 0x04,
    ITEM_WARP_BLOCK = 0x05,
    ITEM_PLUNDER_CHEST = 0x06,
    ITEM_BOWSER_PHONE = 0x07,
    ITEM_DUELING_GLOVE = 0x08,
    ITEM_LUCKY_LAMP = 0x09,
    ITEM_GOLDEN_MUSHROOM = 0x0A,
    ITEM_BOO_BELL = 0x0B,
    ITEM_BOO_REPELLANT = 0x0C,
    ITEM_BOWSER_SUIT = 0x0D,
    ITEM_MAGIC_LAMP = 0x0E,
    ITEM_KOOPA_CARD = 0x0F,
    ITEM_BARTER_BOX = 0x10,
    ITEM_LUCKY_COIN = 0x11,
    ITEM_WACKY_WATCH = 0x12
};

int weighedRandItem() {
    int randInt;
    int i;
    randInt = getRand(0x63);
    
    for (i = 0; i < 11; i++) {
        if (itemOdds[i] < randInt) {
            return itemIDsForRand[i];
        }
    }
}

struct player {
    s8 unk0;
    s8 cpu_difficulty;
    s8 controller;
    u8 character;
    u8 flags;
    s8 pad0[5];
    s16 coins; // 10
    s16 minigame_coins; // 12
    s8 stars; // 14

    u8 cur_chain_index; // 15
    u8 cur_space_index; // 16
    u8 next_chain_index; // 17
    u8 next_space_index; // 18
    u8 unk1_chain_index; // 19
    u8 unk1_space_index; // 20
    u8 reverse_chain_index; // 21
    u8 reverse_space_index; // 22

    u8 flags2; // 23
    u8 items[3]; // 24
    u8 bowser_suit_flag; // 27
    u8 turn_color_status; // 28

    s8 pad1[7]; // 29 - 35

    void *obj; // 36 // struct object *
    s16 minigame_star; // 40
    s16 coin_star; // 42
    s8 happening_space_count; // 44
    s8 red_space_count;
    s8 blue_space_count;
    s8 chance_space_count;
    s8 bowser_space_count; // 48
    s8 battle_space_count;
    s8 item_space_count;
    s8 bank_space_count;
    s8 game_guy_space_count; // 52

    // s8 pad2[3];
}; // sizeof == 56

void main() {
	int curPlayerIndex;
	curPlayerIndex = GetCurrentPlayerIndex();
	struct player *player = GetPlayerStruct(curPlayerIndex);
	SetBoardPlayerAnimation(-1, -1, 2); //set player to idle
	func_800ED20C(-1, 8, SpaceToFace); //rotate player to space
	handleMainEvent();
	SleepProcess(40); //sleep 1 second 40 frames
}

u8 handleMainEvent() {
	int chosenIndex;
	int curPlayerIndex;
	int itemToFeed;
	int coinsToAdd;
	int randItem;
	int givenItem;
	int afterEatingMessage;
	int soundID;
	int i;
	int eatBool = 0;
	char optionIndex1Color[8] = "\x08\x00\x00\x00\x00\x00\x00\x00";
	char optionIndex2Color[8] = "\x08\x00\x00\x00\x00\x00\x00\x00";

	char optionIndex1ColorDark[8] = "\x01\x00\x00\x00\x00\x00\x00";
	char optionIndex2ColorDark[8] = "\x01\x00\x00\x00\x00\x00\x00";

	curPlayerIndex = GetCurrentPlayerIndex();
	struct player *player = GetPlayerStruct(curPlayerIndex);
	beginning:
	
	
	if (player->items[0] == 0xFF && player->items[1] == 0xFF && player->items[2] == 0xFF) {
		ShowMessage(-1, noitems, 0, 0, 0, 0, 0);
		messageCleanup();
    } else {
		if (player->items[1] == 0xFF) {
			bcopy(optionIndex1Color, optionIndex1ColorDark, 1);
        }
		if (player->items[2] == 0xFF) {
			bcopy(optionIndex2Color, optionIndex2ColorDark, 1);
        }
		sprintf(initialStringFormatted, initialStringFormatters, optionIndex1Color, optionIndex2Color);
		ShowMessage(-1, initialStringFormatted, 0, 0, 0, 0, 0);
		if (player->items[1] == 0xFF) {
			DisablePromptOption(2, 1);
        } 
		if (player->items[2] == 0xFF) {
			DisablePromptOption(2, 2);
        }

		chosenIndex = GetBasicPromptSelection (2, 1);
		messageCleanup();
		if (chosenIndex == 0) {
			itemToFeed = player->items[0];
    	} else if (chosenIndex == 1) {
			itemToFeed = player->items[1];
    	} else if (chosenIndex == 2) {
			itemToFeed = player->items[2];
    	} else if (chosenIndex == 3) {
			ViewBoardMap();
			goto beginning;
    	} else if (chosenIndex == 4) {
			itemToFeed = 0xFF;
    	}
	
		switch (itemToFeed) {
        	case ITEM_MUSHROOM:
				coinsToAdd = 10;
				randItem = 0xFF;
				givenItem = 0xFF;
				afterEatingMessage = mushroomText;
				break;
        	case ITEM_REVERSE_MUSHROOM:
				coinsToAdd = 10;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = reverseMushroomText;
				break;
        	case ITEM_POISON_MUSHROOM:
				coinsToAdd = 5;
				randItem = 0xFF;
				givenItem = 0xFF;
				afterEatingMessage = poisonMushroomText;
				break;
        	case ITEM_GOLDEN_MUSHROOM:
				coinsToAdd = 15;
				randItem = weighedRandItem();
				givenItem = ITEM_MAGIC_LAMP;
				afterEatingMessage = goldenMushroomText;
				break;
        	case ITEM_MAGIC_LAMP:
				coinsToAdd = 50;
				randItem = 0xFF;
				givenItem = 0xFF;
				afterEatingMessage = magicLampText;
				break;
        	case ITEM_BOO_BELL:
				coinsToAdd = 25;
				randItem = 0xFF;
				givenItem = 0xFF;
				afterEatingMessage = booBellText;
            	break;
        	case ITEM_SKELETON_KEY:
				coinsToAdd = 15;
				randItem = 0xFF;
				givenItem = 0xFF;
				afterEatingMessage = skeletonKeyText;
				break;
        	case ITEM_BARTER_BOX:
				coinsToAdd = 35;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = barterBoxText;
				break;
        	case ITEM_BOO_REPELLANT:
				coinsToAdd = 15;
				randItem = weighedRandItem();
				givenItem = ITEM_CELLULAR_SHOPPER;
				afterEatingMessage = booRepellantText;
				break;
        	case ITEM_BOWSER_PHONE:
				coinsToAdd = 10;
				randItem = weighedRandItem();
				givenItem = ITEM_BOO_REPELLANT;
				afterEatingMessage = bowserPhoneText;
				break;
        	case ITEM_WACKY_WATCH:
				coinsToAdd = 50;
				randItem = weighedRandItem();
				givenItem = ITEM_BOO_BELL;
				afterEatingMessage = wackyWatchText;
				break;
        	case ITEM_LUCKY_LAMP:
				coinsToAdd = 25;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = luckyLampText;
				break;
            case ITEM_PLUNDER_CHEST:
				coinsToAdd = 10;
				randItem = 0xFF;
				givenItem = ITEM_CELLULAR_SHOPPER;
				afterEatingMessage = plunderChestText;
				break;
            case ITEM_CELLULAR_SHOPPER:
				coinsToAdd = 10;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = cellularShopperText;
				break;
            case ITEM_LUCKY_COIN:
				coinsToAdd = 25;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = luckyCoinText;
				break;
            case ITEM_DUELING_GLOVE:
				coinsToAdd = 15;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = duelingGloveText;
				break;
            case ITEM_WARP_BLOCK:
				coinsToAdd = 15;
				randItem = weighedRandItem();
				givenItem = 0xFF;
				afterEatingMessage = warpBlockText;
				break;
        	case ITEM_KOOPA_CARD:
				coinsToAdd = 10;
				randItem = weighedRandItem();
				givenItem = ITEM_BOWSER_PHONE;
				afterEatingMessage = koopaCardText;
				break;
            default:
				coinsToAdd = 0;
				randItem = 0xFF;
				givenItem = 0xFF;
				afterEatingMessage = invalidItemText;
				eatBool = 1;
				//item not in above switch statement will default here
    	}
		if (eatBool == 0) {
			player->items[chosenIndex] = 0xFF;
			while (i < 2) {
				PlaySound(0x493);
				SleepProcess(15); //sleep 15 frames
				i++;
            }
        }
		FixUpPlayerItemSlots(curPlayerIndex);
		FixUpPlayerItemSlots(curPlayerIndex);
		RefreshHUD(curPlayerIndex);
		ShowMessage(-1, afterEatingMessage, 0, 0, 0, 0, 0);
		messageCleanup();

		if (coinsToAdd != 0) {
		AdjustPlayerCoinsGradual(curPlayerIndex, coinsToAdd);
		ShowPlayerCoinChange(curPlayerIndex, coinsToAdd);
    	}
    	if (randItem != 0xFF){
			player->items[chosenIndex] = randItem;
    	}
		if (givenItem != 0xFF) {
			displaymessage:
			ShowMessage(-1, replacewithlamp, 0, 0, 0, 0, 0);
			chosenIndex = GetBasicPromptSelection(2, 0); //CPUs always choose 0
			messageCleanup();
			if (chosenIndex <= 2) {
				player->items[chosenIndex] = givenItem;
            } else if (chosenIndex == 3) {
				ViewBoardMap();
				goto displaymessage;
            } else if (chosenIndex == 4) {}
    	}
		FixUpPlayerItemSlots(curPlayerIndex);
		FixUpPlayerItemSlots(curPlayerIndex);
		RefreshHUD(curPlayerIndex);
		if (eatBool == 0) {
			PlaySound (player->character + 0x2BE);
			SetBoardPlayerAnimation(-1, 5, 0);
        }
	}
}

void messageCleanup() {
    func_800EC9DC();
    CloseMessage();
    func_800EC6EC();
}

asm(
"getRand:\n"
"ADDIU sp, sp, -0x18\n"
"SW ra, 0x0010 (sp)\n"
"BLTZL a0, label1_func0\n"
"SUBU a0, r0, a0\n"
"label1_func0:\n"
"BEQZ a0, End1\n"
"DADDU v0, r0, r0\n"
"ADDIU v0, r0, 0x0001\n"
"BNEL a0, v0, label2_func0\n"
"ADDIU v0, r0, 0x0064\n"
"JAL Function1\n"
"ADDIU a0, r0, 0x03E8\n"
"SLTI v0, v0, 0x01F5\n"
"J End1\n"
"XORI v0, v0, 0x0001\n"
"label2_func0:\n"
"BEQ a0, v0, label3_func0\n"
"NOP\n"
"JAL Function1\n"
"NOP\n"
"J End1\n"
"NOP\n"
"label3_func0:\n"
"JAL Function1\n"
"ADDIU a0, r0, 0x03F1\n"
"LUI v1, 0x6666\n"
"ORI v1, v1, 0x6667\n"
"MULT v0, v1\n"
"SRA v0, v0, 31\n"
"MFHI a1\n"
"SRA v1, a1, 2\n"
"SUBU v0, v1, v0\n"
"End1:\n"
"LW ra, 0x0010 (sp)\n"
"JR RA\n"
"ADDIU sp, sp, 0x18\n"
//
"Function1:\n"
"ADDIU sp, sp, -0x20\n"
"SW s0, 0x0010 (sp)\n"
"ADDIU s0, r0, 0xFFFF\n"
"SW s1, 0x0014 (sp)\n"
"ADDIU s1, a0, 0x0001\n"
"BNEZ s1, label1_func1\n"
"DIVU s0, s1\n"
"BREAK 0x01C00\n"
"label1_func1:\n"
"MFLO s0\n"
"BNEZ s0, label2_func1\n"
"SW ra, 0x0018 (sp)\n"
"ADDIU s0, r0, 0x0001\n"
"label2_func1:\n"
"JAL Function2\n"
"NOP\n"
"BNEZ s0, label3_func1\n"
"DIVU v0, s0\n"
"BREAK 0x01C00\n"
"label3_func1:\n"
"MFLO v1\n"
"SLTU v0, v1, s1\n"
"BEQZ v0, label2_func1\n"
"DADDU v0, v1, r0\n"
"LW ra, 0x0018 (sp)\n"
"LW s1, 0x0014 (sp)\n"
"LW s0, 0x0010 (sp)\n"
"JR RA\n"
"ADDIU sp, sp, 0x20\n"
//
"Function2:\n"
"LUI v0, 0x8009\n"
"LW v0, 0x7650 (v0)\n"
"LUI v1, 0x41C6\n"
"ORI v1, v1, 0x4E6D\n"
"MULT v0, v1\n"
"MFLO v0\n"
"ADDIU v0, v0, 0x3039\n"
"LUI v1, 0x8009\n"
"JR RA\n"
"SW v0, 0x7650 (v1)\n"
);

