Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 1, 2011, 12:54:47 AM (14 years ago)
Author:
jo
Message:

'Stable' update. Merging by hand.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ai2/src/orxonox/controllers/ArtificialController.cc

    r8706 r8723  
    4444#include "controllers/NewHumanController.h"
    4545#include "controllers/DroneController.h"
     46#include "weaponsystem/WeaponMode.h"
     47#include "weaponsystem/WeaponPack.h"
     48#include "weaponsystem/Weapon.h"
    4649
    4750namespace orxonox
     
    5255    SetConsoleCommand("ArtificialController", "passivebehaviour", &ArtificialController::passivebehaviour);
    5356    SetConsoleCommand("ArtificialController", "formationsize",    &ArtificialController::formationsize);
     57    SetConsoleCommand("ArtificialController", "setbotlevel",      &ArtificialController::setAllBotLevel);
    5458
    5559    static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7;
     
    8488
    8589        this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
     90        this->bSetupWorked = false;
     91        this->numberOfWeapons = 0;
     92        this->botlevel_ = 1.0f;
     93        this->mode_ = DEFAULT;////Vector-implementation: mode_.push_back(DEFAULT);
     94        this->timeout_=0;
    8695    }
    8796
     
    8998    {
    9099        if (this->isInitialized())
    91         {
     100        {//Vector-implementation: mode_.erase(mode_.begin(),mode_.end());
    92101            this->removeFromFormation();
    93102
     
    10201029        return (team1 == team2 && team1 != -1);
    10211030    }
     1031
     1032    /**
     1033        @brief DoFire is called when a bot should shoot and decides which weapon is used and whether the bot shoots at all.
     1034    */
     1035    void ArtificialController::doFire()
     1036    {
     1037        if(!bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
     1038        {
     1039            this->setupWeapons();
     1040            if(numberOfWeapons>0)
     1041                bSetupWorked=true;
     1042        }
     1043        else if(this->getControllableEntity()&&(numberOfWeapons>0)&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
     1044        {
     1045            if (this->isCloseAtTarget(130) &&(weapons[1]==1) )
     1046            {//LENSFLARE: short range weapon     
     1047                this->getControllableEntity()->fire(1); //ai uses lens flare if they're close enough to the target
     1048            }
     1049            else if((weapons[3]==3)&& this->isCloseAtTarget(400) /*&&projectiles[3]*/ )
     1050            {//ROCKET: mid range weapon
     1051                //TODO: Which weapon is the rocket? How many rockets are available?
     1052                this->mode_ = ROCKET;//Vector-implementation: mode_.push_back(ROCKET);
     1053                this->getControllableEntity()->fire(3);//launch rocket
     1054                if(this->getControllableEntity()&&this->target_)//after fire(3) getControllableEntity() refers to the rocket!
     1055                {
     1056                    float speed = this->getControllableEntity()->getVelocity().length() - target_->getVelocity().length();
     1057                    if(!speed) speed = 0.1f;
     1058                    float distance = target_->getPosition().length() - this->getControllableEntity()->getPosition().length();
     1059                    this->timeout_= distance/speed*sgn(speed*distance) + 1.8f;//predicted time of target hit (+ tolerance)
     1060                }
     1061                else
     1062                    this->timeout_ = 4.0f;//TODO: find better default value
     1063               
     1064                this->projectiles[3]-=1;//decrease ammo !!
     1065            }
     1066            else if ((weapons[0]==0))//LASER: default weapon
     1067                this->getControllableEntity()->fire(0);
     1068        }
     1069    }
     1070
     1071    /**
     1072        @brief Information gathering: Which weapons are ready to use?
     1073    */
     1074    void ArtificialController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
     1075    {
     1076        if(this->getControllableEntity())
     1077        {
     1078            Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
     1079            if(pawn)
     1080            {
     1081                for(unsigned int i=0; i<WeaponSystem::MAX_WEAPON_MODES; i++)
     1082                {
     1083                    const std::string wpn = getWeaponname(i, pawn); COUT(0)<<wpn<< std::endl;//Temporary debug info.
     1084                    /*if(wpn=="")
     1085                        weapons[i]=-1;
     1086                    else if(wpn=="LaserMunition")//other munitiontypes are not defined yet :-(
     1087                        weapons[0]=0;
     1088                    else if(wpn=="FusionMunition")
     1089                        weapons[1]=1;
     1090                    else if(wpn=="TargetSeeking Rockets")
     1091                        weapons[2]=2;
     1092                    else if(wpn=="ROCKET")//TODO: insert right munition name
     1093                        weapons[3]=3;
     1094                    */
     1095                    if(pawn->getWeaponSet(i)) //main part: find which weapons a pawn can use; hard coded at the moment!
     1096                    {
     1097                        weapons[i]=i;
     1098                        projectiles[i]=1;//TODO: how to express infinite ammo? how to get data?? getWeaponmode(i)->getMunition()->getNumMunition(WeaponMode* user)
     1099                          numberOfWeapons++;
     1100                    }
     1101                    else
     1102                        weapons[i]=-1;
     1103                }
     1104                 //pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
     1105            }
     1106        }
     1107    }
     1108
     1109    const std::string& ArtificialController::getWeaponname(int i, Pawn* pawn)
     1110    {//is there a way to minimize this long if-return structure, without triggering nullpointer exceptions?
     1111        if(!pawn) return "";
     1112        WeaponPack* wPack = pawn->getWeaponPack(i);
     1113        if(!wPack) return "";
     1114        Weapon* wpn = wPack->getWeapon(i);
     1115        if(!wpn) return "";
     1116        WeaponMode* wMode = wpn->getWeaponmode(i);
     1117        if(!wMode) return "";
     1118        return wMode->getMunitionName();
     1119    }//pawn->getWeaponpack(i)->getWeapon(i)->getWeaponmode(i)->getMunitionName()
     1120
     1121   
     1122    void ArtificialController::setBotLevel(float level)
     1123    {
     1124        if (level < 0.0f)
     1125            this->botlevel_ = 0.0f;
     1126        else if (level > 1.0f)
     1127            this->botlevel_ = 1.0f;
     1128        else
     1129            this->botlevel_ = level;
     1130    }
     1131   
     1132    void ArtificialController::setAllBotLevel(float level)
     1133    {
     1134        for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it != ObjectList<ArtificialController>::end(); ++it)
     1135            it->setBotLevel(level);
     1136    }
     1137
     1138    void ArtificialController::setPreviousMode()
     1139    {
     1140        this->mode_ = DEFAULT; //Vector-implementation: mode_.pop_back();
     1141    }
     1142   
    10221143}
Note: See TracChangeset for help on using the changeset viewer.