1 |
90 |
MrJackdaw-1942 |
function Sort() |
2 |
105 |
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 |
111 |
MrJackdaw-1942 |
if SortBag:IsVisible() then Turbine.Shell.WriteLine("Sortbag window closed on sorting. Open again after sort.") SortBag:Close() closedsortbag=true end |
5 |
105 |
MrJackdaw-1942 |
end |
6 |
90 |
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 |
101 |
MrJackdaw-1942 |
local VirtPack |
9 |
90 |
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 |
50 |
MrJackdaw-1942 |
|
20 |
90 |
MrJackdaw-1942 |
Timer=Turbine.UI.Control() |
21 |
50 |
MrJackdaw-1942 |
|
22 |
90 |
MrJackdaw-1942 |
function Timer:Start() |
23 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Sort Started") |
24 |
|
MrJackdaw-1942 |
FirstStart= Turbine.Engine.GetGameTime() |
25 |
104 |
MrJackdaw-1942 |
VirtPack=analysepack() |
26 |
50 |
MrJackdaw-1942 |
|
27 |
90 |
MrJackdaw-1942 |
Slot=1 |
28 |
|
MrJackdaw-1942 |
Start= Turbine.Engine.GetGameTime() |
29 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(true) |
30 |
|
MrJackdaw-1942 |
end |
31 |
70 |
MrJackdaw-1942 |
|
32 |
90 |
MrJackdaw-1942 |
function Timer:Update() |
33 |
|
MrJackdaw-1942 |
local Time = Turbine.Engine.GetGameTime() |
34 |
|
MrJackdaw-1942 |
local elapsed = Time - Start |
35 |
54 |
MrJackdaw-1942 |
|
36 |
90 |
MrJackdaw-1942 |
if Slot>size then |
37 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(false) |
38 |
105 |
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 |
90 |
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 |
|
MrJackdaw-1942 |
if item~=nil then Name=item:GetName() else Name="zEmpty" end |
55 |
|
MrJackdaw-1942 |
|
56 |
|
MrJackdaw-1942 |
--Turbine.Shell.WriteLine("<rgb=#888888>Slot "..Slot.." should contain "..target.." but contains ".. Name) |
57 |
|
MrJackdaw-1942 |
|
58 |
|
MrJackdaw-1942 |
--If the Name of the current item is equal to the target name, move on. |
59 |
|
MrJackdaw-1942 |
if Name==target then Slot=Slot+1 return end |
60 |
53 |
MrJackdaw-1942 |
|
61 |
90 |
MrJackdaw-1942 |
if elapsed>0.2 then |
62 |
|
MrJackdaw-1942 |
--Search for the item in the rest of the pack. |
63 |
|
MrJackdaw-1942 |
for i=Slot+1,size do |
64 |
|
MrJackdaw-1942 |
item=backpack:GetItem(i) |
65 |
|
MrJackdaw-1942 |
if item~=nil then item=item:GetName() else item="zEmpty" end |
66 |
|
MrJackdaw-1942 |
if item==target then |
67 |
|
MrJackdaw-1942 |
swop(i,Slot) |
68 |
|
MrJackdaw-1942 |
Start= Turbine.Engine.GetGameTime() |
69 |
|
MrJackdaw-1942 |
return |
70 |
|
MrJackdaw-1942 |
end |
71 |
53 |
MrJackdaw-1942 |
end |
72 |
90 |
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Should never get here! Error in Update function. Did your inventory change during the sort? Restarting!") |
73 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(false) |
74 |
|
MrJackdaw-1942 |
Timer:Start() |
75 |
53 |
MrJackdaw-1942 |
end |
76 |
|
MrJackdaw-1942 |
end |
77 |
52 |
MrJackdaw-1942 |
|
78 |
90 |
MrJackdaw-1942 |
function swop(i,j) |
79 |
52 |
MrJackdaw-1942 |
|
80 |
90 |
MrJackdaw-1942 |
if backpack:GetItem(j)==backpack:GetItem(i) then return end |
81 |
|
MrJackdaw-1942 |
|
82 |
|
MrJackdaw-1942 |
item=backpack:GetItem(j) |
83 |
53 |
MrJackdaw-1942 |
|
84 |
90 |
MrJackdaw-1942 |
if item~=nil then |
85 |
|
MrJackdaw-1942 |
backpack:PerformItemDrop(item,i, false) |
86 |
|
MrJackdaw-1942 |
return |
87 |
|
MrJackdaw-1942 |
end |
88 |
|
MrJackdaw-1942 |
|
89 |
|
MrJackdaw-1942 |
item=backpack:GetItem(i) |
90 |
53 |
MrJackdaw-1942 |
|
91 |
90 |
MrJackdaw-1942 |
if item~=nil then |
92 |
|
MrJackdaw-1942 |
backpack:PerformItemDrop(item,j, false) |
93 |
|
MrJackdaw-1942 |
return |
94 |
|
MrJackdaw-1942 |
end |
95 |
|
MrJackdaw-1942 |
|
96 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>Should never be seen. Error in Swop function.") |
97 |
|
MrJackdaw-1942 |
Timer:SetWantsUpdates(false) |
98 |
|
MrJackdaw-1942 |
Timer:Start() |
99 |
53 |
MrJackdaw-1942 |
end |
100 |
|
MrJackdaw-1942 |
|
101 |
104 |
MrJackdaw-1942 |
Timer:Start() |
102 |
|
MrJackdaw-1942 |
end |
103 |
|
MrJackdaw-1942 |
|
104 |
|
MrJackdaw-1942 |
function weight(i) |
105 |
|
MrJackdaw-1942 |
--Calculate the weight |
106 |
|
MrJackdaw-1942 |
local Weight = 0 |
107 |
|
MrJackdaw-1942 |
|
108 |
|
MrJackdaw-1942 |
item = backpack:GetItem(i) |
109 |
99 |
MrJackdaw-1942 |
|
110 |
104 |
MrJackdaw-1942 |
if item == nil then |
111 |
|
MrJackdaw-1942 |
--Empty slots have no name, quality, category etc |
112 |
|
MrJackdaw-1942 |
Category=0 |
113 |
|
MrJackdaw-1942 |
Quality=0 |
114 |
|
MrJackdaw-1942 |
else |
115 |
|
MrJackdaw-1942 |
Category=item:GetCategory() |
116 |
|
MrJackdaw-1942 |
Quality=item:GetQuality() |
117 |
|
MrJackdaw-1942 |
if Quality==nil then Quality=0 end |
118 |
|
MrJackdaw-1942 |
end |
119 |
|
MrJackdaw-1942 |
|
120 |
|
MrJackdaw-1942 |
CategoryText=ItemCategory[Category] |
121 |
|
MrJackdaw-1942 |
if CategoryText~=nil then |
122 |
|
MrJackdaw-1942 |
CatWeight=value[CategoryText] --First scan the CategoryText using the value table |
123 |
65 |
MrJackdaw-1942 |
else |
124 |
104 |
MrJackdaw-1942 |
--This item has not been given a category number by turbine - it is missing from the category text list. |
125 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>"..item:GetName().." has not been given category text by Turbine. Dammit. It's number is "..Category.." Please inform MrJackdaw!") |
126 |
65 |
MrJackdaw-1942 |
end |
127 |
104 |
MrJackdaw-1942 |
|
128 |
|
MrJackdaw-1942 |
QualWeight=Quality |
129 |
65 |
MrJackdaw-1942 |
|
130 |
105 |
MrJackdaw-1942 |
Weight=CatWeight*100+QualWeight |
131 |
104 |
MrJackdaw-1942 |
|
132 |
|
MrJackdaw-1942 |
if item==nil then Weight = 99999 end |
133 |
|
MrJackdaw-1942 |
|
134 |
|
MrJackdaw-1942 |
return Weight |
135 |
|
MrJackdaw-1942 |
end |
136 |
|
MrJackdaw-1942 |
|
137 |
|
MrJackdaw-1942 |
function strip(str) |
138 |
|
MrJackdaw-1942 |
--Strip numbers first 7 characters to make the sort more logical |
139 |
|
MrJackdaw-1942 |
local left=string.sub(str, 1, 7) |
140 |
|
MrJackdaw-1942 |
local right=string.sub(str, 8) |
141 |
|
MrJackdaw-1942 |
left=string.gsub(left,"[^%a]","") |
142 |
|
MrJackdaw-1942 |
str= left .. right |
143 |
|
MrJackdaw-1942 |
--Strip all spaces now! |
144 |
|
MrJackdaw-1942 |
str=string.gsub(str,"[%s]","") |
145 |
|
MrJackdaw-1942 |
return str |
146 |
|
MrJackdaw-1942 |
end |
147 |
|
MrJackdaw-1942 |
|
148 |
|
MrJackdaw-1942 |
function analysepack() |
149 |
|
MrJackdaw-1942 |
local VirtPack={} |
150 |
|
MrJackdaw-1942 |
for i=1,size do |
151 |
|
MrJackdaw-1942 |
VirtPack[i]={} |
152 |
|
MrJackdaw-1942 |
VirtPack[i].id=i |
153 |
|
MrJackdaw-1942 |
item = backpack:GetItem(i) |
154 |
|
MrJackdaw-1942 |
if item~=nil then |
155 |
105 |
MrJackdaw-1942 |
--Data that may be useful... |
156 |
104 |
MrJackdaw-1942 |
VirtPack[i].Name=item:GetName() |
157 |
105 |
MrJackdaw-1942 |
VirtPack[i].Group=ItemGroup[ItemCategory[item:GetCategory()]] |
158 |
90 |
MrJackdaw-1942 |
else |
159 |
104 |
MrJackdaw-1942 |
VirtPack[i].Name="zEmpty" |
160 |
105 |
MrJackdaw-1942 |
VirtPack[i].Group=0 |
161 |
90 |
MrJackdaw-1942 |
end |
162 |
104 |
MrJackdaw-1942 |
VirtPack[i].Weight=weight(i) |
163 |
|
MrJackdaw-1942 |
end |
164 |
|
MrJackdaw-1942 |
|
165 |
|
MrJackdaw-1942 |
--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" |
166 |
|
MrJackdaw-1942 |
table.sort(VirtPack, function (a,b) |
167 |
|
MrJackdaw-1942 |
if a.Weight == b.Weight then |
168 |
|
MrJackdaw-1942 |
--return a.Name < b.Name |
169 |
111 |
MrJackdaw-1942 |
if revorder then return strip(a.Name)> strip(b.Name) else return strip(a.Name)< strip(b.Name) end |
170 |
|
MrJackdaw-1942 |
|
171 |
104 |
MrJackdaw-1942 |
else |
172 |
111 |
MrJackdaw-1942 |
if revorder then return (a.Weight > b.Weight) else return (a.Weight < b.Weight) end |
173 |
104 |
MrJackdaw-1942 |
end |
174 |
|
MrJackdaw-1942 |
end) |
175 |
|
MrJackdaw-1942 |
return VirtPack |
176 |
90 |
MrJackdaw-1942 |
end |