



var current_selected = -1;



var lock_image = new Image();
lock_image.src = "img/icons/lock.png";

var slot_texture = new Image();
slot_texture.src = "img/ui/slot_empty.png";

var fat_texture = new Image();
fat_texture.src = "img/icons/fat.png";

var metabolism_texture = new Image();
metabolism_texture.src = "img/icons/fav.png";

var capacity_texture = new Image();
capacity_texture.src = "img/icons/fullness.png";

var weight_texture = new Image();
weight_texture.src = "img/icons/weight.png";

var small_confirm = new Image();
small_confirm.src = "img/ui/small_confirm.png";

var big_confirm = new Image();
big_confirm.src = "img/ui/big_confirm.png";

var big_back= new Image();
big_back.src = "img/ui/big_back.png";

var dialog_texture = new Image();
dialog_texture.src = "img/ui/dialog_box.png";

var npc_frame = new Image();
npc_frame.src = "img/ui/npc_frame.png";

var cutscene0_button = new Image();
cutscene0_button.src = "img/ui/cutscene0.png";

var cutscene1_button = new Image();
cutscene1_button.src = "img/ui/cutscene1.png";

var availability = [];

var current_fat = 0;
var display_fat = 0;

var current_extractions = 0;
var total_extracts = 0;


UPGRADE_STATE_ENUM = {
    LEVELUP:0,
    FLOORSELECT:1,
    PARTYSELECT:2,
    RESETCONFIRM:3,

    NUM_REWARD_TYPE:4
}

var upgrade_state = UPGRADE_STATE_ENUM.LEVELUP;

var rank_icons = [];
rank_icons[RANK_ENUM.S_RANK] = new Image();
rank_icons[RANK_ENUM.S_RANK].src = "img/icons/s_rank.png";

rank_icons[RANK_ENUM.A_RANK] = new Image();
rank_icons[RANK_ENUM.A_RANK].src = "img/icons/a_rank.png";

rank_icons[RANK_ENUM.B_RANK] = new Image();
rank_icons[RANK_ENUM.B_RANK].src = "img/icons/b_rank.png";

rank_icons[RANK_ENUM.C_RANK] = new Image();
rank_icons[RANK_ENUM.C_RANK].src = "img/icons/c_rank.png";

rank_icons[RANK_ENUM.D_RANK] = new Image();
rank_icons[RANK_ENUM.D_RANK].src = "img/icons/d_rank.png";

rank_icons[RANK_ENUM.F_RANK] = new Image();
rank_icons[RANK_ENUM.F_RANK].src = "img/icons/f_rank.png";

var level_list_position = 0;

var world_pos_x = 0.0;
var world_pos_y = 0.0;

var roster_panel_anim_f = 1.0;
var character_panel_anim_f = 1.0;
var close_button_anim_f = 1.0;
var currency_panel_anim_f = 1.0;
var golevel_button_anim_f = 1.0;

var replay_start_anim_f = 1.0;
var replay_end_anim_f = 1.0;

var goprep_button_anim_f = 1.0;
var goreal_button_anim_f = 1.0;
var back_button_anim_f = 1.0;
var level_info_panel_anim_f = 1.0;
var item_panel_anim_f = 1.0;
var reset_panel_anim_f = 1.0;
var world_anim_f = 0.0;

SORT_TYPE_ENUM = {
    DEFAULT: 0,
    COST: 1,
    WEIGHT: 2,
    FOOD: 3,
    ABILITIES: 4,

    NUM_SORT_TYPE: 5
}

var sort_buttons = [];
sort_buttons[SORT_TYPE_ENUM.DEFAULT] = new Image();
sort_buttons[SORT_TYPE_ENUM.DEFAULT].src = "img/ui/sort_default.png";

sort_buttons[SORT_TYPE_ENUM.COST] = new Image();
sort_buttons[SORT_TYPE_ENUM.COST].src = "img/ui/sort_cost.png";

sort_buttons[SORT_TYPE_ENUM.WEIGHT] = new Image();
sort_buttons[SORT_TYPE_ENUM.WEIGHT].src = "img/ui/sort_weight.png";

sort_buttons[SORT_TYPE_ENUM.FOOD] = new Image();
sort_buttons[SORT_TYPE_ENUM.FOOD].src = "img/ui/sort_food.png";

sort_buttons[SORT_TYPE_ENUM.ABILITIES] = new Image();
sort_buttons[SORT_TYPE_ENUM.ABILITIES].src = "img/ui/sort_ability.png";


var current_sort_type = SORT_TYPE_ENUM.DEFAULT;
var current_sort_counter = 0;

var sort_arrow = new Image();
sort_arrow.src = "img/ui/sort_arrow.png";


/*ABILITY_TOOLTIPS_ENUM = {
    NONE: 0,
    POWER: 1,
    SPEED: 2,
    METABOLISM: 3,
    CAPACITY: 4,

    NUM_ABILITY_TOOLTIPS:5

}

var ability_tooltip_state = ABILITY_TOOLTIPS_ENUM.NONE;*/


var upgrade_tutorial_messages =
    [
"Extract to return an adventurer to their starting size and convert all their weight into XP",
"To begin with, you can extract only once. Every time you beat a level, you get your extractions back",
        "Spend XP to level up your adventurer's stats",
        "Power increases your ability strength. Strength gained from being fat is capped at twice the power stat",
        "Speed reduces the cooldown penalty from being fat",
        "Metabolism speeds up digestion, making them get fat quicker and recover from KO faster",
        "Capacity increases the maximum size of the fullness gauge, and makes them less likely to be KO'd"
];

var upgrade_tutorial_titles =
    [
        "Acquiring XP",
        "Extraction",
        "Leveling up",
        "Power",
        "Speed",
        "Metabolism",
        "Capacity"
    ];

var upgrade_tutorial_images = {};
upgrade_tutorial_images[0] = new Image;
upgrade_tutorial_images[1] = new Image;
upgrade_tutorial_images[2] = new Image;
upgrade_tutorial_images[3] = new Image;
upgrade_tutorial_images[4] = new Image;
upgrade_tutorial_images[5] = new Image;
upgrade_tutorial_images[6] = new Image;

upgrade_tutorial_images[0].src = "img/tut/tut17.png";
upgrade_tutorial_images[1].src = "img/tut/tut18.png";
upgrade_tutorial_images[2].src = "img/tut/tut19.png";
upgrade_tutorial_images[3].src = "img/tut/tut20.png";
upgrade_tutorial_images[4].src = "img/tut/tut21.png";
upgrade_tutorial_images[5].src = "img/tut/tut22.png";
upgrade_tutorial_images[6].src = "img/tut/tut23.png";

var advance_tutorial = false;
var tutorial_index = 0;


var dialog_queue = [];
//var current_dialog;
var dialog_enter_mode = true;
var dialog_enter_timer = 0.0;

var portrait_list = new Set();
var switch_gamestate_flag = false;
var sorted_adventurers = [];

var sorted_adventurer_pos = [];

//this helps us track if the level we just did was completed prior to entering it
//if the player is returning from a level they had not completed, and they just completed it for the first time,
//then we will search for the next available level.
//otherwise, we will retain the current level.
var was_level_incomplete = true;

function init_upgrade_gamestate()
{
    for (var i = 0; i < 21; ++i)
    { 
        sorted_adventurers[i] = i; //default sorting order
        sorted_adventurer_pos[i] = {};
        sorted_adventurer_pos[i].x = - CANVAS_WIDTH;
        sorted_adventurer_pos[i].y = .5 * CANVAS_HEIGHT;
    }

    velocity.x = 0.0;
    velocity.y = 0.0;
    tutorial_lock_dismissal_timer = 0.0;

    //switch_gamestate_flag = false;
    switch_bgm(SONG_ENUM.BGM_WORLD);

    if (__level_completed[0] && !__level_completed[1]) {
        tutorial_mode = true;
        tutorial_index = 0;
    }

    torial_lock_dismissal_timer = 0.0;

     roster_panel_anim_f = 1.0;
     character_panel_anim_f = 1.0;
     close_button_anim_f = 1.0;
     currency_panel_anim_f = 1.0;
     golevel_button_anim_f = 1.0;
     goprep_button_anim_f = 1.0;
     goreal_button_anim_f = 1.0;
     back_button_anim_f = 1.0;
     level_info_panel_anim_f = 1.0;
     item_panel_anim_f = 1.0;
     reset_panel_anim_f = 1.0;
     world_anim_f = 0.0;

    if (__level_completed[current_level] && was_level_incomplete)//this is the first time clearing this level
    {
        previous_level = current_level;
        current_level = -1; //invalid

        //find a level that has the previous level as a prereq
        for (var i in __floor_list) { //for each level

            for (var j in __floor_list[previous_level].stage_prereq) {
                if (__floor_list[i].stage_prereq[j] == previous_level)//if previous level is contained in this level's prereqs
                {
                    var prereqs_met = true;
                    for (var j in __floor_list[i].stage_prereq) {
                        if (!__level_completed[__floor_list[i].stage_prereq[j]])
                            prereqs_met = false;
                    }

                    if (!__level_completed[i] && prereqs_met) {
                        current_level = i;//if the prereqs are met and the level has not been completed, select it
                    }
                }
            }

            if (current_level == -1) {
                for (var i in __floor_list) {
                    var prereqs_met = true;
                    for (var j in __floor_list[i].stage_prereq) {
                        if (!__level_completed[__floor_list[i].stage_prereq[j]])
                            prereqs_met = false;
                    }


                    if (!__level_completed[i] && prereqs_met) {
                        current_level = i;

                        //world_pos_y = -y;// + CANVAS_HEIGHT * .5;

                    }
                }
            }

            if (current_level == -1)
                for (var i in __floor_list) {
                    current_level = i;
                }
        }
    }



    var x = __floor_list[current_level].world_position_x * CANVAS_WIDTH / 1700.0;
    var y = __floor_list[current_level].world_position_y * CANVAS_WIDTH / 1700.0;

    world_pos_x = .5 * TRUE_WORLD_MAP_WIDTH * CANVAS_WIDTH / 1700.0 - x + CANVAS_WIDTH * .33;
    world_pos_y = .5 * TRUE_WORLD_MAP_HEIGHT * CANVAS_WIDTH / 1700.0 - y + CANVAS_HEIGHT * .5;

    /*
    if(unlocked_levels > NUM_LEVELS - 1) 
    unlocked_levels = NUM_LEVELS - 1;

    current_level = unlocked_levels;

    level_list_position = unlocked_levels - 7;
    if(level_list_position < 0)
        level_list_position = 0;
    */

    //__level_completed[0] = true;

    upgrade_state = UPGRADE_STATE_ENUM.LEVELUP;

    availability = [];
    for(var i in __adventurer_list)
    {
        availability[i] = true;
    }
    
    //save();


}

