GameQuest.java 8.79 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;
Akka's avatar
Akka committed
7
import emu.grasscutter.data.excels.ChapterData;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.data.excels.QuestData;
akatatsu27's avatar
akatatsu27 committed
9
import emu.grasscutter.data.excels.TriggerExcelConfigData;
Melledy's avatar
Melledy committed
10
11
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.quest.enums.QuestState;
Akka's avatar
Akka committed
12
13
import emu.grasscutter.game.quest.enums.QuestTrigger;
import emu.grasscutter.net.proto.ChapterStateOuterClass;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.net.proto.QuestOuterClass.Quest;
akatatsu27's avatar
akatatsu27 committed
15
16
import emu.grasscutter.scripts.data.SceneGroup;

Akka's avatar
Akka committed
17
import emu.grasscutter.server.packet.send.PacketChapterStateNotify;
18
import emu.grasscutter.server.packet.send.PacketQuestListUpdateNotify;
Melledy's avatar
Melledy committed
19
20
import emu.grasscutter.server.packet.send.PacketQuestProgressUpdateNotify;
import emu.grasscutter.utils.Utils;
akatatsu27's avatar
akatatsu27 committed
21
22
23
24
25
26
27
28
import lombok.Getter;
import lombok.Setter;

import javax.script.Bindings;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Melledy's avatar
Melledy committed
29
30
31

@Entity
public class GameQuest {
github-actions's avatar
github-actions committed
32
33
    @Transient @Getter @Setter private GameMainQuest mainQuest;
    @Transient @Getter private QuestData questData;
github-actions's avatar
github-actions committed
34

github-actions's avatar
github-actions committed
35
36
37
    @Getter private int subQuestId;
    @Getter private int mainQuestId;
    @Getter @Setter
github-actions's avatar
github-actions committed
38
39
    private QuestState state;

github-actions's avatar
github-actions committed
40
41
42
    @Getter @Setter private int startTime;
    @Getter @Setter private int acceptTime;
    @Getter @Setter private int finishTime;
github-actions's avatar
github-actions committed
43

github-actions's avatar
github-actions committed
44
45
    @Getter private int[] finishProgressList;
    @Getter private int[] failProgressList;
akatatsu27's avatar
akatatsu27 committed
46
47
48
    @Transient @Getter private Map<String, TriggerExcelConfigData> triggerData;
    @Getter private Map<String, Boolean> triggers;
    private transient Bindings bindings;
github-actions's avatar
github-actions committed
49

github-actions's avatar
github-actions committed
50
51
    @Deprecated // Morphia only. Do not use.
    public GameQuest() {}
github-actions's avatar
github-actions committed
52

github-actions's avatar
github-actions committed
53
54
55
56
57
58
    public GameQuest(GameMainQuest mainQuest, QuestData questData) {
        this.mainQuest = mainQuest;
        this.subQuestId = questData.getId();
        this.mainQuestId = questData.getMainId();
        this.questData = questData;
        this.state = QuestState.QUEST_STATE_UNSTARTED;
akatatsu27's avatar
akatatsu27 committed
59
60
61
62
63
        this.triggerData = new HashMap<>();
        this.triggers = new HashMap<>();
    }

