LUADOC - Farming Simulator 19

PlayerStateMachine

Functions

activateState

Description
Activates a player state. Checks if active states allows to use the player state we want to activate. If allowed, the state is activated.
Definition
activateState(string stateNameTo)
Arguments
stringstateNameTothe player state we want to activate
Code
235function PlayerStateMachine:activateState(stateNameTo)
236 local allowed = true
237
238 for stateNameFrom, stateInstance in pairs(self.stateList) do
239 if stateInstance.isActive and (self.fsmTable[stateNameFrom] == nil or not self.fsmTable[stateNameFrom][stateNameTo]) then
240 allowed = false
241 break
242 end
243 end
244
245 -- if self.debugMode then
246 -- print(string.format("-- [PlayerStateMachine:activateState] state(%s) allowed(%s) active(%s)", stateNameTo, tostring(allowed), tostring(self.stateList[stateNameTo].isActive)))
247 -- end
248 if allowed and (self.stateList[stateNameTo] ~= nil) and (self.stateList[stateNameTo].isActive == false) then
249 self.stateList[stateNameTo]:activate()
250 end
251end

consoleCommandDebugFinalStateMachine

Description
Console command to toggle debug of player state machine
Definition
consoleCommandDebugFinalStateMachine()
Code
278function PlayerStateMachine:consoleCommandDebugFinalStateMachine()
279 if self.debugMode then
280 self.debugMode = false
281 else
282 self.debugMode = true
283 end
284end

deactivateState

Description
Deactivates a player state
Definition
deactivateState(string stateName)
Arguments
stringstateName
Code
256function PlayerStateMachine:deactivateState(stateName)
257 if (self.stateList[stateName] ~= nil) and (self.stateList[stateName].isActive == true) then
258 self.stateList[stateName]:deactivate()
259 end
260end

debugDraw

Description
Execute all debug draw methods. Displays is states are active and available. Also draw internal player state debug method.
Definition
debugDraw(float dt)
Arguments
floatdtdelta time in ms
Code
214function PlayerStateMachine:debugDraw(dt)
215 if self.debugMode then
216 setTextColor(1, 1, 0, 1)
217 renderText(0.05, 0.60, 0.02, "[state machine]")
218 local i = 0
219 for stateName, stateInstance in pairs(self.stateList) do
220 renderText(0.05, 0.58 - i * 0.02 , 0.02, string.format("- %s active(%s) isAvailable(%s)", stateName, tostring(stateInstance.isActive), tostring(stateInstance:isAvailable())))
221 i = i + 1
222 end
223 end
224
225 for stateName, stateInstance in pairs(self.stateList) do
226 if stateInstance.inDebugMode(self) then
227 stateInstance:debugDraw(dt)
228 end
229 end
230end

delete

Description
Methods for deleting player state machine
Definition
delete()
Code
146function PlayerStateMachine:delete()
147 if self.player.isOwner then
148 removeConsoleCommand("gsToggleDebugPlayerFSM")
149 end
150
151 for _, stateInstance in pairs(self.stateList) do
152 stateInstance:delete()
153 stateInstance = {}
154 end
155end

getState

Description
Returns a player state
Definition
getState(string stateName)
Arguments
stringstateNamename of the state to search for
Return Values
tableplayerstate
Code
161function PlayerStateMachine:getState(stateName)
162 return self.stateList[stateName]
163end

isActive

Description
Check if a player state is active
Definition
isActive(string stateName)
Arguments
stringstateName
Return Values
booltrueif player state is active
Code
182function PlayerStateMachine:isActive(stateName)
183 if self.stateList[stateName] ~= nil then
184 return self.stateList[stateName].isActive
185 end
186 return false
187end

isAvailable

Description
Check if a player state is available and not already active.
Definition
isAvailable(string stateName)
Arguments
stringstateName
Return Values
booltrueif player state is available
Code
169function PlayerStateMachine:isAvailable(stateName)
170 if self.stateList[stateName] ~= nil then
171 local result = (self.stateList[stateName].isActive == false) and self.stateList[stateName]:isAvailable()
172
173 return result
174 end
175 return false
176end

load

