Search in sources :

Example 1 with IHasShipManager

use of org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class EventsClient method onClientTick.

@SubscribeEvent
public void onClientTick(ClientTickEvent event) {
    final World world = Minecraft.getMinecraft().world;
    if (world == null) {
        // There's no world, so there's nothing to run.
        return;
    }
    // Pretend this is the world tick, because diesieben07 doesn't want WorldClient to make world tick events.
    switch(event.phase) {
        case START:
            for (PhysicsObject wrapper : ((IHasShipManager) world).getManager().getAllLoadedPhysObj()) {
                // This is necessary because Minecraft will run a raytrace right after this
                // event to determine what the player is looking at for interaction purposes.
                // That raytrace will use the render transform, so we must have the render
                // transform set to a partialTick of 1.0.
                wrapper.getShipTransformationManager().updateRenderTransform(1.0);
            }
            // Reset the air pocket status of all entities
            for (final Entity entity : world.loadedEntityList) {
                final IDraggable draggable = (IDraggable) entity;
                draggable.decrementTicksAirPocket();
            }
            break;
        case END:
            if (!Minecraft.getMinecraft().isGamePaused()) {
                // Tick the IShipManager on the world client.
                IHasShipManager shipManager = (IHasShipManager) world;
                shipManager.getManager().tick();
                EntityDraggable.tickAddedVelocityForWorld(world);
            }
            break;
    }
}
Also used : Entity(net.minecraft.entity.Entity) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager) IDraggable(org.valkyrienskies.mod.common.ships.entity_interaction.IDraggable) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) World(net.minecraft.world.World) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with IHasShipManager

use of org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinWorld method rayTraceBlocksForGetBlockDensity.

/**
 * Use Bresenham's tracing algorithm instead of raytracing for getBlockDensity, makes it way faster
 */
@Redirect(method = "getBlockDensity", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;rayTraceBlocks(Lnet/minecraft/util/math/Vec3d;Lnet/minecraft/util/math/Vec3d;)Lnet/minecraft/util/math/RayTraceResult;"))
private RayTraceResult rayTraceBlocksForGetBlockDensity(World world, Vec3d start, Vec3d end) {
    if (VSConfig.explosionMode == ExplosionMode.VANILLA) {
        // Vanilla raytrace, ignore ships and perform the function like normal
        this.shouldInterceptRayTrace = false;
        RayTraceResult result = rayTraceBlocks(start, end);
        this.shouldInterceptRayTrace = true;
        return result;
    } else if (VSConfig.explosionMode == ExplosionMode.SLOW_VANILLA) {
        // Vanilla raytrace, include ships and perform the function like normal
        return rayTraceBlocks(start, end);
    }
    java.util.function.Predicate<BlockPos> canCollide = pos -> {
        IBlockState blockState = world.getBlockState(pos);
        return blockState.getBlock().canCollideCheck(blockState, false);
    };
    // Get all the blocks between start and end
    List<BlockPos> blocks = VSMath.generateLineBetween(start, end, BlockPos::new);
    // Whether or not this ray trace hit a block that was collidable.
    boolean collided = blocks.stream().anyMatch(canCollide);
    IPhysObjectWorld physObjectWorld = ((IHasShipManager) (this)).getManager();
    if (physObjectWorld != null) {
        List<PhysicsObject> nearbyShips = physObjectWorld.getPhysObjectsInAABB(new AxisAlignedBB(start.x, start.y, start.z, end.x, end.y, end.z));
        for (PhysicsObject obj : nearbyShips) {
            Vec3d transformedStart = obj.transformVector(start, TransformType.GLOBAL_TO_SUBSPACE);
            Vec3d transformedEnd = obj.transformVector(end, TransformType.GLOBAL_TO_SUBSPACE);
            // Transform the raytrace into ship space and check whether or not it hit a block
            List<BlockPos> physoBlocks = VSMath.generateLineBetween(transformedStart, transformedEnd, BlockPos::new);
            collided |= physoBlocks.stream().anyMatch(canCollide);
        }
    }
    // important information in the ray trace result.
    return collided ? DUMMY_RAYTRACE_RESULT : null;
}
Also used : Polygon(org.valkyrienskies.mod.common.collision.Polygon) Inject(org.spongepowered.asm.mixin.injection.Inject) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager) IWorldEventListener(net.minecraft.world.IWorldEventListener) ShipPolygon(org.valkyrienskies.mod.common.collision.ShipPolygon) MixinWorldIntrinsicMethods(org.valkyrienskies.mod.fixes.MixinWorldIntrinsicMethods) Function(java.util.function.Function) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) ArrayList(java.util.ArrayList) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) Lists(com.google.common.collect.Lists) EntityPolygonCollider(org.valkyrienskies.mod.common.collision.EntityPolygonCollider) ExplosionMode(org.valkyrienskies.mod.common.config.VSConfig.ExplosionMode) org.spongepowered.asm.mixin(org.spongepowered.asm.mixin) Vector3d(org.joml.Vector3d) Chunk(net.minecraft.world.chunk.Chunk) Nullable(javax.annotation.Nullable) At(org.spongepowered.asm.mixin.injection.At) Entity(net.minecraft.entity.Entity) Iterator(java.util.Iterator) VSMath(org.valkyrienskies.mod.common.util.VSMath) net.minecraft.util.math(net.minecraft.util.math) World(net.minecraft.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect) TransformType(valkyrienwarfare.api.TransformType) EnumFacing(net.minecraft.util.EnumFacing) Remap(org.spongepowered.asm.mixin.Interface.Remap) Type(net.minecraft.util.math.RayTraceResult.Type) IBlockState(net.minecraft.block.state.IBlockState) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) List(java.util.List) ValkyrienUtils(org.valkyrienskies.mod.common.util.ValkyrienUtils) VSConfig(org.valkyrienskies.mod.common.config.VSConfig) Predicate(com.google.common.base.Predicate) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IWorldVS(org.valkyrienskies.mod.common.ships.ship_world.IWorldVS) Optional(java.util.Optional) IBlockState(net.minecraft.block.state.IBlockState) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 3 with IHasShipManager

