forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilderManager.lua
More file actions
316 lines (288 loc) · 11.5 KB
/
BuilderManager.lua
File metadata and controls
316 lines (288 loc) · 11.5 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
#***************************************************************************
#*
#** File : /lua/sim/BuilderManager.lua
#**
#** Summary : Manage builders
#**
#** Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
#****************************************************************************
local AIUtils = import('/lua/ai/aiutilities.lua')
local Builder = import('/lua/sim/Builder.lua')
local AIBuildUnits = import('/lua/ai/aibuildunits.lua')
BuilderManager = Class {
Create = function(self, brain)
self.Trash = TrashBag()
self.Brain = brain
self.BuilderData = {}
self.BuilderCheckInterval = 13
self.BuilderList = false
self.Active = false
self.NumBuilders = 0
self:SetEnabled(true)
self.NumGet = 0
end,
Destroy = function(self)
for _,bType in self.BuilderData do
for k,v in bType do
v = nil
end
end
self.Trash:Destroy()
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,
SetEnabled = function(self, enable)
if not self.BuilderThread and enable then
self.BuilderThread = self:ForkThread(self.ManagerThread)
self.Active = true
else
KillThread(self.BuilderThread)
self.BuilderThread = nil
self.Active = false
end
end,
SortBuilderList = function(self, bType)
# Make sure there is a type
if not self.BuilderData[bType] then
error('*BUILDMANAGER ERROR: Trying to sort platoons of invalid builder type - ' .. bType)
return false
end
local sortedList = {}
#Simple selection sort, this can be made faster later if we decide we need it.
for i = 1, table.getn(self.BuilderData[bType].Builders) do
local highest = 0
local key, value
for k, v in self.BuilderData[bType].Builders do
if v.Priority > highest then
highest = v.Priority
value = v
key = k
end
end
sortedList[i] = value
table.remove(self.BuilderData[bType].Builders, key)
end
self.BuilderData[bType].Builders = sortedList
self.BuilderData[bType].NeedSort = false
end,
AlterBuilder = function(self, builderName, changeTable)
for k,v in self.BuilderData do
for num,builder in v.Builders do
if builder.BuilderName == builderName then
for key,change in changeTable do
builder.key = change
if key == BuilderConditions then
ChangeState(builder, builder.SetupState)
end
end
end
break
end
end
end,
AddBuilder = function(self, builderData, locationType, builderType)
local newBuilder = Builder.CreateBuilder(self.Brain, builderData, locationType)
self:AddInstancedBuilder(newBuilder, builderType)
end,
AddInstancedBuilder = function(self,newBuilder, builderType)
builderType = builderType or newBuilder:GetBuilderType()
if not builderType then
-- Warn the programmer that something is wrong. We can continue, hopefully the builder is not too important for the AI ;)
-- But good for testing, and the case that a mod has bad builders.
-- Output: WARNING: [buildermanager.lua, line:xxx] *BUILDERMANAGER ERROR: No BuilderData for builder: T3 Air Scout
WARN('['..string.gsub(debug.getinfo(1).source, ".*\\(.*.lua)", "%1")..', line:'..debug.getinfo(1).currentline..'] *BUILDERMANAGER ERROR: Invalid builder type: ' .. repr(builderType) .. ' - in builder: ' .. newBuilder.BuilderName)
return
end
if newBuilder then
if not self.BuilderData[builderType] then
-- Warn the programmer that something is wrong here. Same here, we can continue.
-- Output: WARNING: [buildermanager.lua, line:xxx] *BUILDERMANAGER ERROR: No BuilderData for builder: T3 Air Scout
WARN('['..string.gsub(debug.getinfo(1).source, ".*\\(.*.lua)", "%1")..', line:'..debug.getinfo(1).currentline..'] *BUILDERMANAGER ERROR: No BuilderData for builder: ' .. newBuilder.BuilderName)
return
end
table.insert(self.BuilderData[builderType].Builders, newBuilder)
self.BuilderData[builderType].NeedSort = true
self.BuilderList = true
end
self.NumBuilders = self.NumBuilders + 1
if newBuilder.InstantCheck then
self:ManagerLoopBody(newBuilder)
end
end,
GetBuilderPriority = function(self, builderName)
for _,bType in self.BuilderData do
for _,builder in bType.Builders do
if builder.BuilderName == builderName then
return builder.Priority
end
end
end
return false
end,
GetActivePriority = function(self, builderName)
for _,bType in self.BuilderData do
for _,builder in bType.Builders do
if builder.BuilderName == builderName then
return builder:GetActivePriority()
end
end
end
return false
end,
SetBuilderPriority = function(self,builderName,priority,temporary,setbystrat)
for _,bType in self.BuilderData do
for _,builder in bType.Builders do
if builder.BuilderName == builderName then
builder:SetPriority(priority, temporary, setbystrat)
return
end
end
end
end,
ResetBuilderPriority = function(self,builderName)
for _,bType in self.BuilderData do
for _,builder in bType.Builders do
if builder.BuilderName == builderName then
builder:ResetPriority()
return
end
end
end
end,
GetLocationType = function(self)
return self.LocationType
end,
GetLocationCoords = function(self)
if not self.Location then
return false
end
local height = GetTerrainHeight(self.Location[1], self.Location[3])
if GetSurfaceHeight(self.Location[1], self.Location[3]) > height then
height = GetSurfaceHeight(self.Location[1], self.Location[3])
end
return { self.Location[1], height, self.Location[3] }
end,
GetLocationRadius = function(self)
return self.Radius
end,
AddBuilderType = function(self, type)
self.BuilderData[type] = { Builders = {}, NeedSort = false }
end,
SetCheckInterval = function(self, interval)
self.BuildCheckInterval = interval
end,
ClearBuilderLists = function(self)
for k,v in self.Builders do
v.Builders = {}
v.NeedSort = false
end
self.BuilderList = false
end,
HasBuilderList = function(self)
return self.BuilderList
end,
# Function that is run in GetHighestBuilder; allows us to test if the builder is valid
# within a certain type of builder mananger (ie: can factories build the builder)
BuilderParamCheck = function(self,builder,params)
return true
end,
GetBuilder = function(self, builderName)
for _,bType in self.BuilderData do
for _,builder in bType.Builders do
if builder.BuilderName == builderName then
return builder
end
end
end
return false
end,
-- We delay buildplatoons to give engineers the time to move and start building before we call this builder again.
IsPlattonBuildDelayed = function(self, DelayEqualBuildPlattons)
if DelayEqualBuildPlattons then
local CheckDelayTime = GetGameTimeSeconds()
local PlatoonName = DelayEqualBuildPlattons[1]
if not self.Brain.DelayEqualBuildPlattons[PlatoonName] or self.Brain.DelayEqualBuildPlattons[PlatoonName] < CheckDelayTime then
--LOG('Setting '..DelayEqualBuildPlattons[2]..' sec. delaytime for builder ['..PlatoonName..']')
self.Brain.DelayEqualBuildPlattons[PlatoonName] = CheckDelayTime + DelayEqualBuildPlattons[2]
return false
else
--LOG('Builder ['..PlatoonName..'] still delayed for '..(CheckDelayTime - self.Brain.DelayEqualBuildPlattons[PlatoonName])..' seconds.')
return true
end
end
end,
GetHighestBuilder = function(self,bType,params)
if not self.BuilderData[bType] then
error('*BUILDERMANAGER ERROR: Invalid builder type - ' .. bType)
end
if not self.Brain.BuilderManagers[self.LocationType] then
return false
end
self.NumGet = self.NumGet + 1
local found = false
local possibleBuilders = {}
for k,v in self.BuilderData[bType].Builders do
if v.Priority >= 1 and self:BuilderParamCheck(v,params) and (not found or v.Priority == found) and v:GetBuilderStatus() then
if not self:IsPlattonBuildDelayed(v.DelayEqualBuildPlattons) then
found = v.Priority
table.insert(possibleBuilders, k)
end
elseif found and v.Priority < found then
break
end
end
if found and found > 0 then
local whichBuilder = Random(1,table.getn(possibleBuilders))
return self.BuilderData[bType].Builders[ possibleBuilders[whichBuilder] ]
end
return false
end,
# Called every 13 seconds to perform any cleanup; Provides better inheritance
ManagerThreadCleanup = function(self)
for bType,bTypeData in self.BuilderData do
if bTypeData.NeedSort then
self:SortBuilderList(bType)
end
end
end,
ManagerLoopBody = function(self,builder,bType)
if builder:CalculatePriority(self) then
self.BuilderData[bType].NeedSort = true
end
#builder:CheckBuilderConditions(self.Brain)
end,
ManagerThread = function(self)
while self.Active do
self:ManagerThreadCleanup()
local numPerTick = math.ceil(self.NumBuilders / (self.BuilderCheckInterval * 10))
local numTicks = 0
local numTested = 0
for bType,bTypeData in self.BuilderData do
for bNum,bData in bTypeData.Builders do
numTested = numTested + 1
if numTested >= numPerTick then
WaitTicks(1)
if self.NumGet > 1 then
#LOG('*AI STAT: NumGet = ' .. self.NumGet)
end
self.NumGet = 0
numTicks = numTicks + 1
numTest = 0
end
self:ManagerLoopBody(bData,bType)
end
end
if numTicks <= (self.BuilderCheckInterval * 10) then
WaitTicks((self.BuilderCheckInterval * 10) - numTicks)
end
end
end,
}