GameQuest.java 7.32 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
25
import emu.grasscutter.server.packet.send.PacketQuestProgressUpdateNotify;
import emu.grasscutter.utils.Utils;

@Entity
public class GameQuest {
	@Transient private GameMainQuest mainQuest;
Melledy's avatar
Melledy committed
26
	@Transient private QuestData questData;
Akka's avatar
Akka committed
27

Melledy's avatar
Melledy committed
28
29
30
	private int questId;
	private int mainQuestId;
	private QuestState state;
Akka's avatar
Akka committed
31

Melledy's avatar
Melledy committed
32
33
34
	private int startTime;
	private int acceptTime;
	private int finishTime;
Akka's avatar
Akka committed
35

Melledy's avatar
Melledy committed
36
37
	private int[] finishProgressList;
	private int[] failProgressList;
Akka's avatar
Akka committed
38

Melledy's avatar
Melledy committed
39
40
	@Deprecated // Morphia only. Do not use.
	public GameQuest() {}
Akka's avatar
Akka committed
41

Melledy's avatar
Melledy committed
42
	public GameQuest(GameMainQuest mainQuest, QuestData questData) {
Melledy's avatar
Melledy committed
43
		this.mainQuest = mainQuest;
Melledy's avatar
Melledy committed
44
45
46
		this.questId = questData.getId();
		this.mainQuestId = questData.getMainId();
		this.questData = questData;
Melledy's avatar
Melledy committed
47
48
49
		this.acceptTime = Utils.getCurrentSeconds();
		this.startTime = this.acceptTime;
		this.state = QuestState.QUEST_STATE_UNFINISHED;
Akka's avatar
Akka committed
50
51
52

		if (questData.getFinishCond() != null && questData.getAcceptCond().size() != 0) {
			this.finishProgressList = new int[questData.getFinishCond().size()];
Melledy's avatar
Melledy committed
53
		}
54

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

Melledy's avatar
Melledy 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
65
66
67
68
69
70
71
72

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

        if (ChapterData.beginQuestChapterMap.containsKey(questId)){
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
                ChapterData.beginQuestChapterMap.get(questId).getId(),
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_BEGIN
            ));
        }

        Grasscutter.getLogger().debug("Quest {} is started", questId);
Melledy's avatar
Melledy committed
73
	}
Akka's avatar
Akka committed
74

Melledy's avatar
Melledy committed
75
76
77
78
79
80
81
	public GameMainQuest getMainQuest() {
		return mainQuest;
	}

	public void setMainQuest(GameMainQuest mainQuest) {
		this.mainQuest = mainQuest;
	}
Akka's avatar
Akka committed
82

Melledy's avatar
Melledy committed
83
84
85
86
87
88
89
90
91
92
93
94
	public Player getOwner() {
		return getMainQuest().getOwner();
	}

	public int getQuestId() {
		return questId;
	}

	public int getMainQuestId() {
		return mainQuestId;
	}

Melledy's avatar
Melledy committed
95
96
	public QuestData getData() {
		return questData;
Melledy's avatar
Melledy committed
97
98
	}

Melledy's avatar
Melledy committed
99
	public void setConfig(QuestData config) {
Melledy's avatar
Melledy committed
100
		if (this.getQuestId() != config.getId()) return;
Melledy's avatar
Melledy committed
101
		this.questData = config;
Melledy's avatar
Melledy committed
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
	}

	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;
	}
Akka's avatar
Akka committed
135

Melledy's avatar
Melledy committed
136
137
138
	public int[] getFinishProgressList() {
		return finishProgressList;
	}
Akka's avatar
Akka committed
139

Melledy's avatar
Melledy committed
140
141
142
143
144
145
146
	public void setFinishProgress(int index, int value) {
		finishProgressList[index] = value;
	}

	public int[] getFailProgressList() {
		return failProgressList;
	}
Akka's avatar
Akka committed
147

Melledy's avatar
Melledy committed
148
149
150
151
152
153
154
	public void setFailProgress(int index, int value) {
		failProgressList[index] = value;
	}

	public void finish() {
		this.state = QuestState.QUEST_STATE_FINISHED;
		this.finishTime = Utils.getCurrentSeconds();
Akka's avatar
Akka committed
155

Melledy's avatar
Melledy committed
156
157
158
159
160
		if (this.getFinishProgressList() != null) {
			for (int i = 0 ; i < getFinishProgressList().length; i++) {
				getFinishProgressList()[i] = 1;
			}
		}
Akka's avatar
Akka committed
161

Melledy's avatar
Melledy committed
162
		this.getOwner().getSession().send(new PacketQuestProgressUpdateNotify(this));
163
		this.getOwner().getSession().send(new PacketQuestListUpdateNotify(this));
Akka's avatar
Akka committed
164

165
166
167
168
169
170
171
172
		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
178
179
180
181
182
183
184
185

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

        if (ChapterData.endQuestChapterMap.containsKey(questId)){
            mainQuest.getOwner().sendPacket(new PacketChapterStateNotify(
                ChapterData.endQuestChapterMap.get(questId).getId(),
                ChapterStateOuterClass.ChapterState.CHAPTER_STATE_END
            ));
        }

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

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

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

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

					if (questData == null || questData.getAcceptCond() == null
Akka's avatar
Akka committed
199
							|| questData.getAcceptCond().size() == 0) {
Melledy's avatar
Melledy committed
200
201
						continue;
					}
202

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

205
					// TODO
Akka's avatar
Akka committed
206
207
					for (int i = 0; i < questData.getAcceptCond().size(); i++) {
						QuestCondition condition = questData.getAcceptCond().get(i);
208
						boolean result = getOwner().getServer().getQuestSystem().triggerCondition(this, condition,
Akka's avatar
Akka committed
209
                                condition.getParamStr(),
210
211
								condition.getParam());

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

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

217
					if (shouldAccept) {
Melledy's avatar
Melledy committed
218
						this.getOwner().getQuestManager().addQuest(questData.getId());
219
220
					}
				}
Melledy's avatar
Melledy committed
221
222
			}
		} catch (Exception e) {
223
			Grasscutter.getLogger().error("An error occurred while trying to accept quest.", e);
Melledy's avatar
Melledy committed
224
225
226
227
		}

		return false;
	}
Akka's avatar
Akka committed
228

Melledy's avatar
Melledy committed
229
230
231
	public void save() {
		getMainQuest().save();
	}
Akka's avatar
Akka committed
232

Melledy's avatar
Melledy committed
233
234
235
236
237
238
239
240
	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());
Akka's avatar
Akka committed
241

Melledy's avatar
Melledy committed
242
243
244
245
246
		if (this.getFinishProgressList() != null) {
			for (int i : this.getFinishProgressList()) {
				proto.addFinishProgressList(i);
			}
		}
Akka's avatar
Akka committed
247

Melledy's avatar
Melledy committed
248
249
250
251
252
		if (this.getFailProgressList() != null) {
			for (int i : this.getFailProgressList()) {
				proto.addFailProgressList(i);
			}
		}
Akka's avatar
Akka committed
253

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