Changeset 10910 for code/branches
- Timestamp:
- Dec 1, 2015, 4:33:46 PM (9 years ago)
- Location:
- code/branches/campaignHS15/src/orxonox/controllers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc
r10909 r10910 143 143 SUPER(ActionpointController, tick, dt); 144 144 } 145 std::pair <ControllableEntity*, ControllableEntity*> ActionpointController::closestTargets() 146 { 147 WeakPtr<ControllableEntity> firstTarget, secondTarget, tempTarget; 148 float firstDistance = std::numeric_limits<float>::infinity(), secondDistance = std::numeric_limits<float>::infinity(), tempDistance = 0; 149 Gametype* gt = this->getGametype(); 150 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) 151 { 152 tempTarget = static_cast<ControllableEntity*>(*itP); 153 if (CommonController::sameTeam (this->getControllableEntity(), tempTarget, gt)) 154 continue; 155 156 tempDistance = CommonController::distance (*itP, this->getControllableEntity()); 157 if (tempDistance < firstDistance) 158 { 159 secondDistance = firstDistance; 160 secondTarget = firstTarget; 161 162 firstDistance = tempDistance; 163 firstTarget = tempTarget; 164 } 165 else if (tempDistance < secondDistance) 166 { 167 secondDistance = tempDistance; 168 secondTarget = tempTarget; 169 } 170 } 171 return std::make_pair (firstTarget, secondTarget); 172 } 173 174 //patrol can only be called by divisionController or others if they don't have a leader 175 //searches for closest enemy and sets target to it. If wasn't fighting, pushes Action::FIGHT on a stack. 176 //patrol gets called constantly either if this is fighting or if this->bDefaultPatrol_ is set to true via XML 177 bool ActionpointController::patrol() 178 { 179 std::pair <ControllableEntity*, ControllableEntity*> newTargets = this->closestTargets(); 180 if (!newTargets.first) 181 return false; 182 ControllableEntity* newTarget = newTargets.first; 183 184 float distance = CommonController::distance (this->getControllableEntity(), newTarget); 185 if (distance < this->attackRange_ && distance < CommonController::distance(this->getControllableEntity(), this->target_)) 186 { 187 this->setTarget (newTarget); 188 189 if (this->getIdentifier()->getName() == "DivisionController") 190 { 191 if (!newTargets.second) 192 { 193 if (this->myFollower_) 194 this->myFollower_->setTarget(newTargets.first); 195 } 196 else 197 { 198 if (this->myFollower_ && CommonController::distance(this->getControllableEntity(), newTargets.second) < this->attackRange_ + 600.0f) 199 this->myFollower_->setTarget(newTargets.second); 200 else if (this->myFollower_) 201 this->myFollower_->setTarget(newTargets.first); 202 } 203 } 204 if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL) 205 { 206 this->bPatrolling_ = true; 207 if (this->bLoop_) 208 { 209 Point p = { Action::FIGHT, "", Vector3::ZERO, true }; 210 this->loopActionpoints_.push_back(p); 211 this->executeActionpoint(); 212 } 213 else 214 { 215 Point p = { Action::FIGHT, "", Vector3::ZERO, false }; 216 this->parsedActionpoints_.push_back(p); 217 this->executeActionpoint(); 218 } 219 } 220 return true; 221 } 222 else 223 { 224 return false; 225 } 226 } 227 //checks if state is still to be executed and makes a transition otherwise. 228 //if this->bDefaultPatrol_ == true, patrols area for enemies and, if not fighting, pushes a state with action = FIGHT 229 void ActionpointController::stateMachine() 230 { 231 //Check if calculations needed 232 if (!this->getControllableEntity() || !orxonox_cast<Pawn*> (this->getControllableEntity()) || !this->isActive()) 233 return; 234 235 //state NONE means that either previous state finished executing and next state is to be fetched or no states are left to execute 236 //NONE is never on a stack -> it is only a transition state saved in a variable 237 if (this->action_ == Action::NONE || this->bTakenOver_) 238 { 239 //no actionpoints a.k.a. states left to execute 240 if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty()) 241 { 242 //default action is fighting 243 if (this->bDefaultFightAll_) 244 { 245 //make state 246 Point p = { Action::FIGHTALL, "", Vector3::ZERO, false }; 247 //push it on the stack 248 this->parsedActionpoints_.push_back (p); 249 } 250 //default action is nothing 251 else 252 { 253 this->bActive_ = false; 254 return; 255 } 256 } 257 //switch to the new state 258 this->executeActionpoint(); 259 this->bTakenOver_ = false; 260 } 261 if (this->action_ == Action::FIGHTALL) 262 { 263 if (!this->hasTarget()) 264 { 265 ControllableEntity* newTarget = this->closestTarget(); 266 if (newTarget) 267 { 268 this->setTarget (newTarget); 269 } 270 else 271 { 272 this->nextActionpoint(); 273 this->executeActionpoint(); 274 } 275 } 276 else 277 { 278 bool b = this->patrol(); 279 } 280 281 } 282 if (this->action_ == Action::FIGHT) 283 { 284 if (!this->hasTarget() ) 285 { 286 if (!this->patrol()) 287 { 288 if (this->bPatrolling_) 289 { 290 if (this->bLoop_) 291 { 292 if (!this->loopActionpoints_.empty()) 293 { 294 this->loopActionpoints_.pop_back(); 295 } 296 } 297 else 298 { 299 if (!this->parsedActionpoints_.empty()) 300 { 301 this->parsedActionpoints_.pop_back(); 302 } 303 } 304 this->setAction(Action::NONE); 305 this->bHasTargetPosition_ = false; 306 this->bPatrolling_ = false; 307 } 308 else 309 { 310 this->nextActionpoint(); 311 } 312 this->executeActionpoint(); 313 } 314 } 315 else if (this->hasTarget()) 316 { 317 if (CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_) 318 { 319 this->setTarget(0); 320 this->stateMachine(); 321 } 322 } 323 } 324 if (this->action_ == Action::FLY) 325 { 326 if (this->squaredDistanceToTarget() <= this->squaredaccuracy_) 327 { 328 this->nextActionpoint(); 329 this->executeActionpoint(); 330 } 331 else if (this->bDefaultPatrol_) 332 { 333 bool b = this->patrol(); 334 } 335 } 336 if (this->action_ == Action::PROTECT) 337 { 338 if (!this->getProtect()) 339 { 340 this->nextActionpoint(); 341 this->executeActionpoint(); 342 } 343 else 344 { 345 this->stayNearProtect(); 346 } 347 if (this->bDefaultPatrol_) 348 { 349 bool b = this->patrol(); 350 } 351 } 352 if (this->action_ == Action::ATTACK) 353 { 354 if (!this->hasTarget()) 355 { 356 this->nextActionpoint(); 357 this->executeActionpoint(); 358 } 359 if (this->bDefaultPatrol_) 360 { 361 bool b = this->patrol(); 362 } 363 } 364 } 365 366 367 145 368 void ActionpointController::action() 146 369 { -
code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h
r10909 r10910 180 180 virtual bool hasFollower() 181 181 { return true; } 182 void stateMachine(); 183 bool patrol(); 184 std::pair <ControllableEntity*, ControllableEntity*> closestTargets(); 182 185 183 186 protected: … … 239 242 bool bActionCalled_; 240 243 bool bManeuverCalled_; 244 bool bDefaultFightAll_; 245 bool bActive_; 246 bool bPatrolling_; 247 bool bDefaultPatrol_; 241 248 242 249 private:
Note: See TracChangeset
for help on using the changeset viewer.