[Orxonox-commit 2203] r6919 - in code/branches/ai/src/orxonox: controllers worldentities
solex at orxonox.net
solex at orxonox.net
Mon May 17 16:45:50 CEST 2010
Author: solex
Date: 2010-05-17 16:45:50 +0200 (Mon, 17 May 2010)
New Revision: 6919
Modified:
code/branches/ai/src/orxonox/controllers/AIController.cc
code/branches/ai/src/orxonox/controllers/ArtificialController.cc
code/branches/ai/src/orxonox/controllers/ArtificialController.h
code/branches/ai/src/orxonox/worldentities/ControllableEntity.cc
Log:
finally some decent formation flying!
Modified: code/branches/ai/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/AIController.cc 2010-05-17 14:36:18 UTC (rev 6918)
+++ code/branches/ai/src/orxonox/controllers/AIController.cc 2010-05-17 14:45:50 UTC (rev 6919)
@@ -58,19 +58,20 @@
if (this->state_ == FREE)
{
-
+ if (this->formationFlight_)
+ {
// return to Master after being forced free
- if (this->freedomCount_ == 1)
- {
+ if (this->freedomCount_ == 1)
+ {
this->state_ = SLAVE;
this->freedomCount_ = 0;
+ }
+
+ random = rnd(maxrand);
+ if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
+ this->searchNewMaster();
}
- random = rnd(maxrand);
- if (random < 90 && (((!this->target_) || (random < 50 && this->target_)) && !this->forcedFree()))
- this->searchNewMaster();
-
-
// search enemy
random = rnd(maxrand);
if (random < 15 && (!this->target_))
@@ -115,7 +116,7 @@
if (this->state_ == SLAVE)
{
- // this->bShooting_ = true;
+
}
if (this->state_ == MASTER)//MASTER
@@ -124,54 +125,80 @@
this->commandSlaves();
+ if (this->specificMasterAction_ != NONE)
+ {
+ if (this->specificMasterAction_ == HOLD)
+ this->specificMasterActionHold();
- // lose master status (only if less than 4 slaves in formation)
-// random = rnd(maxrand);
-// if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
-// this->loseMasterState();
+ if (this->specificMasterAction_ == TURN180)
+ this->turn180();
- // look out for outher masters if formation is small
- random = rnd(maxrand);
- if(this->slaves_.size() < 3 && random < 20)
- this->searchNewMaster();
+ if (this->specificMasterAction_ == SPIN)
+ this->spin();
+ }
- // search enemy
- random = rnd(maxrand);
- if (random < 15 && (!this->target_))
- this->searchNewTarget();
+ else {
- // forget enemy
- random = rnd(maxrand);
- if (random < 5 && (this->target_))
- this->forgetTarget();
+ // make 180 degree turn - a specific Master Action
+ random = rnd(maxrand);
+ if (random < 5)
+ this->specificMasterAction_ = TURN180;
- // next enemy
- random = rnd(maxrand);
- if (random < 10 && (this->target_))
- this->searchNewTarget();
+ // spin around - a specific Master Action
+ random = rnd(maxrand);
+ if (random < 5)
+ this->specificMasterAction_ = SPIN;
- // fly somewhere
- random = rnd(maxrand);
- if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
- this->searchRandomTargetPosition();
+ // lose master status (only if less than 4 slaves in formation)
+ random = rnd(maxrand);
+ if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 )
+ this->loseMasterState();
+ // look out for outher masters if formation is small
+ random = rnd(maxrand);
+ if(this->slaves_.size() < 3 && random < 20)
+ this->searchNewMaster();
- // fly somewhere else
- random = rnd(maxrand);
- if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
- this->searchRandomTargetPosition();
+ // search enemy
+ random = rnd(maxrand);
+ if (random < 15 && (!this->target_))
+ this->searchNewTarget();
- // shoot
- random = rnd(maxrand);
- if (random < 5 && (this->target_ && !this->bShooting_))
- {
+ // forget enemy
+ random = rnd(maxrand);
+ if (random < 5 && (this->target_))
+ this->forgetTarget();
+
+ // next enemy
+ random = rnd(maxrand);
+ if (random < 10 && (this->target_))
+ this->searchNewTarget();
+
+ // fly somewhere
+ random = rnd(maxrand);
+ if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
+ this->searchRandomTargetPosition();
+
+
+ // fly somewhere else
+ random = rnd(maxrand);
+ if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
+ this->searchRandomTargetPosition();
+
+ // shoot
+ random = rnd(maxrand);
+ if (random < 5 && (this->target_ && !this->bShooting_))
+ {
this->bShooting_ = true;
-// this->forceFreeSlaves();
+ this->forceFreeSlaves();
+ }
+
+ // stop shooting
+ random = rnd(maxrand);
+ if (random < 25 && (this->bShooting_))
+ this->bShooting_ = false;
+
}
- // stop shooting
- random = rnd(maxrand);
- if (random < 25 && (this->bShooting_))
- this->bShooting_ = false;
}
}
@@ -188,9 +215,11 @@
if (this->bHasTargetPosition_)
this->moveToTargetPosition();
-
- if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
- this->getControllableEntity()->fire(0);
+ if (this->specificMasterAction_ == NONE)
+ {
+ if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
+ this->getControllableEntity()->fire(0);
+ }
}
if (this->state_ == SLAVE)
Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.cc 2010-05-17 14:36:18 UTC (rev 6918)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.cc 2010-05-17 14:45:50 UTC (rev 6919)
@@ -41,7 +41,9 @@
namespace orxonox
{
- static const unsigned int MAX_FORMATION_SIZE = 6;
+ static const unsigned int MAX_FORMATION_SIZE = 7;
+ static const int FORMATION_LENGTH = 10;
+ static const int FORMATION_WIDTH = 110;
static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
static const float SPEED_MASTER = 0.6f;
static const float ROTATEFACTOR_MASTER = 0.2f;
@@ -53,10 +55,13 @@
RegisterObject(ArtificialController);
this->target_ = 0;
+ this->formationFlight_ = true;
this->myMaster_ = 0;
this->freedomCount_ = 0;
this->team_ = -1;
this->state_ = FREE;
+ this->specificMasterAction_ = NONE;
+ this->specificMasterActionHoldCount_ = 0;
this->bShooting_ = false;
this->bHasTargetPosition_ = false;
this->targetPosition_ = Vector3::ZERO;
@@ -72,7 +77,8 @@
{
SUPER(ArtificialController, XMLPort, xmlelement, mode);
- XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
+ XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1);
+// XMLPortParam(ArtificialController, "formation", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);
}
// gets called when Bot dies
@@ -133,28 +139,23 @@
if(this->state_ == SLAVE)
{
-// if (this->target_ || distance > 10)
-// {
- float rotateFactor;
- if(this->state_ == SLAVE) rotateFactor = 1.0f;
+ this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
+ this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
- this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
- this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
-// }
- if (this->target_ && distance < 500 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
+ if (distance < 300)
{
- if (this->target_ && distance < 60)
+ if (distance < 40)
{
- this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
- } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
+ this->getControllableEntity()->moveFrontBack(0.8f*SPEED_MASTER);
+ } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER);
} else {
- this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
+ this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f);
}
}
}
@@ -175,7 +176,7 @@
std::vector<ArtificialController*>::iterator it = std::find(myMaster_->slaves_.begin(), myMaster_->slaves_.end(), this);
if( it != myMaster_->slaves_.end() )
myMaster_->slaves_.erase(it);
-//COUT(0) << "~unregister slave" << std::endl;
+// COUT(0) << "~unregister slave" << std::endl;
}
}
@@ -187,7 +188,7 @@
this->targetPosition_ = this->getControllableEntity()->getPosition();
this->forgetTarget();
-
+ int teamSize = 0;
//go through all pawns
for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
{
@@ -196,19 +197,26 @@
if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
continue;
- //has it an ArtificialController and is it a master?
+ //has it an ArtificialController?
if (!it->getController())
continue;
+ //is pawn oneself?
+ if (static_cast<ControllableEntity*>(*it) == this->getControllableEntity())
+ continue;
+
+ teamSize++;
+
ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController());
+ //is it a master?
if (!newMaster || newMaster->getState() != MASTER)
continue;
float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
- //is pawn oneself? && is pawn in range?
- if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000)
+ // is pawn in range?
+ if (distance < 5000)
{
if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
@@ -226,9 +234,9 @@
break;
}
}//for
+ //hasn't encountered any masters in range? -> become a master
+ if (state_ != SLAVE && teamSize != 0) state_ = MASTER;//master encounters master? ->done
- //hasn't encountered any masters in range? -> become a master
- if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
}
void ArtificialController::commandSlaves() {
@@ -242,20 +250,28 @@
dest += 4*orient*WorldEntity::BACK;
this->slaves_.front()->setTargetPosition(dest);
}
-
- // 2 slaves: triangle
- if (this->slaves_.size() == 2)
+ else
{
- dest += 10*orient*WorldEntity::BACK;
- this->slaves_[0]->setTargetPosition(dest + 10*orient*WorldEntity::LEFT);
- this->slaves_[1]->setTargetPosition(dest + 10*orient*WorldEntity::RIGHT);
- }
+ dest += 1.0f*orient*WorldEntity::BACK;
+ Vector3 pos = Vector3::ZERO;
+ int i = 1;
- if (this->slaves_.size() > MAX_FORMATION_SIZE)
- {
for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
{
- (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
+ pos = Vector3::ZERO;
+ if (i <= 1) pos += dest + FORMATION_WIDTH*WorldEntity::LEFT;
+ if (i == 2) pos += dest + FORMATION_WIDTH*WorldEntity::RIGHT;
+ if (i == 3) pos += dest + FORMATION_WIDTH*WorldEntity::UP;
+ if (i >= 4)
+ {
+ pos += dest + FORMATION_WIDTH*WorldEntity::DOWN;
+ i = 1;
+ dest += FORMATION_LENGTH*orient*WorldEntity::BACK;
+ (*it)->setTargetPosition(pos);
+ continue;
+ }
+ i++;
+ (*it)->setTargetPosition(pos);
}
}
}
@@ -326,6 +342,23 @@
} else return false;
}
+ void ArtificialController::specificMasterActionHold()
+ {
+ if (specificMasterActionHoldCount_ == 0) this->specificMasterAction_ = NONE;
+ else specificMasterActionHoldCount_--;
+ }
+
+ void ArtificialController::turn180()
+ {
+ this->specificMasterAction_ = NONE;
+ }
+
+ void ArtificialController::spin()
+ {
+ this->specificMasterAction_ = NONE;
+ }
+
+
void ArtificialController::setTargetPosition(const Vector3& target)
{
this->targetPosition_ = target;
Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.h 2010-05-17 14:36:18 UTC (rev 6918)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.h 2010-05-17 14:45:50 UTC (rev 6919)
@@ -52,16 +52,25 @@
{ this->team_ = team; }
inline int getTeam() const
{ return this->team_; }
+ inline void setFormationFlight(bool formation)
+ { this->formationFlight_ = formation; }
+ inline bool getFormationFlight() const
+ { return this->formationFlight_; }
virtual void changedControllableEntity();
protected:
int team_;
+ bool formationFlight_;
int freedomCount_;
enum State {SLAVE, MASTER, FREE};
State state_;
std::vector<ArtificialController*> slaves_;
+ ArtificialController *myMaster_;
+ enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180};
+ SpecificMasterAction specificMasterAction_;
+ int specificMasterActionHoldCount_;
void targetDied();
@@ -79,9 +88,10 @@
void loseMasterState();
void forceFreedom();
bool forcedFree();
+ void specificMasterActionHold();
+ void turn180();
+ void spin();
- ArtificialController *myMaster_;
-
void setTargetPosition(const Vector3& target);
void searchRandomTargetPosition();
Modified: code/branches/ai/src/orxonox/worldentities/ControllableEntity.cc
===================================================================
--- code/branches/ai/src/orxonox/worldentities/ControllableEntity.cc 2010-05-17 14:36:18 UTC (rev 6918)
+++ code/branches/ai/src/orxonox/worldentities/ControllableEntity.cc 2010-05-17 14:45:50 UTC (rev 6919)
@@ -576,7 +576,7 @@
else if (this->bHasLocalController_)
{
MobileEntity::setAngularVelocity(velocity);
- this->client_angular_velocity_ = this->getAngularVelocity();
+// // this->client_angular_velocity_ = this->getAngularVelocity();
}
}
More information about the Orxonox-commit
mailing list