|
1 -- Library for keeping track of gears in the game |
|
2 -- and running functions on them |
|
3 -- also keeps track of clans and teams |
|
4 |
|
5 local trackingTeams = false |
|
6 local resurrecting = false |
|
7 local gears = {} |
|
8 local teams = {} |
|
9 local clans = {} |
|
10 local resurrectedHogs = {} |
|
11 local gearValues = {} |
|
12 local teamValues = {} |
|
13 local clanValues = {} |
|
14 |
|
15 -- Registers when a gear is added |
|
16 function trackGear(gear) |
|
17 table.insert(gears, gear) |
|
18 if trackingTeams and GetGearType(gear) == gtResurrector then |
|
19 resurrecting = true |
|
20 elseif resurrecting and GetGearType(gear) == gtHedgehog then |
|
21 table.insert(resurrectedHogs, gear) |
|
22 end |
|
23 end |
|
24 |
|
25 -- Registers when a gear is deleted |
|
26 function trackDeletion(gear) |
|
27 gearValues[gear] = nil |
|
28 for k, g in ipairs(gears) do |
|
29 if g == gear then |
|
30 table.remove(gears, k) |
|
31 break |
|
32 end |
|
33 end |
|
34 if trackingTeams and GetGearType(gear) == gtHedgehog then |
|
35 for team, hogs in pairs(teams) do |
|
36 if team == GetHogTeamName(gear) then |
|
37 if table.maxn(hogs) == 1 then |
|
38 hogs = nil |
|
39 else |
|
40 for k, hog in ipairs(hogs) do |
|
41 if hog == gear then |
|
42 table.remove(hogs, k) |
|
43 break |
|
44 end |
|
45 end |
|
46 end |
|
47 break |
|
48 end |
|
49 end |
|
50 elseif resurrecting and GetGearType(gear) == gtResurrector then |
|
51 for k, gear in ipairs(resurrectedHogs) do |
|
52 team = GetHogTeamName(gear) |
|
53 if teams[team] == nil then |
|
54 teams[team] = {} |
|
55 end |
|
56 table.insert(teams[team], gear) |
|
57 end |
|
58 resurrecting = false |
|
59 resurrectedHogs = {} |
|
60 end |
|
61 end |
|
62 |
|
63 -- Not needed? |
|
64 -- Registers when a gear is resurrected |
|
65 --function trackResurrection(gear) |
|
66 -- if trackingTeams then |
|
67 -- if GetGearType(gear) == gtHedgehog then |
|
68 -- found = false |
|
69 -- for team, hogs in pairs(teams) do |
|
70 -- if team == GetHogTeamName(gear) then |
|
71 -- table.insert(hogs, gear) |
|
72 -- found = true |
|
73 -- end |
|
74 -- end |
|
75 -- if not found then |
|
76 -- teams[GetHogTeamName(gear)] = { gear } |
|
77 -- end |
|
78 -- end |
|
79 -- end |
|
80 --end |
|
81 |
|
82 -- Start to keep track of teams |
|
83 function trackTeams() |
|
84 if not trackingTeams then |
|
85 trackingTeams = true |
|
86 for k, gear in ipairs(gears) do |
|
87 if GetGearType(gear) == gtHedgehog then |
|
88 team = GetHogTeamName(gear) |
|
89 if teams[team] == nil then |
|
90 teams[team] = { gear } |
|
91 clans[team] = GetHogClan(gear) |
|
92 else |
|
93 table.insert(teams[team], gear) |
|
94 end |
|
95 end |
|
96 end |
|
97 end |
|
98 end |
|
99 |
|
100 -- Get a value for a specific gear |
|
101 function getGearValue(gear, key) |
|
102 if gearValues[gear] ~= nil then |
|
103 return gearValues[gear][key] |
|
104 end |
|
105 return nil |
|
106 end |
|
107 |
|
108 -- Set a value for a specific gear |
|
109 function setGearValue(gear, key, value) |
|
110 found = false |
|
111 for id, values in pairs(gearValues) do |
|
112 if id == gear then |
|
113 values[key] = value |
|
114 found = true |
|
115 end |
|
116 end |
|
117 if not found then |
|
118 gearValues[gear] = { [key] = value } |
|
119 end |
|
120 end |
|
121 |
|
122 -- Increase a value for a specific gear |
|
123 function increaseGearValue(gear, key) |
|
124 for id, values in pairs(gearValues) do |
|
125 if id == gear then |
|
126 values[key] = values[key] + 1 |
|
127 end |
|
128 end |
|
129 end |
|
130 |
|
131 -- Decrease a value for a specific gear |
|
132 function decreaseGearValue(gear, key) |
|
133 for id, values in pairs(gearValues) do |
|
134 if id == gear then |
|
135 values[key] = values[key] - 1 |
|
136 end |
|
137 end |
|
138 end |
|
139 |
|
140 -- Get a value for a specific team |
|
141 function getTeamValue(team, key) |
|
142 if teamValues[team] ~= nil then |
|
143 return teamValues[team][key] |
|
144 end |
|
145 return nil |
|
146 end |
|
147 |
|
148 -- Set a value for a specific team |
|
149 function setTeamValue(team, key, value) |
|
150 found = false |
|
151 for name, values in pairs(teamValues) do |
|
152 if name == team then |
|
153 values[key] = value |
|
154 found = true |
|
155 end |
|
156 end |
|
157 if not found then |
|
158 teamValues[team] = { [key] = value } |
|
159 end |
|
160 end |
|
161 |
|
162 -- Increase a value for a specific team |
|
163 function increaseTeamValue(team, key) |
|
164 for name, values in pairs(teamValues) do |
|
165 if name == team then |
|
166 values[key] = values[key] + 1 |
|
167 end |
|
168 end |
|
169 end |
|
170 |
|
171 -- Decrease a value for a specific team |
|
172 function decreaseTeamValue(team, key) |
|
173 for name, values in pairs(teamValues) do |
|
174 if name == team then |
|
175 values[key] = values[key] - 1 |
|
176 end |
|
177 end |
|
178 end |
|
179 |
|
180 -- Get a value for a specific clan |
|
181 function getClanValue(clan, key) |
|
182 if clanValues[clan] ~= nil then |
|
183 return clanValues[clan][key] |
|
184 end |
|
185 return nil |
|
186 end |
|
187 |
|
188 -- Set a value for a specific clan |
|
189 function setClanValue(clan, key, value) |
|
190 found = false |
|
191 for num, values in ipairs(clanValues) do |
|
192 if num == clan then |
|
193 values[key] = value |
|
194 found = true |
|
195 end |
|
196 end |
|
197 if not found then |
|
198 clanValues[clan] = { [key] = value } |
|
199 end |
|
200 end |
|
201 |
|
202 -- Increase a value for a specific clan |
|
203 function increaseClanValue(clan, key) |
|
204 for num, values in ipairs(clanValues) do |
|
205 if num == clan then |
|
206 values[key] = values[key] + 1 |
|
207 end |
|
208 end |
|
209 end |
|
210 |
|
211 -- Decrease a value for a specific clan |
|
212 function decreaseClanValue(clan, key) |
|
213 for num, values in ipairs(clanValues) do |
|
214 if num == clan then |
|
215 values[key] = values[key] - 1 |
|
216 end |
|
217 end |
|
218 end |
|
219 |
|
220 -- Run a function on all tracked gears |
|
221 function runOnGears(func) |
|
222 for k, gear in ipairs(gears) do |
|
223 func(gear) |
|
224 end |
|
225 end |
|
226 |
|
227 -- Run a function on all tracked hogs |
|
228 function runOnHogs(func) |
|
229 for k, hogs in pairs(teams) do |
|
230 for m, hog in ipairs(hogs) do |
|
231 func(hog) |
|
232 end |
|
233 end |
|
234 end |
|
235 |
|
236 -- Run a function on hogs in a team |
|
237 function runOnHogsInTeam(func, team) |
|
238 if teams[team] ~= nil then |
|
239 for k, hog in ipairs(teams[team]) do |
|
240 func(hog) |
|
241 end |
|
242 end |
|
243 end |
|
244 |
|
245 -- Run a function on hogs in other teams |
|
246 function runOnHogsInOtherTeams(func, team) |
|
247 for k, hogs in pairs(teams) do |
|
248 if k ~= team then |
|
249 for m, hog in ipairs(hogs) do |
|
250 func(hog) |
|
251 end |
|
252 end |
|
253 end |
|
254 end |
|
255 |
|
256 -- Run a function on hogs in a clan |
|
257 function runOnHogsInClan(func, clan) |
|
258 for i = 1, table.maxn(clans) do |
|
259 if clans[i] == clan then |
|
260 for k, hog in ipairs(teams[i]) do |
|
261 func(hog) |
|
262 end |
|
263 end |
|
264 end |
|
265 end |
|
266 |
|
267 -- Run a function on hogs in other clans |
|
268 function runOnHogsInOtherClans(func, clan) |
|
269 for i = 1, table.maxn(clans) do |
|
270 if clans[i] ~= clan then |
|
271 for k, hog in ipairs(teams[i]) do |
|
272 func(hog) |
|
273 end |
|
274 end |
|
275 end |
|
276 end |
|
277 |
|
278 function numGears() |
|
279 return table.maxn(gears) |
|
280 end |
|
281 |
|
282 function numTeams() |
|
283 num = 0 |
|
284 for team, hogs in pairs(teams) do |
|
285 num = num + 1 |
|
286 end |
|
287 return num |
|
288 end |