-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlayerMovement.lua
486 lines (396 loc) · 13.1 KB
/
PlayerMovement.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
-- ============================================================= --
-- PLAYER MOVEMENT MOD - loki_79
-- ============================================================= --
PlayerMovement = {}
PlayerMovement.name = g_currentModName
PlayerMovement.path = g_currentModDirectory
PlayerMovement.SETTINGS = {}
PlayerMovement.CONTROLS = {}
addModEventListener(PlayerMovement)
PlayerMovement.menuItems = {
'walkingMultiplier',
'runningMultiplier',
'jumpMultiplier',
'showDebug',
}
PlayerMovement.menuItemGroups = {
walkingSpeed = 'walkingMultiplier',
fallingSpeed = 'walkingMultiplier',
swimmingSpeed = 'walkingMultiplier',
crouchingSpeed = 'walkingMultiplier',
runningSpeed = 'runningMultiplier',
swimmingSprintSpeed = 'runningMultiplier',
acceleration = 'runningMultiplier',
deceleration = 'runningMultiplier',
jumpForce = 'jumpMultiplier',
}
PlayerMovement.walkingMultiplier = 1
PlayerMovement.runningMultiplier = 1
PlayerMovement.jumpMultiplier = 1
PlayerMovement.walkingSpeed = 7
PlayerMovement.runningSpeed = 15
PlayerMovement.fallingSpeed = 4
PlayerMovement.swimmingSpeed = 4
PlayerMovement.swimmingSprintSpeed = 10
PlayerMovement.crouchingSpeed = 4
PlayerMovement.gravity = 9.81
PlayerMovement.jumpForce = 5.5
PlayerMovement.acceleration = 50
PlayerMovement.deceleration = 40
PlayerMovement.showDebug = false
function debugPrint(str)
if PlayerMovement.showDebug then
print("[PlayerMovement] " .. str)
end
end
function PlayerMovement.updateValue(variable, class, value, noPrint)
local function compareFloats(a, b)
local epsilon = 0.001
return math.abs(1.0*a - 1.0*b) < epsilon
end
local group = PlayerMovement.menuItemGroups[variable]
local multiplier = PlayerMovement[group] or 1
local newValue = multiplier * PlayerMovement[variable]
if not compareFloats(class[value], newValue) then
class[value] = newValue
if not noPrint then
debugPrint(value .. " = " .. newValue)
end
end
end
function PlayerMovement:doUpdate(dt)
local masterServer = g_masterServerConnection.masterServerCallbackTarget
local isSaving = masterServer.isSaving
local isLoadingMap = masterServer.isLoadingMap
local isExitingGame = masterServer.isExitingGame
local isSynchronizingWithPlayers = masterServer.isSynchronizingWithPlayers
if isLoadingMap or isExitingGame or isSaving or isSynchronizingWithPlayers then
return
end
PlayerMovement.startupTime = PlayerMovement.startupTime or 0
if PlayerMovement.startupTime < 500 then
PlayerMovement.startupTime = PlayerMovement.startupTime + dt
-- debugPrint("wait for startup.." .. PlayerMovement.startupTime)
return
end
-- Dedicated Server has no player
if not g_localPlayer then
debugPrint("Warning: no player ID")
return
end
local player = g_localPlayer
local isInVehicle = player:getIsInVehicle()
local isControlled = player.isControlled
local playerIsEntered = isControlled and not isInVehicle
if playerIsEntered and not g_gui:getIsGuiVisible() then
-- CHANGE GLOBAL VALUES ON FIRST RUN
if not PlayerMovement.initialised then
debugPrint("*** PlayerMovement - DEBUG ENABLED ***")
-- UPDATE ALL THE VALUES IF NEEDED
PlayerMovement.updateValue('walkingSpeed', PlayerStateWalk, 'MAXIMUM_WALK_SPEED')
PlayerMovement.updateValue('runningSpeed', PlayerStateWalk, 'MAXIMUM_RUN_SPEED')
PlayerMovement.updateValue('fallingSpeed', PlayerStateFall, 'MAXIMUM_MOVE_SPEED')
PlayerMovement.updateValue('crouchingSpeed', PlayerStateCrouch, 'MAXIMUM_MOVE_SPEED')
PlayerMovement.updateValue('swimmingSpeed', PlayerStateSwim, 'MAXIMUM_MOVE_SPEED')
PlayerMovement.updateValue('swimmingSprintSpeed', PlayerStateSwim, 'MAXIMUM_SPRINT_SPEED')
PlayerMovement.updateValue('gravity', PlayerMover, 'GRAVITY')
PlayerMovement.updateValue('jumpForce', PlayerStateJump, 'JUMP_UPFORCE')
PlayerMovement.updateValue('acceleration', PlayerMover, 'ACCELERATION')
PlayerMovement.updateValue('deceleration', PlayerMover, 'DECELERATION')
PlayerMovement.initialised = true
end
-- SHOW THE SPEED IN F1 MENU
if PlayerMovement.showDebug then
g_currentMission:addExtraPrintText(g_i18n:getText("ui_player_speed") .. string.format(": %.2f ", player.getSpeed() or 0) .. " m/s")
end
-- UPDATE THE JUMP SPEED TO ALWAYS MATCH THE RUNNING/WALKING SPEED
if player.graphicsState.isRunning then
PlayerMovement.updateValue('runningSpeed', PlayerStateJump, 'MAXIMUM_MOVE_SPEED', true)
else
PlayerMovement.updateValue('walkingSpeed', PlayerStateJump, 'MAXIMUM_MOVE_SPEED', true)
end
end
end
function PlayerMovement:update(dt)
if PlayerMovement.stopError then
if not PlayerMovement.printedError then
PlayerMovement.printedError = true
print("PlayerMovement - FATAL ERROR: " .. PlayerMovement.result)
end
return
end
local status, result = pcall(PlayerMovement.doUpdate, self, dt)
if not status then
PlayerMovement.stopError = true
PlayerMovement.result = result
end
end
--DEV
PlayerMovement.SETTINGS.showDebug = {
-- PlayerMovement.showDebug = false
['default'] = 1,
['values'] = {false, true},
['strings'] = {
g_i18n:getText("ui_off"),
g_i18n:getText("ui_on")
}
}
--PLAYER
PlayerMovement.SETTINGS.walkingMultiplier = {
['default'] = 2,
['permission'] = 'playerMovement',
['values'] = {0.8,1.0,1.2,1.5,1.75,2.0,2.5,3.0},
['strings'] = {
"80%",
"100%",
"120%",
"150%",
"175%",
"200%",
"250%",
"300%",
}
}
PlayerMovement.SETTINGS.runningMultiplier = {
['default'] = 2,
['permission'] = 'playerMovement',
['values'] = {0.8,1.0,1.2,1.5,1.75,2.0,2.5,3.0},
['strings'] = {
"80%",
"100%",
"120%",
"150%",
"175%",
"200%",
"250%",
"300%",
}
}
PlayerMovement.SETTINGS.jumpMultiplier = {
['default'] = 2,
['permission'] = 'playerMovement',
['values'] = {0.8,1.0,1.2,1.5,1.75,2.0,2.5,3.0},
['strings'] = {
"80%",
"100%",
"120%",
"150%",
"175%",
"200%",
"250%",
"300%",
}
}
-- HELPER FUNCTIONS
local inGameMenu = g_gui.screenControllers[InGameMenu]
local settingsPage = inGameMenu.pageSettings
local settingsLayout = settingsPage.generalSettingsLayout
PlayerMovementControls = {}
PlayerMovementControls.name = settingsPage.name
function PlayerMovement.setValue(id, value)
PlayerMovement[id] = value
end
function PlayerMovement.getValue(id)
return PlayerMovement[id]
end
function PlayerMovement.getStateIndex(id, value)
local value = value or PlayerMovement.getValue(id)
local values = PlayerMovement.SETTINGS[id].values
if type(value) == 'number' then
local index = PlayerMovement.SETTINGS[id].default
local initialdiff = math.huge
for i, v in pairs(values) do
local currentdiff = math.abs(v - value)
if currentdiff < initialdiff then
initialdiff = currentdiff
index = i
end
end
return index
else
for i, v in pairs(values) do
if value == v then
return i
end
end
end
print(id .. " USING DEFAULT")
return PlayerMovement.SETTINGS[id].default
end
function PlayerMovement.addMenuOption(id)
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
if #PlayerMovement.SETTINGS[id].values == 2 then
original = settingsPage.checkWoodHarvesterAutoCutBox
else
original = settingsPage.multiVolumeVoiceBox
end
local options = PlayerMovement.SETTINGS[id].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 = PlayerMovementControls
menuOption:setCallback("onClickCallback", callback)
menuOption:setDisabled(false)
local toolTip = menuOption.elements[1]
toolTip:setText(g_i18n:getText("tooltip_playermovement_" .. id))
local setting = menuOptionBox.elements[2]
setting:setText(g_i18n:getText("setting_playermovement_" .. id))
menuOption:setTexts({unpack(options)})
menuOption:setState(PlayerMovement.getStateIndex(id))
PlayerMovement.CONTROLS[id] = menuOption
updateFocusIds(menuOptionBox)
table.insert(settingsPage.controlsList, menuOptionBox)
return menuOption
end
-- READ/WRITE SETTINGS
function PlayerMovement.writeSettings()
local key = "playerMovementSettings"
local userSettingsFile = Utils.getFilename("modSettings/PlayerMovement.xml", getUserProfileAppPath())
local xmlFile = createXMLFile("settings", userSettingsFile, key)
if xmlFile ~= 0 then
local function setXmlValue(id)
if not id or not PlayerMovement.SETTINGS[id] then
return
end
if PlayerMovement.SETTINGS[id].serverOnly and g_server == nil then
return
end
local xmlValueKey = "playerMovementSettings." .. id .. "#value"
local value = PlayerMovement.getValue(id)
if type(value) == 'number' then
setXMLFloat(xmlFile, xmlValueKey, value)
elseif type(value) == 'boolean' then
setXMLBool(xmlFile, xmlValueKey, value)
end
end
for _, id in pairs(PlayerMovement.menuItems) do
setXmlValue(id)
end
saveXMLFile(xmlFile)
delete(xmlFile)
end
end
function PlayerMovement.readSettings()
local userSettingsFile = Utils.getFilename("modSettings/PlayerMovement.xml", getUserProfileAppPath())
if not fileExists(userSettingsFile) then
print("CREATING user settings file: "..userSettingsFile)
PlayerMovement.writeSettings()
return
end
local xmlFile = loadXMLFile("playerMovementSettings", userSettingsFile)
if xmlFile ~= 0 then
local function getXmlValue(id)
local setting = PlayerMovement.SETTINGS[id]
if setting then
local xmlValueKey = "playerMovementSettings." .. id .. "#value"
local value = PlayerMovement.getValue(id)
local value_string = tostring(value)
if hasXMLProperty(xmlFile, xmlValueKey) then
if type(value) == 'number' then
value = getXMLFloat(xmlFile, xmlValueKey) or value
if value == math.floor(value) then
value_string = tostring(value)
else
value_string = string.format("%.3f", value)
end
elseif type(value) == 'boolean' then
value = getXMLBool(xmlFile, xmlValueKey) or false
value_string = tostring(value)
end
PlayerMovement.setValue(id, value)
return value_string
end
end
return "MISSING"
end
print("PLAYER MOVEMENT SETTINGS")
for _, id in pairs(PlayerMovement.menuItems) do
local valueString = getXmlValue(id)
print(" " .. id .. ": " .. valueString)
end
delete(xmlFile)
end
end
function PlayerMovement:loadMap(name)
-- print("Load Mod: 'Player Movement Settings'")
PlayerMovement.readSettings()
addConsoleCommand("playerMovementLoadSettings", "Load Player Movement Settings from the local mod settings file", "readSettings", PlayerMovement)
end
-- MENU CALLBACK
function PlayerMovementControls.onMenuOptionChanged(self, state, menuOption)
local id = menuOption.id
local setting = PlayerMovement.SETTINGS
local value = setting[id].values[state]
if value ~= nil then
debugPrint("SET " .. id .. " = " .. tostring(value))
PlayerMovement.setValue(id, value)
end
PlayerMovement.writeSettings()
PlayerMovement.initialised = false
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_PlayerMovement_TITLE"))
else
local title = TextElement.new()
title:applyProfile("fs25_settingsSectionHeader", true)
title:setText(g_i18n:getText("menu_PlayerMovement_TITLE"))
title.name = "sectionHeader"
settingsLayout:addElement(title)
end
sectionTitle.focusId = FocusManager:serveAutoFocusId()
table.insert(settingsPage.controlsList, sectionTitle)
PlayerMovement.CONTROLS[sectionTitle.name] = sectionTitle
for _, id in pairs(PlayerMovement.menuItems) do
PlayerMovement.addMenuOption(id)
end
settingsLayout:invalidateLayout()
-- 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(PlayerMovement.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)
InGameMenuSettingsFrame.onFrameOpen = Utils.appendedFunction(InGameMenuSettingsFrame.onFrameOpen, function()
local isAdmin = g_currentMission:getIsServer() or g_currentMission.isMasterUser
for _, id in pairs(PlayerMovement.menuItems) do
local menuOption = PlayerMovement.CONTROLS[id]
menuOption:setState(PlayerMovement.getStateIndex(id))
menuOption:setDisabled(not isAdmin)
end
end)