Debian Codegraph @

terps/frankendrift/FrankenDrift.Adrift/clsEvent.vb

raw
1 Imports FrankenDrift.Glue
2
3 Public Class clsEvent
4 Inherits clsItem
5 Public Enum EventTypeEnum As Integer
6 TurnBased = 0
7 TimeBased = 1
8 End Enum
9 Public EventType As EventTypeEnum
10
11 Public Enum WhenStartEnum
12 Immediately = 1
13 BetweenXandYTurns = 2
14 AfterATask = 3
15 End Enum
16 Friend WhenStart As WhenStartEnum
17 Friend arlEventDescriptions As EventDescriptionArrayList
18 Private iTimerToEndOfEvent As Integer
19 Friend iLastSubEventTime As Integer = 0
20 Private stackLookText As New Stack
21 Private Enum Command
22 [Nothing] = 0
23 Start = 1
24 [Stop] = 2
25 Pause = 3
26 [Resume] = 4
27 End Enum
28 Private NextCommand As Command = Command.Nothing
29
30 Private Class clsLookText
31 Public sDescription As String
32 Public sLocationKey As String
33
34 Public Sub New(ByVal sKey As String, ByVal sDescription As String)
35 Me.sLocationKey = sKey
36 Me.sDescription = sDescription
37 End Sub
38
39 End Class
40
41 Public Enum StatusEnum
42 NotYetStarted = 0
43 Running = 1
44 CountingDownToStart = 2
45 Paused = 3
46 Finished = 4
47 End Enum
48 Friend Status As StatusEnum
49
50 Friend EventControls() As EventOrWalkControl = {}
51
52 Public Class SubEvent
53 Implements ICloneable
54
55 Private sParentKey As String
56 Public Sub New(ByVal sEventKey As String)
57 sParentKey = sEventKey
58 End Sub
59
60
61 Public Enum WhenEnum
62 FromLastSubEvent
63 FromStartOfEvent
64 BeforeEndOfEvent
65 End Enum
66 Public eWhen As WhenEnum
67
68 Public Enum WhatEnum
69 DisplayMessage
70 SetLook
71 ExecuteTask
72 UnsetTask
73 End Enum
74 Public eWhat As WhatEnum
75
76 Public Enum MeasureEnum
77 Turns
78 Seconds
79 End Enum
80 Public eMeasure As MeasureEnum
81
82 Friend ftTurns As New FromTo
83 Friend oDescription As New Description
84 Public sKey As String
85
86 Private Function Clone() As Object Implements System.ICloneable.Clone
87 Dim se As SubEvent
88 se = CType(Me.MemberwiseClone, SubEvent)
89 se.ftTurns = ftTurns.CloneMe
90 se.oDescription = oDescription.Copy
91 Return se
92 End Function
93 Public Function CloneMe() As SubEvent
94 Return CType(Me.Clone, SubEvent)
95 End Function
96
97 Public WithEvents tmrTrigger As System.Timers.Timer
98 Public dtStart As DateTime
99 Public iMilliseconds As Integer
100 Private Sub tmrTrigger_Tick(sender As Object, e As System.EventArgs) Handles tmrTrigger.Elapsed
101 tmrTrigger.Enabled = False
102 tmrTrigger.Stop()
103 UserSession.TriggerTimerEvent(sParentKey, Me)
104 End Sub
105
106 End Class
107
108
109 Public SubEvents() As SubEvent = {}
110
111 Friend StartDelay As FromTo
112 Friend Length As FromTo
113
114 Public Property Description() As String
115 Public Property Repeating() As Boolean
116 Public Property RepeatCountdown As Boolean
117
118 Public Sub New()
119 Me.StartDelay = New FromTo
120 Me.Length = New FromTo
121 End Sub
122
123 Public Overrides ReadOnly Property Clone() As clsItem
124 Get
125 Dim ev As clsEvent = CType(Me.MemberwiseClone, clsEvent)
126 ev.StartDelay = ev.StartDelay.CloneMe
127 ev.Length = ev.Length.CloneMe
128 Return ev
129 End Get
130 End Property
131
132 Public Function LookText() As String
133
134 If Status = StatusEnum.Running Then
135
136 ' Pop the first matching LookText off the stack
137 Dim bOkToDisplay As Boolean = False
138 Dim sLookText As String = Nothing
139
140 For Each lt As clsLookText In stackLookText.ToArray
141 If Adventure.Player.IsInGroupOrLocation(lt.sLocationKey) Then
142 sLookText = lt.sDescription
143 bOkToDisplay = True
144 Exit For
145 End If
146 Next
147
148 If bOkToDisplay Then Return sLookText
149
150 End If
151
152 Return Nothing
153
154 End Function
155
156
157 Public Property TimerToEndOfEvent() As Integer
158 Get
159 Return iTimerToEndOfEvent
160 End Get
161 Set(ByVal value As Integer)
162 Dim iStartValue As Integer = iTimerToEndOfEvent
163 iTimerToEndOfEvent = value
164
165 ' If the timer has ticked down and we're ready to start
166 If Status = StatusEnum.CountingDownToStart AndAlso TimerFromStartOfEvent = 0 Then
167 Start(True)
168 End If
169
170 ' If we've reached the end of the timer
171 If Status = StatusEnum.Running AndAlso iTimerToEndOfEvent = 0 Then
172 lStop(True)
173 End If
174
175 End Set
176 End Property
177
178 Friend LastSubEvent As SubEvent
179 Private ReadOnly Property TimerFromLastSubEvent() As Integer
180 Get
181 Return TimerFromStartOfEvent - iLastSubEventTime
182 End Get
183 End Property
184 Friend ReadOnly Property TimerFromStartOfEvent() As Integer
185 Get
186 Return Length.Value - TimerToEndOfEvent ' + 1
187 End Get
188 End Property
189
190
191 Public sTriggeringTask As String
192
193
194 Public bJustStarted As Boolean = False
195 Public Sub Start(Optional ByVal bForce As Boolean = False)
196 If bForce OrElse UserSession.bEventsRunning Then
197 lStart()
198 Else
199 NextCommand = Command.Start
200 End If
201 End Sub
202 Private Sub lStart(Optional ByVal bRestart As Boolean = False)
203 If Status = StatusEnum.NotYetStarted OrElse Status = StatusEnum.CountingDownToStart OrElse Status = StatusEnum.Finished OrElse (Status = StatusEnum.Running AndAlso bRestart) Then
204 If Not bRestart Then UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Low, "Starting event " & Description)
205 Status = StatusEnum.Running
206 Length.Reset()
207
208 LastSubEvent = Nothing
209 iLastSubEventTime = 0
210
211 For Each se As clsEvent.SubEvent In SubEvents
212 se.ftTurns.Reset()
213 If se.eMeasure = SubEvent.MeasureEnum.Seconds AndAlso EventType = EventTypeEnum.TurnBased Then
214 #If Not Adravalon Then
215 se.tmrTrigger = New System.Windows.Forms.Timer
216 #Else
217 se.tmrTrigger = New System.Timers.Timer
218 #End If
219 If se.eWhen = SubEvent.WhenEnum.FromStartOfEvent OrElse (se.eWhen = SubEvent.WhenEnum.FromLastSubEvent AndAlso se Is SubEvents(0)) Then
220 se.tmrTrigger.Interval = se.ftTurns.Value * 1000
221 se.tmrTrigger.Start()
222 se.dtStart = Now
223 End If
224 End If
225 Next
226
227 ' WHAT TO DO HERE?
228 ' If it's length 0, we need to run our start actions
229 ' if it's length 2 we don't want it being set to 1 immediately from the incrementtimer
230 TimerToEndOfEvent = Length.Value
231 If TimerFromStartOfEvent = 0 Then DoAnySubEvents() ' To run 'after 0 turns' subevents
232
233 If WhenStart = WhenStartEnum.Immediately Then WhenStart = WhenStartEnum.BetweenXandYTurns ' So we get 'after 0 turns' on any repeats
234 bJustStarted = True
235 Else
236 'Throw New Exception("Can't Start an Event that isn't waiting!")
237 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Error, "Can't Start an Event that isn't waiting!")
238 End If
239 End Sub
240
241
242 Public Sub Pause()
243 If UserSession.bEventsRunning Then lPause() Else NextCommand = Command.Pause
244 End Sub
245 Private Sub lPause()
246 If Status = StatusEnum.Running Then
247 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Low, "Pausing event " & Description)
248 Status = StatusEnum.Paused
249 For Each se As SubEvent In SubEvents
250 If se.tmrTrigger IsNot Nothing AndAlso se.tmrTrigger.Enabled Then
251 se.iMilliseconds = CInt(se.ftTurns.Value * 1000 - Now.Subtract(se.dtStart).TotalMilliseconds)
252 se.tmrTrigger.Stop()
253 End If
254 Next
255 Else
256 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Error, "Can't Pause an Event that isn't running!")
257 End If
258 End Sub
259
260
261 Public Sub [Resume]()
262 If UserSession.bEventsRunning Then lResume() Else NextCommand = Command.Resume
263 End Sub
264 Private Sub lResume()
265 If Status = StatusEnum.Paused Then
266 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Low, "Resuming event " & Description)
267 Status = StatusEnum.Running
268 For Each se As SubEvent In SubEvents
269 If se.tmrTrigger IsNot Nothing Then
270 If se.iMilliseconds > 0 Then
271 se.tmrTrigger.Interval = se.iMilliseconds
272 se.iMilliseconds = 0
273 se.tmrTrigger.Start()
274 End If
275 End If
276 Next
277 Else
278 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Error, "Can't Resume an Event that isn't paused!")
279 End If
280 End Sub
281
282
283 Public Sub [Stop]()
284 ' If an event runs a task and that task starts/stops an event, do it immediately
285 If UserSession.bEventsRunning Then lStop() Else NextCommand = Command.Stop
286 End Sub
287 Private Sub lStop(Optional ByVal bRunSubEvents As Boolean = False)
288
289 If bRunSubEvents Then DoAnySubEvents()
290 If Status = StatusEnum.Paused Then Exit Sub
291 Status = StatusEnum.Finished
292 For Each se As SubEvent In SubEvents
293 If se.tmrTrigger IsNot Nothing Then se.tmrTrigger.Stop()
294 Next
295 If Repeating AndAlso TimerToEndOfEvent = 0 Then
296 If Length.Value > 0 Then
297 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Low, "Restarting event " & Description)
298 If RepeatCountdown Then
299 Status = clsEvent.StatusEnum.CountingDownToStart
300 StartDelay.Reset()
301 TimerToEndOfEvent = StartDelay.Value + Length.Value
302 Else
303 lStart(True) ' Make sure we don't get ourselves in a loop for zero length events
304 End If
305 Else
306 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Low, "Not restarting event " & Description & " otherwise we'd get in an infinite loop as zero length.")
307 End If
308 Else
309 UserSession.DebugPrint(ItemEnum.Event, Me.Key, DebugDetailLevelEnum.Low, "Finishing event " & Description)
310 End If
311
312 End Sub
313
314
315 Public Sub IncrementTimer()
316
317 If NextCommand <> Command.Nothing Then
318 Select Case NextCommand
319 Case Command.Start
320 lStart()
321 Case Command.Stop
322 lStop()
323 Case Command.Pause
324 lPause()
325 Case Command.Resume
326 lResume()
327 End Select
328 NextCommand = Command.Nothing
329 sTriggeringTask = ""
330 End If
331
332 If Status = StatusEnum.Running OrElse Status = StatusEnum.CountingDownToStart Then UserSession.DebugPrint(ItemEnum.Event, Key, DebugDetailLevelEnum.High, "Event " & Description & " [" & TimerFromStartOfEvent + 1 & "/" & Length.Value & "]")
333
334 ' Split this into 2 case statements, as changing timer here may change status
335 Select Case Status
336 Case StatusEnum.NotYetStarted
337 Case StatusEnum.CountingDownToStart
338 TimerToEndOfEvent -= 1
339 Case StatusEnum.Running
340 If Not bJustStarted Then TimerToEndOfEvent -= 1
341 Case StatusEnum.Paused
342 Case StatusEnum.Finished
343 End Select
344
345 If Not bJustStarted Then DoAnySubEvents()
346
347 bJustStarted = False
348
349 End Sub
350
351 Friend Sub DoAnySubEvents()
352
353 Select Case Status
354 Case StatusEnum.Running
355 ' Check all the subevents to see if we need to do anything
356 Dim iIndex As Integer = 0
357 For Each se As SubEvent In SubEvents
358 If se.eMeasure = SubEvent.MeasureEnum.Turns OrElse EventType = EventTypeEnum.TimeBased Then
359 Dim bRunSubEvent As Boolean = False
360 Select Case se.eWhen
361 Case SubEvent.WhenEnum.FromStartOfEvent
362 If TimerFromStartOfEvent = se.ftTurns.Value AndAlso se.ftTurns.Value <= Length.Value AndAlso (se.ftTurns.Value > 0 OrElse Me.WhenStart <> WhenStartEnum.Immediately) Then bRunSubEvent = True
363 Case SubEvent.WhenEnum.FromLastSubEvent
364 If TimerFromLastSubEvent = se.ftTurns.Value Then
365 If (LastSubEvent Is Nothing AndAlso iIndex = 0) OrElse (iIndex > 0 AndAlso LastSubEvent Is SubEvents(iIndex - 1)) Then bRunSubEvent = True
366 End If
367 Case SubEvent.WhenEnum.BeforeEndOfEvent
368 If TimerToEndOfEvent = se.ftTurns.Value Then bRunSubEvent = True
369 End Select
370
371 If bRunSubEvent Then RunSubEvent(se)
372
373 End If
374 iIndex += 1
375 Next
376 End Select
377
378 End Sub
379
380
381 Public Sub RunSubEvent(ByVal se As SubEvent)
382
383 Select Case se.eWhat
384 Case SubEvent.WhatEnum.DisplayMessage
385 If se.sKey <> "" AndAlso Adventure.Player.IsInGroupOrLocation(se.sKey) Then UserSession.Display(se.oDescription.ToString)
386 Case SubEvent.WhatEnum.ExecuteTask
387 If Adventure.htblTasks.ContainsKey(se.sKey) Then
388 UserSession.DebugPrint(ItemEnum.Event, Key, DebugDetailLevelEnum.Medium, "Event '" & Description & "' attempting to execute task '" & Adventure.htblTasks(se.sKey).Description & "'")
389 UserSession.AttemptToExecuteTask(se.sKey, True, , , , , , , True)
390 End If
391 Case SubEvent.WhatEnum.SetLook
392 ' Push a LookText onto the stack
393 stackLookText.Push(New clsLookText(se.sKey, se.oDescription.ToString))
394 Case SubEvent.WhatEnum.UnsetTask
395 UserSession.DebugPrint(ItemEnum.Event, Key, DebugDetailLevelEnum.Medium, "Event '" & Description & "' unsetting task '" & Adventure.htblTasks(se.sKey).Description & "'")
396 Adventure.htblTasks(se.sKey).Completed = False
397 End Select
398 iLastSubEventTime = TimerFromStartOfEvent
399 LastSubEvent = se
400
401 Dim i As Integer = 0
402 For Each ose As SubEvent In SubEvents
403 i += 1
404 If ose Is se Then
405 If i < SubEvents.Length Then
406 With SubEvents(i)
407 If .eMeasure = SubEvent.MeasureEnum.Seconds AndAlso EventType = EventTypeEnum.TurnBased Then
408 If .eWhen = SubEvent.WhenEnum.FromLastSubEvent Then
409 .tmrTrigger = New System.Timers.Timer
410 If .ftTurns.Value > 0 Then
411 .tmrTrigger.Interval = .ftTurns.Value * 1000
412 .tmrTrigger.Start()
413 .dtStart = Now
414 Else
415 RunSubEvent(SubEvents(i))
416 End If
417 End If
418 End If
419 End With
420 Exit For
421 End If
422 End If
423 Next
424 End Sub
425
426
427 Public Overrides ReadOnly Property CommonName() As String
428 Get
429 Return Description
430 End Get
431 End Property
432
433
434 Friend Overrides ReadOnly Property AllDescriptions() As Generic.List(Of SharedModule.Description)
435 Get
436 Dim all As New Generic.List(Of Description)
437 For Each se As SubEvent In SubEvents
438 all.Add(se.oDescription)
439 Next
440 Return all
441 End Get
442 End Property
443
444
445 Friend Overrides Function FindStringLocal(sSearchString As String, Optional sReplace As String = Nothing, Optional bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As Object
446 Dim iCount As Integer = iReplacements
447 iReplacements += MyBase.FindStringInStringProperty(Description, sSearchString, sReplace, bFindAll)
448 Return iReplacements - iCount
449 End Function
450
451 Public Overrides Function ReferencesKey(ByVal sKey As String) As Integer
452
453 Dim iCount As Integer = 0
454 For i As Integer = 0 To EventControls.Length - 1
455 If EventControls(i).sTaskKey = sKey Then iCount += 1
456 Next
457 For i As Integer = 0 To SubEvents.Length - 1
458 With SubEvents(i)
459 iCount += .oDescription.ReferencesKey(sKey)
460 If .sKey = sKey Then iCount += 1
461 End With
462 Next
463 Return iCount
464
465 End Function
466
467
468 Public Overrides Function DeleteKey(ByVal sKey As String) As Boolean
469
470 For i As Integer = EventControls.Length - 1 To 0 Step -1
471 If EventControls(i).sTaskKey = sKey Then
472 For j As Integer = EventControls.Length - 2 To i Step -1
473 EventControls(j) = EventControls(j + 1)
474 Next
475 ReDim Preserve EventControls(EventControls.Length - 2)
476 End If
477 Next
478 For i As Integer = 0 To SubEvents.Length - 1
479 With SubEvents(i)
480 If Not .oDescription.DeleteKey(sKey) Then Return False
481 If .sKey = sKey Then .sKey = ""
482 End With
483 Next
484
485 Return True
486
487 End Function
488
489 End Class
490
491
492 Public Class clsEventDescription
493
494 Friend arlShowInRooms As New StringArrayList
495 Public Enum WhenShowEnum
496 EventStart
497 WhenPlayerLooks
498 XTurnsFromStart
499 XTurnsFromFinish
500 EventFinish
501 End Enum
502 Friend WhenShow As WhenShowEnum
503
504 End Class