QuestManager.java 6.91 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.quest;

Akka's avatar
Akka committed
3
import java.util.*;
Melledy's avatar
Melledy committed
4
import java.util.function.Consumer;
Akka's avatar
Akka committed
5
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.data.GameData;
Akka's avatar
Akka committed
7
import emu.grasscutter.data.binout.MainQuestData;
Melledy's avatar
Melledy committed
8
9
import emu.grasscutter.data.excels.QuestData;
import emu.grasscutter.data.excels.QuestData.QuestCondition;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.player.BasePlayerManager;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.player.Player;
Akka's avatar
Akka committed
13
import emu.grasscutter.game.quest.enums.ParentQuestState;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.game.quest.enums.QuestTrigger;
15
import emu.grasscutter.game.quest.enums.LogicType;
Melledy's avatar
Melledy committed
16
import emu.grasscutter.game.quest.enums.QuestState;
Akka's avatar
Akka committed
17
import emu.grasscutter.server.packet.send.*;
Melledy's avatar
Melledy committed
18
19
20
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

Melledy's avatar
Melledy committed
21
public class QuestManager extends BasePlayerManager {
github-actions's avatar
github-actions committed
22
23
    private final Int2ObjectMap<GameMainQuest> quests;

hartie95's avatar
hartie95 committed
24
25
26
27
28
    public static long getQuestKey(int mainQuestId){
        QuestEncryptionKey questEncryptionKey = GameData.getMainQuestEncryptionMap().get(mainQuestId);
        return questEncryptionKey != null ? questEncryptionKey.getEncryptionKey() : 0L;
    }

github-actions's avatar
github-actions committed
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
    public QuestManager(Player player) {
        super(player);
        this.quests = new Int2ObjectOpenHashMap<>();
    }

    public Int2ObjectMap<GameMainQuest> getQuests() {
        return quests;
    }

    public GameMainQuest getMainQuestById(int mainQuestId) {
        return getQuests().get(mainQuestId);
    }

    public GameQuest getQuestById(int questId) {
        QuestData questConfig = GameData.getQuestDataMap().get(questId);
        if (questConfig == null) {
            return null;
        }

        GameMainQuest mainQuest = getQuests().get(questConfig.getMainId());

        if (mainQuest == null) {
            return null;
        }

        return mainQuest.getChildQuests().get(questId);
    }

    public void forEachQuest(Consumer<GameQuest> callback) {
        for (GameMainQuest mainQuest : getQuests().values()) {
            for (GameQuest quest : mainQuest.getChildQuests().values()) {
                callback.accept(quest);
            }
        }
    }

    public void forEachMainQuest(Consumer<GameMainQuest> callback) {
        for (GameMainQuest mainQuest : getQuests().values()) {
            callback.accept(mainQuest);
        }
    }

    // TODO
    public void forEachActiveQuest(Consumer<GameQuest> callback) {
        for (GameMainQuest mainQuest : getQuests().values()) {
            for (GameQuest quest : mainQuest.getChildQuests().values()) {
                if (quest.getState() != QuestState.QUEST_STATE_FINISHED) {
                    callback.accept(quest);
                }
            }
        }
    }

    public GameMainQuest addMainQuest(QuestData questConfig) {
        GameMainQuest mainQuest = new GameMainQuest(getPlayer(), questConfig.getMainId());
        getQuests().put(mainQuest.getParentQuestId(), mainQuest);

        getPlayer().sendPacket(new PacketFinishedParentQuestUpdateNotify(mainQuest));

        return mainQuest;
    }

