?? syncedagent.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 <iostream>#include <unistd.h>#include <Logger.h>#include <SyncedAgent.h>#include <OnlineConnection.h>using namespace std;SyncedAgent::SyncedAgent(int argc, char **argv): BasicAgent(argc, argv){ seeReceiveMS = 0; seeReceivedFlag = false; quickDecideFlag = false; bodyCycleCommandSentFlag = false; headCycleCommandSentFlag = false; bodyCycleCommandLostFlag = false; headCycleCommandLostFlag = false;}SyncedAgent::~SyncedAgent(){}void SyncedAgent::senseBody(const SExpression &exp){ BasicAgent::senseBody(exp); if (isHeadCycleCommandLost() && isHeadCycleCommandSent()) recoverSeeReceiveMSAfterChangeView(changeViewCycleCommand); delete lastBodyCycleCommand; lastBodyCycleCommand = bodyCycleCommand; bodyCycleCommand = NULL; if (!lastBodyCycleCommand) // Just happen when we hadn't any time // in last cycle to start deciding at least. lastBodyCycleCommand = new EmptyCommand(AT_NONE); setBodyCycleCommandSent(false); delete sayCycleCommand; sayCycleCommand = NULL; delete pointToCycleCommand; pointToCycleCommand = NULL; delete changeViewCycleCommand; changeViewCycleCommand = NULL; delete attentionCycleCommand; attentionCycleCommand = NULL; delete headCycleCommand; headCycleCommand = NULL; setHeadCycleCommandSent(false); setSenseBodyReceived(true); setQuickDecide(false); setSeeReceived(false); setSeeReceiveMSAfterSenseBody();// usleep(1); MEISAM2005}void SyncedAgent::see(const SExpression &exp){ BasicAgent::see(exp); setSeeReceived(true); setSeeReceiveMSAfterSee();}void SyncedAgent::hear(const SExpression &exp){ BasicAgent::hear(exp);}void SyncedAgent::fullState(const SExpression &exp){ BasicAgent::fullState(exp);}void SyncedAgent::serverParam(const SExpression &exp){ BasicAgent::serverParam(exp);}void SyncedAgent::playerParam(const SExpression &exp){ BasicAgent::playerParam(exp);}void SyncedAgent::playerType(const SExpression &exp){ BasicAgent::playerType(exp);}void SyncedAgent::init(const SExpression &exp){ BasicAgent::init(exp);}void SyncedAgent::think(const SExpression &exp){ BasicAgent::think(exp);}void SyncedAgent::changePlayerType(const SExpression &exp){ BasicAgent::changePlayerType(exp);}bool SyncedAgent::decide(){ LOG << "SyncedAgent::decide" << endl; if (!isSenseBodyReceived()) return true; string queue = ""; // bodyCycleCommand Deciding if (worldModel->getCurTime() < BODY_CYCLE_COMMAND_LATE || worldModel->getBody().getTackleExpires() > 0) bodyCycleCommand = new EmptyCommand(AT_NONE); if (isBodyCycleCommandDecidePermitted()) {// LOG << "SyncedAgent:: permission OK..." << endl; if (!bodyCycleCommand) {// LOG << "SyncedAgent:: bodyCycleCommand ... OK ...." << endl; if (isSeeReceived()) {// LOG << "SyncedAgent:: seeReceived ... OK ...." << endl; bodyDecide(getQuickLevel()); } else if (getSeeReceiveMS() > (SENSE_BODY_STEP / 2 - 3)) { bool wmUpdateNecessary = bodyDecide(getQuickLevel()); if (getSeeReceiveMS() < (SENSE_BODY_STEP - 3) && wmUpdateNecessary) setQuickDecide(true); } } else if (isSeeReceived() && isQuickDecide() && !isBodyCycleCommandSent()) { LOG << "SyncedAgent:: second if .... OK ..." <<endl; bodyDecide(getQuickLevel()); setQuickDecide(false); } } // bodyCycleCommand Sending if (bodyCycleCommand && isBodyCycleCommandSendPermitted() && !isBodyCycleCommandSent() && !isQuickDecide() && !isLateForSending()) { queue += bodyCycleCommand->toString(); setBodyCycleCommandSent(true); } // headCycleCommand and sayCycleCommand and PointToCycleCommand Deciding if (worldModel->getCurTime() < HEAD_CYCLE_COMMAND_LATE) { headCycleCommand = new EmptyCommand(AT_NONE); changeViewCycleCommand = new EmptyCommand(AT_NONE); sayCycleCommand = new EmptyCommand(AT_NONE); attentionCycleCommand = new EmptyCommand(AT_NONE); pointToCycleCommand = new EmptyCommand(AT_NONE); } if (!headCycleCommand && isHeadCycleCommandDecidePermitted()) { if (isSeeReceived() || getSeeReceiveMS() > (SENSE_BODY_STEP - 3)) { headDecide(); sayDecide(); pointToDecide(); } } // headCycleCommand and sayCycleCommand and PointToCycleCommand Sending if (headCycleCommand && isHeadCycleCommandSendPermitted() && !isHeadCycleCommandSent() && !isLateForSending()) { queue += headCycleCommand->toString(); queue += changeViewCycleCommand->toString(); setHeadCycleCommandSent(true); queue += sayCycleCommand->toString(); queue += attentionCycleCommand->toString(); queue += pointToCycleCommand->toString(); setSeeReceiveMSAfterChangeView(changeViewCycleCommand); } if (queue != "") { connection->send(queue); LOG << "SendStr: " << queue << endl; } return true;}bool SyncedAgent::synchronize(){// LOG << "SyncedAgent::synchronize" << endl; string message; if (dynamic_cast<OnlineConnection *>(connection) && connection->receive(message) == 1) { dynamic_cast<OnlineConnection *>(connection)->setLocalBuffer(message); return true; } string queue = ""; bool emergencySendFlag = false; // emergency sending system if (isSenseBodyReceived() && worldModel->getTimer().now() < (SENSE_BODY_STEP - 2) && ((worldModel->getTimer().now() > getSeeReceiveMS() + (SENSE_BODY_STEP / 6.00) && getSeeReceiveMS() > (SENSE_BODY_STEP / 2.00)) || worldModel->getTimer().now() > (SENSE_BODY_STEP * 0.90))) { if (bodyCycleCommand && !isBodyCycleCommandSent()) { LOG << "Emergency Sending bodyCycleCommand." << endl; queue += bodyCycleCommand->toString(); setBodyCycleCommandSent(true); emergencySendFlag = true; } if (!headCycleCommand && worldModel->getCurTime() >= HEAD_CYCLE_COMMAND_LATE) { LOG << "Emergency Sending headCycleCommand." << endl; headDecide(); queue += headCycleCommand->toString(); queue += changeViewCycleCommand->toString(); setHeadCycleCommandSent(true); sayDecide(); queue += sayCycleCommand->toString(); queue += attentionCycleCommand->toString(); pointToDecide(); queue += pointToCycleCommand->toString(); setSeeReceiveMSAfterChangeView(changeViewCycleCommand); emergencySendFlag = true; } } if (queue != "") { connection->send(queue); LOG << "SendStr: " << queue << endl; } if (emergencySendFlag) OFFLOG << "EmgSend" << endl; if (worldModel->getTimer().now() > SERVER_TIME_OUT) { LOG << "Server does not respond in the expected time." << endl; cout << "Server does not respond in the expected time." << endl; return false; } return true;}bool SyncedAgent::isLateForSending() const{ if (worldModel->getTimer().now() > (SENSE_BODY_STEP - 2)) return true; return false;}void SyncedAgent::setSeeReceiveMSAfterSee(){ switch (worldModel->getBody().getVMWidth()) { case VMW_NARROW: seeReceiveMS = curReceiveMS + (SEE_STEP / 2); break; case VMW_NORMAL: seeReceiveMS = curReceiveMS + SEE_STEP; break; case VMW_WIDE: seeReceiveMS = curReceiveMS + (SEE_STEP * 2); break; } LOG << "seeReceiveMS: " << seeReceiveMS << endl;}void SyncedAgent::setSeeReceiveMSAfterSenseBody(){ seeReceiveMS -= SENSE_BODY_STEP; LOG << "seeReceiveMS: " << seeReceiveMS << endl;}void SyncedAgent::setSeeReceiveMSAfterChangeView( const Command *command){ const ChangeViewCommand *changeViewCommand = dynamic_cast<const ChangeViewCommand *>(command); if (!changeViewCommand) return; seeReceiveMS = getLastSeeReceiveMS(); switch(changeViewCommand->getWidth()) { case VMW_NARROW: seeReceiveMS += (int)(SEE_STEP / 2); break; case VMW_NORMAL: seeReceiveMS += SEE_STEP; break; case VMW_WIDE: seeReceiveMS += SEE_STEP * 2; break; } LOG << "seeReceiveMS: " << seeReceiveMS << endl;}void SyncedAgent::recoverSeeReceiveMSAfterChangeView(const Command *command){ const ChangeViewCommand *changeViewCommand = dynamic_cast<const ChangeViewCommand *>(command); if (!changeViewCommand) return; seeReceiveMS = getLastSeeReceiveMS(); switch(changeViewCommand->getWidth()) { case VMW_NARROW: seeReceiveMS -= (int)(SEE_STEP / 2); break; case VMW_NORMAL: seeReceiveMS -= SEE_STEP; break; case VMW_WIDE: seeReceiveMS -= SEE_STEP * 2; break; }}int SyncedAgent::getLastSeeReceiveMS() const{ switch (worldModel->getBody().getVMWidth()) { case VMW_NARROW: return (int)(seeReceiveMS - (SEE_STEP / 2)); case VMW_NORMAL: return (int)(seeReceiveMS - SEE_STEP); case VMW_WIDE: return (int)(seeReceiveMS - SEE_STEP * 2); } return 0;}void SyncedAgent::logEndInf(){ LOG << "End of cycle." << endl; OFFLOG << "EndInf: " << (int)(bodyCycleCommand != NULL) << (int)(isBodyCycleCommandSent()) << (int)(headCycleCommand != NULL) << (int)(isHeadCycleCommandSent()) << endl;}ViewModeWidth SyncedAgent::getSyncedWide() const{ if (getLastSeeReceiveMS() <= (SENSE_BODY_STEP / 2.0) - (SENSE_BODY_STEP / 20.0)) return VMW_WIDE; return VMW_NARROW;}ViewModeWidth SyncedAgent::getSyncedNormal() const{ if (getLastSeeReceiveMS() >= (SENSE_BODY_STEP / 2.0) - (SENSE_BODY_STEP / 6.66) && getLastSeeReceiveMS() < (SENSE_BODY_STEP / 2.0) - (SENSE_BODY_STEP / 50.0)) return VMW_NARROW; return VMW_NORMAL;}ViewModeWidth SyncedAgent::getSyncedNarrow() const{ if (getLastSeeReceiveMS() <= (SENSE_BODY_STEP / 2.0) - (SENSE_BODY_STEP / 6.66)) return VMW_NORMAL; return VMW_NARROW;}int SyncedAgent::getQuickLevel(){ if (worldModel->getTimer().now() < ZERO_QUICK_LEVEL_MS) return 0; return 1;}void SyncedAgent::checkForLosts(){ // Checking for bodyCycleCommand setBodyCycleCommandLost(false); if (bodyCycleCommand && !isBodyCycleCommandSent()) { setBodyCycleCommandLost(true); LOG << "*** Body command didn't send ***" << endl; } else if (isBodyCycleCommandSent()) { if ((dynamic_cast<KickCommand *>(bodyCycleCommand) && !worldModel->getBody().isKickCountChanged()) || (dynamic_cast<DashCommand *>(bodyCycleCommand) && !worldModel->getBody().isDashCountChanged()) || (dynamic_cast<TurnCommand *>(bodyCycleCommand) && !worldModel->getBody().isTurnCountChanged()) || (dynamic_cast<CatchCommand *>(bodyCycleCommand) && !worldModel->getBody().isCatchCountChanged()) || (dynamic_cast<MoveCommand *>(bodyCycleCommand) && !worldModel->getBody().isMoveCountChanged()) || (dynamic_cast<TackleCommand *>(bodyCycleCommand) && !worldModel->getBody().isTackleCountChanged())) { if ((dynamic_cast<KickCommand *>(lastBodyCycleCommand) && worldModel->getBody().isKickCountChanged()) || (dynamic_cast<DashCommand *>(lastBodyCycleCommand) && worldModel->getBody().isDashCountChanged()) || (dynamic_cast<TurnCommand *>(lastBodyCycleCommand) && worldModel->getBody().isTurnCountChanged()) || (dynamic_cast<CatchCommand *>(lastBodyCycleCommand) && worldModel->getBody().isCatchCountChanged()) || (dynamic_cast<MoveCommand *>(lastBodyCycleCommand) && worldModel->getBody().isMoveCountChanged()) || (dynamic_cast<TackleCommand *>(lastBodyCycleCommand) && worldModel->getBody().isTackleCountChanged())) { delete bodyCycleCommand; bodyCycleCommand = lastBodyCycleCommand; lastBodyCycleCommand = new EmptyCommand(AT_NONE); LOG << "*** Last losted Body command executed ***" << endl; } else { setBodyCycleCommandLost(true); LOG << "*** Body command lost ***" << endl; } } } // Checking for headCycleCommand setHeadCycleCommandLost(false); if (headCycleCommand && !isHeadCycleCommandSent()) { setHeadCycleCommandLost(true); LOG << "*** Head command didn't send ***" << endl; } else if (isHeadCycleCommandSent() && !worldModel->getBody().isFocusCountChanged()) { if (dynamic_cast<TurnNeckCommand *>(headCycleCommand) && !worldModel->getBody().isTurnNeckCountChanged()) { setHeadCycleCommandLost(true); LOG << "*** Head command lost ***" << endl; } }}// Getting functionsint SyncedAgent::getSeeReceiveMS() const{ return seeReceiveMS;}bool SyncedAgent::isSeeReceived() const{ return seeReceivedFlag;}bool SyncedAgent::isSenseBodyReceived() const{ return senseBodyReceivedFlag;}bool SyncedAgent::isQuickDecide() const{ return quickDecideFlag;}bool SyncedAgent::isBodyCycleCommandSent() const{ return bodyCycleCommandSentFlag;}bool SyncedAgent::isHeadCycleCommandSent() const{ return headCycleCommandSentFlag;}bool SyncedAgent::isBodyCycleCommandLost() const{ return bodyCycleCommandLostFlag;}bool SyncedAgent::isHeadCycleCommandLost() const{ return headCycleCommandLostFlag;}// Setting functionsvoid SyncedAgent::setSeeReceiveMS(int seeReceiveMSArg){ seeReceiveMS = seeReceiveMSArg;}void SyncedAgent::setSeeReceived(bool seeReceivedFlagArg){ seeReceivedFlag = seeReceivedFlagArg;}void SyncedAgent::setSenseBodyReceived(bool senseBodyReceivedFlagArg){ senseBodyReceivedFlag = senseBodyReceivedFlagArg;}void SyncedAgent::setQuickDecide(bool quickDecideFlagArg){ quickDecideFlag = quickDecideFlagArg;}void SyncedAgent::setBodyCycleCommandSent(bool bodyCycleCommandSentFlagArg){ bodyCycleCommandSentFlag = bodyCycleCommandSentFlagArg;}void SyncedAgent::setHeadCycleCommandSent(bool headCycleCommandSentFlagArg){ headCycleCommandSentFlag = headCycleCommandSentFlagArg;}void SyncedAgent::setBodyCycleCommandLost(bool bodyCycleCommandLostFlagArg){ bodyCycleCommandLostFlag = bodyCycleCommandLostFlagArg;}void SyncedAgent::setHeadCycleCommandLost(bool headCycleCommandLostFlagArg){ headCycleCommandLostFlag = headCycleCommandLostFlagArg;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -