Search in sources :

Example 46 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class CharacterSystem method getInstigatorName.

/**
 * Extracts the name from an entity.
 * If the entity is a character, then the display name from the {@link ClientComponent#clientInfo} is used.
 * Otherwise the entity itself is checked for a {@link DisplayNameComponent}.
 * In the last case, the prefab name of the entity is used, e.g. "engine:player" will be parsed to "Player".
 *
 * @param instigator The entity for which an instigator name is needed.
 * @return The instigator name.
 */
public String getInstigatorName(EntityRef instigator) {
    if (instigator.hasComponent(CharacterComponent.class)) {
        EntityRef instigatorClient = instigator.getComponent(CharacterComponent.class).controller;
        EntityRef instigatorClientInfo = instigatorClient.getComponent(ClientComponent.class).clientInfo;
        DisplayNameComponent displayNameComponent = instigatorClientInfo.getComponent(DisplayNameComponent.class);
        return displayNameComponent.name;
    } else if (instigator.getParentPrefab() != null) {
        // A DisplayName can be specified in the entity prefab
        // Otherwise, the game will attempt to generate one from the name of that prefab
        Prefab parentPrefab = instigator.getParentPrefab();
        if (parentPrefab.hasComponent(DisplayNameComponent.class)) {
            DisplayNameComponent displayNameComponent = parentPrefab.getComponent(DisplayNameComponent.class);
            return displayNameComponent.name;
        } else {
            String instigatorName = parentPrefab.getName();
            // getParentPrefab.getName() returns a ResourceUrn String such as "engine:player"
            // The following calls change the damage type to be more readable
            // For instance, "engine:player" becomes "Player"
            instigatorName = instigatorName.replaceAll(".*:(.*)", "$1");
            instigatorName = Character.toUpperCase(instigatorName.charAt(0)) + instigatorName.substring(1);
            return instigatorName;
        }
    } else {
        return null;
    }
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.engine.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 47 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef 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 48 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class VisualCharacterSystem method createVisualCharacterIfNotOwnCharacter.

void createVisualCharacterIfNotOwnCharacter(EntityRef characterEntity, VisualCharacterComponent visualCharacterComponent) {
    boolean isCharacterOfLocalPlayer = characterEntity.getOwner().equals(localPlayer.getClientEntity());
    if (isCharacterOfLocalPlayer) {
        return;
    }
    CreateVisualCharacterEvent event = new CreateVisualCharacterEvent(entityManager.newBuilder());
    characterEntity.send(event);
    EntityBuilder entityBuilder = event.getVisualCharacterBuilder();
    EntityRef visualCharacterEntity = createAndAttachVisualEntityStrategy.createAndAttachVisualEntity(entityBuilder, characterEntity);
    visualCharacterComponent.visualCharacter = visualCharacterEntity;
    characterEntity.saveComponent(visualCharacterComponent);
}
Also used : EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) CreateVisualCharacterEvent(org.terasology.engine.logic.characters.events.CreateVisualCharacterEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 49 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BehaviorEditorScreen method onEntitySelected.

private void onEntitySelected(Interpreter value, PropertyProvider provider) {
    if (selectedInterpreter != null) {
        selectedInterpreter.setCallback(null);
    }
    selectedInterpreter = value;
    if (selectedInterpreter != null) {
        EntityRef entity = value.actor().getEntity();
        onTreeSelected(selectedInterpreter.getTree());
        entityProperties.clear();
        for (Component component : entity.iterateComponents()) {
            String name = component.getClass().getSimpleName().replace("Component", "");
            List<Property<?, ?>> componentProperties = provider.createProperties(component);
            entityProperties.addProperties(name, componentProperties);
        }
        selectedInterpreter.setCallback(behaviorEditor);
    }
}
Also used : BehaviorComponent(org.terasology.engine.logic.behavior.BehaviorComponent) Component(org.terasology.gestalt.entitysystem.component.Component) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Property(org.terasology.nui.properties.Property)

Example 50 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BehaviorEditorScreen method onAssignPressed.

private void onAssignPressed() {
    if (selectedTree != null && selectedInterpreter != null) {
        EntityRef minion = selectedInterpreter.actor().getEntity();
        minion.removeComponent(BehaviorComponent.class);
        BehaviorComponent component = new BehaviorComponent();
        component.tree = selectedTree;
        minion.addComponent(component);
        List<Interpreter> interpreter = behaviorSystem.getInterpreters();
        selectEntity.setSelection(null);
        for (Interpreter i : interpreter) {
            if (i.actor().getEntity() == minion) {
                selectEntity.setSelection(i);
                break;
            }
        }
    }
}
Also used : Interpreter(org.terasology.engine.logic.behavior.Interpreter) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BehaviorComponent(org.terasology.engine.logic.behavior.BehaviorComponent)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)298 Test (org.junit.jupiter.api.Test)88 ClientComponent (org.terasology.engine.network.ClientComponent)55 Vector3f (org.joml.Vector3f)51 LocationComponent (org.terasology.engine.logic.location.LocationComponent)44 Vector3i (org.joml.Vector3i)36 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)34 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)29 StringComponent (org.terasology.unittest.stubs.StringComponent)26 NetworkComponent (org.terasology.engine.network.NetworkComponent)23 EntityData (org.terasology.protobuf.EntityData)23 Quaternionf (org.joml.Quaternionf)19 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)19 Component (org.terasology.gestalt.entitysystem.component.Component)19 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)15 Map (java.util.Map)14 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)13 BlockComponent (org.terasology.engine.world.block.BlockComponent)13 Block (org.terasology.engine.world.block.Block)11 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)10