GameQuest.java 8.78 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 {
akatatsu27's avatar
akatatsu27 committed
32
33
	@Transient @Getter @Setter private GameMainQuest mainQuest;
	@Transient @Getter private QuestData questData;
github-actions's avatar
github-actions committed
34

akatatsu27's avatar
akatatsu27 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;

akatatsu27's avatar
akatatsu27 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

akatatsu27's avatar
akatatsu27 committed
44
45
46
47
48
	@Getter private int[] finishProgressList;
	@Getter private int[] failProgressList;
    @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

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

akatatsu27's avatar
akatatsu27 committed
53
54
55
56
57
58
59
60
61
62
63
	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;
        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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        List<QuestData.QuestCondition> triggerCond = questData.getFinishCond().stream()
            .filter(p -> p.getType() == QuestTrigger.QUEST_CONTENT_TRIGGER_FIRE).toList();
        if(triggerCond.size() > 0) {
            for (QuestData.QuestCondition cond : triggerCond) {
                TriggerExcelConfigData newTrigger = GameData.getTriggerExcelConfigDataMap().get(cond.getParam()[0]);
                if(newTrigger != null) {
                    if(this.triggerData == null) {
                        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


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

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

akatatsu27's avatar
akatatsu27 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

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

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

akatatsu27's avatar
akatatsu27 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

akatatsu27's avatar
akatatsu27 committed
140
141
142
		if (getFinishProgressList() != null) {
            Arrays.fill(getFinishProgressList(), 1);
		}
github-actions's avatar
github-actions committed
143

akatatsu27's avatar
akatatsu27 committed
144
		getOwner().getSession().send(new PacketQuestProgressUpdateNotify(this));
github-actions's avatar
github-actions committed
145

Akka's avatar
Akka committed
146

akatatsu27's avatar
akatatsu27 committed
147
148
149
150
		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
151

akatatsu27's avatar
akatatsu27 committed
152
153
154
155
        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
156

akatatsu27's avatar
akatatsu27 committed
157
        if (ChapterData.endQuestChapterMap.containsKey(subQuestId)){
Akka's avatar
Akka committed
158
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
akatatsu27's avatar
akatatsu27 committed
159
                ChapterData.endQuestChapterMap.get(subQuestId).getId(),
Akka's avatar
Akka committed
160
161
162
163
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_END
            ));
        }

akatatsu27's avatar
akatatsu27 committed
164
165
        Grasscutter.getLogger().debug("Quest {} is finished", subQuestId);
	}
github-actions's avatar
github-actions committed
166

akatatsu27's avatar
akatatsu27 committed
167
168
169
170
    //TODO
    public void fail() {
        this.state = QuestState.QUEST_STATE_FAILED;
        this.finishTime = Utils.getCurrentSeconds();
github-actions's avatar
github-actions committed
171

akatatsu27's avatar
akatatsu27 committed
172
173
        if (getFailProgressList() != null) {
            Arrays.fill(getFailProgressList(), 1);
github-actions's avatar
github-actions committed
174
175
        }

akatatsu27's avatar
akatatsu27 committed
176
        getOwner().getSession().send(new PacketQuestProgressUpdateNotify(this));
github-actions's avatar
github-actions committed
177

akatatsu27's avatar
akatatsu27 committed
178
179
180
181
        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
182

akatatsu27's avatar
akatatsu27 committed
183
184
185
186
187
188
189
190
191
192
    }
    // Return true if ParentQuest should rewind to this childQuest
    public boolean rewind(GameQuest nextRewind) {
        if (questData.isRewind()) {
            if(nextRewind == null) {return true;}
            // if the next isRewind subQuest is none or unstarted, reset all subQuests with order higher than this one, and restart this quest
            if(nextRewind.getState() == QuestState.QUEST_STATE_NONE|| nextRewind.getState() == QuestState.QUEST_STATE_UNSTARTED) {
                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
193
194
            }
        }
akatatsu27's avatar
akatatsu27 committed
195
        return false;
github-actions's avatar
github-actions committed
196
    }
akatatsu27's avatar
akatatsu27 committed
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
	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
224
}