📄 basiccoach.cpp
字号:
/* * Copyright 2002-2005, Mersad Team, Allameh Helli High School (NODET). * * 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. * * 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 Library General Public License for more details. * * This file is created by: Mohammad Salehe * and is modified by: Darioush Jalali * * Released on Monday 1 August 2005, 10 Mordad 1384 by Mersad RoboCup Team. * For more information please read README file.*/#include <assert.h>#include <iostream>#include <sstream>#include <fstream>#include "Config.h"#include "Logger.h"#include "BasicCoach.h"#include "SExpression.h"#include "SignalHandler.h"#include "OnlineConnection.h"#include "VirtualConnection.h"#include "SampleScenario.h"using namespace std;BasicCoach::BasicCoach(int argc, char **argv){ setConfigDefaults(); config["Agent"].setByFile("./Configs/Coach.conf"); if (!config["Agent"].setByArguments(argc, argv)) exit(1); worldModel = new CoachWorldModel(); isTrainer = config["Agent"]["Public"]["IsTrainer"].asBool(); freeFormCycleCommand = NULL; changePlayerCycleCommand = NULL; sigAlarmCounter = 0; trainer = NULL;}BasicCoach::~BasicCoach(){ delete worldModel;}void BasicCoach::run(){ if (config["Agent"]["Server"]["OfflinePlayer"].asBool()) {/* initVirtualConnection(); offlinePlayerManager(); closeVirtualConnection();*/ } else { SignalHandler::initSignalHandlers(this); initOnlineConnection(); if (isTrainer) { trainer = new SampleScenario(worldModel, &worldModelHistory, connection); } else { trainer = NULL; } SignalHandler::run(); SignalHandler::finalSignalHandlers(); closeOnlineConnection(); }}void BasicCoach::ok (const SExpression &exp) { LOG << "OK header received from server" << endl; LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; SExpAtomic *at =NULL; if (exp.size() > 1) { at = dynamic_cast<SExpAtomic *>(exp[1]); assert(at); string type = at->asString(); if (type == "team_names") okTeamNames(exp); else LOG << "Skipping _ok_ message with unknown header" << endl; }};void BasicCoach::okTeamNames (const SExpression &exp) { SExpression *expr =NULL; SExpAtomic *at =NULL; if (exp.size() > 2) { LOG << "Left Team exists" << endl; expr = dynamic_cast<SExpression * >(exp[2]); assert(expr); at = dynamic_cast<SExpAtomic * >((*expr)[0]); assert(at); assert(at->asString() == "team"); at = dynamic_cast<SExpAtomic * >((*expr)[1]); assert(at); assert(at->asString() == "l"); at = dynamic_cast<SExpAtomic * >((*expr)[2]); assert(at); LOG << "Setting left team name " << at->asString() << endl; worldModel->setLeftTeam(at->asString()); } if (exp.size() > 3) { LOG << "Right team exists" << endl; expr = dynamic_cast<SExpression * >(exp[3]); assert(expr); at = dynamic_cast<SExpAtomic * >((*expr)[0]); assert(at); assert(at->asString() == "team"); at = dynamic_cast<SExpAtomic * >((*expr)[1]); assert(at); assert(at->asString() == "r"); at = dynamic_cast<SExpAtomic * >((*expr)[2]); assert(at); LOG << "Setting right team name " << at->asString() << endl; worldModel->setRightTeam(at->asString()); }};void BasicCoach::seeGlobal(const SExpression &exp){ unsigned curCycle; SExpAtomic *at;// logEndInf(); at = dynamic_cast<SExpAtomic *>(exp[1]); assert(at); curCycle = at->asInt(); worldModel->setTimer().resetCycle(curCycle); LOG.newCycle(); DRAW.newCycle(); if (!(curCycle % 20)) { // every twenty cycles we check again for new teams updateTeamNames(); } LOG << "RecStr: " << exp.toString() << endl;// OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->setCurTime(worldModel->getCurTime() + 1); worldModel->resetObjects(); worldModel->parseSeeGlobal(exp); // checkForLosts(); worldModel->setCurCycle(curCycle);// worldModel->updateSeeDeltaCyclesAfterSenseBody(); worldModel->updatePlayModeRemainCycleAfterSenseBody(); worldModel->logObjects(); worldModelHistory.saveWorldModel(worldModel, worldModel->getCurTime());// //TODO}void BasicCoach::hear(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->parseHear(exp);}void BasicCoach::fullState(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl;}void BasicCoach::serverParam(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->initServerParam(exp);}void BasicCoach::playerParam(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->initPlayerParam(exp);}void BasicCoach::playerType(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->initPlayerType(exp); LOG << "Sending Eye On" << endl; Command *eye = new EyeCommand(AT_BASIC_COACH, true); connection->send(eye); delete eye;}void BasicCoach::init(const SExpression &exp){ string logFileName, offLogFileName; SExpAtomic *at; at = dynamic_cast<SExpAtomic *>(exp[0]); assert(at); assert(at->toString() == "init"); if (isTrainer) worldModel->setFieldSide(SI_LEFT); else { at = dynamic_cast<SExpAtomic *>(exp[1]); assert(at); if (at->toString()[0] == 'l') worldModel->setFieldSide(SI_LEFT); else { assert(at->toString()[0] == 'r'); worldModel->setFieldSide(SI_RIGHT); } } // Initing log file if ((config["Agent"]["AgentLog"] ["OutputLogAddress"].asString().end() - 1)[0] != '/') logFileName = "/"; else logFileName = ""; logFileName += config["Agent"]["Public"]["TeamName"].asString(); logFileName += (char)('C'); logFileName += ".log"; if (config["Agent"]["AgentLog"]["LogToFile"].asBool()) logger.add("MainLog", new LogFile( config["Agent"]["AgentLog"] ["OutputLogAddress"].asString() + logFileName, &worldModel->getTimer())); else logger.add("MainLog", new LogNull()); offLogFileName = logFileName + ".off"; if (config["Agent"]["AgentLog"]["OffLogToFile"].asBool()) logger.add("OfflineLog", new LogFile( config["Agent"]["AgentLog"] ["OutputLogAddress"].asString() + offLogFileName, &worldModel->getTimer())); else logger.add("OfflineLog", new LogNull()); LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; cout << "Coach is connected." << endl; if (logger["MainLog"].isOutValid()) cout << " Log: " << config["Agent"]["AgentLog"] ["OutputLogAddress"].asString() + logFileName << endl; if (logger["OfflineLog"].isOutValid()) cout << " OffLog: " << config["Agent"]["AgentLog"] ["OutputLogAddress"].asString() + offLogFileName << endl; /// filling the team names if (isTrainer) { LOG << "[t0] Requesting team names from server" << endl; updateTeamNames(); }}void BasicCoach::updateTeamNames() { if ((worldModel->getLeftTeam() =="NULL" )|| (worldModel->getRightTeam() =="NULL" )) { LOG << "Null team names exist, checking.." <<endl; Command * command = new TeamNamesCommand(AT_BASIC_COACH); connection->send(command); delete command; } else { LOG << "Team names are OK, not checking.. (" << worldModel->getLeftTeam() << ") (" << worldModel->getRightTeam() << ")" <<endl; }}; void BasicCoach::think(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl;}void BasicCoach::changePlayerType(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->parseChangePlayerType(exp);}bool BasicCoach::sigAlrmHandler(){/* sigAlarmCounter++; if (sigAlarmCounter == 200) { Command *eye = new EyeCommand(AT_BASIC_COACH, true); connection->send(eye); delete eye; } else if (sigAlarmCounter == 250) { } */ return synchronize();}bool BasicCoach::sigIntHandler(){ LOG << "Interrupt Handler." << endl; cout << "Interrupt Handler." << endl; return false;}bool BasicCoach::sigIOHandler(){ string message; while (connection->receive(message) == 1) { curReceiveMS = worldModel->getTimer().now(); //std::cout << "SCRREC : " << message << std::endl; unsigned i = 0; SExpression exp(message, i);// assert(i + 1 == message.length()); string header = ((SExpAtomic *)exp[0])->asString(); if (header == "see_global") seeGlobal(exp); else if (header == "hear") hear(exp); else if (header == "fullstate") fullState(exp); else if (header == "server_param") serverParam(exp); else if (header == "player_param") playerParam(exp); else if (header == "player_type")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -