- Timestamp:
- May 15, 2014, 5:10:55 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
r10049 r10060 35 35 RegisterClass(TurretController); 36 36 37 /** 38 @brief 39 Sets default values for all variables. 40 41 @param context 42 The context 43 */ 37 44 TurretController::TurretController(Context* context) : ArtificialController(context) 38 45 { … … 43 50 } 44 51 52 /** 53 @brief 54 Destructor. Nothing to see here. 55 */ 45 56 TurretController::~TurretController() 46 57 { … … 48 59 } 49 60 61 /** 62 @brief 63 Searches a valid target for the turret to aim at. 64 65 Loops through all pawns and tests, if it is in range. Scores every pawn and chooses the best one (the one with the lowest score). 66 If the turret has a parent, try to aim at the same target the parent has, if there is one. 67 68 @see targetScore 69 The function that scores the pawns. 70 */ 50 71 void TurretController::searchTarget() 51 72 { 52 73 Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity()); 53 if(target_ && turret->isInRange(target_->getWorldPosition())) 74 75 //The controller might find a target before teams are set, so we need to check again here. 76 if(this->target_ && turret->isInRange(target_) != -1.f && !FormationController::sameTeam(turret, this->target_, this->getGametype())) 54 77 { 55 78 return; … … 66 89 { 67 90 Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget()); 68 if(parenttarget && turret->isInRange(parenttarget ->getWorldPosition()))91 if(parenttarget && turret->isInRange(parenttarget)) 69 92 { 70 93 this->setTarget(parenttarget); … … 74 97 } 75 98 76 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) 99 float maxScore = 0; 100 float tempScore; 101 Pawn* maxScorePawn = 0; 102 103 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) 77 104 { 78 105 Pawn* entity = orxonox_cast<Pawn*>(*it); 79 if ( this->FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))106 if (!entity || FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype())) 80 107 continue; 81 82 if(t urret->isInRange(entity->getWorldPosition()))108 tempScore = turret->isInRange(entity); 109 if(tempScore != -1.f) 83 110 { 84 this->setTarget(entity); 85 turret->setTarget(entity); 86 break; 111 if(tempScore > maxScore) 112 { 113 maxScore = tempScore; 114 maxScorePawn = entity; 115 } 87 116 } 88 } 89 } 90 117 } 118 this->setTarget(maxScorePawn); 119 turret->setTarget(maxScorePawn); 120 } 121 122 /** 123 @brief 124 Tests, if the turret is looking at the target, with a specified tolerance 125 126 This uses the world position as opposed to the local position in the old version. 127 128 @param angle 129 The tolerance, in radians 130 */ 91 131 bool TurretController::isLookingAtTargetNew(float angle) const 92 132 { … … 94 134 } 95 135 136 /** 137 @brief 138 Scores a pawn as a target, based on distance and health. 139 140 The more health and distance a pawn has, the higher the score. This means lower equals better target. 141 142 @param pawn 143 The pawn to score 144 145 @param distance 146 The distance. Can be squared or normed, doesn't matter as long as all are treated the same. 147 */ 148 float TurretController::targetScore(Pawn* pawn, float distance) const 149 { 150 return pawn->getHealth()/pawn->getMaxHealth() + distance; 151 } 152 153 /** 154 @brief 155 Does all the controlling of the turret. 156 157 If the turret has a parent, copies the team from there, if it's not already set. 158 Other actions are: Search a target. If a target has been found, aim and shoot at it. 159 */ 96 160 void TurretController::tick(float dt) 97 161 { … … 100 164 101 165 102 if(!this->once_) 103 { 104 if(this->getTeam() != -1) 105 { 106 orxout(internal_warning) << "Turret: Team already set, may result in undesired behaviour" << endl; 107 } 108 else 109 { 110 //Make sure the turret is in the same team as the parent 111 ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent()); 112 if(parent) 113 { 114 Controller* parentcontroller = parent->getController(); 115 if(parentcontroller) 116 { 117 this->setTeam(parentcontroller->getTeam()); 118 } 119 else 120 { 121 this->setTeam(parent->getTeam()); 122 } 123 this->getControllableEntity()->setTeam(parent->getTeam()); 124 } 125 } 126 this->once_ = true; 166 ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent()); 167 if(this->getTeam() != -1 && !this->once_ && parent) 168 { 169 orxout(internal_warning) << "TurretController: Team already set, may result in undesired behaviour. Will get overridden by the parent's team." << endl; 170 } 171 172 if(!this->once_) 173 this->once_ = true; 174 175 //Teams aren't set immediately, after creation, so we have to check every tick... 176 if(parent) 177 { 178 Controller* parentcontroller = parent->getController(); 179 if(parentcontroller) 180 { 181 this->setTeam(parentcontroller->getTeam()); 182 } 183 else 184 { 185 this->setTeam(parent->getTeam()); 186 } 187 this->getControllableEntity()->setTeam(parent->getTeam()); 127 188 } 128 189 129 190 this->searchTarget(); 130 if(t arget_)191 if(this->target_) 131 192 { 132 193 Turret* turret = orxonox_cast<Turret*> (this->getControllableEntity());
Note: See TracChangeset
for help on using the changeset viewer.