Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 7, 2015, 8:34:20 AM (8 years ago)
Author:
gania
Message:

check in

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc

    r10915 r10923  
    4040    ActionpointController::ActionpointController(Context* context) : FightingController(context)
    4141    {
     42        this->ticks_ = 0;
    4243        this->bPatrolling_ = false;
    43 
    44         this->actionCounter_ = 0;
     44        this->maneuverCounter_ = 0;
    4545        this->bInLoop_ = false;
    4646        this->bLoop_ = false;
     
    4949        parsedActionpoints_.clear();
    5050        actionpoints_.clear();
    51         healthSpawners_.clear();
    52         damageSpawners_.clear();
    53         speedSpawners_.clear();
     51
    5452        this->bTakenOver_ = false;
    5553        this->action_ = Action::NONE;
     
    9694        parsedActionpoints_.clear();
    9795        actionpoints_.clear();
    98         healthSpawners_.clear();
    99         damageSpawners_.clear();
    100         speedSpawners_.clear();
     96        ActionpointController::nextActionpointControllerId_--;
     97        if (ActionpointController::nextActionpointControllerId_ < 0)
     98            ActionpointController::nextActionpointControllerId_ = 0;
    10199    }
    102100    void ActionpointController::tick(float dt)
    103101    {
    104         if (!this->getControllableEntity())
    105             return;
     102        if (!this || !this->getControllableEntity())
     103            return;
     104        ++this->ticks_;
     105        // orxout (internal_error) << "this id = " << this->actionpointControllerId_ << endl;
     106        if (ActionpointController::sTicks_ < this->ticks_)
     107        {
     108            // orxout (internal_error) << "total id's = " << ActionpointController::nextActionpointControllerId_ << endl;
     109            ++ActionpointController::sTicks_;
     110        }
     111
     112        if (this->ticks_ == 1)
     113        {
     114
     115            this->actionpointControllerId_ = ActionpointController::nextActionpointControllerId_++;
     116            std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end());
     117            std::reverse(actionpoints_.begin(), actionpoints_.end());
     118            if (this->parsedActionpoints_.empty())
     119            {
     120                this->action_ = Action::FIGHTALL;
     121            }
     122        }
     123        if (this->ticks_ == 1)
     124        {   
     125            this->timeOffset_ = 0.0f;
     126            this->bActionCalled_ = false;
     127           
     128            this->bFirstTick_ = false;
     129        }
     130/*        if (ActionpointController::sTicks_ % (ActionpointController::nextActionpointControllerId_+1) != this->actionpointControllerId_)
     131        {
     132            return;
     133        }*/
     134        if (!this || !this->getControllableEntity())
     135            return;
     136       
     137        if (this->bHasTargetPosition_)
     138        {
     139            this->moveToTargetPosition(dt);
     140        }
     141        else if (this->bLookAtTarget_)
     142        {
     143            this->lookAtTarget(dt);
     144        }
     145        if (this->bShooting_)
     146        {
     147            this->doFire();
     148        }
     149
     150        if (!this || !this->getControllableEntity())
     151            return;
     152       
    106153        if (stop_)
    107154            return;
    108         if (this->timeout_ > 0 && this->bFiredRocket_)
    109         {
    110             this->timeout_ -= dt;
    111         }
    112         if (this->bStartedDodging_)
    113         {
    114             this->timeDodged_ += dt;
    115             if (this->timeDodged_ > 4.0f)
    116             {
    117                 this->bStartedDodging_ = false;
    118                 this->timeDodged_ = 0;
    119             }
    120         }
    121         maneuverCounter_ += dt;
    122         if (maneuverCounter_ > 6.0f)
    123             maneuverCounter_ = 0;
     155       
    124156        this->timeOffset_ += dt;
    125157        if (this->timeOffset_ >= 2.4f)
     
    127159        if (timeout_ <= 0)
    128160            this->bFiredRocket_ = false;
    129 
    130         if (this->bHasTargetPosition_)
    131         {
    132             this->moveToTargetPosition(dt);
    133         }
    134         else if (this->bLookAtTarget_)
    135         {
    136             this->lookAtTarget(dt);
    137         }
    138 
    139         if (!this->getControllableEntity())
    140             return;
    141         if (this->bShooting_)
    142         {
    143             this->doFire();
    144         }
    145         if (this->bFirstTick_)
    146         {
    147             std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end());
    148             std::reverse(actionpoints_.begin(), actionpoints_.end());
    149             if (this->parsedActionpoints_.empty())
    150             {
    151                 this->action_ = Action::FIGHTALL;
    152             }
    153         }
    154         if (this->bFirstTick_)
    155         {   
    156             this->timeOffset_ = 0.0f;
    157             this->bActionCalled_ = false;
    158             setSpawners();
    159             // orxout(internal_error) << "health spawners size = " << this->healthSpawners_.size() <<
    160             // ", damage spawners size = " << this->damageSpawners_.size() <<
    161             // ", speed spawners size = " << this->speedSpawners_.size() << endl;       
    162             this->bFirstTick_ = false;
    163         }
    164         if (!this->getControllableEntity())
    165             return;
     161        if (!this || !this->getControllableEntity())
     162            return;
     163
     164        if (!this || !this->getControllableEntity())
     165            return;
     166        if (this->timeout_ > 0 && this->bFiredRocket_)
     167            {
     168                this->timeout_ -= dt;
     169            }
     170            if (this->bStartedDodging_)
     171            {
     172                this->timeDodged_ += dt;
     173            }
     174            maneuverCounter_ += dt;
     175
    166176        //maneuver every 0.25 sec ->
    167         if (this->hasTarget() &&
    168             ((this->timeOffset_ >= 0.4f && this->timeOffset_ < 0.8f) ||
    169              (this->timeOffset_ >= 1.2f && this->timeOffset_ < 1.6f) ||
    170              (this->timeOffset_ >= 1.8f && this->timeOffset_ < 2.0f) ||
    171              (this->timeOffset_ >= 2.2f && this->timeOffset_ < 2.4f)) && !this->bManeuverCalled_)
    172         {
    173             this->bManeuverCalled_ = true;
     177        int step =  5;
     178        if (this->hasTarget() && ActionpointController::sTicks_ % 100 == step * (this->actionpointControllerId_) + 3)
     179        {
     180            if (maneuverCounter_ > 6.0f)
     181                maneuverCounter_ = 0;
     182            if (this->bStartedDodging_ &&  this->timeDodged_ > 4.0f)
     183            {
     184                this->bStartedDodging_ = false;
     185                this->timeDodged_ = 0;
     186            }
    174187            this->maneuver();
     188        }
     189        if (ActionpointController::sTicks_ % 100 == step * this->actionpointControllerId_ + 1)
     190        {
     191            this->action();
     192        }
     193        if (!this || !this->getControllableEntity())
     194            return;
     195
     196        if (!this || !this->getControllableEntity())
     197            return;
     198        if (this->hasTarget() &&  ActionpointController::sTicks_ % 100 == step * (this->actionpointControllerId_))
     199        {
    175200            this->bShooting_ = this->canFire();
    176         }
    177         if ((this->timeOffset_ >= 0.0f && this->timeOffset_ < 0.4f) ||
    178              (this->timeOffset_ >= 0.8f && this->timeOffset_ < 1.2f) ||
    179              (this->timeOffset_ >= 1.6f && this->timeOffset_ < 1.8f) ||
    180              (this->timeOffset_ >= 2.0f && this->timeOffset_ < 2.2f))
    181             this->bManeuverCalled_ = false;
     201           
     202        }
     203        if (!this || !this->getControllableEntity())
     204            return;
     205       
     206
    182207        SUPER(ActionpointController, tick, dt);
    183208    }
     
    195220            this->startAttackingEnemiesThatAreClose();
    196221        }
     222        if (!this || !this->getControllableEntity())
     223            return;
     224
    197225        this->deltaHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth() - this->previousHp;
    198226        this->previousHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth();
     227        if (!this || !this->getControllableEntity())
     228            return;
    199229
    200230        // if (this->actionCounter_ % 2 == 0)
     
    202232        if (this->action_ == Action::NONE || this->bTakenOver_)
    203233        {
    204             // Point point = closestPickup(PickupType::HEALTH);
    205             // if (point.action != Action::NONE)
    206             // {
    207             //     orxout(internal_error) << "found health" << endl;
    208             //     this->parsedActionpoints_.push_back(point);
    209             // }
    210 
    211234            if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty() && this->bDefaultFightAll_)
    212235            {
     
    218241            // this->action();
    219242        }
     243        if (!this || !this->getControllableEntity())
     244            return;
     245
    220246        //Action fightall -> fight till nobody alive
    221247        if (this->action_ == Action::FIGHTALL)
     
    307333        }
    308334
    309             // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length());
    310             // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_)
    311             // {
    312             //     //----choose where to go----
    313             //     this->maneuver();
    314             // }
    315             // else
    316             // {
    317             //     this->dodgeTowards(healthPosition);
    318             // }
    319             // //----fire if you can----
    320             // this->bShooting_ = this->canFire(); 
    321          
    322         this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ;
    323335
    324336    }
     
    402414        {
    403415            case Action::FIGHT:
    404             { return "FIGHT"; break; }
     416            { return "FIGHT"; }
    405417            case Action::FLY:
    406             { return "FLY"; break; }
     418            { return "FLY"; }
    407419            case Action::PROTECT:
    408             { return "PROTECT"; break; }
     420            { return "PROTECT"; }
    409421            case Action::NONE:
    410             { return "NONE"; break; }
     422            { return "NONE"; }
    411423            case Action::FIGHTALL:
    412             { return "FIGHTALL"; break; }
     424            { return "FIGHTALL"; }
    413425            case Action::ATTACK:
    414             { return "ATTACK"; break; }
     426            { return "ATTACK"; }
    415427            default:
    416428                return "NONE";
     
    462474    void ActionpointController::executeActionpoint()
    463475    {
     476        if (!this || !this->getControllableEntity())
     477            return;
     478
    464479        Point p;
    465480        if (this->bLoop_ && !loopActionpoints_.empty())
     
    478493        else
    479494        {
     495            if (!this || !this->getControllableEntity())
     496                return;
     497
    480498            this->setTarget(0);
    481499            this->setTargetPosition(this->getControllableEntity()->getWorldPosition());
     
    520538            case Action::PROTECT:
    521539            {
    522                
     540                if (!this || !this->getControllableEntity())
     541                    return;
     542
    523543                std::string protectName = p.name;
    524544                if (protectName == "reservedKeyword:human")
     
    583603    void ActionpointController::stayNearProtect()
    584604    {
     605        if (!this || !this->getControllableEntity())
     606            return;
     607
    585608        Vector3 targetRelativePosition(0, 300, 300);
    586609        if (!this->getProtect())
     
    665688    Pawn* ActionpointController::closestTarget()
    666689    {
    667         if (!this->getControllableEntity())
     690        if (!this || !this->getControllableEntity())
    668691            return 0;
    669692
     
    691714    void ActionpointController::startAttackingEnemiesThatAreClose()
    692715    {
     716        if (!this || !this->getControllableEntity())
     717            return;
     718
    693719        //if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
    694720        {
     
    700726                        <= this->attackRange_ )
    701727                {
     728                    if (!this || !this->getControllableEntity())
     729                        return;
    702730                    this->setTarget(newTarget);
    703731                    if (this->bLoop_ && !this->bPatrolling_)
     
    718746        }
    719747    }
    720     Vector3 ActionpointController::bestHealthPickup(float searchRadius)
    721     {
    722 
    723         //1000000/distance^2 * 1/index+1
    724         float maxUse = 0;
    725         float tempUse = -1;
    726         int index = 0;
    727         float distance = 0;
    728         Vector3 bestPosition = this->getControllableEntity()->getWorldPosition();
    729 
    730         for (std::multimap<int, std::pair<PickupSpawner*, bool> >::iterator it=healthSpawners_.begin(); it!=healthSpawners_.end(); ++it)
    731         {
    732             distance = (this->getControllableEntity()->getWorldPosition() - (*it).second.first->getWorldPosition()).length();
    733             if (distance < this->tolerance_ || !(*it).second.second)
    734             {
    735                 (*it).second.second = false;
    736                 continue;
    737             }
    738             if (distance > searchRadius)
    739                 continue;
    740             index = (*it).first;
    741             tempUse = 1000000*1/(1+distance*distance) * 1/((index/2.0f)+1);
    742             if (tempUse > maxUse)
    743             {
    744                 bestPosition = (*it).second.first->getWorldPosition();
    745                 maxUse = tempUse;
    746             }
    747         }
    748         //std::multimap<int, std::pair<PickupSpawner*, bool> >::iterator it = this->healthSpawners_.find(index);
    749         //P.S. check if it == ... .end()
    750         //orxout (internal_error) << "best position = " << bestPosition << endl;
    751         return  bestPosition;
    752     }
    753     void ActionpointController::setSpawners()
    754     {
    755         std::vector<std::string> damagePickups;
    756         std::vector<std::string> healthPickups;
    757         std::vector<std::string> speedPickups;       
    758         int index = 0;
    759 
    760 
    761         damagePickups.push_back("largedamageboostpickup");
    762         damagePickups.push_back("dronepickup");           
    763         damagePickups.push_back("mediumdamageboostpickup");
    764         damagePickups.push_back("smalldamageboostpickup");
    765 
    766         healthPickups.push_back("triplehealthspeedinvisibilitypickup");           
    767         healthPickups.push_back("crazyhealthpickup");
    768         healthPickups.push_back("hugehealthpickup");
    769         healthPickups.push_back("hugeshieldpickup");
    770         healthPickups.push_back("hugeinvisiblepickup");
    771         healthPickups.push_back("hugeshrinkpickup");
    772         healthPickups.push_back("mediumhealthpickup");
    773         healthPickups.push_back("mediumshieldpickup");
    774         healthPickups.push_back("mediuminvisiblepickup");
    775         healthPickups.push_back("mediumshrinkpickup"); 
    776         healthPickups.push_back("smallhealthpickup");                   
    777         healthPickups.push_back("smallshieldpickup");
    778         healthPickups.push_back("smallinvisiblepickup");
    779         healthPickups.push_back("smallshrinkpickup");
    780 
    781         speedPickups.push_back("triplehealthspeedinvisibilitypickup");
    782         speedPickups.push_back("hugespeedpickup");
    783         speedPickups.push_back("smalljumppickup");
    784         speedPickups.push_back("mediumspeedpickup");
    785         speedPickups.push_back("smallspeedpickup");
    786        
    787 
    788 
    789         for (ObjectList<WorldEntity>::iterator it = ObjectList<WorldEntity>::begin(); it; ++it)
    790         {
    791             if ((*it)->getIdentifier()->getName() != "PickupSpawner")
    792                 continue;
    793             PickupSpawner* spawner = static_cast<PickupSpawner*>(*it);
    794             std::string name = spawner->getPickupTemplateName();
    795             //float distance = ((*it)->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length();
    796             // if (distance < 50.0f)
    797             //     continue;           
    798             for (unsigned int i = 0; i < healthPickups.size(); ++i)
    799             {
    800                 if (name == healthPickups.at(i))
    801                 {
    802                     index = i;
    803                     this->healthSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true)));
    804                     break;
    805                 }
    806             }
    807             for (unsigned int i = 0; i < damagePickups.size(); ++i)
    808             {
    809                 if (name == damagePickups.at(i))
    810                 {
    811                     //tempUse = 1000000*1/(1+distance*distance) * 1/((i/2.0f)+1);
    812                     index = i;
    813                     this->damageSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true)));
    814                     break;
    815                 }
    816             }
    817             for (unsigned int i = 0; i < speedPickups.size(); ++i)
    818             {
    819                 if (name == speedPickups.at(i))
    820                 {
    821                     //tempUse = 1000000*1/(1+distance*distance) * 1/((i/2.0f)+1);
    822                     index = i;
    823                     this->speedSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true)));
    824                     break;
    825                 }
    826             }
    827             // if (tempUse > maxUse)
    828             // {
    829             //     Point p = { Action::FLY, "", (*it)->getWorldPosition(), false };
    830             //     maxUse = tempUse;
    831             //     maxPoint = p;
    832             // }
    833             // else
    834             // {
    835             //     tempUse = -1;
    836             //     continue;
    837             // }
    838             // orxout(internal_error) << "resp time  = " << static_cast<PickupSpawner*>(*it)->getRespawnTime() << endl;
    839         } 
    840     }
    841 }
     748    int ActionpointController::nextActionpointControllerId_ = 0;
     749    int ActionpointController::sTicks_ = 0;
     750}   
Note: See TracChangeset for help on using the changeset viewer.