DungeonChallenge.java 4.12 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
	
	private int score;
	private int objective = 0;
34
	private IntSet rewardedPlayers;
35

36
37
38
	public DungeonChallenge(Scene scene, SceneGroup group) {
		this.scene = scene;
		this.group = group;
39
		this.setRewardedPlayers(new IntOpenHashSet());
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	}

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

66
67
68
69
70
71
72
73
	public int getObjective() {
		return objective;
	}

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

74
	public boolean isSuccess() {
75
		return success;
76
77
78
	}

	public void setSuccess(boolean isSuccess) {
79
80
81
82
83
		this.success = isSuccess;
	}
	
	public boolean inProgress() {
		return progress;
84
85
	}

Melledy's avatar
Melledy committed
86
87
88
	public int getScore() {
		return score;
	}
89
90
91
92
93
94
95
96
97
98
99
100
	
	public int getTimeLimit() {
		return 600;
	}

	public IntSet getRewardedPlayers() {
		return rewardedPlayers;
	}

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

102
	public void start() {
103
		this.progress = true;
104
105
106
107
		getScene().broadcastPacket(new PacketDungeonChallengeBeginNotify(this));
	}
	
	public void finish() {
108
		this.progress = false;
109
		
110
111
112
		getScene().broadcastPacket(new PacketDungeonChallengeFinishNotify(this));
		
		if (this.isSuccess()) {
113
			// Call success script event
Melledy's avatar
Melledy committed
114
			this.getScene().getScriptManager().callEvent(EventType.EVENT_CHALLENGE_SUCCESS, null);
115
116
117
			
			// Settle
			settle();
118
		} else {
Melledy's avatar
Melledy committed
119
			this.getScene().getScriptManager().callEvent(EventType.EVENT_CHALLENGE_FAIL, null);
120
121
		}
	}
122
123
	
	private void settle() {
124
		getScene().getDungeonSettleObservers().forEach(o -> o.onDungeonSettle(getScene()));
125
126
127
		
		getScene().getScriptManager().callEvent(EventType.EVENT_DUNGEON_SETTLE, new ScriptArgs(this.isSuccess() ? 1 : 0));
	}
128
129

	public void onMonsterDie(EntityMonster entity) {
Melledy's avatar
Melledy committed
130
131
132
		score = getScore() + 1;
		
		getScene().broadcastPacket(new PacketChallengeDataNotify(this, 1, getScore()));
133
		
134
		if (getScore() >= getObjective() && this.progress) {
135
136
137
138
			this.setSuccess(true);
			finish();
		}
	}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	
	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());
	}
161
}