<commit_before>#include <Internal/PotentialField/PotentialFieldImpl.hpp>
#include <Internal/SubModule/PotentialFieldSubModuleImpl.hpp>
#include <Internal/HelperFunctions.hpp>

// Config module
#include <DoremiEngine/Configuration/Include/ConfigurationModule.hpp>

#include <iostream>


namespace DoremiEngine
{
    namespace AI
    {
        PotentialFieldImpl::PotentialFieldImpl(AIContext& p_aiContext) : m_phermoneEffect(15), m_context(p_aiContext)
        {
            m_stepDistance = m_context.config.GetAllConfigurationValues().AIJumpDistance;
        }
        PotentialFieldImpl::~PotentialFieldImpl() {}
        void PotentialFieldImpl::SetGrid(PotentialFieldGridPoint* p_grid)
        {
            // m_grid = p_grid;
            m_grid = p_grid;
        }
        void PotentialFieldImpl::Update()
        {
            // TODOKO optimize!!!! threads would be awesome here...
            // Lets all the actors update all the gridpoints in the potentialfield using the distance and charge
            using namespace DirectX;

            for(size_t x = 0; x < m_numberOfQuadsWidth; x++)
            {
                for(size_t y = 0; y < m_numberOfQuadsHeight; y++)
                {
                    XMFLOAT3 quadnPos3d = GetGridQuadPosition(x, y);
                    XMFLOAT2 quadPos = XMFLOAT2(quadnPos3d.x, quadnPos3d.z);
                    float totalCharge = 0;
                    for(auto actor : m_staticActors)
                    {
                        XMINT2 myQuad = XMINT2(x, y);
                        XMINT2 closestQuad = actor->GetClosestOccupied(myQuad);
                        if(myQuad.x == closestQuad.x && myQuad.y == closestQuad.y)
                        {
                            // Same quad
                            m_grid[x + m_numberOfQuadsWidth * y].occupied = true;
                            // std::cout << "x " << x << " y " << y << std::endl;
                        }
                        XMFLOAT3 actorQuadPos3d = GetGridQuadPosition(closestQuad.x, closestQuad.y);
                        XMFLOAT2 actorQuadPosition = XMFLOAT2(actorQuadPos3d.x, actorQuadPos3d.z);

                        float actorCharge = actor->GetCharge();
                        float actorRange = actor->GetRange();
                        // Calculate charge
                        XMVECTOR actorQuadPosVec = XMLoadFloat2(&actorQuadPosition);
                        XMVECTOR quadPosVec = XMLoadFloat2(&quadPos);

                        XMVECTOR distance = actorQuadPosVec - quadPosVec;
                        float dist = *XMVector2Length(distance).m128_f32;
                        if(dist < actorRange)
                        {
                            float force = actorCharge * std::fmaxf(1.0f - dist / actorRange, 0.0f);
                            totalCharge += force;
                        }
                    }
                    m_grid[x + m_numberOfQuadsWidth * y].charge = totalCharge;
                }
            }
            for(size_t x = 0; x < m_numberOfQuadsWidth; x++)
            {
                for(size_t y = 0; y < m_numberOfQuadsHeight; y++)
                {
                    if(m_grid[x + m_numberOfQuadsWidth * y].occupied == true)
                    {
                        std::cout << "X";
                    }
                    else
                    {
                        std::cout << " ";
                    }
                }
                std::cout << std::endl;
            }
            m_needsUpdate = false;
        }
        void PotentialFieldImpl::AddActor(PotentialFieldActor* p_newActor)
        {
            if(p_newActor->IsStatic() && m_staticActors.find(p_newActor) == m_staticActors.end())
            {
                // The actor is static and not in list
                m_staticActors.insert(p_newActor);
                m_needsUpdate = true;
            }
            else if(!p_newActor->IsStatic())
            {
                // TODOKO might need to check if it's in the list
                m_dynamicActors.push_back(p_newActor);
            }
        }
        void PotentialFieldImpl::RemoveActor(PotentialFieldActor* p_newActor)
        {
            if(m_staticActors.count(p_newActor) != 0)
            {
                m_staticActors.erase(p_newActor);
            }
            else
            {
                size_t length = m_dynamicActors.size();
                for (size_t i = 0; i < length; i++)
                {
                    if (m_dynamicActors[i] == p_newActor)
                    {
                        m_dynamicActors.erase(m_dynamicActors.begin() + i);
                        length = m_dynamicActors.size();
                    }
                }
            }
        }
        DirectX::XMINT2 PotentialFieldImpl::WhatGridPosAmIOn(const DirectX::XMFLOAT3& p_unitPosition)
        {
            using namespace DirectX;
            XMFLOAT2 position2D = XMFLOAT2(p_unitPosition.x, p_unitPosition.z); // Needs to be modifiable
            float gridQuadWidth = m_width / static_cast<float>(m_numberOfQuadsWidth); // Gets the width and hight of one quad
            float gridQuadHeight = m_height / static_cast<float>(m_numberOfQuadsHeight);
            // Offset given position with the fields offset to take it back to origo so we are able to calculate which quad we are in
            XMFLOAT2 bottomLeft = XMFLOAT2(m_center.x - m_width / 2.0f, m_center.z - m_height / 2.0f);
            position2D.x -= bottomLeft.x;
            position2D.y -= bottomLeft.y;
            int quadNrX = static_cast<int>(std::floor(position2D.x / gridQuadWidth)); // What quad in x and y
            int quadNrY = static_cast<int>(std::floor(position2D.y / gridQuadHeight));
            if(quadNrX >= 0 && quadNrX < m_numberOfQuadsWidth && quadNrY >= 0 && quadNrY < m_numberOfQuadsHeight)
            {
                return XMINT2(quadNrX, quadNrY);
            }
            else
            {
                return XMINT2(-1, -1);
            }
        }

