Limit co-op teams to 4 avatars max total

This commit is contained in:
Melledy 2022-04-17 23:36:47 -07:00
parent 7925d1cda3
commit c919c9c9eb
3 changed files with 22 additions and 10 deletions

View File

@ -60,13 +60,16 @@ public class TeamInfo {
} }
public void copyFrom(TeamInfo team, int maxTeamSize) { public void copyFrom(TeamInfo team, int maxTeamSize) {
// Clear // Clone avatar ids from team to copy from
List<Integer> avatarIds = new ArrayList<>(team.getAvatars());
// Clear current avatar list first
this.getAvatars().clear(); this.getAvatars().clear();
// Copy from team // Copy from team
int len = Math.min(team.getAvatars().size(), maxTeamSize); int len = Math.min(avatarIds.size(), maxTeamSize);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
int id = team.getAvatars().get(i); int id = avatarIds.get(i);
this.getAvatars().add(id); this.getAvatars().add(id);
} }
} }

View File

@ -166,7 +166,7 @@ public class TeamManager {
// Methods // Methods
public void updateTeamResonances() { private void updateTeamResonances() {
Int2IntOpenHashMap map = new Int2IntOpenHashMap(); Int2IntOpenHashMap map = new Int2IntOpenHashMap();
this.getTeamResonances().clear(); this.getTeamResonances().clear();
@ -196,7 +196,7 @@ public class TeamManager {
} }
} }
private void updateTeamEntities(GenshinPacket responsePacket) { public void updateTeamEntities(GenshinPacket responsePacket) {
// Sanity check - Should never happen // Sanity check - Should never happen
if (this.getCurrentTeamInfo().getAvatars().size() <= 0) { if (this.getCurrentTeamInfo().getAvatars().size() <= 0) {
return; return;
@ -241,7 +241,7 @@ public class TeamManager {
// Set new selected character index // Set new selected character index
if (prevSelectedAvatarIndex == -1) { if (prevSelectedAvatarIndex == -1) {
// Previous selected avatar is not in the same spot, we will select the current one in the prev slot // Previous selected avatar is not in the same spot, we will select the current one in the prev slot
prevSelectedAvatarIndex = Math.min(this.currentCharacterIndex, getCurrentTeamInfo().getAvatars().size() - 1); prevSelectedAvatarIndex = Math.min(this.currentCharacterIndex, this.getActiveTeam().size() - 1);
} }
this.currentCharacterIndex = prevSelectedAvatarIndex; this.currentCharacterIndex = prevSelectedAvatarIndex;

View File

@ -28,7 +28,6 @@ import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify;
import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify; import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify;
import emu.grasscutter.server.packet.send.PacketSceneEntityDisappearNotify; import emu.grasscutter.server.packet.send.PacketSceneEntityDisappearNotify;
import emu.grasscutter.server.packet.send.PacketScenePlayerInfoNotify; import emu.grasscutter.server.packet.send.PacketScenePlayerInfoNotify;
import emu.grasscutter.server.packet.send.PacketSceneTeamUpdateNotify;
import emu.grasscutter.server.packet.send.PacketSyncScenePlayTeamEntityNotify; import emu.grasscutter.server.packet.send.PacketSyncScenePlayTeamEntityNotify;
import emu.grasscutter.server.packet.send.PacketSyncTeamEntityNotify; import emu.grasscutter.server.packet.send.PacketSyncTeamEntityNotify;
import emu.grasscutter.server.packet.send.PacketWorldPlayerInfoNotify; import emu.grasscutter.server.packet.send.PacketWorldPlayerInfoNotify;
@ -169,7 +168,7 @@ public class World implements Iterable<GenshinPlayer> {
player.setPeerId(this.getNextPeerId()); player.setPeerId(this.getNextPeerId());
player.getTeamManager().setEntityId(getNextEntityId(EntityIdType.TEAM)); player.getTeamManager().setEntityId(getNextEntityId(EntityIdType.TEAM));
// TODO Update team of all players // Setup team avatars
this.setupPlayerAvatars(player); this.setupPlayerAvatars(player);
// Info packet for other players // Info packet for other players
@ -212,18 +211,23 @@ public class World implements Iterable<GenshinPlayer> {
private void updatePlayerInfos(GenshinPlayer paramPlayer) { private void updatePlayerInfos(GenshinPlayer paramPlayer) {
for (GenshinPlayer player : getPlayers()) { for (GenshinPlayer player : getPlayers()) {
// Dont send packets if player is loading in // Dont send packets if player is loading in and filter out joining player
if (!player.hasSentAvatarDataNotify() || player.getSceneLoadState().getValue() < SceneLoadState.INIT.getValue() || player == paramPlayer) { if (!player.hasSentAvatarDataNotify() || player.getSceneLoadState().getValue() < SceneLoadState.INIT.getValue() || player == paramPlayer) {
continue; continue;
} }
// 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);
}
// World player info packets // World player info packets
player.getSession().send(new PacketWorldPlayerInfoNotify(this)); player.getSession().send(new PacketWorldPlayerInfoNotify(this));
player.getSession().send(new PacketScenePlayerInfoNotify(this)); player.getSession().send(new PacketScenePlayerInfoNotify(this));
player.getSession().send(new PacketWorldPlayerRTTNotify(this)); player.getSession().send(new PacketWorldPlayerRTTNotify(this));
// Team packets // Team packets
player.getSession().send(new PacketSceneTeamUpdateNotify(player));
player.getSession().send(new PacketSyncTeamEntityNotify(player)); player.getSession().send(new PacketSyncTeamEntityNotify(player));
player.getSession().send(new PacketSyncScenePlayTeamEntityNotify(player)); player.getSession().send(new PacketSyncScenePlayTeamEntityNotify(player));
} }
@ -283,6 +287,11 @@ public class World implements Iterable<GenshinPlayer> {
EntityAvatar entity = new EntityAvatar(this, player.getAvatars().getAvatarById(avatarId)); EntityAvatar entity = new EntityAvatar(this, player.getAvatars().getAvatarById(avatarId));
player.getTeamManager().getActiveTeam().add(entity); player.getTeamManager().getActiveTeam().add(entity);
} }
// Limit character index in case its out of bounds
if (player.getTeamManager().getCurrentCharacterIndex() >= player.getTeamManager().getActiveTeam().size() || player.getTeamManager().getCurrentCharacterIndex() < 0) {
player.getTeamManager().setCurrentCharacterIndex(player.getTeamManager().getCurrentCharacterIndex() - 1);
}
} }
private void removePlayerAvatars(GenshinPlayer player) { private void removePlayerAvatars(GenshinPlayer player) {