---------------------------------------------
--
--    Function that all map NPCS use.
--    SE updated the map NPCs to sell maps from the normal areas, RoZ, and CoP areas (Update was in Nov 5, 2013)
---------------------------------------------

require("scripts/globals/keyitems");

local Maps = {dsp.ki.MAP_OF_THE_SAN_DORIA_AREA, dsp.ki.MAP_OF_THE_BASTOK_AREA, dsp.ki.MAP_OF_THE_WINDURST_AREA, dsp.ki.MAP_OF_THE_JEUNO_AREA, dsp.ki.MAP_OF_ORDELLES_CAVES, dsp.ki.MAP_OF_GHELSBA, dsp.ki.MAP_OF_DAVOI, dsp.ki.MAP_OF_CARPENTERS_LANDING, dsp.ki.MAP_OF_THE_ZERUHN_MINES,
    dsp.ki.MAP_OF_THE_PALBOROUGH_MINES, dsp.ki.MAP_OF_BEADEAUX, dsp.ki.MAP_OF_GIDDEUS, dsp.ki.MAP_OF_CASTLE_OZTROJA, dsp.ki.MAP_OF_THE_MAZE_OF_SHAKHRAMI, dsp.ki.MAP_OF_THE_LITELOR_REGION, dsp.ki.MAP_OF_BIBIKI_BAY, dsp.ki.MAP_OF_QUFIM_ISLAND, dsp.ki.MAP_OF_THE_ELDIEME_NECROPOLIS,
    dsp.ki.MAP_OF_THE_GARLAIGE_CITADEL, dsp.ki.MAP_OF_THE_ELSHIMO_REGIONS, dsp.ki.MAP_OF_THE_NORTHLANDS_AREA, dsp.ki.MAP_OF_KING_RANPERRES_TOMB, dsp.ki.MAP_OF_THE_DANGRUF_WADI, dsp.ki.MAP_OF_THE_HORUTOTO_RUINS, dsp.ki.MAP_OF_BOSTAUNIEUX_OUBLIETTE,
    dsp.ki.MAP_OF_THE_TORAIMARAI_CANAL, dsp.ki.MAP_OF_THE_GUSGEN_MINES, dsp.ki.MAP_OF_THE_CRAWLERS_NEST, dsp.ki.MAP_OF_THE_RANGUEMONT_PASS, dsp.ki.MAP_OF_DELKFUTTS_TOWER, dsp.ki.MAP_OF_FEIYIN, dsp.ki.MAP_OF_CASTLE_ZVAHL, dsp.ki.MAP_OF_THE_KUZOTZ_REGION,
    dsp.ki.MAP_OF_THE_RUAUN_GARDENS, dsp.ki.MAP_OF_NORG, dsp.ki.MAP_OF_THE_TEMPLE_OF_UGGALEPIH, dsp.ki.MAP_OF_THE_DEN_OF_RANCOR, dsp.ki.MAP_OF_THE_KORROLOKA_TUNNEL, dsp.ki.MAP_OF_THE_KUFTAL_TUNNEL, dsp.ki.MAP_OF_THE_BOYAHDA_TREE, dsp.ki.MAP_OF_THE_VELUGANNON_PALACE,
    dsp.ki.MAP_OF_IFRITS_CAULDRON, dsp.ki.MAP_OF_THE_QUICKSAND_CAVES, dsp.ki.MAP_OF_THE_SEA_SERPENT_GROTTO, dsp.ki.MAP_OF_THE_VOLLBOW_REGION, dsp.ki.MAP_OF_THE_LABYRINTH_OF_ONZOZO, dsp.ki.MAP_OF_THE_ULEGUERAND_RANGE, dsp.ki.MAP_OF_THE_ATTOHWA_CHASM, dsp.ki.MAP_OF_PSOXJA,
    dsp.ki.MAP_OF_OLDTON_MOVALPOLOS, dsp.ki.MAP_OF_NEWTON_MOVALPOLOS, dsp.ki.MAP_OF_TAVNAZIA, dsp.ki.MAP_OF_THE_AQUEDUCTS, dsp.ki.MAP_OF_THE_SACRARIUM, dsp.ki.MAP_OF_CAPE_RIVERNE, dsp.ki.MAP_OF_ALTAIEU, dsp.ki.MAP_OF_HUXZOI, dsp.ki.MAP_OF_RUHMET, dsp.ki.MAP_OF_AL_ZAHBI, dsp.ki.MAP_OF_NASHMAU,
    dsp.ki.MAP_OF_WAJAOM_WOODLANDS, dsp.ki.MAP_OF_CAEDARVA_MIRE, dsp.ki.MAP_OF_MOUNT_ZHAYOLM, dsp.ki.MAP_OF_AYDEEWA_SUBTERRANE, dsp.ki.MAP_OF_MAMOOK, dsp.ki.MAP_OF_HALVUNG, dsp.ki.MAP_OF_ARRAPAGO_REEF, dsp.ki.MAP_OF_ALZADAAL_RUINS, dsp.ki.MAP_OF_BHAFLAU_THICKETS,
    dsp.ki.MAP_OF_VUNKERL_INLET, dsp.ki.MAP_OF_GRAUBERG, dsp.ki.MAP_OF_FORT_KARUGONARUGO};

