GameQuest.java 8.37 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
package emu.grasscutter.game.quest;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
5
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
7
8
import emu.grasscutter.data.binout.MainQuestData;
import emu.grasscutter.data.binout.MainQuestData.SubQuestData;
Akka's avatar
Akka committed
9
import emu.grasscutter.data.excels.ChapterData;
Melledy's avatar
Melledy committed
10
11
import emu.grasscutter.data.excels.QuestData;
import emu.grasscutter.data.excels.QuestData.QuestCondition;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.player.Player;
13
import emu.grasscutter.game.quest.enums.LogicType;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.game.quest.enums.QuestState;
Akka's avatar
Akka committed
15
16
import emu.grasscutter.game.quest.enums.QuestTrigger;
import emu.grasscutter.net.proto.ChapterStateOuterClass;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.net.proto.QuestOuterClass.Quest;
Akka's avatar
Akka committed
18
import emu.grasscutter.server.packet.send.PacketChapterStateNotify;
19
import emu.grasscutter.server.packet.send.PacketQuestListUpdateNotify;
Melledy's avatar
Melledy committed
20
21
22
23
24
import emu.grasscutter.server.packet.send.PacketQuestProgressUpdateNotify;
import emu.grasscutter.utils.Utils;

@Entity
public class GameQuest {
github-actions's avatar
github-actions committed
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
    @Transient private GameMainQuest mainQuest;
    @Transient private QuestData questData;

    private int questId;
    private int mainQuestId;
    private QuestState state;

    private int startTime;
    private int acceptTime;
    private int finishTime;

    private int[] finishProgressList;
    private int[] failProgressList;

    @Deprecated // Morphia only. Do not use.
    public GameQuest() {}

    public GameQuest(GameMainQuest mainQuest, QuestData questData) {
        this.mainQuest = mainQuest;
        this.questId = questData.getId();
        this.mainQuestId = questData.getMainId();
        this.questData = questData;
        this.acceptTime = Utils.getCurrentSeconds();
        this.startTime = this.acceptTime;
        this.state = QuestState.QUEST_STATE_UNFINISHED;

        if (questData.getFinishCond() != null && questData.getAcceptCond().size() != 0) {
            this.finishProgressList = new int[questData.getFinishCond().size()];
        }
54

github-actions's avatar
github-actions committed
55
56
57
        if (questData.getFailCond() != null && questData.getFailCond().size() != 0) {
            this.failProgressList = new int[questData.getFailCond().size()];
        }
Akka's avatar
Akka committed
58

github-actions's avatar
github-actions committed
59
        this.mainQuest.getChildQuests().put(this.questId, this);
Akka's avatar
Akka committed
60

61
        this.getData().getBeginExec().forEach(e -> getOwner().getServer().getQuestSystem().triggerExec(this, e, e.getParam()));
Akka's avatar
Akka committed
62
63
64

        this.getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_CONTENT_QUEST_STATE_EQUAL, this.questId, this.state.getValue());

github-actions's avatar
github-actions committed
65
        if (ChapterData.beginQuestChapterMap.containsKey(questId)) {
Akka's avatar
Akka committed
66
67
68
69
70
71
72
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
                ChapterData.beginQuestChapterMap.get(questId).getId(),
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_BEGIN
            ));
        }

        Grasscutter.getLogger().debug("Quest {} is started", questId);