    public void start() {
github-actions's avatar
github-actions committed
64
65
66
        this.acceptTime = Utils.getCurrentSeconds();
        this.startTime = this.acceptTime;
        this.state = QuestState.QUEST_STATE_UNFINISHED;
akatatsu27's avatar
akatatsu27 committed
67
68
        List<QuestData.QuestCondition> triggerCond = questData.getFinishCond().stream()
            .filter(p -> p.getType() == QuestTrigger.QUEST_CONTENT_TRIGGER_FIRE).toList();
github-actions's avatar
github-actions committed
69
        if (triggerCond.size() > 0) {
akatatsu27's avatar
akatatsu27 committed
70
71
            for (QuestData.QuestCondition cond : triggerCond) {
                TriggerExcelConfigData newTrigger = GameData.getTriggerExcelConfigDataMap().get(cond.getParam()[0]);
github-actions's avatar
github-actions committed
72
73
                if (newTrigger != null) {
                    if (this.triggerData == null) {
akatatsu27's avatar
akatatsu27 committed
74
75
76
77
78
79
80
81
82
                        this.triggerData = new HashMap<>();
                    }
                    triggerData.put(newTrigger.getTriggerName(), newTrigger);
                    triggers.put(newTrigger.getTriggerName(), false);
                    SceneGroup group = SceneGroup.of(newTrigger.getGroupId()).load(newTrigger.getSceneId());
                    getOwner().getWorld().getSceneById(newTrigger.getSceneId()).loadTriggerFromGroup(group, newTrigger.getTriggerName());
                }
            }
        }
github-actions's avatar
github-actions committed
83

akatatsu27's avatar
akatatsu27 committed
84
        if (questData.getFinishCond() != null && questData.getFinishCond().size() != 0) {
github-actions's avatar
github-actions committed
85
86
            this.finishProgressList = new int[questData.getFinishCond().size()];
        }
87

github-actions's avatar
github-actions committed
88
89
90
        if (questData.getFailCond() != null && questData.getFailCond().size() != 0) {
            this.failProgressList = new int[questData.getFailCond().size()];
        }
Akka's avatar
Akka committed
91

akatatsu27's avatar
akatatsu27 committed
92
        getQuestData().getBeginExec().forEach(e -> getOwner().getServer().getQuestSystem().triggerExec(this, e, e.getParam()));
Akka's avatar
Akka committed
93
94


github-actions's avatar
github-actions committed
95
        if (ChapterData.beginQuestChapterMap.containsKey(subQuestId)) {
Akka's avatar
Akka committed
96
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
akatatsu27's avatar
akatatsu27 committed
97
                ChapterData.beginQuestChapterMap.get(subQuestId).getId(),
Akka's avatar
Akka committed
98
99
100
101
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_BEGIN
            ));
        }

akatatsu27's avatar
akatatsu27 committed
102
103
104
        //Some subQuests and talks become active when some other subQuests are unfinished (even from different MainQuests)
        this.getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_CONTENT_QUEST_STATE_EQUAL, this.getSubQuestId(), this.getState().getValue(),0,0,0);
        this.getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_COND_STATE_EQUAL, this.getSubQuestId(), this.getState().getValue(),0,0,0);
github-actions's avatar
github-actions committed
105

akatatsu27's avatar
akatatsu27 committed
106
        Grasscutter.getLogger().debug("Quest {} is started", subQuestId);
github-actions's avatar
github-actions committed
107
108
    }

akatatsu27's avatar
akatatsu27 committed
109
110
    public String getTriggerNameById(int id) {
        TriggerExcelConfigData trigger = GameData.getTriggerExcelConfigDataMap().get(id);
github-actions's avatar
github-actions committed
111
        if (trigger != null) {
akatatsu27's avatar
akatatsu27 committed
112
113
114
115
116
            String triggerName = trigger.getTriggerName();
            return triggerName;
        }
        //return empty string if can't find trigger
        return "";
github-actions's avatar
github-actions committed
117
118
    }

github-actions's avatar
github-actions committed
119
120
121
    public Player getOwner() {
        return this.getMainQuest().getOwner();
    }
github-actions's avatar
github-actions committed
122

github-actions's avatar
github-actions committed
123
124
125
126
    public void setConfig(QuestData config) {
        if (getSubQuestId() != config.getId()) return;
        this.questData = config;
    }
github-actions's avatar
github-actions committed
127

github-actions's avatar
github-actions committed
128
129
130
    public void setFinishProgress(int index, int value) {
        finishProgressList[index] = value;
    }
github-actions's avatar
github-actions committed
131

github-actions's avatar
github-actions committed
132
133
134
    public void setFailProgress(int index, int value) {
        failProgressList[index] = value;
    }
