WoWInterface SVN MetaMapv4.0

[/] [trunk/] [MetaMap/] [MetaMapFunctions.lua] - Rev 164

Compare with Previous | Blame | View Log

METAMAP_SORTBY_NAME = "name";
METAMAP_SORTBY_DESC = "desc";
METAMAP_SORTBY_LEVEL = "level";
METAMAP_SORTBY_LOCATION = "location";

function MetaMap_SortCriteria(a, b)
        if (MetaMap_sortType == METAMAP_SORTBY_NAME) then
                if (a.name < b.name) then
                        return true;
                elseif (a.name > b.name) then
                        return false;
                end
        elseif (MetaMap_sortType == METAMAP_SORTBY_DESC) then
                if (a.desc < b.desc) then
                        return true;
                elseif (a.desc > b.desc) then
                        return false;
                end
        elseif (MetaMap_sortType == METAMAP_SORTBY_LEVEL) then
                if (a.level < b.level) then
                        return true;
                elseif (a.level > b.level) then
                        return false;
                end
        elseif (MetaMap_sortType == METAMAP_SORTBY_LOCATION) then
                if (a.location < b.location) then
                        return true;
                elseif (a.location > b.location) then
                        return false;
                end
        else
                if (a < b) then
                        return true;
                elseif (a > b) then
                        return false;
                end
                if (a == nil) then
                        if (b == nil) then
                                return false;
                        else
                                return true;
                        end
                elseif (b == nil) then
                        return false;
                end
        end
end

function MetaMap_GetMapContinents()
        local continentNames = { };
        for i = 1, 1000 do
                local mapInfo = C_Map.GetMapInfo(i);
                if ( mapInfo and mapInfo.mapType == 2 and mapInfo.parentMapID > 0 ) then
                        continentNames[mapInfo.mapID] = mapInfo.name;
                end
        end

        return continentNames;
end

function MetaMap_GetMapZones(cMapID)
        local zoneNames = { };
        local mapInfo = C_Map.GetMapChildrenInfo(cMapID);
        for key, zone in pairs(mapInfo) do
                if ( zone.mapType == 3 ) then
                        zoneNames[zone.mapID] = zone.name;
                        -- some maps have sub-maps
                        local sMapInfo = C_Map.GetMapChildrenInfo(zone.mapID);
                        for sKey, sZone in pairs(sMapInfo) do
                                if ( sZone.mapType == 3 ) then
                                        zoneNames[sZone.mapID] = sZone.name;
                                end
                        end
                end
        end

        return zoneNames;
end

