Unfinished Basic AutoHunting?

08/22/2024 14:09 TheFadedOne#1
Here you go guys, it was a simple system i was attempting to make. But i couldent figure out the Dcells and So it would walk over parts of the map it shouldn't. Maybe one of you guys could do something with it. enjoy lol.
Code:
using COServer.Structures;
using System;
using System.Collections.Generic;

namespace COServer.Game
{
    public class AutoHuntSystem
    {
        private Character Player;
        private Mob CurrentMob;

        public AutoHuntSystem(Character player, Mob mob)
        {
            Player = player;
            CurrentMob = mob;
        }

        // Your other class members go here

        public void AutoHunt()
        {
            while (true) // You may want to implement some exit condition
            {
                // Detect nearby mobs
                List<Mob> nearbyMobs = DetectNearbyMobs();

                // Attack mobs if found
                if (nearbyMobs.Count > 0)
                {
                    Mob targetMob = SelectTargetMob(nearbyMobs);
                    ApproachAndAttackMob(targetMob);
                }

                // Implement other actions as needed
            }
        }

        private List<Mob> DetectNearbyMobs()
        {
            List<Mob> detectedMobs = new List<Mob>();

            // Iterate through mobs in the same map and add them to detectedMobs if they meet the criteria
            foreach (Mob mob in World.H_Mobs[CurrentMob.Loc.Map].Values)
            {
                if (mob != CurrentMob && mob.Type == MobBehaveour.HuntPlayers && mob.Alive)
                {
                    float distance = MyMath.PointDistance(Player.Loc.X, Player.Loc.Y, mob.Loc.X, mob.Loc.Y);
                    if (distance <= 24)
                    {
                        detectedMobs.Add(mob);
                    }
                }
            }

            return detectedMobs;
        }

        // Implement SelectTargetMob, IsInRange, CalculateDirection, MoveTowards, and AttackMob methods here as previously defined in your code.
        // You may also want to consider adding additional properties and methods for improved readability and maintainability.

        public Mob SelectTargetMob(List<Mob> nearbyMobs)
        {
            Mob nearestMob = null;
            float nearestDistance = float.MaxValue;

            foreach (Mob mob in nearbyMobs)
            {
                // Calculate the distance from the player to the current mob
                float distance = MyMath.PointDistance(Player.Loc.X, Player.Loc.Y, mob.Loc.X, mob.Loc.Y);

                // Check if the current mob is closer than the previously selected nearest mob
                if (distance < nearestDistance)
                {
                    nearestDistance = distance;
                    nearestMob = mob;
                }
            }

            return nearestMob;
        }

        public void ApproachAndAttackMob(Mob mob)
        {
            if (mob != null)
            {
                if (IsInRange(mob))
                {
                    // Attack the target mob
                    AttackMob(mob);
                }
                else
                {
                    // Calculate direction to move towards the target mob
                    byte direction = CalculateDirection(mob);

                    // Move towards the target mob while avoiding obstacles
                    MoveTowards(mob, direction);
                }
            }
        }

        public bool IsInRange(Mob mob)
        {
            // Implement logic to check if the mob is within attack range
            // You can use a predefined attack range constant or calculate it dynamically
            // For example, you can check the distance between the player and the mob
            float distance = MyMath.PointDistance(Player.Loc.X, Player.Loc.Y, mob.Loc.X, mob.Loc.Y);
            return distance <= Player.Loc.X;
        }

        public byte CalculateDirection(Mob mob)
        {
            // Calculate the direction vector from the player to the target mob
            Vector2 directionVector = new Vector2((ushort)(mob.Loc.X - Player.Loc.X), (ushort)(mob.Loc.Y - Player.Loc.Y));

            // Calculate the angle in radians from the direction vector
            float angle = (float)Math.Atan2(directionVector.Y, directionVector.X);

            // Convert the angle to degrees
            float degrees = (float)((angle * 180f) / Math.PI);

            // Ensure the degrees value is positive
            if (degrees < 0)
            {
                degrees += 360f;
            }

            // Map the degrees value to a byte representing a direction
            byte direction = MapDegreesToDirection(degrees);

            return direction;
        }

        // Helper function to map degrees to a direction byte
        private byte MapDegreesToDirection(float degrees)
        {
            // Define direction ranges (e.g., 45 degrees for each direction)
            float directionRange = 360f / 8;

            // Calculate the direction based on the degrees value
            if (degrees >= 0 && degrees < directionRange)
            {
                return 0; // East
            }
            else if (degrees >= directionRange && degrees < 2 * directionRange)
            {
                return 1; // Southeast
            }
            else if (degrees >= 2 * directionRange && degrees < 3 * directionRange)
            {
                return 2; // South
            }
            else if (degrees >= 3 * directionRange && degrees < 4 * directionRange)
            {
                return 3; // Southwest
            }
            else if (degrees >= 4 * directionRange && degrees < 5 * directionRange)
            {
                return 4; // West
            }
            else if (degrees >= 5 * directionRange && degrees < 6 * directionRange)
            {
                return 5; // Northwest
            }
            else if (degrees >= 6 * directionRange && degrees < 7 * directionRange)
            {
                return 6; // North
            }
            else
            {
                return 7; // Northeast
            }
        }

        public void MoveTowards(Mob mob, byte direction)
        {
            // Implement logic to move the character towards the target mob
            // while avoiding obstacles
            // You can use a pathfinding algorithm or simple movement logic here

            // Example: Move the player one step in the specified direction
            switch (direction)
            {
                case 0: // East
                    Player.Loc.X += 1;
                    break;
                case 1: // Southeast
                    Player.Loc.X += 1;
                    Player.Loc.Y += 1;
                    break;
                case 2: // South
                    Player.Loc.Y += 1;
                    break;
                case 3: // Southwest
                    Player.Loc.X -= 1;
                    Player.Loc.Y += 1;
                    break;
                case 4: // West
                    Player.Loc.X -= 1;
                    break;
                case 5: // Northwest
                    Player.Loc.X -= 1;
                    Player.Loc.Y -= 1;
                    break;
                case 6: // North
                    Player.Loc.Y -= 1;
                    break;
                case 7: // Northeast
                    Player.Loc.X += 1;
                    Player.Loc.Y -= 1;
                    break;
                default:
                    break;
            }
        }


        public void AttackMob(Mob mob)
        {
            // Check if the target mob is valid and alive
            if (mob != null && mob.Alive)
            {
                // Implement your attack logic here
                // Calculate the damage to deal to the mob and any other attack actions

                // For example, you can calculate damage based on the player's stats
                uint damage = CalculateDamage(Player, mob);

                // Apply the damage to the mob by passing it by reference with 'ref'
                mob.TakeAttack(Player, ref damage, AttackType.Melee, true);

                // Optionally, check if the mob is defeated and handle the aftermath
                if (!mob.Alive)
                {
                    // Handle the aftermath of defeating the mob, such as gaining experience or loot
                }
            }
        }

        // Implement a method to calculate the damage to be dealt to the target mob.
        private uint CalculateDamage(Character player, Mob mob)
        {
            // Implement your logic to calculate the damage based on the player's attributes,
            // equipment, and any other relevant factors.
            // You can customize this calculation based on your game's combat mechanics.

            // Example: Calculate damage as a random value between MinDamage and MaxDamage
            uint damage = 100;

            return damage;
        }

        // Top-level statements or other classes can go here
    }
}
06/07/2025 10:25 dohteMxx#2
you can't do it with AI anyway lol