GameQuest.java 5.87 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
9
10
import emu.grasscutter.data.binout.MainQuestData;
import emu.grasscutter.data.binout.MainQuestData.SubQuestData;
import emu.grasscutter.data.excels.QuestData;
import emu.grasscutter.data.excels.QuestData.QuestCondition;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.player.Player;
12
import emu.grasscutter.game.quest.enums.LogicType;
Melledy's avatar
Melledy committed
13
14
import emu.grasscutter.game.quest.enums.QuestState;
import emu.grasscutter.net.proto.QuestOuterClass.Quest;
15
import emu.grasscutter.server.packet.send.PacketCodexDataUpdateNotify;
16
import emu.grasscutter.server.packet.send.PacketQuestListUpdateNotify;
Melledy's avatar
Melledy committed
17
18
19
20
21
22
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
23
	@Transient private QuestData questData;
Melledy's avatar
Melledy committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	
	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() {}
	
Melledy's avatar
Melledy committed
39
	public GameQuest(GameMainQuest mainQuest, QuestData questData) {
Melledy's avatar
Melledy committed
40
		this.mainQuest = mainQuest;
Melledy's avatar
Melledy committed
41
42
43
		this.questId = questData.getId();
		this.mainQuestId = questData.getMainId();
		this.questData = questData;
Melledy's avatar
Melledy committed
44
45
46
47
		this.acceptTime = Utils.getCurrentSeconds();
		this.startTime = this.acceptTime;
		this.state = QuestState.QUEST_STATE_UNFINISHED;
		
48
		if (questData.getFinishCond() != null && questData.getAcceptCond().length != 0) {
Melledy's avatar
Melledy committed
49
			this.finishProgressList = new int[questData.getFinishCond().length];
Melledy's avatar
Melledy committed
50
		}
51
52

		if (questData.getFailCond() != null && questData.getFailCond().length != 0) {
Melledy's avatar
Melledy committed
53
			this.failProgressList = new int[questData.getFailCond().length];
Melledy's avatar
Melledy committed
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
		}
		
		this.mainQuest.getChildQuests().put(this.questId, this);
	}
	
	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;
	}

Melledy's avatar
Melledy committed
79
80
	public QuestData getData() {
		return questData;
Melledy's avatar
Melledy committed
81
82
	}

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

	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));
147
148
		this.getOwner().getSession().send(new PacketQuestListUpdateNotify(this));
		
149
150
151
152
153
154
155
156
		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();
		}
Melledy's avatar
Melledy committed
157
158
	}
	
159
	public boolean tryAcceptQuestLine() {
Melledy's avatar
Melledy committed
160
		try {
Melledy's avatar
Melledy committed
161
			MainQuestData questConfig = GameData.getMainQuestDataMap().get(this.getMainQuestId());
162

Melledy's avatar
Melledy committed
163
164
			for (SubQuestData subQuest : questConfig.getSubQuests()) {
				GameQuest quest = getMainQuest().getChildQuestById(subQuest.getSubId());
165

166
				if (quest == null) {
Melledy's avatar
Melledy committed
167
					QuestData questData = GameData.getQuestDataMap().get(subQuest.getSubId());
168
169
170

					if (questData == null || questData.getAcceptCond() == null
							|| questData.getAcceptCond().length == 0) {
Melledy's avatar
Melledy committed
171
172
						continue;
					}
173

174
					int[] accept = new int[questData.getAcceptCond().length];
175

176
177
178
					// TODO
					for (int i = 0; i < questData.getAcceptCond().length; i++) {
						QuestCondition condition = questData.getAcceptCond()[i];
179
180
181
						boolean result = getOwner().getServer().getQuestHandler().triggerCondition(this, condition,
								condition.getParam());

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

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

187
					if (shouldAccept) {
Melledy's avatar
Melledy committed
188
						this.getOwner().getQuestManager().addQuest(questData.getId());
189
190
					}
				}
Melledy's avatar
Melledy committed
191
192
			}
		} catch (Exception e) {
193
			Grasscutter.getLogger().error("An error occurred while trying to accept quest.", e);
Melledy's avatar
Melledy committed
194
195
196
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
224
225
226
		}

		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();
	}
}