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

📄 main.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************** *   Copyright (C) 2004 - 2006 by ZJUBase                                  *
 *                                National Lab of Industrial Control Tech. * *                                Zhejiang University, China               *
*                                                                         * *   Team members:                                                         *
 *    Currently the team leader is,                                        * *           Hao JIANG (jianghao@iipc.zju.edu.cn; riveria@gmail.com)       *
 *    In the next season, the leader will be                               * *           Yifeng ZHANG (yfzhang@iipc.zju.edu.cn)                        *
 *    ZJUBase 3D agent is created by                                       * *           Dijun LUO (djluo@iipc.zju.edu.cn)                             *
 *    All the members who has ever contributed:                            * *           Jun JIANG                                                     *
 *           Xinfeng DU (xfdu@iipc.zju.edu.cn)                             *
 *           Yang ZHOU (yzhou@iipc.zju.edu.cn)                             *
 *           Zhipeng YANG                                                  *
 *           Xiang FAN                                                     *
 *                                                                         *
 *   Team Manager:                                                          *
 *      Ms. Rong XIONG (rxiong@iipc.zju.edu.cn)                            *
 *                                                                         *
 *   If you met any problems or you have something to discuss about        * *   ZJUBase. Please feel free to contact us through EMails given below.   * *                                                                         * *   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; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   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.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/
#include <vcl.h>
#pragma hdrstop

#include "main.h"
#include <sstream>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "parser.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    int i;
    for(i=0;i<11;i++){
        TRadioButton * rb = new TRadioButton(GroupBox1);
        rbs[i]=rb;
        rb->Parent = GroupBox1;
        rb->Caption = i+1;
        rb->Top = 20;
        rb->Left = 10 + i*44;
        rb->Width = 30;
        rb->Enabled = false;
        rb->OnEnter = RadioButton1->OnEnter;
        rb->OnKeyDown = RadioButton1->OnKeyDown;
    }
    TrackBar1->Enabled = false;
    currentNumber = 1;
//    ToolBar1->Enabled = false;
    interval = 1;
    connects = 0;
    center = Vector(Memo1->Left/2,(Height+TrackBar1->Top+TrackBar1->Height)/2);
    scale = 5;
    mouse_left_down = false;
    mouse_right_down = false;
    big = false;
    mLiveMonitor = false;
    mShowReal = false;
    mFieldReverse = false;
    mMonitorOnly = false;

    MonitorCommandEdit->Text = "(ball (pos 0 0 10) (vel 6.0 0.0 0.1))";
}
//---------------------------------------------------------------------------


void TForm1::ReadFiles(TStrings *files)
{
    //TODO: Add your source code here
    int i,j,mynumber;
    FILE *p;
    char buff[65536];
    for(i=0;i<11;i++)
        rbs[i]->Enabled = false;
    TrackBar1->Enabled = false;
    for(i=0;i<11;i++){
        collWM[i].clear();
    }
    monitor.clear();
    mMonitorOnly = true;
    for(i=0;i<files->Count;i++){
        if(files->Strings[i].Pos("monitor.log")) {
            continue;
        }
        p = fopen(files->Strings[i].c_str(),"r");
        if(!p)
            continue;
        fgets(buff,65535,p);
        if(strcmp(buff,"setting teamname to ZJUBase\n")){
            fclose(p);
            continue;
        }
		for(int j=0;j<100;j++){
			fgets(buff,65535,p);
			mynumber = -1;
			sscanf(buff,"my number is set to %d",&mynumber);
			if(mynumber>0&&mynumber<12)
				break;
		}
		if(!(mynumber>0&&mynumber<12)){
            fclose(p);
            continue;
        }
        mMonitorOnly = false;
        ReadFile(p,collWM[mynumber-1]);
        rbs[mynumber-1]->Enabled = true;
        rbs[mynumber-1]->Checked = true;
        TrackBar1->Max = collWM[mynumber-1].size();
        TrackBar1->Enabled = true;
        currentNumber = mynumber;
        ToolBar1->Enabled = true;
        Timer2->Enabled = true;
        fclose(p);
    }
    for (i = 0; i < files->Count; ++ i) {
        if(files->Strings[i].Pos("monitor.log")) {
            ReadMonitor(files->Strings[i].c_str());
        }
    }

    if (mMonitorOnly) {
        TrackBar1->Enabled = true;
        TrackBar1->Max = monitor.size();
    }
}

