2022-04-17 12:43:07 +00:00
|
|
|
package emu.grasscutter.game;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import emu.grasscutter.game.entity.GenshinEntity;
|
|
|
|
import emu.grasscutter.game.props.ClimateType;
|
|
|
|
import emu.grasscutter.game.props.EnterReason;
|
|
|
|
import emu.grasscutter.game.props.EntityIdType;
|
|
|
|
import emu.grasscutter.game.props.FightProperty;
|
|
|
|
import emu.grasscutter.game.props.LifeState;
|
2022-04-18 17:14:32 +00:00
|
|
|
import emu.grasscutter.data.GenshinData;
|
|
|
|
import emu.grasscutter.data.def.SceneData;
|
2022-04-17 12:43:07 +00:00
|
|
|
import emu.grasscutter.game.GenshinPlayer.SceneLoadState;
|
|
|
|
import emu.grasscutter.game.entity.EntityAvatar;
|
|
|
|
import emu.grasscutter.game.entity.EntityClientGadget;
|
2022-04-18 06:43:47 +00:00
|
|
|
import emu.grasscutter.game.entity.EntityGadget;
|
2022-04-17 12:43:07 +00:00
|
|
|
import emu.grasscutter.net.packet.GenshinPacket;
|
|
|
|
import emu.grasscutter.net.proto.AttackResultOuterClass.AttackResult;
|
|
|
|
import emu.grasscutter.net.proto.EnterTypeOuterClass.EnterType;
|
|
|
|
import emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketDelTeamEntityNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketLifeStateChangeNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketSceneEntityDisappearNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketScenePlayerInfoNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketSyncScenePlayTeamEntityNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketSyncTeamEntityNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketWorldPlayerInfoNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketWorldPlayerRTTNotify;
|
2022-04-18 16:39:29 +00:00
|
|
|
import emu.grasscutter.utils.Position;
|
2022-04-17 12:43:07 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
|
|
|
|
|
|
public class World implements Iterable<GenshinPlayer> {
|
|
|
|
private final GenshinPlayer owner;
|
|
|
|
private final List<GenshinPlayer> players;
|
2022-04-18 16:39:29 +00:00
|
|
|
private final Int2ObjectMap<GenshinScene> scenes;
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
private int levelEntityId;
|
|
|
|
private int nextEntityId = 0;
|
|
|
|
private int nextPeerId = 0;
|
|
|
|
private int worldLevel;
|
2022-04-18 16:39:29 +00:00
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
private boolean isMultiplayer;
|
|
|
|
|
|
|
|
public World(GenshinPlayer player) {
|
|
|
|
this(player, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public World(GenshinPlayer player, boolean isMultiplayer) {
|
|
|
|
this.owner = player;
|
|
|
|
this.players = Collections.synchronizedList(new ArrayList<>());
|
2022-04-18 16:39:29 +00:00
|
|
|
this.scenes = new Int2ObjectOpenHashMap<>();
|
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
this.levelEntityId = getNextEntityId(EntityIdType.MPLEVEL);
|
|
|
|
this.worldLevel = player.getWorldLevel();
|
|
|
|
this.isMultiplayer = isMultiplayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public GenshinPlayer getHost() {
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLevelEntityId() {
|
|
|
|
return levelEntityId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getHostPeerId() {
|
|
|
|
if (this.getHost() == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return this.getHost().getPeerId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getNextPeerId() {
|
|
|
|
return ++this.nextPeerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getWorldLevel() {
|
|
|
|
return worldLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setWorldLevel(int worldLevel) {
|
|
|
|
this.worldLevel = worldLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<GenshinPlayer> getPlayers() {
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
public Int2ObjectMap<GenshinScene> getScenes() {
|
|
|
|
return this.scenes;
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
public GenshinScene getSceneById(int sceneId) {
|
2022-04-18 17:14:32 +00:00
|
|
|
// Get scene normally
|
|
|
|
GenshinScene scene = getScenes().get(sceneId);
|
|
|
|
if (scene != null) {
|
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create scene from scene data if it doesnt exist
|
|
|
|
SceneData sceneData = GenshinData.getSceneDataMap().get(sceneId);
|
|
|
|
if (sceneData != null) {
|
|
|
|
scene = new GenshinScene(this, sceneData);
|
2022-04-19 06:00:12 +00:00
|
|
|
this.registerScene(scene);
|
2022-04-18 17:14:32 +00:00
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
public int getPlayerCount() {
|
|
|
|
return getPlayers().size();
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isMultiplayer() {
|
|
|
|
return isMultiplayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getNextEntityId(EntityIdType idType) {
|
|
|
|
return (idType.getId() << 24) + ++this.nextEntityId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void addPlayer(GenshinPlayer player) {
|
|
|
|
// Check if player already in
|
|
|
|
if (getPlayers().contains(player)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove player from prev world
|
|
|
|
if (player.getWorld() != null) {
|
|
|
|
player.getWorld().removePlayer(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register
|
|
|
|
player.setWorld(this);
|
|
|
|
getPlayers().add(player);
|
2022-04-18 16:39:29 +00:00
|
|
|
|
|
|
|
// Set player variables
|
2022-04-17 12:43:07 +00:00
|
|
|
player.setPeerId(this.getNextPeerId());
|
|
|
|
player.getTeamManager().setEntityId(getNextEntityId(EntityIdType.TEAM));
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
// Copy main team to mp team
|
|
|
|
if (this.isMultiplayer()) {
|
|
|
|
player.getTeamManager().getMpTeam().copyFrom(player.getTeamManager().getCurrentSinglePlayerTeamInfo(), player.getTeamManager().getMaxTeamSize());
|
2022-04-19 08:43:38 +00:00
|
|
|
player.getTeamManager().setCurrentCharacterIndex(0);
|
2022-04-18 16:39:29 +00:00
|
|
|
}
|
2022-04-17 12:43:07 +00:00
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
// Add to scene
|
|
|
|
GenshinScene scene = this.getSceneById(player.getSceneId());
|
|
|
|
scene.addPlayer(player);
|
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
// Info packet for other players
|
|
|
|
if (this.getPlayers().size() > 1) {
|
|
|
|
this.updatePlayerInfos(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void removePlayer(GenshinPlayer player) {
|
|
|
|
// Remove team entities
|
|
|
|
player.sendPacket(
|
|
|
|
new PacketDelTeamEntityNotify(
|
|
|
|
player.getSceneId(),
|
|
|
|
getPlayers().stream().map(p -> p.getTeamManager().getEntityId()).collect(Collectors.toList())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Deregister
|
|
|
|
getPlayers().remove(player);
|
|
|
|
player.setWorld(null);
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
// Remove from scene
|
|
|
|
GenshinScene scene = this.getSceneById(player.getSceneId());
|
|
|
|
scene.removePlayer(player);
|
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
// Info packet for other players
|
|
|
|
if (this.getPlayers().size() > 0) {
|
|
|
|
this.updatePlayerInfos(player);
|
|
|
|
}
|
2022-04-18 16:39:29 +00:00
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
// Disband world if host leaves
|
|
|
|
if (getHost() == player) {
|
|
|
|
List<GenshinPlayer> kicked = new ArrayList<>(this.getPlayers());
|
|
|
|
for (GenshinPlayer victim : kicked) {
|
|
|
|
World world = new World(victim);
|
|
|
|
world.addPlayer(victim);
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
victim.sendPacket(new PacketPlayerEnterSceneNotify(victim, EnterType.EnterSelf, EnterReason.TeamKick, victim.getSceneId(), victim.getPos()));
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:00:12 +00:00
|
|
|
public void registerScene(GenshinScene scene) {
|
|
|
|
this.getScenes().put(scene.getId(), scene);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deregisterScene(GenshinScene scene) {
|
|
|
|
this.getScenes().remove(scene.getId());
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:14:32 +00:00
|
|
|
public boolean transferPlayerToScene(GenshinPlayer player, int sceneId, Position pos) {
|
|
|
|
if (player.getScene().getId() == sceneId || GenshinData.getSceneDataMap().get(sceneId) == null) {
|
|
|
|
return false;
|
2022-04-18 16:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 16:39:29 +00:00
|
|
|
if (player.getScene() != null) {
|
|
|
|
player.getScene().removePlayer(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
GenshinScene scene = this.getSceneById(sceneId);
|
|
|
|
scene.addPlayer(player);
|
|
|
|
player.getPos().set(pos);
|
2022-04-18 17:14:32 +00:00
|
|
|
|
|
|
|
// Teleport packet
|
2022-04-18 16:39:29 +00:00
|
|
|
player.sendPacket(new PacketPlayerEnterSceneNotify(player, EnterType.EnterSelf, EnterReason.TransPoint, sceneId, pos));
|
2022-04-18 17:14:32 +00:00
|
|
|
return true;
|
2022-04-18 16:39:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
private void updatePlayerInfos(GenshinPlayer paramPlayer) {
|
|
|
|
for (GenshinPlayer player : getPlayers()) {
|
2022-04-18 06:36:47 +00:00
|
|
|
// Dont send packets if player is loading in and filter out joining player
|
2022-04-17 12:43:07 +00:00
|
|
|
if (!player.hasSentAvatarDataNotify() || player.getSceneLoadState().getValue() < SceneLoadState.INIT.getValue() || player == paramPlayer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-18 06:36:47 +00:00
|
|
|
// Update team of all players since max players has been changed - Probably not the best way to do it
|
|
|
|
if (this.isMultiplayer()) {
|
|
|
|
player.getTeamManager().getMpTeam().copyFrom(player.getTeamManager().getMpTeam(), player.getTeamManager().getMaxTeamSize());
|
|
|
|
player.getTeamManager().updateTeamEntities(null);
|
|
|
|
}
|
|
|
|
|
2022-04-17 12:43:07 +00:00
|
|
|
// World player info packets
|
|
|
|
player.getSession().send(new PacketWorldPlayerInfoNotify(this));
|
|
|
|
player.getSession().send(new PacketScenePlayerInfoNotify(this));
|
|
|
|
player.getSession().send(new PacketWorldPlayerRTTNotify(this));
|
|
|
|
|
|
|
|
// Team packets
|
|
|
|
player.getSession().send(new PacketSyncTeamEntityNotify(player));
|
|
|
|
player.getSession().send(new PacketSyncScenePlayTeamEntityNotify(player));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void broadcastPacket(GenshinPacket packet) {
|
|
|
|
// Send to all players - might have to check if player has been sent data packets
|
|
|
|
for (GenshinPlayer player : this.getPlayers()) {
|
|
|
|
player.getSession().send(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<GenshinPlayer> iterator() {
|
|
|
|
return getPlayers().iterator();
|
|
|
|
}
|
|
|
|
}
|