use of co.paralleluniverse.data.record.Record in project spaceships-demo by puniverse.
the class Spaceship method chaseAndShoot.
private void chaseAndShoot() throws SuspendExecution, InterruptedException {
record(1, "Spaceship", "chaseAndShoot", "%s: locked", this);
// check lock range, chase, shoot
boolean foundLockedOn = false;
try (Element<Record<SpaceshipState>> target = global.sb.readElement(lockedOn)) {
final Record<SpaceshipState> lockedSpaceship;
if (target != null && (lockedSpaceship = target.get()) != null) {
foundLockedOn = true;
final AABB aabb = getAABB(lockedSpaceship);
// double angularDiversion = abs(atan2(lockedSpaceship.vx, lockedSpaceship.vy) - getCurrentHeading(shootTime));
if (inShotRange(aabb) & global.random.nextGaussian() < SHOOT_PROBABLITY) {
record(1, "Spaceship", "chaseAndShoot", "%s: shootrange", this);
final double range = mag(lockedSpaceship.get($x) - state.get($x), lockedSpaceship.get($y) - state.get($y));
final long now = global.now();
shoot(range, now);
lockedSpaceship.get($spaceship).send(new Shot(state.get($x), state.get($y)));
}
if (inLockRange(aabb)) {
record(1, "Spaceship", "chaseAndShoot", "%s: lockrange", this);
chase(lockedSpaceship);
} else {
record(1, "Spaceship", "chaseAndShoot", "%s: release lock", this);
// not in range, release lock
lockOnTarget(null);
}
}
}
if (!foundLockedOn)
lockOnTarget(null);
}
use of co.paralleluniverse.data.record.Record in project spaceships-demo by puniverse.
the class Spaceship method applyNeighborRejectionAndMove.
private void applyNeighborRejectionAndMove(final long now) throws InterruptedException, SuspendExecution {
record(1, "Spaceship", "applyNeighborRejectionAndMove", "%s", this);
AABB myAABB = getAABB();
try (ResultSet<Record<SpaceshipState>> rs = global.sb.queryForUpdate(SpatialQueries.range(myAABB, global.range), SpatialQueries.equals(state, myAABB), false)) {
// Debug.exit(1);
assert rs.getResultForUpdate().size() == 1;
ElementUpdater<Record<SpaceshipState>> updater = rs.getResultForUpdate().iterator().next();
// this is me
assert updater.elem().equals(state);
applyNeighborRejection(rs.getResultReadOnly(), now);
move(now);
state.set($status, status);
state.set($timeFired, timeFired);
state.set($shotLength, shotLength);
state.set($exVelocityUpdated, exVelocityUpdated);
updater.update(getAABB());
}
reduceExternalVelocity(now);
}
use of co.paralleluniverse.data.record.Record in project spaceships-demo by puniverse.
the class Spaceships method initSpaceBase.
/**
* reads properties file and creates a SpaceBase instance with the requested properties.
*/
private SpaceBase<Record<SpaceshipState>> initSpaceBase(Properties props) {
final boolean optimistic = Boolean.parseBoolean(props.getProperty("optimistic", "true"));
final int optimisticHeight = Integer.parseInt(props.getProperty("optimistic-height", "1"));
final int optimisticRetryLimit = Integer.parseInt(props.getProperty("optimistic-retry-limit", "3"));
final boolean compressed = Boolean.parseBoolean(props.getProperty("compressed", "false"));
final boolean singlePrecision = Boolean.parseBoolean(props.getProperty("single-precision", "false"));
final int nodeWidth = Integer.parseInt(props.getProperty("node-width", "10"));
println("SpaceBase properties");
println("Optimistic: " + optimistic);
println("Optimistic height: " + optimisticHeight);
println("Optimistic retry limit: " + optimisticRetryLimit);
println("Node width: " + nodeWidth);
println("Compressed: " + compressed);
println("Single precision: " + singlePrecision);
println();
SpaceBaseBuilder builder = new SpaceBaseBuilder();
if (glxNode > 0)
builder.setStore(GalaxyStore.class);
builder.setQueueBackpressure(1000);
if (optimistic)
builder.setOptimisticLocking(optimisticHeight, optimisticRetryLimit);
else
builder.setPessimisticLocking();
builder.setDimensions(2);
builder.setSinglePrecision(singlePrecision).setCompressed(compressed);
builder.setNodeWidth(nodeWidth);
builder.setMonitoringType(MonitorType.JMX);
if (metricsDir != null) {
com.codahale.metrics.CsvReporter.forRegistry(Metrics.registry()).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build(metricsDir).start(1, TimeUnit.SECONDS);
}
final SpaceBase<Record<SpaceshipState>> space = builder.build("base1");
if (glxNode > 0) {
GalaxyStore store = (GalaxyStore) space.getStore();
store.addMigrationListener(new MigrationListener<Record<SpaceshipState>>() {
@Override
public void immigrating(Record<SpaceshipState> record) {
try {
Actor.hire(record.get($spaceship));
} catch (SuspendExecution e) {
throw new AssertionError(e);
}
}
@Override
public void emigrating(Record<SpaceshipState> record) {
System.out.println("Spaceship leaving: " + record);
}
});
}
return space;
}
Aggregations