github-actions's avatar
github-actions committed
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
    }

    public GameMainQuest getMainQuest() {
        return mainQuest;
    }

    public void setMainQuest(GameMainQuest mainQuest) {
        this.mainQuest = mainQuest;
    }

    public Player getOwner() {
        return getMainQuest().getOwner();
    }

    public int getQuestId() {
        return questId;
    }

    public int getMainQuestId() {
        return mainQuestId;
    }

    public QuestData getData() {
        return questData;
    }

    public void setConfig(QuestData config) {
        if (this.getQuestId() != config.getId()) return;
        this.questData = config;
    }

    public QuestState getState() {
        return state;
    }

    public void setState(QuestState state) {
        this.state = state;
    }

    public int getStartTime() {
        return startTime;
    }

    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }

    public int getAcceptTime() {
        return acceptTime;
    }

    public void setAcceptTime(int acceptTime) {
        this.acceptTime = acceptTime;
    }

    public int getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(int finishTime) {
        this.finishTime = finishTime;
    }

    public int[] getFinishProgressList() {
        return finishProgressList;
    }

    public void setFinishProgress(int index, int value) {
        finishProgressList[index] = value;
    }

    public int[] getFailProgressList() {
        return failProgressList;
    }

    public void setFailProgress(int index, int value) {
        failProgressList[index] = value;
    }

    public void finish() {
        this.state = QuestState.QUEST_STATE_FINISHED;
        this.finishTime = Utils.getCurrentSeconds();

        if (this.getFinishProgressList() != null) {
            for (int i = 0 ; i < getFinishProgressList().length; i++) {
                getFinishProgressList()[i] = 1;
            }
        }

        this.getOwner().getSession().send(new PacketQuestProgressUpdateNotify(this));
        this.getOwner().getSession().send(new PacketQuestListUpdateNotify(this));

        if (this.getData().finishParent()) {
            // This quest finishes the questline - the main quest will also save the quest to db so we dont have to call save() here
            this.getMainQuest().finish();
        } else {
            // Try and accept other quests if possible
            this.tryAcceptQuestLine();
            this.save();
        }
Akka's avatar
Akka committed
173

174
        this.getData().getFinishExec().forEach(e -> getOwner().getServer().getQuestSystem().triggerExec(this, e, e.getParam()));
Akka's avatar
Akka committed
175
176
177

        this.getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_CONTENT_QUEST_STATE_EQUAL, this.questId, this.state.getValue());

github-actions's avatar
github-actions committed
178
        if (ChapterData.endQuestChapterMap.containsKey(questId)) {
Akka's avatar
Akka committed
179
180
181
182
183
184
185
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
                ChapterData.endQuestChapterMap.get(questId).getId(),
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_END
            ));
        }

        Grasscutter.getLogger().debug("Quest {} is finished", questId);
github-actions's avatar
github-actions committed
186
    }
Akka's avatar
Akka committed
187

github-actions's avatar
github-actions committed
188
189
190
    public boolean tryAcceptQuestLine() {
        try {
            MainQuestData questConfig = GameData.getMainQuestDataMap().get(this.getMainQuestId());
191

github-actions's avatar
github-actions committed
192
193
            for (SubQuestData subQuest : questConfig.getSubQuests()) {
                GameQuest quest = getMainQuest().getChildQuestById(subQuest.getSubId());
194

github-actions's avatar
github-actions committed
195
196
                if (quest == null) {
                    QuestData questData = GameData.getQuestDataMap().get(subQuest.getSubId());
197

github-actions's avatar
github-actions committed
198
199
200
201
                    if (questData == null || questData.getAcceptCond() == null
                            || questData.getAcceptCond().size() == 0) {
                        continue;
                    }
202

github-actions's avatar
github-actions committed
203
                    int[] accept = new int[questData.getAcceptCond().size()];
204

github-actions's avatar
github-actions committed
205
206
207
208
                    // TODO
                    for (int i = 0; i < questData.getAcceptCond().size(); i++) {
                        QuestCondition condition = questData.getAcceptCond().get(i);
                        boolean result = getOwner().getServer().getQuestSystem().triggerCondition(this, condition,
Akka's avatar
Akka committed
209
                                condition.getParamStr(),
github-actions's avatar
github-actions committed
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
                                condition.getParam());

                        accept[i] = result ? 1 : 0;
                    }

                    boolean shouldAccept = LogicType.calculate(questData.getAcceptCondComb(), accept);

                    if (shouldAccept) {
                        this.getOwner().getQuestManager().addQuest(questData.getId());
                    }
                }
            }
        } catch (Exception e) {
            Grasscutter.getLogger().error("An error occurred while trying to accept quest.", e);
        }

        return false;
    }

    public void save() {
        getMainQuest().save();
    }

    public Quest toProto() {
        Quest.Builder proto = Quest.newBuilder()
                .setQuestId(this.getQuestId())
                .setState(this.getState().getValue())
                .setParentQuestId(this.getMainQuestId())
                .setStartTime(this.getStartTime())
                .setStartGameTime(438)
                .setAcceptTime(this.getAcceptTime());

        if (this.getFinishProgressList() != null) {
            for (int i : this.getFinishProgressList()) {
                proto.addFinishProgressList(i);
            }
        }

        if (this.getFailProgressList() != null) {
            for (int i : this.getFailProgressList()) {
                proto.addFailProgressList(i);
            }
        }

        return proto.build();
    }
Melledy's avatar
Melledy committed
256
}