📄 basicagent.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: Ahmad Boorghany * * 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 <Command.h>#include <BasicAgent.h>#include <SExpression.h>#include <SignalHandler.h>#include <OnlineConnection.h>#include <VirtualConnection.h>using namespace std;BasicAgent::BasicAgent(int argc, char **argv){ setConfigDefaults(); config["Agent"].setByFile("./Configs/Agent.conf"); if (!config["Agent"].setByArguments(argc, argv)) exit(1); worldModel = new WorldModel(); sayCycleCommand = NULL; bodyCycleCommand = NULL; headCycleCommand = NULL; pointToCycleCommand = NULL; attentionCycleCommand = NULL; changeViewCycleCommand = NULL; lastBodyCycleCommand = new EmptyCommand(AT_NONE); setBodyCycleCommandDecidePermitted(true); setBodyCycleCommandSendPermitted(true); setHeadCycleCommandDecidePermitted(true); setHeadCycleCommandSendPermitted(true);}BasicAgent::~BasicAgent(){ delete worldModel;}void BasicAgent::run(){ if (config["Agent"]["Server"]["OfflinePlayer"].asBool()) { initVirtualConnection(); offlinePlayerManager(); closeVirtualConnection(); } else { SignalHandler::initSignalHandlers(this); initOnlineConnection(); SignalHandler::run(); SignalHandler::finalSignalHandlers(); closeOnlineConnection(); }}void BasicAgent::senseBody(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(); LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->setCurTime(worldModel->getCurTime() + 1); worldModel->setBody().parseSenseBody(exp, worldModel->getFieldSide()); checkForLosts(); if (!isBodyCycleCommandLost()) worldModel->setBody().simulateByAction(bodyCycleCommand, true); worldModel->setBody().simulateByDynamics(true); worldModel->setBody().completeParseSenseBody(); const Body lastBody = worldModel->getBody(); // for ball simulate if (!isBodyCycleCommandLost()) worldModel->simulateObjects(lastBody, bodyCycleCommand); else worldModel->simulateObjects(lastBody, NULL); worldModel->setCurCycle(worldModel->getBody().getSenseBodyCycle()); worldModel->updateSeeDeltaCyclesAfterSenseBody(); worldModel->updatePlayModeRemainCycleAfterSenseBody(); worldModel->updateAfterSenseBody(); worldModel->logObjects();}void BasicAgent::see(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->resetObjects(); worldModel->parseSee(exp); worldModel->updateObjects(); worldModel->updateSeeDeltaCyclesAfterSee(); worldModel->logObjects();}void BasicAgent::hear(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->parseHear(exp);}void BasicAgent::fullState(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl;}void BasicAgent::serverParam(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->initServerParam(exp);}void BasicAgent::playerParam(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->initPlayerParam(exp);}void BasicAgent::playerType(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->initPlayerType(exp);}void BasicAgent::init(const SExpression &exp){ string logFileName, offLogFileName; SExpAtomic *at; int bodyUniNum; at = dynamic_cast<SExpAtomic *>(exp[0]); assert(at); assert(at->toString() == "init"); 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); } at = dynamic_cast<SExpAtomic *>(exp[2]); assert(at); assert(at->asInt() <= FULL_PLAYERS_NUM); bodyUniNum = at->asInt(); worldModel->setBody().setUniNum(bodyUniNum); worldModel->setBody().setGoalie( config["Agent"]["Public"]["IsGoalie"].asBool()); worldModel->replaceBodyInPlayersList(bodyUniNum); at = dynamic_cast<SExpAtomic *>(exp[3]); assert(at); assert(at->toString() == "before_kick_off"); // Initing log file if ((config["Agent"]["AgentLog"] ["OutputLogAddress"].asString().end() - 1)[0] != '/') logFileName = "/"; else logFileName = ""; logFileName += config["Agent"]["Public"]["TeamName"].asString(); if (bodyUniNum < 10) logFileName += (char)('0' + bodyUniNum); else logFileName += (char)('A' + bodyUniNum - 10); 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()); string drawLogFileName = logFileName + ".draw"; if (config["Agent"]["AgentLog"]["DrawLogToFile"].asBool()) logger.add("DrawLog", new LogFile( config["Agent"]["AgentLog"] ["DrawLogAddress"].asString() + drawLogFileName, &worldModel->getTimer())); else logger.add("DrawLog", new LogNull()); LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; cout << "Player " << bodyUniNum << " 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; Command *clangVer = new CLangVersionCommand(AT_BASIC_AGENT, 7, 9); LOG << "SendStr: " << clangVer->toString() << endl; connection->send(clangVer); delete clangVer;}void BasicAgent::think(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl;}void BasicAgent::changePlayerType(const SExpression &exp){ LOG << "RecStr: " << exp.toString() << endl; OFFLOG << "RecStr: " << exp.toString() << endl; worldModel->parseChangePlayerType(exp);}bool BasicAgent::sigAlrmHandler(){ return synchronize();}bool BasicAgent::sigIntHandler(){ LOG << "Interrupt Handler." << endl; cout << "Interrupt Handler." << endl; return false;}bool BasicAgent::sigIOHandler(){ bool iReceivedSomething = false; string message; while (connection->receive(message) == 1) { iReceivedSomething = true; curReceiveMS = worldModel->getTimer().now(); unsigned i = 0; SExpression exp(message, i);// assert(i + 1 == message.length()); string header = ((SExpAtomic *)exp[0])->asString(); if (header == "sense_body") senseBody(exp); else if (header == "see") see(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") playerType(exp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -