forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrainConditionsMonitor.lua
More file actions
362 lines (320 loc) · 12.8 KB
/
BrainConditionsMonitor.lua
File metadata and controls
362 lines (320 loc) · 12.8 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#***************************************************************************
#*
#** File : /lua/sim/BrainConditionsMonitor.lua
#**
#** Summary : Monitors conditions for a brain and stores them in a keyed
#** table
#**
#** Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
#****************************************************************************
BrainConditionsMonitor = Class {
PreCreate = function(self)
if self.PreCreateFinished then
return true
end
self.Trash = TrashBag()
self.ThreadWaitDuration = 7
self.Brain = false
self.Active = false
self.ResultTable = {}
self.ConditionData = {
TableConditions = {},
FunctionConditions = {},
}
self.ConditionsTable = {}
self.PreCreateFinished = true
end,
# Create the thing
Create = function(self, brain)
if not self.PreCreateFinished then
self:PreCreate()
end
self.Brain = brain
end,
Destroy = function(self)
KillThread(self.ConditionMonitor)
self.Active = false
self.Trash:Destroy()
end,
# Gets result for the keyed condition
CheckKeyedCondition = function(self, conditionKey, reportFailure)
if self.ResultTable[conditionKey] != nil then
return self.ResultTable[conditionKey]:GetStatus(reportFailure)
end
WARN('*WARNING: No Condition found with condition key: ' .. conditionKey)
return false
end,
# Checks the condition and returns the result
CheckConditionTable = function(self, cFilename, cFunctionName, cData)
if not cData or not type(cData) == 'table' then
WARN('*WARNING: Invalid argumetns for build condition: ' .. cFilename .. '[' .. cFunctionName .. ']')
return false
end
return import(cFilename)[cFunctionName](self.Brain, unpack(cData))
end,
# Runs the function and retuns the result
CheckConditionFunction = function(self, func, params)
return func(unpack(params))
end,
# Find the key for a condition or adds it to the table and checks the condition
GetConditionKey = function(self, cFilename, cFunctionName, cData)
if not cFunctionName then
error('*BUILD CONDITION MONITOR: Invalid BuilderCondition - Missing function name')
elseif not cData or type(cData) != 'table' then
error('*BUILD CONDITION MONITOR: Invalid BuilderCondition - Missing data table')
end
# Key the TableConditions by filename
if not self.ConditionData.TableConditions[cFilename] then
self.ConditionData.TableConditions[cFilename] = {}
end
# Key the filenames by function name
if not self.ConditionData.TableConditions[cFilename][cFunctionName] then
self.ConditionData.TableConditions[cFilename][cFunctionName] = {}
end
# Check if the cData matches up
for num,data in self.ConditionData.TableConditions[cFilename][cFunctionName] do
# Check if the data is the same length
if table.getn(data.ConditionParameters) == table.getn(cData) then
local match = true
# Check each piece of data to make sure it matches
for k,v in data.ConditionParameters do
if v != cData[k] then
match = false
break
end
end
# Match found, return the key
if match then
return data.Key
end
end
end
# No match found, so add the data to the table and return the key (same number as num items)
local newCondition
if cFilename == '/lua/editor/InstantBuildConditions.lua'
or cFilename == '/lua/editor/UnitCountBuildConditions.lua' or cFilename == '/lua/editor/EconomyBuildConditions.lua'
or cFilename == '/lua/editor/SorianInstantBuildConditions.lua' then
newCondition = InstantImportCondition()
else
newCondition = ImportCondition()
end
newCondition:Create(self.Brain, table.getn(self.ResultTable) + 1, cFilename, cFunctionName, cData)
table.insert(self.ResultTable, newCondition)
# Add in a hashed table for quicker key lookup, may not be necessary
local newTable = {
ConditionParameters = cData,
Key = newCondition:GetKey(),
}
table.insert(self.ConditionData.TableConditions[cFilename][cFunctionName], newTable)
return newTable.Key
end,
# Find the key for a condition that is a function
GetConditionKeyFunction = function(self, func, parameters)
# See if there is a matching function
for k,v in self.ConditionData.FunctionConditions do
if v.Function == func then
local found = true
for num,data in v.ConditionParameters do
if data != parameters[num] then
found = false
break
end
end
if found then
return v.Key
end
end
end
# No match, insert data into the function conditions table
local newCondition = FunctionCondition()
newCondition:Create(self.Brain, table.getn(self.ResultTable) + 1, func, parameters)
table.insert(self.ResultTable, newCondition)
local newTable = {
Function = func,
Key = newCondition:GetKey(),
ConditionParameters = parameters,
}
table.insert(self.ConditionData.FunctionConditions, newTable)
return newTable.Key
end,
# Thread that will monitor conditions the brain asks for over time
ConditionMonitorThread = function(self)
while true do
local checks = 0
local numResults = 0
local numChecks = table.getn(self.ResultTable)
local numPerTick = math.ceil(numChecks / (self.ThreadWaitDuration * 10))
for k,v in self.ResultTable do
if not v:LocationExists() then
#Don't remove the condition, just skip over.
#If the base gets rebuilt the AI will use the old keyed conditions.
continue
end
numResults = numResults + 1
v:CheckCondition()
# Load balance per tick here
checks = checks + 1
if checks >= numPerTick then
WaitTicks(1)
checks = 0
end
end
#LOG('*AI DEBUG: '.. self.Brain.Nickname ..' ConditionMonitorThread checked: '..numResults)
WaitTicks(1)
end
end,
# Adds a condition to the table and returns the key
AddCondition = function(self, cFilename, cFunctionName, cData)
if not self.Active then
self.Active = true
self.ConditionMonitor = self:ForkThread(self.ConditionMonitorThread)
end
if type(cFilename) == 'function' then
return self:GetConditionKeyFunction(cFilename, cFunctionName)
end
return self:GetConditionKey(cFilename, cFunctionName, cData)
end,
# forking and storing a thread on the monitor
ForkThread = function(self, fn, ...)
if fn then
local thread = ForkThread(fn, self, unpack(arg))
self.Trash:Add(thread)
return thread
else
return nil
end
end,
}
function CreateConditionsMonitor(brain)
local cMonitor = BrainConditionsMonitor()
cMonitor:Create(brain)
return cMonitor
end
Condition = Class {
# Create the thing
Create = function(self,brain,key)
self.Status = false
self.Brain = brain
self.Key = key
end,
CheckCondition = function(self)
self.Status = false
return self.Status
end,
GetStatus = function(self, reportFailure)
return self.Status
end,
GetKey = function(self)
return self.Key
end
}
ImportCondition = Class(Condition) {
Create = function(self,brain,key,filename,funcName,funcData)
Condition.Create(self,brain,key)
self.Filename = filename
self.FunctionName = funcName
self.FunctionData = funcData
self.CheckTime = false
end,
CheckCondition = function(self)
if self.CheckTime != GetGameTimeSeconds() then
self.Status = import(self.Filename)[self.FunctionName](self.Brain, unpack(self.FunctionData))
self.CheckTime = GetGameTimeSeconds()
end
return self.Status
end,
GetStatus = function(self, reportFailure)
if reportFailure and not self.Status then
LOG('*AI DEBUG: Build Condition failed - ' .. self.FunctionName .. ' - Data: ' .. repr(self.FunctionData))
end
return self.Status
end,
LocationExists = function(self)
local found = false
for k,v in self.FunctionData do
if type(v) == 'string' and not (v == 'Naval Area' or v == 'Expansion Area' or v == 'Large Expansion Area') then
if string.find(v, 'ARMY_') or string.find(v, 'Large Expansion') or string.find(v, 'Expansion Area') or string.find(v, 'EXPANSION_AREA') or string.find(v, 'Naval Area') or string.find(v, 'MAIN') then
found = true
if self.Brain.BuilderManagers[v] then
return true
end
end
end
end
if not found then return true end
self.Status = false
return false
end,
}
InstantImportCondition = Class(Condition) {
Create = function(self,brain,key,filename,funcName,funcData)
Condition.Create(self,brain,key)
self.Filename = filename
self.FunctionName = funcName
self.FunctionData = funcData
self.CheckTime = false
end,
# This class doesn't change when CheckCondition is called; Only changed when requested
CheckCondition = function(self)
#if self.CheckTime != GetGameTimeSeconds() then
#self.Status = import(self.Filename)[self.FunctionName](self.Brain, unpack(self.FunctionData))
#self.CheckTime = GetGameTimeSeconds()
#end
return self.Status
end,
# This class always performs the check when getting status (basically for stat checks)
GetStatus = function(self, reportFailure)
if self.CheckTime != GetGameTimeSeconds() then
self.Status = import(self.Filename)[self.FunctionName](self.Brain, unpack(self.FunctionData))
self.CheckTime = GetGameTimeSeconds()
#LOG('*AI LOG: Instant Check')
end
if reportFailure and not self.Status then
LOG('*AI DEBUG: Build Condition failed - ' .. self.FunctionName .. ' - Data: ' .. repr(self.FunctionData))
end
return self.Status
end,
LocationExists = function(self)
local found = false
for k,v in self.FunctionData do
if type(v) == 'string' and not (v == 'Naval Area' or v == 'Expansion Area' or v == 'Large Expansion Area') then
if string.find(v, 'ARMY_') or string.find(v, 'Large Expansion') or string.find(v, 'Expansion Area') or string.find(v, 'EXPANSION_AREA') or string.find(v, 'Naval Area') or string.find(v, 'MAIN') then
found = true
if self.Brain.BuilderManagers[v] then
return true
end
end
end
end
if not found then return true end
self.Status = false
return false
end,
}
FunctionCondition = Class(Condition) {
Create = function(self,brain,key,funcHandle,funcParams)
Condition.Create(self,brain,key)
self.FunctionHandle = funcHandle
self.FunctionParameters = funcParams or {}
end,
CheckCondition = function(self)
self.Status = self.FunctionHandle(self.Brain, unpack(self.FunctionParameters))
return self.Status
end,
LocationExists = function(self)
local found = false
for k,v in self.FunctionParameters do
if type(v) == 'string' and not (v == 'Naval Area' or v == 'Expansion Area' or v == 'Large Expansion Area') then
if string.find(v, 'ARMY_') or string.find(v, 'Large Expansion') or string.find(v, 'Expansion Area') or string.find(v, 'EXPANSION_AREA') or string.find(v, 'Naval Area') or string.find(v, 'MAIN') then
found = true
if self.Brain.BuilderManagers[v] then
return true
end
end
end
end
if not found then return true end
self.Status = false
return false
end,
}