use of org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class MixinWorld method rayTraceBlocksIgnoreShip.

@Override
public RayTraceResult rayTraceBlocksIgnoreShip(Vec3d vec31, Vec3d vec32, boolean stopOnLiquid, boolean ignoreBlockWithoutBoundingBox, boolean returnLastUncollidableBlock, PhysicsObject toIgnore) {
    this.shouldInterceptRayTrace = false;
    RayTraceResult vanillaTrace = World.class.cast(this).rayTraceBlocks(vec31, vec32, stopOnLiquid, ignoreBlockWithoutBoundingBox, returnLastUncollidableBlock);
    IPhysObjectWorld physObjectWorld = ((IHasShipManager) (this)).getManager();
    if (physObjectWorld == null) {
        return vanillaTrace;
    }
    Vec3d playerReachVector = vec32.subtract(vec31);
    AxisAlignedBB playerRangeBB = new AxisAlignedBB(vec31.x, vec31.y, vec31.z, vec32.x, vec32.y, vec32.z);
    List<PhysicsObject> nearbyShips = physObjectWorld.getPhysObjectsInAABB(playerRangeBB);
    // Get rid of the Ship that we're not supposed to be RayTracing for
    nearbyShips.remove(toIgnore);
    double reachDistance = playerReachVector.length();
    double worldResultDistFromPlayer = 420000000D;
    if (vanillaTrace != null && vanillaTrace.hitVec != null) {
        worldResultDistFromPlayer = vanillaTrace.hitVec.distanceTo(vec31);
    }
    for (PhysicsObject wrapper : nearbyShips) {
        Vec3d playerEyesPos = vec31;
        playerReachVector = vec32.subtract(vec31);
        ShipTransform shipTransform = wrapper.getShipTransformationManager().getRenderTransform();
        playerEyesPos = shipTransform.transform(playerEyesPos, TransformType.GLOBAL_TO_SUBSPACE);
        playerReachVector = shipTransform.rotate(playerReachVector, TransformType.GLOBAL_TO_SUBSPACE);
        Vec3d playerEyesReachAdded = playerEyesPos.add(playerReachVector.x * reachDistance, playerReachVector.y * reachDistance, playerReachVector.z * reachDistance);
        RayTraceResult resultInShip = World.class.cast(this).rayTraceBlocks(playerEyesPos, playerEyesReachAdded, stopOnLiquid, ignoreBlockWithoutBoundingBox, returnLastUncollidableBlock);
        if (resultInShip != null && resultInShip.hitVec != null && resultInShip.typeOfHit == Type.BLOCK) {
            double shipResultDistFromPlayer = resultInShip.hitVec.distanceTo(playerEyesPos);
            if (shipResultDistFromPlayer < worldResultDistFromPlayer) {
                worldResultDistFromPlayer = shipResultDistFromPlayer;
                // The hitVec must ALWAYS be in global coordinates.
                resultInShip.hitVec = shipTransform.transform(resultInShip.hitVec, TransformType.SUBSPACE_TO_GLOBAL);
                vanillaTrace = resultInShip;
            }
        }
    }
    this.shouldInterceptRayTrace = true;
    return vanillaTrace;
}
Also used : ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) World(net.minecraft.world.World) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)

