package electrosphere.entity.state.attack; import electrosphere.net.synchronization.BehaviorTreeIdEnums; import electrosphere.collision.collidable.Collidable; import electrosphere.engine.Globals; import electrosphere.engine.Main; import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.entity.ServerEntityUtils; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.state.collidable.Impulse; import electrosphere.entity.state.equip.ClientEquipState; import electrosphere.entity.state.movement.groundmove.ClientGroundMovementTree; import electrosphere.entity.state.rotator.RotatorTree; import electrosphere.entity.types.attach.AttachUtils; import electrosphere.entity.types.collision.CollisionObjUtils; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.entity.types.hitbox.HitboxUtils; import electrosphere.entity.types.item.ItemUtils; import electrosphere.entity.types.projectile.ProjectileUtils; import electrosphere.game.data.creature.type.attack.AttackMove; import electrosphere.game.data.creature.type.equip.EquipPoint; import electrosphere.net.parser.net.message.EntityMessage; import electrosphere.net.synchronization.annotation.SyncedField; import electrosphere.net.synchronization.annotation.SynchronizableEnum; import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; import electrosphere.renderer.actor.Actor; import electrosphere.renderer.anim.Animation; import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.utils.EntityLookupUtils; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.joml.Quaterniond; import org.joml.Quaternionf; import org.joml.Quaternionfc; import org.joml.Vector3d; import org.joml.Vector3f; @SynchronizedBehaviorTree(name = "clientAttackTree", isServer = false, correspondingTree="serverAttackTree") /** * Client basic attack tree */ public class ClientAttackTree implements BehaviorTree { @SynchronizableEnum public static enum AttackTreeState { WINDUP, HOLD, ATTACK, COOLDOWN, IDLE, } //the state of drifting forward during the attack @SynchronizableEnum public static enum AttackTreeDriftState { DRIFT, NO_DRIFT, } @SyncedField AttackTreeState state; @SyncedField AttackTreeDriftState driftState; Entity parent; CopyOnWriteArrayList networkMessageQueue = new CopyOnWriteArrayList(); long lastUpdateTime = 0; float frameCurrent; String animationName = "SwingWeapon"; int maxFrame = 60; List currentMoveset = null; AttackMove currentMove = null; @SyncedField String currentMoveId = null; Entity currentWeapon = null; boolean currentMoveHasWindup; boolean currentMoveCanHold; boolean stillHold = true; boolean firesProjectile = false; String projectileToFire = null; String attackingPoint = null; public ClientAttackTree(Entity e){ state = AttackTreeState.IDLE; driftState = AttackTreeDriftState.NO_DRIFT; parent = e; } /** *

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)){ parent.putData(EntityDataStrings.ATTACK_MOVE_TYPE_ACTIVE, attackType); currentMoveset = (List)parent.getData(attackType); if(currentMoveset != null){ Globals.clientConnection.queueOutgoingMessage(EntityMessage.constructstartAttackMessage()); } } } public void release(){ stillHold = false; } public void interrupt(){ state = AttackTreeState.IDLE; } public void slowdown(){ state = AttackTreeState.COOLDOWN; } @Override public void simulate(float deltaTime){ frameCurrent = frameCurrent + (float)Globals.timekeeper.getSimFrameTime(); float velocity = CreatureUtils.getVelocity(parent); Actor entityActor = EntityUtils.getActor(parent); Vector3d position = EntityUtils.getPosition(parent); Vector3d movementVector = CreatureUtils.getFacingVector(parent); //synchronize move from server if(this.currentMove == null && this.currentMoveId != null){ for(AttackMove move : currentMoveset){ if(move.getAttackMoveId().equals(currentMoveId)){ currentMove = move; } } } //parse attached network messages for(EntityMessage message : networkMessageQueue){ networkMessageQueue.remove(message); // System.out.println("MOVE to " + message.getX() + " " + message.getY() + " " + message.getZ()); long updateTime = message.gettime(); switch(message.getMessageSubtype()){ case ATTACKUPDATE: if(updateTime > lastUpdateTime){ lastUpdateTime = updateTime; switch(message.gettreeState()){ case 0: state = AttackTreeState.WINDUP; frameCurrent = 0; // System.out.println("Set state STARTUP"); break; case 1: frameCurrent = currentMove.getWindupFrames()+1; state = AttackTreeState.ATTACK; // System.out.println("Set state MOVE"); break; case 2: frameCurrent = currentMove.getWindupFrames()+currentMove.getAttackFrames()+1; state = AttackTreeState.COOLDOWN; // System.out.println("Set state SLOWDOWN"); break; case 3: frameCurrent = 60; state = AttackTreeState.IDLE; // System.out.println("Set state IDLE"); break; } } EntityUtils.getPosition(parent).set(message.getpositionX(),message.getpositionY(),message.getpositionZ()); CreatureUtils.setFacingVector(parent, new Vector3d(message.getrotationX(),message.getrotationY(),message.getrotationZ())); break; case ATTACHENTITYTOENTITY: case CREATE: case DESTROY: case MOVE: case MOVEUPDATE: case SETBEHAVIORTREE: case SETFACING: case SETPOSITION: case SETPROPERTY: case KILL: case SPAWNCREATURE: //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()){ driftState = AttackTreeDriftState.NO_DRIFT; } } break; case NO_DRIFT: if(currentMove != null){ if(frameCurrent > currentMove.getDriftFrameStart() && frameCurrent < currentMove.getDriftFrameEnd()){ driftState = AttackTreeDriftState.DRIFT; } } break; } // if(state != AttackTreeState.IDLE){ // System.out.println(frameCurrent); // } //state machine switch(state){ case WINDUP: if(parent.containsKey(EntityDataStrings.CLIENT_ROTATOR_TREE)){ RotatorTree.getClientRotatorTree(parent).setActive(true); } if(entityActor != null){ if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationName)){ entityActor.playAnimation(animationName,1); entityActor.incrementAnimationTime(0.0001); } } if(currentMove != null && frameCurrent > currentMove.getWindupFrames()){ if(currentMoveCanHold && stillHold){ state = AttackTreeState.HOLD; } else { state = AttackTreeState.ATTACK; } } break; case HOLD: if(entityActor != null){ if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationName)){ entityActor.playAnimation(animationName,1); entityActor.incrementAnimationTime(0.0001); } } if(!stillHold){ state = AttackTreeState.ATTACK; } break; case ATTACK: if(parent.containsKey(EntityDataStrings.ATTACH_CHILDREN_LIST)){ List attachedEntities = AttachUtils.getChildrenList(parent); for(Entity currentAttached : attachedEntities){ if(currentAttached.containsKey(EntityDataStrings.HITBOX_ASSOCIATED_LIST)){ List hitboxes = HitboxUtils.getHitboxAssociatedList(currentAttached); for(Entity hitbox : hitboxes){ HitboxUtils.getHitboxData(hitbox).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; ClientEquipState equipState = ClientEquipState.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(new Vector3f(0,0,1), 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(); ProjectileUtils.clientSpawnBasicProjectile(projectileToFire, spawnPosition, arrowRotation, 750, initialVector, 0.03f); projectileToFire = null; } if(currentMove != null && frameCurrent > currentMove.getWindupFrames() + currentMove.getAttackFrames()){ state = AttackTreeState.COOLDOWN; } break; case COOLDOWN: if(parent.containsKey(EntityDataStrings.ATTACH_CHILDREN_LIST)){ List attachedEntities = AttachUtils.getChildrenList(parent); for(Entity currentAttached : attachedEntities){ if(currentAttached.containsKey(EntityDataStrings.HITBOX_ASSOCIATED_LIST)){ List hitboxes = HitboxUtils.getHitboxAssociatedList(currentAttached); for(Entity hitbox : hitboxes){ HitboxUtils.getHitboxData(hitbox).setActive(false); } } } } if(currentMove != null && frameCurrent > currentMove.getWindupFrames() + currentMove.getAttackFrames() + currentMove.getCooldownFrames()){ state = AttackTreeState.IDLE; frameCurrent = 0; if(parent.containsKey(EntityDataStrings.CLIENT_ROTATOR_TREE)){ RotatorTree.getClientRotatorTree(parent).setActive(false); } } break; case IDLE: currentMove = null; currentMoveset = null; break; } } public void addNetworkMessage(EntityMessage networkMessage) { networkMessageQueue.add(networkMessage); } String getAttackType(){ String rVal = null; if(ClientEquipState.hasEquipState(parent)){ ClientEquipState equipState = ClientEquipState.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 "bow2h": rVal = EntityDataStrings.ATTACK_MOVE_TYPE_BOW_TWO_HAND; break; } } } } return rVal; } 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 != null && currentMove.getNextMoveId() != null && !currentMove.getNextMoveId().equals("") && frameCurrent >= currentMove.getMoveChainWindowStart() && frameCurrent <= currentMove.getMoveChainWindowEnd() ){ rVal = true; } } else { if(ClientEquipState.hasEquipState(parent)){ ClientEquipState equipState = ClientEquipState.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; } /** *

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; } /** *

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; } /** *

(initially) Automatically generated

*

More parameters can be safely added to this method

*

* Attaches this tree to the entity. *

* @param entity The entity to attach to * @param tree The behavior tree to attach */ public static ClientAttackTree attachTree(Entity parent){ ClientAttackTree rVal = new ClientAttackTree(parent); //put manual code here (setting params, etc) //!!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 parent.putData(EntityDataStrings.TREE_CLIENTATTACKTREE, rVal); Globals.clientScene.registerBehaviorTree(rVal); Globals.entityValueTrackingService.attachTreeToEntity(parent, BehaviorTreeIdEnums.BTREE_CLIENTATTACKTREE_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_CLIENTATTACKTREE_ID); } /** *

* Gets the ClientAttackTree of the entity *

* @param entity the entity * @return The ClientAttackTree */ public static ClientAttackTree getClientAttackTree(Entity entity){ return (ClientAttackTree)entity.getData(EntityDataStrings.TREE_CLIENTATTACKTREE); } /** *

Automatically generated

*

* Converts this enum type to an equivalent short value *

* @param enumVal The enum value * @return The short value */ public static short getAttackTreeStateEnumAsShort(AttackTreeState enumVal){ switch(enumVal){ case WINDUP: return 0; case HOLD: return 1; case ATTACK: return 2; case COOLDOWN: return 3; case IDLE: return 4; default: return 0; } } /** *

Automatically generated

*

* Converts a short to the equivalent enum value *

* @param shortVal The short value * @return The enum value */ public static AttackTreeState getAttackTreeStateShortAsEnum(short shortVal){ switch(shortVal){ case 0: return AttackTreeState.WINDUP; case 1: return AttackTreeState.HOLD; case 2: return AttackTreeState.ATTACK; case 3: return AttackTreeState.COOLDOWN; case 4: return AttackTreeState.IDLE; default: return AttackTreeState.WINDUP; } } /** *

Automatically generated

*

* Converts this enum type to an equivalent short value *

* @param enumVal The enum value * @return The short value */ public static short getAttackTreeDriftStateEnumAsShort(AttackTreeDriftState enumVal){ switch(enumVal){ case DRIFT: return 0; case NO_DRIFT: return 1; default: return 0; } } /** *

Automatically generated

*

* Converts a short to the equivalent enum value *

* @param shortVal The short value * @return The enum value */ public static AttackTreeDriftState getAttackTreeDriftStateShortAsEnum(short shortVal){ switch(shortVal){ case 0: return AttackTreeDriftState.DRIFT; case 1: return AttackTreeDriftState.NO_DRIFT; default: return AttackTreeDriftState.DRIFT; } } /** *

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; } }