-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathQuestManager.cs
More file actions
4136 lines (3641 loc) · 195 KB
/
Copy pathQuestManager.cs
File metadata and controls
4136 lines (3641 loc) · 195 KB
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
using Arrowgene.Ddon.GameServer.Quests;
using Arrowgene.Ddon.GameServer.Scripting.Interfaces;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Shared;
using Arrowgene.Ddon.Shared.Entity.Structure;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Ddon.Shared.Model.Quest;
using Arrowgene.Logging;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
namespace Arrowgene.Ddon.GameServer.Characters
{
public class QuestManager
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(QuestManager));
private QuestManager()
{
}
/**
* @note gQuests contains a map of <QuestScheduleId:QuestId>.
* @note gVarientQuests maps QuestId:HashSet<QuestScheduleId>.
*
* A QuestScheduleId should always get us back to a unique quest object.
* A QuestId can return us a list of related QuestScheduleIds which all use the same QuestId.
*/
private static Dictionary<uint, Quest> gQuests = new();
private static Dictionary<QuestId, HashSet<uint>> gVariantQuests = new();
// Prevents two concurrent JSON reloads from interleaving their swaps.
// Readers never acquire this lock; they are safe because the swap replaces
// collection references rather than mutating live collections, so any reader
// that captured a reference before the swap iterates the old, immutable dict.
private static readonly object _reloadLock = new();
private static Dictionary<QuestType, Dictionary<uint, HashSet<uint>>> QuestByStageNo = new();
private static Dictionary<QuestAreaId, HashSet<QuestId>> gWorldQuests = new Dictionary<QuestAreaId, HashSet<QuestId>>();
private static Dictionary<QuestAdventureGuideCategory, HashSet<uint>> gAdventureGuideCategories = new Dictionary<QuestAdventureGuideCategory, HashSet<uint>>();
private static Dictionary<QuestAreaId, Dictionary<uint,uint>> gAreaTrialRanks = new Dictionary<QuestAreaId, Dictionary<uint, uint>>();
private static Dictionary<QuestId, (QuestSubstoryGroupId SubstoryGroupId, uint SeqNo)> gSubstoryLookup = new();
/// <summary>
/// QuestScheduleIds that are requested as part of World Manage Quests from pcaps.
/// We know they can't be found, so don't audibly complain about them.
/// TODO: Remove this when those quests are handled properly.
/// </summary>
private static readonly HashSet<uint> KnownBadQuestScheduleIds = new HashSet<uint>()
{
0, 25077, 43645, 43646, 47734, 47735, 47736, 47737, 47738, 47739, 49692, 77644, 151381, 208640, 233576, 259411, 259412, 287378, 315624
};
private static void AddQuestToCategory(Quest quest)
{
AddQuestToCollections(quest, gVariantQuests, QuestByStageNo, gWorldQuests, gAdventureGuideCategories, gAreaTrialRanks);
}
// Populates the supplied index collections with the quest. Callers pass either the
// live static dicts (initial load / scripted hotload) or freshly allocated locals
// (JSON hotload build-then-swap path).
private static void AddQuestToCollections(
Quest quest,
Dictionary<QuestId, HashSet<uint>> variantQuests,
Dictionary<QuestType, Dictionary<uint, HashSet<uint>>> questByStageNo,
Dictionary<QuestAreaId, HashSet<QuestId>> worldQuests,
Dictionary<QuestAdventureGuideCategory, HashSet<uint>> adventureGuideCategories,
Dictionary<QuestAreaId, Dictionary<uint, uint>> areaTrialRanks)
{
if (!variantQuests.ContainsKey(quest.QuestId))
variantQuests[quest.QuestId] = new HashSet<uint>();
variantQuests[quest.QuestId].Add(quest.QuestScheduleId);
if (quest.QuestType == QuestType.Tutorial || quest.QuestType == QuestType.Substory)
{
uint stageNo = (uint)StageManager.ConvertIdToStageNo(quest.StageId);
if (!questByStageNo.ContainsKey(quest.QuestType))
questByStageNo[quest.QuestType] = new();
var questDict = questByStageNo[quest.QuestType];
if (!questDict.ContainsKey(stageNo))
questDict[stageNo] = new HashSet<uint>();
questDict[stageNo].Add(quest.QuestScheduleId);
}
else if (quest.QuestType == QuestType.World)
{
if (!worldQuests.ContainsKey(quest.QuestAreaId))
worldQuests[quest.QuestAreaId] = new HashSet<QuestId>();
worldQuests[quest.QuestAreaId].Add(quest.QuestId);
}
if (!adventureGuideCategories.ContainsKey(quest.AdventureGuideCategory))
adventureGuideCategories[quest.AdventureGuideCategory] = new HashSet<uint>();
adventureGuideCategories[quest.AdventureGuideCategory].Add(quest.QuestScheduleId);
// Build a ranking list for quests for area trials
// TODO: This should probably be done in quest scripts, but who wants to rewrite 70+ quests?
if (quest.AdventureGuideCategory == QuestAdventureGuideCategory.AreaTrialOrMission)
{
// Search all process states for a CheckAreaRank command rather than assuming
// it is always the very first command - JSON quests and scripted .csx quests
// serialize their process state lists differently, so .First() is unreliable.
var questData = quest.ToCDataQuestList(0);
uint requiredRank = 0;
QuestAreaId areaId = quest.QuestAreaId;
bool found = false;
foreach (var processState in questData.QuestProcessStateList)
{
foreach (var checkCmdGroup in processState.CheckCommandList)
{
foreach (var cmd in checkCmdGroup.ResultCommandList)
{
if (cmd.Command == (ushort)QuestCheckCommand.CheckAreaRank)
{
requiredRank = (uint)cmd.Param02;
if (areaId == QuestAreaId.None)
areaId = (QuestAreaId)cmd.Param01;
found = true;
break;
}
}
if (found) break;
}
if (found) break;
}
if (found)
{
if (!areaTrialRanks.ContainsKey(areaId))
areaTrialRanks[areaId] = new Dictionary<uint, uint>();
areaTrialRanks[areaId][quest.QuestScheduleId] = requiredRank;
}
}
}
public static void LoadScriptedQuest(DdonGameServer server, IQuest questScript)
{
var quest = questScript.GenerateQuest(server);
gQuests[quest.QuestScheduleId] = quest;
if (quest.Enabled)
{
AddQuestToCategory(quest);
}
}
private static void ComputeSubstoryLookups(DdonGameServer server)
{
var substoryMissionMap = server.GameSettings.Get<Dictionary<QuestSubstoryGroupId, Dictionary<uint, List<QuestId>>>>("substory", "SubstoryMissionMap") ??
throw new ResponseErrorException(ErrorCode.ERROR_CODE_SERVER_CONFIG_ERROR);
gSubstoryLookup.Clear();
foreach (var (substoryGroupId, data) in substoryMissionMap)
{
foreach (var (seqNo, questIds) in data)
{
foreach (var questId in questIds)
{
gSubstoryLookup[questId] = (substoryGroupId, seqNo);
}
}
}
server.GameSettings.Set<bool>("substory", "RecomputeSubstoryLookups", false);
}
public static (QuestSubstoryGroupId SubstoryGroupId, uint SeqNo) GetSubstoryQuestProperties(DdonGameServer server, QuestId questId)
{
if (server.GameSettings.Get<bool>("substory", "RecomputeSubstoryLookups"))
{
ComputeSubstoryLookups(server);
}
if (!gSubstoryLookup.ContainsKey(questId))
{
return (QuestSubstoryGroupId.Invalid, 0);
}
return gSubstoryLookup[questId];
}
/// <summary>
/// Called after a substory quest completes. If all quests in the current sequence are done,
/// advances the sequence step (or marks the group complete). Persists to DB if changed.
/// </summary>
public static void AdvanceSubstoryProgress(DdonGameServer server, Character character, QuestId completedQuestId, DbConnection? connectionIn = null)
{
var props = GetSubstoryQuestProperties(server, completedQuestId);
if (props.SubstoryGroupId == QuestSubstoryGroupId.Invalid) return;
var substorySequenceSettings = server.GameSettings.Get<Dictionary<QuestSubstoryGroupId, List<uint>>>("substory", "SubstorySequence");
var substoryMissionMap = server.GameSettings.Get<Dictionary<QuestSubstoryGroupId, Dictionary<uint, List<QuestId>>>>("substory", "SubstoryMissionMap");
if (substorySequenceSettings == null || substoryMissionMap == null) return;
if (!substorySequenceSettings.ContainsKey(props.SubstoryGroupId)) return;
if (!character.SubstoryProgress.ContainsKey(props.SubstoryGroupId))
{
character.SubstoryProgress[props.SubstoryGroupId] = new SubstoryProgress
{
SubstoryGroupId = props.SubstoryGroupId,
SequenceStep = 0,
IsComplete = false
};
}
var progress = character.SubstoryProgress[props.SubstoryGroupId];
if (progress.IsComplete) return;
var sequences = substorySequenceSettings[props.SubstoryGroupId];
if (progress.SequenceStep >= sequences.Count) return;
var currentSeqNo = sequences[progress.SequenceStep];
if (!substoryMissionMap[props.SubstoryGroupId].ContainsKey(currentSeqNo)) return;
// Check if every quest in the current sequence is now completed
var sequenceQuests = substoryMissionMap[props.SubstoryGroupId][currentSeqNo];
bool allDone = sequenceQuests.TrueForAll(qid => character.CompletedQuests.ContainsKey(qid));
if (!allDone) return;
progress.SequenceStep += 1;
if (progress.SequenceStep >= sequences.Count)
{
progress.IsComplete = true;
}
server.Database.UpsertSubstoryProgress(character.CharacterId, progress, connectionIn);
}
public static void LoadQuests(DdonGameServer server)
{
// Iterate over quests generated from json
foreach (var questAsset in server.AssetRepository.QuestAssets.Quests)
{
var quest = GenericQuest.FromAsset(server, questAsset);
gQuests[quest.QuestScheduleId] = quest;
if (quest.Enabled)
{
AddQuestToCategory(quest);
}
}
LoadLightQuests(server);
ComputeSubstoryLookups(server);
}
public static void ReloadJsonQuests(DdonGameServer server)
{
Logger.Info($"Hotloading JSON quests...");
// Build entirely new index collections without touching live state.
// Scripted quests are preserved by copying them from a snapshot of the
// current live collections; JSON quests are discarded and rebuilt from
// the freshly loaded asset data.
var newQuests = new Dictionary<uint, Quest>();
var newVariantQuests = new Dictionary<QuestId, HashSet<uint>>();
var newQuestByStageNo = new Dictionary<QuestType, Dictionary<uint, HashSet<uint>>>();
var newWorldQuests = new Dictionary<QuestAreaId, HashSet<QuestId>>();
var newAdventureGuideCategories = new Dictionary<QuestAdventureGuideCategory, HashSet<uint>>();
var newAreaTrialRanks = new Dictionary<QuestAreaId, Dictionary<uint, uint>>();
// Snapshot current reference so we iterate a stable collection.
var currentQuests = gQuests;
foreach (var (scheduleId, quest) in currentQuests)
{
if (quest.QuestSource == QuestSource.Json)
continue;
newQuests[scheduleId] = quest;
AddQuestToCollections(quest, newVariantQuests, newQuestByStageNo, newWorldQuests, newAdventureGuideCategories, newAreaTrialRanks);
}
foreach (var questAsset in server.AssetRepository.QuestAssets.Quests)
{
var quest = GenericQuest.FromAsset(server, questAsset);
newQuests[quest.QuestScheduleId] = quest;
if (quest.Enabled)
AddQuestToCollections(quest, newVariantQuests, newQuestByStageNo, newWorldQuests, newAdventureGuideCategories, newAreaTrialRanks);
}
// Atomically swap all references under a lock to prevent two concurrent
// reloads from interleaving. Readers never acquire this lock; they are
// safe because swapping the reference (not mutating the dict) means any
// reader that already holds a reference to the old collection will
// finish iterating it without a "Collection was modified" exception.
lock (_reloadLock)
{
gQuests = newQuests;
gVariantQuests = newVariantQuests;
QuestByStageNo = newQuestByStageNo;
gWorldQuests = newWorldQuests;
gAdventureGuideCategories = newAdventureGuideCategories;
gAreaTrialRanks = newAreaTrialRanks;
}
Logger.Info($"JSON quest file reloaded. {server.AssetRepository.QuestAssets.Quests.Count} JSON quests total in memory.");
}
public static void LoadLightQuests(DdonGameServer server)
{
foreach(var quest in server.LightQuestManager.ReadQuests(true))
{
gQuests[quest.QuestScheduleId] = quest;
AddQuestToCategory(quest);
}
}
public static void AddQuests(DdonGameServer server, IEnumerable<Quest> quests)
{
foreach(var quest in quests)
{
gQuests[quest.QuestScheduleId] = quest;
AddQuestToCategory(quest);
}
}
public static HashSet<uint> GetQuestsByType(QuestType type)
{
HashSet<uint> results = new HashSet<uint>();
// TODO: We probably need to optimize this as more quests are added
foreach (var (scheduleId, quest) in gQuests)
{
if (quest.QuestType == type)
{
results.Add(quest.QuestScheduleId);
}
}
return results;
}
public static HashSet<QuestId> GetWorldQuestIdsByAreaId(QuestAreaId areaId)
{
var snapshot = gWorldQuests;
return snapshot.TryGetValue(areaId, out var ids) ? ids : new HashSet<QuestId>();
}
public static Quest GetQuestByBoardId(ulong boardId)
{
uint questId = BoardManager.GetQuestIdFromBoardId(boardId);
return GetQuestByScheduleId(questId);
}
public static HashSet<uint> GetQuestByStageNo(QuestType questType, uint stageNo)
{
var snapshot = QuestByStageNo;
if (!snapshot.TryGetValue(questType, out var byStage))
return new();
return byStage.TryGetValue(stageNo, out var ids) ? ids : new();
}
public static bool IsVariantQuest(QuestId baseQuestId)
{
return gVariantQuests.ContainsKey(baseQuestId);
}
public static Quest GetQuestByScheduleId(uint questScheduleId)
{
var snapshot = gQuests;
if (snapshot.TryGetValue(questScheduleId, out var quest))
return quest;
if (!KnownBadQuestScheduleIds.Contains(questScheduleId) && !IsBoardQuest(questScheduleId))
Logger.Error($"GetQuestByScheduleId: Invalid questScheduleId {questScheduleId}");
return null;
}
public static Quest GetQuestByQuestId(QuestId questId)
{
var questScheduleIds = GetQuestScheduleIdsForQuestId(questId);
if (questScheduleIds.Count > 1)
{
throw new Exception($"The quest {questId} has multiple implementations. Use GetQuestScheduleIdsForQuestId instead.");
}
return questScheduleIds.Count > 0 ? QuestManager.GetQuestByScheduleId(questScheduleIds.ToList()[0]) : null;
}
public static HashSet<Quest> GetQuestsByQuestId(QuestId questId)
{
var questScheduleIds = GetQuestScheduleIdsForQuestId(questId);
return questScheduleIds.Select(x => QuestManager.GetQuestByScheduleId(x))
.Where(x => x is not null)
.ToHashSet();
}
public static HashSet<uint> GetQuestScheduleIdsForQuestId(QuestId questId)
{
var snapshot = gVariantQuests;
return snapshot.TryGetValue(questId, out var ids) ? ids : new HashSet<uint>();
}
public static Quest RollQuestForQuestId(QuestId questId)
{
var quests = GetQuestScheduleIdsForQuestId(questId);
var questScheduleId = quests.ElementAt(Random.Shared.Next(0, quests.Count));
var snapshot = gQuests;
return snapshot.TryGetValue(questScheduleId, out var quest) ? quest : null;
}
public static HashSet<uint> GetQuestsByAdventureGuideCategory(QuestAdventureGuideCategory category)
{
var snapshot = gAdventureGuideCategories;
return snapshot.TryGetValue(category, out var ids) ? ids.ToHashSet() : new();
}
public static bool IsQuestEnabled(uint questScheduleId)
{
var quest = GetQuestByScheduleId(questScheduleId);
return (quest == null) ? false : quest.Enabled;
}
public static QuestStateManager GetQuestStateManager(GameClient client, Quest quest)
{
return quest.IsPersonal ? client.QuestState : client.Party.QuestState;
}
public static QuestStateManager GetQuestStateManager(GameClient client, uint questScheduleId)
{
return GetQuestStateManager(client, QuestManager.GetQuestByScheduleId(questScheduleId));
}
public static bool IsClientAlignedForMainQuestProgress(DdonGameServer server, GameClient client, Quest quest, uint step, DbConnection? connectionIn = null)
{
if (quest.QuestType != QuestType.Main)
{
return true;
}
if (client.Character.HasQuestCompleted(quest.QuestId))
{
return false;
}
var progress = server.Database.GetQuestProgressByScheduleId(client.Character.CommonId, quest.QuestScheduleId, connectionIn);
return progress != null && (quest.SaveWorkAsStep || progress.Step == step);
}
public static HashSet<uint> CollectQuestScheduleIds(GameClient client, StageLayoutId stageId)
{
var questScheduleIds = new HashSet<uint>();
questScheduleIds.UnionWith(client.Party.QuestState.StageQuests(stageId));
if (client.Party.Leader is not null)
{
questScheduleIds.UnionWith(client.Party.Leader.QuestState.StageQuests(stageId));
}
return questScheduleIds;
}
public static void PurgeUnstartedTutorialQuests(GameClient client)
{
var unstartedTutorialQuests = client.QuestState.GetActiveQuestScheduleIds()
.Select(x => QuestManager.GetQuestByScheduleId(x))
.Where(x => x != null && x.QuestType == QuestType.Tutorial)
.Where(x => {
var mgr = QuestManager.GetQuestStateManager(client, x);
return mgr != null && mgr.GetQuestState(x.QuestScheduleId)?.State == QuestProgressState.Unknown;
})
.ToList();
foreach (var quest in unstartedTutorialQuests)
{
var questStateManager = QuestManager.GetQuestStateManager(client, quest);
if (questStateManager != null)
{
questStateManager.RemoveQuest(quest);
}
}
}
public static HashSet<uint> GetActiveQuestScheduleIds(GameClient client, QuestType questType = QuestType.All)
{
var activeQuestScheduleIds = client.Party.QuestState.GetActiveQuestScheduleIds()
.Where(x => (questType == QuestType.All) || QuestManager.GetQuestByScheduleId(x).QuestType == questType)
.ToList()
.ToHashSet();
activeQuestScheduleIds.UnionWith(
client.QuestState.GetActiveQuestScheduleIds()
.Where(x => (questType == QuestType.All) || QuestManager.GetQuestByScheduleId(x).QuestType == questType)
.ToList()
.ToHashSet());
return activeQuestScheduleIds;
}
public static Dictionary<uint,uint> GetAreaTrialRankings(QuestAreaId areaId)
{
if (!gAreaTrialRanks.ContainsKey(areaId))
{
return new();
}
return gAreaTrialRanks[areaId];
}
public static QuestAreaId GetAreaIdForTrial(uint questScheduleId)
{
foreach (var (areaId, rankings) in gAreaTrialRanks)
{
if (rankings.ContainsKey(questScheduleId))
return areaId;
}
return QuestAreaId.None;
}
public static uint GetScheduleId(DdonGameServer server, QuestId questId, uint variantNumber)
{
if (IsDatabaseManaged(questId, out uint baseScheduleId))
{
int bits = 27;
int maxVariant = 2 << bits;
if (variantNumber >= maxVariant)
{
throw new Exception($"Invalid variant number {variantNumber} > {maxVariant} for quest {questId}.");
}
return baseScheduleId + variantNumber;
}
else
{
if (variantNumber >= 128)
{
throw new Exception($"Invalid variant number {variantNumber} > 127 for quest {questId}.");
}
return server.AssetRepository.QuestScheduleIdAsset[questId] + variantNumber;
}
}
public static uint GetVariantIndex(DdonGameServer server, QuestId questId, uint questScheduleId)
{
if (IsDatabaseManaged(questId, out uint baseScheduleId))
{
return QuestScheduleId.GetRotatingVariant(questScheduleId);
}
else
{
return QuestScheduleId.GetVariant(questScheduleId);
}
}
/// <summary>
/// For quests that are managed from the database.
/// These quests have schedule IDs that are defined solely by type + offset, and so accept larger offsets.
/// </summary>
public static bool IsDatabaseManaged(QuestId questId, out uint baseScheduleId)
{
baseScheduleId = 0;
// Light Quests
if (IsBoardQuest(questId))
{
baseScheduleId = QuestScheduleId.GenerateRotatingId(4, 0);
return true;
}
// TODO: Clan Quests, Wild Hunt
return false;
}
public static List<QuestProgressWork> CollectWorkItems(GameClient client, QuestProgressWorkType workType)
{
return client.QuestState.ProgressWork[workType].Concat(client.Party.QuestState.ProgressWork[workType]).ToList();
}
public class LayoutFlag
{
public static CDataQuestLayoutFlagSetInfo Create(uint layoutFlag, uint stageNo, uint groupId)
{
return new CDataQuestLayoutFlagSetInfo()
{
LayoutFlagNo = layoutFlag,
SetInfoList = new List<CDataQuestSetInfo>()
{
new CDataQuestSetInfo()
{
StageNo = stageNo,
GroupId = groupId
}
}
};
}
public static CDataQuestLayoutFlagSetInfo Create(uint layoutFlag, StageInfo stageInfo, uint groupId)
{
return Create(layoutFlag, stageInfo.StageNo, groupId);
}
}
public class AcceptConditions
{
public static CDataQuestOrderConditionParam NoRestriction()
{
return new CDataQuestOrderConditionParam() { Type = 0x0 };
}
public static CDataQuestOrderConditionParam MinimumLevelRestriction(uint level)
{
return new CDataQuestOrderConditionParam() { Type = 0x1, Param01 = (int)level };
}
public static CDataQuestOrderConditionParam MinimumVocationRestriction(JobId jobId, uint level)
{
return new CDataQuestOrderConditionParam() { Type = 0x2, Param01 = (int)jobId, Param02 = (int)level };
}
public static CDataQuestOrderConditionParam Solo()
{
return new CDataQuestOrderConditionParam() { Type = 0x3 };
}
public static CDataQuestOrderConditionParam MainQuestCompletionRestriction(QuestId questId)
{
return new CDataQuestOrderConditionParam() { Type = 0x6, Param01 = (int)questId };
}
public static CDataQuestOrderConditionParam ClearTutorialQuestRestriction(int param01, int param02 = 0)
{
return new CDataQuestOrderConditionParam() { Type = 0x7, Param01 = param01, Param02 = param02 };
}
public static CDataQuestOrderConditionParam ClearTutorialQuestRestriction(QuestId questId, int param02 = 0)
{
return new CDataQuestOrderConditionParam() { Type = 0x7, Param01 = (int)questId, Param02 = param02 };
}
}
public static CDataQuestProcessState CreateQuestProcessState(ushort processNo, ushort sequenceNo, ushort blockNo, List<CDataQuestCommand> resultCommands, List<CDataQuestCommand> checkCommands)
{
return new CDataQuestProcessState()
{
ProcessNo = processNo,
SequenceNo = sequenceNo,
BlockNo = blockNo,
ResultCommandList = resultCommands,
CheckCommandList = QuestManager.CheckCommand.AddCheckCommands(checkCommands)
};
}
public class CheckCommand
{
public static List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> AddCheckCommands(List<CDataQuestCommand> commands)
{
List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> result = new List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand>();
// This struct seems to be a list serialized inside of a list
result.Add(new CDataQuestProcessState.MtTypedArrayCDataQuestCommand());
result[0].ResultCommandList = commands;
return result;
}
public static List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> AddCheckCommands(List<CDataQuestCommand> commands0, List<CDataQuestCommand> commands1)
{
List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> result = new List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand>();
result.Add(new CDataQuestProcessState.MtTypedArrayCDataQuestCommand());
result.Add(new CDataQuestProcessState.MtTypedArrayCDataQuestCommand());
result[0].ResultCommandList = commands0;
result[1].ResultCommandList = commands1;
return result;
}
public static List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> AddCheckCommands(List<List<CDataQuestCommand>> commands)
{
List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> result = new List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand>();
foreach (List<CDataQuestCommand> commandList in commands)
{
var checkCommands = new CDataQuestProcessState.MtTypedArrayCDataQuestCommand();
checkCommands.ResultCommandList = commandList;
result.Add(checkCommands);
}
return result;
}
public static List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> AppendCheckCommand(List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> obj, CDataQuestCommand command)
{
obj[0].ResultCommandList.Add(command);
return obj;
}
public static List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> AppendCheckCommands(List<CDataQuestProcessState.MtTypedArrayCDataQuestCommand> obj, List<CDataQuestCommand> commands)
{
obj[0].ResultCommandList.AddRange(commands);
return obj;
}
/**
* @brief
* @param stageNo
* @param npcId
*/
public static CDataQuestCommand TalkNpc(uint stageNo, NpcId npcId, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.TalkNpc, Param01 = (int)stageNo, Param02 = (int)npcId, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param stageNo
* @param groupNo
* @param setNo
*/
public static CDataQuestCommand DieEnemy(uint stageNo, int groupNo, int setNo, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.DieEnemy, Param01 = (int)stageNo, Param02 = groupNo, Param03 = setNo, Param04 = param04 };
}
/**
* @brief
* @param stageNo
* @param sceNo
*/
public static CDataQuestCommand SceHitIn(uint stageNo, int sceNo, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.SceHitIn, Param01 = (int)stageNo, Param02 = sceNo, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param itemId
* @param itemNum
*/
public static CDataQuestCommand HaveItem(int itemId, int itemNum, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.HaveItem, Param01 = itemId, Param02 = itemNum, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param itemId
* @param itemNum
* @param npcId
* @param msgNo
*/
public static CDataQuestCommand DeliverItem(int itemId, int itemNum, NpcId npcId = NpcId.None, int msgNo = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.DeliverItem, Param01 = itemId, Param02 = itemNum, Param03 = (int)npcId, Param04 = msgNo };
}
/**
* @brief
* @param enemyNameId (NOT enemyId)
* @param enemyLv
* @param enemyNum
*/
public static CDataQuestCommand EmDieLight(int enemyId, int enemyLv, int enemyNum, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.EmDieLight, Param01 = enemyId, Param02 = enemyLv, Param03 = enemyNum, Param04 = param04 };
}
/**
* @brief
* @param questId
* @param flagNo
*/
public static CDataQuestCommand QstFlagOn(int questId, int flagNo, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.QstFlagOn, Param01 = questId, Param02 = flagNo, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param questId
* @param flagNo
*/
public static CDataQuestCommand QstFlagOff(int questId, int flagNo, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.QstFlagOff, Param01 = questId, Param02 = flagNo, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param flagNo
*/
public static CDataQuestCommand MyQstFlagOn(int flagNo, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.MyQstFlagOn, Param01 = flagNo, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param flagNo
*/
public static CDataQuestCommand MyQstFlagOff(int flagNo, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.MyQstFlagOff, Param01 = flagNo, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand Padding00(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.Padding00, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand Padding01(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.Padding01, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand Padding02(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.Padding02, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param stageNo
*/
public static CDataQuestCommand StageNo(uint stageNo, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.StageNo, Param01 = (int)stageNo, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param stageNo
* @param eventNo
*/
public static CDataQuestCommand EventEnd(uint stageNo, int eventNo, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.EventEnd, Param01 = (int)stageNo, Param02 = eventNo, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param stageNo
* @param x
* @param y
* @param z
*/
public static CDataQuestCommand Prt(uint stageNo, int x, int y, int z)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.Prt, Param01 = (int)stageNo, Param02 = x, Param03 = y, Param04 = z };
}
/**
* @brief
* @param minCount
* @param maxCount
*/
public static CDataQuestCommand Clearcount(int minCount, int maxCount, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.Clearcount, Param01 = minCount, Param02 = maxCount, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param flagNo
*/
public static CDataQuestCommand SceFlagOn(int flagNo, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.SceFlagOn, Param01 = flagNo, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param flagNo
*/
public static CDataQuestCommand SceFlagOff(int flagNo, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.SceFlagOff, Param01 = flagNo, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param stageNo
* @param npcId
*/
public static CDataQuestCommand TouchActToNpc(uint stageNo, NpcId npcId, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.TouchActToNpc, Param01 = (int)stageNo, Param02 = (int)npcId, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param npcId
*/
public static CDataQuestCommand OrderDecide(NpcId npcId, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.OrderDecide, Param01 = (int)npcId, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand IsEndCycle(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.IsEndCycle, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand IsInterruptCycle(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.IsInterruptCycle, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand IsFailedCycle(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.IsFailedCycle, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand IsEndResult(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.IsEndResult, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief Used to order a quest from an NPC with multiple talking options.
* @param stageNo
* @param npcId
* @param noOrderGroupSerial
*/
public static CDataQuestCommand NpcTalkAndOrderUi(uint stageNo, NpcId npcId, int noOrderGroupSerial, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.NpcTalkAndOrderUi, Param01 = (int)stageNo, Param02 = (int)npcId, Param03 = noOrderGroupSerial, Param04 = param04 };
}
/**
* @brief Used to order a quest from an NPC with no additional talking options.
* @param stageNo
* @param npcId
* @param noOrderGroupSerial
*/
public static CDataQuestCommand NpcTouchAndOrderUi(uint stageNo, NpcId npcId, int noOrderGroupSerial, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.NpcTouchAndOrderUi, Param01 = (int)stageNo, Param02 = (int)npcId, Param03 = noOrderGroupSerial, Param04 = param04 };
}
/**
* @brief
* @param stageNo
*/
public static CDataQuestCommand StageNoNotEq(uint stageNo, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.StageNoNotEq, Param01 = (int)stageNo, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param warLevel
*/
public static CDataQuestCommand Warlevel(int warLevel, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.Warlevel, Param01 = warLevel, Param02 = param02, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param stageNo
* @param npcId
*/
public static CDataQuestCommand TalkNpcWithoutMarker(uint stageNo, NpcId npcId, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.TalkNpcWithoutMarker, Param01 = (int)stageNo, Param02 = (int)npcId, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param gold
* @param type
*/
public static CDataQuestCommand HaveMoney(int gold, int type, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.HaveMoney, Param01 = gold, Param02 = type, Param03 = param03, Param04 = param04 };
}
/**
* @brief
* @param clearNum
* @param areaId
*/
public static CDataQuestCommand SetQuestClearNum(int clearNum, int areaId, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.SetQuestClearNum, Param01 = clearNum, Param02 = areaId, Param03 = param03, Param04 = param04 };
}
/**
* @brief
*/
public static CDataQuestCommand MakeCraft(int param01 = 0, int param02 = 0, int param03 = 0, int param04 = 0)
{
return new CDataQuestCommand() { Command = (ushort)QuestCheckCommand.MakeCraft, Param01 = param01, Param02 = param02, Param03 = param03, Param04 = param04 };
}