/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or ( at your option )any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * Dominik Solenicki * */ #include "controllers/CommonController.h" #include "core/XMLPort.h" //stuff for sameTeam function #include "worldentities/pawns/TeamBaseMatchBase.h" #include "gametypes/TeamDeathmatch.h" #include "gametypes/Dynamicmatch.h" #include "gametypes/Mission.h" #include "gametypes/Gametype.h" #include "controllers/WaypointPatrolController.h" #include "controllers/NewHumanController.h" #include "controllers/DroneController.h" namespace orxonox { RegisterClass( CommonController ); const float SPEED = 0.9f/0.02f; const float ROTATEFACTOR = 1.0f/0.02f; CommonController::CommonController( Context* context ): Controller( context ) { this->bFirstTick_ = true; RegisterObject( CommonController ); } CommonController::~CommonController() { } void CommonController::tick(float dt) { SUPER(CommonController, tick, dt); } void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) { SUPER( CommonController, XMLPort, xmlelement, mode ); } //"Virtual" methods bool CommonController::setWingman ( CommonController* wingman ) { return false; } bool CommonController::hasWingman() { return true; } float CommonController::randomInRange( float a, float b ) { float random = rnd( 1.0f ); float diff = b - a; float r = random * diff; return a + r; } float CommonController::distance (ControllableEntity* entity1, ControllableEntity* entity2) { if (!entity1 || !entity2) return std::numeric_limits::infinity(); return ( entity1->getPosition() - entity2->getPosition() ).length(); } bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) { /*if (!entity1 || !entity2) return false; return entity1->getTeam() == entity2->getTeam();*/ if (entity1 == entity2) return true; int team1 = entity1->getTeam(); int team2 = entity2->getTeam(); Controller* controller = 0; if (entity1->getController()) controller = entity1->getController(); else controller = entity1->getXMLController(); if (controller) { CommonController* ac = orxonox_cast(controller); if (ac) team1 = ac->getTeam(); } if (entity2->getController()) controller = entity2->getController(); else controller = entity2->getXMLController(); if (controller) { CommonController* ac = orxonox_cast(controller); if (ac) team2 = ac->getTeam(); } TeamGametype* tdm = orxonox_cast(gametype); if (tdm) { if (entity1->getPlayer()) team1 = tdm->getTeam(entity1->getPlayer()); if (entity2->getPlayer()) team2 = tdm->getTeam(entity2->getPlayer()); } TeamBaseMatchBase* base = 0; base = orxonox_cast(entity1); if (base) { switch (base->getState()) { case BaseState::ControlTeam1: team1 = 0; break; case BaseState::ControlTeam2: team1 = 1; break; case BaseState::Uncontrolled: default: team1 = -1; } } base = orxonox_cast(entity2); if (base) { switch (base->getState()) { case BaseState::ControlTeam1: team2 = 0; break; case BaseState::ControlTeam2: team2 = 1; break; case BaseState::Uncontrolled: default: team2 = -1; } } DroneController* droneController = 0; droneController = orxonox_cast(entity1->getController()); if (droneController && static_cast(droneController->getOwner()) == entity2) return true; droneController = orxonox_cast(entity2->getController()); if (droneController && static_cast(droneController->getOwner()) == entity1) return true; DroneController* droneController1 = orxonox_cast(entity1->getController()); DroneController* droneController2 = orxonox_cast(entity2->getController()); if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) return true; Dynamicmatch* dynamic = orxonox_cast(gametype); if (dynamic) { if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} if (entity1->getPlayer()) team1 = dynamic->getParty(entity1->getPlayer()); if (entity2->getPlayer()) team2 = dynamic->getParty(entity2->getPlayer()); if (team1 ==-1 ||team2 ==-1 ) {return false;} else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} else return true; } return (team1 == team2 && team1 != -1); } bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) { if ( !entityThatLooks || !entityBeingLookedAt ) return false; return ( getAngle( entityThatLooks ->getPosition() , entityThatLooks->getOrientation() * WorldEntity::FRONT, entityBeingLookedAt->getWorldPosition() ) < angle ); } std::string CommonController::getName(Pawn* entity) { std::string name = entity->getName(); if (name == "") { const void * address = static_cast(entity); std::stringstream ss; ss << address; name = ss.str(); } return name; } }