import "Turbine.UI";
import "Turbine.Utils";
import "Turbine.UI.Lotro";
import "Turbine.Gameplay";
import"JackdawPlugins.SortPack.functions";
import"JackdawPlugins.SortPack.List";
--[[
--SortPack by MrJackdaw
--*************************
--What it does: Sorts the Backpacks
--Updates:
--Note: My coding experience is all from looking at other peoples work and tinkering - so this is probably full of bad, wicked syntax. Forgive me! Chastise me! But don't hate me... *grin*
--TODO
--On hold now till GetCategory actually DOES something! All of the other data is pretty useless too. So, until they sort it out, the 'value' list below helps sort it out, and after that it is alphabetical.]]--
--Define the 'Importance' list
--This is in *reverse* order - more important items AT THE BOTTOM!
mergetable(value,{
trash,
weapons,
armour,
recipe,
crit,
scholarmats,
gem,
wood,
leather,
ore,
shard,
skirmishbarter,
tokens,
travellingrations,
itemadvfood,
beer,
woundresistfood,
poisonresistfood,
fearresistfood,
diseaseresistfood,
willfood,
vitalityfood,
mightfood,
fatefood,
agilityfood,
incombatpowerfood,
incombatmoralefood,
dothealmoralefood,
noncombatmoralefood,
curative,
potion,
buff,
misc,
captain,
woodworkingtools,
tailortools,
smithingtools,
scholartools,
prospectortools,
jewellertools,
forestertools,
farmingtools,
cookingtools,
map})
--Register a slash command
shellCommand = Turbine.ShellCommand()
function shellCommand:Execute(command, i)
sorting=true
SortPackNow()
sorting=false
--Now run a little proggy just to see if it actually did it's work!
--checker()
--temp()
end
Turbine.Shell.AddCommand('Sort', shellCommand)
function SortPackNow()
if sorting~=true then Turbine.Shell.WriteLine("Already sorting - gotcha ya nasty bug!") return end
local player = Turbine.Gameplay.LocalPlayer(); --Define player to be the player
local backpack = player:GetBackpack(); --Players backpack object
local size =backpack:GetSize() --How big it is
--Store the backpack contents seperately - just easier to understand later and lower server load (possibly)
local BackpackContents = {}
--Also create a "Virtual Backpack" - storing an image of the weights and id of each item - the id is its beginning slot location.
local VirtualBackpack={}
--Retrieve backpack contents
for i = 1, size do
item = backpack:GetItem(i);
BackpackContents[i]={}
if item == nil then
--Empty slots have no name, quality, category etc
BackpackContents[i].Name=0
BackpackContents[i].Category=0
BackpackContents[i].Durability=0
BackpackContents[i].Quality=0
BackpackContents[i].WearState=0
else
--Turbine.Shell.WriteLine(item:GetName());
BackpackContents[i].Name=item:GetName()
BackpackContents[i].Category=item:GetCategory()
BackpackContents[i].Durability=item:GetDurability()
BackpackContents[i].Quality=item:GetQuality()
BackpackContents[i].WearState=item:GetWearState()
end
--Calculate the weight
local Weight = 0
Weight=SearchTable(value,BackpackContents[i].Name)*100 --First scan the Name using the value table
Weight=Weight+90-string.byte(BackpackContents[i].Name) --Finally, all things being equal, alphabetical. A=65 by the way so (90-) is there for a reason!
if item==nil then Weight = -100 end
--Build a "virtual" backpack. This stores the weights etc and is sorted later before being copied to the real backpack
VirtualBackpack[i]={Weight=Weight,id=i}
end
--Sort VirtualBackpack by weight. This is a simple sort from http://www.lua.org/pil/6.html
table.sort(VirtualBackpack, function (a,b) return (a.Weight > b.Weight) end)
--
--Now, map the virtual backpack to the real backpack
for i = 1, size do
item = backpack:GetItem(VirtualBackpack[i].id);
if item ~= nil then
backpack:PerformItemDrop(item,i, false)
--Turbine.Shell.WriteLine(item:GetName().." has a weight of "..VirtualBackpack[i].Weight) <<Just in case I want to check it is doing its job right.
end
end
--Save a copy
Turbine.PluginData.Save( Turbine.DataScope.Character, "PackInfo",BackpackContents)
end
Turbine.Shell.WriteLine("SortPack loaded - type /sort to run")