📄 basicagent.cpp
字号:
else if (header == "init") init(exp); else if (header == "think") think(exp); else if (header == "change_player_type") changePlayerType(exp); else LOG << "Skipping message with unknown header: " << exp.toString() << endl; } if (iReceivedSomething) decide(); return true;}bool BasicAgent::sigKillHandler(){ LOG << "Kill Handler." << endl; cout << "Kill Handler." << endl; return false;}bool BasicAgent::sigQuitHandler(){ LOG << "Quit Handler." << endl; cout << "Quit Handler." << endl; return false;}bool BasicAgent::sigSegVHandler(){ LOG << "Segmentation Violation Handler." << endl; cout << "Segmentation Violation Handler." << endl; exit(1); return false;}bool BasicAgent::sigTermHandler(){ LOG << "Termination Handler." << endl; cout << "Termination Handler." << endl; return false;}SignalsMask BasicAgent::signalsMask(){ SignalsMask result = SIG_INT | SIG_KILL | SIG_QUIT | SIG_SEGV | SIG_TERM; // if (online) result |= SIG_ALRM | SIG_IO; return result;}void BasicAgent::sigAlrmTiming(long &begin, long &interval){ begin = interval = 10;}void BasicAgent::setConfigDefaults(){ config.add("Agent"); config["Agent"].addGroup("Server"); config["Agent"]["Server"].add("HostName", "localhost"); config["Agent"]["Server"].add("PlayerPort", "6000"); config["Agent"]["Server"].add("Version", "9.4.5"); config["Agent"]["Server"].add("OfflinePlayer", "False"); config["Agent"].addGroup("Public"); config["Agent"]["Public"].add("TeamName", "Mersad"); config["Agent"]["Public"].add("IsGoalie", "False"); config["Agent"]["Public"].add("UniformNum", "0"); config["Agent"]["Public"].add("LatestTrick", "False"); config["Agent"].addGroup("AgentLog"); config["Agent"]["AgentLog"].add("LogToFile", "False"); config["Agent"]["AgentLog"].add("OffLogToFile", "False"); config["Agent"]["AgentLog"].add("InputLogAddress", "."); config["Agent"]["AgentLog"].add("OutputLogAddress", ".");}void BasicAgent::offlinePlayerManager(){ bool breakWhile = false; unsigned logHistoryNum = 0; ifstream inputLog; LogHistory logHistories[MAX_LOG_HOSTORY]; VirtualConnection *virtualConnection = dynamic_cast<VirtualConnection *>(connection); assert(virtualConnection); VirtualTimer *virtualTimer = dynamic_cast<VirtualTimer *>(&worldModel->setTimer()); if (config["Agent"]["Public"]["UniformNum"].asInt() <= 0) { cout << "Offline Player Manager : you must specify player number." << endl << "Type \"--number pnum\" after command." << endl; return; } if (!initOfflinePlayerInputLog(inputLog)) { cout << "Offline Player Manager : can not open input log file for player " << config["Agent"]["Public"]["UniformNum"].asInt() << endl; return; } long prevCycleSizeMS, cycleSizeMS = 0; while (!breakWhile) { // Finding "EndInf" title and making logHistory logHistoryNum = 0; prevCycleSizeMS = cycleSizeMS; while (1) { logHistories[logHistoryNum].milliSecond = parseOfflinePlayerLogLine(inputLog, logHistories[logHistoryNum].title, logHistories[logHistoryNum].message); if (logHistories[logHistoryNum].milliSecond == -1) // end of file { setBodyCycleCommandDecidePermitted(true); setBodyCycleCommandSendPermitted(true); setHeadCycleCommandDecidePermitted(true); setHeadCycleCommandSendPermitted(true); breakWhile = true; break; } else if (logHistories[logHistoryNum].title == "EndInf") { if (logHistories[logHistoryNum].message[0] - '0') setBodyCycleCommandDecidePermitted(true); else setBodyCycleCommandDecidePermitted(false); if (logHistories[logHistoryNum].message[1] - '0') setBodyCycleCommandSendPermitted(true); else setBodyCycleCommandSendPermitted(false); if (logHistories[logHistoryNum].message[2] - '0') setHeadCycleCommandDecidePermitted(true); else setHeadCycleCommandDecidePermitted(false); if (logHistories[logHistoryNum].message[3] - '0') setHeadCycleCommandSendPermitted(true); else setHeadCycleCommandSendPermitted(false); cycleSizeMS = logHistories[logHistoryNum].milliSecond; break; } else logHistoryNum++; } // Running LogHistory unsigned i; for (i = 0; i < logHistoryNum; i++) { if (logHistories[i].title == "RecStr") { virtualConnection->addMessage(logHistories[i].message); if (logHistories[i].message.substr(1, 10) == "sense_body") virtualTimer->setCurMS(prevCycleSizeMS); else virtualTimer->setCurMS(logHistories[i].milliSecond); for (i = i + 1; i < logHistoryNum; i++) if (logHistories[i].title == "RecStr" && logHistories[i].message.substr(1, 4) == "hear" && logHistories[i].message.find("referee") > logHistories[i].message.size()) virtualConnection->addMessage(logHistories[i].message); else { i--; // This message is not Hear so I fix my change. break; } sigIOHandler(); } else if (logHistories[i].title == "EmgSend") { virtualTimer->setCurMS(logHistories[i].milliSecond); sigAlrmHandler(); } } } inputLog.close(); LOG << "End of input offline log." << endl;}bool BasicAgent::initOfflinePlayerInputLog(ifstream &inputLog){ ostringstream ss; if (config["Agent"]["Public"]["UniformNum"].asInt() < 10) ss << config["Agent"]["AgentLog"]["InputLogAddress"].asString() << "/" << config["Agent"]["Public"]["TeamName"].asString() << config["Agent"]["Public"]["UniformNum"].asInt() << ".log.off.in"; else ss << config["Agent"]["AgentLog"]["InputLogAddress"].asString() << "/" << config["Agent"]["Public"]["TeamName"].asString() << (char)(config["Agent"]["Public"]["UniformNum"].asInt() - 10 + 'A') << ".log.off.in"; inputLog.open(ss.str().c_str()); return inputLog;}int BasicAgent::parseOfflinePlayerLogLine(ifstream &inputLog, string &title, string &message){ string line; string temp; int milliSecond; if (getline(inputLog, line)) { unsigned colon = line.find(':'); istringstream ss(line.substr(0, colon)); ss >> milliSecond >> temp >> title; if (colon < line.size()) message = line.substr(colon + 2); else message = ""; return milliSecond; } return -1;}void BasicAgent::initOnlineConnection(){ connection = new OnlineConnection( config["Agent"]["Server"]["HostName"].asString(), config["Agent"]["Server"]["PlayerPort"].asInt()); Command *init = new InitCommand(AT_BASIC_AGENT, config["Agent"]["Public"]["TeamName"].asString(), config["Agent"]["Server"]["Version"].asString(), config["Agent"]["Public"]["IsGoalie"].asBool()); connection->send(init); delete init;}void BasicAgent::closeOnlineConnection(){ Command *bye = new ByeCommand(AT_BASIC_AGENT); connection->send(bye); delete bye; delete connection; connection = NULL;}void BasicAgent::initVirtualConnection(){ connection = new VirtualConnection();}void BasicAgent::closeVirtualConnection(){ delete connection; connection = NULL;}// Getting functionsbool BasicAgent::isBodyCycleCommandDecidePermitted() const{ return bodyCycleCommandDecidePermittedFlag;}bool BasicAgent::isBodyCycleCommandSendPermitted() const{ return bodyCycleCommandSendPermittedFlag;}bool BasicAgent::isHeadCycleCommandDecidePermitted() const{ return headCycleCommandDecidePermittedFlag;}bool BasicAgent::isHeadCycleCommandSendPermitted() const{ return headCycleCommandSendPermittedFlag;}const WorldModel &BasicAgent::getWorldModel() const{ return *worldModel;}// Setting functionsvoid BasicAgent::setBodyCycleCommandDecidePermitted(bool bodyCycleCommandDecidePermittedFlagArg){ bodyCycleCommandDecidePermittedFlag = bodyCycleCommandDecidePermittedFlagArg;}void BasicAgent::setBodyCycleCommandSendPermitted(bool bodyCycleCommandSendPermittedFlagArg){ bodyCycleCommandSendPermittedFlag = bodyCycleCommandSendPermittedFlagArg;}void BasicAgent::setHeadCycleCommandDecidePermitted(bool headCycleCommandDecidePermittedFlagArg){ headCycleCommandDecidePermittedFlag = headCycleCommandDecidePermittedFlagArg;}void BasicAgent::setHeadCycleCommandSendPermitted(bool headCycleCommandSendPermittedFlagArg){ headCycleCommandSendPermittedFlag = headCycleCommandSendPermittedFlagArg;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -