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

📄 gamestate.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    }
}

void
GameState::SetupProcessMap()
{
    mProcessMap.clear();

    // predicate name -> member function
    mProcessMap["Init"] = &GameState::ProcessInit;
    mProcessMap["Info"] = &GameState::ProcessInfo;
    mProcessMap["Die"] = &GameState::ProcessShutdown;
}

void
GameState::SetupFlagMap()
{
    mFlagTypeMap.clear();
    mFlagNameMap.clear();
    mFlagTypeMap["F_1_l"] = eFLAG_1_L;  mFlagNameMap[eFLAG_1_L] = "F_1_l";
    mFlagTypeMap["F_2_l"] = eFLAG_2_L;  mFlagNameMap[eFLAG_2_L] = "F_2_l";
    mFlagTypeMap["F_1_r"] = eFLAG_1_R;  mFlagNameMap[eFLAG_1_R] = "F_1_r";
    mFlagTypeMap["F_2_r"] = eFLAG_2_R;  mFlagNameMap[eFLAG_2_R] = "F_2_r";
    mFlagTypeMap["G_1_l"] = eGOAL_1_L;  mFlagNameMap[eGOAL_1_L] = "G_1_l";
    mFlagTypeMap["G_2_l"] = eGOAL_2_L;  mFlagNameMap[eGOAL_2_L] = "G_2_l";
    mFlagTypeMap["G_1_r"] = eGOAL_1_R;  mFlagNameMap[eGOAL_1_R] = "G_1_r";
    mFlagTypeMap["G_2_r"] = eGOAL_2_R;  mFlagNameMap[eGOAL_2_R] = "G_2_r";
}

void
GameState::SetupPlaymodeMap()
{
    // This is the default mapping for play modes.
    // It was used for the first version of the logfile format, when the
    // simulator did not send the play modes mapping in the init message.
    // In order to be able to replay these logfiles, this mapping here should
    // not be changed.
    // In this logfiles, modes 3,11,12,16, and 17 should not occur.
    mPlaymodes.resize(18);
    mPlaymodes[0] = STR_PM_BeforeKickOff;       //  0: before kick off
    mPlaymodes[1] = STR_PM_KickOff_Left;        //  1: kick off left
    mPlaymodes[2] = STR_PM_KickOff_Right;       //  2: kick off right
    mPlaymodes[3] = "kick_off";                 //  3: pseudo kick off state
    mPlaymodes[4] = STR_PM_PlayOn;              //  4: play on
    mPlaymodes[5] = STR_PM_KickIn_Left;         //  5: kick in left
    mPlaymodes[6] = STR_PM_KickIn_Right;        //  6: kick in right
    mPlaymodes[7] = STR_PM_CORNER_KICK_LEFT;    //  7: corner kick left
    mPlaymodes[8] = STR_PM_CORNER_KICK_RIGHT;   //  8: corner kick right
    mPlaymodes[9] = STR_PM_GOAL_KICK_LEFT;      //  9: goal kick left
    mPlaymodes[10] = STR_PM_GOAL_KICK_RIGHT;    // 10: goal kick right
    mPlaymodes[11] = STR_PM_OFFSIDE_LEFT;       // 11: offside left
    mPlaymodes[12] = STR_PM_OFFSIDE_RIGHT;      // 12: offside left
    mPlaymodes[13] = STR_PM_GameOver;           // 13: game over
    mPlaymodes[14] = STR_PM_Goal_Left;          // 14: goal left
    mPlaymodes[15] = STR_PM_Goal_Right;         // 15: goal right
    mPlaymodes[16] = STR_PM_FREE_KICK_LEFT;     // 16: free kick left
    mPlaymodes[17] = STR_PM_FREE_KICK_RIGHT;    // 17: free kick right
}

