| [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 |  | 
|---|
 | 39 |  | 
|---|
 | 40 | #include "npc.h" | 
|---|
 | 41 |  | 
|---|
 | 42 | ObjectListDefinition(NPC); | 
|---|
 | 43 | CREATE_FACTORY(NPC); | 
|---|
 | 44 |  | 
|---|
 | 45 |  | 
|---|
 | 46 | #include "script_class.h" | 
|---|
 | 47 | CREATE_SCRIPTABLE_CLASS(NPC, | 
|---|
 | 48 |                         addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) | 
|---|
 | 49 |                         ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) | 
|---|
 | 50 |                         ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) | 
|---|
 | 51 |                         ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) | 
|---|
 | 52 |                         ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir)) | 
|---|
| [10459] | 53 |                         ->addMethod("fire", Executor0<NPC, lua_State*>(&NPC::fire)) | 
|---|
 | 54 |  | 
|---|
| [10406] | 55 |                        ); | 
|---|
 | 56 |  | 
|---|
 | 57 | NPC::NPC(const TiXmlElement* root) | 
|---|
 | 58 |   : weaponMan(this) | 
|---|
 | 59 | { | 
|---|
 | 60 |   this->registerObject(this, NPC::_objectList); | 
|---|
 | 61 |  | 
|---|
 | 62 |   this->toList(OM_GROUP_01); | 
|---|
| [10440] | 63 |   this->bAIEnabled = false; | 
|---|
| [10406] | 64 |  | 
|---|
 | 65 |   if( root != NULL) | 
|---|
| [10440] | 66 |     this->loadParams(root); | 
|---|
| [10406] | 67 |  | 
|---|
| [10444] | 68 |   if( this->bAIEnabled && ! this->entityTrack) | 
|---|
| [10440] | 69 |   { | 
|---|
 | 70 |     std::cout << "Team Number: " << teamNumber << "\n"; | 
|---|
 | 71 |     std::cout << "Swarm Number:" << swarmNumber << "\n"; | 
|---|
| [10406] | 72 |  | 
|---|
| [10444] | 73 |     AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance); | 
|---|
| [10440] | 74 |   } | 
|---|
| [10406] | 75 |  | 
|---|
 | 76 |   this->bFire = false; | 
|---|
| [10444] | 77 |   if( this->entityTrack) | 
|---|
| [10448] | 78 |   { | 
|---|
| [10444] | 79 |       this->setParent(this->entityTrack->getTrackNode()); | 
|---|
| [10448] | 80 |       this->setRelCoor(0,0,0); | 
|---|
 | 81 |   } | 
|---|
| [10406] | 82 |  | 
|---|
 | 83 |     // create the weapons and their manager | 
|---|
 | 84 |  | 
|---|
 | 85 |  | 
|---|
 | 86 |   this->getWeaponManager().changeWeaponConfig(1); | 
|---|
 | 87 | //   Weapon* wpRight = new TestGun(0); | 
|---|
 | 88 | //   wpRight->setName("testGun Right"); | 
|---|
 | 89 | //   Weapon* wpLeft = new TestGun(1); | 
|---|
 | 90 | //   wpLeft->setName("testGun Left"); | 
|---|
 | 91 | // | 
|---|
 | 92 | //   wpRight->toList( this->getOMListNumber()); | 
|---|
 | 93 | //   wpLeft->toList( this->getOMListNumber()); | 
|---|
 | 94 | // | 
|---|
 | 95 | // | 
|---|
 | 96 | //   this->addWeapon(wpLeft, 1, 0); | 
|---|
 | 97 | //   this->addWeapon(wpRight,1 ,1); | 
|---|
 | 98 | // | 
|---|
 | 99 | //   wpLeft->increaseEnergy( 100); | 
|---|
 | 100 | //   wpRight->increaseEnergy( 100); | 
|---|
 | 101 |  | 
|---|
 | 102 |   this->setHealthMax(100); | 
|---|
 | 103 |   this->setHealth(80); | 
|---|
 | 104 |  | 
|---|
 | 105 | //   this->getWeaponManager().setSlotCount(7); | 
|---|
 | 106 | // | 
|---|
 | 107 | //   this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0)); | 
|---|
 | 108 | //   this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); | 
|---|
 | 109 | // | 
|---|
 | 110 | //   this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0)); | 
|---|
 | 111 | //   this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); | 
|---|
 | 112 | // | 
|---|
 | 113 | //   this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5)); | 
|---|
 | 114 | //   this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); | 
|---|
 | 115 | // | 
