📄 simulationserver.cpp
字号:
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: simulationserver.cpp,v 1.18 2008/03/26 10:31:00 hedayat Exp $ 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; version 2 of the License. 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., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "simulationserver.h"#include "simcontrolnode.h"#include <zeitgeist/logserver/logserver.h>#include <signal.h>#include <algorithm>#include <boost/bind.hpp>using namespace oxygen;using namespace zeitgeist;using namespace salt;using namespace std;using namespace boost;bool SimulationServer::mExit = false;void SimulationServer::CatchSignal(int sig_num){ if (sig_num == SIGINT) { signal(SIGINT, CatchSignal); SimulationServer::mExit = true; std::cout << "(SimulationServer) caught SIGINT. exiting.\n"; }}SimulationServer::SimulationServer() : Node(), mAdjustSpeed(false), mMaxStepsPerCycle(3), mThreadBarrier(0){ mSimTime = 0.0f; mSimStep = 0.2f; mAutoTime = true; mCycle = 0; mSumDeltaTime = 0; mArgC = 0; mArgV = 0; mMultiThreads = true; signal(SIGINT, CatchSignal);}SimulationServer::~SimulationServer(){}void SimulationServer::OnLink(){ RegisterCachedPath(mMonitorServer, "/sys/server/monitor"); if (mMonitorServer.expired()) { GetLog()->Error() << "(SimulationServer) ERROR: MonitorServer not found.\n"; } RegisterCachedPath(mGameControlServer, "/sys/server/gamecontrol"); if (mGameControlServer.expired()) { GetLog()->Error() << "(SimulationServer) ERROR: GameControlServer not found.\n"; } RegisterCachedPath(mSceneServer, "/sys/server/scene"); if (mSceneServer.expired()) { GetLog()->Error() << "(SimulationServer) ERROR: SceneServer not found.\n"; }}void SimulationServer::Quit(){ mExit = true;}bool SimulationServer::WantsToQuit(){ return mExit;}int SimulationServer::GetArgC(){ return mArgC;}char** SimulationServer::GetArgV(){ return mArgV;}float SimulationServer::GetTime(){ return mSimTime;}void SimulationServer::ResetTime(){ mSimTime = 0.0f;}float SimulationServer::GetSimStep(){ return mSimStep;}void SimulationServer::SetSimStep(float deltaTime){ mSimStep = deltaTime;}float SimulationServer::GetSumDeltaTime(){ return mSumDeltaTime;}void SimulationServer::SetAutoTimeMode(bool set){ mAutoTime = set;}bool SimulationServer::GetAutoTimeMode(){ return mAutoTime;}int SimulationServer::GetCycle(){ return mCycle;}bool SimulationServer::InitControlNode(const std::string& className, const std::string& name){ shared_ptr<Leaf> prevCtrNode = GetChild(name); if (prevCtrNode.get() != 0) { RemoveChildReference(prevCtrNode); GetLog()->Debug() << "(SimulationServer) removing previous SimControlNode instance " << name << "'\n"; } shared_ptr<SimControlNode> control = shared_dynamic_cast<SimControlNode>(GetCore()->New(className)); if (control.get() == 0) { GetLog()->Error() << "(SimulationServer) ERROR: " << "Unable to create '" << className << "'\n"; return false; } control->SetName(name); AddChildReference(control); GetLog()->Normal() << "(SimulationServer) SimControlNode '" << name << "' registered\n"; return true;}shared_ptr<SimControlNode>SimulationServer::GetControlNode(const string& controlName){ shared_ptr<SimControlNode> ctrNode = shared_dynamic_cast<SimControlNode>(GetChild(controlName)); if (ctrNode.get() == 0) { GetLog()->Normal() << "(SimulationServer) SimControlNode '" << controlName << "' not found\n"; } return ctrNode;}void SimulationServer::AdvanceTime(float deltaTime){ mSumDeltaTime += deltaTime;}void SimulationServer::Step(){ if ( mSceneServer.expired() || mGameControlServer.expired() ) { return; } if (mSimStep > 0) { // world is stepped in discrete steps float finalStep = 0; while (int(mSumDeltaTime*100) >= int(mSimStep*100)) { mSceneServer->PrePhysicsUpdate(mSimStep); mSceneServer->PhysicsUpdate(mSimStep); UpdateDeltaTimeAfterStep(); finalStep += mSimStep; } mSceneServer->PostPhysicsUpdate(); mGameControlServer->Update(finalStep); mSimTime += finalStep; } else { // simulate passed time in one single step mSceneServer->Update(mSumDeltaTime); mGameControlServer->Update(mSumDeltaTime); mSimTime += mSumDeltaTime; mSumDeltaTime = 0; }}void SimulationServer::ControlEvent(EControlEvent event){ int time = int(mSimTime*100); for ( TLeafList::iterator iter=begin(); iter != end(); ++iter ) { shared_ptr<SimControlNode> ctrNode = shared_dynamic_cast<SimControlNode>(*iter); if (ctrNode.get() == 0) { continue; } if ( int(ctrNode->GetTime()*100) > time ) continue; switch (event) { case CE_Init : ctrNode->InitSimulation(); break; case CE_Done : ctrNode->DoneSimulation(); break; case CE_StartCycle : ctrNode->StartCycle(); break; case CE_SenseAgent : ctrNode->SenseAgent(); break; case CE_ActAgent : ctrNode->ActAgent(); ctrNode->SetSimTime(mSimTime); break; case CE_EndCycle : ctrNode->EndCycle(); break; default: GetLog()->Error() << "(SimulationServer) ERROR: unknown control event " << event << "\n"; return; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -