This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfsm.bmx
272 lines (230 loc) · 6.4 KB
/
fsm.bmx
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
Rem
Copyright (c) 2010 Christiaan Kras
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
End Rem
SuperStrict
Rem
bbdoc: htbaapub.fsm
EndRem
Module htbaapub.fsm
ModuleInfo "Name: htbaapub.fsm"
ModuleInfo "Version: 1.02"
ModuleInfo "License: MIT"
ModuleInfo "Author: Christiaan Kras"
ModuleInfo "Git repository: <a href='http://github.com/Htbaa/fsm.mod/'>http://github.com/Htbaa/fsm.mod/</a>"
Import brl.linkedlist
Rem
bbdoc: Default FSM State type
End Rem
Const FSM_STATE_NONE:Int = 0
Rem
bbdoc: Default FSM Machine type
End Rem
Const FSM_MACH_NONE:Int = 0
Rem
bbdoc: Finite Machine State Type
End Rem
Type TFSMState Abstract
Field m_type:Int
Field m_parent:Object
Rem
bbdoc: Method executed when this state is entered
End Rem
Method Enter()
End Method
Rem
bbdoc: Main loop of state
about: Optionally assign the amount of time passed to t:Double. This is usually wanted with delta timing.
End Rem
Method Update(t:Double = 0)
End Method
Rem
bbdoc: Method to execute when the State Machine is being setup
End Rem
Method Init()
End Method
Rem
bbdoc: Check for transitions
returns: ID of state to transition to. Make sure every state has a unique identifier
about: In this method you can check if you want to transition to another state or not
End Rem
Method CheckTransitions:Int()
Return FSM_STATE_NONE
End Method
Rem
bbdoc: Method to execute when leaving this state
End Rem
Method Quit()
End Method
Rem
bbdoc: Freeing of state
End Rem
Method Free()
Self.m_parent = Null
End Method
Rem
bbdoc: Handle an incoming message
End Rem
Method OnMessage:Int(message:Object)
Return False
End Method
End Type
Rem
bbdoc: Finite Machine Type
about: TFSMMachine extends TFSMState so you can use the machine itself as a state of another machine
End Rem
Type TFSMMachine Extends TFSMState
Field m_type:Int
'Private data members
Field m_states:TList = New TList
Field m_currentState:TFSMState
Field m_defaultState:TFSMState
Field m_goalState:TFSMState
Field m_goalID:Int
Rem
bbdoc: Create A Finite State Machine
returns: Finite Machine
End Rem
Method Create:TFSMMachine(state_type:Int = FSM_MACH_NONE, parent:Object)
Self.m_type = state_type
Self.m_parent = parent
Self.m_currentState = Null
Self.m_defaultState = Null
Self.m_goalState = Null
Return Self
End Method
Rem
bbdoc: Create A Finite State Machine * DEPRECATED *
about: This function has been deprecated. Use the method Create() instead.
End Rem
Function CreateMachine:TFSMMachine(state_type:Int = FSM_MACH_NONE, parent:Object) Final
Return New TFSMMachine.Create(state_type, parent)
End Function
Rem
bbdoc: Handle incoming messages
returns: True/False
End Rem
Method HandleMessage:Int(message:Object)
If Not Self.m_currentState
Return False
End If
Return Self.m_currentState.OnMessage(message)
End Method
Rem
bbdoc: Update State Machine. Handles state changeing and execution
End Rem
Method UpdateMachine(t:Double)
'Don't do anything if you have no states
If Self.m_states.Count() = 0
Return
End If
'Don't do anything if there's no current
'state, and no default state
If Not Self.m_currentState
Self.m_currentState = m_defaultState
End If
If Not Self.m_currentState
Return
End If
'Check for transitions and then update
Local oldStateID:Int = Self.m_currentState.m_type
Self.m_goalID = Self.m_currentState.CheckTransitions()
'Switch if there was a transition
If Self.m_goalID <> oldStateID
'If Not (Self.m_goalID=oldStateID)
If Self.TransitionState(Self.m_goalID)
Self.m_currentState.Quit()
Self.m_currentState = Self.m_goalState
Self.m_currentState.Enter()
End If
End If
Self.m_currentState.Update(t)
End Method
Rem
bbdoc: Add a state to machine
End Rem
Method AddState(state:TFSMState)
Self.m_states.AddLast(state)
End Method
Rem
bbdoc: Set default state
End Rem
Method SetDefaultState(state:TFSMState)
Self.m_defaultState = state
End Method
Rem
bbdoc: Set goal id
about: Internal method
End Rem
Method SetGoalId(goal:Int)
Self.m_goalID = goal
End Method
Rem
bbdoc: Change state
returns: True/False depending if the transition to the goal state was successful
End Rem
Method TransitionState:Int(goal:Int)
'Don't do anything if you have no states
If Self.m_states.Count() = 0
Return False
End If
'Determine if we have state of type 'goal'
'in the list, and switch to it, otherwise, quit out
For Local state:TFSMState = EachIn Self.m_states
If state.m_type = goal
Self.m_goalState = state
Return True
End If
Next
Return False
End Method
Rem
bbdoc: Reset the state machine
about: Calls Quit() of the machine and states, re-initializes and starts with default state
End Rem
Method Reset()
Self.Quit()
If Self.m_currentState
Self.m_currentState.Quit()
End If
Self.m_currentState = Self.m_defaultState;
'Init all the states
For Local state:TFSMState = EachIn Self.m_states
state.Init()
Next
'And now enter the m_defaultState, if any
If Self.m_currentState
Self.m_currentState.Enter()
End If
End Method
Rem
bbdoc: Free state machine
End Rem
Method Free()
Self.m_currentState = Null
Self.m_defaultState = Null
Self.m_goalState = Null
'Free every state
For Local state:TFSMState = EachIn Self.m_states
state.Free()
Next
'Remove all the states
Self.m_states.Clear()
'Remove parent
Self.m_parent = Null
End Method
End Type