package electrosphere.entity.state.attack; import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils; import electrosphere.net.parser.net.message.SynchronizationMessage; import electrosphere.server.datacell.utils.DataCellSearchUtils; import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.btree.StateTransitionUtil; import electrosphere.entity.btree.StateTransitionUtil.StateTransitionUtilItem; import electrosphere.entity.state.attack.ClientAttackTree.AttackTreeDriftState; import electrosphere.entity.state.attack.ClientAttackTree.AttackTreeState; import electrosphere.entity.state.collidable.Impulse; import electrosphere.entity.state.equip.ServerEquipState; import electrosphere.entity.state.hitbox.HitboxCollectionState; import electrosphere.entity.state.movement.groundmove.ServerGroundMovementTree; import electrosphere.entity.state.rotator.ServerRotatorTree; import electrosphere.entity.types.attach.AttachUtils; import electrosphere.entity.types.collision.CollisionObjUtils; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.entity.types.item.ItemUtils; import electrosphere.entity.types.projectile.ProjectileUtils; import electrosphere.game.data.common.TreeDataState; import electrosphere.game.data.creature.type.attack.AttackMove; import electrosphere.game.data.creature.type.equip.EquipPoint; import electrosphere.logger.LoggerInterface; import electrosphere.net.parser.net.message.EntityMessage; import electrosphere.net.synchronization.annotation.SyncedField; import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; import electrosphere.net.synchronization.enums.BehaviorTreeIdEnums; import electrosphere.net.synchronization.enums.FieldIdEnums; import electrosphere.renderer.actor.Actor; import electrosphere.server.datacell.Realm; import electrosphere.util.math.MathUtils; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.joml.Quaterniond; import org.joml.Vector3d; import org.joml.Vector3f; @SynchronizedBehaviorTree(name = "serverAttackTree", isServer = true, correspondingTree="clientAttackTree") /** * Server basic attack tree */ public class ServerAttackTree implements BehaviorTree { @SyncedField //the state of the attack tree AttackTreeState state; @SyncedField //the state of drifting caused by the attack animation AttackTreeDriftState driftState; Entity parent; CopyOnWriteArrayList networkMessageQueue = new CopyOnWriteArrayList(); long lastUpdateTime = 0; float frameCurrent; String animationName = "SwingWeapon"; int maxFrame = 60; List currentMoveset = null; @SyncedField String currentMoveId = null; //the id of the current move -- used to synchronize the move to client AttackMove currentMove = null; //the actual current move object Entity currentWeapon = null; boolean currentMoveHasWindup; boolean currentMoveCanHold; boolean stillHold = true; boolean firesProjectile = false; String projectileToFire = null; String attackingPoint = null; //The state transition util StateTransitionUtil stateTransitionUtil; private ServerAttackTree(Entity e, Object ... params){ state = AttackTreeState.IDLE; driftState = AttackTreeDriftState.NO_DRIFT; parent = e; this.stateTransitionUtil = StateTransitionUtil.create(parent, true, new StateTransitionUtilItem[]{ StateTransitionUtilItem.create( AttackTreeState.WINDUP, () -> { TreeDataState state = currentMove.getWindupState(); if(state == null){ return null; } else { return state.getAnimation(); } }, () -> { TreeDataState state = currentMove.getWindupState(); if(state == null){ return null; } else { return state.getAudioData(); } }, () -> { if(currentMoveCanHold && stillHold){ setState(AttackTreeState.HOLD); } else { setState(AttackTreeState.ATTACK); } this.stateTransitionUtil.interrupt(AttackTreeState.WINDUP); } ), StateTransitionUtilItem.create( AttackTreeState.HOLD, () -> { TreeDataState state = currentMove.getHoldState(); if(state == null){ return null; } else { return state.getAnimation(); } }, () -> { TreeDataState state = currentMove.getHoldState(); if(state == null){ return null; } else { return state.getAudioData(); } }, null ), StateTransitionUtilItem.create( AttackTreeState.ATTACK, () -> { TreeDataState state = currentMove.getAttackState(); if(state == null){ return null; } else { return state.getAnimation(); } }, null, () -> { this.setState(AttackTreeState.COOLDOWN); this.stateTransitionUtil.interrupt(AttackTreeState.ATTACK); } ), StateTransitionUtilItem.create( AttackTreeState.COOLDOWN, () -> { TreeDataState state = currentMove.getCooldownState(); if(state == null){ return null; } else { return state.getAnimation(); } }, null, null ), }); } /** *

Automatically generated

*

* Gets state. *

*/ public AttackTreeState getState(){ return state; } public void start(){ currentMoveCanHold = false; currentMoveHasWindup = false; stillHold = true; firesProjectile = false; projectileToFire = null; currentWeapon = null; attackingPoint = null; //figure out attack type we should be doing String attackType = getAttackType(); //if we can attack, setup doing so if(canAttack(attackType)){ setAttackMoveTypeActive(attackType); currentMoveset = getMoveset(attackType); if(currentMoveset != null){ if(currentMove == null){ currentMove = currentMoveset.get(0); } else { currentMove = getNextMove(currentMoveset,currentMove.getNextMoveId()); } if(currentMove != null){ firesProjectile = currentMove.getFiresProjectile(); if(firesProjectile){ projectileToFire = ItemUtils.getWeaponDataRaw(currentWeapon).getProjectileModel(); } //set initial stuff (this alerts the client as well) setCurrentMoveId(currentMove.getAttackMoveId()); //start tree if(currentMove.getWindupState() != null){ setState(AttackTreeState.WINDUP); } else if(currentMove.getAttackState() != null){ setState(AttackTreeState.ATTACK); } else { LoggerInterface.loggerEngine.ERROR(new IllegalStateException("Trying to start attacking tree, but current move does not have windup or attack states defined!")); } //intuit can hold from presence of windup anim currentMoveCanHold = currentMove.getHoldState() != null; //stop movement tree if(parent.containsKey(EntityDataStrings.SERVER_MOVEMENT_BT)){ BehaviorTree movementTree = CreatureUtils.serverGetEntityMovementTree(parent); if(movementTree instanceof ServerGroundMovementTree){ ((ServerGroundMovementTree)movementTree).interrupt(); } } Vector3d movementVector = CreatureUtils.getFacingVector(parent); EntityUtils.getRotation(parent).rotationTo(MathUtils.getOriginVector(), new Vector3d(movementVector.x,movementVector.y,movementVector.z)); frameCurrent = 0; } else { setState(AttackTreeState.IDLE); } } } } public void release(){ stillHold = false; } /** * Interrupts the tree */ public void interrupt(){ setState(AttackTreeState.IDLE); //activate hitboxes List attachedEntities = AttachUtils.getChildrenList(parent); for(Entity currentAttached : attachedEntities){ if(HitboxCollectionState.hasHitboxState(currentAttached)){ HitboxCollectionState currentState = HitboxCollectionState.getHitboxState(currentAttached); currentState.setActive(false); } } } @Override public void simulate(float deltaTime){ frameCurrent = frameCurrent + (float)Globals.timekeeper.getDeltaFrames(); float velocity = CreatureUtils.getVelocity(parent); Vector3d position = EntityUtils.getPosition(parent); Vector3d movementVector = CreatureUtils.getFacingVector(parent); //parse attached network messages for(EntityMessage message : networkMessageQueue){ networkMessageQueue.remove(message); switch(message.getMessageSubtype()){ case ATTACKUPDATE: EntityUtils.getPosition(parent).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); break; case STARTATTACK: { start(); } break; default: //silently ignore break; } } //handle the drifting if we're supposed to currently switch(driftState){ case DRIFT: if(currentMove != null){ //calculate the vector of movement CollisionObjUtils.getCollidable(parent).addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), currentMove.getDriftGoal() * Globals.timekeeper.getSimFrameTime(), "movement")); if(frameCurrent > currentMove.getDriftFrameEnd()){ setDriftState(AttackTreeDriftState.NO_DRIFT); } } break; case NO_DRIFT: if(currentMove != null){ if(frameCurrent > currentMove.getDriftFrameStart() && frameCurrent < currentMove.getDriftFrameEnd()){ setDriftState(AttackTreeDriftState.DRIFT); } } break; } // if(state != AttackTreeState.IDLE){ // System.out.println(frameCurrent); // } //state machine switch(state){ case WINDUP: { if(parent.containsKey(EntityDataStrings.SERVER_ROTATOR_TREE)){ ServerRotatorTree.getServerRotatorTree(parent).setActive(true); } this.stateTransitionUtil.simulate(AttackTreeState.WINDUP); Globals.server.broadcastMessage( EntityMessage.constructattackUpdateMessage( parent.getId(), System.currentTimeMillis(), (float)position.x, (float)position.y, (float)position.z, movementVector.x, movementVector.y, movementVector.z, velocity, 0 ) ); } break; case HOLD: { this.stateTransitionUtil.simulate(AttackTreeState.HOLD); if(!stillHold){ setState(AttackTreeState.ATTACK); this.stateTransitionUtil.interrupt(AttackTreeState.HOLD); } } break; case ATTACK: { this.stateTransitionUtil.simulate(AttackTreeState.ATTACK); //activate hitboxes List attachedEntities = AttachUtils.getChildrenList(parent); for(Entity currentAttached : attachedEntities){ if(HitboxCollectionState.hasHitboxState(currentAttached)){ HitboxCollectionState currentState = HitboxCollectionState.getHitboxState(currentAttached); currentState.setActive(true); } } if(firesProjectile && projectileToFire != null){ //spawn projectile //TODO: solve spawnPosition, initialVector Vector3d spawnPosition = new Vector3d(0,0,0); Quaterniond arrowRotation = new Quaterniond(); String targetBone = null; ServerEquipState equipState = ServerEquipState.getEquipState(parent); EquipPoint weaponPoint = null; if((weaponPoint = equipState.getEquipPoint(attackingPoint)) != null){ targetBone = weaponPoint.getBone(); } if(targetBone != null){ Actor parentActor = EntityUtils.getActor(parent); //transform bone space spawnPosition = new Vector3d(parentActor.getBonePosition(targetBone)); spawnPosition = spawnPosition.mul(((Vector3f)EntityUtils.getScale(parent))); Quaterniond rotation = EntityUtils.getRotation(parent); spawnPosition = spawnPosition.rotate(new Quaterniond(rotation.x,rotation.y,rotation.z,rotation.w)); //transform worldspace spawnPosition.add(new Vector3d(EntityUtils.getPosition(parent))); //set // EntityUtils.getPosition(currentEntity).set(position); //set rotation // Quaternionf rotation = parentActor.getBoneRotation(targetBone); // EntityUtils.getRotation(currentEntity).set(rotation).normalize(); // Vector3d facingAngle = CreatureUtils.getFacingVector(parent); arrowRotation = parentActor.getBoneRotation(targetBone); // EntityUtils.getRotation(currentEntity).rotationTo(MathUtils.ORIGIN_VECTORF, new Vector3f((float)facingAngle.x,(float)facingAngle.y,(float)facingAngle.z)).mul(parentActor.getBoneRotation(targetBone)).normalize(); } Vector3f initialVector = new Vector3f((float)movementVector.x,(float)movementVector.y,(float)movementVector.z).normalize(); Realm parentRealm = Globals.realmManager.getEntityRealm(parent); ProjectileUtils.serverSpawnBasicProjectile(parentRealm, projectileToFire, spawnPosition, arrowRotation, 750, initialVector, 0.03f); projectileToFire = null; } Globals.server.broadcastMessage( EntityMessage.constructattackUpdateMessage( parent.getId(), System.currentTimeMillis(), (float)position.x, (float)position.y, (float)position.z, movementVector.x, movementVector.y, movementVector.z, velocity, 1 ) ); } break; case COOLDOWN: { this.stateTransitionUtil.simulate(AttackTreeState.COOLDOWN); //deactive hitboxes List attachedEntities = AttachUtils.getChildrenList(parent); for(Entity currentAttached : attachedEntities){ if(HitboxCollectionState.hasHitboxState(currentAttached)){ HitboxCollectionState currentState = HitboxCollectionState.getHitboxState(currentAttached); currentState.setActive(false); } } if(frameCurrent > currentMove.getWindupFrames() + currentMove.getAttackFrames() + currentMove.getCooldownFrames()){ setState(AttackTreeState.IDLE); this.stateTransitionUtil.interrupt(AttackTreeState.COOLDOWN); frameCurrent = 0; if(parent.containsKey(EntityDataStrings.SERVER_ROTATOR_TREE)){ ServerRotatorTree.getServerRotatorTree(parent).setActive(false); } } Globals.server.broadcastMessage( EntityMessage.constructattackUpdateMessage( parent.getId(), System.currentTimeMillis(), (float)position.x, (float)position.y, (float)position.z, movementVector.x, movementVector.y, movementVector.z, velocity, 2 ) ); } break; case IDLE: { currentMove = null; currentMoveset = null; } break; } } public void addNetworkMessage(EntityMessage networkMessage) { networkMessageQueue.add(networkMessage); } String getAttackType(){ String rVal = null; if(ServerEquipState.hasEquipState(parent)){ ServerEquipState equipState = ServerEquipState.getEquipState(parent); for(String point : equipState.equippedPoints()){ Entity item = equipState.getEquippedItemAtPoint(point); if(ItemUtils.isWeapon(item)){ attackingPoint = point; currentWeapon = item; switch(ItemUtils.getWeaponClass(item)){ case "sword1h": rVal = EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND; break; case "sword2h": rVal = EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_TWO_HAND; break; case "bow2h": rVal = EntityDataStrings.ATTACK_MOVE_TYPE_BOW_TWO_HAND; break; } } } } return rVal; } /** * Checks whether the entity can attack or not * @param attackType The type of attack to perform * @return true if it can attack, false otherwise */ boolean canAttack(String attackType){ boolean rVal = true; if(attackType == null){ return false; } else if(state != AttackTreeState.IDLE){ //checks if we have a next move and if we're in the specified range of frames when we're allowed to chain into it if( currentMove.getNextMoveId() != null && !currentMove.getNextMoveId().equals("") && frameCurrent >= currentMove.getMoveChainWindowStart() && frameCurrent <= currentMove.getMoveChainWindowEnd() ){ rVal = true; } } else { if(ServerEquipState.hasEquipState(parent)){ // ServerEquipState equipState = ServerEquipState.getEquipState(parent); // if(equipState.hasEquipPrimary()){ // switch(attackType){ // case EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND: // break; // default: // rVal = false; // break; // } // } else { // switch(attackType){ // case EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND: // rVal = false; // break; // default: // rVal = false; // break; // } // } } } return rVal; } AttackMove getNextMove(List moveset, String nextMoveId){ AttackMove rVal = null; for(AttackMove move : moveset){ if(move.getAttackMoveId().equals(nextMoveId)){ rVal = move; break; } } return rVal; } /** * Sets the current attack type of the entity * @param attackType the current attack type */ public void setAttackMoveTypeActive(String attackType){ parent.putData(EntityDataStrings.ATTACK_MOVE_TYPE_ACTIVE, attackType); } /** * Checks whether the attack tree is active or not * @return true if active, false otherwise */ public boolean isAttacking(){ return this.state != AttackTreeState.IDLE; } /** * Gets the current moveset * @param attackType the attack type * @return The moveset if it exists */ public List getMoveset(String attackType){ return (List)parent.getData(attackType); } /** *

Automatically generated

*

* Sets state and handles the synchronization logic for it. *

* @param state The value to set state to. */ public void setState(AttackTreeState state){ this.state = state; int value = ClientAttackTree.getAttackTreeStateEnumAsShort(state); DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStateMessage(parent.getId(), BehaviorTreeIdEnums.BTREE_SERVERATTACKTREE_ID, FieldIdEnums.TREE_SERVERATTACKTREE_SYNCEDFIELD_STATE_ID, value)); } /** *

Automatically generated

*

* Gets driftState. *

*/ public AttackTreeDriftState getDriftState(){ return driftState; } /** *

Automatically generated

*

* Sets driftState and handles the synchronization logic for it. *

* @param driftState The value to set driftState to. */ public void setDriftState(AttackTreeDriftState driftState){ this.driftState = driftState; int value = ClientAttackTree.getAttackTreeDriftStateEnumAsShort(driftState); DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStateMessage(parent.getId(), BehaviorTreeIdEnums.BTREE_SERVERATTACKTREE_ID, FieldIdEnums.TREE_SERVERATTACKTREE_SYNCEDFIELD_DRIFTSTATE_ID, value)); } /** *

(initially) Automatically generated

*

* Attaches this tree to the entity. *

* @param entity The entity to attach to * @param tree The behavior tree to attach * @param params Optional parameters that will be provided to the constructor */ public static ServerAttackTree attachTree(Entity parent, Object ... params){ ServerAttackTree rVal = new ServerAttackTree(parent,params); //!!WARNING!! from here below should not be touched //This was generated automatically to properly alert various systems that the btree exists and should be tracked ServerBehaviorTreeUtils.attachBTreeToEntity(parent, rVal); parent.putData(EntityDataStrings.TREE_SERVERATTACKTREE, rVal); Globals.entityValueTrackingService.attachTreeToEntity(parent, BehaviorTreeIdEnums.BTREE_SERVERATTACKTREE_ID); return rVal; } /** *

Automatically generated

*

* Detatches this tree from the entity. *

* @param entity The entity to detach to * @param tree The behavior tree to detach */ public static void detachTree(Entity entity, BehaviorTree tree){ Globals.entityValueTrackingService.detatchTreeFromEntity(entity, BehaviorTreeIdEnums.BTREE_SERVERATTACKTREE_ID); } /** *

* Gets the ServerAttackTree of the entity *

* @param entity the entity * @return The ServerAttackTree */ public static ServerAttackTree getServerAttackTree(Entity entity){ return (ServerAttackTree)entity.getData(EntityDataStrings.TREE_SERVERATTACKTREE); } /** *

Automatically generated

*

* Gets currentMoveId. *

*/ public String getCurrentMoveId(){ return currentMoveId; } /** *

Automatically generated

*

* Sets currentMoveId and handles the synchronization logic for it. *

* @param currentMoveId The value to set currentMoveId to. */ public void setCurrentMoveId(String currentMoveId){ this.currentMoveId = currentMoveId; DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStringStateMessage(parent.getId(), BehaviorTreeIdEnums.BTREE_SERVERATTACKTREE_ID, FieldIdEnums.TREE_SERVERATTACKTREE_SYNCEDFIELD_CURRENTMOVEID_ID, currentMoveId)); } }