void GameState::ProcessInit(Predicate& predicate)
{
    predicate.GetValue("FieldLength", mFieldLength);
    predicate.GetValue("FieldWidth", mFieldWidth);
    predicate.GetValue("FieldHeight", mFieldHeight);
    predicate.GetValue("GoalWidth", mGoalWidth);
    predicate.GetValue("GoalDepth", mGoalDepth);
    predicate.GetValue("GoalHeight", mGoalHeight);
    predicate.GetValue("BorderSize", mBorderSize);
    predicate.GetValue("LineWidth", mLineWidth);
    predicate.GetValue("FreeKickDistance", mFreeKickDistance);
    predicate.GetValue("AgentMass", mAgentMass);
    predicate.GetValue("AgentRadius", mAgentRadius);
    predicate.GetValue("AgentMaxSpeed", mAgentMaxSpeed);
    predicate.GetValue("BallRadius", mBallRadius);
    predicate.GetValue("BallMass", mBallMass);

    predicate.GetValue("play_modes", mPlaymodes);
}



void
GameState::ProcessInfo(Predicate& predicate)
{
    bool sawBall = false;
    bool sawPlayer = false;
    bool sawFlag = false;
    
    mAck = "";
    predicate.GetValue("ack", mAck);
    predicate.GetValue("time", mTime);
    predicate.GetValue("half", mHalf);
    predicate.GetValue("score_left", mScoreLeft);
    predicate.GetValue("score_right", mScoreRight);
    predicate.GetValue("team_left", mTeamLeft);
    predicate.GetValue("team_right", mTeamRight);
    predicate.GetValue("play_mode", mPlayMode);

    int players_seen = 0;
    PlayerInfo pi;
    int t1, inx;
    string t2;
    Vector3 pos;

    mPlayers.clear();
    for (Predicate::iterator it = predicate.begin(); it != predicate.end(); ++ it) {
        for (inx = 0; inx < (it->second).size() && (it->second)[inx] != "pos"; ++ inx);
        if (inx < (it->second).size() && (it->second).size() - 3) {
            pos.x = atof((it->second)[inx + 1].data());
            pos.y = atof((it->second)[inx + 2].data());
            pos.z = atof((it->second)[inx + 3].data());
        } else {
            continue;
        }
        switch((it->first)[0]) {
        case 'B':
            if (sawBall) {
                cerr << "Found more than one ball in this message." << endl;
            }
            else
            {
                sawBall = true;
                ResetBall();
            }

            mSeenBall = true;
            mBall = pos;

            break;
        case 'P':
            if (!sawPlayer)
            {
                sawPlayer = true;
                ResetPlayers();
            }

            for (inx = 0; inx < it->second.size() && (it->second)[inx] != "s"; ++ inx);
            if (inx < (it->second).size() && (it->second).size() - 1) {
                t1 = atoi((it->second)[inx + 1].data());
                pi.mSide = static_cast<TTeamIndex>(t1);
            }
            for (inx = 0; inx < it->second.size() && (it->second)[inx] != "id"; ++ inx);
            if (inx < (it->second).size() && (it->second).size() - 1) {
                pi.mUnum = atoi((it->second)[inx + 1].data());
            } else {
                pi.mUnum = 0;
            }
            pi.mPos = pos;

            if (mSeenPlayers.size() <= players_seen)
                mSeenPlayers.resize(players_seen + 1, false);
            if (mPlayers.size() <= players_seen)
                mPlayers.resize(players_seen + 1);
            mSeenPlayers[players_seen] = true;
            mPlayers[players_seen] = pi;
            ++ players_seen;

            break;
        case 'F':
        case 'G':
            if (!sawFlag)
            {
                sawFlag = true;
                ResetFlags();
            }

            t2 = "";
            for (inx = 0; inx < it->second.size() && (it->second)[inx] != "id"; ++ inx);
            if (inx < (it->second).size() && (it->second).size() - 1) {
                t2 = (it->second)[inx + 1];
                t2 = it->first + "_" + t2;
                TFlagTypeMap::const_iterator f = mFlagTypeMap.find(t2);
                if (f == mFlagTypeMap.end()) break;
                mSeenFlags[f->second] = true;
                mFlags[f->second] = pos;
            }

            break;
        }
    }

    if (!sawBall) {
        cerr << "Didn't see a ball in this message" << endl;
    }
    if (!sawPlayer) {
        cerr << "Didn't see any players in this message" << endl;
    }

/*
    for (Predicate::Iterator i = iter; i != iter.end(); ++i)
    {
        Predicate::Iterator j = i;
        if (!predicate.DescentList(j))
        {
            continue;
        }

        // read the object name
        std::string name;
        if (!predicate.GetValue(j,name) || name.length() == 0)
        {
            continue;
        }

        Vector3 pos;
        if (!predicate.GetValue(j, "pos", pos))
        {
            continue;
        }

        switch (name[0])
        {
        case 'B':
            if (sawBall)
                GetLog()->Error() << "Found more than one ball in this message.\n";
            else
            {
                sawBall = true;
                ResetBall();
            }

            mSeenBall = true;
            mBall = pos;
            break;
        case 'P':
            if (!sawPlayer)
            {
                sawPlayer = true;
                ResetPlayers();
            }
            
            pi.mPos = pos;
            if (predicate.GetValue(j, "s", t1))
            {
                pi.mSide = static_cast<TTeamIndex>(t1);
            }
            if (!predicate.GetValue(j, "id", pi.mUnum))
            {
                pi.mUnum = 0;
            }
            if (!predicate.GetValue(j, "say", pi.mMessage))
            {
                pi.mMessage.clear();
            }
            if (mSeenPlayers.size() <= players_seen)
                mSeenPlayers.resize(players_seen + 1, false);
            if (mPlayers.size() <= players_seen)
                mPlayers.resize(players_seen + 1);
            mSeenPlayers[players_seen] = true;
            mPlayers[players_seen] = pi;
            ++players_seen;
            break;
        case 'F':
        case 'G':
            if (!sawFlag)
            {
                sawFlag = true;
                ResetFlags();
            }

            t2 = "";
            if (!predicate.GetValue(j, "id", t2))
            {
                break;
            }
            t2 = name + "_" + t2;

            TFlagTypeMap::const_iterator f = mFlagTypeMap.find(t2);
            if (f == mFlagTypeMap.end()) break;
            mSeenFlags[f->second] = true;
            mFlags[f->second] = pos;
            break;
        }
    }
    if (!sawBall)
        GetLog()->Error()<< "Didn't see a ball in this message\n";
    if (!sawPlayer)
        GetLog()->Error() << "Didn't see any players in this message\n";
*/
}