function compare_stat_prevelance(a,b) {
    if (a.strength < b.strength)
      return 1;
    if (a.strength > b.strength)
      return -1;
    return 0;
  }


var world_map = new Image();
world_map.src = "img/ui/worldmap.png";


var roster_panel = new Image();
roster_panel.src = "img/ui/roster_panel.png";
var currency_panel = new Image();
currency_panel.src = "img/ui/currency_panel.png";
var item_panel = new Image();
item_panel.src = "img/ui/item_panel.png";
var info_panel = new Image();
info_panel.src = "img/ui/info_panel.png";
var reset_warning = new Image();
reset_warning.src = "img/ui/reset_warning.png";

var plus_button = new Image();
plus_button.src = "img/ui/plus_button.png";
var info_button = new Image();
info_button.src = "img/ui/info_button.png";

var reset_button = new Image();
reset_button.src = "img/ui/reset_button.png";
var extract_button = new Image();
extract_button.src = "img/ui/extract_button.png";
var close_button = new Image();
close_button.src = "img/ui/close_button.png";
var golevel_button = new Image();
golevel_button.src = "img/ui/golevel_button.png";
var go_button = new Image();
go_button.src = "img/ui/go_button.png";
var back_button = new Image();
back_button.src = "img/ui/back_button.png";
var no_button = new Image();
no_button.src = "img/ui/no_button.png";
var yes_button = new Image();
yes_button.src = "img/ui/yes_button.png";


var plus_button_disable = new Image();
plus_button_disable.src = "img/ui/plus_button_disable.png";
var reset_button_disable = new Image();
reset_button_disable.src = "img/ui/reset_button_disable.png";
var extract_button_disable = new Image();
extract_button_disable.src = "img/ui/extract_button_disable.png";

var nameplate_tex = new Image();
nameplate_tex.src = "img/ui/stage_nameplate.png";

var TRUE_WORLD_MAP_WIDTH = 6480;
var TRUE_WORLD_MAP_HEIGHT = 4320;

//var pos_x = 0;


var old_mouse_pos_x = 0.0;
var old_mouse_pos_y = 0.0;
var map_scroll_mode = false;
var current_dialog_index = 0;

function enter_cutscene(index)
{
    current_dialog_index = index;
    if (index == 0)
        dialog_queue = __floor_list[current_level].dialog0.slice();
    else
        dialog_queue = __floor_list[current_level].dialog1.slice();

    if (dialog_queue.length == 0)
        return;

    if (!dialog_queue[0].second)
        dialog_queue[0].active = dialog_queue[0].first;
    else if (npcs[dialog_queue[0].first.npc_id].adventurer && __adventurer_list[npcs[dialog_queue[0].first.npc_id].adventurer_index].unlocked)
        dialog_queue[0].active = dialog_queue[0].first
    else if (!npcs[dialog_queue[0].first.npc_id].adventurer)
        dialog_queue[0].active = dialog_queue[0].first
    else
        dialog_queue[0].active = dialog_queue[0].second;
    

    if (npcs[dialog_queue[0].first.npc_id].adventurer || npcs[dialog_queue[0].active.npc_id].name.length > 0)
    portrait_list.add(dialog_queue[0].active.npc_id);
}

var velocity = {};

var indicator = new Image();
indicator.src = "img/ui/glow_indicator.png";

