Search in sources :

Example 6 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class EntityMotionState method getWorldTransform.

@Override
public void getWorldTransform(Matrix4f transform) {
    LocationComponent loc = entity.getComponent(LocationComponent.class);
    transform.translationRotateScale(loc.getWorldPosition(position), loc.getWorldRotation(rot), loc.getWorldScale());
}
Also used : LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 7 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3ic blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(new Vector3f(blockPosition)));
    builder.addComponent(new BlockComponent(block, blockPosition));
    boolean isTemporary = isTemporaryBlock(builder, block);
    if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
        builder.addComponent(new NetworkComponent());
    }
    EntityRef blockEntity;
    if (isTemporary) {
        blockEntity = builder.buildWithoutLifecycleEvents();
        temporaryBlockEntities.add(blockEntity);
    } else {
        blockEntity = builder.build();
    }
    blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
    return blockEntity;
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 8 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class CharacterSystem method onAttackRequest.

@ReceiveEvent(components = LocationComponent.class, netFilter = RegisterMode.AUTHORITY)
public void onAttackRequest(AttackRequest event, EntityRef character, CharacterComponent characterComponent) {
    // if an item is used,  make sure this entity is allowed to attack with it
    if (event.getItem().exists()) {
        if (!character.equals(event.getItem().getOwner())) {
            return;
        }
    }
    OnItemUseEvent onItemUseEvent = new OnItemUseEvent();
    character.send(onItemUseEvent);
    if (!onItemUseEvent.isConsumed()) {
        EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(character);
        LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
        Vector3f direction = gazeLocation.getWorldDirection(new Vector3f());
        Vector3f originPos = gazeLocation.getWorldPosition(new Vector3f());
        if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
            directionAndOriginPosRecorderList.getAttackEventDirectionAndOriginPosRecorder().add(direction, originPos);
        } else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
            Vector3f[] data = directionAndOriginPosRecorderList.getAttackEventDirectionAndOriginPosRecorder().poll();
            direction = data[0];
            originPos = data[1];
        }
        HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange, Sets.newHashSet(character), DEFAULTPHYSICSFILTER);
        if (result.isHit()) {
            result.getEntity().send(new AttackEvent(character, event.getItem()));
        }
    }
}
Also used : HitResult(org.terasology.engine.physics.HitResult) OnItemUseEvent(org.terasology.engine.logic.characters.events.OnItemUseEvent) Vector3f(org.joml.Vector3f) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) AttackEvent(org.terasology.engine.logic.characters.events.AttackEvent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 9 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class CharacterSystem method isPredictionOfEventCorrect.

private boolean isPredictionOfEventCorrect(EntityRef character, ActivationRequest event) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    EntityRef camera = GazeAuthoritySystem.getGazeEntityForCharacter(character);
    LocationComponent location = camera.getComponent(LocationComponent.class);
    Vector3f direction = location.getWorldDirection(new Vector3f());
    if (!(event.getDirection().equals(direction, 0.0001f))) {
        logger.error("Direction at client {} was different than direction at server {}", event.getDirection(), direction);
    }
    // Assume the exact same value in case there are rounding mistakes:
    direction = event.getDirection();
    Vector3f originPos = location.getWorldPosition(new Vector3f());
    if (!(event.getOrigin().equals(originPos, 0.0001f))) {
        String msg = "Player {} seems to have cheated: It stated that it performed an action from {} but the predicted position is {}";
        logger.info(msg, getPlayerNameFromCharacter(character), event.getOrigin(), originPos);
        return false;
    }
    if (event.isOwnedEntityUsage()) {
        if (!event.getUsedOwnedEntity().exists()) {
            String msg = "Denied activation attempt by {} since the used entity does not exist on the authority";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
        if (!networkSystem.getOwnerEntity(event.getUsedOwnedEntity()).equals(networkSystem.getOwnerEntity(character))) {
            String msg = "Denied activation attempt by {} since it does not own the entity at the authority";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
    } else {
        // check for cheats so that data can later be trusted:
        if (event.getUsedOwnedEntity().exists()) {
            String msg = "Denied activation attempt by {} since it is not properly marked as owned entity usage";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
    }
    if (event.isEventWithTarget()) {
        if (!event.getTarget().exists()) {
            String msg = "Denied activation attempt by {} since the target does not exist on the authority";
            logger.info(msg, getPlayerNameFromCharacter(character));
            // can happen if target existed on client
            return false;
        }
        // FIXME This is the same code as in LocalPlayer#activateTargetOrOwnedEntity to derive the actual interaction range from the
        // player's character component and the used item's range component...
        float interactionRange;
        if (event.isOwnedEntityUsage() && event.getUsedOwnedEntity().hasComponent(RangeComponent.class)) {
            interactionRange = Math.max(event.getUsedOwnedEntity().getComponent(RangeComponent.class).range, characterComponent.interactionRange);
        } else {
            interactionRange = characterComponent.interactionRange;
        }
        HitResult result = physics.rayTrace(originPos, direction, interactionRange, Sets.newHashSet(character), DEFAULTPHYSICSFILTER);
        if (!result.isHit()) {
            String msg = "Denied activation attempt by {} since at the authority there was nothing to activate at that place";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
        EntityRef hitEntity = result.getEntity();
        if (!hitEntity.equals(event.getTarget())) {
            /**
             * Tip for debugging this issue: Obtain the network id of hit entity and search it in both client and
             * server entity dump. When certain fields don't get replicated, then wrong entity might get hin in the
             * hit test.
             */
            String msg = "Denied activation attempt by {} since at the authority another entity would have been activated";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
        if (!(event.getHitPosition().equals(result.getHitPoint(), 0.0001f))) {
            String msg = "Denied activation attempt by {} since at the authority the object got hit at a differnt position";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
    } else {
        // In order to trust the data later we need to verify it even if it should be correct if no one cheats:
        if (event.getTarget().exists()) {
            String msg = "Denied activation attempt by {} since the event was not properly labeled as having a target";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
        if (event.getHitPosition() != null) {
            String msg = "Denied activation attempt by {} since the event was not properly labeled as having a hit position";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
        if (event.getHitNormal() != null) {
            String msg = "Denied activation attempt by {} since the event was not properly labeled as having a hit delta";
            logger.info(msg, getPlayerNameFromCharacter(character));
            return false;
        }
    }
    return true;
}
Also used : HitResult(org.terasology.engine.physics.HitResult) RangeComponent(org.terasology.engine.logic.common.RangeComponent) Vector3f(org.joml.Vector3f) PlayerCharacterComponent(org.terasology.engine.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 10 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class KinematicCharacterMover method followToParent.

private void followToParent(final CharacterStateEvent state, EntityRef entity) {
    LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
    if (!locationComponent.getParent().equals(EntityRef.NULL)) {
        Vector3f position = locationComponent.getWorldPosition(new Vector3f());
        Vector3f velocity = new Vector3f(position);
        velocity.sub(state.getPosition());
        state.getVelocity().set(velocity);
        state.getPosition().set(position);
    }
}
Also used : Vector3f(org.joml.Vector3f) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Aggregations

LocationComponent (org.terasology.engine.logic.location.LocationComponent)65 Vector3f (org.joml.Vector3f)46 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)40 Quaternionf (org.joml.Quaternionf)18 ClientComponent (org.terasology.engine.network.ClientComponent)17 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)9 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)9 Matrix4f (org.joml.Matrix4f)7 Vector3i (org.joml.Vector3i)6 CharacterTeleportEvent (org.terasology.engine.logic.characters.CharacterTeleportEvent)5 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)5 Vector3fc (org.joml.Vector3fc)4 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)4 CharacterHeldItemComponent (org.terasology.engine.logic.characters.CharacterHeldItemComponent)4 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)4 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)4 DropItemEvent (org.terasology.engine.logic.inventory.events.DropItemEvent)4 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)4 BlockItemFactory (org.terasology.engine.world.block.items.BlockItemFactory)4 Component (org.terasology.gestalt.entitysystem.component.Component)4