plan_decision.cpp

来自「巫魔问题求解」· C++ 代码 · 共 196 行

CPP
196
字号
#include "Plan.h"
#include <Wum_Base/src/Cell.h>
#include "ClientMap.h"
#include "WorldModel.h"
#include "Effector.h"
#include <Wum_Server/src/Explorer.h>
#include <Wum_Tool/src/Math.h>
#include "Thinker.h"

using namespace _algorithm;
using namespace _base;
using namespace _server;
/////////////////////////////////////////////
Plan::Plan(_algorithm::WorldModel &wm, Effector &eff)
{
    this->mpWorldModel = &wm;
    this->mpEffector = &eff;
    this->mpThinker = new Thinker(wm);
}

/////////////////////////////////////////////
Plan::~Plan(void)
{
    delete this->mpThinker;
}

/////////////////////////////////////////////
Plan::Plan(const Plan &from)
{
}

/////////////////////////////////////////////
_algorithm::WorldModel &Plan::TheWorldModel(void)
{
    return *mpWorldModel;
}

/////////////////////////////////////////////
const _algorithm::WorldModel &Plan::GetWorldModel(void) const
{
    return *mpWorldModel;
}

/////////////////////////////////////////////
Effector &Plan::TheEffector(void)
{
    return *mpEffector;
}

/////////////////////////////////////////////
const Effector &Plan::GetEffector(void) const
{
    return *mpEffector;
}

/////////////////////////////////////////////
Thinker &Plan::TheThinker(void)
{
    return *mpThinker;
}

/////////////////////////////////////////////
const Thinker &Plan::GetThinker(void) const
{
    return *mpThinker;
}

////////////////////////////////////////////
bool Plan::IsNeedNewInfo(void)
{
    while ((this->mPaths.size() != 0) && (this->mPaths[0].size() <= 1))
    {
        this->mPaths.erase(this->mPaths.begin());
    }

    return (this->mPaths.size() == 0) && (this->mTasks.size() == 0);
}

/////////////////////////////////////////////
void Plan::ExeDecision(void)
{
    if (this->mTasks.size())
    {
        switch (this->mTasks[0])
        {
        case O_TurnLeft:
            this->TheEffector().TurnLeft();
            break;

        case O_TurnRight:
            this->TheEffector().TurnRight();
            break;

        case O_GoAhead:
            this->TheEffector().GoAhead();
            break;

        case O_Escape:
            this->TheEffector().Escape();
            break;

        case O_Shoot:
            this->TheEffector().Shoot();
            break;
        }
        this->mTasks.erase(this->mTasks.begin());
    }
}

///////////////////////////////////////////
void Plan::Step(void)
{
    this->TheThinker().Think();
    if (this->mPaths.size() == 0 && this->mTasks.size() == 0)
    {
        Cell *tp = this->GenTargetPlace();
        if (tp)
        {
            Cell *from = &this->TheWorldModel().TheClientMap().TheCell(this->GetWorldModel().GetInfo().mPositionX,
                                                                       this->GetWorldModel().GetInfo().mPositionY);
            Cell *to = tp;
            int from_i = -1;
            int to_i = -1;
            for (unsigned int i=0; i<this->TheWorldModel().TheClientMap().TheSafeCells().size(); i++)
            {
                if (this->TheWorldModel().TheClientMap().TheSafeCells()[i] == from)
                {
                    from_i = i;
                }
                else if (this->TheWorldModel().TheClientMap().TheSafeCells()[i] == to)
                {
                    to_i = i;
                }
            }
            this->mPaths.push_back(this->GenPath(from_i, to_i));
        }
        else
        {
            this->TheWorldModel().TheClientMap()._su_IsNoSolution() = true;
            Cell *tp = this->GenTargetPlace();
            if (tp)
            {
                Cell *from = &this->TheWorldModel().TheClientMap().TheCell(this->GetWorldModel().GetInfo().mPositionX,
                                                                           this->GetWorldModel().GetInfo().mPositionY);
                Cell *to = tp;
                int from_i = -1;
                int to_i = -1;
                for (unsigned int i=0; i<this->TheWorldModel().TheClientMap().TheSafeCells().size(); i++)
                {
                    if (this->TheWorldModel().TheClientMap().TheSafeCells()[i] == from)
                    {
                        from_i = i;
                    }
                    else if (this->TheWorldModel().TheClientMap().TheSafeCells()[i] == to)
                    {
                        to_i = i;
                    }
                }
                this->mPaths.push_back(this->GenPath(from_i, to_i));
            }
        }
    }
    this->GenTasks();
    if (this->TheWorldModel().TheClientMap().IsNoSolution() && 
        this->TheWorldModel().GetInfo().mPositionX == 0 &&
        this->TheWorldModel().GetInfo().mPositionY == 0)
    {
        this->mTasks.assign(1, O_Escape);
    }
    this->ExeDecision();
}

//////////////////////////////////////////////////
void Plan::GenEscape(void)
{
    //Cell *from = &this->TheWorldModel().TheClientMap().TheCell(this->GetWorldModel().GetInfo().mPositionX,
    //                                                           this->GetWorldModel().GetInfo().mPositionY);
    //Cell *to = &this->TheWorldModel().TheClientMap().TheCell(0,0);
    //int from_i = -1;
    //int to_i = -1;
    //for (unsigned int i=0; i<this->TheWorldModel().TheClientMap().TheSafeCells().size(); i++)
    //{
    //    if (this->TheWorldModel().TheClientMap().TheSafeCells()[i] == from)
    //    {
    //        from_i = i;
    //    }
    //    else if (this->TheWorldModel().TheClientMap().TheSafeCells()[i] == to)
    //    {
    //        to_i = i;
    //    }
    //}
    //this->mPaths.push_back(this->GenPath(from_i, to_i));
    //this->GenTasks();
    this->mTasks.push_back(O_Escape);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?