Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathCourseplay.lua
More file actions
Latest commit
366 lines (328 loc) · 14.6 KB
/
Copy pathCourseplay.lua
File metadata and controls
366 lines (328 loc) · 14.6 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
363
364
365
366
--- Global class
---@classCourseplay
---@operatorcall:Courseplay
Courseplay=CpObject()
Courseplay.MOD_NAME=g_currentModName
Courseplay.BASE_DIRECTORY=g_currentModDirectory
Courseplay.baseXmlKey="Courseplay"
Courseplay.xmlKey=Courseplay.baseXmlKey.."."
functionCourseplay:init()
g_gui:loadProfiles( Utils.getFilename("config/gui/GUIProfiles.xml", Courseplay.BASE_DIRECTORY) )
--- Base cp folder
self.baseDir=getUserProfileAppPath() .."modSettings/" ..Courseplay.MOD_NAME.."/"
createFolder(self.baseDir)
--- Base cp folder
self.cpFilePath=self.baseDir.."courseplay.xml"
end
functionCourseplay:registerXmlSchema()
self.xmlSchema=XMLSchema.new("Courseplay")
self.xmlSchema:register(XMLValueType.STRING, self.baseXmlKey.."#lastVersion")
self.globalSettings:registerXmlSchema(self.xmlSchema, self.xmlKey)
CpBaseHud.registerXmlSchema(self.xmlSchema, self.xmlKey)
CpHudInfoTexts.registerXmlSchema(self.xmlSchema, self.xmlKey)
end
--- Loads data not tied to a savegame.
functionCourseplay:loadUserSettings()
localxmlFile=XMLFile.loadIfExists("cpXmlFile", self.cpFilePath, self.xmlSchema)
ifxmlFilethen
self:showUserInformation(xmlFile, self.baseXmlKey)
self.globalSettings:loadFromXMLFile(xmlFile, self.xmlKey)
CpBaseHud.loadFromXmlFile(xmlFile, self.xmlKey)
CpHudInfoTexts.loadFromXmlFile(xmlFile, self.xmlKey)
xmlFile:save()
xmlFile:delete()
else
self:showUserInformation()
end
end
--- Saves data not tied to a savegame.
functionCourseplay:saveUserSettings()
localxmlFile=XMLFile.create("cpXmlFile", self.cpFilePath, self.baseXmlKey, self.xmlSchema)
ifxmlFilethen
self.globalSettings:saveUserSettingsToXmlFile(xmlFile, self.xmlKey)
CpBaseHud.saveToXmlFile(xmlFile, self.xmlKey)
CpHudInfoTexts.saveToXmlFile(xmlFile, self.xmlKey)
ifself.currentVersionthen
xmlFile:setValue(self.baseXmlKey.."#lastVersion", self.currentVersion)
end
xmlFile:save()
xmlFile:delete()
end
end
------------------------------------------------------------------------------------------------------------------------
-- User info with github reference and update notification.
------------------------------------------------------------------------------------------------------------------------
functionCourseplay:showUserInformation(xmlFile, key)
localshowInfoDialog=true
self.currentVersion=g_modManager:getModByName(self.MOD_NAME).version
locallastLoadedVersion="----"
ifxmlFilethen
lastLoadedVersion=xmlFile:getValue(key.."#lastVersion")
showInfoDialog=self.currentVersion~=lastLoadedVersion
else
lastLoadedVersion="no config file!"
end
CpUtil.info("Current mod name: %s, Current version: %s, Last version: %s",
self.MOD_NAME, self.currentVersion, lastLoadedVersion)
ifshowInfoDialogthen
g_gui:showInfoDialog({
text=string.format(g_i18n:getText("CP_infoText"), self.currentVersion)
})
ifxmlFilethen
xmlFile:setValue(key.."#lastVersion", self.currentVersion)
end
end
end
------------------------------------------------------------------------------------------------------------------------
-- Global Giants functions listener
------------------------------------------------------------------------------------------------------------------------
--- This function is called on loading a savegame.
---@paramfilenamestring
functionCourseplay:loadMap(filename)
self.globalSettings=CpGlobalSettings()
self:registerXmlSchema()
self:loadUserSettings()
--- Savegame infos here
CpUtil.info("Map loaded: %s, Savegame name: %s(%d)",
g_currentMission.missionInfo.mapId,
g_currentMission.missionInfo.savegameName,
g_currentMission.missionInfo.savegameIndex)
self:load()
self:setupGui()
ifg_currentMission.missionInfo.savegameDirectory~=nilthen
localsaveGamePath=g_currentMission.missionInfo.savegameDirectory.."/"
localfilePath=saveGamePath.."Courseplay.xml"
self.xmlFile=XMLFile.load("cpXml", filePath , self.xmlSchema)
ifself.xmlFile==nilthenreturnend
self.globalSettings:loadFromXMLFile(self.xmlFile, g_Courseplay.xmlKey)
self.xmlFile:delete()
g_assignedCoursesManager:loadAssignedCourses(saveGamePath)
end
--- Ugly hack to get access to the global AutoDrive table, as this global is dependent on the auto drive folder name.
self.autoDrive=FS22_AutoDriveandFS22_AutoDrive.AutoDrive
CpUtil.info("Auto drive found: %s", tostring(self.autoDrive~=nil))
g_courseEditor:load()
end
functionCourseplay:deleteMap()
g_courseEditor:delete()
BufferedCourseDisplay.deleteBuffer()
g_signPrototypes:delete()
g_devHelper:delete()
end
functionCourseplay:setupGui()
localvehicleSettingsFrame=CpVehicleSettingsFrame.new()
localglobalSettingsFrame=CpGlobalSettingsFrame.new()
localcourseManagerFrame=CpCourseManagerFrame.new(self.courseStorage)
g_gui:loadGui(Utils.getFilename("config/gui/VehicleSettingsFrame.xml", Courseplay.BASE_DIRECTORY),
"CpVehicleSettingsFrame", vehicleSettingsFrame, true)
g_gui:loadGui(Utils.getFilename("config/gui/GlobalSettingsFrame.xml", Courseplay.BASE_DIRECTORY),
"CpGlobalSettingsFrame", globalSettingsFrame, true)
g_gui:loadGui(Utils.getFilename("config/gui/CourseManagerFrame.xml", Courseplay.BASE_DIRECTORY),
"CpCourseManagerFrame", courseManagerFrame, true)
localfunctionpredicateFunc()
-- Only allow the vehicle bound pages, when a vehicle with cp functionality is chosen/entered.
localvehicle=CpInGameMenuAIFrameExtended.getVehicle()
returnvehicle~=nilandvehicle.spec_cpAIWorker~=nil
end
--- As precision farming decided to be moved in between the normal map and the ai map,
--- we move it down one position.
localpos=g_modIsLoaded["FS22_precisionFarming"] and4or3
CpGuiUtil.fixInGameMenuPage(vehicleSettingsFrame, "pageCpVehicleSettings",
{896, 0, 128, 128}, pos+1, predicateFunc)
CpGuiUtil.fixInGameMenuPage(globalSettingsFrame, "pageCpGlobalSettings",
{768, 0, 128, 128}, pos+1, function () returntrueend)
CpGuiUtil.fixInGameMenuPage(courseManagerFrame, "pageCpCourseManager",
{256, 0, 128, 128}, pos+1, predicateFunc)
self.infoTextsHud=CpHudInfoTexts()
g_currentMission.hud.ingameMap.drawFields=Utils.appendedFunction(g_currentMission.hud.ingameMap.drawFields, Courseplay.drawHudMap)
end
--- Enables drawing onto the hud map.
functionCourseplay.drawHudMap(map)
ifg_Courseplay.globalSettings.drawOntoTheHudMap:getValue() then
localvehicle=g_currentMission.controlledVehicle
ifvehicleandvehicle:getIsEntered() andnotg_gui:getIsGuiVisible() andvehicle.spec_cpAIWorkerandnotvehicle.spec_locomotivethen
SpecializationUtil.raiseEvent(vehicle, "onCpDrawHudMap", map)
end
end
end
--- Adds cp help info to the in game help menu.
functionCourseplay:loadMapDataHelpLineManager(superFunc, ...)
localret=superFunc(self, ...)
ifretthen
self:loadFromXML(Utils.getFilename("config/HelpMenu.xml", Courseplay.BASE_DIRECTORY))
returntrue
end
returnfalse
end
HelpLineManager.loadMapData=Utils.overwrittenFunction( HelpLineManager.loadMapData, Courseplay.loadMapDataHelpLineManager)
--- Saves all global data, for example global settings.
functionCourseplay.saveToXMLFile(missionInfo)
ifmissionInfo.isValidthen
localsaveGamePath=missionInfo.savegameDirectory.."/"
localxmlFile=XMLFile.create("cpXml", saveGamePath.."Courseplay.xml",
"Courseplay", g_Courseplay.xmlSchema)
ifxmlFilethen
g_Courseplay.globalSettings:saveToXMLFile(xmlFile, g_Courseplay.xmlKey)
xmlFile:save()
xmlFile:delete()
end
g_Courseplay:saveUserSettings()
g_assignedCoursesManager:saveAssignedCourses(saveGamePath)
end
end
FSCareerMissionInfo.saveToXMLFile=Utils.prependedFunction(FSCareerMissionInfo.saveToXMLFile, Courseplay.saveToXMLFile)
functionCourseplay:update(dt)
g_devHelper:update()
g_bunkerSiloManager:update(dt)
g_triggerManager:update(dt)
g_baleToCollectManager:update(dt)
g_courseEditor:update(dt)
ifnotself.postInitthen
-- Doubles the map zoom for 4x Maps. Mainly to make it easier to set targets for unload triggers.
self.postInit=true
localfunctionsetIngameMapFix(mapElement)
localfactor=2*mapElement.terrainSize/2048
mapElement.zoomMax=mapElement.zoomMax*factor
end
setIngameMapFix(g_currentMission.inGameMenu.pageAI.ingameMap)
setIngameMapFix(g_currentMission.inGameMenu.pageMapOverview.ingameMap)
end
end
functionCourseplay:draw()
ifnotg_gui:getIsGuiVisible() then
g_vineScanner:draw()
g_bunkerSiloManager:draw()
g_triggerManager:draw()
g_baleToCollectManager:draw()
end
g_devHelper:draw()
CpDebug:draw()
ifnotg_gui:getIsGuiVisible() andnotg_noHudModeEnabledthen
self.infoTextsHud:draw()
end
end
---@paramposXnumber
---@paramposYnumber
---@paramisDownboolean
---@paramisUpboolean
---@parambuttonnumber
functionCourseplay:mouseEvent(posX, posY, isDown, isUp, button)
ifnotg_gui:getIsGuiVisible() then
localvehicle=g_currentMission.controlledVehicle
localhud=vehicleandvehicle.getCpHudandvehicle:getCpHud()
ifhudthen
hud:mouseEvent(posX, posY, isDown, isUp, button)
end
self.infoTextsHud:mouseEvent(posX, posY, isDown, isUp, button)
end
end
---@paramunicodenumber
---@paramsymnumber
---@parammodifiernumber
---@paramisDownboolean
functionCourseplay:keyEvent(unicode, sym, modifier, isDown)
g_devHelper:keyEvent(unicode, sym, modifier, isDown)
end
functionCourseplay:load()
--- Sub folder for debug information
self.debugDir=self.baseDir.."Debug/"
createFolder(self.debugDir)
--- Sub folder for debug prints
self.debugPrintDir=self.debugDir.."DebugPrints/"
createFolder(self.debugPrintDir)
--- Default path to save prints without an explicit name.
self.defaultDebugPrintPath=self.debugDir.."DebugPrint.xml"
self.courseDir=self.baseDir.."Courses"
createFolder(self.courseDir)
self.courseStorage=FileSystem(self.courseDir, g_currentMission.missionInfo.mapId)
self.courseStorage:fixCourseStorageRoot()
self.customFieldDir=self.baseDir.."CustomFields"
createFolder(self.customFieldDir)
g_customFieldManager=CustomFieldManager(FileSystem(self.customFieldDir, g_currentMission.missionInfo.mapId))
g_vehicleConfigurations:loadFromXml()
g_assignedCoursesManager:registerXmlSchema()
--- Register additional AI messages.
CpAIMessages.register()
g_vineScanner:setup()
end
------------------------------------------------------------------------------------------------------------------------
-- Player action events
------------------------------------------------------------------------------------------------------------------------
--- Adds player mouse action event, for global info texts.
functionCourseplay.addPlayerActionEvents(mission)
ifmission.playerthen
CpUtil.debugFormat(CpDebug.DBG_HUD, "Added player input events")
mission.player.inputInformation.registrationList[InputAction.CP_TOGGLE_MOUSE] = {
text="",
triggerAlways=false,
triggerDown=false,
eventId="",
textVisibility=false,
triggerUp=true,
callback=Courseplay.onOpenCloseMouseEvent,
activeType=Player.INPUT_ACTIVE_TYPE.STARTS_ENABLED
}
mission.player.updateActionEvents=Utils.appendedFunction(mission.player.updateActionEvents, Courseplay.updatePlayerActionEvents)
mission.player.removeActionEvents=Utils.prependedFunction(mission.player.removeActionEvents, Courseplay.removePlayerActionEvents)
end
end
FSBaseMission.onStartMission=Utils.appendedFunction(FSBaseMission.onStartMission , Courseplay.addPlayerActionEvents)
--- Open/close mouse in player state.
functionCourseplay.onOpenCloseMouseEvent(player, forceReset)
ifg_Courseplay.infoTextsHud:isVisible() andnotplayer:hasHandtoolEquipped() then
ifforceResetorg_Courseplay.globalSettings.infoTextHudPlayerMouseActive:getValue() then
localshowMouseCursor=notg_inputBinding:getShowMouseCursor()
g_inputBinding:setShowMouseCursor(showMouseCursor)
localleftRightRotationEventId=player.inputInformation.registrationList[InputAction.AXIS_LOOK_LEFTRIGHT_PLAYER].eventId
localupDownRotationEventId=player.inputInformation.registrationList[InputAction.AXIS_LOOK_UPDOWN_PLAYER].eventId
g_inputBinding:setActionEventActive(leftRightRotationEventId, notshowMouseCursor)
g_inputBinding:setActionEventActive(upDownRotationEventId, notshowMouseCursor)
player.wasCpMouseActive=showMouseCursor
end
end
end
--- Enables/disables the player mouse action event, if there are any info texts.
functionCourseplay.updatePlayerActionEvents(player)
localeventId=player.inputInformation.registrationList[InputAction.CP_TOGGLE_MOUSE].eventId
g_inputBinding:setActionEventTextVisibility(eventId, false)
ifnotplayer:hasHandtoolEquipped() then
ifg_Courseplay.infoTextsHud:isVisible() andg_Courseplay.globalSettings.infoTextHudPlayerMouseActive:getValue() then
g_inputBinding:setActionEventTextVisibility(eventId, true)
elseifplayer.wasCpMouseActivethen
Courseplay.onOpenCloseMouseEvent(player, true)
end
end
end
--- Resets the mouse cursor on entering a vehicle for example.
functionCourseplay.removePlayerActionEvents(player)
ifplayer.wasCpMouseActivethen
g_inputBinding:setShowMouseCursor(false)
end
player.wasCpMouseActive=nil
end
--- Registers all cp specializations.
---@paramtypeManagertable
functionCourseplay.register(typeManager)
iftypeManager.typeName=="vehicle" andg_modIsLoaded[Courseplay.MOD_NAME] then
--- TODO: make this function async.
fortypeName, typeEntryinpairs(typeManager.types) do
CpAIWorker.register(typeManager, typeName, typeEntry.specializations)
CpVehicleSettings.register(typeManager, typeName, typeEntry.specializations)
CpCourseGeneratorSettings.register(typeManager, typeName, typeEntry.specializations)
CpCourseManager.register(typeManager, typeName, typeEntry.specializations)
CpAIFieldWorker.register(typeManager, typeName, typeEntry.specializations)
CpAIBaleFinder.register(typeManager, typeName, typeEntry.specializations)
CpAICombineUnloader.register(typeManager, typeName, typeEntry.specializations)
CpAISiloLoaderWorker.register(typeManager, typeName, typeEntry.specializations)
CpAIBunkerSiloWorker.register(typeManager, typeName, typeEntry.specializations)
CpGamePadHud.register(typeManager, typeName,typeEntry.specializations)
CpHud.register(typeManager, typeName, typeEntry.specializations)
CpInfoTexts.register(typeManager, typeName, typeEntry.specializations)
CpShovelPositions.register(typeManager, typeName, typeEntry.specializations)
end
typeManager:addSpecialization("fillableImplement", "aiLoadable")
end
end
TypeManager.finalizeTypes=Utils.prependedFunction(TypeManager.finalizeTypes, Courseplay.register)
g_Courseplay=Courseplay()
addModEventListener(g_Courseplay)