github-actions's avatar
github-actions committed
135

github-actions's avatar
github-actions committed
136
137
138
    public void finish() {
        this.state = QuestState.QUEST_STATE_FINISHED;
        this.finishTime = Utils.getCurrentSeconds();
github-actions's avatar
github-actions committed
139

github-actions's avatar
github-actions committed
140
141
142
143
        if (getQuestData().finishParent()) {
            // This quest finishes the questline - the main quest will also save the quest to db, so we don't have to call save() here
            getMainQuest().finish();
        }
Akka's avatar
Akka committed
144

akatatsu27's avatar
akatatsu27 committed
145
146
147
148
        getQuestData().getFinishExec().forEach(e -> getOwner().getServer().getQuestSystem().triggerExec(this, e, e.getParam()));
        //Some subQuests have conditions that subQuests are finished (even from different MainQuests)
        getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_CONTENT_QUEST_STATE_EQUAL, this.subQuestId, this.state.getValue(),0,0,0);
        getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_COND_STATE_EQUAL, this.subQuestId, this.state.getValue(),0,0,0);
Akka's avatar
Akka committed
149

github-actions's avatar
github-actions committed
150
        if (ChapterData.endQuestChapterMap.containsKey(subQuestId)) {
Akka's avatar
Akka committed
151
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
akatatsu27's avatar
akatatsu27 committed
152
                ChapterData.endQuestChapterMap.get(subQuestId).getId(),
Akka's avatar
Akka committed
153
154
155
156
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_END
            ));
        }

akatatsu27's avatar
akatatsu27 committed
157
        Grasscutter.getLogger().debug("Quest {} is finished", subQuestId);
github-actions's avatar
github-actions committed
158
    }
github-actions's avatar
github-actions committed
159

akatatsu27's avatar
akatatsu27 committed
160
161
162
163
    //TODO
    public void fail() {
        this.state = QuestState.QUEST_STATE_FAILED;
        this.finishTime = Utils.getCurrentSeconds();
github-actions's avatar
github-actions committed
164

akatatsu27's avatar
akatatsu27 committed
165
166
167
168
        getQuestData().getFailExec().forEach(e -> getOwner().getServer().getQuestSystem().triggerExec(this, e, e.getParam()));
        //Some subQuests have conditions that subQuests fail (even from different MainQuests)
        getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_CONTENT_QUEST_STATE_EQUAL, this.subQuestId, this.state.getValue(),0,0,0);
        getOwner().getQuestManager().triggerEvent(QuestTrigger.QUEST_COND_STATE_EQUAL, this.subQuestId, this.state.getValue(),0,0,0);
github-actions's avatar
github-actions committed
169

akatatsu27's avatar
akatatsu27 committed
170
171
172
173
    }
    // Return true if ParentQuest should rewind to this childQuest
    public boolean rewind(GameQuest nextRewind) {
        if (questData.isRewind()) {
github-actions's avatar
github-actions committed
174
            if (nextRewind == null) {return true;}
akatatsu27's avatar
akatatsu27 committed
175
            // if the next isRewind subQuest is none or unstarted, reset all subQuests with order higher than this one, and restart this quest
github-actions's avatar
github-actions committed
176
            if (nextRewind.getState() == QuestState.QUEST_STATE_NONE|| nextRewind.getState() == QuestState.QUEST_STATE_UNSTARTED) {
akatatsu27's avatar
akatatsu27 committed
177
178
179
                getMainQuest().getChildQuests().values().stream().filter(p -> p.getQuestData().getOrder() > this.getQuestData().getOrder()).forEach(q -> q.setState(QuestState.QUEST_STATE_UNSTARTED));
                this.start();
                return true;
github-actions's avatar
github-actions committed
180
181
            }
        }
akatatsu27's avatar
akatatsu27 committed
182
        return false;
github-actions's avatar
github-actions committed
183
    }
184
    
github-actions's avatar
github-actions committed
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
    public void save() {
        getMainQuest().save();
    }

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

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

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

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