[Orxonox-commit 4052] r8723 - in code/branches/ai2/src: modules/weapons/projectiles orxonox/controllers orxonox/worldentities
jo at orxonox.net
jo at orxonox.net
Fri Jul 1 00:54:48 CEST 2011
Author: jo
Date: 2011-07-01 00:54:47 +0200 (Fri, 01 Jul 2011)
New Revision: 8723
Modified:
code/branches/ai2/src/modules/weapons/projectiles/Rocket.cc
code/branches/ai2/src/orxonox/controllers/ArtificialController.cc
code/branches/ai2/src/orxonox/controllers/ArtificialController.h
code/branches/ai2/src/orxonox/controllers/DroneController.cc
code/branches/ai2/src/orxonox/controllers/DroneController.h
code/branches/ai2/src/orxonox/worldentities/ControllableEntity.h
Log:
'Stable' update. Merging by hand.
Modified: code/branches/ai2/src/modules/weapons/projectiles/Rocket.cc
===================================================================
--- code/branches/ai2/src/modules/weapons/projectiles/Rocket.cc 2011-06-30 20:49:24 UTC (rev 8722)
+++ code/branches/ai2/src/modules/weapons/projectiles/Rocket.cc 2011-06-30 22:54:47 UTC (rev 8723)
@@ -57,6 +57,7 @@
this->localAngularVelocity_ = 0;
this->lifetime_ = 100;
+ this->bIsRocket=true;
if (GameMode::isMaster())
{
@@ -115,6 +116,7 @@
{
if(this->isInitialized())
{
+ this->bIsRocket=false;
if (GameMode::isMaster())
{
this->destructionEffect();
Modified: code/branches/ai2/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai2/src/orxonox/controllers/ArtificialController.cc 2011-06-30 20:49:24 UTC (rev 8722)
+++ code/branches/ai2/src/orxonox/controllers/ArtificialController.cc 2011-06-30 22:54:47 UTC (rev 8723)
@@ -43,6 +43,9 @@
#include "controllers/WaypointPatrolController.h"
#include "controllers/NewHumanController.h"
#include "controllers/DroneController.h"
+#include "weaponsystem/WeaponMode.h"
+#include "weaponsystem/WeaponPack.h"
+#include "weaponsystem/Weapon.h"
namespace orxonox
{
@@ -51,6 +54,7 @@
SetConsoleCommand("ArtificialController", "followme", &ArtificialController::followme);
SetConsoleCommand("ArtificialController", "passivebehaviour", &ArtificialController::passivebehaviour);
SetConsoleCommand("ArtificialController", "formationsize", &ArtificialController::formationsize);
+ SetConsoleCommand("ArtificialController", "setbotlevel", &ArtificialController::setAllBotLevel);
static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7;
static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;
@@ -83,12 +87,17 @@
this->targetPosition_ = Vector3::ZERO;
this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
+ this->bSetupWorked = false;
+ this->numberOfWeapons = 0;
+ this->botlevel_ = 1.0f;
+ this->mode_ = DEFAULT;////Vector-implementation: mode_.push_back(DEFAULT);
+ this->timeout_=0;
}
ArtificialController::~ArtificialController()
{
if (this->isInitialized())
- {
+ {//Vector-implementation: mode_.erase(mode_.begin(),mode_.end());
this->removeFromFormation();
for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it; ++it)
@@ -1019,4 +1028,116 @@
return (team1 == team2 && team1 != -1);
}
+
+ /**
+ @brief DoFire is called when a bot should shoot and decides which weapon is used and whether the bot shoots at all.
+ */
+ void ArtificialController::doFire()
+ {
+ if(!bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
+ {
+ this->setupWeapons();
+ if(numberOfWeapons>0)
+ bSetupWorked=true;
+ }
+ else if(this->getControllableEntity()&&(numberOfWeapons>0)&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
+ {
+ if (this->isCloseAtTarget(130) &&(weapons[1]==1) )
+ {//LENSFLARE: short range weapon
+ this->getControllableEntity()->fire(1); //ai uses lens flare if they're close enough to the target
+ }
+ else if((weapons[3]==3)&& this->isCloseAtTarget(400) /*&&projectiles[3]*/ )
+ {//ROCKET: mid range weapon
+ //TODO: Which weapon is the rocket? How many rockets are available?
+ this->mode_ = ROCKET;//Vector-implementation: mode_.push_back(ROCKET);
+ this->getControllableEntity()->fire(3);//launch rocket
+ if(this->getControllableEntity()&&this->target_)//after fire(3) getControllableEntity() refers to the rocket!
+ {
+ float speed = this->getControllableEntity()->getVelocity().length() - target_->getVelocity().length();
+ if(!speed) speed = 0.1f;
+ float distance = target_->getPosition().length() - this->getControllableEntity()->getPosition().length();
+ this->timeout_= distance/speed*sgn(speed*distance) + 1.8f;//predicted time of target hit (+ tolerance)
+ }
+ else
+ this->timeout_ = 4.0f;//TODO: find better default value
+
+ this->projectiles[3]-=1;//decrease ammo !!
+ }
+ else if ((weapons[0]==0))//LASER: default weapon
+ this->getControllableEntity()->fire(0);
+ }
+ }
+
+ /**
+ @brief Information gathering: Which weapons are ready to use?
+ */
+ void ArtificialController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
+ {
+ if(this->getControllableEntity())
+ {
+ Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
+ if(pawn)
+ {
+ for(unsigned int i=0; i<WeaponSystem::MAX_WEAPON_MODES; i++)
+ {
+ const std::string wpn = getWeaponname(i, pawn); COUT(0)<<wpn<< std::endl;//Temporary debug info.
+ /*if(wpn=="")
+ weapons[i]=-1;
+ else if(wpn=="LaserMunition")//other munitiontypes are not defined yet :-(
+ weapons[0]=0;
+ else if(wpn=="FusionMunition")
+ weapons[1]=1;
+ else if(wpn=="TargetSeeking Rockets")
+ weapons[2]=2;
+ else if(wpn=="ROCKET")//TODO: insert right munition name
+ weapons[3]=3;
+ */
+ if(pawn->getWeaponSet(i)) //main part: find which weapons a pawn can use; hard coded at the moment!
+ {
+ weapons[i]=i;
+ projectiles[i]=1;//TODO: how to express infinite ammo? how to get data?? getWeaponmode(i)->getMunition()->getNumMunition(WeaponMode* user)
+ numberOfWeapons++;
+ }
+ else
+ weapons[i]=-1;
+ }
+ //pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
+ }
+ }
+ }
+
+ const std::string& ArtificialController::getWeaponname(int i, Pawn* pawn)
+ {//is there a way to minimize this long if-return structure, without triggering nullpointer exceptions?
+ if(!pawn) return "";
+ WeaponPack* wPack = pawn->getWeaponPack(i);
+ if(!wPack) return "";
+ Weapon* wpn = wPack->getWeapon(i);
+ if(!wpn) return "";
+ WeaponMode* wMode = wpn->getWeaponmode(i);
+ if(!wMode) return "";
+ return wMode->getMunitionName();
+ }//pawn->getWeaponpack(i)->getWeapon(i)->getWeaponmode(i)->getMunitionName()
+
+
+ void ArtificialController::setBotLevel(float level)
+ {
+ if (level < 0.0f)
+ this->botlevel_ = 0.0f;
+ else if (level > 1.0f)
+ this->botlevel_ = 1.0f;
+ else
+ this->botlevel_ = level;
+ }
+
+ void ArtificialController::setAllBotLevel(float level)
+ {
+ for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it != ObjectList<ArtificialController>::end(); ++it)
+ it->setBotLevel(level);
+ }
+
+ void ArtificialController::setPreviousMode()
+ {
+ this->mode_ = DEFAULT; //Vector-implementation: mode_.pop_back();
+ }
+
}
Modified: code/branches/ai2/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai2/src/orxonox/controllers/ArtificialController.h 2011-06-30 20:49:24 UTC (rev 8722)
+++ code/branches/ai2/src/orxonox/controllers/ArtificialController.h 2011-06-30 22:54:47 UTC (rev 8723)
@@ -36,6 +36,7 @@
#include "util/Math.h"
#include "Controller.h"
#include "controllers/NewHumanController.h"
+#include "weaponsystem/WeaponSystem.h"
namespace orxonox
{
@@ -77,6 +78,12 @@
static void passivebehaviour(const bool passive);
static void formationsize(const int size);
+ virtual void doFire();
+ void setBotLevel(float level=1.0f);
+ inline float getBotLevel() const
+ { return this->botlevel_; }
+ static void setAllBotLevel(float level);
+
protected:
int team_;
@@ -140,7 +147,20 @@
WeakPtr<Pawn> target_;
bool bShooting_;
+ int numberOfWeapons; //< Used for weapon init function. Displayes number of weapons available for a bot.
+ int weapons[WeaponSystem::MAX_WEAPON_MODES];
+ int projectiles[WeaponSystem::MAX_WEAPON_MODES];
+ float botlevel_; //< Makes the level of a bot configurable.
+ float timeout_; //< Timeout for rocket usage. (If a rocket misses, a bot should stop using it.)
+
+ enum Mode {DEFAULT, ROCKET, DEFENCE, MOVING};//TODO; implement DEFENCE, MOVING modes
+ Mode mode_; //TODO: replace single value with stack-like implementation: std::vector<Mode> mode_;
+ void setPreviousMode();
+
private:
+ void setupWeapons();
+ const std::string& getWeaponname(int i, Pawn* pawn);
+ bool bSetupWorked;
};
}
Modified: code/branches/ai2/src/orxonox/controllers/DroneController.cc
===================================================================
--- code/branches/ai2/src/orxonox/controllers/DroneController.cc 2011-06-30 20:49:24 UTC (rev 8722)
+++ code/branches/ai2/src/orxonox/controllers/DroneController.cc 2011-06-30 22:54:47 UTC (rev 8723)
@@ -102,7 +102,8 @@
{
this->isShooting_ = true;
this->aimAtTarget();
- this->getDrone()->fire(0);
+ if(!this->friendlyFire())
+ this->getDrone()->fire(0);
}
}
@@ -146,4 +147,23 @@
else
this->destroy();
}
+
+ bool DroneController::friendlyFire()
+ { ControllableEntity* droneEntity_ = this->getControllableEntity();
+ if (!droneEntity_) return false;
+ if(!owner_) return false;
+ if(this->bHasTargetPosition_)
+ {
+ Vector3 ownerPosition_ = owner_->getPosition();
+ Vector3 toOwner_ = owner_->getPosition() - droneEntity_->getPosition();
+ Vector3 toTarget_ = targetPosition_ - droneEntity_->getPosition();
+ if(toTarget_.length() < toOwner_.length()) return false; //owner is far away = in safty
+ float angleToOwner = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, ownerPosition_);
+ float angleToTarget = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, targetPosition_);
+ float angle = angleToOwner - angleToTarget;//angle between target and owner, observed by the drone
+ if(std::sin(angle)*toOwner_.length() < 5.0f)//calculate owner's distance to shooting line
+ return true;
+ }
+ return false;//Default return value: Usually there is no friendlyFire
+ }
}
Modified: code/branches/ai2/src/orxonox/controllers/DroneController.h
===================================================================
--- code/branches/ai2/src/orxonox/controllers/DroneController.h 2011-06-30 20:49:24 UTC (rev 8722)
+++ code/branches/ai2/src/orxonox/controllers/DroneController.h 2011-06-30 22:54:47 UTC (rev 8723)
@@ -65,6 +65,7 @@
protected:
virtual void action();
void ownerDied();
+ bool friendlyFire(); //< returns true if the owner_ would be hit.
bool isShooting_;
private:
Modified: code/branches/ai2/src/orxonox/worldentities/ControllableEntity.h
===================================================================
--- code/branches/ai2/src/orxonox/worldentities/ControllableEntity.h 2011-06-30 20:49:24 UTC (rev 8722)
+++ code/branches/ai2/src/orxonox/worldentities/ControllableEntity.h 2011-06-30 22:54:47 UTC (rev 8723)
@@ -162,6 +162,8 @@
virtual WorldEntity* getTarget()
{ return this->target_.get(); }
void setTargetInternal( uint32_t targetID );
+ inline bool getRocket() const
+ { return this-> bIsRocket; }
protected:
virtual void preDestroy();
@@ -180,6 +182,7 @@
void destroyHud(void);
Ogre::SceneNode* cameraPositionRootNode_;
+ bool bIsRocket; //Workaround to see, if the controllable entity is a Rocket.
private:
void registerVariables();
More information about the Orxonox-commit
mailing list