forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscore.lua
More file actions
287 lines (249 loc) · 11.7 KB
/
score.lua
File metadata and controls
287 lines (249 loc) · 11.7 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
historyInterval = 10
scoreInterval = 1
resourcesInterval = 0.1
alliesScore = true
local scoreData = {current={}, historical={}}
local scoreOption = ScenarioInfo.Options.Score or "no"
local ArmyScore = {}
-- Some of these values pre-existed and are used in other places, that's why their naming is not consistent
local categoriesToCollect = {
land=categories.LAND,
air=categories.AIR,
naval=categories.NAVAL,
cdr=categories.COMMAND,
sacu=categories.SUBCOMMANDER,
engineer=categories.ENGINEER,
tech1=categories.TECH1,
tech2=categories.TECH2,
tech3=categories.TECH3,
experimental=categories.EXPERIMENTAL,
structures=categories.STRUCTURE,
transportation=categories.TRANSPORTATION
}
function UpdateScoreData(newData)
scoreData.current = table.deepcopy(newData)
end
function CalculateBrainScore(brain)
local commanderKills = brain:GetArmyStat("Enemies_Commanders_Destroyed",0).Value
local massSpent = brain:GetArmyStat("Economy_TotalConsumed_Mass",0.0).Value
local massProduced = brain:GetArmyStat("Economy_TotalProduced_Mass",0.0).Value -- not currently being used
local energySpent = brain:GetArmyStat("Economy_TotalConsumed_Energy",0.0).Value
local energyProduced = brain:GetArmyStat("Economy_TotalProduced_Energy",0.0).Value -- not currently being used
local massValueDestroyed = brain:GetArmyStat("Enemies_MassValue_Destroyed",0.0).Value
local massValueLost = brain:GetArmyStat("Units_MassValue_Lost",0.0).Value
local energyValueDestroyed = brain:GetArmyStat("Enemies_EnergyValue_Destroyed",0.0).Value
local energyValueLost = brain:GetArmyStat("Units_EnergyValue_Lost",0.0).Value
-- helper variables to make equation more clear
local excessMassProduced = massProduced - massSpent -- not currently being used
local excessEnergyProduced = energyProduced - energySpent -- not currently being used
local energyValueCoefficient = 20
local commanderKillBonus = commanderKills + 1 -- not currently being used
-- score components calculated
local resourceProduction = ((massSpent) + (energySpent / energyValueCoefficient)) / 2
local battleResults = (((massValueDestroyed - massValueLost- (commanderKills * 2000)) + ((energyValueDestroyed - energyValueLost - (commanderKills * 5000000)) / energyValueCoefficient)) / 2)
if battleResults < 0 then
battleResults = 0
end
-- score calculated
local score = math.floor(resourceProduction + battleResults + (commanderKills * 5000))
return score
end
function ScoreHistoryThread()
while true do
WaitSeconds(historyInterval)
table.insert(scoreData.historical, table.deepcopy(scoreData.current))
end
end
function ScoreThread()
for index, brain in ArmyBrains do
ArmyScore[index] = {
faction = 0,
name = '',
type = '',
general = {
score = 0,
mass = 0,
lastReclaimedMass = 0,
lastReclaimedEnergy = 0,
energy = 0,
kills = {
count = 0,
mass = 0,
energy = 0
},
built = {
count = 0,
mass = 0,
energy = 0
},
lost = {
count = 0,
mass = 0,
energy = 0
},
currentunits = {
count = 0
},
currentcap = {
count = 0
}
},
blueprints = {
-- filled dynamically below
},
units = {
-- filled dynamically below
},
resources = {
massin = {
total = 0,
rate = 0
},
massout = {
total = 0,
rate = 0
},
energyin = {
total = 0,
rate = 0
},
energyout = {
total = 0,
rate = 0
},
massover = {
total = 0,
rate = 0
},
energyover = {
total = 0,
rate = 0
},
storage = {
storedMass = 0,
storedEnergy = 0,
maxMass = 0,
maxEnergy = 0,
},
MassReclaimRate = 0,
EnergyReclaimRate = 0
}
}
for categoryName, category in categoriesToCollect do
ArmyScore[index].units[categoryName] = {
kills = 0,
built = 0,
lost = 0
}
end
end
ForkThread(ScoreDisplayResourcesThread)
while true do
local updInterval = scoreInterval / table.getsize(ArmyBrains)
for index, brain in ArmyBrains do
ArmyScore[index].faction = brain:GetFactionIndex()
ArmyScore[index].name = brain.Nickname
ArmyScore[index].type = brain.BrainType
ArmyScore[index].general.score = CalculateBrainScore(brain)
ArmyScore[index].general.mass = brain:GetArmyStat("Economy_TotalProduced_Mass", 0.0).Value
ArmyScore[index].general.energy = brain:GetArmyStat("Economy_TotalProduced_Energy", 0.0).Value
ArmyScore[index].general.currentunits.count = brain:GetArmyStat("UnitCap_Current", 0.0).Value
ArmyScore[index].general.currentcap.count = brain:GetArmyStat("UnitCap_MaxCap", 0.0).Value
ArmyScore[index].general.kills.count = brain:GetArmyStat("Enemies_Killed", 0.0).Value
ArmyScore[index].general.kills.mass = brain:GetArmyStat("Enemies_MassValue_Destroyed", 0.0).Value
ArmyScore[index].general.kills.energy = brain:GetArmyStat("Enemies_EnergyValue_Destroyed", 0.0).Value
ArmyScore[index].general.built.count = brain:GetArmyStat("Units_History", 0.0).Value
ArmyScore[index].general.built.mass = brain:GetArmyStat("Units_MassValue_Built", 0.0).Value
ArmyScore[index].general.built.energy = brain:GetArmyStat("Units_EnergyValue_Built", 0.0).Value
ArmyScore[index].general.lost.count = brain:GetArmyStat("Units_Killed", 0.0).Value
ArmyScore[index].general.lost.mass = brain:GetArmyStat("Units_MassValue_Lost", 0.0).Value
ArmyScore[index].general.lost.energy = brain:GetArmyStat("Units_EnergyValue_Lost", 0.0).Value
ArmyScore[index].resources.massin.total = brain:GetArmyStat("Economy_TotalProduced_Mass", 0.0).Value
ArmyScore[index].resources.massout.total = brain:GetArmyStat("Economy_TotalConsumed_Mass", 0.0).Value
ArmyScore[index].resources.massout.rate = brain:GetArmyStat("Economy_Output_Mass", 0.0).Value
ArmyScore[index].resources.massover.total = brain:GetArmyStat("Economy_AccumExcess_Mass", 0.0).Value
ArmyScore[index].resources.energyin.total = brain:GetArmyStat("Economy_TotalProduced_Energy", 0.0).Value
ArmyScore[index].resources.energyout.total = brain:GetArmyStat("Economy_TotalConsumed_Energy", 0.0).Value
ArmyScore[index].resources.energyout.rate = brain:GetArmyStat("Economy_Output_Energy", 0.0).Value
ArmyScore[index].resources.energyover.total = brain:GetArmyStat("Economy_AccumExcess_Energy", 0.0).Value
ArmyScore[index].resources.storage.storedMass = brain:GetEconomyStored('MASS')
ArmyScore[index].resources.storage.storedEnergy = brain:GetEconomyStored('ENERGY')
ArmyScore[index].resources.storage.maxMass = brain:GetArmyStat("Economy_MaxStorage_Mass", 0.0).Value
ArmyScore[index].resources.storage.maxEnergy = brain:GetArmyStat("Economy_MaxStorage_Energy", 0.0).Value
for unitId, stats in brain.UnitStats do
if ArmyScore[index].blueprints[unitId] == nil then
ArmyScore[index].blueprints[unitId] = {}
end
for statName, value in stats do
ArmyScore[index].blueprints[unitId][statName] = value
end
end
for categoryName, category in categoriesToCollect do
ArmyScore[index].units[categoryName]['kills'] = brain:GetBlueprintStat("Enemies_Killed", category)
ArmyScore[index].units[categoryName]['built'] = brain:GetBlueprintStat("Units_History", category)
ArmyScore[index].units[categoryName]['lost'] = brain:GetBlueprintStat("Units_Killed", category)
end
WaitSeconds(updInterval)
end
UpdateScoreData(ArmyScore)
SyncScores()
end
end
function ScoreDisplayResourcesThread()
-- For certain stats, we need to do this every tick. We can't for all because it is quite heavy CPU
-- We don't need to sync every tick though, just make sure the number is right
while true do
for index, brain in ArmyBrains do
local reclaimedMass = brain:GetArmyStat("Economy_Reclaimed_Mass", 0.0).Value
local massReclaimRate = reclaimedMass - ArmyScore[index].general.lastReclaimedMass
ArmyScore[index].resources.MassReclaimRate = massReclaimRate
ArmyScore[index].resources.massin.rate = brain:GetArmyStat("Economy_Income_Mass", 0.0).Value - massReclaimRate
ArmyScore[index].general.lastReclaimedMass = reclaimedMass
ArmyScore[index].resources.massover.rate = ArmyScore[index].resources.massin.rate - ArmyScore[index].resources.massout.rate + ArmyScore[index].resources.MassReclaimRate
local reclaimedEnergy = brain:GetArmyStat("Economy_Reclaimed_Energy", 0.0).Value
local energyReclaimRate = reclaimedEnergy - ArmyScore[index].general.lastReclaimedEnergy
ArmyScore[index].resources.EnergyReclaimRate = energyReclaimRate
ArmyScore[index].resources.energyin.rate = brain:GetArmyStat("Economy_Income_Energy", 0.0).Value - energyReclaimRate
ArmyScore[index].general.lastReclaimedEnergy = reclaimedEnergy
ArmyScore[index].resources.energyover.rate = ArmyScore[index].resources.energyin.rate - ArmyScore[index].resources.energyout.rate + ArmyScore[index].resources.EnergyReclaimRate
end
WaitSeconds(resourcesInterval)
end
end
local observer = false
function SyncScores()
local myArmyIndex = GetFocusArmy()
observer = myArmyIndex == -1
local victory = import('/lua/victory.lua')
if observer or victory.gameOver or SessionIsReplay() then
Sync.FullScoreSync = true
Sync.ScoreAccum = scoreData
Sync.Score = scoreData.current
-- We don't want to report full scores to server unless game over
if victory.gameOver then
Sync.StatsToSend = Sync.Score
end
else
for index, brain in ArmyBrains do
if brain.Result and not brain.StatsSent then
Sync.StatsToSend = table.deepcopy(scoreData.current)
brain.StatsSent = true
end
if (myArmyIndex == index) or (alliesScore and IsAlly(myArmyIndex, index)) then
Sync.Score[index] = ArmyScore[index]
else
Sync.Score[index] = {}
Sync.Score[index].general = {}
end
if scoreOption ~= 'no' then
Sync.Score[index].general.score = ArmyScore[index].general.score
else
Sync.Score[index].general.score = -1
end
end
end
end
function init()
ForkThread(ScoreThread)
ForkThread(ScoreHistoryThread)
end