void TForm1::ReadFile(FILE *p,vecWM &collWM)
{
    //TODO: Add your source code here
    char buff[65536], buf[1000];
    MyWM wm;
    int i;
    while(!feof(p)){
        fgets(buff,65535,p);
        if(buff[0]=='N'){// a new cycle
            strcpy(wm.str[0],buff);
            for(i=0;i<LOG_NUM_LINE-1;i++){
                fgets(wm.str[i+1],1000,p);
//                strcpy(wm.str[i+1],buff);
            }
            collWM.push_back(wm);
        }
    }

//    for(i=0;i<31;i++)
//        ShowMessage(collWM[2].str[i]);
}
void __fastcall TForm1::RadioButton1Enter(TObject *Sender)
{
    TRadioButton* rb = (TRadioButton*)Sender;
    Caption = rb->Caption;
    int number;
    number = Caption.ToInt();
    int m = collWM[number-1].size();
    TrackBar1->Max = m;
    currentNumber = number;
    SendWM(TrackBar1->Position);

        
}
//---------------------------------------------------------------------------


void TForm1::ReadMonitor(char* filename)
{
    //TODO: Add your source code here
    FILE *p = fopen(filename,"r");
    if(!p)
        return;
    char buff[65536];
    fgets(buff,65535,p);
    initstr = buff;
	sscanf(buff,"(Init (FieldLength %lf)(FieldWidth %lf)(FieldHeight %lf)(GoalWidth %lf)(GoalDepth %lf)(GoalHeight %lf)",
		&FieldLength,
		&FieldWidth,
		&FieldHeight,
		&GoalWidth,
		&GoalDepth,
		&GoalHeight);

    while(!feof(p)){
        fgets(buff,65535,p);
        monitor.push_back(buff);
    }

    // set the 3D-View
    Predicate predicate;
    Parser::Parse(initstr, predicate);
    hMonitor.mGameState.ProcessInput(predicate);
    static bool lastMonitorOnly = false;
    if (!mMonitorOnly) {
        string oppName = "opp";
        for (int i = 0; i < monitor.size(); ++ i) {
            int pos = monitor[i].find("team_left"), p;
            if (pos != -1) {
                for (pos += string("team_left").size(); pos < monitor[i].size() && monitor[i][pos] == ' '; ++ pos);
                for (p = pos; p < monitor[i].size() && monitor[i][p] != ' ' && monitor[i][p] != ')'; ++ p);
                if (p < monitor[i].size() && (oppName = monitor[i].substr(pos, p - pos)) != "ZJUBase") {
                    mFieldReverse = true;
                    break;
                }
            }
            pos = monitor[i].find("team_right");
            if (pos != -1) {
                for (pos += string("team_right").size(); pos < monitor[i].size() && monitor[i][pos] == ' '; ++ pos);
                for (p = pos; p < monitor[i].size() && monitor[i][p] != ' ' && monitor[i][p] != ')'; ++ p);
                if (p < monitor[i].size() && (oppName = monitor[i].substr(pos, p - pos)) != "ZJUBase") {
                    mFieldReverse = false;
                    break;
                }
            }
        }
        hMonitor.mGameState.SetTeamnameLeft("ZJUBase");
        hMonitor.mGameState.SetTeamnameRight(oppName);
        // read the real WM from monitor.log
        hMonitor.mGameStateReal.ProcessInput(predicate);
        hMonitor.mGameStateReal = hMonitor.mGameState;
    } else {
        mFieldReverse = false;
        string oppName = "team_left";
        for (int i = 0; i < monitor.size(); ++ i) {
            int pos = monitor[i].find("team_left"), p;
            if (pos != -1) {
                for (pos += string("team_left").size(); pos < monitor[i].size() && monitor[i][pos] == ' '; ++ pos);
                for (p = pos; p < monitor[i].size() && monitor[i][p] != ' ' && monitor[i][p] != ')'; ++ p);
                if (p < monitor[i].size()) {
                    oppName = monitor[i].substr(pos, p - pos);
                    break;
                }
            }
        }
        hMonitor.mGameState.SetTeamnameLeft(oppName);
        oppName = "team_right";
        for (int i = 0; i < monitor.size(); ++ i) {
            int pos = monitor[i].find("team_right"), p;
            if (pos != -1) {
                for (pos += string("team_right").size(); pos < monitor[i].size() && monitor[i][pos] == ' '; ++ pos);
                for (p = pos; p < monitor[i].size() && monitor[i][p] != ' ' && monitor[i][p] != ')'; ++ p);
                if (p < monitor[i].size()) {
                    oppName = monitor[i].substr(pos, p - pos);
                    break;
                }
            }
        }
        hMonitor.mGameState.SetTeamnameRight(oppName);
        if (!lastMonitorOnly) {
            for (int i = 0; i < 4; ++ i) {
                swap(Monitor::sTeamColorLeft[i], Monitor::sTeamColorRight[i]);
            }
        }
    }
    lastMonitorOnly = mMonitorOnly;
}