function update_upgrade_gamestate()
{

    var WORLD_MAP_WIDTH = TRUE_WORLD_MAP_WIDTH * CANVAS_WIDTH / 1700.0;
    var WORLD_MAP_HEIGHT = TRUE_WORLD_MAP_HEIGHT * CANVAS_WIDTH / 1700.0;

    if (upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT && dialog_queue == 0 && !tutorial_mode)
        world_anim_f += 4.0 * get_frame_time();
    else
        world_anim_f -= 4.0 * get_frame_time();
    world_anim_f = clamp(world_anim_f, 0.5, 1.0);

    if (INPUT.left_pressed && upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT && dialog_queue == 0 && !tutorial_mode)
    {
        if(!map_scroll_mode)
        {
            old_mouse_pos_x = INPUT.mouse_screen.x;
            old_mouse_pos_y = INPUT.mouse_screen.y;
        }
        map_scroll_mode = true;
    }
    else
        map_scroll_mode = false;

    //move map with mouse
    if (map_scroll_mode) {
        world_pos_x += INPUT.mouse_screen.x - old_mouse_pos_x;
        world_pos_y += INPUT.mouse_screen.y - old_mouse_pos_y;


        velocity.x = INPUT.mouse_screen.x - old_mouse_pos_x;
        velocity.y = INPUT.mouse_screen.y - old_mouse_pos_y;


        old_mouse_pos_x = INPUT.mouse_screen.x;
        old_mouse_pos_y = INPUT.mouse_screen.y;
    }
    else //otherwise, apply velocity/acceleration
    {
        world_pos_x += velocity.x;
        world_pos_y += velocity.y;

        velocity.x *= .95;
        velocity.y *= .95;
    }

    //map bounds
    /*var max_x = __floor_list[0].world_position_x * CANVAS_WIDTH / 1700.0;//.5 * WORLD_MAP_WIDTH;
    var min_x = __floor_list[0].world_position_x * CANVAS_WIDTH / 1700.0;//CANVAS_WIDTH - .5 * WORLD_MAP_WIDTH;
    var max_y = __floor_list[0].world_position_y * CANVAS_WIDTH / 1700.0;//.5 * WORLD_MAP_WIDTH;
    var min_y = __floor_list[0].world_position_y * CANVAS_WIDTH / 1700.0;//CANVAS_WIDTH - .5 * WORLD_MAP_WIDTH;
    for (var i in __floor_list) {

        if (__level_completed[__floor_list[i].stage_prereq]) {
            var x = __floor_list[i].world_position_x * CANVAS_WIDTH / 1700.0;
            if (x < min_x)
                min_x = x;
            if (x > max_x)
                max_x = x;


            var y = __floor_list[i].world_position_y * CANVAS_WIDTH / 1700.0;
            if (y < min_y)
                min_y = y;
            if (y > max_y)
                max_y = y;
        }
    }

    if (world_pos_x > max_x - min_x - .5 * CANVAS_WIDTH)
        world_pos_x = max_x - min_x - .5 * CANVAS_WIDTH;
    if (world_pos_x < 2 * CANVAS_WIDTH - .5 * (max_x + min_x) + .5 * CANVAS_WIDTH)
    world_pos_x = 2 * CANVAS_WIDTH - .5 * (max_x + min_x) + .5 * CANVAS_WIDTH;

    if (world_pos_y > CANVAS_HEIGHT + .5 * (max_y + min_y))// + .5 * CANVAS_HEIGHT)
        world_pos_y = CANVAS_HEIGHT + .5 * (max_y + min_y);// + .5 * CANVAS_HEIGHT;
    //if (world_pos_y < -(max_y - min_y))// - .5 * CANVAS_HEIGHT)
    //world_pos_y = -(max_y - min_y);// - .5 * CANVAS_HEIGHT;
    */
    //maximum bounds
    //*
    if (world_pos_x > .5 * WORLD_MAP_WIDTH )
        world_pos_x = .5 * (WORLD_MAP_WIDTH );
    if (world_pos_x < CANVAS_WIDTH - .5 * (WORLD_MAP_WIDTH))
        world_pos_x = CANVAS_WIDTH - .5 * (WORLD_MAP_WIDTH);


    if (world_pos_y > .5 * WORLD_MAP_HEIGHT)
        world_pos_y = .5 * WORLD_MAP_HEIGHT;
    if (world_pos_y < CANVAS_HEIGHT - .5 * WORLD_MAP_HEIGHT)
        world_pos_y = CANVAS_HEIGHT - .5 * WORLD_MAP_HEIGHT;
    //*/

    draw_center(world_map, world_pos_x, world_pos_y, WORLD_MAP_WIDTH, WORLD_MAP_HEIGHT, world_anim_f);

    //draw_text(old_mouse_pos_x + ", " + old_mouse_pos_y, 10, 20, 20);
    //draw_text(world_pos_x + ", " + world_pos_y, 10, 50, 20);

    for (var i in __floor_list) {

        var prereqs_met = true;
        for (var k in __floor_list[i].stage_prereq) {
            if (!__level_completed[__floor_list[i].stage_prereq[k]])
                prereqs_met = false;
        }

        if (prereqs_met)
        {
            for(var j in __floor_list[i].stage_prereq)
            {
            
                var x0 = world_pos_x - .5 * WORLD_MAP_WIDTH + __floor_list[i].world_position_x * CANVAS_WIDTH / 1700.0;
                var y0 = world_pos_y - .5 * WORLD_MAP_HEIGHT + __floor_list[i].world_position_y * CANVAS_WIDTH / 1700.0;

                var x1 = world_pos_x - .5 * WORLD_MAP_WIDTH + __floor_list[__floor_list[i].stage_prereq[j]].world_position_x * CANVAS_WIDTH / 1700.0;
                var y1 = world_pos_y - .5 * WORLD_MAP_HEIGHT + __floor_list[__floor_list[i].stage_prereq[j]].world_position_y * CANVAS_WIDTH / 1700.0;

                context.setLineDash([10, 6]);/*dashes are 5px and spaces are 3px*/
                context.beginPath();
                context.moveTo(x0, y0);
                context.lineTo(x1, y1);
                context.lineWidth = 10;
                context.strokeStyle = "#FF0000";
                context.stroke();
                context.setLineDash([]);/*dashes are 5px and spaces are 3px*/
            
            }
        }
    }

    for (var j = 0; j < 2; ++j)
    {
        for (var i in __floor_list)
        {
            if (j == 1 && current_level != i)
                continue;
            if (j == 0 && current_level == i)
                continue;

            var prereqs_met = true;
            for (var k in __floor_list[i].stage_prereq) {
                if (!__level_completed[__floor_list[i].stage_prereq[k]])
                    prereqs_met = false;
            }

            if (prereqs_met) {
                //console.log(__floor_list[i].name);
                //if(i + level_list_position > unlocked_levels)
                //continue;


                //if(current_level == i + level_list_position)
                //draw_center(big_confirm, 50* frame_scale + CANVAS_WIDTH - pos_x, 100* frame_scale + 50 * i* frame_scale, 40* frame_scale, 40* frame_scale);
                var x = world_pos_x - .5 * WORLD_MAP_WIDTH + __floor_list[i].world_position_x * CANVAS_WIDTH / 1700.0;
                var y = world_pos_y - .5 * WORLD_MAP_HEIGHT + __floor_list[i].world_position_y * CANVAS_WIDTH / 1700.0;
                //console.log(y);

                var mult = 1.0 + .5 * j;
                if (upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT &&
                    dialog_queue == 0 &&
                    !tutorial_mode &&
                    point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, 1.2 * 108.0 * CANVAS_WIDTH / 1700.0, 1.2 * 72.0 * CANVAS_WIDTH / 1700.0)
                    && (x < .7 * canvas.width && y < y < .8 * canvas.height) )
                    
                    {
                    mult += .2;
                    if (INPUT.left_triggered) {
                        current_level = i;
                        button_sfx.play();
                    }
                }

                var alph = 1.0;
                if (!__level_completed[i])
                    alph = .75;

                draw_center(__floor_list[i].icon,
                    x,
                    y,
                    mult * 150.0 * CANVAS_WIDTH / 1700.0, mult * 150.0 * CANVAS_WIDTH / 1700.0,alph);

                var tw = mult * 25.0 * CANVAS_WIDTH / 1700.0;

                draw_center(nameplate_tex,
                    x,
                    y + mult * 38.5 * CANVAS_WIDTH / 1700.0,
                    mult * 220.0 * CANVAS_WIDTH / 1700.0, mult * 35.0 * CANVAS_WIDTH / 1700.0);

                draw_text(__floor_list[i].name, x - .2 * tw * __floor_list[i].name.length, y + mult * 50.0 * CANVAS_WIDTH / 1700.0, tw)

                //*
                if (__level_completed[i])
                {
                    var draw_f = true;
                    for (var k in __floor_list[i].clear_times)
                    {
                        if (best_times[i] < __floor_list[i].clear_times[k])
                        {
                            draw_f = false;
                            draw_center(rank_icons[k], x, y + mult * 70.0 * CANVAS_WIDTH / 1700.0,
                                mult * 70.0 * CANVAS_WIDTH / 1700.0,
                                mult * 70.0 * CANVAS_WIDTH / 1700.0);
                            break;
                        }
                    }
                    if (draw_f) {
                        draw_center(rank_icons[5], x, y + mult * 70.0 * CANVAS_WIDTH / 1700.0,
                            mult * 70.0 * CANVAS_WIDTH / 1700.0,
                            mult * 70.0 * CANVAS_WIDTH / 1700.0);
                    }
                }
                //*/
            }
        }
    }

    var slot_size = 100 * frame_scale;
    var margin = 25 * frame_scale;

    
    for(var i in __adventurer_list)
    {
        availability[i] = true;
    }

    /*
    if(upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT)
    {
        if(Math.abs(pos_x - CANVAS_WIDTH) < CANVAS_WIDTH * 7.0 * get_frame_time())
            pos_x = CANVAS_WIDTH;    
        else if(pos_x < CANVAS_WIDTH)
            pos_x += CANVAS_WIDTH * 6.0 * get_frame_time();
        else if(pos_x > CANVAS_WIDTH)
                pos_x -= CANVAS_WIDTH * 6.0 * get_frame_time();
    }
    else if(upgrade_state == UPGRADE_STATE_ENUM.LEVELUP)
    {
        if(pos_x > 0)
            pos_x -= CANVAS_WIDTH * 6.0 * get_frame_time();
        else
            pos_x = 0;   
    }
    else if(upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT)
    {
        if(pos_x < 1.525 * CANVAS_WIDTH)
            pos_x += CANVAS_WIDTH * 6.0 * get_frame_time();
        else
            pos_x = 1.525 * CANVAS_WIDTH;   
    }
    //*/
     //draw_text("Level Up", 0 - pos_x, 80 * frame_scale, 75* frame_scale);
  



    //current party slots
    for(var i in adventurer_field)
    {

    }

    /*
    for(var i in current_party)
    {
        var x = CANVAS_WIDTH - (slot_size + margin) * 1.5 + (slot_size + margin) * (i % 2) - pos_x + CANVAS_WIDTH;
        var y = (margin + slot_size) * (.5 + Math.floor(i / 2));

        var bw = slot_size / 2;
        var bh = (150 / 250) * bw;

        if(current_party[i] == -1)
            draw_center(slot_texture, x, y, slot_size, slot_size);
        else
        {
            availability[current_party[i]] = false;
            __adventurer_list[current_party[i]].draw(x, y, slot_size,slot_size);

            if(INPUT.left_triggered)
            {
                if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x,y, slot_size, slot_size - bh))
                {
                    debugger;
                    current_selected = current_party[i];
                    current_party[i] = -1;
                }
            }
        }

        if(current_selected != -1 && !INPUT.left_pressed)
        {
            if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, slot_size, slot_size))
              current_party[i] = current_selected;
        }
    }
    */

    //show all adventurers

    if ((upgrade_state == UPGRADE_STATE_ENUM.LEVELUP || upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT) && dialog_queue == 0 && !tutorial_mode)
        roster_panel_anim_f -= 4.0 * get_frame_time();
    else
        roster_panel_anim_f += 4.0 * get_frame_time();
    roster_panel_anim_f = clamp(roster_panel_anim_f, 0.0, 1.0);

    var rf = roster_panel_anim_f * roster_panel_anim_f * roster_panel_anim_f * roster_panel_anim_f;
    draw_center(roster_panel, CANVAS_WIDTH * .25 - rf * CANVAS_WIDTH, CANVAS_HEIGHT * .5, CANVAS_WIDTH * .5, CANVAS_HEIGHT);

    //counter refers to the position in the adventurer select UI
    //sorted_adventurers[] is indexed by counter, and returns an adventurer id in __adventurer_list[]
    //current_selected stores an index in __adventurer_list[]
    //sorted_adventurer_pos[] is indexed by __adventurer_list[].id
    var counter = 0;
    while(counter < 21)
    {
        var goal_x = CANVAS_WIDTH * (0.75 / 13.0) + .5 * (2.0 / 13.0) * CANVAS_WIDTH * (counter % 6) - rf * CANVAS_WIDTH;
        var goal_y = CANVAS_HEIGHT * .2 + (300.0 / 270.0) * .09 * CANVAS_WIDTH * (Math.floor(counter / 6) - .5);
        var w = .5 * CANVAS_WIDTH / 7.0;
        var alpha = 1.0;

        sorted_adventurer_pos[sorted_adventurers[counter]].x = .875 * sorted_adventurer_pos[sorted_adventurers[counter]].x + .125 * goal_x;
        sorted_adventurer_pos[sorted_adventurers[counter]].y = .875 * sorted_adventurer_pos[sorted_adventurers[counter]].y + .125 * goal_y;

        var x = sorted_adventurer_pos[sorted_adventurers[counter]].x;
        var y = sorted_adventurer_pos[sorted_adventurers[counter]].y;

        var current_party_index = -1;
        if (upgrade_state == UPGRADE_STATE_ENUM.LEVELUP && dialog_queue == 0 && !tutorial_mode)
        {
            if (current_selected != sorted_adventurers[counter])
                alpha = 0.5;
        }
        else
        {
            for (var i = 0; i < 4; ++i)
            {
                if (adventurer_field[i] && adventurer_field[i].id == __adventurer_list[sorted_adventurers[counter]].id)
                    current_party_index = i;
            }

            if (current_party_index == -1)
                alpha = 0.5;
            else
                draw_center(indicator, x, y, 1.15 * w, 1.15 * (300.0 / 270.0) * w, 1.0);
        }


        if (!__adventurer_list[sorted_adventurers[counter]].unlocked)
        {
            draw_center(lock_image, x,y, .5 * w, .5 * w, 1.0);
            alpha = .1;
        }
        else
        {
            if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, w, (300.0 / 270.0) * w))
            {
                
                w *= 1.05;
                //h *= 1.05;
            

            if(INPUT.left_triggered)
            {
                if (upgrade_state == UPGRADE_STATE_ENUM.LEVELUP && dialog_queue == 0 && !tutorial_mode)
                {
                    if (point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, .125 * CANVAS_WIDTH, (300.0 / 270.0) * .125 * CANVAS_WIDTH)) {
                        current_selected = sorted_adventurers[counter];
                        button_sfx.play();
                    }
                }
                else
                {

                    button_sfx.play();
                                      

                        //current_selected = counter + current_display_index;

                        if (current_party_index == -1) 
                        {
                            for (var i = 0; i < 4; ++i) 
                            {
                                if (!adventurer_field[i]) 
                                {
                                    adventurer_field[i] = __adventurer_list[sorted_adventurers[counter]];
                                    break;
                                }
                            }
                        }
                        else 
                        {
                            for (var i = 0; i < 4; ++i) 
                            {
                                if (adventurer_field[i] && adventurer_field[i].id == __adventurer_list[sorted_adventurers[counter]].id)
                                    adventurer_field[i] = null;
                            }


                        }
                    }
                }
                
            }
        }

        __adventurer_list[sorted_adventurers[counter]].draw_small(x, y, w, alpha);



        counter += 1;

        
    }

    //default
    for (var i = 0; i < SORT_TYPE_ENUM.NUM_SORT_TYPE; ++i)
    {
        if (draw_button(sort_buttons[i], -123 - i,
            (.5 + i)  * (.5 * .5 * .5 * .225 * CANVAS_WIDTH + frame_scale * 10) - Math.pow( rf, i+1) * CANVAS_WIDTH,
            CANVAS_HEIGHT - 135 * frame_scale - 10,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH))//, 250, 150))
        {
            current_sort_type = i;
            ++current_sort_counter;

            //sort by weight
            var sorted = [];
            var counter = 0;
            for (var j = 0; j < 21; ++j)
            {
                if (__adventurer_list[j].unlocked)
                {
                    sorted[counter] = {};
                    sorted[counter].id = j;
                    sorted[counter].strength = 0;
                    ++counter;
                }
            }


            if (current_sort_type == SORT_TYPE_ENUM.DEFAULT) {

                for (var j in sorted)//for (var j = 0; j < 21; ++j) {
                    sorted[j].strength = sorted[j].id;
                //}


            }
            else if (current_sort_type == SORT_TYPE_ENUM.COST) {

                for (var j in sorted)//for (var j = 0; j < 21; ++j) {
                    sorted[j].strength = __adventurer_list[sorted[j].id].level +
                        __adventurer_list[sorted[j].id].stomach_level +
                        __adventurer_list[sorted[j].id].metabolism +
                        __adventurer_list[sorted[j].id].mobility;
                    //sorted[__adventurer_list[j].id].id = __adventurer_list[j].id;
                //}

                
            }
            else if (current_sort_type == SORT_TYPE_ENUM.WEIGHT) {

                for (var j in sorted)//for (var j = 0; j < 21; ++j) {
                    sorted[j].strength = __adventurer_list[sorted[j].id].current_stats[STAT_TYPE_ENUM.WEIGHT];
                    //sorted[__adventurer_list[j].id].id = __adventurer_list[j].id;
                //}


            }
            else if (current_sort_type == SORT_TYPE_ENUM.FOOD) {

                for (var j in sorted)//for (var j = 0; j < 21; ++j) {
                    sorted[j].strength = __adventurer_list[sorted[j].id].favorite_food;
                    //sorted[__adventurer_list[j].id].id = __adventurer_list[j].id;
                //}


            }
            else if (current_sort_type == SORT_TYPE_ENUM.ABILITIES) {
                
                for (var j in sorted)//for (var j = 0; j < 21; ++j) {

                    sorted[j].strength = __adventurer_list[sorted[j].id].get_actual_stat(current_sort_counter % NUM_ATTACKS_ABILITIES);
                        //sorted[__adventurer_list[j].id].id = __adventurer_list[j].id;
                    //}
                //}

            }


            sorted.sort(compare_stat_prevelance);

                if (current_sort_type != SORT_TYPE_ENUM.ABILITIES && current_sort_counter % 2 == 0)
                sorted.reverse();


            for (var j = 0; j < 21; ++j)
                if (!__adventurer_list[j].unlocked) {
                    sorted[sorted.length] = {};
                    sorted[sorted.length-1].id = j;
                    sorted[sorted.length-1].strength = 0;
                }


            for (var j = 0; j < 21; ++j)
            sorted_adventurers[j] = sorted[j].id;
        }
    }


    if (current_sort_type != SORT_TYPE_ENUM.ABILITIES)
    draw_center_rotation(sort_arrow,
        (.5 + SORT_TYPE_ENUM.NUM_SORT_TYPE) * (.5 * .5 * .5 * .225 * CANVAS_WIDTH + frame_scale * 10) - Math.pow(rf, 7) * CANVAS_WIDTH,
        CANVAS_HEIGHT - 135 * frame_scale - 10,
        .5 * .5 * .5 * .225 * CANVAS_WIDTH,
        .5 * .5 * .5 * .225 * CANVAS_WIDTH, 3.14 * (current_sort_counter % 2));

    var sort_label = "INVALID_SORT";
    if (current_sort_type == SORT_TYPE_ENUM.DEFAULT)
        sort_label = "Sort: Default";
    else if (current_sort_type == SORT_TYPE_ENUM.COST)
        sort_label = "Sort: Level Up Cost";
    else if (current_sort_type == SORT_TYPE_ENUM.WEIGHT)
        sort_label = "Sort: Weight";
    else if (current_sort_type == SORT_TYPE_ENUM.FOOD)
        sort_label = "Sort: Favorite Food";
    else if (current_sort_type == SORT_TYPE_ENUM.ABILITIES) {

        if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.WEAPON)
            sort_label = "Sort: Weapon";
        else if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.MAGIC)
            sort_label = "Sort: Magic";
        else if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.FAITH)
            sort_label = "Sort: Faith";
        else if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.SNEAK)
            sort_label = "Sort: Sneak";
        else if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.HEAL)
            sort_label = "Sort: Heal";
        else if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.BUFF)
            sort_label = "Sort: Buff";
        else if (current_sort_counter % NUM_ATTACKS_ABILITIES == STAT_TYPE_ENUM.ARMOR)
            sort_label = "Sort: Armor";
    }

    draw_text(sort_label,
        (1 + SORT_TYPE_ENUM.NUM_SORT_TYPE) * (.5 * .5 * .5 * .225 * CANVAS_WIDTH + frame_scale * 10) - Math.pow(rf, 8) * CANVAS_WIDTH,
        CANVAS_HEIGHT - 135 * frame_scale - 10 + .5 * .5 * .5 * .225 * CANVAS_WIDTH * .4,
        .5 * .5 * .5 * .225 * CANVAS_WIDTH)


    if (upgrade_state == UPGRADE_STATE_ENUM.RESETCONFIRM && dialog_queue == 0 && !tutorial_mode)
        reset_panel_anim_f -= 2.0 * get_frame_time();
    else
        reset_panel_anim_f += 4.0 * get_frame_time();
    reset_panel_anim_f = clamp(reset_panel_anim_f, 0.0, 1.0);
        
    var recf = reset_panel_anim_f * reset_panel_anim_f * reset_panel_anim_f;


    draw_center(reset_warning, CANVAS_WIDTH * .5, CANVAS_HEIGHT * .5 + recf * CANVAS_WIDTH, CANVAS_WIDTH * .33, 462.0 / 600.0 * CANVAS_WIDTH * .33);

    if(draw_button( yes_button, -789, 
            (.5 - .075) * CANVAS_WIDTH - recf * CANVAS_WIDTH,
            (3.75 / 6.0) * CANVAS_HEIGHT,
            .5 * .2 * CANVAS_WIDTH, .5 * (126.0 / 200.0) * .2 * CANVAS_WIDTH, ""))
    {
        reset_sfx.play();
        while (__adventurer_list[current_selected].level +
     __adventurer_list[current_selected].mobility +
     __adventurer_list[current_selected].metabolism +
     __adventurer_list[current_selected].stomach_level > 4) {
            current_fat += 5 * (__adventurer_list[current_selected].level +
                __adventurer_list[current_selected].mobility +
                __adventurer_list[current_selected].metabolism +
                __adventurer_list[current_selected].stomach_level - 1);

            if (__adventurer_list[current_selected].level > 1)
                __adventurer_list[current_selected].level -= 1;

            else if (__adventurer_list[current_selected].mobility > 1)
                __adventurer_list[current_selected].mobility -= 1;

            else if (__adventurer_list[current_selected].metabolism > 1)
                __adventurer_list[current_selected].metabolism -= 1;

            else if (__adventurer_list[current_selected].stomach_level > 1)
                __adventurer_list[current_selected].stomach_level -= 1;

        }

        upgrade_state = UPGRADE_STATE_ENUM.LEVELUP;
    }

    if (draw_button(no_button, -654,
        (.5 + .075) * CANVAS_WIDTH + recf * CANVAS_WIDTH,
        (3.75 / 6.0) * CANVAS_HEIGHT,
        .5 * .2 * CANVAS_WIDTH, .5 * (126.0 / 200.0) * .2 * CANVAS_WIDTH, "")) {

        upgrade_state = UPGRADE_STATE_ENUM.LEVELUP;
    }

    if (upgrade_state == UPGRADE_STATE_ENUM.LEVELUP && dialog_queue.length == 0 && !tutorial_mode)
        character_panel_anim_f -= 2.5 * get_frame_time();
    else
        character_panel_anim_f += 2.5 * get_frame_time();
    character_panel_anim_f = clamp(character_panel_anim_f, 0.0, 1.0);

    var cf = character_panel_anim_f * character_panel_anim_f * character_panel_anim_f;


    draw_center(roster_panel, CANVAS_WIDTH * .75 + cf * CANVAS_WIDTH, CANVAS_HEIGHT * .5, CANVAS_WIDTH * .5, CANVAS_HEIGHT);
