DungeonManager.java 3.73 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.dungeons;

Melledy's avatar
Melledy committed
3
4
5
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
6
7
import emu.grasscutter.data.binout.ScenePointEntry;
import emu.grasscutter.data.excels.DungeonData;
Melledy's avatar
Melledy committed
8
9
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.SceneType;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.quest.enums.QuestTrigger;
Melledy's avatar
Melledy committed
11
12
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.server.game.GameServer;
Melledy's avatar
Melledy committed
14
15
16
import emu.grasscutter.server.packet.send.PacketDungeonEntryInfoRsp;
import emu.grasscutter.server.packet.send.PacketPlayerEnterDungeonRsp;
import emu.grasscutter.utils.Position;
Melledy's avatar
Melledy committed
17

18
19
import java.util.List;

Melledy's avatar
Melledy committed
20
21
public class DungeonManager {
	private final GameServer server;
22
	private static final BasicDungeonSettleListener basicDungeonSettleObserver = new BasicDungeonSettleListener();
Melledy's avatar
Melledy committed
23
24
25
26
27
28
29
	public DungeonManager(GameServer server) {
		this.server = server;
	}

	public GameServer getServer() {
		return server;
	}
Melledy's avatar
Melledy committed
30
31
32
33

	public void getEntryInfo(Player player, int pointId) {
		ScenePointEntry entry = GameData.getScenePointEntryById(player.getScene().getId(), pointId);
		
34
		if (entry == null) {
Melledy's avatar
Melledy committed
35
36
37
38
39
40
41
42
			// Error
			player.sendPacket(new PacketDungeonEntryInfoRsp());
			return;
		}
		
		player.sendPacket(new PacketDungeonEntryInfoRsp(player, entry.getPointData()));
	}

43
	public boolean enterDungeon(Player player, int pointId, int dungeonId) {
Melledy's avatar
Melledy committed
44
45
46
		DungeonData data = GameData.getDungeonDataMap().get(dungeonId);
		
		if (data == null) {
47
			return false;
Melledy's avatar
Melledy committed
48
49
		}
		
Melledy's avatar
Melledy committed
50
51
		Grasscutter.getLogger().info(player.getNickname() + " is trying to enter dungeon " + dungeonId);
		
Melledy's avatar
Melledy committed
52
		int sceneId = data.getSceneId();
53
		player.getScene().setPrevScene(sceneId);
Melledy's avatar
Melledy committed
54
		
Melledy's avatar
Melledy committed
55
		if (player.getWorld().transferPlayerToScene(player, sceneId, data)) {
56
			player.getScene().addDungeonSettleObserver(basicDungeonSettleObserver);
Melledy's avatar
Melledy committed
57
			player.getQuestManager().triggerEvent(QuestTrigger.QUEST_CONTENT_ENTER_DUNGEON, data.getId());
58
		}
Melledy's avatar
Melledy committed
59
		
60
		player.getScene().setPrevScenePoint(pointId);
Melledy's avatar
Melledy committed
61
		player.sendPacket(new PacketPlayerEnterDungeonRsp(pointId, dungeonId));
62
		return true;
Melledy's avatar
Melledy committed
63
	}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

	/**
	 * used in tower dungeons handoff
	 */
	public boolean handoffDungeon(Player player, int dungeonId, List<DungeonSettleListener> dungeonSettleListeners) {
		DungeonData data = GameData.getDungeonDataMap().get(dungeonId);

		if (data == null) {
			return false;
		}
		Grasscutter.getLogger().info(player.getNickname() + " is trying to enter tower dungeon " + dungeonId);

		if(player.getWorld().transferPlayerToScene(player, data.getSceneId(), data)){
			dungeonSettleListeners.forEach(player.getScene()::addDungeonSettleObserver);
		}
		return true;
	}

Melledy's avatar
Melledy committed
82
83
84
85
86
87
88
89
90
91
92
93
94
	public void exitDungeon(Player player) {
		if (player.getScene().getSceneType() != SceneType.SCENE_DUNGEON) {
			return;
		}
		
		// Get previous scene
		int prevScene = player.getScene().getPrevScene() > 0 ? player.getScene().getPrevScene() : 3;
		
		// Get previous position
		DungeonData dungeonData = player.getScene().getDungeonData();
		Position prevPos = new Position(GameConstants.START_POSITION);
		
		if (dungeonData != null) {
95
			ScenePointEntry entry = GameData.getScenePointEntryById(prevScene, player.getScene().getPrevScenePoint());
Melledy's avatar
Melledy committed
96
97
98
99
100
			
			if (entry != null) {
				prevPos.set(entry.getPointData().getTranPos());
			}
		}
Akka's avatar
Akka committed
101
102
		// clean temp team if it has
		player.getTeamManager().cleanTemporaryTeam();
103
		player.getTowerManager().clearEntry();
Akka's avatar
Akka committed
104

Melledy's avatar
Melledy committed
105
106
107
108
		// Transfer player back to world
		player.getWorld().transferPlayerToScene(player, prevScene, prevPos);
		player.sendPacket(new BasePacket(PacketOpcodes.PlayerQuitDungeonRsp));
	}
109
110
111
112
113
114
	
	public void updateDailyDungeons() {
		for (ScenePointEntry entry : GameData.getScenePointEntries().values()) {
			entry.getPointData().updateDailyDungeon();
		}
	}
Melledy's avatar
Melledy committed
115
}