    public GameQuest addQuest(int questId) {
        QuestData questConfig = GameData.getQuestDataMap().get(questId);
        if (questConfig == null) {
            return null;
        }

        // Main quest
        GameMainQuest mainQuest = this.getMainQuestById(questConfig.getMainId());

        // Create main quest if it doesnt exist
        if (mainQuest == null) {
            mainQuest = addMainQuest(questConfig);
        }

        // Sub quest
        GameQuest quest = mainQuest.getChildQuestById(questId);

        if (quest != null) {
            return null;
        }

        // Create
        quest = new GameQuest(mainQuest, questConfig);

        // Save main quest
        mainQuest.save();

        // Send packet
        getPlayer().sendPacket(new PacketQuestListUpdateNotify(quest));

        return quest;
    }
    public void startMainQuest(int mainQuestId) {
Akka's avatar
Akka committed
124
125
        var mainQuestData = GameData.getMainQuestDataMap().get(mainQuestId);

github-actions's avatar
github-actions committed
126
        if (mainQuestData == null) {
Akka's avatar
Akka committed
127
128
129
130
131
132
133
134
135
136
137
138
            return;
        }

        Arrays.stream(mainQuestData.getSubQuests())
            .min(Comparator.comparingInt(MainQuestData.SubQuestData::getOrder))
            .map(MainQuestData.SubQuestData::getSubId)
            .ifPresent(this::addQuest);
    }
    public void triggerEvent(QuestTrigger condType, int... params) {
        triggerEvent(condType, "", params);
    }

github-actions's avatar
github-actions committed
139
    public void triggerEvent(QuestTrigger condType, String paramStr, int... params) {
Akka's avatar
Akka committed
140
        Grasscutter.getLogger().debug("Trigger Event {}, {}, {}", condType, paramStr, params);
github-actions's avatar
github-actions committed
141
        Set<GameQuest> changedQuests = new HashSet<>();
Akka's avatar
Akka committed
142

github-actions's avatar
github-actions committed
143
144
        this.forEachActiveQuest(quest -> {
            QuestData data = quest.getData();
Akka's avatar
Akka committed
145

github-actions's avatar
github-actions committed
146
147
148
149
150
151
            for (int i = 0; i < data.getFinishCond().size(); i++) {
                if (quest.getFinishProgressList() == null
                    || quest.getFinishProgressList().length == 0
                    || quest.getFinishProgressList()[i] == 1) {
                    continue;
                }
Akka's avatar
Akka committed
152

github-actions's avatar
github-actions committed
153
                QuestCondition condition = data.getFinishCond().get(i);
Akka's avatar
Akka committed
154

github-actions's avatar
github-actions committed
155
156
157
                if (condition.getType() != condType) {
                    continue;
                }
Akka's avatar
Akka committed
158

github-actions's avatar
github-actions committed
159
                boolean result = getPlayer().getServer().getQuestSystem().triggerContent(quest, condition, paramStr, params);
Akka's avatar
Akka committed
160

github-actions's avatar
github-actions committed
161
162
                if (result) {
                    quest.getFinishProgressList()[i] = 1;
Akka's avatar
Akka committed
163

github-actions's avatar
github-actions committed
164
165
166
167
                    changedQuests.add(quest);
                }
            }
        });
Akka's avatar
Akka committed
168

github-actions's avatar
github-actions committed
169
170
171
        for (GameQuest quest : changedQuests) {
            LogicType logicType = quest.getData().getFailCondComb();
            int[] progress = quest.getFinishProgressList();
Akka's avatar
Akka committed
172

github-actions's avatar
github-actions committed
173
174
            // Handle logical comb
            boolean finish = LogicType.calculate(logicType, progress);
175

github-actions's avatar
github-actions committed
176
177
178
179
180
181
182
183
184
            // Finish
            if (finish) {
                quest.finish();
            } else {
                getPlayer().sendPacket(new PacketQuestProgressUpdateNotify(quest));
                quest.save();
            }
        }
    }
Melledy's avatar
Melledy committed
185

Akka's avatar
Akka committed
186
187
188
189
190
191
192
193
194
    public List<QuestGroupSuite> getSceneGroupSuite(int sceneId) {
        return getQuests().values().stream()
            .filter(i -> i.getState() != ParentQuestState.PARENT_QUEST_STATE_FINISHED)
            .map(GameMainQuest::getQuestGroupSuites)
            .filter(Objects::nonNull)
            .flatMap(Collection::stream)
            .filter(i -> i.getScene() == sceneId)
            .toList();
    }
github-actions's avatar
github-actions committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    public void loadFromDatabase() {
        List<GameMainQuest> quests = DatabaseHelper.getAllQuests(getPlayer());

        for (GameMainQuest mainQuest : quests) {
            mainQuest.setOwner(this.getPlayer());

            for (GameQuest quest : mainQuest.getChildQuests().values()) {
                quest.setMainQuest(mainQuest);
                quest.setConfig(GameData.getQuestDataMap().get(quest.getQuestId()));
            }

            this.getQuests().put(mainQuest.getParentQuestId(), mainQuest);
        }
    }
Melledy's avatar
Melledy committed
209
}