-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmark.lua
More file actions
237 lines (183 loc) · 6.83 KB
/
Benchmark.lua
File metadata and controls
237 lines (183 loc) · 6.83 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
Benchmark = {}
local Benchmark_mt = Class(Benchmark)
function Benchmark.new(customMt)
local self = setmetatable({}, customMt or Benchmark_mt)
self.isRunning = false
self.currentCamPath = nil
self.hasFinished = false
if g_isDevelopmentVersion then
addConsoleCommand("gsBenchmark", "Starts a benchmark sequence with camera flight and working vehicles", "consoleCommandBenchmark", self)
end
return self
end
function Benchmark:delete()
removeConsoleCommand("gsBenchmark")
end
function Benchmark:load()
local mapXMLFilename = Utils.getFilename(g_currentMission.missionInfo.mapXMLFilename, g_currentMission.baseDirectory)
local mapXmlFile = loadXMLFile("MapXML", mapXMLFilename)
self.vehicles = {}
self.idToVehicle = {}
self.delayToAiVehicle = {}
self.fields = {}
self.camPaths = {}
local benchmarkXmlFilePath = getXMLString(mapXmlFile, "map.benchmark#filename")
if benchmarkXmlFilePath ~= nil then
benchmarkXmlFilePath = Utils.getFilename(benchmarkXmlFilePath, g_currentMission.baseDirectory)
local benchmarkXml = XMLFile.load("benchmarkXml", benchmarkXmlFilePath)
if benchmarkXml ~= nil then
log("Loading benchmark config")
local cameraFov = benchmarkXml:getFloat("benchmark.camera#fieldOfView") or 70
log("camera field of view", cameraFov)
self.benchmarkCam = createCamera("benchmarkCam", math.rad(cameraFov), 1, 10000)
link(getRootNode(), self.benchmarkCam)
self.benmarkVehiclesXmlFilepath = Utils.getFilename(benchmarkXml:getString("benchmark.vehicles#xmlFilename"), g_currentMission.baseDirectory)
benchmarkXml:iterate("benchmark.aiStarts.aiStart", function (index, key)
local vehicleId = benchmarkXml:getInt(key .. "#vehicleId")
local startDelay = benchmarkXml:getFloat(key .. "#startDelay") * 1000
if self.delayToAiVehicle[startDelay] == nil then
self.delayToAiVehicle[startDelay] = {}
end
table.insert(self.delayToAiVehicle[startDelay], vehicleId)
end)
benchmarkXml:iterate("benchmark.fields.field", function (index, key)
local fieldNumber = benchmarkXml:getInt(key .. "#number")
local fruitType = benchmarkXml:getString(key .. "#fruitType")
local growthState = benchmarkXml:getInt(key .. "#growthState")
table.insert(self.fields, {
fieldNumber,
fruitType,
growthState
})
end)
benchmarkXml:iterate("benchmark.cameraPaths.cameraPath", function (index, key)
local i3dFilename = Utils.getFilename(benchmarkXml:getString(key .. "#i3dFilename"), g_currentMission.baseDirectory)
local camPathSpeedScale = benchmarkXml:getString(key .. "#speedScale", 1)
local camPath = CameraPath.createFromI3D(i3dFilename, camPathSpeedScale, self.benchmarkCam)
table.insert(self.camPaths, camPath)
end)
if #self.camPaths == 0 then
Logging.error("At least one camera path has to be defined in %s", benchmarkXmlFilePath)
end
benchmarkXml:delete()
end
else
Logging.error("No benchmark xml defined (map.benchmark#filename) in %s", g_currentMission.missionInfo.mapXMLFilename)
return false
end
return true
end
function Benchmark:startBenchmark()
g_currentMission.player.walkingIsLocked = true
g_currentMission.hud:setIsVisible(false)
self:setupFields()
self:setupVehicles()
end
function Benchmark:setupFields()
log("Setting up fields with fruits")
g_farmlandManager:consoleCommandBuyAllFarmlands()
for i = 1, #self.fields do
local fieldNumber, fruitType, growthState, sprayTypeState, fertilizerState, plowingState, weedState, limeState, stubbleState, setSpray = unpack(self.fields[i])
g_fieldManager:consoleCommandSetFieldFruit(fieldNumber, fruitType, growthState, sprayTypeState, fertilizerState, plowingState, weedState, limeState, stubbleState, setSpray, true)
end
end
function Benchmark:setupVehicles()
g_currentMission:consoleCommandVehicleRemoveAll()
log("deleted all vehicles")
log("loading vehicles from", self.benmarkVehiclesXmlFilepath)
VehicleLoadingUtil.loadVehiclesFromSavegame(self.benmarkVehiclesXmlFilepath, false, g_currentMission.missionInfo, g_currentMission.missionDynamicInfo, self.loadingVehiclesFinished, self, {
self.benmarkVehiclesXmlFilepath,
false
})
end
function Benchmark:loadingVehiclesFinished(asyncCallbackArguments, vehiclesById)
log("vehicle loading finished")
self.idToVehicle = vehiclesById
for id, vehicle in pairs(vehiclesById) do
log(vehicle:getFullName())
if vehicle.startMotor ~= nil then
vehicle:startMotor()
end
if vehicle.setBeaconLightsVisibility ~= nil then
vehicle:setBeaconLightsVisibility(true, true)
vehicle:setTurnLightState(Lights.TURNLIGHT_HAZARD, true)
vehicle:setLightsTypesMask(Lights.LIGHT_TYPE_HIGHBEAM + Lights.LIGHT_TYPE_WORK_FRONT + Lights.LIGHT_TYPE_WORK_BACK, true)
end
end
self.time = 0
self.isRunning = true
end
function Benchmark:finishBenchmark()
if self.currentCamPath then
self.currentCamPath:deactivate()
self.currentCamPath = nil
end
if self.benchmarkCam ~= nil then
delete(self.benchmarkCam)
end
for id, vehicle in pairs(self.idToVehicle) do
if vehicle.stopCurrentAIJob then
vehicle:stopCurrentAIJob(AIMessageSuccessStoppedByUser.new())
end
end
log("Finished benchmark")
self.isRunning = false
self.hasFinished = true
self.time = 0
g_currentMission.player.walkingIsLocked = false
g_currentMission.hud:setIsVisible(true)
end
function Benchmark:update(dt)
if self.isRunning then
self.time = self.time + dt
for delay, vehicleIds in pairs(self.delayToAiVehicle) do
if delay < self.time then
for i = 1, #vehicleIds do
local vehicle = self.idToVehicle[vehicleIds[i]]
if vehicle then
log(" started ai", vehicle:getFullName())
vehicle:startAIVehicle(nil, nil, vehicle.ownerFarmId)
else
log("unknown vehicle id", vehicleIds[i])
end
end
self.delayToAiVehicle[delay] = nil
end
end
if self.currentCamPath == nil then
self.currentCamPath = table.remove(self.camPaths, 1)
if self.currentCamPath ~= nil then
local durationSecWithSpeedScale = self.currentCamPath.maxTime * 1 / self.currentCamPath.speedScale / 1000
log(string.format("starting next cam path, duration %.3fs", durationSecWithSpeedScale))
self.currentCamPath:activate()
else
log("no more camera paths")
self:finishBenchmark()
end
else
self.currentCamPath:update(dt)
if self.currentCamPath.maxTime <= self.currentCamPath.time then
log("finished camera path")
self.currentCamPath:deactivate()
self.currentCamPath = nil
end
end
end
end
function Benchmark:consoleCommandBenchmark()
if g_currentMission == nil then
return "Error: Cannot run benchmark, load a map first"
end
if g_currentMission.missionDynamicInfo.isMultiplayer then
return "Error: No available in Multiplayer"
end
if self.isRunning then
self:finishBenchmark()
end
if self:load() then
self:startBenchmark()
return "Started benchmark"
else
return "Error: Cannot run benchmark, errors in config"
end
end