local Uoption = {    --User option selected.
    1,            --SanDoria Area
    65537,        --Bastok Area
    131073,        --Windurst Area
    196609,        --Jeuno Area
    262145,        --Ordelles Caves
    327681,        --Ghelsba
    393217,        --Davoi
    458753,        --Carpenters Landing
    524289,        --Zeruhn Mines
    589825,        --Palborough Mines
    655361,        --Beadeaux
    720897,        --Giddeus
    786433,        --Castle Oztroja
    851969,        --Maze of Shakhrami
    917505,        --Li'Telor Region
    983041,        --Bibiki Bay
    1048577,    --Qufim Island
    1114113,    --Eldieme Necropolis
    1179649,    --Garlaige Citadel
    1245185,    --Elshimo Regions
    1310721,    --Northlands Area
    1376257,    --King Ranperres Tomb
    1441793,    --Dangruf Wadi
    1507329,    --Horutoto Ruins
    1572865,    --Bostaunieux Oubliette
    1638401,    --Toraimarai Canal
    1703937,    --Gusgen Mines
    1769473,    --Crawlers Nest
    1835009,    --Ranguemont Pass
    1900545,    --Delkfutts Tower
    1966081,    --Feiyin
    2031617,    --Castle Zvahl
    2097153,    --Kuzotz region
    2162689,    --Ru'Aun Gardens
    2228225,    --Norg
    2293761,    --Temple of Uggalepih
    2359297,    --Den of Rancor
    2424833,    --Korroloka Tunnel
    2490369,    --Kuftal Tunnel
    2555905,    --Boyahda Tree
    2621441,    --Ve'Lugannon Palace
    2686977,    --Ifrit's Cauldron
    2752513,    --Quicksand Caves
    2818049,    --Sea Serpent Grotto
    2883585,    --Vollbow region
    2949121,    --Labyrinth of Onzozo
    3014657,    --Uleguerand Range
    3080193,    --Attohwa Chasm
    3145729,    --Pso'Xja
    3211265,    --Oldton Movalpolos
    3276801,    --Newton Movalpolos
    3342337,    --Tavnazia
    3407873,    --Aqueducts
    3473409,    --Sacrarium
    3538945,    --Cape Riverne
    3604481,    --Al'Taieu
    3670017,    --Hu'Xzoi
    3735553,    --Ru'Hmet
    3801089,    --Al Zahbi
    3866625,    --Nashmau
    3932161,    --Wajaom Woodlands
    3997697,    --Caedarva Mire
    4063233,    --Mount Zhayolm
    4128769,    --Aydeewa Subterrane
    4194305,    --Mamook
    4259841,    --Halvung
    4325377,    --Arrapago Reef
    4390913,    --Alzadall Ruins
    4456449,    --Bhaflau Thickets
    4521985,    --Vunkerl Inlet
    4587521,    --Grauberg
    4653057        --Fort Karugo-Narugo
    };

--Groups maps by price, based off the user option.
local p2 = { --Maps that are price at 200 gil
    Uoption[1],        --SanDoria Area
    Uoption[2],        --Bastok Area
    Uoption[3],        --Windurst Area
    Uoption[9]        --Zeruhn Mines
    };
local p6 = { --Maps that are price at 600 gil
    Uoption[4],        --Jeuno Area
    Uoption[5],        --Ordelles Caves
    Uoption[6],        --Ghelsba
    Uoption[10],    --Palborough Mines
    Uoption[11],    --Beadeaux
    Uoption[12],    --Giddeus
    Uoption[14],    --Maze of Shakhrami
    Uoption[22],    --King Ranperres Tomb
    Uoption[23],    --Dangruf Wadi
    Uoption[24],    --Horutoto Ruins
    Uoption[27],    --Gusgen Mines
    Uoption[59]        --Al Zahbi
    };
