Files
prestonbane/src/engine/devcmd/cmds/SplatMobCmd.java
T

165 lines
4.0 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
package engine.devcmd.cmds;
import engine.devcmd.AbstractDevCmd;
import engine.gameManager.ZoneManager;
import engine.math.Vector3fImmutable;
import engine.objects.AbstractGameObject;
import engine.objects.Mob;
import engine.objects.PlayerCharacter;
import engine.objects.Zone;
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 create multiple
* mobiles of a given UUID within a supplied range
2022-04-30 09:41:17 -04:00
*/
public class SplatMobCmd 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 int _mobileUUID;
private int _mobileCount;
private float _targetRange;
private Vector3fImmutable _currentLocation;
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 SplatMobCmd() {
2022-04-30 09:41:17 -04:00
super("splatmob");
}
2023-07-15 09:23:48 -04:00
// AbstractDevCmd Overridden methods
2022-04-30 09:41:17 -04:00
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
// incorrect number of arguments test
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (userInput.length != 3)
return false;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Test for UUID conversion to int
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
Integer.parseInt(userInput[0]);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Test for Number of Mobs conversion to int
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
Integer.parseInt(userInput[1]);
} catch (NumberFormatException | NullPointerException e) {
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[2]);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
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
@Override
protected void _doCmd(PlayerCharacter pc, String[] args,
AbstractGameObject target) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Member variables
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Vector3fImmutable mobileLocation;
Mob mobile;
Zone serverZone;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Concurrency write lock due to instance variable usage
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
lock.writeLock().lock();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Validate user input
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (validateUserInput(args) == false) {
this.sendUsage(pc);
return;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Parse user input
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
// Begin creating mobiles
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
_currentLocation = pc.getLoc();
serverZone = ZoneManager.findSmallestZone(_currentLocation);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
for (int i = 0; i < _mobileCount; i++) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
mobile = Mob.createMob(_mobileUUID,
Vector3fImmutable.getRandomPointInCircle(_currentLocation, _targetRange),
null, true, serverZone, null, 0, "", 1);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (mobile != null) {
mobile.updateDatabase();
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} // End Try Block
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Release Reentrant lock
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
finally {
lock.writeLock().unlock();
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
@Override
protected String _getHelpString() {
return "Spawns multiple mobiles with a given range";
}
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
@Override
protected String _getUsageString() {
return "/splatmob UUID [Count <= 100] [range <= 1200]";
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private void parseUserInput(String[] userInput) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Clear previous values
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
_mobileUUID = 0;
_mobileCount = 0;
_targetRange = 0f;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Parse first argument into mobile UID.
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
_mobileUUID = Integer.parseInt(userInput[0]);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Parse second argument into mobile count. Cap at 100 mobs.
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
_mobileCount = Integer.parseInt(userInput[1]);
_mobileCount = Math.min(_mobileCount, 100);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Parse third argument into range. Cap at 200 units.
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
_targetRange = Float.parseFloat(userInput[2]);
_targetRange = Math.min(_targetRange, 1200f);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
}