void
GameState::ProcessShutdown(Predicate& )
{
    mFinished = true;
}



double
GameState::GetFieldLength() const
{
    return mFieldLength;
}

double
GameState::GetFieldWidth() const
{
    return mFieldWidth;
}

double
GameState::GetFieldHeight() const
{
    return mFieldHeight;
}

double
GameState::GetLineWidth() const
{
    return mLineWidth;
}

double
GameState::GetBorderSize() const
{
    return mBorderSize;
}

double
GameState::GetGoalWidth() const
{
    return mGoalWidth;
}

double
GameState::GetGoalHeight() const
{
    return mGoalHeight;
}

double
GameState::GetGoalDepth() const
{
    return mGoalDepth;
}

Vector3
GameState::GetGoalSize(bool invert_x) const
{
    return Vector3(invert_x ? mGoalDepth : -mGoalDepth,
                          mGoalWidth,
                          mGoalHeight);
}

double
GameState::GetAgentMass() const
{
    return mAgentMass;
}

double
GameState::GetAgentRadius() const
{
    return mAgentRadius;
}

double
GameState::GetAgentMaxSpeed() const
{
    return mAgentMaxSpeed;
}

double
GameState::GetBallRadius() const
{
    return mBallRadius;
}

double
GameState::GetBallMass() const
{
    return mBallMass;
}

⌨️ 快捷键说明

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