1 |
112 |
MrJackdaw-1942 |
function Sort() |
2 |
|
MrJackdaw-1942 |
--If the bag function is enabled then close the sortbag before sorting and open it again when finished. Stops lots of errors. |
3 |
|
MrJackdaw-1942 |
if SortBag~=nil then |
4 |
|
MrJackdaw-1942 |
if SortBag:IsVisible() then Turbine.Shell.WriteLine("Sortbag window closed on sorting. Open again after sort.") SortBag:Close() closedsortbag=true end |
5 |
|
MrJackdaw-1942 |
end |
6 |
|
MrJackdaw-1942 |
|
7 |
|
MrJackdaw-1942 |
--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. |
8 |
|
MrJackdaw-1942 |
local VirtPack |
9 |
|
MrJackdaw-1942 |
local item |
10 |
|
MrJackdaw-1942 |
local Category |
11 |
|
MrJackdaw-1942 |
local CategoryText |
12 |
|
MrJackdaw-1942 |
local CatWeight |
13 |
|
MrJackdaw-1942 |
local FirstStart |
14 |
|
MrJackdaw-1942 |
local Name |
15 |
|
MrJackdaw-1942 |
local Quality |
16 |
|
MrJackdaw-1942 |
local QualWeight |
17 |
|
MrJackdaw-1942 |
local Slot |
18 |
|
MrJackdaw-1942 |
local Start |
19 |
|
MrJackdaw-1942 |
|
20 |
|
MrJackdaw-1942 |
Timer=Turbine.UI.Control() |
21 |
|
MrJackdaw-1942 |
|
22 |
|
MrJackdaw-1942 |
function Timer:Start() |
23 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Sort Started") |
24 |
|
MrJackdaw-1942 |
FirstStart= Turbine.Engine.GetGameTime() |
25 |
|
MrJackdaw-1942 |
VirtPack=analysepack() |
26 |
|
MrJackdaw-1942 |
|
27 |
|
MrJackdaw-1942 |
Slot=1 |
28 |
|
MrJackdaw-1942 |
Start= Turbine.Engine.GetGameTime() |
29 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(true) |
30 |
|
MrJackdaw-1942 |
end |
31 |
|
MrJackdaw-1942 |
|
32 |
|
MrJackdaw-1942 |
function Timer:Update() |
33 |
|
MrJackdaw-1942 |
local Time = Turbine.Engine.GetGameTime() |
34 |
|
MrJackdaw-1942 |
local elapsed = Time - Start |
35 |
|
MrJackdaw-1942 |
|
36 |
|
MrJackdaw-1942 |
if Slot>size then |
37 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(false) |
38 |
|
MrJackdaw-1942 |
--If sortbag is loaded and the window was open before sorting, then open it again. |
39 |
|
MrJackdaw-1942 |
if SortBag~=nil then |
40 |
|
MrJackdaw-1942 |
if closedsortbag then closedsortbag=nil openbag() end |
41 |
|
MrJackdaw-1942 |
end |
42 |
|
MrJackdaw-1942 |
Timer=nil |
43 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Sort finished after "..Time-FirstStart.." seconds.") |
44 |
|
MrJackdaw-1942 |
return |
45 |
|
MrJackdaw-1942 |
end |
46 |
|
MrJackdaw-1942 |
|
47 |
|
MrJackdaw-1942 |
--Get the name of the target item |
48 |
|
MrJackdaw-1942 |
target=VirtPack[Slot].Name |
49 |
|
MrJackdaw-1942 |
|
50 |
|
MrJackdaw-1942 |
--Get item in current slot |
51 |
|
MrJackdaw-1942 |
item=backpack:GetItem(Slot) |
52 |
|
MrJackdaw-1942 |
|
53 |
|
MrJackdaw-1942 |
--Get the name of the item in the current slot. If empty return that name as "zEmpty" |
54 |
156 |
MrJackdaw-1942 |
if item~=nil then |
55 |
|
MrJackdaw-1942 |
local itemInfo=item:GetItemInfo() |
56 |
|
MrJackdaw-1942 |
Name=itemInfo:GetName() |
57 |
|
MrJackdaw-1942 |
else |
58 |
|
MrJackdaw-1942 |
Name="zEmpty" |
59 |
|
MrJackdaw-1942 |
end |
60 |
112 |
MrJackdaw-1942 |
|
61 |
|
MrJackdaw-1942 |
--Turbine.Shell.WriteLine("<rgb=#888888>Slot "..Slot.." should contain "..target.." but contains ".. Name) |
62 |
|
MrJackdaw-1942 |
|
63 |
|
MrJackdaw-1942 |
--If the Name of the current item is equal to the target name, move on. |
64 |
|
MrJackdaw-1942 |
if Name==target then Slot=Slot+1 return end |
65 |
|
MrJackdaw-1942 |
|
66 |
|
MrJackdaw-1942 |
if elapsed>0.2 then |
67 |
|
MrJackdaw-1942 |
--Search for the item in the rest of the pack. |
68 |
|
MrJackdaw-1942 |
for i=Slot+1,size do |
69 |
|
MrJackdaw-1942 |
item=backpack:GetItem(i) |
70 |
156 |
MrJackdaw-1942 |
if item~=nil then |
71 |
|
MrJackdaw-1942 |
local itemInfo=item:GetItemInfo() |
72 |
|
MrJackdaw-1942 |
Name=itemInfo:GetName() |
73 |
|
MrJackdaw-1942 |
--Turbine.Shell.WriteLine("Looking for "..target.." Found "..Name) |
74 |
|
MrJackdaw-1942 |
else |
75 |
|
MrJackdaw-1942 |
Name="zEmpty" |
76 |
|
MrJackdaw-1942 |
end |
77 |
|
MrJackdaw-1942 |
if Name==target then |
78 |
112 |
MrJackdaw-1942 |
swop(i,Slot) |
79 |
|
MrJackdaw-1942 |
Start= Turbine.Engine.GetGameTime() |
80 |
|
MrJackdaw-1942 |
return |
81 |
|
MrJackdaw-1942 |
end |
82 |
|
MrJackdaw-1942 |
end |
83 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Should never get here! Error in Update function. Did your inventory change during the sort? Restarting!") |
84 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(false) |
85 |
|
MrJackdaw-1942 |
Timer:Start() |
86 |
|
MrJackdaw-1942 |
end |
87 |
|
MrJackdaw-1942 |
end |
88 |
|
MrJackdaw-1942 |
|
89 |
|
MrJackdaw-1942 |
function swop(i,j) |
90 |
|
MrJackdaw-1942 |
|
91 |
|
MrJackdaw-1942 |
if backpack:GetItem(j)==backpack:GetItem(i) then return end |
92 |
|
MrJackdaw-1942 |
|
93 |
|
MrJackdaw-1942 |
item=backpack:GetItem(j) |
94 |
|
MrJackdaw-1942 |
|
95 |
|
MrJackdaw-1942 |
if item~=nil then |
96 |
|
MrJackdaw-1942 |
backpack:PerformItemDrop(item,i, false) |
97 |
|
MrJackdaw-1942 |
return |
98 |
|
MrJackdaw-1942 |
end |
99 |
|
MrJackdaw-1942 |
|
100 |
|
MrJackdaw-1942 |
item=backpack:GetItem(i) |
101 |
|
MrJackdaw-1942 |
|
102 |
|
MrJackdaw-1942 |
if item~=nil then |
103 |
|
MrJackdaw-1942 |
backpack:PerformItemDrop(item,j, false) |
104 |
|
MrJackdaw-1942 |
return |
105 |
|
MrJackdaw-1942 |
end |
106 |
|
MrJackdaw-1942 |
|
107 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Should never be seen. Error in Swop function.") |
108 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(false) |
109 |
|
MrJackdaw-1942 |
Timer:Start() |
110 |
|
MrJackdaw-1942 |
end |
111 |
|
MrJackdaw-1942 |
|
112 |
|
MrJackdaw-1942 |
Timer:Start() |
113 |
|
MrJackdaw-1942 |
end |