local p3 = {    --Maps that are price at 3000 gil
    Uoption[7],        --Davoi
    Uoption[8],        --Carpenters Landing
    Uoption[13],    --Castle Oztroja
    Uoption[15],    --Li'Telor Region
    Uoption[16],    --Bibiki Bay
    Uoption[17],    --Qufim Island
    Uoption[18],    --Eldieme Necropolis
    Uoption[19],    --Garlaige Citadel
    Uoption[20],    --Elshimo Regions
    Uoption[21],    --Northlands Area
    Uoption[25],    --Bostaunieux Oubliette
    Uoption[26],    --Toraimarai Canal
    Uoption[28],    --Crawlers Nest
    Uoption[29],    --Ranguemont Pass
    Uoption[30],    --Delkfutts Tower
    Uoption[31],    --Feiyin
    Uoption[32],    --Castle Zvahl
    Uoption[33],    --Kuzotz region
    Uoption[34],    --Ru'Aun Gardens
    Uoption[35],    --Norg
    Uoption[36],    --Temple of Uggalepih
    Uoption[37],    --Den of Rancor
    Uoption[38],    --Korroloka Tunnel
    Uoption[39],    --Kuftal Tunnel
    Uoption[40],    --Boyahda Tree
    Uoption[41],    --Ve'Lugannon Palace
    Uoption[42],    --Ifrit's Cauldron
    Uoption[43],    --Quicksand Caves
    Uoption[44],    --Sea Serpent Grotto
    Uoption[45],    --Vollbow region
    Uoption[46],    --Labyrinth of Onzozo
    Uoption[47],    --Uleguerand Range
    Uoption[48],    --Attohwa Chasm
    Uoption[49],    --Pso'Xja
    Uoption[50],    --Oldton Movalpolos
    Uoption[51],    --Newton Movalpolos
    Uoption[52],    --Tavnazia
    Uoption[53],    --Aqueducts
    Uoption[54],    --Sacrarium
    Uoption[55],    --Cape Riverne
    Uoption[56],    --Al'Taieu
    Uoption[57],    --Hu'Xzoi
    Uoption[58],    --Ru'Hmet
    Uoption[60],    --Nashmau
    Uoption[61],    --Wajaom Woodlands
    Uoption[62],    --Caedarva Mire
    Uoption[63],    --Mount Zhayolm
    Uoption[64],    --Aydeewa Subterrane
    Uoption[65],    --Mamook
    Uoption[66],    --Halvung
    Uoption[67],    --Arrapago Reef
    Uoption[68],    --Alzadall Ruins
    Uoption[69]        --Bhaflau Thickets
    };
local p30 = { --Maps that are price at 30,000 gil
    Uoption[70],    --Vunkerl Inlet
    Uoption[71],    --Grauberg
    Uoption[72]        --Fort Karugo-Narugo
    };
    
function CheckMaps(player, npc, csid)
    local i = 0;
    local mapVar1 = 0;
    local mapVar2 = 0;
    local mapVar3 = 0;

    while i <= 31 do
        if player:hasKeyItem(Maps[i+1]) then
            mapVar1 = bit.bor(mapVar1, bit.lshift(1,i));
        end
        i = i + 1;
    end
    
    while i <= 63 do
        if player:hasKeyItem(Maps[i+1]) then
            mapVar2 = bit.bor(mapVar2, bit.lshift(1,i));
        end
        i = i + 1;
    end
    
    while i <= 71 do
        if player:hasKeyItem(Maps[i+1]) then
            mapVar3 = bit.bor(mapVar3, bit.lshift(1,i));
        end
        i = i + 1;
    end
    
    player:startEvent(csid, mapVar1, mapVar2, mapVar3);
end;

function CheckMapsUpdate (player, option, NOT_HAVE_ENOUGH_GIL, KEYITEM_OBTAINED)
    local price = 0;
    local MadePurchase = false;
    local KI = 0;
    local i = 0;
    local mapVar1 = 0;
    local mapVar2 = 0;
    local mapVar3 = 0;

    while i <= 71 do
        if (option == Uoption[i+1]) then
            local x = 1;
            while x <= 53 do
                if (x <= 4 and option == p2[x]) then
                    price = 200;
                elseif (x <= 12 and option == p6[x]) then
                    price = 600;
                elseif (x <= 53 and option == p3[x]) then
                    price = 3000;
                elseif (x <= 3 and option == p30[x]) then
                    price = 30000;
                end
                x=x+1;
            end
            MadePurchase = true;
            KI = Maps[i+1];
        end
        i = i + 1;
    end

    if (price > player:getGil()) then
        player:messageSpecial(NOT_HAVE_ENOUGH_GIL);
        MadePurchase = false;
        price = 0;
    elseif (price > 0 and MadePurchase == true) then
        player:delGil(price);
        MadePurchase = false;
        player:addKeyItem(KI);
        player:messageSpecial(KEYITEM_OBTAINED, KI);
    end
    
    i=0;
    while i <= 31 do
        if player:hasKeyItem(Maps[i+1]) then
            mapVar1 = bit.bor(mapVar1, bit.lshift(1,i));
        end
        i = i + 1;
    end
    
    while i <= 63 do
        if player:hasKeyItem(Maps[i+1]) then
            mapVar2 = bit.bor(mapVar2, bit.lshift(1,i));
        end
        i = i + 1;
    end
    
    while i <= 71 do
        if player:hasKeyItem(Maps[i+1]) then
            mapVar3 = bit.bor(mapVar3, bit.lshift(1,i));
        end
        i = i + 1;
    end
    
    player:updateEvent(mapVar1, mapVar2, mapVar3);
end;