[10719] | 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 |
---|
[10946] | 11 | * of the License, or (at your option)any later version. |
---|
[10719] | 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: |
---|
[10885] | 23 | * Gani Aliguzhinov |
---|
[10719] | 24 | * Co-authors: |
---|
| 25 | * Dominik Solenicki |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | #include "controllers/CommonController.h" |
---|
[10759] | 29 | |
---|
[10946] | 30 | //here starts stuff for sameTeam function copied from FormationController |
---|
[10838] | 31 | #include "gametypes/TeamDeathmatch.h" |
---|
| 32 | #include "gametypes/Gametype.h" |
---|
| 33 | #include "controllers/DroneController.h" |
---|
[10877] | 34 | #include "gametypes/Dynamicmatch.h" |
---|
[10838] | 35 | |
---|
[10877] | 36 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
| 37 | |
---|
[10719] | 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | |
---|
[10946] | 41 | RegisterClass(CommonController); |
---|
| 42 | CommonController::CommonController(Context* context): Controller(context) |
---|
[10719] | 43 | { |
---|
[10946] | 44 | RegisterObject(CommonController); |
---|
[10719] | 45 | } |
---|
[10799] | 46 | CommonController::~CommonController() |
---|
[10719] | 47 | { |
---|
[10946] | 48 | //no member variables - nothing to destroy |
---|
[10731] | 49 | } |
---|
[10946] | 50 | /** |
---|
| 51 | @brief |
---|
| 52 | PRE: a < b. |
---|
| 53 | returns random float between a and b. |
---|
| 54 | */ |
---|
| 55 | float CommonController::randomInRange(float a, float b) |
---|
[10796] | 56 | { |
---|
[10885] | 57 | return a + rnd(1.0f) * (b - a); |
---|
[10796] | 58 | } |
---|
[10946] | 59 | /** |
---|
| 60 | @brief |
---|
| 61 | returns distance between two entities, if either is zero pointer, returns infinity |
---|
| 62 | */ |
---|
| 63 | float CommonController::distance (const ControllableEntity* entity1, const ControllableEntity* entity2) |
---|
[10826] | 64 | { |
---|
| 65 | if (!entity1 || !entity2) |
---|
| 66 | return std::numeric_limits<float>::infinity(); |
---|
[10946] | 67 | return (entity1->getPosition() - entity2->getPosition()).length(); |
---|
[10826] | 68 | } |
---|
[10946] | 69 | /** |
---|
| 70 | @brief |
---|
| 71 | bad function from FormationController that returns true if both entities have same team |
---|
| 72 | */ |
---|
[10838] | 73 | bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
[10826] | 74 | { |
---|
[10946] | 75 | //uncomment following code if functions stops working due to being a hack |
---|
[10838] | 76 | /*if (!entity1 || !entity2) |
---|
[10826] | 77 | return false; |
---|
[10838] | 78 | return entity1->getTeam() == entity2->getTeam();*/ |
---|
[10946] | 79 | if (!entity1 || !entity2) |
---|
| 80 | return false; |
---|
[10838] | 81 | if (entity1 == entity2) |
---|
| 82 | return true; |
---|
| 83 | |
---|
| 84 | int team1 = entity1->getTeam(); |
---|
| 85 | int team2 = entity2->getTeam(); |
---|
| 86 | |
---|
| 87 | Controller* controller = 0; |
---|
| 88 | if (entity1->getController()) |
---|
| 89 | controller = entity1->getController(); |
---|
| 90 | else |
---|
| 91 | controller = entity1->getXMLController(); |
---|
| 92 | if (controller) |
---|
| 93 | { |
---|
[10953] | 94 | if (controller->getIdentifier()->getName() == "MasterController") |
---|
| 95 | return true; |
---|
[10838] | 96 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
| 97 | if (ac) |
---|
| 98 | team1 = ac->getTeam(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if (entity2->getController()) |
---|
| 102 | controller = entity2->getController(); |
---|
| 103 | else |
---|
| 104 | controller = entity2->getXMLController(); |
---|
| 105 | if (controller) |
---|
| 106 | { |
---|
[10953] | 107 | if (controller->getIdentifier()->getName() == "MasterController") |
---|
| 108 | return true; |
---|
[10838] | 109 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
| 110 | if (ac) |
---|
| 111 | team2 = ac->getTeam(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype); |
---|
| 115 | if (tdm) |
---|
| 116 | { |
---|
| 117 | if (entity1->getPlayer()) |
---|
| 118 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
| 119 | |
---|
| 120 | if (entity2->getPlayer()) |
---|
| 121 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | TeamBaseMatchBase* base = 0; |
---|
| 125 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
| 126 | if (base) |
---|
| 127 | { |
---|
| 128 | switch (base->getState()) |
---|
| 129 | { |
---|
| 130 | case BaseState::ControlTeam1: |
---|
| 131 | team1 = 0; |
---|
| 132 | break; |
---|
| 133 | case BaseState::ControlTeam2: |
---|
| 134 | team1 = 1; |
---|
| 135 | break; |
---|
| 136 | case BaseState::Uncontrolled: |
---|
| 137 | default: |
---|
| 138 | team1 = -1; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
| 142 | if (base) |
---|
| 143 | { |
---|
| 144 | switch (base->getState()) |
---|
| 145 | { |
---|
| 146 | case BaseState::ControlTeam1: |
---|
| 147 | team2 = 0; |
---|
| 148 | break; |
---|
| 149 | case BaseState::ControlTeam2: |
---|
| 150 | team2 = 1; |
---|
| 151 | break; |
---|
| 152 | case BaseState::Uncontrolled: |
---|
| 153 | default: |
---|
| 154 | team2 = -1; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | DroneController* droneController = 0; |
---|
| 159 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
| 160 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
| 161 | return true; |
---|
| 162 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
| 163 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
| 164 | return true; |
---|
| 165 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
| 166 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
| 167 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
| 168 | return true; |
---|
| 169 | |
---|
| 170 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
| 171 | if (dynamic) |
---|
| 172 | { |
---|
| 173 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
| 174 | |
---|
| 175 | if (entity1->getPlayer()) |
---|
| 176 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
| 177 | |
---|
| 178 | if (entity2->getPlayer()) |
---|
| 179 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
| 180 | |
---|
[10946] | 181 | if (team1 ==-1 ||team2 ==-1) {return false;} |
---|
[10838] | 182 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
| 183 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
| 184 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
| 185 | else return true; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | return (team1 == team2 && team1 != -1); |
---|
[10826] | 189 | } |
---|
[10946] | 190 | /** |
---|
| 191 | @brief |
---|
| 192 | returns true if entityThatLooks does look at entityBeingLookeAt with a tolerance of angle. |
---|
| 193 | */ |
---|
| 194 | bool CommonController::isLooking(const ControllableEntity* entityThatLooks, const ControllableEntity* entityBeingLookedAt, float angle) |
---|
[10851] | 195 | { |
---|
[10946] | 196 | if (!entityThatLooks || !entityBeingLookedAt) |
---|
[10851] | 197 | return false; |
---|
[10946] | 198 | return (getAngle(entityThatLooks ->getPosition() , |
---|
[10851] | 199 | entityThatLooks->getOrientation() * WorldEntity::FRONT, |
---|
[10946] | 200 | entityBeingLookedAt->getWorldPosition()) < angle); |
---|
[10851] | 201 | } |
---|
[10946] | 202 | /** |
---|
| 203 | @brief |
---|
| 204 | returns a name of a Pawn entity, if no name set, returns string representing address of the Pawn. |
---|
| 205 | */ |
---|
| 206 | std::string CommonController::getName(const Pawn* entity) |
---|
[10851] | 207 | { |
---|
| 208 | std::string name = entity->getName(); |
---|
| 209 | if (name == "") |
---|
| 210 | { |
---|
| 211 | const void * address = static_cast<const void*>(entity); |
---|
| 212 | std::stringstream ss; |
---|
| 213 | ss << address; |
---|
| 214 | name = ss.str(); |
---|
| 215 | } |
---|
| 216 | return name; |
---|
| 217 | } |
---|
[10719] | 218 | } |
---|