-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCMission.cpp
1478 lines (1091 loc) · 30.6 KB
/
CMission.cpp
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CMission.cpp
//
// CMission class
// Copyright (c) 2012 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
static CObjectClass<CMission>g_MissionClass(OBJID_CMISSION, NULL);
#define EVENT_ON_ACCEPTED CONSTLIT("OnAccepted")
#define EVENT_ON_COMPLETED CONSTLIT("OnCompleted")
#define EVENT_ON_DECLINED CONSTLIT("OnDeclined")
#define EVENT_ON_REWARD CONSTLIT("OnReward")
#define EVENT_ON_SET_PLAYER_TARGET CONSTLIT("OnSetPlayerTarget")
#define EVENT_ON_STARTED CONSTLIT("OnStarted")
#define PROPERTY_ACCEPTED_ON CONSTLIT("acceptedOn")
#define PROPERTY_COMPLETED_ON CONSTLIT("completedOn")
#define PROPERTY_DEBRIEFER_ID CONSTLIT("debrieferID")
#define PROPERTY_IN_PROGRESS CONSTLIT("inProgress")
#define PROPERTY_IS_ACTIVE CONSTLIT("isActive")
#define PROPERTY_IS_COMPLETED CONSTLIT("isCompleted")
#define PROPERTY_IS_DEBRIEFED CONSTLIT("isDebriefed")
#define PROPERTY_IS_DECLINED CONSTLIT("isDeclined")
#define PROPERTY_IS_FAILURE CONSTLIT("isFailure")
#define PROPERTY_IS_INTRO_SHOWN CONSTLIT("isIntroShown")
#define PROPERTY_IS_OPEN CONSTLIT("isOpen")
#define PROPERTY_IS_RECORDED CONSTLIT("isRecorded")
#define PROPERTY_IS_SUCCESS CONSTLIT("isSuccess")
#define PROPERTY_IS_UNAVAILABLE CONSTLIT("isUnavailable")
#define PROPERTY_NAME CONSTLIT("name")
#define PROPERTY_NODE_ID CONSTLIT("nodeID")
#define PROPERTY_OWNER_ID CONSTLIT("ownerID")
#define PROPERTY_SUMMARY CONSTLIT("summary")
#define PROPERTY_UNID CONSTLIT("unid")
#define REASON_ACCEPTED CONSTLIT("accepted")
#define REASON_DEBRIEFED CONSTLIT("debriefed")
#define REASON_DESTROYED CONSTLIT("destroyed")
#define REASON_FAILURE CONSTLIT("failure")
#define REASON_IN_PROGRESS CONSTLIT("inProgress")
#define REASON_NEW_SYSTEM CONSTLIT("newSystem")
#define REASON_SUCCESS CONSTLIT("success")
#define SPECIAL_OWNER_ID CONSTLIT("ownerID:")
#define STATUS_OPEN CONSTLIT("open")
#define STATUS_CLOSED CONSTLIT("closed")
#define STATUS_ACCEPTED CONSTLIT("accepted")
#define STATUS_PLAYER_SUCCESS CONSTLIT("playerSuccess")
#define STATUS_PLAYER_FAILURE CONSTLIT("playerFailure")
#define STATUS_SUCCESS CONSTLIT("success")
#define STATUS_FAILURE CONSTLIT("failure")
#define STR_A_REASON CONSTLIT("aReason")
CMission::CMission (void) : CSpaceObject(&g_MissionClass)
// CMission constructor
{
}
void CMission::CloseMission (void)
// CloseMission
//
// Closes a mission
{
// Remove all subscriptions
CSystem *pSystem = g_pUniverse->GetCurrentSystem();
if (pSystem)
RemoveAllEventSubscriptions(pSystem);
// Cancel all timer events
g_pUniverse->CancelEvent(this);
// If this is a player mission then refresh another player mission
if (m_fAcceptedByPlayer)
g_pUniverse->RefreshCurrentMission();
}
void CMission::CompleteMission (ECompletedReasons iReason)
// CompleteMission
//
// Complete the mission
{
if (IsCompleted())
return;
bool bIsPlayerMission = (m_iStatus == statusAccepted);
m_dwCompletedOn = g_pUniverse->GetTicks();
// Handle player missions differently
if (bIsPlayerMission)
{
// Mission failure
if (iReason == completeFailure || iReason == completeDestroyed)
{
m_iStatus = statusPlayerFailure;
// Tell the player that we failed
CSpaceObject *pPlayer = g_pUniverse->GetPlayerShip();
if (pPlayer)
{
CString sMessage;
if (!Translate(CONSTLIT("FailureMsg"), NULL, &sMessage))
sMessage = CONSTLIT("Mission failed!");
pPlayer->SendMessage(NULL, sMessage);
}
// Set the player target (mission usually sets the target back to the
// station that gave the mission).
FireOnSetPlayerTarget(REASON_FAILURE);
// Let the player record the mission failure
pPlayer->OnMissionCompleted(this, false);
}
// Mission success
else if (iReason == completeSuccess)
{
m_iStatus = statusPlayerSuccess;
// Tell the player that we succeeded
CSpaceObject *pPlayer = g_pUniverse->GetPlayerShip();
if (pPlayer)
{
CString sMessage;
if (!Translate(CONSTLIT("SuccessMsg"), NULL, &sMessage))
sMessage = CONSTLIT("Mission complete!");
pPlayer->SendMessage(NULL, sMessage);
}
// Set the player target (mission usually sets the target back to the
// station that gave the mission).
FireOnSetPlayerTarget(REASON_SUCCESS);
// Let the player record the mission success
if (pPlayer)
pPlayer->OnMissionCompleted(this, true);
}
// If there is no debrief, then we close the mission
if (!m_pType->HasDebrief())
{
m_fDebriefed = true;
FireOnSetPlayerTarget(REASON_DEBRIEFED);
CloseMission();
}
}
// Set status for non-player missions
else
{
if (iReason == completeFailure || iReason == completeDestroyed)
m_iStatus = statusFailure;
else if (iReason == completeSuccess)
m_iStatus = statusSuccess;
// For non-player missions we can close now. (For players we wait until
// debrief.)
CloseMission();
}
}
ALERROR CMission::Create (CMissionType *pType,
CSpaceObject *pOwner,
ICCItem *pCreateData,
CMission **retpMission,
CString *retsError)
// Create
//
// Creates a new mission object. We return ERR_NOTFOUND if the mission could
// not be created because conditions do not allow it.
{
CMission *pMission;
// If we cannot encounter this mission, then we fail
if (!pType->CanBeCreated(pOwner, pCreateData))
return ERR_NOTFOUND;
// Create the new object
pMission = new CMission;
if (pMission == NULL)
{
*retsError = CONSTLIT("Out of memory");
return ERR_MEMORY;
}
pMission->m_pType = pType;
// Initialize
pMission->m_iStatus = statusOpen;
pMission->m_fIntroShown = false;
pMission->m_fDeclined = false;
pMission->m_fDebriefed = false;
pMission->m_fAcceptedByPlayer = false;
pMission->m_pOwner = pOwner;
pMission->m_pDebriefer = NULL;
// NodeID
CTopologyNode *pNode = NULL;
CSystem *pSystem = NULL;
if ((pSystem = (pOwner ? pOwner->GetSystem() : g_pUniverse->GetCurrentSystem()))
&& (pNode = pSystem->GetTopology()))
pMission->m_sNodeID = pNode->GetID();
pMission->m_dwCreatedOn = g_pUniverse->GetTicks();
pMission->m_fInMissionSystem = true;
pMission->m_dwAcceptedOn = 0;
pMission->m_dwLeftSystemOn = 0;
pMission->m_dwCompletedOn = 0;
// Set flags so we know which events we have
pMission->SetEventFlags();
// Fire OnCreate
pMission->m_fInOnCreate = true;
CSpaceObject::SOnCreate OnCreate;
OnCreate.pData = pCreateData;
OnCreate.pOwnerObj = pOwner;
pMission->FireOnCreate(OnCreate);
pMission->m_fInOnCreate = false;
// If OnCreate destroyed the object then it means that the mission was not
// suitable. We return ERR_NOTFOUND
if (pMission->IsDestroyed())
{
delete pMission;
return ERR_NOTFOUND;
}
// Get the mission title and description (we remember these because we may
// need to access them outside of the system).
pMission->RefreshSummary();
// If we haven't subscribed to the owner, do it now
if (pOwner && !pOwner->FindEventSubscriber(pMission))
pOwner->AddEventSubscriber(pMission);
// Mission created
pMission->m_pType->OnMissionCreated();
// Done
if (retpMission)
*retpMission = pMission;
return NOERROR;
}
void CMission::FireCustomEvent (const CString &sEvent, ICCItem *pData)
// FireCustomEvent
//
// Fires a custom timed event
{
SEventHandlerDesc Event;
if (FindEventHandler(sEvent, &Event))
{
CCodeChainCtx Ctx;
Ctx.SetEvent(eventDoEvent);
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
Ctx.SaveAndDefineDataVar(pData);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(sEvent, pResult);
Ctx.Discard(pResult);
}
}
void CMission::FireOnAccepted (void)
// FireOnAccepted
//
// Fire <OnAccepted>
{
SEventHandlerDesc Event;
if (FindEventHandler(EVENT_ON_ACCEPTED, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(EVENT_ON_ACCEPTED, pResult);
Ctx.Discard(pResult);
}
}
ICCItem *CMission::FireOnDeclined (void)
// FireOnDeclined
//
// Fire <OnDeclined>. We return the result of the event, which might contain
// instructions for the mission screen.
//
// Callers are responsible for discarding the result, if not NULL.
{
SEventHandlerDesc Event;
if (FindEventHandler(EVENT_ON_DECLINED, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
{
ReportEventError(EVENT_ON_DECLINED, pResult);
Ctx.Discard(pResult);
return NULL;
}
return pResult;
}
return NULL;
}
ICCItem *CMission::FireOnReward (ICCItem *pData)
// FireOnReward
//
// Fire <OnReward>
//
// Callers are responsible for discarding the result, if not NULL.
{
SEventHandlerDesc Event;
if (FindEventHandler(EVENT_ON_REWARD, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
Ctx.SaveAndDefineDataVar(pData);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
{
ReportEventError(EVENT_ON_REWARD, pResult);
Ctx.Discard(pResult);
return NULL;
}
return pResult;
}
return NULL;
}
void CMission::FireOnSetPlayerTarget (const CString &sReason)
// FireOnSetPlayerTarget
//
// Fire <OnSetPlayerTarget>
{
SEventHandlerDesc Event;
if (FindEventHandler(EVENT_ON_SET_PLAYER_TARGET, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
Ctx.DefineString(STR_A_REASON, sReason);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(EVENT_ON_SET_PLAYER_TARGET, pResult);
Ctx.Discard(pResult);
}
}
void CMission::FireOnStart (void)
// FireOnStart
//
// Fire <OnStarted>
{
SEventHandlerDesc Event;
if (FindEventHandler(EVENT_ON_STARTED, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(EVENT_ON_STARTED, pResult);
Ctx.Discard(pResult);
}
}
void CMission::FireOnStop (const CString &sReason, ICCItem *pData)
// FireOnStop
//
// Fire <OnCompleted>
{
SEventHandlerDesc Event;
if (FindEventHandler(EVENT_ON_COMPLETED, &Event))
{
CCodeChainCtx Ctx;
Ctx.DefineContainingType(this);
Ctx.SaveAndDefineSourceVar(this);
Ctx.SaveAndDefineDataVar(pData);
Ctx.DefineString(STR_A_REASON, sReason);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
ReportEventError(EVENT_ON_COMPLETED, pResult);
Ctx.Discard(pResult);
}
}
ICCItem *CMission::GetProperty (CCodeChainCtx &Ctx, const CString &sName)
// GetProperty
//
// Returns a property
{
CCodeChain &CC = g_pUniverse->GetCC();
if (strEquals(sName, PROPERTY_ACCEPTED_ON))
return (m_fAcceptedByPlayer ? CC.CreateInteger(m_dwAcceptedOn) : CC.CreateNil());
else if (strEquals(sName, PROPERTY_COMPLETED_ON))
return (IsCompleted() ? CC.CreateInteger(m_dwCompletedOn) : CC.CreateNil());
else if (strEquals(sName, PROPERTY_DEBRIEFER_ID))
{
if (m_pDebriefer.GetID() != OBJID_NULL)
return CC.CreateInteger(m_pDebriefer.GetID());
else if (m_pOwner.GetID() != OBJID_NULL)
return CC.CreateInteger(m_pOwner.GetID());
else
return CC.CreateNil();
}
else if (strEquals(sName, PROPERTY_IN_PROGRESS))
return CC.CreateBool(IsActive() && !IsCompleted());
else if (strEquals(sName, PROPERTY_IS_ACTIVE))
return CC.CreateBool(IsActive());
else if (strEquals(sName, PROPERTY_IS_COMPLETED))
return CC.CreateBool(IsCompleted());
else if (strEquals(sName, PROPERTY_IS_DEBRIEFED))
return CC.CreateBool(m_fDebriefed);
else if (strEquals(sName, PROPERTY_IS_DECLINED))
return CC.CreateBool(m_fDeclined);
else if (strEquals(sName, PROPERTY_IS_FAILURE))
return CC.CreateBool(IsFailure());
else if (strEquals(sName, PROPERTY_IS_INTRO_SHOWN))
return CC.CreateBool(m_fIntroShown);
else if (strEquals(sName, PROPERTY_IS_OPEN))
return CC.CreateBool(m_iStatus == statusOpen);
else if (strEquals(sName, PROPERTY_IS_RECORDED))
return CC.CreateBool(IsRecorded());
else if (strEquals(sName, PROPERTY_IS_SUCCESS))
return CC.CreateBool(IsSuccess());
else if (strEquals(sName, PROPERTY_IS_UNAVAILABLE))
return CC.CreateBool(IsUnavailable());
else if (strEquals(sName, PROPERTY_NAME))
return CC.CreateString(m_sTitle);
else if (strEquals(sName, PROPERTY_NODE_ID))
return (m_sNodeID.IsBlank() ? CC.CreateNil() : CC.CreateString(m_sNodeID));
else if (strEquals(sName, PROPERTY_OWNER_ID))
{
if (m_pOwner.GetID() == OBJID_NULL)
return CC.CreateNil();
else
return CC.CreateInteger(m_pOwner.GetID());
}
else if (strEquals(sName, PROPERTY_SUMMARY))
return CC.CreateString(m_sInstructions);
else
return CSpaceObject::GetProperty(Ctx, sName);
}
bool CMission::HasSpecialAttribute (const CString &sAttrib) const
// HasSpecialAttribute
//
// Returns TRUE if object has the special attribute
//
// NOTE: Subclasses may override this, but they must call the
// base class if they do not handle the attribute.
{
if (strStartsWith(sAttrib, SPECIAL_OWNER_ID))
{
DWORD dwOwnerID = strToInt(strSubString(sAttrib, SPECIAL_OWNER_ID.GetLength()), 0);
return (dwOwnerID == m_pOwner.GetID());
}
else
return CSpaceObject::HasSpecialAttribute(sAttrib);
}
bool CMission::MatchesCriteria (CSpaceObject *pSource, const SCriteria &Criteria)
// MatchesCriteria
//
// Returns TRUE if the given mission matches the criteria
{
int i;
// By status
if (!(Criteria.bIncludeActive && IsActive())
&& !(Criteria.bIncludeOpen && IsOpen())
&& !(Criteria.bIncludeRecorded && IsRecorded())
&& !(Criteria.bIncludeCompleted && IsCompleted())
&& !(Criteria.bIncludeUnavailable && IsUnavailable()))
return false;
// Owned by source
if (Criteria.bOnlySourceOwner)
{
if (pSource)
{
if (pSource->GetID() != m_pOwner.GetID())
return false;
}
else
{
if (m_pOwner.GetID() != OBJID_NULL)
return false;
}
}
if (Criteria.bOnlySourceDebriefer)
{
if (m_pDebriefer.GetID() != OBJID_NULL)
{
if (pSource == NULL || pSource->GetID() != m_pDebriefer.GetID())
return false;
}
else
{
if (pSource)
{
if (pSource->GetID() != m_pOwner.GetID())
return false;
}
else
{
if (m_pOwner.GetID() != OBJID_NULL)
return false;
}
}
}
// Check required attributes
for (i = 0; i < Criteria.AttribsRequired.GetCount(); i++)
if (!HasAttribute(Criteria.AttribsRequired[i]))
return false;
// Check attributes not allowed
for (i = 0; i < Criteria.AttribsNotAllowed.GetCount(); i++)
if (HasAttribute(Criteria.AttribsNotAllowed[i]))
return false;
// Check special attribs required
for (i = 0; i < Criteria.SpecialRequired.GetCount(); i++)
if (!HasSpecialAttribute(Criteria.SpecialRequired[i]))
return false;
// Check special attribs not allowed
for (i = 0; i < Criteria.SpecialNotAllowed.GetCount(); i++)
if (HasSpecialAttribute(Criteria.SpecialNotAllowed[i]))
return false;
// Match
return true;
}
void CMission::OnDestroyed (SDestroyCtx &Ctx)
// OnDestroyed
//
// Mission is destroyed
{
DEBUG_TRY
if (m_fInOnCreate)
return;
// Mission destroyed
m_pType->OnMissionDestroyed();
// If the mission is running then we need to stop
if (m_iStatus == statusClosed || m_iStatus == statusAccepted)
{
FireOnStop(REASON_DESTROYED, NULL);
CSpaceObject *pOwner = m_pOwner.GetObj();
if (pOwner)
pOwner->FireOnMissionCompleted(this, REASON_DESTROYED);
}
// Make sure the mission is completed
CompleteMission(completeDestroyed);
// Destroy the mission
FireOnDestroy(Ctx);
DEBUG_CATCH
}
void CMission::OnNewSystem (CSystem *pSystem)
// OnNewSystem
//
// We are in a new system.
//
// NOTE: pSystem can be NULL in some cases, particularly at the end of a game
// (when we're resetting things).
{
// Ignore any closed missions (completed missions)
if (IsClosed())
return;
// Resolve owner
m_pOwner.Resolve();
m_pDebriefer.Resolve();
// Clear any object references (because they might belong to a different
// system).
ClearObjReferences();
// Keep track to see if we're leaving the mission system.
CTopologyNode *pNode;
if (!m_sNodeID.IsBlank()
&& pSystem
&& (pNode = pSystem->GetTopology()))
{
if (strEquals(m_sNodeID, pNode->GetID()))
{
// Back in our system
m_fInMissionSystem = true;
m_dwLeftSystemOn = 0;
}
else
{
// If mission fails when we leave the system, fail now
if (m_pType->GetOutOfSystemTimeOut() == 0
&& IsAccepted())
SetFailure(NULL);
// If required, close the mission
if (!m_fDebriefed
&& IsCompleted()
&& m_pType->CloseIfOutOfSystem())
{
m_fDebriefed = true;
FireOnSetPlayerTarget(REASON_DEBRIEFED);
CloseMission();
return;
}
// Left the system. If we used to be in our system, then keep
// track of when we left.
if (m_fInMissionSystem)
m_dwLeftSystemOn = g_pUniverse->GetTicks();
m_fInMissionSystem = false;
}
}
}
void CMission::OnObjDestroyedNotify (SDestroyCtx &Ctx)
// OnObjDestroyedNotify
//
// An object that we subscribe to has been destroyed
{
// Fire events
FireOnObjDestroyed(Ctx);
// If this object was ascended, then we don't need to do anything else
// (since the pointer is still valid).
if (Ctx.iCause == ascended)
return;
// If this is the owner then the mission fails
if (Ctx.pObj->GetID() == m_pOwner.GetID())
{
// Mission fails
if (m_pType->FailureWhenOwnerDestroyed())
{
SetFailure(NULL);
// Since the owner is dead, we do not require a debrief
if (IsActive())
{
m_fDebriefed = true;
FireOnSetPlayerTarget(REASON_DEBRIEFED);
CloseMission();
}
}
// Clear out owner pointer (unless we left a wreck)
if (Ctx.pWreck == NULL)
m_pOwner.CleanUp();
else if (Ctx.pWreck->GetID() != m_pOwner.GetID())
m_pOwner = Ctx.pWreck;
}
if (Ctx.pObj->GetID() == m_pDebriefer.GetID())
{
// If our debriefer has been destroyed, then remove it.
// (But only if it didn't leave a wreck).
if (Ctx.pWreck == NULL)
m_pDebriefer.CleanUp();
else if (Ctx.pWreck->GetID() != m_pDebriefer.GetID())
m_pDebriefer = Ctx.pWreck;
}
}
void CMission::OnPlayerEnteredSystem (CSpaceObject *pPlayer)
// OnPlayerEnteredSystem
//
// Player has entered the system
{
// Fire event
FireOnPlayerEnteredSystem(pPlayer);
// For active missions, fire event to reset player targets.
if (IsPlayerMission() && IsActive())
FireOnSetPlayerTarget(REASON_NEW_SYSTEM);
}
void CMission::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Read from stream
//
// DWORD Mission type UNID
// DWORD Mission status
// CGlobalSpaceObject m_pOwner
// CGlobalSpaceObject m_pDebriefer
// CString m_sNodeID
// DWORD m_dwCreatedOn
// DWORD m_dwLeftSystemOn
// DWORD m_dwAcceptedOn
// DWORD m_dwCompletedOn
// CString m_sTitle
// CString m_sInstructions
// DWORD Flags
{
DWORD dwLoad;
Ctx.pStream->Read(dwLoad);
m_pType = CMissionType::AsType(g_pUniverse->FindDesignType(dwLoad));
if (m_pType == NULL)
throw CException(ERR_FAIL, strPatternSubst(CONSTLIT("Undefined mission type: %08x"), dwLoad));
Ctx.pStream->Read(dwLoad);
m_iStatus = (EStatus)dwLoad;
m_pOwner.ReadFromStream(Ctx);
if (Ctx.dwVersion >= 89)
m_pDebriefer.ReadFromStream(Ctx);
m_sNodeID.ReadFromStream(Ctx.pStream);
if (Ctx.dwVersion >= 85)
Ctx.pStream->Read(m_dwCreatedOn);
else
m_dwCreatedOn = 0;
if (Ctx.dwVersion >= 84)
Ctx.pStream->Read(m_dwLeftSystemOn);
else
m_dwLeftSystemOn = 0;
if (Ctx.dwVersion >= 86)
Ctx.pStream->Read(m_dwAcceptedOn);
else
m_dwAcceptedOn = 0;
if (Ctx.dwVersion >= 165)
Ctx.pStream->Read(m_dwCompletedOn);
else
m_dwCompletedOn = 0;
if (Ctx.dwVersion >= 86)
{
m_sTitle.ReadFromStream(Ctx.pStream);
m_sInstructions.ReadFromStream(Ctx.pStream);
}
// Flags
Ctx.pStream->Read(dwLoad);
m_fIntroShown = ((dwLoad & 0x00000001) ? true : false);
m_fDeclined = ((dwLoad & 0x00000002) ? true : false);
m_fDebriefed = ((dwLoad & 0x00000004) ? true : false);
m_fInMissionSystem = ((dwLoad & 0x00000008) ? true : false);
m_fAcceptedByPlayer = ((dwLoad & 0x00000010) ? true : false);
m_fInOnCreate = false;
// For backwards compatibility
if (Ctx.dwVersion < 84)
m_fInMissionSystem = true;
// If this is a closed non-player mission, then we set the destroyed flag.
// These missions should not be saved, but previous versions did
// accidentally save them.
if (IsCompletedNonPlayer())
SetDestroyed();
#ifdef DEBUG
SetEventFlags();
#endif
}
void CMission::OnUpdate (SUpdateCtx &Ctx, Metric rSecondsPerTick)
// OnUpdate
//
// Active missions update every tick.
{
DEBUG_TRY
ASSERT(IsActive());
// If we're out of the system then see if we've failed the mission.
// NOTE: It is OK to leave the system if we've completed the mission
// but not yet been debriefed.
int iTimeout;
if (!m_fInMissionSystem
&& IsAccepted()
&& (iTimeout = m_pType->GetOutOfSystemTimeOut()) != -1)
{
if (m_dwLeftSystemOn + iTimeout < (DWORD)g_pUniverse->GetTicks())
{
SetFailure(NULL);
// If required, close the mission
if (!m_fDebriefed && m_pType->CloseIfOutOfSystem())
{
m_fDebriefed = true;
FireOnSetPlayerTarget(REASON_DEBRIEFED);
CloseMission();
}
}
}
DEBUG_CATCH
}
void CMission::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write to stream
//
// DWORD Mission type UNID
// DWORD Mission status
// DWORD Player status
// CGlobalSpaceObject m_pOwner
// CGlobalSpaceObject m_pDebriefer
// CString m_sNodeID
// DWORD m_dwCreatedOn
// DWORD m_dwLeftSystemOn
// DWORD m_dwAcceptedOn
// DWORD m_dwCompletedOn
// CString m_sTitle
// CString m_sInstructions
// DWORD Flags
{
DWORD dwSave;
pStream->Write(m_pType->GetUNID());
pStream->Write((DWORD)m_iStatus);
m_pOwner.WriteToStream(pStream);
m_pDebriefer.WriteToStream(pStream);
m_sNodeID.WriteToStream(pStream);
pStream->Write(m_dwCreatedOn);
pStream->Write(m_dwLeftSystemOn);
pStream->Write(m_dwAcceptedOn);