Description
Loads states
Definition
load()
Code
264function PlayerStateMachine:load()
265 for _, stateInstance in pairs(self.stateList) do
266 stateInstance:load()
267 end
268
269 -- Console commands
270 -- self.player.isOwner is init at this point
271 if self.player.isOwner then
272 addConsoleCommand("gsToggleDebugPlayerFSM", "Toggle debug mode for player state machine", "consoleCommandDebugFinalStateMachine", self)
273 end
274end

new

Description
Creating instance of player state machine. Initializing member variables: player states and a table containing those states.
Definition
new(table player, table custom_mt)
Arguments
tableplayerinstance of player
tablecustom_mtmeta table
Return Values
tableinstanceinstance of object
Code
19function PlayerStateMachine:new(player, custom_mt)
20 if custom_mt == nil then
21 custom_mt = PlayerStateMachine_mt
22 end
23 local self = setmetatable({}, custom_mt)
24 self.player = player
25
26 -- State Machine information
27 self.playerStateIdle = PlayerStateIdle:new(self.player, self)
28 self.playerStateWalk = PlayerStateWalk:new(self.player, self)
29 self.playerStateRun = PlayerStateRun:new(self.player, self)
30 self.playerStateJump = PlayerStateJump:new(self.player, self)
31 self.playerStateSwim = PlayerStateSwim:new(self.player, self)
32 self.playerStateFall = PlayerStateFall:new(self.player, self)
33 self.playerStateCrouch = PlayerStateCrouch:new(self.player, self)
34 self.playerStateAnimalInteract = PlayerStateAnimalInteract:new(self.player, self)
35 self.playerStateAnimalRide = PlayerStateAnimalRide:new(self.player, self)
36 self.playerStateAnimalFeed = PlayerStateAnimalFeed:new(self.player, self)
37 self.playerStateAnimalPet = PlayerStateAnimalPet:new(self.player, self)
38 self.playerStatePickup = PlayerStatePickup:new(self.player, self)
39 self.playerStateDrop = PlayerStateDrop:new(self.player, self)
40 self.playerStateThrow = PlayerStateThrow:new(self.player, self)
41 self.playerStateActicateObject = PlayerStateActivateObject:new(self.player, self)
42 self.playerStateUseLight = PlayerStateUseLight:new(self.player, self)
43 self.playerStateCycleHandtool = PlayerStateCycleHandtool:new(self.player, self)
44
45 self.stateList = { ["idle"] = self.playerStateIdle,
46 ["walk"] = self.playerStateWalk,
47 ["run"] = self.playerStateRun,
48 ["jump"] = self.playerStateJump,
49 ["swim"] = self.playerStateSwim,
50 ["fall"] = self.playerStateFall,
51 ["crouch"] = self.playerStateCrouch,
52 ["animalInteract"] = self.playerStateAnimalInteract,
53 ["animalRide"] = self.playerStateAnimalRide,
54 ["animalFeed"] = self.playerStateAnimalFeed,
55 ["animalPet"] = self.playerStateAnimalPet,
56 ["pickup"] = self.playerStatePickup,
57 ["drop"] = self.playerStateDrop,
58 ["throw"] = self.playerStateThrow,
59 ["activateObject"] = self.playerStateActicateObject,
60 ["useLight"] = self.playerStateUseLight,
61 ["cycleHandtool"] = self.playerStateCycleHandtool,
62 }
63
64 -- field [from][to] : allowed
65 self.fsmTable = {}
66 self.fsmTable["walk"] = {}
67 self.fsmTable["walk"]["jump"] = true
68 self.fsmTable["walk"]["run"] = true
69 self.fsmTable["walk"]["swim"] = true
70 self.fsmTable["walk"]["crouch"] = true
71 self.fsmTable["walk"]["pickup"] = true
72 self.fsmTable["walk"]["drop"] = true
73 self.fsmTable["walk"]["throw"] = true
74 self.fsmTable["walk"]["activateObject"] = true
75 self.fsmTable["walk"]["useLight"] = true
76 self.fsmTable["walk"]["cycleHandtool"] = true
77 self.fsmTable["run"] = {}
78 self.fsmTable["run"]["jump"] = true
79 self.fsmTable["run"]["swim"] = true
80 self.fsmTable["run"]["pickup"] = true
81 self.fsmTable["run"]["drop"] = true
82 self.fsmTable["run"]["throw"] = true
83 self.fsmTable["run"]["activateObject"] = true
84 self.fsmTable["run"]["useLight"] = true
85 self.fsmTable["run"]["cycleHandtool"] = true
86 self.fsmTable["run"]["crouch"] = true
87 self.fsmTable["crouch"] = {}
88 self.fsmTable["crouch"]["walk"] = true
89 self.fsmTable["crouch"]["jump"] = true
90 self.fsmTable["crouch"]["swim"] = true
91 self.fsmTable["crouch"]["animalInteract"] = true
92 self.fsmTable["crouch"]["animalRide"] = true
93 self.fsmTable["crouch"]["animalPet"] = true
94 self.fsmTable["crouch"]["animalFeed"] = true
95 self.fsmTable["crouch"]["pickup"] = true
96 self.fsmTable["crouch"]["drop"] = true
97 self.fsmTable["crouch"]["throw"] = true
98 self.fsmTable["crouch"]["activateObject"] = true
99 self.fsmTable["crouch"]["useLight"] = true
100 self.fsmTable["crouch"]["cycleHandtool"] = true
101 self.fsmTable["fall"] = {}
102 self.fsmTable["fall"]["swim"] = true
103 self.fsmTable["fall"]["useLight"] = true
104 self.fsmTable["jump"] = {}
105 self.fsmTable["idle"] = {}
106 self.fsmTable["idle"]["jump"] = true
107 self.fsmTable["idle"]["crouch"] = true
108 self.fsmTable["idle"]["walk"] = true
109 self.fsmTable["idle"]["run"] = true
110 self.fsmTable["idle"]["animalInteract"] = true
111 self.fsmTable["idle"]["animalRide"] = true
112 self.fsmTable["idle"]["animalPet"] = true
113 self.fsmTable["idle"]["animalFeed"] = true
114 self.fsmTable["idle"]["pickup"] = true
115 self.fsmTable["idle"]["drop"] = true
116 self.fsmTable["idle"]["throw"] = true
117 self.fsmTable["idle"]["activateObject"] = true
118 self.fsmTable["idle"]["useLight"] = true
119 self.fsmTable["idle"]["cycleHandtool"] = true
120 self.fsmTable["swim"] = {}
121 self.fsmTable["swim"]["walk"] = true
122 self.fsmTable["swim"]["run"] = true
123 self.fsmTable["swim"]["useLight"] = true
124 self.fsmTable["animalInteract"] = {}
125 self.fsmTable["animalInteract"]["crouch"] = true
126 self.fsmTable["animalInteract"]["idle"] = true
127 self.fsmTable["animalInteract"]["walk"] = true
128 self.fsmTable["animalInteract"]["run"] = true
129 self.fsmTable["animalFeed"] = {}
130 self.fsmTable["animalFeed"]["crouch"] = true
131 self.fsmTable["animalFeed"]["idle"] = true
132 self.fsmTable["animalFeed"]["walk"] = true
133 self.fsmTable["animalFeed"]["run"] = true
134 self.fsmTable["animalPet"] = {}
135 self.fsmTable["animalPet"]["crouch"] = true
136 self.fsmTable["animalPet"]["idle"] = true
137 self.fsmTable["animalPet"]["walk"] = true
138 self.fsmTable["animalPet"]["run"] = true
139
140 self.debugMode = false
141 return self
142end

update

Description
Execute all update methods of active player states
Definition
update(float dt)
Arguments
floatdtdelta time in ms
Code
192function PlayerStateMachine:update(dt)
193 for stateName, stateInstance in pairs(self.stateList) do
194 if stateInstance.isActive then
195 stateInstance:update(dt)
196 end
197 end
198end

updateTick

Description
Execute all update methods when network tick of active player states
Definition
updateTick(float dt)
Arguments
floatdtdelta time in ms
Code
203function PlayerStateMachine:updateTick(dt)
204 for stateName, stateInstance in pairs(self.stateList) do
205 if stateInstance.isActive then
206 stateInstance:updateTick(dt)
207 end
208 end
209end