1 |
3 |
MrJackdaw-1942 |
function mergetable(table1,list) |
2 |
53 |
MrJackdaw-1942 |
--Appends tables from list on the end of table1 |
3 |
|
MrJackdaw-1942 |
for i=1,#list do |
4 |
|
MrJackdaw-1942 |
table2=list[i] |
5 |
|
MrJackdaw-1942 |
local LenTable1=#table1 |
6 |
|
MrJackdaw-1942 |
for a =LenTable1+1,#table1+#table2 do |
7 |
|
MrJackdaw-1942 |
table1[a]=table2[a-LenTable1] |
8 |
|
MrJackdaw-1942 |
end |
9 |
|
MrJackdaw-1942 |
end |
10 |
100 |
MrJackdaw-1942 |
end |
11 |
|
MrJackdaw-1942 |
|
12 |
|
MrJackdaw-1942 |
--Frame control - Gives a nice border to the window. |
13 |
|
MrJackdaw-1942 |
Frame=class(Turbine.UI.Control) |
14 |
|
MrJackdaw-1942 |
|
15 |
|
MrJackdaw-1942 |
function Frame:Constructor() |
16 |
|
MrJackdaw-1942 |
Turbine.UI.Control.Constructor( self ); |
17 |
|
MrJackdaw-1942 |
self.base=Turbine.UI.Control() |
18 |
|
MrJackdaw-1942 |
self.base:SetBackColor(Turbine.UI.Color(1,1,1,1)) |
19 |
|
MrJackdaw-1942 |
self.base:SetParent(self) |
20 |
|
MrJackdaw-1942 |
self.high=Turbine.UI.Control() |
21 |
|
MrJackdaw-1942 |
self.high:SetBackColor(Turbine.UI.Color(1,0,0,0)) |
22 |
|
MrJackdaw-1942 |
self.high:SetPosition(1,1) |
23 |
|
MrJackdaw-1942 |
self.high:SetParent(self.base) |
24 |
|
MrJackdaw-1942 |
self:SetVisible(true) |
25 |
|
MrJackdaw-1942 |
|
26 |
|
MrJackdaw-1942 |
self.SizeChanged=function() |
27 |
|
MrJackdaw-1942 |
for i=1,#self.SizeChange do |
28 |
|
MrJackdaw-1942 |
self.SizeChange[i]() |
29 |
|
MrJackdaw-1942 |
end |
30 |
|
MrJackdaw-1942 |
end |
31 |
|
MrJackdaw-1942 |
self.SizeChange={} |
32 |
|
MrJackdaw-1942 |
table.insert(self.SizeChange,function() |
33 |
|
MrJackdaw-1942 |
local x,y=self:GetSize() |
34 |
|
MrJackdaw-1942 |
self.base:SetSize(x,y) |
35 |
|
MrJackdaw-1942 |
self.high:SetSize(x-2,y-2) |
36 |
|
MrJackdaw-1942 |
end) |
37 |
|
MrJackdaw-1942 |
end |
38 |
111 |
MrJackdaw-1942 |
|
39 |
137 |
MrJackdaw-1942 |
--This is a modifciation to the default "GetCategory" function to allow custom categories |
40 |
156 |
MrJackdaw-1942 |
GetCat=function(self) |
41 |
|
MrJackdaw-1942 |
local itemInfo=self:GetItemInfo(); |
42 |
|
MrJackdaw-1942 |
local name=itemInfo:GetName() |
43 |
138 |
MrJackdaw-1942 |
if CustomItem[name]~=nil then return CustomItem[name].Cat end |
44 |
156 |
MrJackdaw-1942 |
return itemInfo:GetCategory() |
45 |
137 |
MrJackdaw-1942 |
end |
46 |
|
MrJackdaw-1942 |
|
47 |
156 |
MrJackdaw-1942 |
--[[ |
48 |
|
MrJackdaw-1942 |
|
49 |
|
MrJackdaw-1942 |
local itemInfo=tmpItem:GetItemInfo(); |
50 |
|
MrJackdaw-1942 |
BagData[characterName][tmpIndex]= {tmpIndex,itemInfo:GetDurability(),itemInfo:GetQuality(),tmpItem:GetWearState(),itemInfo:GetName(),tmpItem:GetQuantity(),{},itemInfo:GetBackgroundImageID(),itemInfo:GetIconImageID(),itemInfo:GetQualityImageID(),itemInfo:GetShadowImageID(),itemInfo:GetUnderlayImageID(),itemInfo:GetCategory(), itemInfo:IsMagic(), itemInfo:IsUnique(), 0}; |
51 |
|
MrJackdaw-1942 |
|
52 |
|
MrJackdaw-1942 |
]] |
53 |
|
MrJackdaw-1942 |
|
54 |
112 |
MrJackdaw-1942 |
function weight(i) |
55 |
|
MrJackdaw-1942 |
--Calculate the weight |
56 |
|
MrJackdaw-1942 |
local Weight = 0 |
57 |
128 |
MrJackdaw-1942 |
local Category |
58 |
|
MrJackdaw-1942 |
local Quality |
59 |
|
MrJackdaw-1942 |
local Name |
60 |
112 |
MrJackdaw-1942 |
|
61 |
128 |
MrJackdaw-1942 |
local item = backpack:GetItem(i) |
62 |
|
MrJackdaw-1942 |
|
63 |
112 |
MrJackdaw-1942 |
if item == nil then |
64 |
|
MrJackdaw-1942 |
--Empty slots have no name, quality, category etc |
65 |
|
MrJackdaw-1942 |
Category=0 |
66 |
|
MrJackdaw-1942 |
Quality=0 |
67 |
136 |
MrJackdaw-1942 |
Name="" |
68 |
112 |
MrJackdaw-1942 |
else |
69 |
156 |
MrJackdaw-1942 |
local itemInfo=item:GetItemInfo() |
70 |
|
MrJackdaw-1942 |
Category=GetCat(item) |
71 |
|
MrJackdaw-1942 |
Quality=itemInfo:GetQuality() |
72 |
|
MrJackdaw-1942 |
Name=itemInfo:GetName() |
73 |
112 |
MrJackdaw-1942 |
if Quality==nil then Quality=0 end |
74 |
111 |
MrJackdaw-1942 |
end |
75 |
|
MrJackdaw-1942 |
|
76 |
129 |
MrJackdaw-1942 |
local CategoryText=ItemCategory[Category] |
77 |
112 |
MrJackdaw-1942 |
if CategoryText~=nil then |
78 |
|
MrJackdaw-1942 |
CatWeight=value[CategoryText] --First scan the CategoryText using the value table |
79 |
|
MrJackdaw-1942 |
else |
80 |
|
MrJackdaw-1942 |
--This item has not been given a category number by turbine - it is missing from the category text list. |
81 |
134 |
MrJackdaw-1942 |
|
82 |
|
MrJackdaw-1942 |
--** |
83 |
|
MrJackdaw-1942 |
--Added a few lines so each warning is only recieved once per load - of course as the plugin is sometimes reloaded this could be frequent, but hopefully not too frequent. |
84 |
|
MrJackdaw-1942 |
if warned==nil then |
85 |
|
MrJackdaw-1942 |
warned={} |
86 |
|
MrJackdaw-1942 |
end |
87 |
|
MrJackdaw-1942 |
if warned.Category==nil then |
88 |
160 |
MrJackdaw-1942 |
if itemInfo==nil then |
89 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>".."The item in slot "..i.." has not been given category text OR itemInfo by Turbine. Dammit. It's number is "..Category.." Please inform MrJackdaw!") |
90 |
|
MrJackdaw-1942 |
else |
91 |
|
MrJackdaw-1942 |
Turbine.Shell.WriteLine("<rgb=#888888>"..itemInfo:GetName().." has not been given category text by Turbine. Dammit. It's number is "..Category.." Please inform MrJackdaw!") |
92 |
|
MrJackdaw-1942 |
end |
93 |
134 |
MrJackdaw-1942 |
warned.Category=true |
94 |
|
MrJackdaw-1942 |
end |
95 |
|
MrJackdaw-1942 |
--** |
96 |
111 |
MrJackdaw-1942 |
end |
97 |
129 |
MrJackdaw-1942 |
|
98 |
|
MrJackdaw-1942 |
Weight=CatWeight*10+Quality |
99 |
132 |
MrJackdaw-1942 |
|
100 |
|
MrJackdaw-1942 |
if item == nil then Weight=99999 end |
101 |
111 |
MrJackdaw-1942 |
|
102 |
112 |
MrJackdaw-1942 |
return Weight |
103 |
|
MrJackdaw-1942 |
end |
104 |
111 |
MrJackdaw-1942 |
|
105 |
112 |
MrJackdaw-1942 |
function strip(str) |
106 |
|
MrJackdaw-1942 |
--Strip numbers first 7 characters to make the sort more logical |
107 |
|
MrJackdaw-1942 |
local left=string.sub(str, 1, 7) |
108 |
|
MrJackdaw-1942 |
local right=string.sub(str, 8) |
109 |
|
MrJackdaw-1942 |
left=string.gsub(left,"[^%a]","") |
110 |
|
MrJackdaw-1942 |
str= left .. right |
111 |
|
MrJackdaw-1942 |
--Strip all spaces now! |
112 |
|
MrJackdaw-1942 |
str=string.gsub(str,"[%s]","") |
113 |
|
MrJackdaw-1942 |
return str |
114 |
|
MrJackdaw-1942 |
end |
115 |
111 |
MrJackdaw-1942 |
|
116 |
112 |
MrJackdaw-1942 |
function analysepack() |
117 |
|
MrJackdaw-1942 |
local VirtPack={} |
118 |
|
MrJackdaw-1942 |
for i=1,size do |
119 |
|
MrJackdaw-1942 |
VirtPack[i]={} |
120 |
|
MrJackdaw-1942 |
VirtPack[i].id=i |
121 |
|
MrJackdaw-1942 |
item = backpack:GetItem(i) |
122 |
|
MrJackdaw-1942 |
if item~=nil then |
123 |
|
MrJackdaw-1942 |
--Data that may be useful... |
124 |
156 |
MrJackdaw-1942 |
local itemInfo=item:GetItemInfo() |
125 |
|
MrJackdaw-1942 |
VirtPack[i].Name=itemInfo:GetName() |
126 |
|
MrJackdaw-1942 |
VirtPack[i].Group=ItemGroup[ItemCategory[GetCat(item)]] |
127 |
112 |
MrJackdaw-1942 |
else |
128 |
|
MrJackdaw-1942 |
VirtPack[i].Name="zEmpty" |
129 |
|
MrJackdaw-1942 |
VirtPack[i].Group=0 |
130 |
|
MrJackdaw-1942 |
end |
131 |
|
MrJackdaw-1942 |
VirtPack[i].Weight=weight(i) |
132 |
134 |
MrJackdaw-1942 |
if VirtPack[i].Group==nil then VirtPack[i].Group=0 end |
133 |
111 |
MrJackdaw-1942 |
end |
134 |
112 |
MrJackdaw-1942 |
|
135 |
|
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" |
136 |
|
MrJackdaw-1942 |
table.sort(VirtPack, function (a,b) |
137 |
|
MrJackdaw-1942 |
if a.Weight == b.Weight then |
138 |
|
MrJackdaw-1942 |
--return a.Name < b.Name |
139 |
|
MrJackdaw-1942 |
if revorder then return strip(a.Name)> strip(b.Name) else return strip(a.Name)< strip(b.Name) end |
140 |
|
MrJackdaw-1942 |
else |
141 |
|
MrJackdaw-1942 |
if revorder then return (a.Weight > b.Weight) else return (a.Weight < b.Weight) end |
142 |
|
MrJackdaw-1942 |
end |
143 |
|
MrJackdaw-1942 |
end) |
144 |
|
MrJackdaw-1942 |
return VirtPack |
145 |
131 |
MrJackdaw-1942 |
end |
146 |
|
MrJackdaw-1942 |
|
147 |
|
MrJackdaw-1942 |
--Callback handler due to Pengoros... Many thanks! |
148 |
|
MrJackdaw-1942 |
function AddCallback(object, event, callback) |
149 |
|
MrJackdaw-1942 |
if (object[event] == nil) then |
150 |
|
MrJackdaw-1942 |
object[event] = callback; |
151 |
|
MrJackdaw-1942 |
else |
152 |
|
MrJackdaw-1942 |
if (type(object[event]) == "table") then |
153 |
|
MrJackdaw-1942 |
table.insert(object[event], callback); |
154 |
|
MrJackdaw-1942 |
else |
155 |
|
MrJackdaw-1942 |
object[event] = {object[event], callback}; |
156 |
|
MrJackdaw-1942 |
end |
157 |
|
MrJackdaw-1942 |
end |
158 |
|
MrJackdaw-1942 |
return callback; |
159 |
|
MrJackdaw-1942 |
end |
160 |
|
MrJackdaw-1942 |
|
161 |
|
MrJackdaw-1942 |
-- safely remove a callback without clobbering any extras |
162 |
|
MrJackdaw-1942 |
function RemoveCallback(object, event, callback) |
163 |
|
MrJackdaw-1942 |
if (object[event] == callback) then |
164 |
|
MrJackdaw-1942 |
object[event] = nil; |
165 |
|
MrJackdaw-1942 |
else |
166 |
|
MrJackdaw-1942 |
if (type(object[event]) == "table") then |
167 |
|
MrJackdaw-1942 |
local size = table.getn(object[event]); |
168 |
|
MrJackdaw-1942 |
for i = 1, size do |
169 |
|
MrJackdaw-1942 |
if (object[event][i] == callback) then |
170 |
|
MrJackdaw-1942 |
table.remove(object[event], i); |
171 |
|
MrJackdaw-1942 |
break; |
172 |
|
MrJackdaw-1942 |
end |
173 |
|
MrJackdaw-1942 |
end |
174 |
|
MrJackdaw-1942 |
end |
175 |
|
MrJackdaw-1942 |
end |
176 |
138 |
MrJackdaw-1942 |
end |
177 |
|
MrJackdaw-1942 |
|
178 |
|
MrJackdaw-1942 |
--Ordered pair functions from http://lua-users.org/wiki/SortedIteration |
179 |
|
MrJackdaw-1942 |
function __genOrderedIndex( t ) |
180 |
|
MrJackdaw-1942 |
local orderedIndex = {} |
181 |
|
MrJackdaw-1942 |
for key in pairs(t) do |
182 |
|
MrJackdaw-1942 |
table.insert( orderedIndex, key ) |
183 |
|
MrJackdaw-1942 |
end |
184 |
|
MrJackdaw-1942 |
table.sort( orderedIndex ) |
185 |
|
MrJackdaw-1942 |
return orderedIndex |
186 |
|
MrJackdaw-1942 |
end |
187 |
|
MrJackdaw-1942 |
|
188 |
|
MrJackdaw-1942 |
function orderedNext(t, state) |
189 |
|
MrJackdaw-1942 |
-- Equivalent of the next function, but returns the keys in the alphabetic |
190 |
|
MrJackdaw-1942 |
-- order. We use a temporary ordered key table that is stored in the |
191 |
|
MrJackdaw-1942 |
-- table being iterated. |
192 |
|
MrJackdaw-1942 |
|
193 |
|
MrJackdaw-1942 |
--print("orderedNext: state = "..tostring(state) ) |
194 |
|
MrJackdaw-1942 |
if state == nil then |
195 |
|
MrJackdaw-1942 |
-- the first time, generate the index |
196 |
|
MrJackdaw-1942 |
t.__orderedIndex = __genOrderedIndex( t ) |
197 |
|
MrJackdaw-1942 |
key = t.__orderedIndex[1] |
198 |
|
MrJackdaw-1942 |
return key, t[key] |
199 |
|
MrJackdaw-1942 |
end |
200 |
|
MrJackdaw-1942 |
-- fetch the next value |
201 |
|
MrJackdaw-1942 |
key = nil |
202 |
|
MrJackdaw-1942 |
for i = 1,table.getn(t.__orderedIndex) do |
203 |
|
MrJackdaw-1942 |
if t.__orderedIndex[i] == state then |
204 |
|
MrJackdaw-1942 |
key = t.__orderedIndex[i+1] |
205 |
|
MrJackdaw-1942 |
end |
206 |
|
MrJackdaw-1942 |
end |
207 |
|
MrJackdaw-1942 |
|
208 |
|
MrJackdaw-1942 |
if key then |
209 |
|
MrJackdaw-1942 |
return key, t[key] |
210 |
|
MrJackdaw-1942 |
end |
211 |
|
MrJackdaw-1942 |
|
212 |
|
MrJackdaw-1942 |
-- no more value to return, cleanup |
213 |
|
MrJackdaw-1942 |
t.__orderedIndex = nil |
214 |
|
MrJackdaw-1942 |
return |
215 |
|
MrJackdaw-1942 |
end |
216 |
|
MrJackdaw-1942 |
|
217 |
|
MrJackdaw-1942 |
function orderedPairs(t) |
218 |
|
MrJackdaw-1942 |
-- Equivalent of the pairs() function on tables. Allows to iterate |
219 |
|
MrJackdaw-1942 |
-- in order |
220 |
|
MrJackdaw-1942 |
return orderedNext, t, nil |
221 |
112 |
MrJackdaw-1942 |
end |