-
Notifications
You must be signed in to change notification settings - Fork 10
/
MountList.lua
303 lines (251 loc) · 8.07 KB
/
MountList.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
--[[----------------------------------------------------------------------------
LiteMount/MountList.lua
List of mounts with some kinds of extra stuff, mostly shuffle/random.
Copyright 2011 Mike Battersby
----------------------------------------------------------------------------]]--
local _, LM = ...
--[[----------------------------------------------------------------------------
A primer reminder for me on LUA metatables and doing OO stuff in
them. If you rewrite this from scratch don't make it OO, OK.
See also: http://www.lua.org/pil/13.html
You can set a "metatable" on a table with
setmetatable(theTable, theMetaTable)
Special records in the metatable are used for table access in
the case that the table key doesn't exist:
__index = function (table, key) return value end
__newindex = function (table, key, value) store_value_somehow end
Also arithmetic and comparison operators: __add __mul __eq __lt __le
For a full list: http://lua-users.org/wiki/MetatableEvents
The generic case for metatable "inheritence" is
baseTable = { whatever }
childTable = { whateverelse }
metaTable = { __index = function (t,k) return baseTable[k] end }
setmetatable(childTable, metaTable)
This is so common that LUA allows a shortcut where you can set __index
to be a table instead of a function, and it will do the lookups in
that table.
baseTable = { whatever }
childTable = { whateverelse }
metaTable = { __index = baseTable }
setmetatable(childTable, metaTable)
Then as a further shortcut, instead of requiring a separate metatable
you can just use the base table itself as the metatable by setting its
__index record.
baseTable = { whatever }
baseTable.__index = baseTable
setmetatable(childTable, baseTable)
This is typical class-style OO.
----------------------------------------------------------------------------]]--
LM.MountList = { }
LM.MountList.__index = LM.MountList
function LM.MountList:New(ml)
return setmetatable(ml or {}, LM.MountList)
end
function LM.MountList:Copy()
local out = { }
for i,v in ipairs(self) do
out[i] = v
end
return self:New(out)
end
function LM.MountList:Clear()
table.wipe(self)
return self
end
function LM.MountList:Extend(other)
local exists = { }
for _,m in ipairs(self) do
exists[m] = true
end
for _,m in ipairs(other) do
if not exists[m] then
table.insert(self, m)
end
end
return self
end
function LM.MountList:Reduce(other)
local remove = { }
for _,m in ipairs(other) do
remove[m] = true
end
local j, n = 1, #self
for i = 1, n do
if remove[self[i]] then
self[i] = nil
else
if i ~= j then
self[j] = self[i]
self[i] = nil
end
j = j + 1
end
end
return self
end
function LM.MountList:Search(matchfunc, ...)
local result = self:New()
for _,m in ipairs(self) do
if matchfunc(m, ...) then
tinsert(result, m)
end
end
return result
end
-- Note that Find doesn't make another table
function LM.MountList:Find(matchfunc, ...)
for _,m in ipairs(self) do
if matchfunc(m, ...) then
return m
end
end
end
function LM.MountList:Shuffle()
-- Fisher-Yates algorithm.
-- Shuffle, http://forums.wowace.com/showthread.php?t=16628
for i = #self, 2, -1 do
local r = math.random(i)
self[i], self[r] = self[r], self[i]
end
end
function LM.MountList:SimpleRandom(r)
if #self > 0 then
if r then
r = math.ceil(r * #self)
else
r = math.random(#self)
end
return self[r]
end
end
-- This is not a basic weight by priority. The ratios of how often you get a mount of
-- each priority remain the same regardless of how many you have.
function LM.MountList:PriorityWeights()
local priorityCounts = { }
for _,m in ipairs(self) do
local p = m:GetPriority()
priorityCounts[p] = ( priorityCounts[p] or 0 ) + 1
end
local weights = { total=0 }
for i, m in ipairs(self) do
local p, w = m:GetPriority()
-- Handle the "always" priority by setting all the others to weight 0
if priorityCounts[LM.Options.ALWAYS_PRIORITY] and p ~= LM.Options.ALWAYS_PRIORITY then
weights[i] = 0
else
weights[i] = w / ( priorityCounts[p] + 1 )
end
weights.total = weights.total + weights[i]
end
return weights
end
function LM.MountList:RarityWeights()
local weights = { total=0 }
for i, m in ipairs(self) do
if m:GetPriority() == LM.Options.DISABLED_PRIORITY then
weights[i] = 0
else
local rarity = m:GetRarity() or 50
-- The weight is the mount's inverted rarity (rarer mounts are more likely)
-- Math fudge to guard against 0% rarity.
weights[i] = 101 / ( rarity + 1) - 1
end
weights.total = weights.total + weights[i]
end
return weights
end
function LM.MountList:WeightedRandom(weights, r)
if weights.total == 0 then
LM.Debug(' * WeightedRandom n=%d all weights 0', #self)
return
end
local cutoff = (r or math.random()) * weights.total
local t = 0
for i = 1, #self do
t = t + weights[i]
if t > cutoff then
LM.Debug(' * WeightedRandom n=%d, t=%0.3f, c=%0.3f, w=%0.3f, p=%0.3f',
#self, weights.total,
cutoff,
weights[i],
weights[i] / weights.total)
return self[i]
end
end
end
function LM.MountList:Random(r, style)
if #self == 0 then return end
if style == 'Priority' then
local weights = self:PriorityWeights()
return self:WeightedRandom(weights, r)
elseif style == 'Rarity' then
local weights = self:RarityWeights()
return self:WeightedRandom(weights, r)
else
return self:SimpleRandom(r)
end
end
local function filterMatch(m, ...)
return m:MatchesFilters(...)
end
function LM.MountList:FilterSearch(...)
return self:Search(filterMatch, ...)
end
-- Limits can be intersect (no prefix), subtract (-) or union (+). This really
-- only works when called on a list of the full set of mounts because it's
-- assuming self is everything. So bundle up all the limit expressions into a
-- list and call this once.
local function expressionMatch(m, e)
return m:MatchesExpression(e)
end
function LM.MountList:ExpressionSearch(e)
return self:Search(expressionMatch, e)
end
function LM.MountList:Limit(limits)
local mounts = self:Copy()
for _, arg in ipairs(limits) do
local e = arg:ParseExpression()
if e == nil then
-- SYNTAX ERROR PRINT SOMETHING?
return nil
elseif e.op == '+' then
mounts = mounts:Extend(self:ExpressionSearch(e[1]))
elseif e.op == '-' then
mounts = mounts:Reduce(self:ExpressionSearch(e[1]))
elseif e.op == '=' then
mounts = self:ExpressionSearch(e[1])
else
mounts = mounts:ExpressionSearch(e)
end
end
return mounts
end
local SortFunctions = {
-- Show all the collected mounts before the uncollected mounts, then by name
['default'] =
function (a, b)
if a:IsCollected() and not b:IsCollected() then return true end
if not a:IsCollected() and b:IsCollected() then return false end
return a.name < b.name
end,
['name'] =
function (a, b)
return a.name < b.name
end,
['rarity'] =
function (a, b)
return ( a:GetRarity() or 101 ) < ( b:GetRarity() or 101 )
end,
['summons'] =
function (a, b)
return a:GetSummonCount() > b:GetSummonCount()
end,
}
function LM.MountList:Sort(key)
table.sort(self, SortFunctions[key] or SortFunctions.default)
end
function LM.MountList:Dump()
for _,m in ipairs(self) do
m:Dump()
end
end