Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 17, 2010, 4:45:50 PM (15 years ago)
Author:
solex
Message:

finally some decent formation flying!

File:
1 edited

Legend:

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

    r6918 r6919  
    4242{
    4343
    44     static const unsigned int MAX_FORMATION_SIZE = 6;
     44    static const unsigned int MAX_FORMATION_SIZE = 7;
     45    static const int FORMATION_LENGTH =  10;
     46    static const int FORMATION_WIDTH =  110;
    4547    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
    4648    static const float SPEED_MASTER = 0.6f;
     
    5456
    5557        this->target_ = 0;
     58        this->formationFlight_  =  true;
    5659        this->myMaster_ = 0;
    5760        this->freedomCount_ = 0;
    5861        this->team_ = -1;
    5962        this->state_ = FREE;
     63        this->specificMasterAction_ = NONE;
     64        this->specificMasterActionHoldCount_  = 0;
    6065        this->bShooting_ = false;
    6166        this->bHasTargetPosition_ = false;
     
    7378        SUPER(ArtificialController, XMLPort, xmlelement, mode);
    7479
    75         XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0);
     80        XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1);
     81//         XMLPortParam(ArtificialController, "formation", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);
    7682    }
    7783
     
    134140        if(this->state_ == SLAVE)
    135141        {
    136 //             if (this->target_ || distance > 10)
    137 //             {
    138                 float rotateFactor;
    139                 if(this->state_ == SLAVE) rotateFactor = 1.0f;
    140 
    141 
    142                 this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
    143                 this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
    144 
    145 
    146 
    147 //             }
    148 
    149             if (this->target_ && distance < 500 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
    150             {
    151                 if (this->target_ && distance < 60)
     142
     143           this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
     144           this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
     145
     146
     147
     148
     149
     150            if (distance < 300)
     151            {
     152                if (distance < 40)
    152153                {
    153                     this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
    154                 } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
     154                    this->getControllableEntity()->moveFrontBack(0.8f*SPEED_MASTER);
     155                } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER);
    155156
    156157            } else {
    157                 this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
     158                this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f);
    158159            }
    159160        }
     
    176177            if( it != myMaster_->slaves_.end() )
    177178                myMaster_->slaves_.erase(it);
    178 //COUT(0) << "~unregister slave" << std::endl;
     179// COUT(0) << "~unregister slave" << std::endl;
    179180        }
    180181    }
     
    188189        this->targetPosition_ = this->getControllableEntity()->getPosition();
    189190        this->forgetTarget();
    190 
     191        int teamSize = 0;
    191192        //go through all pawns
    192193        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     
    197198                continue;
    198199
    199             //has it an ArtificialController and is it a master?
     200            //has it an ArtificialController?
    200201            if (!it->getController())
    201202                continue;
    202203
     204            //is pawn oneself?
     205            if (static_cast<ControllableEntity*>(*it) == this->getControllableEntity())
     206                continue;
     207
     208            teamSize++;
     209
    203210            ArtificialController *newMaster = static_cast<ArtificialController*>(it->getController());
    204211
     212            //is it a master?
    205213            if (!newMaster || newMaster->getState() != MASTER)
    206214                continue;
     
    208216            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
    209217
    210             //is pawn oneself? && is pawn in range?
    211             if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000)
     218            // is pawn in range?
     219            if (distance < 5000)
    212220            {
    213221                if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
     
    227235            }
    228236        }//for
    229 
    230237        //hasn't encountered any masters in range? -> become a master
    231         if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
     238        if (state_ != SLAVE  && teamSize != 0) state_ = MASTER;//master encounters master? ->done
     239
    232240    }
    233241
     
    243251            this->slaves_.front()->setTargetPosition(dest);
    244252        }
    245 
    246         // 2 slaves: triangle
    247          if (this->slaves_.size() == 2)
    248         {
    249             dest += 10*orient*WorldEntity::BACK;
    250             this->slaves_[0]->setTargetPosition(dest + 10*orient*WorldEntity::LEFT);
    251             this->slaves_[1]->setTargetPosition(dest + 10*orient*WorldEntity::RIGHT);
    252         }
    253 
    254         if (this->slaves_.size() > MAX_FORMATION_SIZE)
    255         {
     253        else
     254        {
     255            dest += 1.0f*orient*WorldEntity::BACK;
     256            Vector3 pos = Vector3::ZERO;
     257            int i = 1;
     258
    256259            for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    257260            {
    258                 (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
     261                pos = Vector3::ZERO;
     262                if (i <= 1) pos += dest  + FORMATION_WIDTH*WorldEntity::LEFT;
     263                if (i == 2) pos += dest  + FORMATION_WIDTH*WorldEntity::RIGHT;
     264                if (i == 3) pos += dest  + FORMATION_WIDTH*WorldEntity::UP;
     265                if (i >= 4)
     266                {
     267                    pos += dest  + FORMATION_WIDTH*WorldEntity::DOWN;
     268                    i = 1;
     269                    dest += FORMATION_LENGTH*orient*WorldEntity::BACK;
     270                    (*it)->setTargetPosition(pos);
     271                    continue;
     272                }
     273                i++;
     274                (*it)->setTargetPosition(pos);
    259275            }
    260276        }
     
    326342        } else return false;
    327343    }
     344
     345    void ArtificialController::specificMasterActionHold()
     346    {
     347        if (specificMasterActionHoldCount_ == 0) this->specificMasterAction_ = NONE;
     348        else specificMasterActionHoldCount_--;
     349    }
     350
     351    void ArtificialController::turn180()
     352    {
     353        this->specificMasterAction_  =  NONE;
     354    }
     355
     356    void ArtificialController::spin()
     357    {
     358        this->specificMasterAction_  =  NONE;
     359    }
     360
    328361
    329362    void ArtificialController::setTargetPosition(const Vector3& target)
Note: See TracChangeset for help on using the changeset viewer.