--[[
function MetaMap_GetMapZoneID(continent,zoneName)
        local mapid, name
        local zonesP6 = { GetMapZones(continent) };
        for i = 2, #zonesP6, 2 do
                mapid = zonesP6[i-1];
                name = zonesP6[i];
                if ( name == zoneName ) then return mapid; end
                local subZonesP6 = { GetMapSubzones(mapid) };
                if ( #subZonesP6 > 0 ) then
                        for j = 2, #subZonesP6, 2 do
                                mapid = subZonesP6[j-1];
                                name = subZonesP6[j];
                                if ( name == zoneName ) then return mapid; end
                        end
                end
        end
end
--]]

function MetaMap_MergeZoneName(zoneTable)
        local nameid;
        if (zoneTable.nameid) then nameid = tostring(zoneTable.nameid); end
        if (nameid) then nameid = "_"..nameid; else nameid = ""; end
        local name = zoneTable[MetaMap_Locale]..nameid;

        return name;
end

function MetaMap_GetMapZoneName(areaid)
        local mapInfo = C_Map.GetMapInfo(areaid);
        if ( mapInfo ) then return mapInfo.name; end
--old
--[[
        local clist = MetaMap_GetMapContinents();
        for c = 1, #clist do
                local mapid, name
                local zonesP6 = { GetMapZones(c) };
                for i = 2, #zonesP6, 2 do
                        mapid = zonesP6[i-1];
                        name = zonesP6[i];
                        if ( mapid == areaid ) then return name; end
                        local subZonesP6 = { GetMapSubzones(mapid) };
                        if ( #subZonesP6 > 0 ) then
                                for j = 2, #subZonesP6, 2 do
                                        mapid = subZonesP6[j-1];
                                        name = subZonesP6[j];
                                        if ( mapid == areaid ) then return name; end
                                end
                        end
                end
        end
--]]
end

function MetaMap_ZoneIDToName(continentKey, zoneKey)
        if (type(zoneKey) ~= "number") then return zoneKey; end
        if (zoneKey == 0) then
                for index, cName in pairs(MetaMap_Continents) do
                        if (continentKey == index) then
                                return cName;
                        end
                end
        end
        if (continentKey > 0) then
                for zKey, zName in pairs(MetaMap_GetMapZones(continentKey)) do
                        if (zKey == zoneKey) then
                                local found;
                                for _, zoneTable in pairs(MetaMap_ZoneTable) do
                                        if (zoneTable[MetaMap_Locale] == zName) then
                                                found = true;
                                                if (not zoneTable.zoneid or zoneTable.zoneid == zKey) then
                                                        return MetaMap_MergeZoneName(zoneTable);
                                                end
                                        end
                                end
                                if (not found) then return zName; end
                        end
                end
        end
        if (continentKey == -1) then
                for index, zoneTable in pairs(MetaMap_ZoneTable) do
                        if ( zoneTable.mapid and MetaMap_UiMapID_AreaID[zoneKey] and (zoneTable.mapid == MetaMap_UiMapID_AreaID[zoneKey].AreaID) ) then
                                return MetaMap_MergeZoneName(zoneTable);
                        end
                end
                local name = MetaMap_GetMapZoneName(zoneKey);
                if (name) then return name; end
        end
        return UNKNOWN;
end

function MetaMap_GetCurrentMapDungeonLevel()
        local level;
        local mapID = WorldMapFrame:GetMapID();
        if ( mapID and MetaMap_UiMapID_AreaID[mapID] ) then
                level = MetaMap_UiMapID_AreaID[mapID].Floor;
        else
                level = 0;
        end
        return level;
end

function MetaMap_GetCurrentMapInfo()
        local mapName = "";
        local mapID = WorldMapFrame:GetMapID();
        if ( MetaMapOptions.ShowDNI ) then
                mapName = MetaMapOptions.MetaMapZone;
        else
                mapName = MetaMap_ZoneIDToName(-1, mapID);
        end
        local mapLevel = 0;
        if ( MetaMap_UiMapID_AreaID[mapID] ) then
                mapLevel = MetaMap_UiMapID_AreaID[mapID].Floor;
        end
                
--old
--[[
        local mapName, dataZone;
        local _, _, _, isMicroDungeon = GetMapInfo();
        local mapLevel = GetCurrentMapDungeonLevel();
        if ( MetaMapOptions.ShowDNI or isMicroDungeon ) then
        if ( MetaMapOptions.ShowDNI ) then
                mapName = MetaMapOptions.MetaMapZone;
        else
                local MapZone = GetCurrentMapZone();
                if ( MapZone > 0 ) then
                        mapName = MetaMap_ZoneIDToName(-1, WorldMapFrame:GetMapID());
                else
                        local mapC = GetCurrentMapContinent();
                        local mapZ = GetCurrentMapZone();
                        -- check if this is a new dungeon map
                        if ( (mapC == -1) and (mapZ == 0) ) then
                                local name = GetMapInfo();
                                if ( name ) then
                                        mapName = MetaMap_ZoneIDToName(-1, WorldMapFrame:GetMapID());
                                end
                        end
                        if ( not mapName ) then
                                mapName = MetaMap_ZoneIDToName(mapC, mapZ);
                        end
                end
        end
--]]
        return mapName, mapLevel, MetaMap_Notes[mapName];
end

function MetaMap_SplitZoneName(zoneName0)
        local zoneName = zoneName0;
        local isNameID;

        local IDidx;
        if (zoneName0) then IDidx = string.find(zoneName0,"_"); end
        if (IDidx) then
                zoneName = string.sub(zoneName0, 1, IDidx-1);
                isNameID = tonumber(string.sub(zoneName0, IDidx+1));
        end

        return zoneName, isNameID;
end

-- correction of the in-game RealZoneText
function MetaMap_GetRealZoneText()
        if ( MetaMap_BaseMap.Name ) then
                return MetaMap_BaseMap.Name;
        else
                return GetRealZoneText();
        end
end

function MetaMap_GetZoneTableEntry(zoneName0, mapid0)
        if (not zoneName0) then return; end

        local zoneName, isNameID = MetaMap_SplitZoneName(zoneName0);

        local name, ztype, mapid;
        local idx = 99999;
        -- loop over all entries, because the index is not ordered
        -- some instances are added to MetaMap_ZoneTable at loading, because they are now also listed in-game as sub-zones with type SZ, thus use entry with lowest index
        for index, zoneTable in pairs(MetaMap_ZoneTable) do
                if (zoneTable.en == zoneName or zoneTable.de == zoneName or zoneTable.fr == zoneName or zoneTable.es == zoneName or zoneTable.ru == zoneName) then
                        local found;
                        if ( not isNameID and not zoneTable.nameid ) then
                                found = true;
                        else
                                if (isNameID) then
                                        if (isNameID == zoneTable.nameid) then found = true; end
                                else
                                        local currentid = MetaMap_CurrentMap.ID;
                                        if (zoneName == MetaMap_GetRealZoneText()) then currentid = MetaMap_BaseMap.ID; end
                                        if (mapid0) then currentid = mapid0; end
                                        if (not currentid) then
                                                found = true;
                                        else
                                                if (MetaMap_UiMapID_AreaID[currentid].AreaID == zoneTable.mapid) then found = true; end
                                        end
                                end
                        end
                        if (found and index < idx) then
                                name = MetaMap_MergeZoneName(zoneTable);
                                idx = index;
                                ztype = zoneTable.ztype;
                                mapid = 0;
                                if (zoneTable.mapid ~= nil) then
                                        for i = 1, 1000 do
                                                if ( MetaMap_UiMapID_AreaID[i] and MetaMap_UiMapID_AreaID[i].AreaID == zoneTable.mapid ) then
                                                        mapid = i;
                                                end
                                        end
                                end
                        end
                end
        end
        if ( idx < 99999 ) then
                return name, idx, ztype, mapid;
        end
end

function MetaMap_NameToZoneID(zoneText)
        local _, idx, zType, mapid = MetaMap_GetZoneTableEntry(zoneText);
        if (zType == "DNI" or zType == "BG" or zType == "SCN" or zType == "RDI") then
                return -1, mapid;
        else
                if(mapid ~= 0 and mapid ~= nil and zType ~= "SZ")  then
                        MetaMap_Debug_Print("NameToZoneID: "..mapid,true);
                        return -1,mapid; 
                end;
                -- check continents first, because "The Maelstrom" is a continent and a zone
                for index, cName in pairs(MetaMap_Continents) do
                        if (zoneText == cName) then
                                return index, 0;
                        end
                end
                -- check all maps
                local zoneid;
                local name = zoneText;
                if (idx) then
                        name = MetaMap_ZoneTable[idx][MetaMap_Locale];
                        zoneid = MetaMap_ZoneTable[idx].zoneid;
                end
                for cKey, cName in pairs(MetaMap_GetMapContinents()) do
                        for zKey, zName in pairs(MetaMap_GetMapZones(cKey)) do
                                if (name == zName) then
                                        if (not zoneid or zoneid == zKey) then
                                                return cKey, zKey;
                                        end
                                end
                        end
                end
                return -1, zoneText;
        end
end

function MetaMap_SubMenuFix(self)
        local value, text;
        MetaMap_Debug_Print("MetaMap_SubMenuFix:Self="..self:GetName(),true);
        value = UIDROPDOWNMENU_MENU_VALUE;
        if (type(value) == "table") then
                text = value["func"];
        end
        return value, text;
end

function MetaMap_CheckValidZone(zoneName)
        local _, idx, zType = MetaMap_GetZoneTableEntry(zoneName);
        if (zType == "BG" or zType == "RDI" or zType == "DNI" or zType == "SCN") then
                return true;
        end
        local zoneid;
        local name = zoneName;
        if (idx) then
                name = MetaMap_ZoneTable[idx][MetaMap_Locale];
                zoneid = MetaMap_ZoneTable[idx].zoneid;
        end
        for cKey, cName in pairs(MetaMap_GetMapContinents()) do
                for zKey, zName in pairs(MetaMap_GetMapZones(cKey)) do
                        if (zName == name) then
                                if (not zoneid or zoneid == zKey) then
                                        return true;
                                end
                        end
                end
        end
        return false;
end

Compare with Previous | Blame