DungeonManager.java 3.77 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;
11
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
12
13
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.server.game.GameServer;
Melledy's avatar
Melledy committed
15
16
17
import emu.grasscutter.server.packet.send.PacketDungeonEntryInfoRsp;
import emu.grasscutter.server.packet.send.PacketPlayerEnterDungeonRsp;
import emu.grasscutter.utils.Position;
Melledy's avatar
Melledy committed
18

19
20
import java.util.List;

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

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

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

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

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

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