lotrointerface.com
Search Downloads

LoTROInterface SVN SortPack

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

Go to most recent revision | 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

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
        else
                Category=item:GetCategory()
                Quality=item:GetQuality()
                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
                                Turbine.Shell.WriteLine("<rgb=#888888>"..item:GetName().." has not been given category text by Turbine. Dammit. It's number is "..Category.." Please inform MrJackdaw!")
                                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...
                                VirtPack[i].Name=item:GetName()
                                VirtPack[i].Group=ItemGroup[ItemCategory[item:GetCategory()]]
                        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

Go to most recent revision | Compare with Previous | Blame


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


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