GoalManager.cpp 2.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
//
// Created by Tobias Schrödter on 2019-02-17.
//

#include "GoalManager.h"

#include "../pedestrian/Pedestrian.h"
#include "WaitingArea.h"

void GoalManager::SetGoals(std::map<int, Goal*> goals)
{
     _allGoals = goals;
}

void GoalManager::ProcessPedPosition(Pedestrian* ped)
{
//     Goal* goal = _allGoals[ped->GetFinalDestination()];
//
//     if (goal!=nullptr){
//          if (WaitingArea* wa = dynamic_cast<WaitingArea*>(goal)) {
//               bool pedInside = wa->IsInsideGoal(ped->GetPos());
//
//               if (pedInside) {
//                    wa->addPed(ped->GetID());
//
//                    if (!wa->isOpen()) {
//                         for (auto& goalItr : _allGoals) {
//                              if (WaitingArea* goal = dynamic_cast<WaitingArea*>(goalItr.second)) {
//                                   goal->updateProbabilities(false, wa->GetId());
//                              }
//                         }
//                    }
//
//               }
//               else {
//
//
//                    // if just has left goal
//                    //   decrease numPed in previous goal
//                    //   if numPed <= maxPed
//                    //        open Goal
//                    //        update for goal where next goal has goal id
//               }
//          }
//     }

     // Ped is in current waiting area
     if (CheckInsideWaitingArea(ped, ped->GetFinalDestination())){
          WaitingArea* wa = dynamic_cast<WaitingArea*>(_allGoals[ped->GetFinalDestination()]);
          wa->addPed(ped->GetID());
          ped->EnterGoal();
          if (!wa->isOpen()) {
               SetState(wa->GetId(), false);
          }
     }

     if ((ped->GetLastGoalID() != -1 ) && (ped->GetFinalDestination() != ped->GetLastGoalID())){
          if (!CheckInsideWaitingArea(ped, ped->GetLastGoalID())){
               WaitingArea* wa = dynamic_cast<WaitingArea*>(_allGoals[ped->GetLastGoalID()]);

               if (wa != nullptr){
                    wa->removePed(ped->GetID());
                    ped->LeaveGoal();
                    if (wa->isOpen()) {
                         SetState(wa->GetId(), true);
                    }

               }
          }
     }
}

bool GoalManager::CheckInside(Pedestrian* ped, int goalID)
{
     Goal* goal = _allGoals[goalID];

     if (goal!=nullptr){
          return goal->IsInsideGoal(ped->GetPos());
     }
     return false;

}

bool GoalManager::CheckInsideWaitingArea(Pedestrian* ped, int goalID)
{
     Goal* goal = _allGoals[goalID];

     if (goal!=nullptr){
Ozaq's avatar
Ozaq committed
89
          if (dynamic_cast<WaitingArea*>(goal)) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
               return goal->IsInsideGoal(ped->GetPos());
          }
     }
     return false;

}

void GoalManager::SetState(int goalID, bool state)
{
     for (auto& goalItr : _allGoals) {
          if (WaitingArea* goal = dynamic_cast<WaitingArea*>(goalItr.second)) {
               goal->updateProbabilities(state, goalID);
          }
     }

}