void GameLoop_OneSecondUpdateTicked(object sender, StardewModdingAPI.Events.OneSecondUpdateTickedEventArgs e)
        {
            if (!CanUpdatePet())
            {
                return;
            }

            switch (petState)
            {
            case PetState.Vanilla:
                break;

            case PetState.CatchingUp:
            case PetState.Waiting:
            case PetState.Retrieve:

                if (petState == PetState.Waiting || !Compare(pet.Position, oldPetPos))
                {
                    oldPetPos = pet.Position;
                    SetTimer();
                }

                if (PlayerPetDistance() > catch_up_distance && Game1.currentLocation == pet.currentLocation)    // && (CurrentPath == null || CurrentPath.Count == 0))
                {
                    var oldpath = CurrentPath;
                    var path    = PathFinder.CalculatePath(pet, new Vector2(Game1.player.getTileX(), Game1.player.getTileY())); //TODO use player.Position instead?
                    CurrentPath = path;

                    if (CurrentPath.Count > 0)
                    {
                        if (oldpath == null || oldpath.Count == 0)
                        {
                            SetPetPositionFromTile(CurrentPath.Peek());     //this sets the pet on a proper tile. This is only done if the pet starts moving
                            //Log("Snapped pet position to first tile of current path");
                        }
                        else
                        {
                            CurrentPath.Dequeue();     //if pet is already moving: remove first node. Otherwise the pet might be a few pixels ahead and tries to move back first.
                        }

                        if (petState == PetState.Waiting)
                        {
                            SetState(PetState.CatchingUp);
                        }
                    }
                    else
                    {
                        CannotReachPlayer();
                    }
                }

                TryChaseCritterInRange();

                if (TimeOut(60))
                {
                    Log("timeout during " + petState);
                    Confused();
                    SetState(PetState.Waiting);
                }
                break;

            case PetState.Chasing:
            case PetState.Fetching:
            case PetState.WaitingToFetch:
                if (TimeOut())
                {
                    Log("timeout during " + petState);
                    Confused();
                    SetState(PetState.Waiting);
                }
                break;
            }
            if (petState != PetState.Vanilla)
            {
                GetPet().CurrentBehavior = PetBehaviour;
            }
        }
