Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 12, 2011, 7:50:43 PM (13 years ago)
Author:
jo
Message:

Ai and tutorial improvements merged back to the trunk. AI features: all weapons are used, the ai-firestrength is configurable, bots are able to collect pickups . I've set the tutorial level as default level.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/controllers/DroneController.cc

    r8729 r8891  
    5252        this->drone_ = 0;
    5353        this->isShooting_ = false;
     54        this->setAccuracy(10);
    5455
    5556        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this)));
     
    9293        The duration of the tick.
    9394    */
     95    /* PORTALS workaround:
     96    if the owner uses a portal -> distance between owner and drone is huge -> is detected by drone
     97    -> drone searches for portal -> drone adds portal as waypoint -> drone flies towards portal //ignores owner
     98    -> if the drone used a portal, then the distance to the owner is small -> remove waypoint // back to normal mode
     99
     100    */
    94101    void DroneController::tick(float dt)
    95102    {
    96103        if (this->getDrone() && this->getOwner())
    97104        {
    98             if (this->target_)
     105            if (this->waypoints_.size() > 0 ) //Waypoint functionality: Drone should follow it's master through portals
     106            {// Idea: after using the the portal, the master is far away.
     107                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
     108                if(wPoint)
     109                {
     110                    float distanceToOwnerSquared = (this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength();
     111                    this->absoluteMoveToPosition(wPoint->getWorldPosition()); //simplified function - needs WORKAROUND
     112                    if (distanceToOwnerSquared <= 90000.0f) //WORKAROUND: if the Drone is again near its owner, the portal has been used correctly.
     113                    {
     114                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
     115                        this->positionReached(); //needed??
     116                    }
     117
     118                }
     119                else
     120                    this->waypoints_.pop_back(); // remove invalid waypoints
     121            }
     122            else
    99123            {
    100                 float distanceToTargetSquared = (this->getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength();
    101                 if (distanceToTargetSquared < (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange()))
     124                if (this->target_)
    102125                {
    103                     this->isShooting_ = true;
    104                     this->aimAtTarget();
    105                     this->getDrone()->fire(0);
     126                    float distanceToTargetSquared = (this->getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength();
     127                    if (distanceToTargetSquared < (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange()))
     128                    {
     129                       this->isShooting_ = true;
     130                       this->aimAtTarget();
     131                       if(!this->friendlyFire())
     132                           this->getDrone()->fire(0);
     133                    }
     134               }
     135
     136
     137                float maxDistanceSquared = this->getDrone()->getMaxDistanceToOwner()*this->getDrone()->getMaxDistanceToOwner();
     138                float minDistanceSquared = this->getDrone()->getMinDistanceToOwner()*this->getDrone()->getMinDistanceToOwner();
     139                if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength()  > 20.0f*maxDistanceSquared)
     140                {//FIX: if the drone's owner uses portal, the drone searches for the portal & adds it as a waypoint.
     141                    this->updatePointsOfInterest("PortalEndPoint", 500.0f); //possible conflict: speed-pickup
    106142                }
     143                if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength()  > maxDistanceSquared)
     144                {
     145                    this->moveToPosition(this->getOwner()->getWorldPosition()); //fly towards owner
     146                }
     147                else if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() < minDistanceSquared)
     148                {
     149                    this->moveToPosition(-this->getOwner()->getWorldPosition()); //fly away from owner
     150                }
     151                else if (!this->isShooting_)
     152                {
     153                    float random = rnd(2.0f);
     154                    float randomSelection = rnd(6.0f);
     155                    if((int)randomSelection==0) drone_->moveUpDown(random);
     156                    else if((int)randomSelection==1) drone_->moveRightLeft(random);
     157                    else if((int)randomSelection==2) drone_->moveFrontBack(random);
     158                    else if((int)randomSelection==3) drone_->rotateYaw(random);
     159                    else if((int)randomSelection==4) drone_->rotatePitch(random);
     160                    else if((int)randomSelection==5) drone_->rotateRoll(random);
     161                }
     162
     163                this->isShooting_ = false;
    107164            }
    108 
    109             float maxDistanceSquared = this->getDrone()->getMaxDistanceToOwner()*this->getDrone()->getMaxDistanceToOwner();
    110             float minDistanceSquared = this->getDrone()->getMinDistanceToOwner()*this->getDrone()->getMinDistanceToOwner();
    111             if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength()  > maxDistanceSquared)
    112             {
    113                 this->moveToPosition(this->getOwner()->getWorldPosition()); //fly towards owner
    114             }
    115             else if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() < minDistanceSquared)
    116             {
    117                 this->moveToPosition(-this->getOwner()->getWorldPosition()); //fly away from owner
    118             }
    119             else if (!this->isShooting_)
    120             {
    121                 float random = rnd(2.0f);
    122                 float randomSelection = rnd(6.0f);
    123                 if((int)randomSelection==0) drone_->moveUpDown(random);
    124                 else if((int)randomSelection==1) drone_->moveRightLeft(random);
    125                 else if((int)randomSelection==2) drone_->moveFrontBack(random);
    126                 else if((int)randomSelection==3) drone_->rotateYaw(random);
    127                 else if((int)randomSelection==4) drone_->rotatePitch(random);
    128                 else if((int)randomSelection==5) drone_->rotateRoll(random);
    129             }
    130 
    131             this->isShooting_ = false;
    132165        }
    133 
    134166        SUPER(AIController, tick, dt);
    135 
    136167    }
    137168
     
    147178            this->destroy();
    148179    }
     180
     181    bool DroneController::friendlyFire()
     182    {   ControllableEntity* droneEntity_ = this->getControllableEntity();
     183        if (!droneEntity_) return false;
     184        if(!owner_) return false;
     185        if(this->bHasTargetPosition_)
     186        {
     187            Vector3 ownerPosition_ = owner_->getPosition();
     188            Vector3 toOwner_ = owner_->getPosition() - droneEntity_->getPosition();
     189            Vector3 toTarget_ = targetPosition_ - droneEntity_->getPosition();
     190            if(toTarget_.length() < toOwner_.length()) return false; //owner is far away = in safty
     191            float angleToOwner = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, ownerPosition_);
     192            float angleToTarget = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, targetPosition_);
     193            float angle = angleToOwner - angleToTarget;//angle between target and owner, observed by the drone
     194            if(std::sin(angle)*toOwner_.length() < 5.0f)//calculate owner's distance to shooting line
     195            return true;
     196        }
     197        return false;//Default return value: Usually there is no friendlyFire
     198    }
    149199}
Note: See TracChangeset for help on using the changeset viewer.