//show currently selected
    if(current_selected != -1)// && upgrade_state == UPGRADE_STATE_ENUM.LEVELUP)
    {
        var off = cf * CANVAS_WIDTH;

        draw_text(__adventurer_list[current_selected].name, .525 * CANVAS_WIDTH + off,
            .125 * CANVAS_HEIGHT, .1 * CANVAS_HEIGHT);

        __adventurer_list[current_selected].draw_small((.51 + .275 * .525) * CANVAS_WIDTH + off, .45 * CANVAS_HEIGHT, .5 * (.525) * CANVAS_WIDTH, 1.0);


        var price =
        (__adventurer_list[current_selected].level +
            __adventurer_list[current_selected].stomach_level +
            __adventurer_list[current_selected].metabolism +
            __adventurer_list[current_selected].mobility) * 10;

        draw_text("LEVEL UP COST:",
        .875 * CANVAS_WIDTH + off,
        .075 * CANVAS_HEIGHT, .04 * CANVAS_HEIGHT);
        draw_text(price.toString(),
        .925 * CANVAS_WIDTH + off,
        .125 * CANVAS_HEIGHT, .04 * CANVAS_HEIGHT);

        draw_center(fat_texture,//price.toString(),
        .905 * CANVAS_WIDTH + off,
        .11 * CANVAS_HEIGHT, .04 * CANVAS_HEIGHT, .04 * CANVAS_HEIGHT);

        if (draw_button(reset_button, -6,

            (.5 + .5 * .5 + .25 * .8) * CANVAS_WIDTH + off,
            (4.0/6.0) * CANVAS_HEIGHT,

            .5 * .15 * CANVAS_WIDTH, .5 * (12.0 / 20.0) * .15 * CANVAS_WIDTH, "",
            undefined, price == 40, reset_button_disable))//*/

            {
            upgrade_state = UPGRADE_STATE_ENUM.RESETCONFIRM;

            }

        //*EXTRACT

        var disable = current_extractions > 0;
        
        if(draw_button( extract_button, -1, 
            (.5 + .5 * .5 + .25 * .4) * CANVAS_WIDTH + off,
            (4.0 / 6.0) * CANVAS_HEIGHT,

            .5 * .2 * CANVAS_WIDTH, .5 * (12.0 / 20.0) * .2 * CANVAS_WIDTH, "",
            undefined, disable, extract_button_disable))//*/
        {
            extract_sfx.play();
            current_extractions += 1;
            current_fat += __adventurer_list[current_selected].current_stats[STAT_TYPE_ENUM.WEIGHT];
            __adventurer_list[current_selected].current_stats[STAT_TYPE_ENUM.WEIGHT] = 0;
            save();
        }

        //if (INPUT.left_triggered)
            //ability_tooltip_state = ABILITY_TOOLTIPS_ENUM.NONE;


        var price = 
        (__adventurer_list[current_selected].level + 
            __adventurer_list[current_selected].stomach_level + 
            __adventurer_list[current_selected].metabolism + 
            __adventurer_list[current_selected].mobility) * 10;

        var disable = current_fat < price;

 //POWER
        draw_center(level_texture,
            (.76 + .25 * .25) * CANVAS_WIDTH + off,
            (4.0 / 6.0) * CANVAS_HEIGHT - 4 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH
        )
        draw_text("Power:",
        (.76 + .25 * .35) * CANVAS_WIDTH + off,
            (3.9 / 6.0) * CANVAS_HEIGHT - 4 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH
        )
        draw_text(__adventurer_list[current_selected].level.toString(),
        (.95) * CANVAS_WIDTH + off,// - .5 * .5 * .225 * CANVAS_WIDTH * __adventurer_list[current_selected].level.toString().length,
            (3.9 / 6.0) * CANVAS_HEIGHT - 4 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH
        )
        if (draw_button(plus_button, -2,
            (.76 + .25 * .4) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 4 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, disable, plus_button_disable))//*/
        {
            level_up0_sfx.play();
            __adventurer_list[current_selected].level += 1;
            current_fat -= price;
            save();
        }
        if (draw_button(info_button, -2 - 2000,
            (.76 + .25 * .85) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 4 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, false))//*/
        {
            //ability_tooltip_state = ABILITY_TOOLTIPS_ENUM.POWER;
            tutorial_index = 3;
            tutorial_mode = true;


        }

            //SPEED
        draw_center(mobility_texture,
            (.76 + .25 * .25) * CANVAS_WIDTH + off,
            (4.0 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH
        )
        draw_text("Speed:",
        (.76 + .25 * .35) * CANVAS_WIDTH + off,
            (3.9 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH

        );
        draw_text(__adventurer_list[current_selected].mobility.toString(),
        (.95) * CANVAS_WIDTH + off,// - .5 * .5 * .225 * CANVAS_WIDTH * __adventurer_list[current_selected].level.toString().length,
            (3.9 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH
        )
        if(draw_button(plus_button, -3,
            (.76 + .25 * .4) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, disable, plus_button_disable))//*/
        {
            level_up2_sfx.play();
            __adventurer_list[current_selected].mobility += 1;
            current_fat -= price;
            save();
        }
        draw_text("Reduce: " + "-" + Math.min(95, (100 - 100 * Math.pow(.99, (__adventurer_list[current_selected].mobility - 1)))).toFixed(1) + "%",
        (.76 + .2375 * .5) * CANVAS_WIDTH + off,
            (4.05 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );

        draw_text("C/D:  " + __adventurer_list[current_selected].get_actual_stat(STAT_TYPE_ENUM.MAX_ATTACK_TIMER).toFixed(1) + " s",
            (.76 + .2375 * .5) * CANVAS_WIDTH + off,
            (4.2 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );
        if (draw_button(info_button, -3 - 2000,
            (.76 + .25 * .85) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 3 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, false))//*/
        {
            //ability_tooltip_state = ABILITY_TOOLTIPS_ENUM.SPEED;
            tutorial_index = 4;
            tutorial_mode = true;

        }

        //METABOLISM
        draw_center(metabolism_texture,
            (.76 + .25 * .25) * CANVAS_WIDTH + off,
            (4.0 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH
        );

        draw_text("Metabolism:",
        (.76 + .25 * .35) * CANVAS_WIDTH + off,
            (3.9 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .66 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );
        draw_text(__adventurer_list[current_selected].metabolism.toString(),
        (.95) * CANVAS_WIDTH + off,// - .5 * .5 * .225 * CANVAS_WIDTH * __adventurer_list[current_selected].level.toString().length,
            (3.9 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH
        )

        if (draw_button(plus_button, -4,
            (.76 + .25 * .4) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, disable, plus_button_disable))//*/
        {
            level_up1_sfx.play();
            __adventurer_list[current_selected].metabolism += 1;
            current_fat -= price;
            save();
        }
        draw_text("Digestion Rate:",
            (.76 + .2375 * .5) * CANVAS_WIDTH + off,
            (4.05 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );
        draw_text("      " + __adventurer_list[current_selected].get_actual_stat(STAT_TYPE_ENUM.DIGESTION_RATE).toFixed(1) + "/s",
        (.76 + .25 * .5) * CANVAS_WIDTH + off,
            (4.2 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );
        if (draw_button(info_button, -4 - 2000,
            (.76 + .25 * .85) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 2 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, false))//*/
        {
            //ability_tooltip_state = ABILITY_TOOLTIPS_ENUM.METABOLISM;
            tutorial_index = 5;
            tutorial_mode = true;

        }

        
        //Capacity
        draw_center(capacity_texture,
            (.76 + .25 * .25) * CANVAS_WIDTH + off,
            (4.0 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .225 * CANVAS_WIDTH
        )
        draw_text("Capacity:   ",
        (.76 + .25 * .35) * CANVAS_WIDTH + off,
            (3.9 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH
        )
        draw_text(__adventurer_list[current_selected].stomach_level.toString(),
        (.95) * CANVAS_WIDTH + off,// - .5 * .5 * .225 * CANVAS_WIDTH * __adventurer_list[current_selected].level.toString().length,
            (3.9 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH
        )
        if (draw_button(plus_button, -5,
            (.76 + .25 * .4) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, disable, plus_button_disable))//*/
        {
            level_up3_sfx.play();
            __adventurer_list[current_selected].stomach_level += 1;
            current_fat -= price;
            save();
        }
        draw_text("Meter Max:",
            (.76 + .2375 * .5) * CANVAS_WIDTH + off,
            (4.05 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );
        draw_text("           " + __adventurer_list[current_selected].get_actual_stat(STAT_TYPE_ENUM.MAX_FULLNESS).toFixed(0),
        (.76 + .25 * .5) * CANVAS_WIDTH + off,
            (4.2 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .5 * .225 * CANVAS_WIDTH
        );
        if (draw_button(info_button, -5 - 2000,
            (.76 + .25 * .85) * CANVAS_WIDTH + off,
            (4.1 / 6.0) * CANVAS_HEIGHT - 1 * .5 * .5 * .25 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH,
            .5 * .5 * .5 * .225 * CANVAS_WIDTH, "",
            undefined, false))//*/
        {
            //ability_tooltip_state = ABILITY_TOOLTIPS_ENUM.CAPACITY;
            tutorial_index = 6;
            tutorial_mode = true;

        }
    } 
    else
    {
        draw_text("Select an adventurer to ", .55 * CANVAS_WIDTH + cf * CANVAS_WIDTH, .35 * CANVAS_HEIGHT, .5 * slot_size);
        draw_text(" extract weight and ", .55 * CANVAS_WIDTH + cf * CANVAS_WIDTH, .425 * CANVAS_HEIGHT, .5 * slot_size);
        draw_text("  level up stats", .55 * CANVAS_WIDTH + cf * CANVAS_WIDTH, .50 * CANVAS_HEIGHT, .5 * slot_size);


    }



    if (upgrade_state == UPGRADE_STATE_ENUM.LEVELUP && dialog_queue == 0 && !tutorial_mode)
        currency_panel_anim_f -= 4.0 * get_frame_time();
    else
        currency_panel_anim_f += 4.0 * get_frame_time();
    currency_panel_anim_f = clamp(currency_panel_anim_f, 0.0, 2.0);

    var crpf = currency_panel_anim_f * currency_panel_anim_f * currency_panel_anim_f * currency_panel_anim_f;

    draw_center(currency_panel, CANVAS_WIDTH * .5 + crpf * CANVAS_HEIGHT, CANVAS_HEIGHT * .875, CANVAS_WIDTH, CANVAS_HEIGHT * .25);

    draw_center(fat_texture, CANVAS_WIDTH * .375 + crpf * CANVAS_HEIGHT, CANVAS_HEIGHT * .9, .075 * CANVAS_WIDTH, .075 * CANVAS_WIDTH);

    if(Math.abs(display_fat - current_fat) <= 1)
        display_fat = current_fat;
    else if(display_fat < current_fat)
        display_fat += 10.0 * get_frame_time() * (current_fat - display_fat);
    else if(display_fat > current_fat)
        display_fat += 10.0 * get_frame_time() * (current_fat - display_fat);

     var   fdisplay_fat = Math.floor( display_fat );
     draw_text(fdisplay_fat.toFixed(0), CANVAS_WIDTH * .41 + crpf * CANVAS_HEIGHT, CANVAS_HEIGHT * .935, .05 * CANVAS_WIDTH);

    //draw_center(weight_texture, .4 * CANVAS_WIDTH- pos_x, CANVAS_HEIGHT - 50* frame_scale, .5 * slot_size, .5 * slot_size);
    draw_text("Extractions:  ", CANVAS_WIDTH * .65 + crpf * CANVAS_HEIGHT, CANVAS_HEIGHT * .935, .05 * CANVAS_WIDTH);
    draw_text((1 - current_extractions).toString(), CANVAS_WIDTH * .9 + crpf * CANVAS_HEIGHT, CANVAS_HEIGHT * .935, .05 * CANVAS_WIDTH);
    //draw_text(, .5 * CANVAS_WIDTH- pos_x, CANVAS_HEIGHT - 40* frame_scale, .5 * slot_size);

    var party_size = 0;
    for(var i = 0; i < 4; ++i)
    {
        if(adventurer_field[i])
        ++party_size;
    }

    if (upgrade_state == UPGRADE_STATE_ENUM.LEVELUP && dialog_queue == 0 && !tutorial_mode)
        close_button_anim_f -= 2.0 * get_frame_time();
    else
        close_button_anim_f += 2.0 * get_frame_time();
    close_button_anim_f = clamp(close_button_anim_f, 0.0, 1.0);

    var cbf = close_button_anim_f * close_button_anim_f * close_button_anim_f * close_button_anim_f * close_button_anim_f * close_button_anim_f;

    if (draw_button(close_button, 0, .5 * 216 * frame_scale - cbf * CANVAS_WIDTH + 10, CANVAS_HEIGHT - 50 * frame_scale - 10, 216 * frame_scale, 100 * frame_scale))//, 250, 150))
     {
         current_selected = -1;
         upgrade_state = UPGRADE_STATE_ENUM.FLOORSELECT;

      //if(upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT)
      //{
        //adventurer_field = [];

//all town operations are instantaneous in prototype v.1
                        //@TODO: re-enable
          /*
          for(var i in current_party)
          {
              if(current_party[i] != -1)
                adventurer_field[i] = __adventurer_list[current_party[i]];
          }//*/
          
         // if(party_size == 4)
               // next_gamestate = GAMESTATE_ENUM.DUNGEON;//*/
      //}
      //else
        //upgrade_state += 1;

     }

    if (upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT && dialog_queue == 0 && !tutorial_mode)
        goreal_button_anim_f -= 2.0 * get_frame_time();
    else
        goreal_button_anim_f += 9.0 * get_frame_time();
    goreal_button_anim_f = clamp(goreal_button_anim_f, 0.0, 1.0);

    var grbaf = goreal_button_anim_f * goreal_button_anim_f * goreal_button_anim_f * goreal_button_anim_f * goreal_button_anim_f;

    if (draw_button(go_button, 200, CANVAS_WIDTH - (1.25 * 216 * frame_scale + 10), CANVAS_HEIGHT - 50 * frame_scale - 10 + grbaf * CANVAS_HEIGHT, 216 * frame_scale, 100 * frame_scale))//, 250, 150))
    {
        if (party_size == 4)
        {
            start_sfx.play();

            //prompt cutscene
            if (!__level_completed[current_level] && current_level != previous_level) 
                enter_cutscene(0);


            was_level_incomplete = !__level_completed[current_level];

            //current_dialog = dialog_queue.shift();
            if (dialog_queue.length > 0) //if there is a cutscene, set the flag, otherwise immediate switch gamestate
                switch_gamestate_flag = true;
            else
              next_gamestate = GAMESTATE_ENUM.DUNGEON;//*/
        }
    }

    if (upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT && dialog_queue == 0 && !tutorial_mode)
        back_button_anim_f -= 4.0 * get_frame_time();
    else
        back_button_anim_f += 4.0 * get_frame_time();
    back_button_anim_f = clamp(back_button_anim_f, 0.0, 1.0);

    var bbaf = back_button_anim_f * back_button_anim_f * back_button_anim_f;

    if (draw_button(back_button, 300, CANVAS_WIDTH - (.4 * 216 * frame_scale + 10), CANVAS_HEIGHT - 50 * frame_scale - 10 + bbaf * CANVAS_HEIGHT, 172 * frame_scale, 75 * frame_scale))//, 250, 150))
        upgrade_state = UPGRADE_STATE_ENUM.FLOORSELECT;


     //console.log(__adventurer_list);

    if (upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT && dialog_queue == 0 && !tutorial_mode)
        golevel_button_anim_f -= 2.0 * get_frame_time();
    else
        golevel_button_anim_f += 2.0 * get_frame_time();
    golevel_button_anim_f = clamp(golevel_button_anim_f, 0.0, 1.0);


    if (upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT && dialog_queue == 0 && !tutorial_mode && __floor_list[current_level].dialog0.length > 0 && __level_completed[current_level])
        replay_start_anim_f -= 2.0 * get_frame_time();
    else
        replay_start_anim_f += 2.0 * get_frame_time();
    replay_start_anim_f = clamp(replay_start_anim_f, 0.0, 1.0);


    if (upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT && dialog_queue == 0 && !tutorial_mode && __floor_list[current_level].dialog1.length > 0 && __level_completed[current_level])
        replay_end_anim_f -= 2.0 * get_frame_time();
    else
        replay_end_anim_f += 2.0 * get_frame_time();
    replay_end_anim_f = clamp(replay_end_anim_f, 0.0, 1.0);

    var glbf = golevel_button_anim_f * golevel_button_anim_f * golevel_button_anim_f;
    var glbf0 = replay_start_anim_f * replay_start_anim_f * replay_start_anim_f * replay_start_anim_f * replay_start_anim_f;
    var glbf1 = replay_end_anim_f * replay_end_anim_f * replay_end_anim_f * replay_end_anim_f * replay_end_anim_f * replay_end_anim_f * replay_end_anim_f;

    if (draw_button(golevel_button, 1, .5 * 216 * frame_scale - glbf * CANVAS_WIDTH + 10, CANVAS_HEIGHT - 50 * frame_scale - 10, 216 * frame_scale, 100 * frame_scale))//, 250, 150))
        upgrade_state = UPGRADE_STATE_ENUM.LEVELUP;


    if (draw_button(cutscene0_button, 20, .5 * 498 * frame_scale - glbf0 * CANVAS_WIDTH + 10, CANVAS_HEIGHT - 30 * frame_scale - 10, 108 * frame_scale, 50 * frame_scale))//, 250, 150))
    {
        if (__floor_list[current_level].dialog0.length > 0)
        enter_cutscene(0);
    }
    if (draw_button(cutscene1_button, 30, .5 * 698 * frame_scale - glbf1 * CANVAS_WIDTH + 10, CANVAS_HEIGHT - 30 * frame_scale - 10, 108 * frame_scale, 50 * frame_scale))//, 250, 150))
    {
        if (__floor_list[current_level].dialog1.length > 0)
        enter_cutscene(1);
    }

    if (upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT && dialog_queue == 0 && !tutorial_mode)
        goprep_button_anim_f -= 3.0 * get_frame_time();
    else
        goprep_button_anim_f += 3.0 * get_frame_time();
    goprep_button_anim_f = clamp(goprep_button_anim_f, 0.0, 1.0);

    var gpbf = goprep_button_anim_f * goprep_button_anim_f * goprep_button_anim_f * goprep_button_anim_f * goprep_button_anim_f;

    if (draw_button(go_button, 10, CANVAS_WIDTH - (.5 * 216 * frame_scale + 10) + gpbf * CANVAS_WIDTH, CANVAS_HEIGHT - 50 * frame_scale - 10, 216 * frame_scale, 100 * frame_scale))//, 250, 150))
        upgrade_state = UPGRADE_STATE_ENUM.PARTYSELECT;





        /*draw_text("Select Level", 50* frame_scale + CANVAS_WIDTH - pos_x, 50* frame_scale, 50* frame_scale);*/
    
    
//var num = parseInt(i, 10);
//num += 1 + level_list_position;

        //if(draw_button(big_confirm, 100* frame_scale + i, 250* frame_scale + CANVAS_WIDTH - pos_x, 100* frame_scale + 50 * i* frame_scale, 450* frame_scale, 40* frame_scale, num + ": " + __floor_list[i + level_list_position].name))
        //{
        //    current_level = i + level_list_position;
        //}

        
        //if(current_level == i + level_list_position)
            ///draw_center(big_confirm, 50* frame_scale + CANVAS_WIDTH - pos_x, 100* frame_scale + 50 * i* frame_scale, 40* frame_scale, 40* frame_scale);
    //}//*/
 //*
    /*if(unlocked_levels >= 8)
    {
        if(draw_button(big_confirm, 303, 500* frame_scale + CANVAS_WIDTH - pos_x, 100* frame_scale, 40* frame_scale, 40* frame_scale, "^"))
        {
            --level_list_position;
        }


        if(draw_button(big_confirm, 304, 500* frame_scale + CANVAS_WIDTH - pos_x, 100* frame_scale + 50 * 7* frame_scale, 40* frame_scale, 40* frame_scale, "V"))
        {
            ++level_list_position;
        }
    }*/

    //if(level_list_position >  unlocked_levels - 7)
    //level_list_position = unlocked_levels - 7;

    //if(level_list_position < 0)
        //level_list_position = 0;

    //stage info panel

    if ((upgrade_state == UPGRADE_STATE_ENUM.FLOORSELECT || upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT) && dialog_queue == 0 && !tutorial_mode)
        level_info_panel_anim_f -= 3.0 * get_frame_time();
    else
        level_info_panel_anim_f += 3.0 * get_frame_time();
    level_info_panel_anim_f = clamp(level_info_panel_anim_f, 0.0, 1.0);

    var lipaf = level_info_panel_anim_f * level_info_panel_anim_f * level_info_panel_anim_f * level_info_panel_anim_f * level_info_panel_anim_f * level_info_panel_anim_f * level_info_panel_anim_f * level_info_panel_anim_f;

    draw_center(info_panel, CANVAS_WIDTH * .75, CANVAS_HEIGHT * .5 - lipaf * CANVAS_HEIGHT, CANVAS_WIDTH * .5, CANVAS_HEIGHT);
//*/
    draw_text(__floor_list[current_level].name,
        5.2 / 8.0 * CANVAS_WIDTH ,
        1.2 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT - lipaf * CANVAS_HEIGHT,
        1.0 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);

    for(var i in __floor_list[current_level].clear_times)
    {
        var x = 5.75 / 8.0 * CANVAS_WIDTH;//.6 * CANVAS_WIDTH + CANVAS_WIDTH + 50* frame_scale - pos_x;
        var y = 2.0 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT + .8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT * i - lipaf * CANVAS_HEIGHT;

        draw_center(rank_icons[i], x, y, 1.0 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, 1.0 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);
        
        var minutes = Math.floor(__floor_list[current_level].clear_times[i] / 60.0);
        var seconds = Math.floor(__floor_list[current_level].clear_times[i] - minutes * 60.0);
        var milliseconds = Math.floor(1000 * (__floor_list[current_level].clear_times[i] - minutes * 60.0 - seconds));

        if (milliseconds < 100) {milliseconds = "0" + milliseconds;}
        if (milliseconds < 10) {milliseconds = "0" + milliseconds;}

        if (seconds < 10) {seconds = "0" + seconds;}



        draw_text(minutes + "\'" + seconds + "\"" + milliseconds, x + 25 * frame_scale, y + 10 * frame_scale, .8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);

        if(__floor_list[current_level].clear_rewards[i].type == REWARD_TYPE_ENUM.ITEM)
        {
            var alph = .5;
            if(__reward_unlocked[current_level])//__item_list[__floor_list[current_level].clear_rewards[i].subtype].unlocked)
             alph = 1.0;

            draw_center(__item_list[__floor_list[current_level].clear_rewards[i].subtype].image,
            x + 200 * frame_scale, y, 0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, 0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, alph);
        }
        if(__floor_list[current_level].clear_rewards[i].type == REWARD_TYPE_ENUM.CHARACTER)
        {
            var alph = .5;
            if(__adventurer_list[__floor_list[current_level].clear_rewards[i].subtype].unlocked)
             alph = 1.0;

            draw_center(__adventurer_list[__floor_list[current_level].clear_rewards[i].subtype].unlock_image,
            x + 200 * frame_scale, y, 0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, 0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, alph);
        }
        if(__floor_list[current_level].clear_rewards[i].type == REWARD_TYPE_ENUM.BONUS_EXTRACT)
        {
            var alph = .5;
            if(__reward_unlocked[current_level])//__item_list[__floor_list[current_level].clear_rewards[i].subtype].unlocked)
             alph = 1.0;

            draw_center(fat_texture,
                x + 200 * frame_scale, y, 0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, 0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT, alph);
        }
    }

    //display defenses

    var count = 0;
    //console.log(defenses);
    //console.log(defenses);

    var minutes = Math.floor(best_times[current_level] / 60.0);
    var seconds = Math.floor(best_times[current_level] - minutes * 60.0);
    var milliseconds = Math.floor(1000 * (best_times[current_level] - minutes * 60.0 - seconds));

    if (milliseconds < 100) {milliseconds = "0" + milliseconds;}
    if (milliseconds < 10) {milliseconds = "0" + milliseconds;}

    if (seconds < 10) {seconds = "0" + seconds;}

    

    draw_text("Best Time: ",
        5.25 / 8.0 * CANVAS_WIDTH,
        6.5 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT - lipaf * CANVAS_HEIGHT,
        0.9 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);

    draw_text( minutes + "\'" + seconds + "\"" + milliseconds,
        6.375 / 8.0 * CANVAS_WIDTH,
        6.5 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT - lipaf * CANVAS_HEIGHT,
        0.9 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);

    for (var i = 0; i < 4; ++i) {

        if (best_teams[current_level][i] != -1) {
            var s = 1.1 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT;
            draw_center(__adventurer_list[best_teams[current_level][i]].unlock_image,
                6.0 / 8.0 * CANVAS_WIDTH + 1.1 * s * i,
                7.2 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT - lipaf * CANVAS_HEIGHT,

                0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT,
                0.8 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);
        }
    }

    //find  rank
    /*var rank = RANK_ENUM.F_RANK;
    for(var i in __floor_list[current_level].clear_times)
    {
        if(best_times[current_level] <= __floor_list[current_level].clear_times[i])
        {
            rank = i;
            break;
        }
    }
    draw_center(rank_icons[rank], CANVAS_WIDTH *.8 + CANVAS_WIDTH - pos_x, CANVAS_HEIGHT * .60, 50* frame_scale, 50* frame_scale);*/


    draw_text("Common Elements:",
        5.5 / 8.0 * CANVAS_WIDTH,
        8.5 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT - lipaf * CANVAS_HEIGHT,
        0.9 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT);

    count = 0;
    for (var i in __floor_list[current_level].common_elements)
    {
        var s = 1.1 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT;
        var x = 5.5 / 8.0 * CANVAS_WIDTH + 1.1 * s * i;
        var y = 9.25 / 9.0 * (2.0 / 3.0) * CANVAS_HEIGHT - lipaf * CANVAS_HEIGHT;//275 + 60 * Math.floor(count / 3);

        //if (__floor_list[current_level].common_elements[count] > 0)
        {
            if (__floor_list[current_level].common_elements[i] < STAT_TYPE_ENUM.NUM_STAT_TYPE) {
                var index = __floor_list[current_level].common_elements[i];

                for (var j in adventurer_field) {
                    if (adventurer_field[j] != null && adventurer_field[j].actual_stats[index] > 0)
                        draw_center(indicator, x, y, 1.1 * s, 1.1 * s);
                }
                draw_center(stat_images[index], x, y, s, s);
            }
            else {
                var index = __floor_list[current_level].common_elements[i] - STAT_TYPE_ENUM.NUM_STAT_TYPE;

                for (var j in adventurer_field) {
                    if (adventurer_field[j] != null && adventurer_field[j].favorite_food == index &&
                        (adventurer_field[j].actual_stats[STAT_TYPE_ENUM.WEAPON] > 0 ||
                        adventurer_field[j].actual_stats[STAT_TYPE_ENUM.MAGIC] > 0 ||
                        adventurer_field[j].actual_stats[STAT_TYPE_ENUM.FAITH] > 0 ||
                        adventurer_field[j].actual_stats[STAT_TYPE_ENUM.SNEAK] > 0)
                    
                    )
                        draw_center(indicator, x, y, 1.1 * s, 1.1 * s);
                }

                draw_center(food_images[index], x, y, s, s);
            }

        }
        //count += 1;
    }
    //console.log("Pres");

    
    //draw_text("Select Party", 2 * CANVAS_WIDTH - pos_x, 50* frame_scale, 50* frame_scale);

     counter = 0;/*
    while(counter < 9)
    {
        var x = 2.0 * CANVAS_WIDTH + CANVAS_WIDTH * .2 + .11 * CANVAS_WIDTH * (counter % 4 - 1.0) - pos_x;
        var y = CANVAS_HEIGHT * .4 + (300.0 / 270.0) * .11 * CANVAS_WIDTH * (Math.floor(counter / 4) - .5);
        var alpha = 1.0;

        var current_party_index = -1;

        for(var i = 0; i < 4; ++i)
        {
            if(adventurer_field[i] && adventurer_field[i].id == __adventurer_list[counter].id)
            current_party_index = i;
        }

        if(current_party_index == -1)
            alpha = 0.5;
    
        var w = .11 * CANVAS_WIDTH;

        if(!__adventurer_list[counter + current_display_index].unlocked)
        {
            draw_center(lock_image, x,y, .5 * w, .5 * w, 1.0);
            alpha = .1;
        }
        else
        {
            if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, w, (300.0 / 270.0) * w))
            {
                w *= 1.05;
                //h *= 1.05;
            }

            if(INPUT.left_triggered)
            {
                if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x,y, .125 * CANVAS_WIDTH, (300.0 / 270.0) *.125 * CANVAS_WIDTH))
                 {
    
                    //current_selected = counter + current_display_index;
    
                    if(current_party_index == -1)
                    {
                        for(var i = 0; i < 4; ++i)
                        {
                            if(!adventurer_field[i])
                            {
                                adventurer_field[i] = __adventurer_list[counter];
                                break;
                            }
                        }
                    }
                    else
                    {
                        for(var i = 0; i < 4; ++i)
                        {
                            if(adventurer_field[i] && adventurer_field[i].id == __adventurer_list[counter].id)
                            adventurer_field[i] = null;
                        }
    
                    }
                }
                      
            }
        }
        __adventurer_list[counter + current_display_index].draw_small(x, y, w, alpha);



        counter += 1;

        
    }*/
    
    counter = 0;


    //items
    if (upgrade_state == UPGRADE_STATE_ENUM.PARTYSELECT && dialog_queue == 0 && !tutorial_mode)
        item_panel_anim_f -= 2.0 * get_frame_time();
    else
        item_panel_anim_f += 2.0 * get_frame_time();
    item_panel_anim_f = clamp(item_panel_anim_f, 0.0, 1.0);

    var ipaf = item_panel_anim_f * item_panel_anim_f * item_panel_anim_f * item_panel_anim_f * item_panel_anim_f * item_panel_anim_f * item_panel_anim_f * item_panel_anim_f * item_panel_anim_f;

    draw_center(item_panel, CANVAS_WIDTH * .5 - ipaf * CANVAS_WIDTH, CANVAS_HEIGHT * (1.0 - .5 * .225), CANVAS_WIDTH, CANVAS_HEIGHT * .225);
    for(var i in __item_list)
    {
        var w = .66 * (.9 / 8.0) * CANVAS_HEIGHT;

        var x = .075 * CANVAS_WIDTH + 1.1 * w * (counter % 11 - .5) - ipaf * CANVAS_WIDTH;
        var y = CANVAS_HEIGHT * (6.8/8.0) + 1.1 * w * Math.floor(counter / 11);// + (300.0 / 270.0) * .125 * CANVAS_WIDTH * (Math.floor(counter / 3) - .5);
        var alpha = 1.0;


        var current_item_index = -1;

        for(var i = 0; i < 4; ++i)
        {
            if(item_field[i] && item_field[i].id == __item_list[counter].id)
            current_item_index = i;
        }

        if(current_item_index == -1)
            alpha = 0.5;
    

        if(__item_list[counter].strength == 0)
        {
            draw_center(lock_image, x,y, .5 * w, .5 * w, 1.0);
            alpha = .1;
        }
        else
        {
            if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, w, w))
            {
                w *= 1.05;
                //h *= 1.05;
            }

            if(INPUT.left_triggered)
            {
                if(point_rect_collision(INPUT.mouse_screen.x, INPUT.mouse_screen.y, x, y, w, w))
                 {
                    button_sfx.play();
                    //current_selected = counter + current_display_index;
    
                    if(current_item_index == -1)
                    {
                        for(var i = 0; i < 4; ++i)
                        {
                            if(!item_field[i])
                            {
                                item_field[i] = __item_list[counter];
                                break;
                            }
                        }
                    }
                    else
                    {
                        for(var i = 0; i < 4; ++i)
                        {
                            if(item_field[i] && item_field[i].id == __item_list[counter].id)
                            item_field[i] = null;
                        }
    
                    }
                }
                      /* 
            adventurer_field[0] = __adventurer_list[0];
            adventurer_field[1] = __adventurer_list[1];
            adventurer_field[2] = __adventurer_list[2];
            adventurer_field[3] = __adventurer_list[3];*/
            }
        }
        __item_list[counter].draw(x, y, w, w, alpha);



        counter += 1;

        
    }


    //draw_text("Party Size: ",  2 * CANVAS_WIDTH - pos_x, CANVAS_HEIGHT - 75* frame_scale, .5 * slot_size);
    //draw_text(party_size + "/4", 2 * CANVAS_WIDTH- pos_x, CANVAS_HEIGHT - 40* frame_scale, .5 * slot_size);



    if (tutorial_mode && dialog_queue.length == 0)
    {
        if (!advance_tutorial)
            tutorial_timer += 3.0 * get_frame_time();
        else {

            tutorial_timer -= 6.0 * get_frame_time();
            if (tutorial_timer < 0.0) {
                advance_tutorial = false;
                ++tutorial_index;
                if (tutorial_index >= 3)
                    tutorial_mode = false;
            }
        }

    tutorial_lock_dismissal_timer += get_frame_time();
    if (tutorial_lock_dismissal_timer > 1.0 && INPUT.left_triggered) {

        tutorial_lock_dismissal_timer = 0.0;
        advance_tutorial = true;
    }
    }

    tutorial_timer = clamp(tutorial_timer, 0.0, 1.0);


    if (tutorial_timer > 0.05) {
        var d = .75 * CANVAS_WIDTH * Math.sqrt(1.0 - tutorial_timer);

        draw_center(fade_texture, CANVAS_WIDTH * .5, CANVAS_HEIGHT * .5, CANVAS_WIDTH, CANVAS_HEIGHT, tutorial_timer);
        draw_center(fade_texture, CANVAS_WIDTH * .5, CANVAS_HEIGHT * .5, CANVAS_WIDTH, CANVAS_HEIGHT, tutorial_timer);

        draw_center(tut_image_texture, CANVAS_WIDTH * .25 - d, CANVAS_HEIGHT * .5, CANVAS_HEIGHT * .75, CANVAS_HEIGHT * .75, 1.0);
        draw_center(upgrade_tutorial_images[tutorial_index], CANVAS_WIDTH * .25 - d, CANVAS_HEIGHT * .5, CANVAS_HEIGHT * .68, CANVAS_HEIGHT * .68, 1.0);

        draw_center(tut_text_texture, CANVAS_WIDTH * .725 + d, CANVAS_HEIGHT * .5, CANVAS_WIDTH * .5, CANVAS_WIDTH * .5 * (201.0 / 545.0), 1.0);


        
        var text_w = Math.sqrt((.8 * CANVAS_WIDTH * .5 * CANVAS_WIDTH * .5 * (201.0 / 545.0)) /
                    (.75 * upgrade_tutorial_messages[tutorial_index].length));

        //for(var i in __floor_list[current_level].rooms[room_counter].messages)
        var room_text = upgrade_tutorial_messages[tutorial_index];
        var text_counter = 0;
        var place = 0;
        var wrapped_text = [];
        var threshold = 1.9 * (CANVAS_WIDTH * .45) / text_w;
        var previous_space = 0;

        for (var i in room_text) {
            if (room_text[i] == ' ')
                previous_space = i;

            if (text_counter > threshold) {
                wrapped_text.push(room_text.slice(place, previous_space))
                place = previous_space;
                text_counter = 0;
                i = previous_space;

                ++place;
                //debugger;
            }
            else {


                ++text_counter;
            }
        }

        wrapped_text.push(room_text.slice(place));

        for (var i in wrapped_text) {
            //if(current_level == 0)
            draw_text(wrapped_text[i],
              CANVAS_WIDTH * .725 - .4 * (CANVAS_WIDTH * .5) + d,
              CANVAS_HEIGHT * .5 - .4 * (CANVAS_WIDTH * .5 * (201.0 / 545.0)) + (i) * text_w + text_w,
              text_w);
            //else
            //draw_text(wrapped_text[i], 550* frame_scale, CANVAS_HEIGHT - 150* frame_scale + 30 * i* frame_scale, 22* frame_scale);
        }

        var title_w = 2.0 * .4 * CANVAS_WIDTH / upgrade_tutorial_titles[tutorial_index].length;

        if (1.5 * text_w < title_w)
            title_w = 1.5 * text_w;

        draw_text(upgrade_tutorial_titles[tutorial_index],
              CANVAS_WIDTH * .725 - .4 * (CANVAS_WIDTH * .5) + d,
              CANVAS_HEIGHT * .5 - .55 * (CANVAS_WIDTH * .5 * (201.0 / 545.0)),
              title_w);
    }


    if ( dialog_queue.length > 0)
    {


        dialog_enter_timer = clamp(dialog_enter_timer, 0.0, 1.0);


        var d =  .5 * CANVAS_WIDTH * Math.sqrt(1.0 - dialog_enter_timer);

        draw_center(dialog_texture, CANVAS_WIDTH * .5, .75 * CANVAS_HEIGHT + d, CANVAS_WIDTH * 1.0, CANVAS_HEIGHT * 0.5, 1.0);

        //current_dialog is a DIALOG_SET, so we need to remember to determine the correct dialog!

        var room_text = dialog_queue[0].active.body;//"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim tortor, hendrerit non viverra eget, tempus sit amet dui. Nam diam massa, volutpat et aliquet in, eleifend ";

        var text_w = Math.sqrt( ((CANVAS_WIDTH * 926.0 / 1080.0) * (CANVAS_HEIGHT * 160.0 / 600.0) /
                    (room_text.length / 1.8)) );
        if (text_w >  (CANVAS_HEIGHT * 160.0 / 600.0) / 4.0)
            text_w =  (CANVAS_HEIGHT * 160.0 / 600.0) / 4.0;
        var text_counter = 0;
        var place = 0;
        var wrapped_text = [];
        var threshold = 2.125 * (CANVAS_WIDTH * 926.0 / 1080.0) / text_w;
        var previous_space = 0;
        for (var i = 0; i < room_text.length; ++i) {
            if (room_text[i] == ' ')
                previous_space = i;

            if (text_counter > threshold) {
                wrapped_text.push(room_text.slice(place, previous_space))
                place = previous_space;
                text_counter = 0;
                i = previous_space;

                ++place;
                //debugger;
            }
            else {


                ++text_counter;
            }
        }

        wrapped_text.push(room_text.slice(place));

        for (var i in wrapped_text) {
            //if(current_level == 0)
            draw_text(wrapped_text[i],
              CANVAS_WIDTH * (70.0 / 1080.0),
              CANVAS_HEIGHT * (388.0 / 600) + (i) * text_w + .75 * text_w + d,
              text_w, TEXT_COLOR_ENUM.WHITE, 1);
            //else
            //draw_text(wrapped_text[i], 550* frame_scale, CANVAS_HEIGHT - 150* frame_scale + 30 * i* frame_scale, 22* frame_scale);
        }
        var name_text = "Lorem ipsum dolor sit ame";
        if (npcs[dialog_queue[0].active.npc_id].adventurer)
            name_text = __adventurer_list[npcs[dialog_queue[0].active.npc_id].adventurer_index].name;
        else
            name_text = npcs[dialog_queue[0].active.npc_id].name;

        draw_text(name_text,
              CANVAS_WIDTH * (100.0 / 1080.0),
              CANVAS_HEIGHT * (355.0 / 600) + d,
              32.0 / 600 * CANVAS_HEIGHT, TEXT_COLOR_ENUM.WHITE, 1);

        var portrait_counter = 0;
        for (let item of portrait_list)
        {
            var s = CANVAS_WIDTH / (portrait_list.size + 1);
            if (s > CANVAS_HEIGHT * .45)
                s = CANVAS_HEIGHT * .45;


            var x = .5 * CANVAS_WIDTH - .5 * s * portrait_list.size + s * portrait_counter + s * .5;
            var w = .6 * s;


            var a = .25;
            if (dialog_queue[0].active.npc_id == item) {
                w = .8 * s;
                a = 1.0;
                npcs[item].current_override_state = dialog_queue[0].active.override;
            }
            if (!dialog_enter_mode && dialog_queue.length == 1)
            {
                //if we are exiting the final box, we have special animations
                x = -.5 * CANVAS_WIDTH;
            }
            npcs[item].display_x += .25 * (x - npcs[item].display_x);
            npcs[item].display_s += .25 * (w - npcs[item].display_s);
            npcs[item].display_a += .25 * (a - npcs[item].display_a);

            if (npcs[item].adventurer)
                __adventurer_list[npcs[item].adventurer_index].draw_npc(npcs[item].display_x, .25 * CANVAS_HEIGHT, npcs[item].display_s, npcs[item].display_a);
            else {

                draw_center(npc_frame, npcs[item].display_x, .25 * CANVAS_HEIGHT, (610.0 / 540.0) * npcs[item].display_s, (670.0 / 600.0) * (300.0 / 270.0) * npcs[item].display_s, 1.0);

                if (npcs[item].current_override_state == -1) {
                    if (current_dialog_index == 0)
                        draw_center(npcs[item].image0, npcs[item].display_x, .25 * CANVAS_HEIGHT, npcs[item].display_s, (300.0 / 270.0) * npcs[item].display_s, npcs[item].display_a);
                    else
                        draw_center(npcs[item].image1, npcs[item].display_x, .25 * CANVAS_HEIGHT, npcs[item].display_s, (300.0 / 270.0) * npcs[item].display_s, npcs[item].display_a);
                }
                else if (npcs[item].current_override_state == 0)
                    draw_center(npcs[item].image0, npcs[item].display_x, .25 * CANVAS_HEIGHT, npcs[item].display_s, (300.0 / 270.0) * npcs[item].display_s, npcs[item].display_a);
                else
                    draw_center(npcs[item].image1, npcs[item].display_x, .25 * CANVAS_HEIGHT, npcs[item].display_s, (300.0 / 270.0) * npcs[item].display_s, npcs[item].display_a);


            }
            ++portrait_counter;
        }

        if (dialog_enter_mode) {
            dialog_enter_timer += 4.0 * get_frame_time();
            if (dialog_enter_timer > .95 && INPUT.left_pressed)
                dialog_enter_mode = false;
        } else {
            dialog_enter_timer -= 7.0 * get_frame_time();
            if (dialog_enter_timer < 0.0) {
                //current_dialog =
                dialog_queue.shift();
                dialog_enter_mode = true;

                if (dialog_queue.length > 0)//continue scene
                {

                    if (npcs[dialog_queue[0].first.npc_id].adventurer && __adventurer_list[npcs[dialog_queue[0].first.npc_id].adventurer_index].unlocked)
                        dialog_queue[0].active = dialog_queue[0].first
                    else if (!npcs[dialog_queue[0].first.npc_id].adventurer)
                        dialog_queue[0].active = dialog_queue[0].first
                    else if (dialog_queue[0].second != null)
                        dialog_queue[0].active = dialog_queue[0].second;
                    else
                        dialog_queue[0].active = dialog_queue[0].first

                    if (npcs[dialog_queue[0].first.npc_id].adventurer || npcs[dialog_queue[0].active.npc_id].name.length > 0)
                        portrait_list.add(dialog_queue[0].active.npc_id);
                }
                else //clean up scene and possibly switch to new gamestate
                {
                    for (let item of portrait_list)
                    {
                        npcs[item].display_x = CANVAS_WIDTH * 3.0;
                        npcs[item].display_s = CANVAS_HEIGHT * .45;
                        npcs[item].display_a = 1.0;
                    }
                    portrait_list.clear();

                    if (switch_gamestate_flag) {
                        switch_gamestate_flag = false;
                        next_gamestate = GAMESTATE_ENUM.DUNGEON;//*/
                    }
                }
            }
        }
    }

    //if (ability_tooltip_state != ABILITY_TOOLTIPS_ENUM.NONE) {

    //}


}

function exit_upgrade_gamestate()
{



}