[10663] | 1 | /* |
---|
| 2 | * orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 orx |
---|
| 5 | * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | * any later version. |
---|
| 10 | * |
---|
| 11 | * ### File Specific: |
---|
| 12 | * main-programmer: Reto Luechinger |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 17 | #include "adm_turret.h" |
---|
[10664] | 18 | #include "weapons/weapon_manager.h" |
---|
| 19 | #include "weapons/weapon.h" |
---|
[10663] | 20 | #include "lib/util/loading/factory.h" |
---|
| 21 | #include "world_entities/projectiles/projectile.h" |
---|
[10664] | 22 | #include "loading/load_param.h" |
---|
| 23 | #include "debug.h" |
---|
[10677] | 24 | #include "loading/load_param_xml.h" |
---|
[10663] | 25 | |
---|
| 26 | ObjectListDefinition(AdmTurret); |
---|
| 27 | CREATE_FACTORY(AdmTurret); |
---|
| 28 | |
---|
[10664] | 29 | |
---|
[10663] | 30 | /** |
---|
| 31 | * Standard constructor |
---|
| 32 | */ |
---|
| 33 | AdmTurret::AdmTurret () |
---|
| 34 | { |
---|
[10664] | 35 | this->init(); |
---|
[10663] | 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
[10666] | 39 | * destructs the turret, deletes allocated memory |
---|
[10663] | 40 | */ |
---|
| 41 | AdmTurret::~AdmTurret () |
---|
| 42 | { |
---|
| 43 | // will be deleted |
---|
| 44 | } |
---|
| 45 | |
---|
[10666] | 46 | /** |
---|
| 47 | * Constructor with XML Element |
---|
| 48 | */ |
---|
[10664] | 49 | AdmTurret::AdmTurret (const TiXmlElement* root) |
---|
[10663] | 50 | { |
---|
[10664] | 51 | this->init(); |
---|
| 52 | if (root != NULL) |
---|
| 53 | { |
---|
| 54 | this->loadParams(root); |
---|
| 55 | } |
---|
[10663] | 56 | } |
---|
[10677] | 57 | /** |
---|
| 58 | * XML Loader |
---|
| 59 | */ |
---|
| 60 | void AdmTurret::loadParams(const TiXmlElement* root) |
---|
| 61 | { |
---|
| 62 | if (root != NULL) |
---|
| 63 | { |
---|
| 64 | WorldEntity::loadParams(root); |
---|
[10664] | 65 | |
---|
[10677] | 66 | LoadParam(root, "target", this, AdmTurret, setTarget) |
---|
| 67 | .describe("The filename of the World Entity, that is to be shot at") |
---|
| 68 | .defaultValues(""); |
---|
[10681] | 69 | |
---|
| 70 | LoadParam(root, "type", this, AdmTurret, setType) |
---|
| 71 | .describe("floor|ceil"); |
---|
[10677] | 72 | |
---|
| 73 | LoadParamXML(root, "Cannons", this, AdmTurret, addCannons) |
---|
| 74 | .describe("add cannons to ADM"); |
---|
| 75 | LoadParamXML(root, "Sensor", this, AdmTurret, addSensor) |
---|
| 76 | .describe("add sensor to ADM"); |
---|
| 77 | |
---|
| 78 | } |
---|
[10681] | 79 | // this->removeNodeFlags( PNODE_ALL ); |
---|
| 80 | // this->addNodeFlags( PNODE_ROTATE_AND_MOVE ); |
---|
[10679] | 81 | |
---|
[10681] | 82 | if ( this->isCeil ) |
---|
| 83 | { |
---|
| 84 | Vector a = this->sensor->getRelCoor(); |
---|
| 85 | this->sensor->setRelCoor( -a.x, -a.y, -a.z ); |
---|
| 86 | a = this->cannons->getRelCoor(); |
---|
| 87 | this->cannons->setRelCoor( -a.x, -a.y, -a.z ); |
---|
| 88 | } |
---|
[10677] | 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Sets Target onto the World Entity with filename "target", given as String |
---|
| 93 | */ |
---|
| 94 | void AdmTurret::setTarget(const std::string& target) |
---|
| 95 | { |
---|
| 96 | WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); |
---|
| 97 | if (targetEntity != NULL) |
---|
| 98 | { |
---|
| 99 | this->myTarget = targetEntity; |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | PRINTF(1)("ERROR ADMTURRET : Target %s does not exist\n", target.c_str()); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
[10681] | 107 | void AdmTurret::setType( const std::string& type ) |
---|
| 108 | { |
---|
| 109 | if ( type == "floor" ) |
---|
| 110 | { |
---|
| 111 | this->isCeil = false; |
---|
| 112 | return; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if ( type == "ceil" ) |
---|
| 116 | { |
---|
| 117 | this->isCeil = true; |
---|
| 118 | return; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | //invalid argument |
---|
| 122 | PRINTF(1)("%s is not a valid type for AdmTurret\n", type.c_str()); |
---|
| 123 | assert("false"); |
---|
| 124 | } |
---|
| 125 | |
---|
[10663] | 126 | void AdmTurret::init() |
---|
| 127 | { |
---|
[10664] | 128 | this->registerObject(this, AdmTurret::_objectList); |
---|
[10679] | 129 | |
---|
[10681] | 130 | this->bodyAngle = this->cannonAngle = 0.0f; |
---|
| 131 | this->isActive = true; |
---|
| 132 | this->range = 400; |
---|
| 133 | this->isCeil = false; |
---|
[10663] | 134 | } |
---|
| 135 | |
---|
[10677] | 136 | void AdmTurret::tick(float dt) |
---|
[10663] | 137 | { |
---|
[10677] | 138 | WorldEntity::tick(dt); |
---|
[10664] | 139 | |
---|
[10677] | 140 | //rotate sensor 2PI/sec |
---|
| 141 | this->sensor->setAbsDir( this->sensor->getAbsDir() * Quaternion( PI*2*dt, Vector( 0, 1, 0 ) ) ); |
---|
[10663] | 142 | |
---|
[10677] | 143 | |
---|
| 144 | Vector playerPos = this->myTarget->getAbsCoor(); |
---|
[10681] | 145 | Vector ds = playerPos - ( this->cannons->getAbsCoor() ); |
---|
| 146 | if ( isActive && ds.len() <= range ) |
---|
| 147 | this->moveTowards( ds, dt); |
---|
| 148 | else |
---|
| 149 | this->moveTowards( Vector(0, -1, 0), dt ); |
---|
[10664] | 150 | } |
---|
[10663] | 151 | |
---|
[10664] | 152 | void AdmTurret::draw() const |
---|
| 153 | { |
---|
[10677] | 154 | WorldEntity::draw(); |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | glMatrixMode(GL_MODELVIEW); |
---|
| 158 | glPushMatrix(); |
---|
| 159 | |
---|
| 160 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 161 | |
---|
| 162 | glDisable(GL_LIGHTING); |
---|
| 163 | glDisable(GL_TEXTURE_2D); |
---|
| 164 | glDisable(GL_BLEND); |
---|
| 165 | glLineWidth(2.0); |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | Vector mp = this->cannons->getAbsCoor(); |
---|
| 169 | Vector op = this->cannons->getAbsDir().apply( Vector(-1, 0, 0) ); |
---|
| 170 | op *= 100; |
---|
| 171 | op += mp; |
---|
| 172 | |
---|
| 173 | glColor3f(1.0, 0.0, 0.0 ); |
---|
| 174 | glBegin(GL_LINE_STRIP); |
---|
| 175 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 176 | glVertex3f(op.x, op.y, op.z); |
---|
| 177 | glEnd(); |
---|
| 178 | |
---|
| 179 | op = this->myTarget->getAbsCoor() - ( this->cannons->getAbsCoor() ); |
---|
| 180 | op += mp; |
---|
| 181 | |
---|
| 182 | glColor3f(0.0, 1.0, 0.0 ); |
---|
| 183 | glBegin(GL_LINE_STRIP); |
---|
| 184 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 185 | glVertex3f(op.x, op.y, op.z); |
---|
| 186 | glEnd(); |
---|
| 187 | |
---|
| 188 | glPopAttrib(); |
---|
| 189 | glPopMatrix(); |
---|
| 190 | |
---|
[10664] | 191 | } |
---|
[10663] | 192 | |
---|
[10664] | 193 | void AdmTurret::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 194 | { |
---|
| 195 | } |
---|
[10663] | 196 | |
---|
[10664] | 197 | void AdmTurret::activate() |
---|
[10663] | 198 | { |
---|
| 199 | } |
---|
[10664] | 200 | |
---|
| 201 | void AdmTurret::deactivate() |
---|
| 202 | { |
---|
[10663] | 203 | } |
---|
| 204 | |
---|
| 205 | bool AdmTurret::isVisible(const WorldEntity myTarget) |
---|
[10664] | 206 | { |
---|
| 207 | return true; |
---|
[10663] | 208 | } |
---|
| 209 | |
---|
[10664] | 210 | float AdmTurret::aim(const WorldEntity Target) |
---|
| 211 | { |
---|
| 212 | return 0; |
---|
| 213 | } |
---|
[10663] | 214 | |
---|
| 215 | void AdmTurret::fire() |
---|
| 216 | { |
---|
[10664] | 217 | // Projectile* pj = this->getProjectile(); |
---|
| 218 | // if (pj == NULL) return; |
---|
[10663] | 219 | |
---|
[10664] | 220 | //pj->setOwner(this->getOwner()); |
---|
| 221 | //pj->setParent(PNode::getNullParent()); |
---|
| 222 | //pj->setVelocity(this->getAbsDir().apply(Vector( , , )) ); |
---|
| 223 | //pj->setAbsCoor(this->getEmissionPoint()); |
---|
| 224 | //pj->setAbsDir(this->getAbsDir()); |
---|
| 225 | //pj->activate(); |
---|
| 226 | } |
---|
[10677] | 227 | |
---|
| 228 | void AdmTurret::addCannons( const TiXmlElement * root ) |
---|
| 229 | { |
---|
| 230 | this->cannons = new WorldEntity(); |
---|
| 231 | this->cannons->setParent(this); |
---|
| 232 | this->cannons->loadParams( root ); |
---|
| 233 | |
---|
[10679] | 234 | //this->cannons->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 235 | //this->cannons->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[10677] | 236 | |
---|
| 237 | this->cannons->toList( getOMListNumber() ); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | void AdmTurret::addSensor( const TiXmlElement * root ) |
---|
| 241 | { |
---|
| 242 | this->sensor = new WorldEntity(); |
---|
| 243 | this->sensor->setParent(this); |
---|
| 244 | this->sensor->loadParams( root ); |
---|
| 245 | |
---|
[10679] | 246 | //this->sensor->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 247 | //this->sensor->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[10677] | 248 | this->sensor->toList( getOMListNumber() ); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void AdmTurret::moveTowards( Vector targetDir, float dt ) |
---|
| 252 | { |
---|
[10681] | 253 | if ( this->cannons->getParent() != NullParent::getNullParent() ) |
---|
| 254 | this->cannons->setParent( NullParent::getNullParent() ); |
---|
[10677] | 255 | |
---|
[10681] | 256 | targetDir.normalize(); |
---|
[10677] | 257 | |
---|
[10681] | 258 | float dAngle = dt; |
---|
[10677] | 259 | |
---|
[10681] | 260 | float bestResult = -1.0f; |
---|
| 261 | Quaternion bestBodyRot; |
---|
| 262 | Quaternion bestCannonRot; |
---|
| 263 | float bestBodyAngle = 0.0f; |
---|
| 264 | float bestCannonAngle = 0.0f; |
---|
[10677] | 265 | |
---|
[10681] | 266 | // Quaternion baseRot(0, Vector); |
---|
| 267 | // if ( isCeil ) |
---|
| 268 | // { |
---|
| 269 | // printf( "ceil\n" ); |
---|
| 270 | // baseRot = Quaternion( PI/2, Vector( 0, 0, 1 ) ); |
---|
| 271 | // } |
---|
[10677] | 272 | |
---|
[10681] | 273 | for ( int dBodyAngle = -1; dBodyAngle<2; dBodyAngle++ ) |
---|
[10679] | 274 | { |
---|
[10681] | 275 | for ( int dCannonAngle = -1; dCannonAngle<2; dCannonAngle++ ) |
---|
| 276 | { |
---|
| 277 | float bodyAngle = this->bodyAngle + dBodyAngle*dAngle; |
---|
| 278 | float cannonAngle = this->cannonAngle + dCannonAngle*dAngle; |
---|
| 279 | |
---|
| 280 | while ( bodyAngle > 2*PI ) |
---|
| 281 | bodyAngle -= 2*PI; |
---|
| 282 | while ( bodyAngle < 0 ) |
---|
| 283 | bodyAngle += 2*PI; |
---|
| 284 | while ( cannonAngle > 2*PI ) |
---|
| 285 | cannonAngle -= 2*PI; |
---|
| 286 | while ( cannonAngle < 0 ) |
---|
| 287 | cannonAngle += 2*PI; |
---|
| 288 | |
---|
| 289 | Quaternion bodyRot( bodyAngle, Vector( 0, 1, 0 ) ); |
---|
| 290 | Quaternion cannonRot( cannonAngle, Vector( 0, 0, 1) ); |
---|
| 291 | |
---|
| 292 | // bodyRot = baseRot * bodyRot; |
---|
| 293 | // cannonRot = baseRot * cannonRot; |
---|
| 294 | |
---|
| 295 | float result = (bodyRot * cannonRot).apply( Vector( -1, 0, 0 ) ).dot( targetDir ); |
---|
| 296 | |
---|
| 297 | if ( result > bestResult ) |
---|
| 298 | { |
---|
| 299 | bestResult = result; |
---|
| 300 | bestBodyRot = bodyRot; |
---|
| 301 | bestBodyAngle = bodyAngle; |
---|
| 302 | bestCannonRot = cannonRot; |
---|
| 303 | bestCannonAngle = cannonAngle; |
---|
| 304 | } |
---|
| 305 | } |
---|
[10679] | 306 | } |
---|
[10677] | 307 | |
---|
[10681] | 308 | this->bodyAngle = bestBodyAngle; |
---|
| 309 | this->cannonAngle = bestCannonAngle; |
---|
| 310 | |
---|
| 311 | this->setAbsDir( bestBodyRot ); |
---|
| 312 | this->cannons->setAbsDir( bestBodyRot * bestCannonRot ); |
---|
[10677] | 313 | } |
---|