Files
Server/src/engine/devcmd/cmds/PurgeObjectsCmd.java
T

322 lines
10 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
package engine.devcmd.cmds;
import engine.InterestManagement.WorldGrid;
import engine.devcmd.AbstractDevCmd;
import engine.gameManager.BuildingManager;
import engine.gameManager.DbManager;
import engine.gameManager.ZoneManager;
import engine.math.Vector3fImmutable;
import engine.mbEnums;
import engine.mbEnums.BuildingGroup;
import engine.mbEnums.GameObjectType;
2022-04-30 09:41:17 -04:00
import engine.objects.*;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;
import java.util.HashSet;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
2023-07-15 09:23:48 -04:00
* @author Summary: Game designer utility command to purge all
* objects of a given type within a supplied range
2022-04-30 09:41:17 -04:00
*/
public class PurgeObjectsCmd extends AbstractDevCmd {
2023-07-15 09:23:48 -04:00
// Instance variables
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private Vector3fImmutable _currentLocation;
private float _targetRange;
private int _targetMask;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Concurrency support
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private ReadWriteLock lock = new ReentrantReadWriteLock();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Constructor
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public PurgeObjectsCmd() {
2022-04-30 09:41:17 -04:00
super("purge");
}
2023-07-15 09:23:48 -04:00
private static void PurgeWalls(Zone zone, PlayerCharacter pc) {
2022-04-30 09:41:17 -04:00
2023-09-20 16:05:57 -04:00
if (!zone.guild_zone)
2022-04-30 09:41:17 -04:00
return;
2023-07-15 09:23:48 -04:00
for (Building building : zone.zoneBuildingSet) {
2022-04-30 09:41:17 -04:00
if (!BuildingManager.IsWallPiece(building))
continue;
2023-07-15 09:23:48 -04:00
for (AbstractCharacter ac : building.getHirelings().keySet()) {
2022-04-30 09:41:17 -04:00
NPC npc = null;
Mob mobA = null;
if (ac.getObjectType() == GameObjectType.NPC)
2023-07-15 09:23:48 -04:00
npc = (NPC) ac;
2022-04-30 09:41:17 -04:00
else if (ac.getObjectType() == GameObjectType.Mob)
2023-07-15 09:23:48 -04:00
mobA = (Mob) ac;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (npc != null) {
for (Integer minionUUID : npc.minions) {
Mob mob = Mob.getMob(minionUUID);
2022-04-30 09:41:17 -04:00
WorldGrid.RemoveWorldObject(mob);
WorldGrid.removeObject(mob, pc);
2024-05-11 07:51:56 -04:00
if (mob.getParentZone() != null) {
2022-04-30 09:41:17 -04:00
mob.getParentZone().zoneMobSet.remove(mob);
2024-05-11 07:51:56 -04:00
}
2022-04-30 09:41:17 -04:00
}
2022-04-30 09:41:17 -04:00
DbManager.NPCQueries.DELETE_NPC(npc);
DbManager.removeFromCache(GameObjectType.NPC,
npc.getObjectUUID());
WorldGrid.RemoveWorldObject(npc);
2023-07-15 09:23:48 -04:00
} else if (mobA != null) {
for (Integer minionUUID : mobA.minions) {
Mob mob = Mob.getMob(minionUUID);
2022-04-30 09:41:17 -04:00
WorldGrid.RemoveWorldObject(mob);
WorldGrid.removeObject(mob, pc);
2024-05-11 07:51:56 -04:00
if (mob.getParentZone() != null) {
2022-04-30 09:41:17 -04:00
mob.getParentZone().zoneMobSet.remove(mob);
2024-05-11 07:51:56 -04:00
}
2022-04-30 09:41:17 -04:00
}
DbManager.MobQueries.DELETE_MOB(mobA);
DbManager.removeFromCache(GameObjectType.Mob,
mobA.getObjectUUID());
WorldGrid.RemoveWorldObject(mobA);
}
}
DbManager.BuildingQueries.DELETE_FROM_DATABASE(building);
DbManager.removeFromCache(building);
WorldGrid.RemoveWorldObject(building);
WorldGrid.removeObject(building, pc);
}
}
// AbstractDevCmd Overridden methods
2023-07-15 09:23:48 -04:00
private static boolean validateUserInput(String[] userInput) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
int stringIndex;
String commandSet = "npcmobmeshall";
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// incorrect number of arguments test
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (userInput.length != 2)
return false;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Test of game object type argument
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
stringIndex = commandSet.indexOf(userInput[0].toLowerCase());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (stringIndex == -1)
return false;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Test if range argument can convert to a float
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
Float.parseFloat(userInput[1]);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// User input passes validation
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return true;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private static void removeBuilding(PlayerCharacter pc, Building building) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ((building.getBlueprintUUID() != 0) &&
(building.getBlueprint().getBuildingGroup() == BuildingGroup.TOL))
return;
if ((building.getBlueprintUUID() != 0) &&
(building.getBlueprint().getBuildingGroup() == BuildingGroup.SHRINE))
Shrine.RemoveShrineFromCacheByBuilding(building);
if ((building.getBlueprint() != null) && (building.getBlueprint().getBuildingGroup() == BuildingGroup.SPIRE))
building.disableSpire(false);
for (AbstractCharacter ac : building.getHirelings().keySet()) {
NPC npc = null;
Mob mobA = null;
if (ac.getObjectType() == GameObjectType.NPC)
npc = (NPC) ac;
else if (ac.getObjectType() == GameObjectType.Mob)
mobA = (Mob) ac;
if (npc != null) {
for (Integer minionUUID : npc.minions) {
Mob mob = Mob.getMob(minionUUID);
2023-07-15 09:23:48 -04:00
WorldGrid.RemoveWorldObject(mob);
WorldGrid.removeObject(mob, pc);
2024-05-11 07:51:56 -04:00
if (mob.getParentZone() != null) {
2023-07-15 09:23:48 -04:00
mob.getParentZone().zoneMobSet.remove(mob);
2024-05-11 07:51:56 -04:00
}
2023-07-15 09:23:48 -04:00
}
DbManager.NPCQueries.DELETE_NPC(npc);
DbManager.removeFromCache(mbEnums.GameObjectType.NPC,
2023-07-15 09:23:48 -04:00
npc.getObjectUUID());
WorldGrid.RemoveWorldObject(npc);
} else if (mobA != null) {
for (Integer minionUUID : mobA.minions) {
Mob mob = Mob.getMob(minionUUID);
2023-07-15 09:23:48 -04:00
WorldGrid.RemoveWorldObject(mob);
WorldGrid.removeObject(mob, pc);
2024-05-11 07:51:56 -04:00
if (mob.getParentZone() != null) {
2023-07-15 09:23:48 -04:00
mob.getParentZone().zoneMobSet.remove(mob);
2024-05-11 07:51:56 -04:00
}
2023-07-15 09:23:48 -04:00
}
DbManager.MobQueries.DELETE_MOB(mobA);
DbManager.removeFromCache(mbEnums.GameObjectType.Mob,
2023-07-15 09:23:48 -04:00
mobA.getObjectUUID());
WorldGrid.RemoveWorldObject(mobA);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
DbManager.BuildingQueries.DELETE_FROM_DATABASE(building);
DbManager.removeFromCache(building);
WorldGrid.RemoveWorldObject(building);
WorldGrid.removeObject(building, pc);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private static void removeNPC(PlayerCharacter pc, NPC npc) {
DbManager.NPCQueries.DELETE_NPC(npc);
DbManager.removeFromCache(npc);
WorldGrid.RemoveWorldObject(npc);
WorldGrid.removeObject(npc, pc);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Class methods
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private static void removeMob(PlayerCharacter pc, Mob mob) {
mob.setLoc(Vector3fImmutable.ZERO); //Move it off the plane..
mob.setBindLoc(Vector3fImmutable.ZERO); //Reset the bind loc..
//mob.setHealth(-1, pc); //Kill it!
DbManager.MobQueries.DELETE_MOB(mob);
DbManager.removeFromCache(mob);
WorldGrid.RemoveWorldObject(mob);
WorldGrid.removeObject(mob, pc);
}
@Override
protected void _doCmd(PlayerCharacter pc, String[] args,
AbstractGameObject target) {
// Grab write lock due to use of instance variables
lock.writeLock().lock();
try {
if (args[0].toLowerCase().equals("walls")) {
Zone zone = ZoneManager.findSmallestZone(pc.getLoc());
PurgeWalls(zone, pc);
return;
}
if (validateUserInput(args) == false) {
this.sendUsage(pc);
return;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
parseUserInput(args);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Arguments have been validated and parsed at this point
// Build array of requested objects
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
_currentLocation = pc.getLoc();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
HashSet<AbstractWorldObject> objectList =
WorldGrid.getObjectsInRangePartial(_currentLocation, _targetRange, _targetMask);
// Iterate through array and remove objects from game world and database
for (AbstractWorldObject awo : objectList) {
switch (awo.getObjectType()) {
case Building:
removeBuilding(pc, (Building) awo);
break;
case NPC:
removeNPC(pc, (NPC) awo);
break;
case Mob:
removeMob(pc, (Mob) awo);
break;
default:
break;
}
}
// Send results to user
throwbackInfo(pc, "Purge: " + objectList.size() + " objects were removed in range " + _targetRange);
} catch (Exception e) {
Logger.error(e);
}
// Release Reentrant lock
finally {
lock.writeLock().unlock();
}
}
@Override
protected String _getHelpString() {
return "Purges game objects within range";
}
@Override
protected String _getUsageString() {
return "/purge [npc|mob|mesh|all] [range <= 200]";
}
private void parseUserInput(String[] userInput) {
_targetMask = 0;
_targetRange = 0f;
// Build mask from user input
switch (userInput[0].toLowerCase()) {
case "npc":
_targetMask = MBServerStatics.MASK_NPC;
break;
case "mob":
_targetMask = MBServerStatics.MASK_MOB;
break;
case "mesh":
_targetMask = MBServerStatics.MASK_BUILDING;
break;
case "all":
_targetMask = MBServerStatics.MASK_NPC | MBServerStatics.MASK_MOB | MBServerStatics.MASK_BUILDING;
break;
default:
break;
}
// Parse second argument into range parameter. Cap at 200 units.
_targetRange = Float.parseFloat(userInput[1]);
_targetRange = Math.min(_targetRange, 200f);
}
2022-04-30 09:41:17 -04:00
}