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 |
|
MrJackdaw-1942 |
--Not yet functional, but here ready! |
41 |
|
MrJackdaw-1942 |
Turbine.Gameplay.Item.GetCat=function(self) |
42 |
138 |
MrJackdaw-1942 |
local name=self:GetName() |
43 |
|
MrJackdaw-1942 |
if CustomItem[name]~=nil then return CustomItem[name].Cat end |
44 |
137 |
MrJackdaw-1942 |
return self:GetCategory() |
45 |
|
MrJackdaw-1942 |
end |
46 |
|
MrJackdaw-1942 |
|
47 |
112 |
MrJackdaw-1942 |
function weight(i) |
48 |
|
MrJackdaw-1942 |
--Calculate the weight |
49 |
|
MrJackdaw-1942 |
local Weight = 0 |
50 |
128 |
MrJackdaw-1942 |
local Category |
51 |
|
MrJackdaw-1942 |
local Quality |
52 |
|
MrJackdaw-1942 |
local Name |
53 |
112 |
MrJackdaw-1942 |
|
54 |
128 |
MrJackdaw-1942 |
local item = backpack:GetItem(i) |
55 |
|
MrJackdaw-1942 |
|
56 |
112 |
MrJackdaw-1942 |
if item == nil then |
57 |
|
MrJackdaw-1942 |
--Empty slots have no name, quality, category etc |
58 |
|
MrJackdaw-1942 |
Category=0 |
59 |
|
MrJackdaw-1942 |
Quality=0 |
60 |
136 |
MrJackdaw-1942 |
Name="" |
61 |
112 |
MrJackdaw-1942 |
else |
62 |
137 |
MrJackdaw-1942 |
Category=item:GetCat() |
63 |
112 |
MrJackdaw-1942 |
Quality=item:GetQuality() |
64 |
136 |
MrJackdaw-1942 |
Name=item:GetName() |
65 |
112 |
MrJackdaw-1942 |
if Quality==nil then Quality=0 end |
66 |
111 |
MrJackdaw-1942 |
end |
67 |
|
MrJackdaw-1942 |
|
68 |
129 |
MrJackdaw-1942 |
local CategoryText=ItemCategory[Category] |
69 |
112 |
MrJackdaw-1942 |
if CategoryText~=nil then |
70 |
|
MrJackdaw-1942 |
CatWeight=value[CategoryText] --First scan the CategoryText using the value table |
71 |
|
MrJackdaw-1942 |
else |
72 |
|
MrJackdaw-1942 |
--This item has not been given a category number by turbine - it is missing from the category text list. |
73 |
134 |
MrJackdaw-1942 |
|
74 |
|
MrJackdaw-1942 |
--** |
75 |
|
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. |
76 |
|
MrJackdaw-1942 |
if warned==nil then |
77 |
|
MrJackdaw-1942 |
warned={} |
78 |
|
MrJackdaw-1942 |
end |
79 |
|
MrJackdaw-1942 |
if warned.Category==nil then |
80 |
|
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!") |
81 |
|
MrJackdaw-1942 |
warned.Category=true |
82 |
|
MrJackdaw-1942 |
end |
83 |
|
MrJackdaw-1942 |
--** |
84 |
111 |
MrJackdaw-1942 |
end |
85 |
129 |
MrJackdaw-1942 |
|
86 |
|
MrJackdaw-1942 |
Weight=CatWeight*10+Quality |
87 |
132 |
MrJackdaw-1942 |
|
88 |
|
MrJackdaw-1942 |
if item == nil then Weight=99999 end |
89 |
111 |
MrJackdaw-1942 |
|
90 |
112 |
MrJackdaw-1942 |
return Weight |
91 |
|
MrJackdaw-1942 |
end |
92 |
111 |
MrJackdaw-1942 |
|
93 |
112 |
MrJackdaw-1942 |
function strip(str) |
94 |
|
MrJackdaw-1942 |
--Strip numbers first 7 characters to make the sort more logical |
95 |
|
MrJackdaw-1942 |
local left=string.sub(str, 1, 7) |
96 |
|
MrJackdaw-1942 |
local right=string.sub(str, 8) |
97 |
|
MrJackdaw-1942 |
left=string.gsub(left,"[^%a]","") |
98 |
|
MrJackdaw-1942 |
str= left .. right |
99 |
|
MrJackdaw-1942 |
--Strip all spaces now! |
100 |
|
MrJackdaw-1942 |
str=string.gsub(str,"[%s]","") |
101 |
|
MrJackdaw-1942 |
return str |
102 |
|
MrJackdaw-1942 |
end |
103 |
111 |
MrJackdaw-1942 |
|
104 |
112 |
MrJackdaw-1942 |
function analysepack() |
105 |
|
MrJackdaw-1942 |
local VirtPack={} |
106 |
|
MrJackdaw-1942 |
for i=1,size do |
107 |
|
MrJackdaw-1942 |
VirtPack[i]={} |
108 |
|
MrJackdaw-1942 |
VirtPack[i].id=i |
109 |
|
MrJackdaw-1942 |
item = backpack:GetItem(i) |
110 |
|
MrJackdaw-1942 |
if item~=nil then |
111 |
|
MrJackdaw-1942 |
--Data that may be useful... |
112 |
|
MrJackdaw-1942 |
VirtPack[i].Name=item:GetName() |
113 |
137 |
MrJackdaw-1942 |
VirtPack[i].Group=ItemGroup[ItemCategory[item:GetCat()]] |
114 |
112 |
MrJackdaw-1942 |
else |
115 |
|
MrJackdaw-1942 |
VirtPack[i].Name="zEmpty" |
116 |
|
MrJackdaw-1942 |
VirtPack[i].Group=0 |
117 |
|
MrJackdaw-1942 |
end |
118 |
|
MrJackdaw-1942 |
VirtPack[i].Weight=weight(i) |
119 |
134 |
MrJackdaw-1942 |
if VirtPack[i].Group==nil then VirtPack[i].Group=0 end |
120 |
111 |
MrJackdaw-1942 |
end |
121 |
112 |
MrJackdaw-1942 |
|
122 |
|
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" |
123 |
|
MrJackdaw-1942 |
table.sort(VirtPack, function (a,b) |
124 |
|
MrJackdaw-1942 |
if a.Weight == b.Weight then |
125 |
|
MrJackdaw-1942 |
--return a.Name < b.Name |
126 |
|
MrJackdaw-1942 |
if revorder then return strip(a.Name)> strip(b.Name) else return strip(a.Name)< strip(b.Name) end |
127 |
|
MrJackdaw-1942 |
else |
128 |
|
MrJackdaw-1942 |
if revorder then return (a.Weight > b.Weight) else return (a.Weight < b.Weight) end |
129 |
|
MrJackdaw-1942 |
end |
130 |
|
MrJackdaw-1942 |
end) |
131 |
|
MrJackdaw-1942 |
return VirtPack |
132 |
131 |
MrJackdaw-1942 |
end |
133 |
|
MrJackdaw-1942 |
|
134 |
|
MrJackdaw-1942 |
--Callback handler due to Pengoros... Many thanks! |
135 |
|
MrJackdaw-1942 |
function AddCallback(object, event, callback) |
136 |
|
MrJackdaw-1942 |
if (object[event] == nil) then |
137 |
|
MrJackdaw-1942 |
object[event] = callback; |
138 |
|
MrJackdaw-1942 |
else |
139 |
|
MrJackdaw-1942 |
if (type(object[event]) == "table") then |
140 |
|
MrJackdaw-1942 |
table.insert(object[event], callback); |
141 |
|
MrJackdaw-1942 |
else |
142 |
|
MrJackdaw-1942 |
object[event] = {object[event], callback}; |
143 |
|
MrJackdaw-1942 |
end |
144 |
|
MrJackdaw-1942 |
end |
145 |
|
MrJackdaw-1942 |
return callback; |
146 |
|
MrJackdaw-1942 |
end |
147 |
|
MrJackdaw-1942 |
|
148 |
|
MrJackdaw-1942 |
-- safely remove a callback without clobbering any extras |
149 |
|
MrJackdaw-1942 |
function RemoveCallback(object, event, callback) |
150 |
|
MrJackdaw-1942 |
if (object[event] == callback) then |
151 |
|
MrJackdaw-1942 |
object[event] = nil; |
152 |
|
MrJackdaw-1942 |
else |
153 |
|
MrJackdaw-1942 |
if (type(object[event]) == "table") then |
154 |
|
MrJackdaw-1942 |
local size = table.getn(object[event]); |
155 |
|
MrJackdaw-1942 |
for i = 1, size do |
156 |
|
MrJackdaw-1942 |
if (object[event][i] == callback) then |
157 |
|
MrJackdaw-1942 |
table.remove(object[event], i); |
158 |
|
MrJackdaw-1942 |
break; |
159 |
|
MrJackdaw-1942 |
end |
160 |
|
MrJackdaw-1942 |
end |
161 |
|
MrJackdaw-1942 |
end |
162 |
|
MrJackdaw-1942 |
end |
163 |
138 |
MrJackdaw-1942 |
end |
164 |
|
MrJackdaw-1942 |
|
165 |
|
MrJackdaw-1942 |
--Ordered pair functions from http://lua-users.org/wiki/SortedIteration |
166 |
|
MrJackdaw-1942 |
function __genOrderedIndex( t ) |
167 |
|
MrJackdaw-1942 |
local orderedIndex = {} |
168 |
|
MrJackdaw-1942 |
for key in pairs(t) do |
169 |
|
MrJackdaw-1942 |
table.insert( orderedIndex, key ) |
170 |
|
MrJackdaw-1942 |
end |
171 |
|
MrJackdaw-1942 |
table.sort( orderedIndex ) |
172 |
|
MrJackdaw-1942 |
return orderedIndex |
173 |
|
MrJackdaw-1942 |
end |
174 |
|
MrJackdaw-1942 |
|
175 |
|
MrJackdaw-1942 |
function orderedNext(t, state) |
176 |
|
MrJackdaw-1942 |
-- Equivalent of the next function, but returns the keys in the alphabetic |
177 |
|
MrJackdaw-1942 |
-- order. We use a temporary ordered key table that is stored in the |
178 |
|
MrJackdaw-1942 |
-- table being iterated. |
179 |
|
MrJackdaw-1942 |
|
180 |
|
MrJackdaw-1942 |
--print("orderedNext: state = "..tostring(state) ) |
181 |
|
MrJackdaw-1942 |
if state == nil then |
182 |
|
MrJackdaw-1942 |
-- the first time, generate the index |
183 |
|
MrJackdaw-1942 |
t.__orderedIndex = __genOrderedIndex( t ) |
184 |
|
MrJackdaw-1942 |
key = t.__orderedIndex[1] |
185 |
|
MrJackdaw-1942 |
return key, t[key] |
186 |
|
MrJackdaw-1942 |
end |
187 |
|
MrJackdaw-1942 |
-- fetch the next value |
188 |
|
MrJackdaw-1942 |
key = nil |
189 |
|
MrJackdaw-1942 |
for i = 1,table.getn(t.__orderedIndex) do |
190 |
|
MrJackdaw-1942 |
if t.__orderedIndex[i] == state then |
191 |
|
MrJackdaw-1942 |
key = t.__orderedIndex[i+1] |
192 |
|
MrJackdaw-1942 |
end |
193 |
|
MrJackdaw-1942 |
end |
194 |
|
MrJackdaw-1942 |
|
195 |
|
MrJackdaw-1942 |
if key then |
196 |
|
MrJackdaw-1942 |
return key, t[key] |
197 |
|
MrJackdaw-1942 |
end |
198 |
|
MrJackdaw-1942 |
|
199 |
|
MrJackdaw-1942 |
-- no more value to return, cleanup |
200 |
|
MrJackdaw-1942 |
t.__orderedIndex = nil |
201 |
|
MrJackdaw-1942 |
return |
202 |
|
MrJackdaw-1942 |
end |
203 |
|
MrJackdaw-1942 |
|
204 |
|
MrJackdaw-1942 |
function orderedPairs(t) |
205 |
|
MrJackdaw-1942 |
-- Equivalent of the pairs() function on tables. Allows to iterate |
206 |
|
MrJackdaw-1942 |
-- in order |
207 |
|
MrJackdaw-1942 |
return orderedNext, t, nil |
208 |
112 |
MrJackdaw-1942 |
end |