use of org.apache.commons.lang3.tuple.Triple in project sponge by softelnet.
the class BaseKnowledgeBaseInterpreter method getCustomRuleEventSpec.
/**
* Resolves event specification "<name> <alias> : <mode>". Uses default value when one not provided.
*
* @param eventSpecString event specification.
* @return rule event specification, i.e. a triple of (name, alias, mode).
*/
protected RuleEventSpec getCustomRuleEventSpec(String eventSpecString) {
if (eventSpecString == null) {
throw new SpongeException("Event specification is null");
}
List<String> mainList = Arrays.stream(eventSpecString.split(":")).map(s -> s.trim()).filter(s -> !s.isEmpty()).collect(Collectors.toList());
if (mainList.isEmpty()) {
throw new SpongeException("Event specification is empty");
} else if (mainList.size() > 2) {
throw new SpongeException("Event specification has too many elements separated by ':'");
}
ImmutablePair<String, String> nameAlias = resolveEventNameAndAlias(mainList.get(0));
EventMode eventMode = RuleAdapter.DEFAULT_MODE;
if (mainList.size() == 2) {
try {
eventMode = EventMode.valueOf(mainList.get(1).toUpperCase());
} catch (Exception e) {
throw new SpongeException("Event mode is incorrect: " + mainList.get(1));
}
}
return new GenericRuleEventSpec(nameAlias.getLeft(), nameAlias.getRight(), eventMode);
}
use of org.apache.commons.lang3.tuple.Triple in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class WorldPhysicsCollider method updatePotentialCollisionCache.
// TODO: The greatest physics lag starts here.
private void updatePotentialCollisionCache() {
ticksSinceCacheUpdate = 0D;
// in the same tick
if (Math.random() > .5) {
ticksSinceCacheUpdate -= .05D;
}
int oldSize = cachedPotentialHits.size();
// Resets the potential hits array in O(1) time! Isn't that something.
// cachedPotentialHits.resetQuick();
cachedPotentialHits.clear();
AxisAlignedBB shipBBOriginal = parent.getPhysicsTransformAABB();
if (shipBBOriginal == null) {
return;
}
final AxisAlignedBB shipBB = shipBBOriginal.grow(3);
// Use the physics tick collision box instead of the game tick collision box.
// We are using grow(3) on both because for some reason if we don't then ships start
// jiggling through the ground. God I can't wait for a new physics engine.
final AxisAlignedBB collisionBB = shipBB.grow(AABB_EXPANSION).expand(calculator.getLinearVelocity().x * .2, calculator.getLinearVelocity().y * .2, calculator.getLinearVelocity().z * .2);
// Ship is outside of world blockSpace, just skip this all togvalkyrium
if (collisionBB.maxY < 0 || collisionBB.minY > 255) {
return;
}
// Has a -1 on the minY value, I hope this helps with preventing things from
// falling through the floor
BlockPos min = new BlockPos(collisionBB.minX, Math.max(collisionBB.minY - 1, 0), collisionBB.minZ);
BlockPos max = new BlockPos(collisionBB.maxX, Math.min(collisionBB.maxY, 255), collisionBB.maxZ);
centerPotentialHit = new BlockPos((min.getX() + max.getX()) / 2D, (min.getY() + max.getY()) / 2D, (min.getZ() + max.getZ()) / 2D);
ChunkCache cache = parent.getCachedSurroundingChunks();
if (cache == null) {
System.err.println("VS Cached Surrounding Chunks was null! This is going to cause catastophric terrible events!!");
return;
}
int chunkMinX = min.getX() >> 4;
int chunkMaxX = (max.getX() >> 4) + 1;
int chunkMinZ = min.getZ() >> 4;
int chunkMaxZ = (max.getZ() >> 4) + 1;
// long startTime = System.nanoTime();
int minX = min.getX();
int minY = min.getY();
int minZ = min.getZ();
int maxX = max.getX();
int maxY = max.getY();
int maxZ = max.getZ();
// More multithreading!
if (VSConfig.MULTITHREADING_SETTINGS.multithreadCollisionCacheUpdate && parent.getBlockPositions().size() > 100) {
List<Triple<Integer, Integer, TIntList>> tasks = new ArrayList<>();
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
tasks.add(new ImmutableTriple<>(chunkX, chunkZ, new TIntArrayList()));
}
}
Consumer<Triple<Integer, Integer, TIntList>> consumer = i -> {
// i is a Tuple<Integer, Integer>
// updateCollisionCacheParrallel(cache, cachedPotentialHits, i.getFirst(),
// i.getSecond(), minX, minY, minZ, maxX, maxY, maxZ);
updateCollisionCacheSequential(cache, i.getLeft(), i.getMiddle(), minX, minY, minZ, maxX, maxY, maxZ, shipBB, i.getRight());
};
ValkyrienSkiesMod.getPhysicsThreadPool().submit(() -> tasks.parallelStream().forEach(consumer)).join();
tasks.forEach(task -> cachedPotentialHits.addAll(task.getRight()));
} else {
// Cast to double to avoid overflow errors
double size = ((double) (chunkMaxX - chunkMinX)) * ((double) (chunkMaxZ - chunkMinZ));
if (size > 300000) {
// Sanity check; don't execute the rest of the code because we'll just freeze the physics thread.
return;
}
// TODO: VS thread freezes here.
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
updateCollisionCacheSequential(cache, chunkX, chunkZ, minX, minY, minZ, maxX, maxY, maxZ, shipBB, cachedPotentialHits);
}
}
}
}
use of org.apache.commons.lang3.tuple.Triple in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class WorldWaterCollider method updatePotentialCollisionCache.
private void updatePotentialCollisionCache() {
secondsSinceCollisionCacheUpdate = 0;
// ships from all updating in the same tick
if (Math.random() > .5) {
secondsSinceCollisionCacheUpdate -= .01;
}
cachedPotentialHits.clear();
AxisAlignedBB shipBBOriginal = parent.getPhysicsTransformAABB();
if (shipBBOriginal == null) {
return;
}
// We are using grow(3) because its good.
final AxisAlignedBB shipBB = shipBBOriginal.grow(3);
final AxisAlignedBB collisionBB = shipBB.grow(AABB_EXPANSION).grow(2 * Math.ceil(RANGE_CHECK));
// Ship is outside of world blockSpace, just skip this
if (collisionBB.maxY < 0 || collisionBB.minY > 255) {
return;
}
// Has a -1 on the minY value, I hope this helps with preventing things from
// falling through the floor
final BlockPos min = new BlockPos(collisionBB.minX, Math.max(collisionBB.minY - 1, 0), collisionBB.minZ);
final BlockPos max = new BlockPos(collisionBB.maxX, Math.min(collisionBB.maxY, 255), collisionBB.maxZ);
centerPotentialHit = new BlockPos((min.getX() + max.getX()) / 2.0, (min.getY() + max.getY()) / 2.0, (min.getZ() + max.getZ()) / 2.0);
final ChunkCache cache = parent.getCachedSurroundingChunks();
if (cache == null) {
System.err.println("VS Cached Surrounding Chunks was null! This is going to cause catastophric terrible events!!");
return;
}
final int chunkMinX = min.getX() >> 4;
final int chunkMaxX = (max.getX() >> 4) + 1;
final int chunkMinZ = min.getZ() >> 4;
final int chunkMaxZ = (max.getZ() >> 4) + 1;
final int minX = min.getX();
final int minY = min.getY();
final int minZ = min.getZ();
final int maxX = max.getX();
final int maxY = max.getY();
final int maxZ = max.getZ();
// More multithreading!
if (VSConfig.MULTITHREADING_SETTINGS.multithreadCollisionCacheUpdate && parent.getBlockPositions().size() > 100) {
final List<Triple<Integer, Integer, TIntList>> tasks = new ArrayList<>();
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
tasks.add(new ImmutableTriple<>(chunkX, chunkZ, new TIntArrayList()));
}
}
Consumer<Triple<Integer, Integer, TIntList>> consumer = i -> updateCollisionCacheSequential(cache, i.getLeft(), i.getMiddle(), minX, minY, minZ, maxX, maxY, maxZ, shipBB, i.getRight());
ValkyrienSkiesMod.getPhysicsThreadPool().submit(() -> tasks.parallelStream().forEach(consumer)).join();
tasks.forEach(task -> cachedPotentialHits.addAll(task.getRight()));
} else {
// Cast to double to avoid overflow errors
final double size = ((double) (chunkMaxX - chunkMinX)) * ((double) (chunkMaxZ - chunkMinZ));
if (size > 300000) {
// Sanity check; don't execute the rest of the code because we'll just freeze the physics thread.
return;
}
for (int chunkX = chunkMinX; chunkX < chunkMaxX; chunkX++) {
for (int chunkZ = chunkMinZ; chunkZ < chunkMaxZ; chunkZ++) {
updateCollisionCacheSequential(cache, chunkX, chunkZ, minX, minY, minZ, maxX, maxY, maxZ, shipBB, cachedPotentialHits);
}
}
}
}
use of org.apache.commons.lang3.tuple.Triple in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.
the class EntityCollisionInjector method getLadderCollisions.
/**
* Returns all the possible ladders that the entity could potentially climb with
*/
public static Iterable<Triple<PhysicsObject, BlockPos, IBlockState>> getLadderCollisions(final EntityLivingBase entity, final List<PhysicsObject> collidingShips) {
final boolean isSpectator = entity instanceof EntityPlayer && ((EntityPlayer) entity).isSpectator();
final World world = entity.getEntityWorld();
final List<Triple<PhysicsObject, BlockPos, IBlockState>> ladderCollisions = new ArrayList<>();
if (!isSpectator) {
final AxisAlignedBB bb = entity.getEntityBoundingBox();
for (PhysicsObject physicsObject : collidingShips) {
final Polygon playerPolyInShip = new Polygon(bb, physicsObject.getShipTransform(), TransformType.GLOBAL_TO_SUBSPACE);
final AxisAlignedBB playerPolyInShipBB = playerPolyInShip.getEnclosedAABB();
int mX = MathHelper.floor(playerPolyInShipBB.minX);
int mY = MathHelper.floor(playerPolyInShipBB.minY);
int mZ = MathHelper.floor(playerPolyInShipBB.minZ);
for (int y2 = mY; (double) y2 < playerPolyInShipBB.maxY; ++y2) {
for (int x2 = mX; (double) x2 < playerPolyInShipBB.maxX; ++x2) {
for (int z2 = mZ; (double) z2 < playerPolyInShipBB.maxZ; ++z2) {
BlockPos tmp = new BlockPos(x2, y2, z2);
IBlockState state = world.getBlockState(tmp);
if (state.getBlock().isLadder(state, world, tmp, entity)) {
ladderCollisions.add(Triple.of(physicsObject, tmp, state));
}
}
}
}
}
}
return ladderCollisions;
}
use of org.apache.commons.lang3.tuple.Triple in project ImmersiveEngineering by BluSunrize.
the class ApiUtils method raytraceWires.
public static Connection raytraceWires(World world, Vec3d start, Vec3d end, @Nullable Connection ignored) {
Map<BlockPos, ImmersiveNetHandler.BlockWireInfo> inDim = ImmersiveNetHandler.INSTANCE.blockWireMap.lookup(world.provider.getDimension());
AtomicReference<Connection> ret = new AtomicReference<>();
AtomicDouble minDistSq = new AtomicDouble(Double.POSITIVE_INFINITY);
if (inDim != null) {
Utils.rayTrace(start, end, world, (pos) -> {
if (inDim.containsKey(pos)) {
ImmersiveNetHandler.BlockWireInfo info = inDim.get(pos);
for (int i = 0; i < 2; i++) {
Set<Triple<Connection, Vec3d, Vec3d>> conns = i == 0 ? info.in : info.near;
for (Triple<Connection, Vec3d, Vec3d> conn : conns) {
Connection c = conn.getLeft();
if (ignored == null || !c.hasSameConnectors(ignored)) {
Vec3d startRelative = start.add(-pos.getX(), -pos.getY(), -pos.getZ());
Vec3d across = conn.getRight().subtract(conn.getMiddle());
double t = Utils.getCoeffForMinDistance(startRelative, conn.getMiddle(), across);
t = MathHelper.clamp(0, t, 1);
Vec3d closest = conn.getMiddle().add(t * across.x, t * across.y, t * across.z);
double distSq = closest.squareDistanceTo(startRelative);
if (distSq < minDistSq.get()) {
ret.set(c);
minDistSq.set(distSq);
}
}
}
}
}
});
}
return ret.get();
}
Aggregations