本文整理汇总了C++中Organism::addRobot方法的典型用法代码示例。如果您正苦于以下问题:C++ Organism::addRobot方法的具体用法?C++ Organism::addRobot怎么用?C++ Organism::addRobot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Organism
的用法示例。
在下文中一共展示了Organism::addRobot方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
/**
* Check whether the organism is still connected
*/
std::vector<Organism*> Organism::checkIntegrity() {
std::vector<Organism*> orgs = std::vector<Organism*>();
if (!isActive()) {
return orgs;
}
std::vector<RobotAgentPtr> not_visited = std::vector<RobotAgentPtr>(this->agents);
std::vector<RobotAgentPtr> loners = std::vector<RobotAgentPtr>();
std::vector<RobotAgentPtr> connected = std::vector<RobotAgentPtr>();
std::stack<RobotAgentPtr> s = std::stack<RobotAgentPtr>();
// Tree visiting algorithm using a stack
// All nodes of the "tree" should be visited, otherwise
// the organism is falling apart
// Repeat until all robots in the (ex)organism are accounted for
while (!not_visited.empty()) {
s.push(not_visited.front());
while (!s.empty()) {
RobotAgentPtr a = s.top();
s.pop();
// This robot has been visited
std::vector<RobotAgentPtr>::iterator it;
it = std::find(not_visited.begin(), not_visited.end(), a);
if (it != not_visited.end()) {
not_visited.erase(it);
}
// so it is connected to the current organism
connected.push_back(a);
// Add the neighbours of this robot to the stack
std::vector<RobotAgentPtr>::iterator it2;
for (it2 = a->getConnected()->begin(); it2 != a->getConnected()->end(); it2++) {
if (std::find(connected.begin(), connected.end(), (*it2)) == connected.end()) {
s.push((*it2));
}
}
}
if (connected.size() == this->agents.size()) {
// organism intact.
return orgs;
} else if (connected.size() > 1) {
// a connected part of the organism exist
Organism *o = new Organism();
std::vector<RobotAgentPtr>::iterator it;
for (it = connected.begin(); it != connected.end(); it++) {
o->addRobot((*it));
(*it)->setOrganism(o);
}
orgs.push_back(o);
} else if (connected.size() == 1) {
// this robot is not connected to any other
connected.front()->letGoOfOrganism();
loners.push_back(connected.front());
}
connected.clear();
}
std::vector<RobotAgentPtr>::iterator it2;
for (it2 = loners.begin(); it2 != loners.end(); it2++) {
(*it2)->setOrganism(NULL);
}
this->setInactive();
return orgs;
}
示例2: connectToRobot
void RobotAgent::connectToRobot(RobotAgentPtr other) {
if (!gUseOrganisms) return;
if (this->isPartOfOrganism() && other->isPartOfOrganism()
&& this->getOrganism()->getId() == other->getOrganism()->getId()) {
// the same organism, do nothing.
} else if (other->isPartOfOrganism() && this->isPartOfOrganism()
&& other->getOrganism()->getId() != this->getOrganism()->getId()) {
// two different organisms
Organism* o1 = this->getOrganism();
Organism* o2 = other->getOrganism();
this->addNeighbour(other);
other->addNeighbour(gWorld->getAgent(this->_wm->_agentId));
// merge smaller into larger
if (o1->size() >= o2->size()) {
gLogFile << "Merging organism " << o2->getId() << " into " << o1->getId() << std::endl;
//other->clickOnto(this, o2);
o2->mergeInto(o1);
o2->setInactive();
//Organism::remove(o2);
//delete o2;
} else {
gLogFile << "Merging organism " << o1->getId() << " into " << o2->getId() << std::endl;
//this->clickOnto(other, o1);
o1->mergeInto(o2);
o1->setInactive();
//Organism::remove(o1);
//delete o1;
}
} else if (other->isPartOfOrganism()) {
Organism* organism = other->getOrganism();
organism->addRobot(gWorld->getAgent(this->_wm->_agentId));
this->setOrganism(organism);
this->addNeighbour(other);
other->addNeighbour(gWorld->getAgent(this->_wm->_agentId));
//this->clickOnto(other);
gLogFile << "Adding agent " << this->_wm->_agentId << " to organism: " << organism->getId() << std::endl;
} else if (this->isPartOfOrganism()) {
Organism* organism = this->getOrganism();
organism->addRobot(other);
other->setOrganism(organism);
this->addNeighbour(other);
other->addNeighbour(gWorld->getAgent(this->_wm->_agentId));
//other->clickOnto(this);
gLogFile << "Adding agent " << other->_wm->_agentId << " to organism: " << organism->getId() << std::endl;
} else {
Organism* organism = new Organism();
gLogFile << "Creating new organism: " << organism->getId() << " robots: " << this->_wm->_agentId << ", " << other->_wm->_agentId << std::endl;
organism->addRobot(gWorld->getAgent(this->_wm->_agentId));
organism->addRobot(other);
this->setOrganism(organism);
other->setOrganism(organism);
this->addNeighbour(other);
other->addNeighbour(gWorld->getAgent(this->_wm->_agentId));
Organism::add(organism);
//this->clickTogether(other);
}
}