Beispiel #2
0
        public void Execute()
        {
            if (DateTime.Now - LastAction < TimeSpan.FromMilliseconds(250))
            {
                return;
            }

            LastAction = DateTime.Now;

            if ((DateTime.Now - LastLastPositionUpdate > TimeSpan.FromMilliseconds(1000) || TryCount > 2))
            {
                Reset();
                return;
            }

            if (CurrentPath.Count == 0)
            {
                TryCount = 0;
                List <Vector3> nodes = new List <Vector3>();

                if (WowInterface.ObjectManager.Player.Position.GetDistance(TargetPosition) > 5)
                {
                    List <Vector3> getPathResult = WowInterface.PathfindingHandler.GetPath((int)WowInterface.ObjectManager.MapId, WowInterface.ObjectManager.Player.Position, TargetPosition);

                    if (getPathResult != null && getPathResult.Count > 0)
                    {
                        nodes.AddRange(getPathResult);
                    }
                }
                else
                {
                    Vector3 moveAlongSurfaceResult = WowInterface.PathfindingHandler.MoveAlongSurface((int)WowInterface.ObjectManager.MapId, WowInterface.ObjectManager.Player.Position, TargetPosition);

                    if (moveAlongSurfaceResult != default && moveAlongSurfaceResult != Vector3.Zero)
                    {
                        nodes.Add(moveAlongSurfaceResult);
                    }
                }

                if (nodes == null || nodes.Count == 0)
                {
                    // pathfinding was unsuccessful
                    return;
                }

                foreach (Vector3 node in nodes)
                {
                    CurrentPath.Enqueue(node);
                }

                CurrentPathTargetPosition = TargetPosition;
            }

            List <Vector3> forces                   = new List <Vector3>();
            Vector3        currentPosition          = WowInterface.ObjectManager.Player.Position;
            Vector3        targetPosition           = CurrentPath.Peek();
            double         distanceToTargetPosition = currentPosition.GetDistance(targetPosition);

            if (distanceToTargetPosition > 4096)
            {
                Reset();
                return;
            }
            else if (distanceToTargetPosition < MovementSettings.WaypointCheckThreshold)
            {
                if (CurrentPath.Count > 0)
                {
                    targetPosition = CurrentPath.Dequeue();
                }
                else if (CurrentPath.Count == 0)
                {
                    return;
                }
            }

            Vector3 positionToGoTo = targetPosition;
            bool    updateForces   = true;

            switch (State)
            {
            case MovementEngineState.Moving:
                forces.Add(PlayerVehicle.Seek(positionToGoTo, 1));
                forces.Add(PlayerVehicle.AvoidObstacles(2));
                break;

            case MovementEngineState.DirectMoving:
                WowInterface.CharacterManager.MoveToPosition(targetPosition);
                updateForces = false;
                break;

            case MovementEngineState.Following:
                forces.Add(PlayerVehicle.Seek(positionToGoTo, 1));
                forces.Add(PlayerVehicle.Seperate(0.5f));
                forces.Add(PlayerVehicle.AvoidObstacles(2));
                break;

            case MovementEngineState.Chasing:
                forces.Add(PlayerVehicle.Seek(positionToGoTo, 1));
                break;

            case MovementEngineState.Fleeing:
                forces.Add(PlayerVehicle.Flee(positionToGoTo, 1));
                break;

            case MovementEngineState.Evading:
                forces.Add(PlayerVehicle.Evade(positionToGoTo, 1, TargetRotation));
                break;

            case MovementEngineState.Wandering:
                forces.Add(PlayerVehicle.Wander(1));
                break;

            case MovementEngineState.Stuck:
                forces.Add(PlayerVehicle.Unstuck(1));
                break;

            default:
                return;
            }

            if (DateTime.Now - LastJumpCheck > TimeSpan.FromMilliseconds(250))
            {
                double distanceTraveled = LastPosition.GetDistance(WowInterface.ObjectManager.Player.Position);
                if ((LastPosition.X == 0 && LastPosition.Y == 0 && LastPosition.Z == 0) || distanceTraveled < 0.01)
                {
                    ++TryCount;
                }
                else
                {
                    TryCount = 0;

                    // if (Straving)
                    // {
                    //     WowInterface.HookManager.LuaDoString("StrafeLeftStop();MoveBackwardStop();StrafeRightStop();MoveBackwardStop();");
                    //     Straving = false;
                    // }
                }

                // if the target position is higher than us, jump
                if (TryCount > 1 || (distanceToTargetPosition < 3 && currentPosition.Z + 2 < targetPosition.Z))
                {
                    WowInterface.CharacterManager.Jump();
                    TryCount = 0;

                    // if (DateTime.Now > StrafeEnd)
                    // {
                    //     int msToStrafe = Rnd.Next(1000, 5000);
                    //
                    //     if (Rnd.Next(0, 2) == 0)
                    //     {
                    //         WowInterface.HookManager.LuaDoString("StrafeLeftStart();MoveBackwardStart();");
                    //         Straving = true;
                    //     }
                    //     else
                    //     {
                    //         WowInterface.HookManager.LuaDoString("StrafeRightStart();MoveBackwardStart();");
                    //         Straving = true;
                    //     }
                    //
                    //     StrafeEnd = DateTime.Now + TimeSpan.FromMilliseconds(msToStrafe + 200);
                    // }
                }

                if (updateForces && !Straving)
                {
                    PlayerVehicle.Update(forces);
                }

                LastPosition           = WowInterface.ObjectManager.Player.Position;
                LastLastPositionUpdate = DateTime.Now;
                LastJumpCheck          = DateTime.Now;
            }

            LastTargetPosition = WowInterface.ObjectManager.Player.Position;
            HasMoved           = true;
        }
Beispiel #3
0
        public void Execute()
        {
            if (CurrentPath.Count == 0 || CurrentPathTargetPosition.GetDistance(TargetPosition) > 1)
            {
                List <Vector3> nodes = GeneratePath.Invoke(GetPosition.Invoke(), TargetPosition);

                if (nodes.Count == 0)
                {
                    // pathfinding was unsuccessful
                    return;
                }

                foreach (Vector3 node in nodes)
                {
                    CurrentPath.Enqueue(node);
                }

                CurrentPathTargetPosition = TargetPosition;
            }

            List <Vector3> forces                   = new List <Vector3>();
            Vector3        currentPosition          = GetPosition.Invoke();
            Vector3        targetPosition           = CurrentPath.Peek();
            double         distanceToTargetPosition = currentPosition.GetDistance2D(targetPosition);

            if (distanceToTargetPosition < MovementSettings.WaypointCheckThreshold)
            {
                if (CurrentPath.Count > 0)
                {
                    targetPosition = CurrentPath.Dequeue();
                }
                else if (CurrentPath.Count == 0)
                {
                    return;
                }
            }

            Vector3 positionToGoTo = MoveAhead(targetPosition, 1.5);

            switch (State)
            {
            case MovementEngineState.Moving:
                forces.Add(PlayerVehicle.Seek(positionToGoTo, 1));
                break;

            case MovementEngineState.Following:
                forces.Add(PlayerVehicle.Seek(positionToGoTo, 1));
                forces.Add(PlayerVehicle.Seperate(1));
                break;

            case MovementEngineState.Chasing:
                forces.Add(PlayerVehicle.Pursuit(positionToGoTo, 1, TargetRotation));
                break;

            case MovementEngineState.Fleeing:
                forces.Add(PlayerVehicle.Flee(positionToGoTo, 1));
                break;

            case MovementEngineState.Evading:
                forces.Add(PlayerVehicle.Evade(positionToGoTo, 1, TargetRotation));
                break;

            case MovementEngineState.Wandering:
                forces.Add(PlayerVehicle.Wander(1));
                break;

            case MovementEngineState.Stuck:
                forces.Add(PlayerVehicle.Unstuck(1));
                break;

            default:
                return;
            }

            // move
            PlayerVehicle.Update(forces);

            if (DateTime.Now - LastJumpCheck > TimeSpan.FromMilliseconds(500))
            {
                double distanceTraveled = LastPosition.GetDistance(GetPosition.Invoke());
                if ((LastPosition.X == 0 && LastPosition.Y == 0 && LastPosition.Z == 0) || distanceTraveled < 0.5)
                {
                    Jump.Invoke();
                }

                LastPosition  = GetPosition.Invoke();
                LastJumpCheck = DateTime.Now;
            }

            HasMoved = true;
        }
