| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option)any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Gani Aliguzhinov | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 | #include "controllers/CommonController.h" | 
|---|
| 29 |  | 
|---|
| 30 | //here starts stuff for sameTeam function copied from FormationController | 
|---|
| 31 | #include "gametypes/TeamDeathmatch.h" | 
|---|
| 32 | #include "gametypes/Gametype.h" | 
|---|
| 33 | #include "controllers/DroneController.h" | 
|---|
| 34 | #include "gametypes/Dynamicmatch.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "worldentities/pawns/TeamBaseMatchBase.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 |     const float CommonController::HARDCODED_PROJECTILE_SPEED = 750; | 
|---|
| 41 |  | 
|---|
| 42 |     RegisterClass(CommonController); | 
|---|
| 43 |     CommonController::CommonController(Context* context): Controller(context) | 
|---|
| 44 |     { | 
|---|
| 45 |         RegisterObject(CommonController); | 
|---|
| 46 |     } | 
|---|
| 47 |     CommonController::~CommonController()  | 
|---|
| 48 |     { | 
|---|
| 49 |         //no member variables - nothing to destroy | 
|---|
| 50 |     } | 
|---|
| 51 |     /** | 
|---|
| 52 |     @brief | 
|---|
| 53 |       PRE: a < b. | 
|---|
| 54 |       returns random float between a and b. | 
|---|
| 55 |     */ | 
|---|
| 56 |     float CommonController::randomInRange(float a, float b) | 
|---|
| 57 |     { | 
|---|
| 58 |         return a + rnd(1.0f) * (b - a); | 
|---|
| 59 |     } | 
|---|
| 60 |     /** | 
|---|
| 61 |     @brief | 
|---|
| 62 |       returns distance between two entities, if either is zero pointer, returns infinity | 
|---|
| 63 |     */     | 
|---|
| 64 |     float CommonController::distance (const ControllableEntity* entity1, const ControllableEntity* entity2) | 
|---|
| 65 |     { | 
|---|
| 66 |         if (!entity1 || !entity2) | 
|---|
| 67 |             return std::numeric_limits<float>::infinity(); | 
|---|
| 68 |         return (entity1->getPosition() - entity2->getPosition()).length(); | 
|---|
| 69 |     } | 
|---|
| 70 |     /** | 
|---|
| 71 |     @brief | 
|---|
| 72 |       bad function from FormationController that returns true if both entities have same team | 
|---|
| 73 |     */     | 
|---|
| 74 |     bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) | 
|---|
| 75 |     { | 
|---|
| 76 |          | 
|---|
| 77 |         if (!entity1 || !entity2) | 
|---|
| 78 |             return false; | 
|---|
| 79 |         if (entity1 == entity2) | 
|---|
| 80 |             return true; | 
|---|
| 81 |  | 
|---|
| 82 |         int team1 = entity1->getTeam(); | 
|---|
| 83 |         int team2 = entity2->getTeam(); | 
|---|
| 84 |  | 
|---|
| 85 |         Controller* controller = nullptr; | 
|---|
| 86 |         if (entity1->getController()) | 
|---|
| 87 |             controller = entity1->getController(); | 
|---|
| 88 |         else | 
|---|
| 89 |             controller = entity1->getXMLController(); | 
|---|
| 90 |         if (controller) | 
|---|
| 91 |         { | 
|---|
| 92 |             if (controller->getIdentifier()->getName() == "MasterController") | 
|---|
| 93 |                 return true; | 
|---|
| 94 |             CommonController* ac = orxonox_cast<CommonController*>(controller); | 
|---|
| 95 |             if (ac) | 
|---|
| 96 |                 team1 = ac->getTeam(); | 
|---|
| 97 |         } | 
|---|
| 98 |  | 
|---|
| 99 |         if (entity2->getController()) | 
|---|
| 100 |             controller = entity2->getController(); | 
|---|
| 101 |         else | 
|---|
| 102 |             controller = entity2->getXMLController(); | 
|---|
| 103 |         if (controller) | 
|---|
| 104 |         { | 
|---|
| 105 |             if (controller->getIdentifier()->getName() == "MasterController") | 
|---|
| 106 |                 return true; | 
|---|
| 107 |             CommonController* ac = orxonox_cast<CommonController*>(controller); | 
|---|
| 108 |             if (ac) | 
|---|
| 109 |                 team2 = ac->getTeam(); | 
|---|
| 110 |         } | 
|---|
| 111 |  | 
|---|
| 112 |         TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype); | 
|---|
| 113 |         if (tdm) | 
|---|
| 114 |         { | 
|---|
| 115 |             if (entity1->getPlayer()) | 
|---|
| 116 |                 team1 = tdm->getTeam(entity1->getPlayer()); | 
|---|
| 117 |  | 
|---|
| 118 |             if (entity2->getPlayer()) | 
|---|
| 119 |                 team2 = tdm->getTeam(entity2->getPlayer()); | 
|---|
| 120 |         } | 
|---|
| 121 |  | 
|---|
| 122 |         TeamBaseMatchBase* base = nullptr; | 
|---|
| 123 |         base = orxonox_cast<TeamBaseMatchBase*>(entity1); | 
|---|
| 124 |         if (base) | 
|---|
| 125 |         { | 
|---|
| 126 |             switch (base->getState()) | 
|---|
| 127 |             { | 
|---|
| 128 |                 case BaseState::ControlTeam1: | 
|---|
| 129 |                     team1 = 0; | 
|---|
| 130 |                     break; | 
|---|
| 131 |                 case BaseState::ControlTeam2: | 
|---|
| 132 |                     team1 = 1; | 
|---|
| 133 |                     break; | 
|---|
| 134 |                 case BaseState::Uncontrolled: | 
|---|
| 135 |                 default: | 
|---|
| 136 |                     team1 = -1; | 
|---|
| 137 |             } | 
|---|
| 138 |         } | 
|---|
| 139 |         base = orxonox_cast<TeamBaseMatchBase*>(entity2); | 
|---|
| 140 |         if (base) | 
|---|
| 141 |         { | 
|---|
| 142 |             switch (base->getState()) | 
|---|
| 143 |             { | 
|---|
| 144 |                 case BaseState::ControlTeam1: | 
|---|
| 145 |                     team2 = 0; | 
|---|
| 146 |                     break; | 
|---|
| 147 |                 case BaseState::ControlTeam2: | 
|---|
| 148 |                     team2 = 1; | 
|---|
| 149 |                     break; | 
|---|
| 150 |                 case BaseState::Uncontrolled: | 
|---|
| 151 |                 default: | 
|---|
| 152 |                     team2 = -1; | 
|---|
| 153 |             } | 
|---|
| 154 |         } | 
|---|
| 155 |  | 
|---|
| 156 |         DroneController* droneController = nullptr; | 
|---|
| 157 |         droneController = orxonox_cast<DroneController*>(entity1->getController()); | 
|---|
| 158 |         if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) | 
|---|
| 159 |             return true; | 
|---|
| 160 |         droneController = orxonox_cast<DroneController*>(entity2->getController()); | 
|---|
| 161 |         if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) | 
|---|
| 162 |             return true; | 
|---|
| 163 |         DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); | 
|---|
| 164 |         DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); | 
|---|
| 165 |         if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) | 
|---|
| 166 |             return true; | 
|---|
| 167 |  | 
|---|
| 168 |         Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); | 
|---|
| 169 |         if (dynamic) | 
|---|
| 170 |         { | 
|---|
| 171 |             if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} | 
|---|
| 172 |  | 
|---|
| 173 |             if (entity1->getPlayer()) | 
|---|
| 174 |                 team1 = dynamic->getParty(entity1->getPlayer()); | 
|---|
| 175 |  | 
|---|
| 176 |             if (entity2->getPlayer()) | 
|---|
| 177 |                 team2 = dynamic->getParty(entity2->getPlayer()); | 
|---|
| 178 |  | 
|---|
| 179 |             if (team1 ==-1 ||team2 ==-1) {return false;} | 
|---|
| 180 |             else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} | 
|---|
| 181 |             else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} | 
|---|
| 182 |             else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} | 
|---|
| 183 |             else return true; | 
|---|
| 184 |         } | 
|---|
| 185 |  | 
|---|
| 186 |         return (team1 == team2 && team1 != -1); | 
|---|
| 187 |     } | 
|---|
| 188 |     /** | 
|---|
| 189 |     @brief | 
|---|
| 190 |       returns true if entityThatLooks does look at entityBeingLookeAt with a tolerance of angle. | 
|---|
| 191 |     */     | 
|---|
| 192 |     bool CommonController::isLooking(const ControllableEntity* entityThatLooks, const ControllableEntity* entityBeingLookedAt, float angle) | 
|---|
| 193 |     { | 
|---|
| 194 |         if (!entityThatLooks || !entityBeingLookedAt) | 
|---|
| 195 |             return false; | 
|---|
| 196 |         return (getAngle(entityThatLooks ->getPosition() ,  | 
|---|
| 197 |             entityThatLooks->getOrientation()  * WorldEntity::FRONT,  | 
|---|
| 198 |             entityBeingLookedAt->getWorldPosition()) < angle); | 
|---|
| 199 |     } | 
|---|
| 200 |     /** | 
|---|
| 201 |     @brief | 
|---|
| 202 |       returns a name of a Pawn entity, if no name set, returns string representing address of the Pawn. | 
|---|
| 203 |     */     | 
|---|
| 204 |     std::string CommonController::getName(const Pawn* entity) | 
|---|
| 205 |     { | 
|---|
| 206 |         std::string name = entity->getName(); | 
|---|
| 207 |         if (name == "") | 
|---|
| 208 |         { | 
|---|
| 209 |             const void * address = static_cast<const void*>(entity); | 
|---|
| 210 |             std::stringstream ss; | 
|---|
| 211 |             ss << address;   | 
|---|
| 212 |             name = ss.str();             | 
|---|
| 213 |         } | 
|---|
| 214 |         return name; | 
|---|
| 215 |     } | 
|---|
| 216 | } | 
|---|