[10406] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
| 15 | co-programmer: |
---|
| 16 | */ |
---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | #include "movement_module.h" |
---|
| 21 | #include "ai_module.h" |
---|
| 22 | #include "ai_team.h" |
---|
| 23 | #include "ai_swarm.h" |
---|
| 24 | #include "ai_engine.h" |
---|
| 25 | |
---|
| 26 | #include "player.h" |
---|
| 27 | #include "playable.h" |
---|
| 28 | |
---|
| 29 | #include "weapons/test_gun.h" |
---|
| 30 | #include "weapons/turret.h" |
---|
| 31 | #include "weapons/cannon.h" |
---|
| 32 | |
---|
| 33 | #include "loading/factory.h" |
---|
| 34 | #include "debug.h" |
---|
| 35 | #include "loading/load_param.h" |
---|
[10440] | 36 | #include "util/loading/load_param_xml.h" |
---|
[10439] | 37 | #include "track/track.h" |
---|
[10406] | 38 | |
---|
[10531] | 39 | #include "particles/dot_emitter.h" |
---|
| 40 | #include "particles/emitter_node.h" |
---|
| 41 | #include "particles/sprite_particles.h" |
---|
[10406] | 42 | |
---|
[10499] | 43 | #include "weapons/test_gun.h" |
---|
| 44 | #include "weapons/light_blaster.h" |
---|
| 45 | #include "weapons/medium_blaster.h" |
---|
| 46 | #include "weapons/heavy_blaster.h" |
---|
| 47 | #include "weapons/swarm_launcher.h" |
---|
| 48 | #include "weapons/spike_launcher.h" |
---|
| 49 | #include "weapons/spike_thrower.h" |
---|
| 50 | #include "weapons/acid_launcher.h" |
---|
| 51 | #include "weapons/boomerang_gun.h" |
---|
| 52 | #include "weapons/turret.h" |
---|
| 53 | #include "weapons/cannon.h" |
---|
| 54 | |
---|
[10406] | 55 | #include "npc.h" |
---|
| 56 | |
---|
| 57 | ObjectListDefinition(NPC); |
---|
| 58 | CREATE_FACTORY(NPC); |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | #include "script_class.h" |
---|
| 62 | CREATE_SCRIPTABLE_CLASS(NPC, |
---|
| 63 | addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
| 64 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
| 65 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
| 66 | ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
| 67 | ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir)) |
---|
[10459] | 68 | ->addMethod("fire", Executor0<NPC, lua_State*>(&NPC::fire)) |
---|
[10498] | 69 | ->addMethod("pause", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::pauseTrack)) |
---|
[10512] | 70 | ->addMethod("setAI", Executor1<NPC, lua_State*, bool>(&NPC::setAI)) |
---|
[10511] | 71 | ->addMethod("setVisibility", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::setVisibility)) |
---|
[10531] | 72 | ->addMethod("destroy", Executor0<NPC, lua_State*/*, WorldEntity**/>(&NPC::destroy)) |
---|
[10406] | 73 | ); |
---|
| 74 | |
---|
| 75 | NPC::NPC(const TiXmlElement* root) |
---|
| 76 | : weaponMan(this) |
---|
| 77 | { |
---|
| 78 | this->registerObject(this, NPC::_objectList); |
---|
| 79 | |
---|
| 80 | this->toList(OM_GROUP_01); |
---|
[10440] | 81 | this->bAIEnabled = false; |
---|
[10406] | 82 | |
---|
[10499] | 83 | |
---|
| 84 | |
---|
| 85 | // create the weapons and their manager |
---|
| 86 | Weapon* wpRight1 = new LightBlaster (); |
---|
| 87 | wpRight1->setName( "LightBlaster"); |
---|
| 88 | Weapon* wpLeft1 = new LightBlaster (); |
---|
| 89 | wpLeft1->setName( "LightBlaster"); |
---|
| 90 | |
---|
| 91 | Weapon* wpRight2 = new MediumBlaster (); |
---|
| 92 | wpRight2->setName( "MediumBlaster"); |
---|
| 93 | Weapon* wpLeft2 = new MediumBlaster (); |
---|
| 94 | wpLeft2->setName( "MediumBlaster"); |
---|
| 95 | |
---|
| 96 | Weapon* wpRight3 = new HeavyBlaster (1); |
---|
| 97 | wpRight3->setName( "HeavyBlaster"); |
---|
| 98 | Weapon* wpLeft3 = new HeavyBlaster (0); |
---|
| 99 | wpLeft3->setName( "HeavyBlaster"); |
---|
| 100 | |
---|
| 101 | Weapon* cannon = new SwarmLauncher(); |
---|
| 102 | cannon->setName( "SwarmLauncher"); |
---|
| 103 | |
---|
| 104 | Weapon* spike = new SpikeThrower(); |
---|
| 105 | spike->setName( "SpikeThrower" ); |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | Weapon* acid0 = new AcidLauncher(); |
---|
| 109 | acid0->setName( "AcidSplasher" ); |
---|
| 110 | |
---|
| 111 | Weapon* acid1 = new AcidLauncher(); |
---|
| 112 | acid1->setName( "AcidSplasher" ); |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | this->weaponMan.addWeapon( wpLeft1, 0, 0); |
---|
| 116 | this->weaponMan.addWeapon( wpRight1, 0, 1); |
---|
| 117 | |
---|
| 118 | this->weaponMan.addWeapon( wpLeft2, 1, 2); |
---|
| 119 | this->weaponMan.addWeapon( wpRight2, 1, 3); |
---|
| 120 | |
---|
| 121 | this->weaponMan.addWeapon( wpLeft3, 2, 4); |
---|
| 122 | this->weaponMan.addWeapon( wpRight3, 2, 5); |
---|
| 123 | /* |
---|
| 124 | this->weaponMan.addWeapon( wpLeft1, 3, 0); |
---|
| 125 | this->weaponMan.addWeapon( wpRight1, 3, 1); |
---|
| 126 | |
---|
| 127 | this->weaponMan.addWeapon( wpLeft2, 3, 2); |
---|
| 128 | this->weaponMan.addWeapon( wpRight2, 3, 3); |
---|
| 129 | |
---|
| 130 | this->weaponMan.addWeapon( wpLeft3, 3, 4); |
---|
| 131 | this->weaponMan.addWeapon( wpRight3, 3, 5); |
---|
| 132 | */ |
---|
| 133 | |
---|
| 134 | this->weaponMan.addWeapon( acid0, 3, 0); |
---|
| 135 | this->weaponMan.addWeapon( acid1, 3, 1); |
---|
| 136 | |
---|
| 137 | |
---|
[10406] | 138 | if( root != NULL) |
---|
[10440] | 139 | this->loadParams(root); |
---|
[10406] | 140 | |
---|
[10444] | 141 | if( this->bAIEnabled && ! this->entityTrack) |
---|
[10440] | 142 | { |
---|
| 143 | std::cout << "Team Number: " << teamNumber << "\n"; |
---|
| 144 | std::cout << "Swarm Number:" << swarmNumber << "\n"; |
---|
[10406] | 145 | |
---|
[10444] | 146 | AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance); |
---|
[10440] | 147 | } |
---|
[10406] | 148 | |
---|
| 149 | this->bFire = false; |
---|
[10444] | 150 | if( this->entityTrack) |
---|
[10448] | 151 | { |
---|
[10444] | 152 | this->setParent(this->entityTrack->getTrackNode()); |
---|
[10448] | 153 | this->setRelCoor(0,0,0); |
---|
| 154 | } |
---|
[10406] | 155 | |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
[10499] | 159 | // this->secWeaponMan.addWeapon( acid0, 2, 2); |
---|
| 160 | // this->secWeaponMan.addWeapon( acid1, 2, 3); |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | // this->weaponMan.changeWeaponConfig(3); |
---|
| 164 | |
---|
| 165 | // this->getWeaponManager().changeWeaponConfig(1); |
---|
| 166 | |
---|
[10406] | 167 | this->setHealthMax(100); |
---|
| 168 | this->setHealth(80); |
---|
| 169 | |
---|
[10499] | 170 | this->getWeaponManager().setSlotCount(7); |
---|
[10406] | 171 | |
---|
[10499] | 172 | this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
| 173 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[10439] | 174 | |
---|
[10499] | 175 | this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
| 176 | this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[10439] | 177 | |
---|
[10499] | 178 | this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 179 | this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
[10440] | 180 | |
---|
[10499] | 181 | this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 182 | this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
| 183 | |
---|
| 184 | this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 185 | this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 186 | |
---|
| 187 | this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 188 | this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 189 | |
---|
| 190 | this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 191 | this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 192 | |
---|
| 193 | this->getWeaponManager().getFixedTarget()->setParent(this); |
---|
| 194 | this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0); |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | |
---|
[10406] | 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | NPC::~NPC () |
---|
| 203 | { |
---|
[10439] | 204 | if(! this->entityTrack) |
---|
[10406] | 205 | AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * loads the xml tags |
---|
| 212 | * @param root: root xml tag for this element |
---|
| 213 | */ |
---|
| 214 | void NPC::loadParams(const TiXmlElement* root) |
---|
| 215 | { |
---|
| 216 | WorldEntity::loadParams(root); |
---|
| 217 | |
---|
[10440] | 218 | LoadParam(root, "enableAI", this, NPC, enableAI) |
---|
| 219 | .describe("enables the AI algorithms"); |
---|
| 220 | |
---|
[10406] | 221 | LoadParam(root, "team", this, NPC, setTeamNumber) |
---|
| 222 | .describe("this sets the team number") |
---|
| 223 | .defaultValues(0); |
---|
| 224 | |
---|
| 225 | LoadParam(root, "swarm", this, NPC, setSwarmNumber) |
---|
| 226 | .describe("this sets the swarm number") |
---|
| 227 | .defaultValues(0); |
---|
| 228 | |
---|
| 229 | LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed) |
---|
| 230 | .describe("this sets the NPC max Speed") |
---|
| 231 | .defaultValues(0); |
---|
| 232 | |
---|
| 233 | LoadParam(root, "attackDistance", this, NPC, setAttackDistance) |
---|
| 234 | .describe("this sets the NPC distance to target") |
---|
| 235 | .defaultValues(0); |
---|
[10440] | 236 | |
---|
[10499] | 237 | LoadParam(root, "weapon-config", this, NPC, setWeaponConfig); |
---|
| 238 | |
---|
| 239 | // LoadParamXML(root, "Weapons", this, NPC, addWeapons) |
---|
| 240 | // .describe("creates and adds weapons"); |
---|
[10406] | 241 | } |
---|
| 242 | |
---|
[10512] | 243 | void NPC::setAI(bool activate) |
---|
| 244 | { |
---|
| 245 | if(activate) AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance); |
---|
| 246 | else AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); |
---|
| 247 | } |
---|
[10406] | 248 | |
---|
[10531] | 249 | void NPC::destroy( /*WorldEntity* killer*/ ) |
---|
| 250 | { |
---|
[10512] | 251 | |
---|
[10531] | 252 | PRINTF(5)("NPC destroy\n"); |
---|
| 253 | |
---|
| 254 | EmitterNode* node = NULL; |
---|
| 255 | DotEmitter* emitter = NULL; |
---|
| 256 | SpriteParticles* explosionParticles = NULL; |
---|
| 257 | |
---|
| 258 | explosionParticles = new SpriteParticles(200); |
---|
| 259 | explosionParticles->setName("SpaceShipExplosionParticles"); |
---|
| 260 | explosionParticles->setLifeSpan(.2, .3); |
---|
| 261 | explosionParticles->setRadius(0.0, 10.0); |
---|
| 262 | explosionParticles->setRadius(.5, 6.0); |
---|
| 263 | explosionParticles->setRadius(1.0, 3.0); |
---|
| 264 | explosionParticles->setColor(0.0, 1,1,1,.9); |
---|
| 265 | explosionParticles->setColor(0.1, 1,1,0,.9); |
---|
| 266 | explosionParticles->setColor(0.5, .8,.4,0,.5); |
---|
| 267 | explosionParticles->setColor(1.0, .2,.2,.2,.5); |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | emitter = new DotEmitter( 2000, 70, 360); |
---|
| 271 | //emitter->setSpread( 0, M_2_PI); |
---|
| 272 | emitter->setEmissionRate( 200.0); |
---|
| 273 | //emitter->setEmissionVelocity( 200.0); |
---|
| 274 | //emitter->setSystem( explosionParticles); |
---|
| 275 | //emitter->setAbsCoor( this->getAbsCoor()); |
---|
| 276 | |
---|
| 277 | node = new EmitterNode( .1f); |
---|
| 278 | node->setupParticle( emitter, explosionParticles); |
---|
| 279 | node->setAbsDir( this->getAbsDir()); |
---|
| 280 | node->setVelocity( this->getVelocity() * .9f); |
---|
| 281 | node->setAbsCoor( this->getAbsCoor()); |
---|
| 282 | if( !node->start()) |
---|
| 283 | PRINTF(0)("Explosion node not correctly started!"); |
---|
| 284 | /* |
---|
| 285 | PNode* node = new PNode(); |
---|
| 286 | node->setAbsCoor(this->getAbsCoor()); |
---|
| 287 | Explosion* explosion = new Explosion(); |
---|
| 288 | explosion->explode( node, Vector(5,5,5)); |
---|
| 289 | */ |
---|
| 290 | /* |
---|
| 291 | if( this->hasPlayer()) |
---|
| 292 | { |
---|
| 293 | this->setAbsCoor(Vector(-10000,10000,10000)); |
---|
| 294 | this->hide(); |
---|
| 295 | } |
---|
| 296 | else |
---|
| 297 | {*/ |
---|
| 298 | //this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0))); |
---|
| 299 | //} |
---|
| 300 | |
---|
| 301 | } |
---|
| 302 | |
---|
[10499] | 303 | void NPC::setWeaponConfig(int i) |
---|
| 304 | { |
---|
| 305 | this->weaponMan.changeWeaponConfig(i); |
---|
| 306 | } |
---|
| 307 | |
---|
[10440] | 308 | void NPC::addWeapons(const TiXmlElement* root) |
---|
| 309 | { |
---|
| 310 | if( root == NULL) |
---|
| 311 | return; |
---|
| 312 | |
---|
| 313 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 314 | { |
---|
| 315 | PRINTF(0)("got weapon: %s\n", element->Value()); |
---|
| 316 | BaseObject* obj = Factory::fabricate(element); |
---|
| 317 | if( obj != NULL && obj->isA( Weapon::staticClassID())) |
---|
| 318 | { |
---|
[10443] | 319 | Weapon* w = dynamic_cast<Weapon*>(obj); |
---|
[10440] | 320 | PRINTF(0)("created a weapon\n"); |
---|
[10443] | 321 | int preferedSlot = w->getPreferedSlot(); |
---|
| 322 | int preferedSide = w->getPreferedSide(); |
---|
| 323 | |
---|
| 324 | this->addWeapon( w, preferedSide, preferedSlot); |
---|
[10440] | 325 | } |
---|
| 326 | } |
---|
| 327 | LOAD_PARAM_END_CYCLE(element); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | |
---|
[10406] | 331 | /** |
---|
| 332 | * @brief adds a Weapon to the NPC. |
---|
| 333 | * @param weapon the Weapon to add. |
---|
| 334 | * @param configID the Configuration ID to add this weapon to. |
---|
| 335 | * @param slotID the slotID to add the Weapon to. |
---|
| 336 | */ |
---|
| 337 | bool NPC::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
| 338 | { |
---|
| 339 | weapon->setOwner(this->getOwner()); |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | if(this->weaponMan.addWeapon(weapon, configID, slotID)) |
---|
| 343 | { |
---|
| 344 | return true; |
---|
| 345 | } |
---|
| 346 | else |
---|
| 347 | { |
---|
| 348 | if (weapon != NULL) |
---|
[10443] | 349 | PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n", |
---|
[10406] | 350 | weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName()); |
---|
| 351 | else |
---|
[10443] | 352 | PRINTF(1)("No weapon defined\n"); |
---|
[10406] | 353 | return false; |
---|
| 354 | |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | * @brief removes a Weapon. |
---|
| 360 | * @param weapon the Weapon to remove. |
---|
| 361 | */ |
---|
| 362 | void NPC::removeWeapon(Weapon* weapon) |
---|
| 363 | { |
---|
| 364 | this->weaponMan.removeWeapon(weapon); |
---|
| 365 | |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | * @brief jumps to the next WeaponConfiguration |
---|
| 370 | */ |
---|
| 371 | void NPC::nextWeaponConfig() |
---|
| 372 | { |
---|
| 373 | this->weaponMan.nextWeaponConfig(); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * @brief moves to the last WeaponConfiguration |
---|
| 378 | */ |
---|
| 379 | void NPC::previousWeaponConfig() |
---|
| 380 | { |
---|
| 381 | this->weaponMan.previousWeaponConfig(); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | * ticking |
---|
| 389 | * @param dt time since last tick |
---|
| 390 | */ |
---|
| 391 | void NPC::tick(float dt) |
---|
| 392 | { |
---|
| 393 | this->weaponMan.tick(dt); |
---|
| 394 | if (this->bFire) |
---|
[10499] | 395 | { |
---|
| 396 | std::cout << "fire..\n"; |
---|
[10406] | 397 | weaponMan.fire(); |
---|
[10499] | 398 | } |
---|
[10406] | 399 | this->bFire = false; |
---|
[10439] | 400 | |
---|
| 401 | if(this->entityTrack) |
---|
| 402 | this->entityTrack->tick(dt); |
---|
| 403 | |
---|
[10406] | 404 | } |
---|
[10447] | 405 | |
---|
| 406 | void NPC::draw() const |
---|
| 407 | { |
---|
[10449] | 408 | if( this->entityTrack != NULL && this->isDrawTrack()) |
---|
| 409 | this->entityTrack->drawGraph(); |
---|
| 410 | |
---|
[10447] | 411 | WorldEntity::draw(); |
---|
[10498] | 412 | } |
---|
[10539] | 413 | |
---|
| 414 | // HACK just to make sure they explode as nice as possible :) |
---|
| 415 | void NPC::hit( float damage, WorldEntity* killer) |
---|
| 416 | { |
---|
| 417 | this->setDamage(killer->getDamage()); |
---|
| 418 | } |
---|