|---|
 | 116 | //   this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5)); | 
|---|
 | 117 | //   this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); | 
|---|
 | 118 | // | 
|---|
 | 119 | //   this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5)); | 
|---|
 | 120 | //   this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); | 
|---|
 | 121 | // | 
|---|
 | 122 | //   this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5)); | 
|---|
 | 123 | //   this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); | 
|---|
 | 124 | // | 
|---|
 | 125 | //   this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0)); | 
|---|
 | 126 | //   this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); | 
|---|
 | 127 | // | 
|---|
 | 128 | //   this->getWeaponManager().getFixedTarget()->setParent(this); | 
|---|
 | 129 | //   this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0); | 
|---|
 | 130 |  | 
|---|
| [10439] | 131 |  | 
|---|
 | 132 |  | 
|---|
| [10440] | 133 |  | 
|---|
| [10406] | 134 | } | 
|---|
 | 135 |  | 
|---|
 | 136 |  | 
|---|
 | 137 | NPC::~NPC () | 
|---|
 | 138 | { | 
|---|
| [10439] | 139 |  if(! this->entityTrack) | 
|---|
| [10406] | 140 |   AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); | 
|---|
 | 141 | } | 
|---|
 | 142 |  | 
|---|
 | 143 |  | 
|---|
 | 144 |  | 
|---|
 | 145 | /** | 
|---|
 | 146 |  * loads the xml tags | 
|---|
 | 147 |  * @param root: root xml tag for this element | 
|---|
 | 148 |  */ | 
|---|
 | 149 | void NPC::loadParams(const TiXmlElement* root) | 
|---|
 | 150 | { | 
|---|
 | 151 |    WorldEntity::loadParams(root); | 
|---|
 | 152 |  | 
|---|
| [10440] | 153 |   LoadParam(root, "enableAI", this, NPC, enableAI) | 
|---|
 | 154 |       .describe("enables the AI algorithms"); | 
|---|
 | 155 |  | 
|---|
| [10406] | 156 |   LoadParam(root, "team", this, NPC, setTeamNumber) | 
|---|
 | 157 |   .describe("this sets the team number") | 
|---|
 | 158 |   .defaultValues(0); | 
|---|
 | 159 |  | 
|---|
 | 160 |   LoadParam(root, "swarm", this, NPC, setSwarmNumber) | 
|---|
 | 161 |   .describe("this sets the swarm number") | 
|---|
 | 162 |   .defaultValues(0); | 
|---|
 | 163 |  | 
|---|
 | 164 |   LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed) | 
|---|
 | 165 |   .describe("this sets the NPC max Speed") | 
|---|
 | 166 |   .defaultValues(0); | 
|---|
 | 167 |  | 
|---|
 | 168 |   LoadParam(root, "attackDistance", this, NPC, setAttackDistance) | 
|---|
 | 169 |   .describe("this sets the NPC distance to target") | 
|---|
 | 170 |   .defaultValues(0); | 
|---|
| [10440] | 171 |  | 
|---|
 | 172 |   LoadParamXML(root, "Weapons", this, NPC, addWeapons) | 
|---|
 | 173 |   .describe("creates and adds weapons"); | 
|---|
| [10406] | 174 | } | 
|---|
 | 175 |  | 
|---|
 | 176 |  | 
|---|
| [10440] | 177 | void NPC::addWeapons(const TiXmlElement* root) | 
|---|
 | 178 | { | 
|---|
 | 179 |   if( root == NULL) | 
|---|
 | 180 |     return; | 
|---|
 | 181 |  | 
|---|
 | 182 |   LOAD_PARAM_START_CYCLE(root, element); | 
|---|
 | 183 |   { | 
|---|
 | 184 |     PRINTF(0)("got weapon: %s\n", element->Value()); | 
|---|
 | 185 |     BaseObject* obj = Factory::fabricate(element); | 
|---|
 | 186 |     if( obj != NULL && obj->isA( Weapon::staticClassID())) | 
|---|
 | 187 |     { | 
|---|
| [10443] | 188 |       Weapon* w = dynamic_cast<Weapon*>(obj); | 
|---|
| [10440] | 189 |       PRINTF(0)("created a weapon\n"); | 
|---|
| [10443] | 190 |       int preferedSlot = w->getPreferedSlot(); | 
|---|
 | 191 |       int preferedSide = w->getPreferedSide(); | 
|---|
 | 192 |  | 
|---|
 | 193 |       this->addWeapon( w, preferedSide, preferedSlot); | 
|---|
| [10440] | 194 |     } | 
|---|
 | 195 |   } | 
|---|
 | 196 |   LOAD_PARAM_END_CYCLE(element); | 
|---|
 | 197 | } | 