Example 4 with IHasShipManager

use of org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class VSWorldPhysicsLoop method physicsTick.

private void physicsTick(double delta) {
    // Update the immutable ship list.
    immutableShipsList = ((IHasShipManager) hostWorld).getManager().getAllLoadedThreadSafe();
    // Run tasks queued to run on physics thread
    recurringTasks.forEach(t -> t.runTask(delta));
    taskQueue.forEach(Runnable::run);
    taskQueue.clear();
    // Make a sublist of physics objects to process physics on.
    List<PhysicsObject> physicsEntitiesToDoPhysics = new ArrayList<>();
    for (PhysicsObject physicsObject : immutableShipsList) {
        if (physicsObject.isPhysicsReady() && physicsObject.isPhysicsEnabled() && physicsObject.getCachedSurroundingChunks() != null) {
            physicsEntitiesToDoPhysics.add(physicsObject);
        }
    }
    // Finally, actually process the physics tick
    tickThePhysicsAndCollision(physicsEntitiesToDoPhysics, delta);
    // Send ship position update packets around 20 times a second
    final long currentTimeMillis = System.currentTimeMillis();
    final double secondsSinceLastPacket = (currentTimeMillis - lastPacketSendTime) / 1000.0;
    // Use .04 to guarantee we're always sending at least 20 packets per second
    if (secondsSinceLastPacket > .04) {
        // Update the last update time
        lastPacketSendTime = currentTimeMillis;
        try {
            // At the end, send the transform update packets
            final ShipTransformUpdateMessage shipTransformUpdateMessage = new ShipTransformUpdateMessage();
            final int dimensionID = hostWorld.provider.getDimension();
            shipTransformUpdateMessage.setDimensionID(dimensionID);
            for (final PhysicsObject physicsObject : immutableShipsList) {
                final UUID shipUUID = physicsObject.getUuid();
                final ShipTransform shipTransform = physicsObject.getShipTransformationManager().getCurrentPhysicsTransform();
                final AxisAlignedBB shipBB = physicsObject.getPhysicsTransformAABB();
                shipTransformUpdateMessage.addData(shipUUID, shipTransform, shipBB);
            }
            ValkyrienSkiesMod.physWrapperTransformUpdateNetwork.sendToDimension(shipTransformUpdateMessage, shipTransformUpdateMessage.getDimensionID());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ShipTransformUpdateMessage(org.valkyrienskies.mod.common.network.ShipTransformUpdateMessage) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) IHasShipManager(org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager)

Aggregations

IHasShipManager (org.valkyrienskies.mod.common.ships.ship_world.IHasShipManager)4 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)4 World (net.minecraft.world.World)3 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)3 IPhysObjectWorld (org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld)3 Entity (net.minecraft.entity.Entity)2 Predicate (com.google.common.base.Predicate)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EnumFacing (net.minecraft.util.EnumFacing)1 net.minecraft.util.math (net.minecraft.util.math)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1 Type (net.minecraft.util.math.RayTraceResult.Type)1