-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTipAnywhere.lua
382 lines (307 loc) · 10.5 KB
/
TipAnywhere.lua
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
-- ============================================================= --
-- TIP ANYWHERE MOD - loki_79
-- ============================================================= --
TipAnywhere = {}
addModEventListener(TipAnywhere)
TipAnywhere.CONTROLS = {}
TipAnywhere.tip = true
TipAnywhere.shovel = true
TipAnywhere.workAreas = {
['BALER'] = true,
['COMBINECHOPPER'] = false,
['COMBINESWATH'] = false,
['CULTIVATOR'] = false,
['CUTTER'] = false,
['FORAGEWAGON'] = true,
['FRUITPREPARER'] = false,
--['HAULMDROP'] = false,
['MULCHER'] = false,
['MOWER'] = false,
['PLOW'] = false,
--['PLOWSHARE'] = false,
--['RIDGEFORMER'] = false,
['RIDGEMARKER'] = false,
['ROLLER'] = false,
['SALTSPREADER'] = false,
['SOWINGMACHINE'] = false,
['SPRAYER'] = false,
['STONEPICKER'] = false,
['STUMPCUTTER'] = false,
['TEDDER'] = true,
['WEEDER'] = false,
['WINDROWER'] = true,
['DEFAULT'] = false,
['AUXILIARY'] = false
}
TipAnywhere.menuItems = {
[1] = 'BALER',
[2] = 'FORAGEWAGON',
[3] = 'TEDDER',
[4] = 'WINDROWER',
[5] = 'MOWER',
[6] = 'COMBINECHOPPER',
[7] = 'COMBINESWATH',
}
TipAnywhere.OPTION = {
['default'] = 1,
['values'] = {false, true},
['strings'] = {
g_i18n:getText("ui_off"),
g_i18n:getText("ui_on")
}
}
-- HELPER FUNCTIONS
local inGameMenu = g_gui.screenControllers[InGameMenu]
local settingsPage = inGameMenu.pageSettings
local settingsLayout = settingsPage.gameSettingsLayout
TipAnywhereControls = {}
TipAnywhereControls.name = settingsPage.name
function TipAnywhere.setValue(id, value)
TipAnywhere.workAreas[id] = value
end
function TipAnywhere.getValue(id)
return TipAnywhere.workAreas[id] or false
end
function TipAnywhere.getStateIndex(id)
local value = TipAnywhere.getValue(id)
local values = TipAnywhere.OPTION.values
for i, v in pairs(values) do
if value == v then
return i
end
end
return TipAnywhere.OPTION.default
end
function TipAnywhere.addMenuOption(id, original)
local function updateFocusIds(element)
if not element then
return
end
element.focusId = FocusManager:serveAutoFocusId()
for _, child in pairs(element.elements) do
updateFocusIds(child)
end
end
local original = original or settingsPage.checkWoodHarvesterAutoCutBox
local options = TipAnywhere.OPTION.strings
local callback = "onMenuOptionChanged"
local menuOptionBox = original:clone(settingsLayout)
if not menuOptionBox then
print("could not create menu option box")
return
end
menuOptionBox.id = id .. "box"
local menuOption = menuOptionBox.elements[1]
if not menuOption then
print("could not create menu option")
return
end
menuOption.id = id
menuOption.target = TipAnywhereControls
menuOption:setCallback("onClickCallback", callback)
menuOption:setDisabled(false)
local toolTip = menuOption.elements[1]
toolTip:setText(g_i18n:getText("tooltip_tipanywhere_" .. id))
local setting = menuOptionBox.elements[2]
setting:setText(g_i18n:getText("setting_tipanywhere_" .. id))
menuOption:setTexts({unpack(options)})
menuOption:setState(TipAnywhere.getStateIndex(id))
TipAnywhere.CONTROLS[id] = menuOption
-- Assign new focus IDs to the controls as clone() copies the existing ones which are supposed to be unique
updateFocusIds(menuOptionBox)
table.insert(settingsPage.controlsList, menuOptionBox)
return menuOption
end
function TipAnywhere.insertMenuItem(id)
local function tableContainsValue(container, id)
for k, v in pairs(container) do
if v == id then
return true
end
end
return false
end
if not tableContainsValue(TipAnywhere.menuItems, id) then
--print("INSERTING MENU ITEM: " .. id)
table.insert(TipAnywhere.menuItems, id)
TipAnywhere.addMenuOption(id)
settingsLayout:invalidateLayout()
end
end
-- MENU CALLBACK
function TipAnywhereControls.onMenuOptionChanged(self, state, menuOption)
local id = menuOption.id
local value = TipAnywhere.OPTION.values[state]
if value ~= nil then
print("SET " .. id .. " = " .. tostring(value))
TipAnywhere.setValue(id, value)
ToggleSettingEvent.sendEvent(id, value)
end
TipAnywhere.writeSettings()
end
local sectionTitle = nil
for idx, elem in ipairs(settingsLayout.elements) do
if elem.name == "sectionHeader" then
sectionTitle = elem:clone(settingsLayout)
break
end
end
if sectionTitle then
sectionTitle:setText(g_i18n:getText("menu_TipAnywhere_TITLE"))
else
local title = TextElement.new()
title:applyProfile("fs25_settingsSectionHeader", true)
title:setText(g_i18n:getText("menu_TipAnywhere_TITLE"))
title.name = "sectionHeader"
settingsLayout:addElement(title)
end
sectionTitle.focusId = FocusManager:serveAutoFocusId()
table.insert(settingsPage.controlsList, sectionTitle)
TipAnywhere.CONTROLS[sectionTitle.name] = sectionTitle
for _, id in pairs(TipAnywhere.menuItems) do
TipAnywhere.addMenuOption(id)
end
settingsLayout:invalidateLayout()
-- READ/WRITE SETTINGS
function TipAnywhere.writeSettings()
local key = "TipAnywhere"
local userSettingsFile = Utils.getFilename("modSettings/TipAnywhere.xml", getUserProfileAppPath())
local xmlFile = createXMLFile("settings", userSettingsFile, key)
if xmlFile ~= 0 then
local function setXmlValue(id)
local options = TipAnywhere.OPTION
if options then
local xmlValueKey = "TipAnywhere." .. id .. "#value"
local value = TipAnywhere.getValue(id)
setXMLBool(xmlFile, xmlValueKey, value)
end
end
for _, id in pairs(TipAnywhere.menuItems) do
setXmlValue(id)
end
saveXMLFile(xmlFile)
delete(xmlFile)
end
end
function TipAnywhere.readSettings()
local userSettingsFile = Utils.getFilename("modSettings/TipAnywhere.xml", getUserProfileAppPath())
if not fileExists(userSettingsFile) then
print("CREATING user settings file: "..userSettingsFile)
TipAnywhere.writeSettings()
return
end
local xmlFile = loadXMLFile("TipAnywhere", userSettingsFile)
if xmlFile ~= 0 then
local function getXmlValue(id)
local options = TipAnywhere.OPTION
if options then
local xmlSettingKey = "TipAnywhere." .. id
local value = getXMLBool(xmlFile, xmlSettingKey .. "#value") or false
TipAnywhere.setValue(id, value)
if g_currentMission:getIsServer() and hasXMLProperty(xmlFile, xmlSettingKey) then
TipAnywhere.insertMenuItem(id)
return true
end
end
end
print("TIP ANYWHERE SETTINGS")
print(" TIP: " .. tostring(TipAnywhere.tip))
print(" SHOVEL: " .. tostring(TipAnywhere.shovel))
for id, _ in pairs(TipAnywhere.workAreas) do
if getXmlValue(id) then
print(" ".. id ..": " .. tostring(TipAnywhere.workAreas[id]))
end
end
delete(xmlFile)
end
end
-- GAME FUNCTIONS
function TipAnywhere:shovelGetCanShovelAtPosition(superFunc, shovelNode)
if shovelNode == nil then
return false
end
return TipAnywhere.shovel
end
function TipAnywhere:dischargeableGetCanDischargeToLand(superFunc, dischargeNode)
if dischargeNode == nil then
return false
end
return TipAnywhere.tip
end
function TipAnywhere:WorkAreaGetIsAccessibleAtWorldPosition(superFunc, farmId, x, z, workAreaType)
local isAccessible, farmlandOwner, buyable = superFunc(self, farmId, x, z, workAreaType)
local workAreaName = g_workAreaTypeManager:getWorkAreaTypeNameByIndex(workAreaType)
-- print("workAreaName: " .. workAreaName)
if TipAnywhere.workAreas[workAreaName] then
isAccessible = true
end
return isAccessible, farmlandOwner, buyable
end
MissionManager.getIsMissionWorkAllowed = Utils.overwrittenFunction(MissionManager.getIsMissionWorkAllowed,
function(self, superFunc, farmId, x, z, workAreaType)
local isAccessible = superFunc(self, farmId, x, z, workAreaType)
local mission = self:getMissionAtWorldPosition(x, z)
if mission ~= nil and mission.farmId == farmId then
local workAreaName = g_workAreaTypeManager:getWorkAreaTypeNameByIndex(workAreaType)
if TipAnywhere.workAreas[workAreaName] then
isAccessible = true
end
end
return isAccessible
end)
function TipAnywhere.registerTipAnywhereFunctions()
for vehicleName, vehicleType in pairs(g_vehicleTypeManager.types) do
if SpecializationUtil.hasSpecialization(Shovel, vehicleType.specializations) then
SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanShovelAtPosition", TipAnywhere.shovelGetCanShovelAtPosition)
-- print("Shovel Anywhere added to " .. vehicleName)
end
if SpecializationUtil.hasSpecialization(Dischargeable, vehicleType.specializations) then
SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanDischargeToLand", TipAnywhere.dischargeableGetCanDischargeToLand)
-- print("Tip Anywhere added to " .. vehicleName)
end
if SpecializationUtil.hasSpecialization(WorkArea, vehicleType.specializations) then
SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsAccessibleAtWorldPosition", TipAnywhere.WorkAreaGetIsAccessibleAtWorldPosition)
-- print("Work Anywhere ADDED to " .. vehicleName)
end
end
end
function TipAnywhere:loadMap(name)
--print("Loaded Mod: 'TIP ANYWHERE'")
TipAnywhere.readSettings()
TipAnywhere.registerTipAnywhereFunctions()
end
InGameMenuSettingsFrame.onFrameOpen = Utils.appendedFunction(InGameMenuSettingsFrame.onFrameOpen, function()
local isAdmin = g_currentMission:getIsServer() or g_currentMission.isMasterUser
for _, id in pairs(TipAnywhere.menuItems) do
local menuOption = TipAnywhere.CONTROLS[id]
menuOption:setState(TipAnywhere.getStateIndex(id))
menuOption:setDisabled(not isAdmin)
end
end)
-- Allow keyboard navigation of menu options
FocusManager.setGui = Utils.appendedFunction(FocusManager.setGui, function(_, gui)
if gui == "ingameMenuSettings" then
-- Let the focus manager know about our custom controls now (earlier than this point seems to fail)
for _, control in pairs(TipAnywhere.CONTROLS) do
if not control.focusId or not FocusManager.currentFocusData.idToElementMapping[control.focusId] then
if not FocusManager:loadElementFromCustomValues(control, nil, nil, false, false) then
Logging.warning("Could not register control %s with the focus manager", control.id or control.name or control.focusId)
end
end
end
-- Invalidate the layout so the up/down connections are analyzed again by the focus manager
local settingsPage = g_gui.screenControllers[InGameMenu].pageSettings
settingsPage.generalSettingsLayout:invalidateLayout()
end
end)
source(g_currentModDirectory .. 'ToggleSettingEvent.lua')
-- SEND SETTINGS TO CLIENT:
FSBaseMission.sendInitialClientState = Utils.appendedFunction(FSBaseMission.sendInitialClientState,
function(self, connection, user, farm)
for _, id in pairs(TipAnywhere.menuItems) do
local value = TipAnywhere.getValue(id)
if value ~= nil then
ToggleSettingEvent.sendEvent(id, value)
end
end
end)