        DirectX::XMFLOAT3 PotentialFieldImpl::GetGridQuadPosition(const int& p_x, const int& p_z)
        {
            using namespace DirectX;
            XMFLOAT3 returnPosition; // position to be returned
            float quadWidth = m_quadSize.x;
            float quadHeight = m_quadSize.y;
            XMFLOAT2 bottomLeft = XMFLOAT2(m_center.x - m_width * 0.5f, m_center.z - m_height * 0.5f);
            returnPosition =
                XMFLOAT3((float)p_x * quadWidth + bottomLeft.x + quadWidth * 0.5f, m_center.y, (float)p_z * quadHeight + bottomLeft.y + quadHeight * 0.5f);
            return returnPosition;
        }

        DirectX::XMFLOAT3 PotentialFieldImpl::GetAttractionPosition(const DirectX::XMFLOAT3& p_unitPosition, bool& p_inField, bool& p_goalInRange,
                                                                    bool& p_shouldJump, PotentialFieldActor* p_currentActor, const bool& p_staticCheck)
        {
            using namespace DirectX;
            // If there is no positive actor/goal to go to simply dont move!
            bool goalInRange = AnyPositiveGoalInRange(p_unitPosition);
            if(!goalInRange)
            {
                p_goalInRange = false;<commit_msg>Smal fix for goal in range in AI<commit_after>#include <Internal/PotentialField/PotentialFieldImpl.hpp>
#include <Internal/SubModule/PotentialFieldSubModuleImpl.hpp>
#include <Internal/HelperFunctions.hpp>

// Config module
#include <DoremiEngine/Configuration/Include/ConfigurationModule.hpp>

#include <iostream>


namespace DoremiEngine
{
    namespace AI
    {
        PotentialFieldImpl::PotentialFieldImpl(AIContext& p_aiContext) : m_phermoneEffect(15), m_context(p_aiContext)
        {
            m_stepDistance = m_context.config.GetAllConfigurationValues().AIJumpDistance;
        }
        PotentialFieldImpl::~PotentialFieldImpl() {}
        void PotentialFieldImpl::SetGrid(PotentialFieldGridPoint* p_grid)
        {
            // m_grid = p_grid;
            m_grid = p_grid;
        }
        void PotentialFieldImpl::Update()
        {
            // TODOKO optimize!!!! threads would be awesome here...
            // Lets all the actors update all the gridpoints in the potentialfield using the distance and charge
            using namespace DirectX;

            for(size_t x = 0; x < m_numberOfQuadsWidth; x++)
            {
                for(size_t y = 0; y < m_numberOfQuadsHeight; y++)
                {
                    XMFLOAT3 quadnPos3d = GetGridQuadPosition(x, y);
                    XMFLOAT2 quadPos = XMFLOAT2(quadnPos3d.x, quadnPos3d.z);
                    float totalCharge = 0;
                    for(auto actor : m_staticActors)
                    {
                        XMINT2 myQuad = XMINT2(x, y);
                        XMINT2 closestQuad = actor->GetClosestOccupied(myQuad);
                        if(myQuad.x == closestQuad.x && myQuad.y == closestQuad.y)
                        {
                            // Same quad
                            m_grid[x + m_numberOfQuadsWidth * y].occupied = true;
                            // std::cout << "x " << x << " y " << y << std::endl;
                        }
                        XMFLOAT3 actorQuadPos3d = GetGridQuadPosition(closestQuad.x, closestQuad.y);
                        XMFLOAT2 actorQuadPosition = XMFLOAT2(actorQuadPos3d.x, actorQuadPos3d.z);

                        float actorCharge = actor->GetCharge();
                        float actorRange = actor->GetRange();
                        // Calculate charge
                        XMVECTOR actorQuadPosVec = XMLoadFloat2(&actorQuadPosition);
                        XMVECTOR quadPosVec = XMLoadFloat2(&quadPos);

                        XMVECTOR distance = actorQuadPosVec - quadPosVec;
                        float dist = *XMVector2Length(distance).m128_f32;
                        if(dist < actorRange)
                        {
                            float force = actorCharge * std::fmaxf(1.0f - dist / actorRange, 0.0f);
                            totalCharge += force;
                        }
                    }
                    m_grid[x + m_numberOfQuadsWidth * y].charge = totalCharge;
                }
            }
            for(size_t x = 0; x < m_numberOfQuadsWidth; x++)
            {
                for(size_t y = 0; y < m_numberOfQuadsHeight; y++)
                {
                    if(m_grid[x + m_numberOfQuadsWidth * y].occupied == true)
                    {
                        std::cout << "X";
                    }
                    else
                    {
                        std::cout << " ";
                    }
                }
                std::cout << std::endl;
            }
            m_needsUpdate = false;
        }
        void PotentialFieldImpl::AddActor(PotentialFieldActor* p_newActor)
        {
            if(p_newActor->IsStatic() && m_staticActors.find(p_newActor) == m_staticActors.end())
            {
                // The actor is static and not in list
                m_staticActors.insert(p_newActor);
                m_needsUpdate = true;
            }
            else if(!p_newActor->IsStatic())
            {
                // TODOKO might need to check if it's in the list
                m_dynamicActors.push_back(p_newActor);
            }
        }
        void PotentialFieldImpl::RemoveActor(PotentialFieldActor* p_newActor)
        {
            if(m_staticActors.count(p_newActor) != 0)
            {
                m_staticActors.erase(p_newActor);
            }
            else
            {
                size_t length = m_dynamicActors.size();
                for (size_t i = 0; i < length; i++)
                {
                    if (m_dynamicActors[i] == p_newActor)
                    {
                        m_dynamicActors.erase(m_dynamicActors.begin() + i);
                        length = m_dynamicActors.size();
                    }
                }
            }
        }
        DirectX::XMINT2 PotentialFieldImpl::WhatGridPosAmIOn(const DirectX::XMFLOAT3& p_unitPosition)
        {
            using namespace DirectX;
            XMFLOAT2 position2D = XMFLOAT2(p_unitPosition.x, p_unitPosition.z); // Needs to be modifiable
            float gridQuadWidth = m_width / static_cast<float>(m_numberOfQuadsWidth); // Gets the width and hight of one quad
            float gridQuadHeight = m_height / static_cast<float>(m_numberOfQuadsHeight);
            // Offset given position with the fields offset to take it back to origo so we are able to calculate which quad we are in
            XMFLOAT2 bottomLeft = XMFLOAT2(m_center.x - m_width / 2.0f, m_center.z - m_height / 2.0f);
            position2D.x -= bottomLeft.x;
            position2D.y -= bottomLeft.y;
            int quadNrX = static_cast<int>(std::floor(position2D.x / gridQuadWidth)); // What quad in x and y
            int quadNrY = static_cast<int>(std::floor(position2D.y / gridQuadHeight));
            if(quadNrX >= 0 && quadNrX < m_numberOfQuadsWidth && quadNrY >= 0 && quadNrY < m_numberOfQuadsHeight)
            {
                return XMINT2(quadNrX, quadNrY);
            }
            else
            {
                return XMINT2(-1, -1);
            }
        }

        DirectX::XMFLOAT3 PotentialFieldImpl::GetGridQuadPosition(const int& p_x, const int& p_z)
        {
            using namespace DirectX;
            XMFLOAT3 returnPosition; // position to be returned
            float quadWidth = m_quadSize.x;
            float quadHeight = m_quadSize.y;
            XMFLOAT2 bottomLeft = XMFLOAT2(m_center.x - m_width * 0.5f, m_center.z - m_height * 0.5f);
            returnPosition =
                XMFLOAT3((float)p_x * quadWidth + bottomLeft.x + quadWidth * 0.5f, m_center.y, (float)p_z * quadHeight + bottomLeft.y + quadHeight * 0.5f);
            return returnPosition;
        }

        DirectX::XMFLOAT3 PotentialFieldImpl::GetAttractionPosition(const DirectX::XMFLOAT3& p_unitPosition, bool& p_inField, bool& p_goalInRange,
                                                                    bool& p_shouldJump, PotentialFieldActor* p_currentActor, const bool& p_staticCheck)
        {
            using namespace DirectX;
            // If there is no positive actor/goal to go to simply dont move!
            p_goalInRange = AnyPositiveGoalInRange(p_unitPosition);
            if(!p_goalInRange)
            {
{'fraction_non_alphanumeric': 0.08714867757751536, 'fraction_numerical': 0.009944064636420136, 'mean_word_length': 1.4891715366105192, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3631026', 'n_tokens_mistral': 4235, 'n_tokens_neox': 3976, 'n_words': 813}