From 6ab5c3de673ce28d83a09b78ab0c2d6cbb249268 Mon Sep 17 00:00:00 2001 From: austin Date: Tue, 6 May 2025 15:12:11 -0400 Subject: [PATCH] structure scanning work --- docs/src/progress/renderertodo.md | 5 ++++ .../engine/threads/ThreadManager.java | 3 +++ .../net/server/protocol/TerrainProtocol.java | 11 ++------ .../actions/interact/PlaceBlockNode.java | 7 ++++- .../ai/services/NearbyEntityService.java | 8 +++--- .../server/player/BlockActions.java | 27 ++++++++++++++++++- .../service/StructureScanningService.java | 20 +++++++++++++- 7 files changed, 66 insertions(+), 15 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 7fe0bb75..3af447f1 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1690,6 +1690,11 @@ Code cleanup More code cleanup Cleanup some TODOs +(05/06/2025) +Refactor block actions +Placing fabs/blocks triggers structure scanning service +Try closing ai manager threads on engine close + diff --git a/src/main/java/electrosphere/engine/threads/ThreadManager.java b/src/main/java/electrosphere/engine/threads/ThreadManager.java index a22f767f..8efea554 100644 --- a/src/main/java/electrosphere/engine/threads/ThreadManager.java +++ b/src/main/java/electrosphere/engine/threads/ThreadManager.java @@ -134,6 +134,9 @@ public class ThreadManager { TerrainChunk.haltThreads(); FoliageModel.haltThreads(); BlockChunkEntity.haltThreads(); + if(Globals.aiManager != null && Globals.aiManager.getPathfindingService() != null){ + Globals.aiManager.getPathfindingService().shutdown(); + } // //interrupt all threads diff --git a/src/main/java/electrosphere/net/server/protocol/TerrainProtocol.java b/src/main/java/electrosphere/net/server/protocol/TerrainProtocol.java index a3eee375..5e4debdd 100644 --- a/src/main/java/electrosphere/net/server/protocol/TerrainProtocol.java +++ b/src/main/java/electrosphere/net/server/protocol/TerrainProtocol.java @@ -13,7 +13,6 @@ import electrosphere.client.block.BlockChunkData; import electrosphere.client.terrain.cache.ChunkData; import electrosphere.engine.Globals; import electrosphere.entity.Entity; -import electrosphere.entity.state.item.ServerChargeState; import electrosphere.logger.LoggerInterface; import electrosphere.net.parser.net.message.TerrainMessage; import electrosphere.net.server.ServerConnectionHandler; @@ -22,7 +21,6 @@ import electrosphere.net.template.ServerProtocolTemplate; import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.ServerWorldData; import electrosphere.server.datacell.utils.EntityLookupUtils; -import electrosphere.server.physics.block.editing.ServerBlockEditing; import electrosphere.server.physics.fluid.manager.ServerFluidChunk; import electrosphere.server.physics.terrain.editing.TerrainEditing; import electrosphere.server.physics.terrain.manager.ServerTerrainChunk; @@ -89,20 +87,15 @@ public class TerrainProtocol implements ServerProtocolTemplate { case REQUESTEDITBLOCK: { LoggerInterface.loggerNetworking.DEBUG("(Server) Received request to edit block at " + message.getworldX() + " " + message.getworldY() + " " + message.getworldZ()); Entity targetEntity = EntityLookupUtils.getEntityById(connectionHandler.getPlayerEntityId()); - // Realm playerRealm = Globals.realmManager.getEntityRealm(targetEntity); Vector3i worldPos = new Vector3i(message.getworldX(),message.getworldY(),message.getworldZ()); Vector3i blockPos = new Vector3i(message.getvoxelX(),message.getvoxelY(),message.getvoxelZ()); - BlockActions.editBlock(targetEntity, worldPos, blockPos, (short)message.getblockType(), message.getblockEditSize()); - // ServerBlockEditing.editBlockArea(playerRealm, worldPos, blockPos, (short)message.getblockType(), (short)message.getblockMetadata(), message.getblockEditSize()); - // ServerChargeState.attemptRemoveCharges(targetEntity, 1); + BlockActions.editBlockArea(targetEntity, worldPos, blockPos, (short)message.getblockType(), message.getblockEditSize()); } break; case REQUESTPLACEFAB: { Vector3i worldPos = new Vector3i(message.getworldX(),message.getworldY(),message.getworldZ()); Vector3i blockPos = new Vector3i(message.getvoxelX(),message.getvoxelY(),message.getvoxelZ()); Entity targetEntity = EntityLookupUtils.getEntityById(connectionHandler.getPlayerEntityId()); - Realm playerRealm = Globals.realmManager.getEntityRealm(targetEntity); - ServerBlockEditing.placeBlockFab(playerRealm, worldPos, blockPos, message.getblockRotation(), message.getfabPath()); - ServerChargeState.attemptRemoveCharges(targetEntity, 1); + BlockActions.placeFab(targetEntity, worldPos, blockPos, message.getblockRotation(), message.getfabPath()); } break; //all ignored message types case UPDATEFLUIDDATA: diff --git a/src/main/java/electrosphere/server/ai/nodes/actions/interact/PlaceBlockNode.java b/src/main/java/electrosphere/server/ai/nodes/actions/interact/PlaceBlockNode.java index a557c5aa..ee22889e 100644 --- a/src/main/java/electrosphere/server/ai/nodes/actions/interact/PlaceBlockNode.java +++ b/src/main/java/electrosphere/server/ai/nodes/actions/interact/PlaceBlockNode.java @@ -16,6 +16,11 @@ import electrosphere.server.player.BlockActions; */ public class PlaceBlockNode implements AITreeNode { + /** + * Size of the edit to apply + */ + static final int EDIT_SIZE = 1; + @Override public AITreeNodeResult evaluate(Entity entity, Blackboard blackboard) { Vector3d position = BeginStructureNode.getStructureBuildTarget(blackboard); @@ -24,7 +29,7 @@ public class PlaceBlockNode implements AITreeNode { short blockType = BeginStructureNode.getBuildBlock(blackboard); - BlockActions.editBlock(entity, chunkPos, blockPos, blockType, 1); + BlockActions.editBlockArea(entity, chunkPos, blockPos, blockType, EDIT_SIZE); SolveBuildMaterialNode.clearBuildTarget(blackboard); diff --git a/src/main/java/electrosphere/server/ai/services/NearbyEntityService.java b/src/main/java/electrosphere/server/ai/services/NearbyEntityService.java index ca723d72..0181060a 100644 --- a/src/main/java/electrosphere/server/ai/services/NearbyEntityService.java +++ b/src/main/java/electrosphere/server/ai/services/NearbyEntityService.java @@ -27,9 +27,11 @@ public class NearbyEntityService implements AIService { for(AI ai : Globals.aiManager.getAIList()){ Entity entity = ai.getParent(); Realm realm = Globals.realmManager.getEntityRealm(entity); - Vector3d position = EntityUtils.getPosition(entity); - Collection nearbyEntities = realm.getDataCellManager().entityLookup(position, NearbyEntityService.SEARCH_DIST); - NearbyEntityService.setNearbyEntities(ai.getBlackboard(), nearbyEntities); + if(realm != null){ + Vector3d position = EntityUtils.getPosition(entity); + Collection nearbyEntities = realm.getDataCellManager().entityLookup(position, NearbyEntityService.SEARCH_DIST); + NearbyEntityService.setNearbyEntities(ai.getBlackboard(), nearbyEntities); + } } } diff --git a/src/main/java/electrosphere/server/player/BlockActions.java b/src/main/java/electrosphere/server/player/BlockActions.java index ebfc8de0..9b6efaa3 100644 --- a/src/main/java/electrosphere/server/player/BlockActions.java +++ b/src/main/java/electrosphere/server/player/BlockActions.java @@ -7,9 +7,12 @@ import electrosphere.entity.Entity; import electrosphere.entity.state.equip.ServerToolbarState; import electrosphere.entity.state.item.ServerChargeState; import electrosphere.entity.types.common.CommonEntityUtils; +import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.game.data.block.BlockType; import electrosphere.game.data.item.Item; +import electrosphere.net.server.player.Player; import electrosphere.server.datacell.Realm; +import electrosphere.server.datacell.ServerWorldData; import electrosphere.server.physics.block.editing.ServerBlockEditing; /** @@ -25,7 +28,7 @@ public class BlockActions { * @param blockType The type of block to edit to * @param editSize The size of the edit */ - public static void editBlock(Entity creature, Vector3i chunkPos, Vector3i blockPos, short blockType, int editSize){ + public static void editBlockArea(Entity creature, Vector3i chunkPos, Vector3i blockPos, short blockType, int editSize){ Realm playerRealm = Globals.realmManager.getEntityRealm(creature); if(ServerToolbarState.hasServerToolbarState(creature)){ //check that we have the block equipped @@ -34,6 +37,10 @@ public class BlockActions { String equippedItemType = CommonEntityUtils.getEntitySubtype(equippedItem); BlockType blockTypeData = Globals.gameConfigCurrent.getBlockData().getTypeFromId((int)blockType); String goalBlockEntityId = Item.getBlockTypeId(blockTypeData); + if(CreatureUtils.hasControllerPlayerId(creature)){ + Player player = Globals.playerManager.getPlayerFromId(CreatureUtils.getControllerPlayerId(creature)); + Globals.structureScanningService.queue(player, ServerWorldData.convertLocalBlockToRealSpace(chunkPos, blockPos)); + } if(equippedItemType.equals(goalBlockEntityId)){ //place the block @@ -43,4 +50,22 @@ public class BlockActions { } } + /** + * Places a fab + * @param creature The creature placing the fab + * @param chunkPos The chunk position to place at + * @param blockPos The block position to place at + * @param blockRotation The rotation to apply to the fab + * @param fabPath The path to the fab itself + */ + public static void placeFab(Entity creature, Vector3i chunkPos, Vector3i blockPos, int blockRotation, String fabPath){ + Realm playerRealm = Globals.realmManager.getEntityRealm(creature); + ServerBlockEditing.placeBlockFab(playerRealm, chunkPos, blockPos, blockRotation, fabPath); + ServerChargeState.attemptRemoveCharges(creature, 1); + if(CreatureUtils.hasControllerPlayerId(creature)){ + Player player = Globals.playerManager.getPlayerFromId(CreatureUtils.getControllerPlayerId(creature)); + Globals.structureScanningService.queue(player, ServerWorldData.convertLocalBlockToRealSpace(chunkPos, blockPos)); + } + } + } diff --git a/src/main/java/electrosphere/server/service/StructureScanningService.java b/src/main/java/electrosphere/server/service/StructureScanningService.java index 6a00c636..3b729a0f 100644 --- a/src/main/java/electrosphere/server/service/StructureScanningService.java +++ b/src/main/java/electrosphere/server/service/StructureScanningService.java @@ -2,10 +2,13 @@ package electrosphere.server.service; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import org.joml.Vector3d; import electrosphere.engine.signal.Signal.SignalType; +import electrosphere.logger.LoggerInterface; import electrosphere.engine.Globals; import electrosphere.engine.signal.SignalServiceImpl; import electrosphere.net.server.player.Player; @@ -55,7 +58,22 @@ public class StructureScanningService extends SignalServiceImpl { * Simulates the service */ public void simulate(){ - + Set> jobs = playerTimeoutMap.entrySet(); + for(Entry job : jobs){ + if(job.getValue().targetFrame <= Globals.timekeeper.getNumberOfSimFramesElapsed()){ + //run this job + playerTimeoutMap.remove(job.getKey()); + StructureScanningService.scanForStructure(job.getValue()); + } + } + } + + /** + * Executes a scanning job + * @param job The job + */ + protected static void scanForStructure(ScanningJob job){ + LoggerInterface.loggerEngine.WARNING("Scan structure at " + job.position); } /**