lotrointerface.com
Search Downloads

LoTROInterface SVN SortPack

[/] [trunk/] [JackdawPlugins/] [SortPack/] [functions.LUA] - Rev 160

Compare with Previous | Blame | View Log

function mergetable(table1,list)
        --Appends tables from list on the end of table1
        for i=1,#list do
                table2=list[i]
                        local LenTable1=#table1
                        for a =LenTable1+1,#table1+#table2 do
                                table1[a]=table2[a-LenTable1]
                        end
        end
end

--Frame control - Gives a nice border to the window.
Frame=class(Turbine.UI.Control)

function Frame:Constructor()
Turbine.UI.Control.Constructor( self );
        self.base=Turbine.UI.Control()
        self.base:SetBackColor(Turbine.UI.Color(1,1,1,1))
        self.base:SetParent(self)
        self.high=Turbine.UI.Control()
        self.high:SetBackColor(Turbine.UI.Color(1,0,0,0))
        self.high:SetPosition(1,1)
        self.high:SetParent(self.base)
        self:SetVisible(true)

        self.SizeChanged=function()
                for i=1,#self.SizeChange do
                        self.SizeChange[i]()
                end
        end
        self.SizeChange={}
        table.insert(self.SizeChange,function()
                local x,y=self:GetSize()
                self.base:SetSize(x,y)
                self.high:SetSize(x-2,y-2)
        end)
end

--This is a modifciation to the default "GetCategory" function to allow custom categories
GetCat=function(self)
        local itemInfo=self:GetItemInfo();
        local name=itemInfo:GetName()
        if CustomItem[name]~=nil then return CustomItem[name].Cat end
        return itemInfo:GetCategory() 
end

--[[

                                local itemInfo=tmpItem:GetItemInfo();
                                BagData[characterName][tmpIndex]= {tmpIndex,itemInfo:GetDurability(),itemInfo:GetQuality(),tmpItem:GetWearState(),itemInfo:GetName(),tmpItem:GetQuantity(),{},itemInfo:GetBackgroundImageID(),itemInfo:GetIconImageID(),itemInfo:GetQualityImageID(),itemInfo:GetShadowImageID(),itemInfo:GetUnderlayImageID(),itemInfo:GetCategory(), itemInfo:IsMagic(), itemInfo:IsUnique(), 0};

]]

function weight(i)
        --Calculate the weight
        local Weight = 0
        local Category
        local Quality
        local Name
        
        local item = backpack:GetItem(i)
        
        if item == nil then
                --Empty slots have no name, quality, category etc
                Category=0
                Quality=0
                Name=""
        else
                local itemInfo=item:GetItemInfo()
                Category=GetCat(item)
                Quality=itemInfo:GetQuality()
                Name=itemInfo:GetName()
                if Quality==nil then Quality=0 end
        end

                local CategoryText=ItemCategory[Category]
                if CategoryText~=nil then       
                        CatWeight=value[CategoryText]           --First scan the CategoryText using the value table
                else
                        --This item has not been given a category number by turbine - it is missing from the category text list.
                        
                        --**
                        --Added a few lines so each warning is only recieved once per load - of course as the plugin is sometimes reloaded this could be frequent, but hopefully not too frequent.
                        if warned==nil then
                                warned={}
                        end
                        if warned.Category==nil then
                                if itemInfo==nil then
                                        Turbine.Shell.WriteLine("<rgb=#888888>".."The item in slot "..i.." has not been given category text OR itemInfo by Turbine. Dammit. It's number is "..Category.." Please inform MrJackdaw!")
                                else    
                                        Turbine.Shell.WriteLine("<rgb=#888888>"..itemInfo:GetName().." has not been given category text by Turbine. Dammit. It's number is "..Category.." Please inform MrJackdaw!")
                                end
                                warned.Category=true
                        end
                        --**
                end
                                
                Weight=CatWeight*10+Quality
                
                if item == nil then Weight=99999 end

        return Weight
end

function strip(str)
        --Strip numbers first 7 characters to make the sort more logical
        local left=string.sub(str, 1, 7)
        local right=string.sub(str, 8)
        left=string.gsub(left,"[^%a]","")
        str= left .. right
        --Strip all spaces now!
        str=string.gsub(str,"[%s]","")
        return str
end

function analysepack()
        local VirtPack={}
                for i=1,size do
                        VirtPack[i]={}
                        VirtPack[i].id=i
                        item = backpack:GetItem(i)
                        if item~=nil then
                                --Data that may be useful...
                                local itemInfo=item:GetItemInfo()
                                VirtPack[i].Name=itemInfo:GetName()
                                VirtPack[i].Group=ItemGroup[ItemCategory[GetCat(item)]]
                        else
                                VirtPack[i].Name="zEmpty"
                                VirtPack[i].Group=0
                        end
                        VirtPack[i].Weight=weight(i)
                        if VirtPack[i].Group==nil then VirtPack[i].Group=0 end
                end
                
                --Sort VirtPack. If the weight of two items is the same compare the Names, after removing some characters. This way "2 Morale Potions" will get sorted with "Morale Potions"
                table.sort(VirtPack, function (a,b)     
                        if a.Weight == b.Weight then 
                                --return a.Name < b.Name
                                if revorder then return strip(a.Name)> strip(b.Name) else return strip(a.Name)< strip(b.Name) end
                        else  
                                if revorder then return (a.Weight > b.Weight) else return (a.Weight < b.Weight) end
                        end
                 end)
        return VirtPack
end

--Callback handler due to Pengoros... Many thanks!
function AddCallback(object, event, callback)
    if (object[event] == nil) then
        object[event] = callback;
    else
        if (type(object[event]) == "table") then
            table.insert(object[event], callback);
        else
            object[event] = {object[event], callback};
        end
    end
    return callback;
end

-- safely remove a callback without clobbering any extras
function RemoveCallback(object, event, callback)
    if (object[event] == callback) then
        object[event] = nil;
    else
        if (type(object[event]) == "table") then
            local size = table.getn(object[event]);
            for i = 1, size do
                if (object[event][i] == callback) then
                    table.remove(object[event], i);
                    break;
                end
            end
        end
    end
end

--Ordered pair functions from http://lua-users.org/wiki/SortedIteration
function __genOrderedIndex( t )
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert( orderedIndex, key )
    end
    table.sort( orderedIndex )
    return orderedIndex
end

function orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    --print("orderedNext: state = "..tostring(state) )
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = __genOrderedIndex( t )
        key = t.__orderedIndex[1]
        return key, t[key]
    end
    -- fetch the next value
    key = nil
    for i = 1,table.getn(t.__orderedIndex) do
        if t.__orderedIndex[i] == state then
            key = t.__orderedIndex[i+1]
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate
    -- in order
    return orderedNext, t, nil
end

Compare with Previous | Blame


All times are GMT -5. The time now is 06:25 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui