import "Turbine";
import "Turbine.UI";
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,
rings,
pocket,
necklace,
earrings,
bracelets,
heavyshoulder,
mediumshoulder,
lightshoulder,
heavyleg,
mediumleg,
lightleg,
heavyhead,
mediumhead,
lighthead,
heavyhand,
mediumhand,
lighthand,
heavyfeet,
mediumfeet,
lightfeet,
heavychest,
mediumchest,
lightchest,
cloaks,
heavyshield,
wardenshield,
shield,
thrownweapons,
twohandedswords,
onehandedswords,
staffs,
spears,
runestones,
onehandedmaces,
javelins,
twohandedhammers,
onehandedhammers,
halberds,
daggers,
crossbows,
twohandedclubs,
onehandedclubs,
bows,
twohandedaxes,
onehandedaxes,
dye,
miscdevices,
questitems,
recipe,
crit,
scholarmats,
gem,
wood,
leather,
ore,
shard,
deconstructables,
reputationitems,
skirmishbarter,
tokens,
travellingrations,
itemadvfood,
beer,
woundresistfood,
poisonresistfood,
fearresistfood,
diseaseresistfood,
willfood,
vitalityfood,
mightfood,
fatefood,
agilityfood,
incombatpowerfood,
incombatmoralefood,
dothealmoralefood,
noncombatmoralefood,
curative,
potion,
buff,
keys,
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)
local player = Turbine.Gameplay.LocalPlayer.GetInstance(); --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={}
function SortPackNow()
if sorting~=true then Turbine.Shell.WriteLine("Already sorting - gotcha ya nasty bug!") return end
--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
count=0
swop(i,VirtualBackpack[i].id)
end
--Save a copy
Turbine.PluginData.Save( Turbine.DataScope.Character, "PackInfo",BackpackContents)
end
function swop (i,j)
local jitem=backpack:GetItem(j) --Source item
local iitem=backpack:GetItem(i) --Target slot
if jitem~=nil then
if iitem~=nil then
if jitem:GetName()~=iitem:GetName() then
--Turbine.Shell.WriteLine("Swopping "..jitem:GetName().." does not equal "..iitem:GetName())
backpack:PerformItemDrop(jitem,i, false)
--backpack:PerformItemDrop(iitem,j,false)
end-- Perform the change ONLY IF THE ITEMS ARE NOT THE SAME.
else
--Turbine.Shell.WriteLine("Swopping "..jitem:GetName().." with blank slot")
backpack:PerformItemDrop(jitem,i, false) --iitem is empty - perform the placement!
end
end
end
Turbine.Shell.WriteLine("SortPack loaded - type /sort to run")