Go to most recent revision |
Compare with Previous |
Blame |
View Log
function Sort()
--If the bag function is enabled then close the sortbag before sorting and open it again when finished. Stops lots of errors.
if SortBag~=nil then
if SortBag:IsVisible() then Turbine.Shell.WriteLine("Sortbag window closed on sorting. Open again after sort.") SortBag:Close() closedsortbag=true end
end
--An attempt to make as many variables local to this proc as possible. May avoid the crash? Either way it "feels" right to have as much data disappear as possible.
local VirtPack
local item
local Category
local CategoryText
local CatWeight
local FirstStart
local Name
local Quality
local QualWeight
local Slot
local Start
Timer=Turbine.UI.Control()
function Timer:Start()
Turbine.Shell.WriteLine("<rgb=#888888>Sort Started")
FirstStart= Turbine.Engine.GetGameTime()
VirtPack=analysepack()
Slot=1
Start= Turbine.Engine.GetGameTime()
Timer:SetWantsUpdates(true)
end
function Timer:Update()
local Time = Turbine.Engine.GetGameTime()
local elapsed = Time - Start
if Slot>size then
Timer:SetWantsUpdates(false)
--If sortbag is loaded and the window was open before sorting, then open it again.
if SortBag~=nil then
if closedsortbag then closedsortbag=nil openbag() end
end
Timer=nil
Turbine.Shell.WriteLine("<rgb=#888888>Sort finished after "..Time-FirstStart.." seconds.")
return
end
--Get the name of the target item
target=VirtPack[Slot].Name
--Get item in current slot
item=backpack:GetItem(Slot)
--Get the name of the item in the current slot. If empty return that name as "zEmpty"
if item~=nil then
local itemInfo=item:GetItemInfo()
Name=itemInfo:GetName()
else
Name="zEmpty"
end
--Turbine.Shell.WriteLine("<rgb=#888888>Slot "..Slot.." should contain "..target.." but contains ".. Name)
--If the Name of the current item is equal to the target name, move on.
if Name==target then Slot=Slot+1 return end
if elapsed>0.2 then
--Search for the item in the rest of the pack.
for i=Slot+1,size do
item=backpack:GetItem(i)
if item~=nil then
local itemInfo=item:GetItemInfo()
Name=itemInfo:GetName()
--Turbine.Shell.WriteLine("Looking for "..target.." Found "..Name)
else
Name="zEmpty"
end
if Name==target then
swop(i,Slot)
Start= Turbine.Engine.GetGameTime()
return
end
end
Turbine.Shell.WriteLine("<rgb=#888888>Should never get here! Error in Update function. Did your inventory change during the sort? Restarting!")
Timer:SetWantsUpdates(false)
Timer:Start()
end
end
function swop(i,j)
if backpack:GetItem(j)==backpack:GetItem(i) then return end
item=backpack:GetItem(j)
if item~=nil then
backpack:PerformItemDrop(item,i, false)
return
end
item=backpack:GetItem(i)
if item~=nil then
backpack:PerformItemDrop(item,j, false)
return
end
Turbine.Shell.WriteLine("<rgb=#888888>Should never be seen. Error in Swop function.")
Timer:SetWantsUpdates(false)
Timer:Start()
end
Timer:Start()
end
Go to most recent revision |
Compare with Previous |
Blame
All times are GMT -5. The time now is 10:47 PM.