Debian Codegraph @

terps/frankendrift/FrankenDrift.Adrift/clsState.vb

raw
1 Imports System.IO
2 Imports System.Collections.Generic
3
4
5 ' Gives us the same functionality as a stack, but restricts it to 100
6 Public Class MyStack
7 Private Const MAXLENGTH As Integer = 100
8
9 Private States(MAXLENGTH) As clsGameState
10 Private iStart As Integer = -1
11 Private iEnd As Integer = -1
12
13 Friend Sub Push(ByVal state As clsGameState)
14 If iStart > iEnd Then iStart += 1
15 If iStart > MAXLENGTH Then iStart = 0
16 iEnd += 1
17
18 If iEnd > MAXLENGTH Then
19 iEnd = 0
20 If iStart = 0 Then iStart = 1
21 End If
22 If iStart = -1 Then iStart = 0
23
24 States(iEnd) = state
25 End Sub
26
27 Friend Function Count() As Integer
28 If iEnd >= iStart Then
29 If iStart = -1 AndAlso iEnd = -1 Then Return 0
30 Return iEnd - iStart + 1
31 Else
32 Return MAXLENGTH + 1
33 End If
34 End Function
35
36 Friend Function Peek() As clsGameState
37 If Count() = 0 Then Return Nothing
38 Return States(iEnd)
39 End Function
40
41 Friend Function Pop() As clsGameState
42
43 Dim state As clsGameState = Peek()
44 If state IsNot Nothing Then
45 iEnd -= 1
46 If iEnd < 0 Then
47 iEnd = MAXLENGTH
48 End If
49 End If
50 Return state
51
52 End Function
53
54
55 Friend Sub Clear()
56 iStart = -1
57 iEnd = -1
58 End Sub
59
60 End Class
61
62 Public Class StateStack
63 Inherits MyStack
64
65 Friend Shadows Sub Push(ByVal state As clsGameState)
66 If stateCurrent IsNot Nothing Then stateLast = stateCurrent
67 MyBase.Push(state)
68 Debug.WriteLine("Pushed (" & MyBase.Count & " on stack)")
69 stateCurrent = state
70 End Sub
71
72 Shadows Sub Pop()
73 RestoreState(CType(MyBase.Pop, clsGameState))
74 Debug.WriteLine("Popped (" & MyBase.Count & " on stack)")
75 End Sub
76
77 Private Sub SaveDisplayOnce(ByVal AllDescriptions As List(Of Description), ByVal htblStore As Dictionary(Of String, Boolean))
78
79 Dim iDesc As Integer = 0
80 For Each d As Description In AllDescriptions
81 iDesc += 1
82 Dim iSing As Integer = 0
83 For Each sd As SingleDescription In d
84 iSing += 1
85 If sd.DisplayOnce AndAlso sd.Displayed Then
86 Dim sKey As String = iDesc & "-" & iSing
87 htblStore.Add(sKey, True)
88 End If
89 Next
90 Next
91 End Sub
92
93 ' Get the current game state, and store in a GameState class
94 Friend Function GetState() As clsGameState
95 Dim NewState As New clsGameState
96
97 With NewState
98
99 .sOutputText = UserSession.sTurnOutput
100
101 For Each loc As clsLocation In Adventure.htblLocations.Values
102 Dim locs As New clsGameState.clsLocationState
103 For Each sPropKey As String In loc.htblActualProperties.Keys
104 Dim prop As clsProperty = loc.htblProperties(sPropKey)
105 Dim props As New clsGameState.clsLocationState.clsStateProperty
106 props.Value = prop.Value(True)
107 locs.htblProperties.Add(sPropKey, props)
108 Next
109 SaveDisplayOnce(loc.AllDescriptions, locs.htblDisplayedDescriptions)
110 locs.bSeen = Adventure.Player.HasSeenLocation(loc.Key)
111 .htblLocationStates.Add(loc.Key, locs)
112 Next
113
114 For Each ob As clsObject In Adventure.htblObjects.Values
115 Dim obs As New clsGameState.clsObjectState
116 obs.Location = ob.Location.Copy
117 For Each sPropKey As String In ob.htblActualProperties.Keys
118 Dim prop As clsProperty = ob.htblProperties(sPropKey)
119 Dim props As New clsGameState.clsObjectState.clsStateProperty
120 props.Value = prop.Value(True)
121 obs.htblProperties.Add(sPropKey, props)
122 Next
123 SaveDisplayOnce(ob.AllDescriptions, obs.htblDisplayedDescriptions)
124 .htblObjectStates.Add(ob.Key, obs)
125 Next
126
127 For Each tas As clsTask In Adventure.htblTasks.Values
128 Dim tass As New clsGameState.clsTaskState
129 tass.Completed = tas.Completed
130 tass.Scored = tas.Scored
131 SaveDisplayOnce(tas.AllDescriptions, tass.htblDisplayedDescriptions)
132 .htblTaskStates.Add(tas.Key, tass)
133 Next
134
135 For Each ev As clsEvent In Adventure.htblEvents.Values
136 Dim evs As New clsGameState.clsEventState
137 evs.Status = ev.Status
138 evs.TimerToEndOfEvent = ev.TimerToEndOfEvent
139 evs.iLastSubEventTime = ev.iLastSubEventTime
140 For i As Integer = 0 To ev.SubEvents.Length - 1
141 If ev.LastSubEvent Is ev.SubEvents(i) Then
142 evs.iLastSubEventIndex = i
143 Exit For
144 End If
145 Next
146 SaveDisplayOnce(ev.AllDescriptions, evs.htblDisplayedDescriptions)
147 .htblEventStates.Add(ev.Key, evs)
148 Next
149
150 For Each ch As clsCharacter In Adventure.htblCharacters.Values
151 Dim chs As New clsGameState.clsCharacterState
152 chs.Location = ch.Location
153 For Each w As clsWalk In ch.arlWalks
154 Dim ws As New clsGameState.clsCharacterState.clsWalkState
155 ws.Status = w.Status
156 ws.TimerToEndOfWalk = w.iTimerToEndOfWalk
157 chs.lWalks.Add(ws)
158 Next
159 chs.lSeenKeys.Clear()
160 For Each sLocKey As String In Adventure.htblLocations.Keys
161 If ch.HasSeenLocation(sLocKey) Then chs.lSeenKeys.Add(sLocKey)
162 Next
163 For Each sObKey As String In Adventure.htblObjects.Keys
164 If ch.HasSeenObject(sObKey) Then chs.lSeenKeys.Add(sObKey)
165 Next
166 For Each sChKey As String In Adventure.htblCharacters.Keys
167 If ch.HasSeenCharacter(sChKey) Then chs.lSeenKeys.Add(sChKey)
168 Next
169 For Each sPropKey As String In ch.htblActualProperties.Keys
170 Dim prop As clsProperty = ch.htblProperties(sPropKey)
171 Dim props As New clsGameState.clsCharacterState.clsStateProperty
172 props.Value = prop.Value(True)
173 chs.htblProperties.Add(sPropKey, props)
174 Next
175 If Not chs.htblProperties.ContainsKey("ProperName") Then
176 Dim props As New clsGameState.clsCharacterState.clsStateProperty
177 props.Value = ch.ProperName
178 chs.htblProperties.Add("ProperName", props)
179 End If
180 SaveDisplayOnce(ch.AllDescriptions, chs.htblDisplayedDescriptions)
181 .htblCharacterStates.Add(ch.Key, chs)
182 Next
183
184 For Each var As clsVariable In Adventure.htblVariables.Values
185 Dim vars As New clsGameState.clsVariableState
186 ReDim vars.Value(var.Length - 1)
187 For i As Integer = 0 To var.Length - 1
188 If var.Type = clsVariable.VariableTypeEnum.Numeric Then
189 vars.Value(i) = var.IntValue(i + 1).ToString
190 Else
191 vars.Value(i) = var.StringValue(i + 1)
192 End If
193 Next
194 SaveDisplayOnce(var.AllDescriptions, vars.htblDisplayedDescriptions)
195 ' TODO: Arrays
196 .htblVariableStates.Add(var.Key, vars)
197 Next
198 For Each grp As clsGroup In Adventure.htblGroups.Values
199 Dim grps As New clsGameState.clsGroupState
200 For Each sMembers As String In grp.arlMembers
201 grps.lstMembers.Add(sMembers)
202 Next
203 .htblGroupStates.Add(grp.Key, grps)
204 Next
205
206 End With
207
208 Return NewState
209
210 End Function
211
212 ' Save the current game state onto our stack
213 Public Sub RecordState()
214 Push(GetState)
215 End Sub
216
217 ' Load from file, and restore
218 Friend Sub LoadState(ByVal sFilePath As String)
219 Dim NewState As clsGameState = Nothing
220 LoadFile(sFilePath, FileTypeEnum.GameState_TAS, LoadWhatEnum.All, False, , NewState)
221 If NewState IsNot Nothing Then RestoreState(NewState)
222 End Sub
223
224 Private Sub RestoreDisplayOnce(ByVal AllDescriptions As List(Of Description), ByVal htblStore As Dictionary(Of String, Boolean))
225
226 Dim iDesc As Integer = 0
227 For Each d As Description In AllDescriptions
228 iDesc += 1
229 Dim iSing As Integer = 0
230 For Each sd As SingleDescription In d
231 iSing += 1
232 If sd.DisplayOnce Then
233 Dim sKey As String = iDesc & "-" & iSing
234 If htblStore.ContainsKey(sKey) Then sd.Displayed = True Else sd.Displayed = False
235 End If
236 Next
237 Next
238
239 End Sub
240
241 Friend Sub RestoreState(ByVal state As clsGameState)
242
243 If state Is Nothing Then Exit Sub
244
245 With state
246
247 UserSession.sTurnOutput = .sOutputText
248
249 For Each loc As clsLocation In Adventure.htblLocations.Values
250 If .htblLocationStates.ContainsKey(loc.Key) Then
251 Dim locs As clsGameState.clsLocationState = CType(.htblLocationStates(loc.Key), clsGameState.clsLocationState)
252 Dim lDelete As New List(Of String)
253 For Each prop As clsProperty In loc.htblProperties.Values
254 If locs.htblProperties.ContainsKey(prop.Key) Then
255 Dim props As clsGameState.clsLocationState.clsStateProperty = CType(locs.htblProperties(prop.Key), clsGameState.clsLocationState.clsStateProperty)
256 prop.Value = props.Value
257 Else
258 lDelete.Add(prop.Key)
259 End If
260 Next
261 For Each sKey As String In lDelete
262 loc.RemoveProperty(sKey)
263 Next
264 For Each sPropKey As String In locs.htblProperties.Keys
265 If Not loc.htblActualProperties.ContainsKey(sPropKey) Then
266 If Adventure.htblLocationProperties.ContainsKey(sPropKey) Then
267 Dim propAdd As clsProperty = CType(Adventure.htblLocationProperties(sPropKey).Clone, clsProperty)
268 If propAdd.Type = clsProperty.PropertyTypeEnum.SelectionOnly Then
269 propAdd.Selected = True
270 loc.AddProperty(propAdd)
271 End If
272 End If
273 End If
274 Next
275 loc.ResetInherited()
276 RestoreDisplayOnce(loc.AllDescriptions, locs.htblDisplayedDescriptions)
277 End If
278 Next
279
280 For Each ob As clsObject In Adventure.htblObjects.Values
281 If .htblObjectStates.ContainsKey(ob.Key) Then
282 Dim obs As clsGameState.clsObjectState = CType(.htblObjectStates(ob.Key), clsGameState.clsObjectState)
283 ob.Location = obs.Location.Copy
284 Dim lDelete As New List(Of String)
285 For Each prop As clsProperty In ob.htblProperties.Values
286 If obs.htblProperties.ContainsKey(prop.Key) Then
287 Dim props As clsGameState.clsObjectState.clsStateProperty = CType(obs.htblProperties(prop.Key), clsGameState.clsObjectState.clsStateProperty)
288 prop.Value = props.Value
289 Else
290 lDelete.Add(prop.Key)
291 End If
292 Next
293 For Each sKey As String In lDelete
294 ob.RemoveProperty(sKey)
295 Next
296 For Each sPropKey As String In obs.htblProperties.Keys
297 If Not ob.htblActualProperties.ContainsKey(sPropKey) Then
298 If Adventure.htblObjectProperties.ContainsKey(sPropKey) Then
299 Dim propAdd As clsProperty = CType(Adventure.htblObjectProperties(sPropKey).Clone, clsProperty)
300 If propAdd.Type = clsProperty.PropertyTypeEnum.SelectionOnly Then
301 propAdd.Selected = True
302 ob.AddProperty(propAdd)
303 End If
304 End If
305 End If
306 Next
307 ob.ResetInherited()
308 RestoreDisplayOnce(ob.AllDescriptions, obs.htblDisplayedDescriptions)
309 End If
310 Next
311
312 For Each tas As clsTask In Adventure.htblTasks.Values
313 If .htblTaskStates.ContainsKey(tas.Key) Then
314 Dim tass As clsGameState.clsTaskState = CType(.htblTaskStates(tas.Key), clsGameState.clsTaskState)
315 tas.Completed = tass.Completed
316 tas.Scored = tass.Scored
317 RestoreDisplayOnce(tas.AllDescriptions, tass.htblDisplayedDescriptions)
318 End If
319 Next
320
321 For Each ev As clsEvent In Adventure.htblEvents.Values
322 If .htblEventStates.ContainsKey(ev.Key) Then
323 Dim evs As clsGameState.clsEventState = CType(.htblEventStates(ev.Key), clsGameState.clsEventState)
324 ev.Status = evs.Status
325 ev.TimerToEndOfEvent = evs.TimerToEndOfEvent
326 ev.iLastSubEventTime = evs.iLastSubEventTime
327 If ev.SubEvents IsNot Nothing AndAlso ev.SubEvents.Length > evs.iLastSubEventIndex Then ev.LastSubEvent = ev.SubEvents(evs.iLastSubEventIndex)
328 RestoreDisplayOnce(ev.AllDescriptions, evs.htblDisplayedDescriptions)
329 End If
330 Next
331
332 For Each ch As clsCharacter In Adventure.htblCharacters.Values
333 If .htblCharacterStates.ContainsKey(ch.Key) Then
334 Dim chs As clsGameState.clsCharacterState = CType(.htblCharacterStates(ch.Key), clsGameState.clsCharacterState)
335 ch.Location = chs.Location
336 If ch.arlWalks.Count = chs.lWalks.Count Then
337 For i As Integer = 0 To ch.arlWalks.Count - 1
338 Dim w As clsWalk = ch.arlWalks(i)
339 Dim ws As clsGameState.clsCharacterState.clsWalkState = chs.lWalks(i)
340 w.Status = ws.Status
341 w.iTimerToEndOfWalk = ws.TimerToEndOfWalk
342 Next
343 End If
344 For Each sLocKey As String In Adventure.htblLocations.Keys
345 ch.HasSeenLocation(sLocKey) = chs.lSeenKeys.Contains(sLocKey)
346 Next
347 For Each sObKey As String In Adventure.htblObjects.Keys
348 ch.HasSeenObject(sObKey) = chs.lSeenKeys.Contains(sObKey)
349 Next
350 For Each sChKey As String In Adventure.htblCharacters.Keys
351 ch.HasSeenCharacter(sChKey) = chs.lSeenKeys.Contains(sChKey)
352 Next
353 Dim lDelete As New List(Of String)
354 For Each prop As clsProperty In ch.htblProperties.Values
355 If chs.htblProperties.ContainsKey(prop.Key) Then
356 Dim props As clsGameState.clsCharacterState.clsStateProperty = CType(chs.htblProperties(prop.Key), clsGameState.clsCharacterState.clsStateProperty)
357 prop.Value = props.Value
358 Else
359 lDelete.Add(prop.Key)
360 End If
361 Next
362 For Each sKey As String In lDelete
363 ch.RemoveProperty(sKey)
364 Next
365 For Each sPropKey As String In chs.htblProperties.Keys
366 If Not ch.htblActualProperties.ContainsKey(sPropKey) Then
367 If Adventure.htblCharacterProperties.ContainsKey(sPropKey) Then
368 Dim propAdd As clsProperty = CType(Adventure.htblCharacterProperties(sPropKey).Clone, clsProperty)
369 If propAdd.Type = clsProperty.PropertyTypeEnum.SelectionOnly Then
370 propAdd.Selected = True
371 ch.AddProperty(propAdd)
372 End If
373 Else
374 Select Case sPropKey
375 Case "ProperName"
376 ch.ProperName = chs.htblProperties(sPropKey).Value
377 End Select
378 End If
379 End If
380 Next
381 ch.ResetInherited()
382 RestoreDisplayOnce(ch.AllDescriptions, chs.htblDisplayedDescriptions)
383 End If
384 Next
385
386 For Each var As clsVariable In Adventure.htblVariables.Values
387 If .htblVariableStates.ContainsKey(var.Key) Then
388 Dim vars As clsGameState.clsVariableState = CType(.htblVariableStates(var.Key), clsGameState.clsVariableState)
389 For i As Integer = 0 To var.Length - 1
390 If var.Type = clsVariable.VariableTypeEnum.Numeric Then var.IntValue(i + 1) = SafeInt(vars.Value(i)) Else var.StringValue(i + 1) = vars.Value(i)
391 Next
392 RestoreDisplayOnce(var.AllDescriptions, vars.htblDisplayedDescriptions)
393 End If
394 Next
395
396 For Each grp As clsGroup In Adventure.htblGroups.Values
397 If .htblGroupStates.ContainsKey(grp.Key) Then
398 Dim grps As clsGameState.clsGroupState = CType(.htblGroupStates(grp.Key), clsGameState.clsGroupState)
399 Dim lMembersToReset As New List(Of String)
400 For Each sMember As String In grp.arlMembers
401 lMembersToReset.Add(sMember)
402 Next
403 grp.arlMembers.Clear()
404 For Each sMember As String In grps.lstMembers
405 grp.arlMembers.Add(sMember)
406 If lMembersToReset.Contains(sMember) Then lMembersToReset.Remove(sMember) Else lMembersToReset.Add(sMember)
407 Next
408 For Each sMember As String In lMembersToReset
409 If Adventure.dictAllItems.ContainsKey(sMember) Then CType(Adventure.dictAllItems(sMember), clsItemWithProperties).ResetInherited()
410 Next
411 End If
412 Next
413
414 End With
415
416 End Sub
417
418
419 Private stateLast As clsGameState
420 Private stateCurrent As clsGameState
421
422 Public Function SetLastState() As Boolean
423 If MyBase.Count > 1 Then
424 MyBase.Pop() ' Discard current state
425 RestoreState(CType(MyBase.Peek, clsGameState))
426 Adventure.eGameState = clsAction.EndGameEnum.Running
427 Debug.WriteLine("Popped (" & MyBase.Count & " on stack)")
428 Return True
429 Else
430 Return False
431 End If
432 End Function
433
434 Public Sub SetCurrentState()
435 RestoreState(stateCurrent)
436 End Sub
437
438 Public Shadows Sub Clear()
439 MyBase.Clear()
440 stateLast = Nothing
441 stateCurrent = Nothing
442 End Sub
443
444 End Class
445
446
447 Friend Class clsGameState
448 Friend sOutputText As String
449 Friend Class clsObjectState
450 Friend Class clsStateProperty
451 Friend Value As String
452 End Class
453
454 Public Location As clsObjectLocation
455 Friend htblProperties As New Dictionary(Of String, clsStateProperty)
456 Friend htblDisplayedDescriptions As New Dictionary(Of String, Boolean)
457 End Class
458 Public htblObjectStates As New Dictionary(Of String, clsObjectState)
459
460 Friend Class clsCharacterState
461 Friend Class clsWalkState
462 Friend Status As clsWalk.StatusEnum
463 Friend TimerToEndOfWalk As Integer
464 End Class
465 <DebuggerDisplay("CharState={Value}")>
466 Friend Class clsStateProperty
467 Friend Value As String
468 End Class
469
470 Friend Location As clsCharacterLocation
471 Friend lWalks As New List(Of clsWalkState)
472 Friend lSeenKeys As New List(Of String)
473 Friend htblProperties As New Dictionary(Of String, clsStateProperty)
474 Friend htblDisplayedDescriptions As New Dictionary(Of String, Boolean)
475 End Class
476 Public htblCharacterStates As New Dictionary(Of String, clsCharacterState)
477
478 Friend Class clsTaskState
479 Friend Completed As Boolean
480 Friend Scored As Boolean
481 Friend htblDisplayedDescriptions As New Dictionary(Of String, Boolean)
482 End Class
483 Friend htblTaskStates As New Dictionary(Of String, clsTaskState)
484
485 Friend Class clsEventState
486 Friend Status As clsEvent.StatusEnum
487 Friend TimerToEndOfEvent As Integer
488 Friend iLastSubEventTime As Integer
489 Friend iLastSubEventIndex As Integer
490 Friend htblDisplayedDescriptions As New Dictionary(Of String, Boolean)
491 End Class
492 Friend htblEventStates As New Dictionary(Of String, clsEventState)
493
494 Friend Class clsVariableState
495 Friend Value() As String
496 Friend htblDisplayedDescriptions As New Dictionary(Of String, Boolean)
497 End Class
498 Friend htblVariableStates As New Dictionary(Of String, clsVariableState)
499
500 Friend Class clsLocationState
501 Friend Class clsStateProperty
502 Friend Value As String
503 End Class
504
505 Friend htblProperties As New Dictionary(Of String, clsStateProperty)
506 Friend htblDisplayedDescriptions As New Dictionary(Of String, Boolean)
507 Friend bSeen As Boolean
508 End Class
509 Public htblLocationStates As New Dictionary(Of String, clsLocationState)
510
511 Friend Class clsGroupState
512 Friend lstMembers As New List(Of String)
513 End Class
514 Friend htblGroupStates As New Dictionary(Of String, clsGroupState)
515
516 End Class