|---|
 | 198 |  | 
|---|
 | 199 |  | 
|---|
| [10406] | 200 | /** | 
|---|
 | 201 |  * @brief adds a Weapon to the NPC. | 
|---|
 | 202 |  * @param weapon the Weapon to add. | 
|---|
 | 203 |  * @param configID the Configuration ID to add this weapon to. | 
|---|
 | 204 |  * @param slotID the slotID to add the Weapon to. | 
|---|
 | 205 |  */ | 
|---|
 | 206 | bool NPC::addWeapon(Weapon* weapon, int configID, int slotID) | 
|---|
 | 207 | { | 
|---|
 | 208 |   weapon->setOwner(this->getOwner()); | 
|---|
 | 209 |  | 
|---|
 | 210 |  | 
|---|
 | 211 |   if(this->weaponMan.addWeapon(weapon, configID, slotID)) | 
|---|
 | 212 |   { | 
|---|
 | 213 |     return true; | 
|---|
 | 214 |   } | 
|---|
 | 215 |   else | 
|---|
 | 216 |   { | 
|---|
 | 217 |     if (weapon != NULL) | 
|---|
| [10443] | 218 |       PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n", | 
|---|
| [10406] | 219 |                 weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName()); | 
|---|
 | 220 |     else | 
|---|
| [10443] | 221 |       PRINTF(1)("No weapon defined\n"); | 
|---|
| [10406] | 222 |     return false; | 
|---|
 | 223 |  | 
|---|
 | 224 |   } | 
|---|
 | 225 | } | 
|---|
 | 226 |  | 
|---|
 | 227 | /** | 
|---|
 | 228 |  * @brief removes a Weapon. | 
|---|
 | 229 |  * @param weapon the Weapon to remove. | 
|---|
 | 230 |  */ | 
|---|
 | 231 | void NPC::removeWeapon(Weapon* weapon) | 
|---|
 | 232 | { | 
|---|
 | 233 |   this->weaponMan.removeWeapon(weapon); | 
|---|
 | 234 |  | 
|---|
 | 235 | } | 
|---|
 | 236 |  | 
|---|
 | 237 | /** | 
|---|
 | 238 |  * @brief jumps to the next WeaponConfiguration | 
|---|
 | 239 |  */ | 
|---|
 | 240 | void NPC::nextWeaponConfig() | 
|---|
 | 241 | { | 
|---|
 | 242 |   this->weaponMan.nextWeaponConfig(); | 
|---|
 | 243 | } | 
|---|
 | 244 |  | 
|---|
 | 245 | /** | 
|---|
 | 246 |  * @brief moves to the last WeaponConfiguration | 
|---|
 | 247 |  */ | 
|---|
 | 248 | void NPC::previousWeaponConfig() | 
|---|
 | 249 | { | 
|---|
 | 250 |   this->weaponMan.previousWeaponConfig(); | 
|---|
 | 251 | } | 
|---|
 | 252 |  | 
|---|
 | 253 |  | 
|---|
 | 254 |  | 
|---|
 | 255 |  | 
|---|
 | 256 | /** | 
|---|
 | 257 |  * ticking | 
|---|
 | 258 |  * @param dt  time since last tick | 
|---|
 | 259 |  */ | 
|---|
 | 260 | void NPC::tick(float dt) | 
|---|
 | 261 | { | 
|---|
 | 262 |         //std::cout << "fire..\n"; | 
|---|
 | 263 |   this->weaponMan.tick(dt); | 
|---|
 | 264 |   if (this->bFire) | 
|---|
 | 265 |     weaponMan.fire(); | 
|---|
 | 266 |   this->bFire = false; | 
|---|
| [10439] | 267 |  | 
|---|
 | 268 |  if(this->entityTrack) | 
|---|
 | 269 |     this->entityTrack->tick(dt); | 
|---|
 | 270 |  | 
|---|
| [10406] | 271 | } | 
|---|
| [10447] | 272 |  | 
|---|
 | 273 | void NPC::draw() const | 
|---|
 | 274 | { | 
|---|
| [10449] | 275 |  if( this->entityTrack != NULL && this->isDrawTrack()) | 
|---|
 | 276 |   this->entityTrack->drawGraph(); | 
|---|
 | 277 |  | 
|---|
| [10447] | 278 |  WorldEntity::draw(); | 
|---|
 | 279 | } | 
|---|