DungeonChallenge.java 4.54 KB
Newer Older
1
2
package emu.grasscutter.game.dungeons;

3
4
import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.def.DungeonData;
5
import emu.grasscutter.game.entity.EntityMonster;
6
7
8
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason;
9
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.scripts.constants.EventType;
11
import emu.grasscutter.scripts.data.SceneGroup;
12
import emu.grasscutter.scripts.data.ScriptArgs;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.server.packet.send.PacketChallengeDataNotify;
14
15
import emu.grasscutter.server.packet.send.PacketDungeonChallengeBeginNotify;
import emu.grasscutter.server.packet.send.PacketDungeonChallengeFinishNotify;
16
17
18
import emu.grasscutter.server.packet.send.PacketGadgetAutoPickDropInfoNotify;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
19

20
21
22
import java.util.ArrayList;
import java.util.List;

23
24
25
26
27
28
public class DungeonChallenge {
	private final Scene scene;
	private final SceneGroup group;
	
	private int challengeIndex;
	private int challengeId;
29
30
	private boolean success;
	private boolean progress;
31
32
33
34
	/**
	 * has more challenge
	 */
	private boolean stage;
35
36
	private int score;
	private int objective = 0;
37
	private IntSet rewardedPlayers;
38

39
	public DungeonChallenge(Scene scene, SceneGroup group, int challengeId, int challengeIndex, int objective) {
40
41
		this.scene = scene;
		this.group = group;
42
43
44
		this.challengeId = challengeId;
		this.challengeIndex = challengeIndex;
		this.objective = objective;
45
		this.setRewardedPlayers(new IntOpenHashSet());
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	}

	public Scene getScene() {
		return scene;
	}

	public SceneGroup getGroup() {
		return group;
	}
	
	public int getChallengeIndex() {
		return challengeIndex;
	}

	public void setChallengeIndex(int challengeIndex) {
		this.challengeIndex = challengeIndex;
	}

	public int getChallengeId() {
		return challengeId;
	}

	public void setChallengeId(int challengeId) {
		this.challengeId = challengeId;
	}

72
73
74
75
76
77
78
79
	public int getObjective() {
		return objective;
	}

	public void setObjective(int objective) {
		this.objective = objective;
	}

80
	public boolean isSuccess() {
81
		return success;
82
83
84
	}

	public void setSuccess(boolean isSuccess) {
85
86
87
88
89
		this.success = isSuccess;
	}
	
	public boolean inProgress() {
		return progress;
90
91
	}

Melledy's avatar
Melledy committed
92
93
94
	public int getScore() {
		return score;
	}
95
96
97
98
99
100
101
102
103

	public boolean isStage() {
		return stage;
	}

	public void setStage(boolean stage) {
		this.stage = stage;
	}

104
105
106
107
108
109
110
111
112
113
114
	public int getTimeLimit() {
		return 600;
	}

	public IntSet getRewardedPlayers() {
		return rewardedPlayers;
	}

	public void setRewardedPlayers(IntSet rewardedPlayers) {
		this.rewardedPlayers = rewardedPlayers;
	}
Melledy's avatar
Melledy committed
115

116
	public void start() {
117
		this.progress = true;
118
119
120
121
		getScene().broadcastPacket(new PacketDungeonChallengeBeginNotify(this));
	}
	
	public void finish() {
122
		this.progress = false;
123
		
124
125
126
		getScene().broadcastPacket(new PacketDungeonChallengeFinishNotify(this));
		
		if (this.isSuccess()) {
127
			// Call success script event
Akka's avatar
Akka committed
128
129
130
			this.getScene().getScriptManager().callEvent(EventType.EVENT_CHALLENGE_SUCCESS,
					// TODO record the time in PARAM2 and used in action
					new ScriptArgs().setParam2(100));
131

132
133
			// Settle
			settle();
134
		} else {
Melledy's avatar
Melledy committed
135
			this.getScene().getScriptManager().callEvent(EventType.EVENT_CHALLENGE_FAIL, null);
136
137
		}
	}
138
139
	
	private void settle() {
140
		getScene().getDungeonSettleObservers().forEach(o -> o.onDungeonSettle(getScene()));
141
142

		if(!stage){
143
			getScene().getScriptManager().callEvent(EventType.EVENT_DUNGEON_SETTLE,
Akka's avatar
Akka committed
144
					new ScriptArgs(this.isSuccess() ? 1 : 0));
145
		}
146
	}
147
148

	public void onMonsterDie(EntityMonster entity) {
Melledy's avatar
Melledy committed
149
150
151
		score = getScore() + 1;
		
		getScene().broadcastPacket(new PacketChallengeDataNotify(this, 1, getScore()));
152
		
153
		if (getScore() >= getObjective() && this.progress) {
154
155
156
157
			this.setSuccess(true);
			finish();
		}
	}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	
	public void getStatueDrops(Player player) {
		DungeonData dungeonData = getScene().getDungeonData();
		if (!isSuccess() || dungeonData == null || dungeonData.getRewardPreview() == null || dungeonData.getRewardPreview().getPreviewItems().length == 0) {
			return;
		}
		
		// Already rewarded
		if (getRewardedPlayers().contains(player.getUid())) {
			return;
		}
		
		List<GameItem> rewards = new ArrayList<>();
		for (ItemParamData param : getScene().getDungeonData().getRewardPreview().getPreviewItems()) {
			rewards.add(new GameItem(param.getId(), Math.max(param.getCount(), 1)));
		}
		
		player.getInventory().addItems(rewards, ActionReason.DungeonStatueDrop);
		player.sendPacket(new PacketGadgetAutoPickDropInfoNotify(rewards));
		
		getRewardedPlayers().add(player.getUid());
	}
180
}