void TForm1::SendWM(int pos)
{
    if (pos >= monitor.size()) {
        pos = monitor.size() - 1;
    }
    if (pos >= TrackBar1->Max) {
        pos = TrackBar1->Max - 1;
    }

    int i;
    Memo1->Lines->Clear();
    geoDebug.clear();
    if (mMonitorOnly) {
        Predicate predicate;
        Parser::Parse(monitor[pos], predicate);
        hMonitor.mGameState.ProcessInput(predicate);
        mFieldReverse = false;
        if (predicate.name == "Info") {
            MyPaint();
        }
        return;
    }
    if(TrackBar1->Max < 1 || TrackBar1->Position ==0)
        return;
    if(currentNumber<1||currentNumber>11)
        return;
    char buff[1024];
    string s;

    if(NMUDP1->RemotePort!=0)
        NMUDP1->SendBuffer(initstr.c_str(),initstr.size(),initstr.size());
//    if(connects>0)
//        ServerSocket1->Socket->Connections[connects-1]->SendText(initstr.c_str());
    for(i=0;i<LOG_NUM_LINE;i++){
        s += collWM[currentNumber-1][pos].str[i];
    }
   strWM = s;
//   MyPaint();
    if(NMUDP1->RemotePort!=0)
        NMUDP1->SendBuffer(s.c_str(),s.size(),s.size());
    s = "";
    string stmp1,stmp2;
    double time,time1,time2;
    sscanf(collWM[currentNumber-1][pos].str[1],"T %lf",&time);
    time1 = 0;

    for(i=0;i<(int)monitor.size();i++){
        stmp2 = monitor[i];
        sscanf(stmp2.c_str(),"(Info (time %lf",&time2);
        if(time2>time){
            if(time2-time<time-time1){
                s = stmp2;
                if(connects>0) {
                    ServerSocket1->Socket->Connections[connects-1]->SendText(s.c_str());
                }
                if(NMUDP1->RemotePort!=0)
                    NMUDP1->SendBuffer(s.c_str(),s.size(),s.size());
                strWMReal = s;
                break;
            }else{
                s = stmp1;
                if(connects>0) {
                    ServerSocket1->Socket->Connections[connects-1]->SendText(s.c_str());
                }
                if(NMUDP1->RemotePort!=0)
                    NMUDP1->SendBuffer(s.c_str(),s.size(),s.size());
                strWMReal = s;
                break;
            }

        }
        stmp1 = stmp2;
        time1 = time2;
    }
    //TODO: Add your source code here
    MyPaint();
}
void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
      int NumberBytes, AnsiString FromIP, int Port)
{
    NMUDP1->RemoteHost = FromIP;
    NMUDP1->RemotePort = Port;
    char buff[4096];

⌨️ 快捷键说明

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