Beispiel #4
0
        void GameLoop_UpdateTicked(object sender, StardewModdingAPI.Events.UpdateTickedEventArgs e)
        {
            if (!CanUpdatePet())
            {
                return;
            }

            //if (pet.FacingDirection != PetBehaviorFacingDir)
            //    Monitor.Log("Game changed FacingDirection: " + PetBehaviorFacingDir + " -> " + GetPet().FacingDirection);

            if (TempRemovedTrashPet && GetPet() != null && !Game1.currentLocation.characters.Contains(pet))
            {
                Game1.currentLocation.characters.Add(pet);
                TempRemovedTrashPet = false;
            }

            switch (petState)
            {
            case PetState.Vanilla:
                break;

            case PetState.CatchingUp:
            case PetState.Chasing:
            case PetState.Fetching:
            case PetState.Retrieve:

                if (CurrentPath.Count > 0)
                {
                    CatchUp(FacingDirectionBeforeUpdate);
                }

                if (petState == PetState.Fetching && CurrentPath.Count == 4 && GetPet() is Cat cat)
                {
                    cat.leap(null);
                }

                int check_distance = petState == PetState.CatchingUp || petState == PetState.Retrieve ? 4 : 6;

                if (CurrentPath.Count == 0)
                {
                    if (petState == PetState.Retrieve)
                    {
                        DropItem();
                    }
                    else if (petState == PetState.Fetching)
                    {
                        PickUpItem();
                    }
                    else
                    {
                        if (petState == PetState.Chasing)
                        {
                            Jump();
                        }
                        SetState(PetState.Waiting);
                    }
                }
                else if (PetCurrentCatchUpGoalDistance() <= check_distance)    //next_path_pixel_distance) TODO
                {
                    //Vector2 velocityTowardsGoal = GetVelocity();
                    Vector2 goalPos = CurrentPath.Dequeue() * Game1.tileSize;
                    pet.Position = goalPos;
                }

                if (petState == PetState.CatchingUp && PlayerPetDistance() <= 2)
                {
                    SetState(PetState.Waiting);
                }
                if (petState == PetState.Retrieve && PlayerPetDistance() <= 1)
                {
                    DropItem();
                }
                break;

            case PetState.Waiting:
                break;
            }

            if (petState != PetState.Vanilla)
            {
                GetPet().CurrentBehavior = PetBehaviour;
            }
        }
Beispiel #5
0
        public override void Execute()
        {
            if (StateMachine.IsMeInCombat() || StateMachine.IsPartyInCombat())
            {
                StateMachine.SwitchState(typeof(BotStateCombat));
                return;
            }

            WowUnit player = ((WowUnit)StateMachine.ObjectManager.GetWowObjectByGuid(StateMachine.WowDataAdapter.PlayerGuid));

            if (UnitToFollow == null || UnitToFollow == player || !StateMachine.IsUnitInFollowRange(UnitToFollow))
            {
                StateMachine.SwitchState(typeof(BotStateIdle));
                return;
            }

            if (CurrentPath.Count == 0)
            {
                UpdatePath();
                if (CurrentPath.Count == 0)
                {
                    return;
                }
            }

            ActiveTargetPosition = CurrentPath.Peek();
            StateMachine.WowActionExecutor?.MoveToPosition(new WowPosition()
            {
                x = ActiveTargetPosition.x, //+ XOffset,
                y = ActiveTargetPosition.y, //+ YOffset,
                z = ActiveTargetPosition.z
            });

            // get position directly from memory
            WowPosition myPosition = StateMachine.WowDataAdapter.ActivePlayerPosition;

            if (BotMath.GetDistance(myPosition, CurrentPath.Peek()) < 6)
            {
                CurrentPath.Dequeue();
            }

            double distanceTraveled = BotMath.GetDistance(myPosition, LastPosition);

            AmeisenBotLogger.Instance.Log($"[{StateMachine.WowActionExecutor.ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tDistance traveled {distanceTraveled}m", LogLevel.Verbose);

            if (distanceTraveled > 0 && distanceTraveled < 0.1)
            {
                AmeisenBotLogger.Instance.Log($"[{StateMachine.WowActionExecutor.ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tTrying to unstuck", LogLevel.Verbose);
                float newX = (float)Math.Cos(myPosition.r + (Math.PI / 2)) + 4 + myPosition.x;
                float newY = (float)Math.Sin(myPosition.r + (Math.PI / 2)) + 4 + myPosition.y;
                StateMachine.WowActionExecutor?.MoveToPosition(new WowPosition()
                {
                    x = newX, y = newY, z = myPosition.z
                });
            }
            else if (distanceTraveled > 0 && distanceTraveled < 0.2)
            {
                AmeisenBotLogger.Instance.Log($"[{StateMachine.WowActionExecutor.ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tTrying to jump", LogLevel.Verbose);
                StateMachine.WowActionExecutor?.Jump();
            }

            LastPosition = myPosition;
        }