diff --git a/src/main/java/emu/grasscutter/game/world/Scene.java b/src/main/java/emu/grasscutter/game/world/Scene.java index cc0186bab..ce4d9eaf3 100644 --- a/src/main/java/emu/grasscutter/game/world/Scene.java +++ b/src/main/java/emu/grasscutter/game/world/Scene.java @@ -3,6 +3,7 @@ package emu.grasscutter.game.world; import emu.grasscutter.Grasscutter; import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameDepot; +import emu.grasscutter.data.binout.SceneNpcBornEntry; import emu.grasscutter.data.excels.*; import emu.grasscutter.game.dungeons.DungeonSettleListener; import emu.grasscutter.game.entity.*; @@ -53,6 +54,7 @@ public class Scene { private DungeonData dungeonData; private int prevScene; // Id of the previous scene private int prevScenePoint; + private Set npcBornEntrySet; public Scene(World world, SceneData sceneData) { this.world = world; this.sceneData = sceneData; @@ -65,6 +67,7 @@ public class Scene { this.spawnedEntities = ConcurrentHashMap.newKeySet(); this.deadSpawnedEntities = ConcurrentHashMap.newKeySet(); this.loadedBlocks = ConcurrentHashMap.newKeySet(); + this.npcBornEntrySet = ConcurrentHashMap.newKeySet(); this.scriptManager = new SceneScriptManager(this); } @@ -426,6 +429,8 @@ public class Scene { if(challenge != null){ challenge.onCheckTimeOut(); } + + checkNpcGroup(); } public int getEntityLevel(int baseLevel, int worldLevelOverride) { @@ -435,6 +440,25 @@ public class Scene { return level; } + public void checkNpcGroup(){ + Set npcBornEntries = ConcurrentHashMap.newKeySet(); + for (Player player : this.getPlayers()) { + npcBornEntries.addAll(loadNpcForPlayer(player)); + } + + // clear the unreachable group for client + var toUnload = this.npcBornEntrySet.stream() + .filter(i -> !npcBornEntries.contains(i)) + .map(SceneNpcBornEntry::getGroupId) + .toList(); + + if(toUnload.size() > 0){ + broadcastPacket(new PacketGroupUnloadNotify(toUnload)); + Grasscutter.getLogger().debug("Unload NPC Group {}", toUnload); + } + // exchange the new npcBornEntry Set + this.npcBornEntrySet = npcBornEntries; + } // TODO - Test public synchronized void checkSpawns() { @@ -569,9 +593,6 @@ public class Scene { .toList(); onLoadGroup(toLoad); } - for (Player player : this.getPlayers()) { - getScriptManager().meetEntities(loadNpcForPlayer(player, block)); - } } } @@ -754,47 +775,27 @@ public class Scene { addEntity(entity); } } - public List loadNpcForPlayer(Player player, SceneBlock block){ - if(!block.contains(player.getPos())){ - return List.of(); - } - + public void loadNpcForPlayerEnter(Player player){ + this.npcBornEntrySet.addAll(loadNpcForPlayer(player)); + } + private List loadNpcForPlayer(Player player){ var pos = player.getPos(); var data = GameData.getSceneNpcBornData().get(getId()); if(data == null){ return List.of(); } - var npcs = SceneIndexManager.queryNeighbors(data.getIndex(), pos.toDoubleArray(), + var npcList = SceneIndexManager.queryNeighbors(data.getIndex(), pos.toDoubleArray(), Grasscutter.getConfig().server.game.loadEntitiesForPlayerRange); - var entityNPCS = npcs.stream().map(item -> { - var group = data.getGroups().get(item.getGroupId()); - if(group == null){ - group = SceneGroup.of(item.getGroupId()); - data.getGroups().putIfAbsent(item.getGroupId(), group); - group.load(getId()); - } - if(group.npc == null){ - return null; - } - var npc = group.npc.get(item.getConfigId()); - if(npc == null){ - return null; - } + var sceneNpcBornEntries = npcList.stream() + .filter(i -> !this.npcBornEntrySet.contains(i)) + .toList(); - return getScriptManager().createNPC(npc, block.id, item.getSuiteIdList().get(0)); - }) - .filter(Objects::nonNull) - .filter(item -> getEntities().values().stream() - .filter(e -> e instanceof EntityNPC) - .noneMatch(e -> e.getConfigId() == item.getConfigId())) - .toList(); - - if(entityNPCS.size() > 0){ - broadcastPacket(new PacketGroupSuiteNotify(entityNPCS)); + if(sceneNpcBornEntries.size() > 0){ + this.broadcastPacket(new PacketGroupSuiteNotify(sceneNpcBornEntries)); + Grasscutter.getLogger().debug("Loaded Npc Group Suite {}", sceneNpcBornEntries); } - - return entityNPCS; + return npcList; } } diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java b/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java index 442ddf903..417da4749 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneGroup.java @@ -5,15 +5,11 @@ import emu.grasscutter.scripts.ScriptLoader; import emu.grasscutter.utils.Position; import lombok.Setter; import lombok.ToString; +import org.luaj.vm2.LuaValue; import javax.script.Bindings; import javax.script.CompiledScript; import javax.script.ScriptException; - -import org.luaj.vm2.LuaTable; -import org.luaj.vm2.LuaValue; - -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -33,7 +29,6 @@ public class SceneGroup { public Map monsters; // public Map gadgets; // public Map triggers; - public Map npc; // public Map regions; public List suites; public List variables; @@ -132,42 +127,10 @@ public class SceneGroup { } // Add variables to suite - this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables")); - // NPC in groups - this.npc = ScriptLoader.getSerializer().toList(SceneNPC.class, this.bindings.get("npcs")).stream() - .collect(Collectors.toMap(x -> x.npc_id, y -> y)); - this.npc.values().forEach(n -> n.group = this); + this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables")); // Add monsters and gadgets to suite - for (SceneSuite suite : this.suites) { - suite.sceneMonsters = new ArrayList<>( - suite.monsters.stream() - .filter(this.monsters::containsKey) - .map(this.monsters::get) - .toList() - ); - - suite.sceneGadgets = new ArrayList<>( - suite.gadgets.stream() - .filter(this.gadgets::containsKey) - .map(this.gadgets::get) - .toList() - ); - - suite.sceneTriggers = new ArrayList<>( - suite.triggers.stream() - .filter(this.triggers::containsKey) - .map(this.triggers::get) - .toList() - ); - - suite.sceneRegions = new ArrayList<>( - suite.regions.stream() - .filter(this.regions::containsKey) - .map(this.regions::get) - .toList() - ); - } + this.suites.forEach(i -> i.init(this)); } catch (ScriptException e) { Grasscutter.getLogger().error("An error occurred while loading group " + this.id + " in scene " + sceneId + ".", e); diff --git a/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java b/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java index 6d5f62020..541d60036 100644 --- a/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java +++ b/src/main/java/emu/grasscutter/scripts/data/SceneSuite.java @@ -1,5 +1,6 @@ package emu.grasscutter.scripts.data; +import java.util.ArrayList; import java.util.List; import lombok.Setter; @@ -8,15 +9,53 @@ import lombok.ToString; @ToString @Setter public class SceneSuite { - public List monsters; - public List gadgets; - public List triggers; - public List regions; + // make it refer the default empty list to avoid NPE caused by some group + public List monsters = List.of(); + public List gadgets = List.of(); + public List triggers = List.of(); + public List regions = List.of(); public int rand_weight; - public transient List sceneMonsters; - public transient List sceneGadgets; - public transient List sceneTriggers; - public transient List sceneRegions; + public transient List sceneMonsters = List.of(); + public transient List sceneGadgets = List.of(); + public transient List sceneTriggers = List.of(); + public transient List sceneRegions = List.of(); + public void init(SceneGroup sceneGroup) { + if(sceneGroup.monsters != null){ + this.sceneMonsters = new ArrayList<>( + this.monsters.stream() + .filter(sceneGroup.monsters::containsKey) + .map(sceneGroup.monsters::get) + .toList() + ); + } + + if(sceneGroup.gadgets != null){ + this.sceneGadgets = new ArrayList<>( + this.gadgets.stream() + .filter(sceneGroup.gadgets::containsKey) + .map(sceneGroup.gadgets::get) + .toList() + ); + } + + if(sceneGroup.triggers != null) { + this.sceneTriggers = new ArrayList<>( + this.triggers.stream() + .filter(sceneGroup.triggers::containsKey) + .map(sceneGroup.triggers::get) + .toList() + ); + } + if(sceneGroup.regions != null) { + this.sceneRegions = new ArrayList<>( + this.regions.stream() + .filter(sceneGroup.regions::containsKey) + .map(sceneGroup.regions::get) + .toList() + ); + } + + } } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerEnterSceneDoneReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerEnterSceneDoneReq.java index d374cfd46..be2d4ac6b 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerEnterSceneDoneReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerEnterSceneDoneReq.java @@ -5,35 +5,33 @@ import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.server.game.GameSession; -import emu.grasscutter.server.packet.send.PacketEnterSceneDoneRsp; -import emu.grasscutter.server.packet.send.PacketPlayerTimeNotify; -import emu.grasscutter.server.packet.send.PacketScenePlayerLocationNotify; -import emu.grasscutter.server.packet.send.PacketWorldPlayerLocationNotify; -import emu.grasscutter.server.packet.send.PacketWorldPlayerRTTNotify; +import emu.grasscutter.server.packet.send.*; @Opcodes(PacketOpcodes.EnterSceneDoneReq) public class HandlerEnterSceneDoneReq extends PacketHandler { - + @Override public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { // Finished loading session.getPlayer().setSceneLoadState(SceneLoadState.LOADED); - + // Done session.send(new PacketEnterSceneDoneRsp(session.getPlayer())); session.send(new PacketPlayerTimeNotify(session.getPlayer())); // Probably not the right place - + // Spawn player in world session.getPlayer().getScene().spawnPlayer(session.getPlayer()); - + // Spawn other entites already in world session.getPlayer().getScene().showOtherEntities(session.getPlayer()); - + // Locations session.send(new PacketWorldPlayerLocationNotify(session.getPlayer().getWorld())); session.send(new PacketScenePlayerLocationNotify(session.getPlayer().getScene())); session.send(new PacketWorldPlayerRTTNotify(session.getPlayer().getWorld())); - + + // spawn NPC + session.getPlayer().getScene().loadNpcForPlayerEnter(session.getPlayer()); // Reset timer for sending player locations session.getPlayer().resetSendPlayerLocTime(); } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketGroupSuiteNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketGroupSuiteNotify.java index 9abcfc049..59c5a83a3 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketGroupSuiteNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketGroupSuiteNotify.java @@ -1,6 +1,6 @@ package emu.grasscutter.server.packet.send; -import emu.grasscutter.game.entity.EntityNPC; +import emu.grasscutter.data.binout.SceneNpcBornEntry; import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.GroupSuiteNotifyOuterClass; @@ -10,14 +10,18 @@ import java.util.List; public class PacketGroupSuiteNotify extends BasePacket { /** - * control which npc suite is loaded + * Real control which npc suite is loaded + * EntityNPC is useless */ - public PacketGroupSuiteNotify(List list) { + public PacketGroupSuiteNotify(List npcBornEntries) { super(PacketOpcodes.GroupSuiteNotify); var proto = GroupSuiteNotifyOuterClass.GroupSuiteNotify.newBuilder(); - list.forEach(item -> proto.putGroupMap(item.getGroupId(), item.getSuiteId())); + npcBornEntries.forEach(x -> + x.getSuiteIdList().forEach(y -> + proto.putGroupMap(x.getGroupId(), y) + )); this.setData(proto); diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketGroupUnloadNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketGroupUnloadNotify.java new file mode 100644 index 000000000..d86746680 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketGroupUnloadNotify.java @@ -0,0 +1,20 @@ +package emu.grasscutter.server.packet.send; + +import emu.grasscutter.net.packet.BasePacket; +import emu.grasscutter.net.packet.PacketOpcodes; +import emu.grasscutter.net.proto.GroupUnloadNotifyOuterClass; + +import java.util.List; + +public class PacketGroupUnloadNotify extends BasePacket { + + public PacketGroupUnloadNotify(List groupList) { + super(PacketOpcodes.GroupUnloadNotify); + + var proto = GroupUnloadNotifyOuterClass.GroupUnloadNotify.newBuilder(); + + proto.addAllGroupList(groupList); + + this.setData(proto); + } +}