⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamestateaspect.cpp

📁 robocup rcssserver 运行防真机器人足球比赛所用的服务器端
💻 CPP
字号:
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-   this file is part of rcssserver3D   Fri May 9 2003   Copyright (C) 2002,2003 Koblenz University   Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group   $Id: gamestateaspect.cpp,v 1.14 2008/03/10 23:57:07 sgvandijk Exp $   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; version 2 of the License.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "gamestateaspect.h"#include <zeitgeist/logserver/logserver.h>#include <soccer/soccerbase/soccerbase.h>#include <soccer/agentstate/agentstate.h>#include <salt/random.h>using namespace oxygen;using namespace boost;using namespace std;using namespace salt;GameStateAspect::GameStateAspect() : SoccerControlAspect(){    mPlayMode = PM_BeforeKickOff;    mTime = 0;    mLeadTime = 0;    mFupTime = 0;    mLastModeChange = 0;    mGameHalf = GH_FIRST;    mScore[0] = 0;    mScore[1] = 0;    mLastKickOff = TI_NONE;    //mSecondHalfKickOff = TI_NONE;    mLeftInit = Vector3f(0,0,0);    mRightInit = Vector3f(0,0,0);    mFinished = false;}GameStateAspect::~GameStateAspect(){}voidGameStateAspect::UpdateTime(float deltaTime){    switch (mPlayMode)    {    case PM_BeforeKickOff:        mLeadTime += deltaTime;        break;    case PM_GameOver:        mFupTime += deltaTime;        break;    default:        mTime += deltaTime;    }}voidGameStateAspect::Update(float deltaTime){    UpdateTime(deltaTime);}TPlayModeGameStateAspect::GetPlayMode() const{    return mPlayMode;}voidGameStateAspect::SetPlayMode(TPlayMode mode){    if (mode == mPlayMode)    {        return;    }    GetLog()->Normal() << "(GameStateAspect) playmode changed to "                       << SoccerBase::PlayMode2Str(mode) << " at t="                       << mTime << "\n";    mPlayMode = mode;    mLastModeChange = mTime;    mLeadTime = 0.0;    mFupTime = 0.0;}// let the monitor handle who kicks off in 2nd half. voidGameStateAspect::KickOff(TTeamIndex ti){    // throw a coin to determine which team kicks off    if (ti == TI_NONE)    {        ti = (salt::UniformRNG<>(0,1)() <= 0.5) ? TI_LEFT : TI_RIGHT;    }        SetPlayMode((ti == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right);        if (mLastKickOff == TI_NONE)        mLastKickOff = ti;}// void// GameStateAspect::KickOff(TTeamIndex ti)// {//     if (mGameHalf == GH_FIRST)//     {//         // throw a coin to determine which team kicks off//         if (ti == TI_NONE)//         {//             ti = (salt::UniformRNG<>(0,1)() <= 0.5) ? TI_LEFT : TI_RIGHT;//         }//         SetPlayMode((ti == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right);//         if (mLastKickOff == TI_NONE)//             mLastKickOff = ti;//     }//     else//     {//         // in the second half, let the opposite team kick off//         SetPlayMode((mLastKickOff == TI_LEFT) ? PM_KickOff_Right : PM_KickOff_Left);//     }// }//---------------------------------------------// void// GameStateAspect::KickOff(TTeamIndex ti)// {//     if (mGameHalf == GH_FIRST)//     {//         // throw a coin to determine which team kicks off//         if (ti == TI_NONE)//         {//             ti = (salt::UniformRNG<>(0,1)() <= 0.5) ? TI_LEFT : TI_RIGHT;//         }//         SetPlayMode((ti == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right);//         mLastKickOff = ti;//         if (mSecondHalfKickOff == TI_NONE)//         {//             //clog << "setting mSecondHalfKickOff\n";//             mSecondHalfKickOff = //                  (mLastKickOff == TI_LEFT) ? TI_RIGHT : TI_LEFT;//         }//     }//     else//     {//         // in the second half, let the opposite team kick off//         SetPlayMode((mSecondHalfKickOff == TI_LEFT) ? PM_KickOff_Left : PM_KickOff_Right);//     }// }//---------------------------------------------TTimeGameStateAspect::GetTime() const{    return mTime;}TTimeGameStateAspect::GetModeTime() const{    switch (mPlayMode)    {    case PM_BeforeKickOff:        return mLeadTime;    case PM_GameOver:        return mFupTime;    default:        return mTime - mLastModeChange;    }}TTimeGameStateAspect::GetLastModeChange() const{    return mLastModeChange;}voidGameStateAspect::SetTeamName(TTeamIndex idx, const std::string& name){    switch (idx)    {    case TI_LEFT:        mTeamName[0] = name;        break;    case TI_RIGHT:        mTeamName[1] = name;        break;    }    return;}std::stringGameStateAspect::GetTeamName(TTeamIndex idx) const{    switch (idx)    {    case TI_LEFT:        return mTeamName[0];    case TI_RIGHT:        return mTeamName[1];    default:        return "";    }}TTeamIndexGameStateAspect::GetTeamIndex(const std::string& teamName){    for (int i=0; i<=1; ++i)    {        if (mTeamName[i].empty())        {            mTeamName[i] = teamName;            return static_cast<TTeamIndex>(i + TI_LEFT);        }        if (mTeamName[i] == teamName)        {            return static_cast<TTeamIndex>(i + TI_LEFT);        }    }    return TI_NONE;}boolGameStateAspect::InsertUnum(TTeamIndex idx, int unum){    int i;    switch (idx)    {    case TI_LEFT:        i = 0;        break;    case TI_RIGHT:        i = 1;        break;    default:        return false;    }    TUnumSet& set = mUnumSet[i];    if (        (set.size() >= 11) ||        (set.find(unum) != set.end())        )    {        return false;    }    set.insert(unum);    return true;}boolGameStateAspect::EraseUnum(TTeamIndex idx, int unum){    int i;    switch (idx)    {    case TI_LEFT:        i = 0;        break;    case TI_RIGHT:        i = 1;        break;    default:        return false;    }    TUnumSet& set = mUnumSet[i];    if (        (set.find(unum) == set.end())        )    {        return false;    }    set.erase(unum);    return true;}boolGameStateAspect::RequestUniform(shared_ptr<AgentState> agentState,                                std::string teamName, unsigned int unum){    if (agentState.get() == 0)    {        return false;    }    TTeamIndex idx = GetTeamIndex(teamName);    if (idx == TI_NONE)    {        GetLog()->Error()            << "ERROR: (GameStateAspect::RequestUniform) invalid teamname "            << teamName << "\n";        return false;    }    if (unum == 0)    {        unum = RequestUniformNumber(idx);    }    if (! InsertUnum(idx,unum))    {        GetLog()->Error()            << "ERROR: (GameStateAspect::RequestUniform) cannot insert uniform"            " number " << unum << " to team " << teamName << "\n";        return false;    }    agentState->SetUniformNumber(unum);    agentState->SetTeamIndex(idx);    //agentState->SetPerceptName(teamName, ObjectState::PT_Default);    agentState->SetPerceptName(teamName, ObjectState::PT_Default, ObjectState::PT_Player );    agentState->SetPerceptName("player", ObjectState::PT_TooFar);    GetLog()->Normal() << "(GameStateAspect) handed out uniform number "                       << unum << " for team " << teamName << "\n";    return true;}boolGameStateAspect::ReturnUniform(TTeamIndex ti, unsigned int unum){    if (! EraseUnum(ti,unum))    {        GetLog()->Error()            << "ERROR: (GameStateAspect::ReturnUniform) cannot erase uniform"            " number " << unum << " from team " << ti << "\n";        return false;    }}voidGameStateAspect::SetGameHalf(TGameHalf half){    if (        (half != GH_FIRST) &&        (half != GH_SECOND)        )    {        return;    }    mGameHalf = half;}TGameHalfGameStateAspect::GetGameHalf() const{    return mGameHalf;}voidGameStateAspect::ScoreTeam(TTeamIndex idx){    switch (idx)    {    case TI_LEFT:        ++mScore[0];        break;    case TI_RIGHT:        ++mScore[1];        break;    }    return;}intGameStateAspect::GetScore(TTeamIndex idx) const{    switch (idx)    {    case TI_LEFT:        return mScore[0];    case TI_RIGHT:        return mScore[1];    default:        return 0;    }}Vector3fGameStateAspect::RequestInitPosition(const TTeamIndex ti){    if (ti == TI_NONE)    {        GetLog()->Debug()            << "(GameStateAspect) RequestInitPosition called with "            << "ti=TI_NONE\n";        return Vector3f(0,0,10);    }    salt::Vector3f& init = (ti ==TI_LEFT) ? mLeftInit : mRightInit;    Vector3f pos = init;    init[1] -= mAgentRadius * 3;        float fieldWidth;    SoccerBase::GetSoccerVar(*this,"FieldWidth",fieldWidth);                if (init[1] < -fieldWidth/2.0)        {            init[1] = fieldWidth/2 - mAgentRadius*2;            init[0] += mAgentRadius * 2;        }    return pos;}floatGameStateAspect::RequestInitOrientation(const TTeamIndex ti) const{    switch ( ti )    {        case TI_LEFT: return -90;        break;        case TI_RIGHT: return 90;        break;    }    return 0;}voidGameStateAspect::OnLink(){    // setup the initial starting positions for the agents    float fieldWidth = 64.0;    SoccerBase::GetSoccerVar(*this,"FieldWidth",fieldWidth);    float fieldLength = 100.0;    SoccerBase::GetSoccerVar(*this,"FieldLength",fieldLength);    mAgentRadius = 3.5;    SoccerBase::GetSoccerVar(*this,"AgentRadius",mAgentRadius);    mLeftInit = Vector3f        (            -fieldLength/2.0 + mAgentRadius*2,            fieldWidth/2 - mAgentRadius*2,            mAgentRadius            );    mRightInit = Vector3f        (            +fieldLength/2.0 - mAgentRadius*2,            fieldWidth/2  - mAgentRadius*2,            mAgentRadius            );}intGameStateAspect::RequestUniformNumber(TTeamIndex ti) const{    int idx;    switch (ti)    {    case TI_LEFT:        idx = 0;        break;    case TI_RIGHT:        idx = 1;        break;    default:        return 0;    }        for (int i = 1; i <=11; ++i)      if (mUnumSet[idx].find(i) == mUnumSet[idx].end())        return i;}

⌨️ 快捷键说明

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