[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 | |
---|
[10499] | 40 | #include "weapons/test_gun.h" |
---|
| 41 | #include "weapons/light_blaster.h" |
---|
| 42 | #include "weapons/medium_blaster.h" |
---|
| 43 | #include "weapons/heavy_blaster.h" |
---|
| 44 | #include "weapons/swarm_launcher.h" |
---|
| 45 | #include "weapons/spike_launcher.h" |
---|
| 46 | #include "weapons/spike_thrower.h" |
---|
| 47 | #include "weapons/acid_launcher.h" |
---|
| 48 | #include "weapons/boomerang_gun.h" |
---|
| 49 | #include "weapons/turret.h" |
---|
| 50 | #include "weapons/cannon.h" |
---|
| 51 | |
---|
[10406] | 52 | #include "npc.h" |
---|
| 53 | |
---|
| 54 | ObjectListDefinition(NPC); |
---|
| 55 | CREATE_FACTORY(NPC); |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | #include "script_class.h" |
---|
| 59 | CREATE_SCRIPTABLE_CLASS(NPC, |
---|
| 60 | addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
| 61 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
| 62 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
| 63 | ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
| 64 | ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir)) |
---|
[10459] | 65 | ->addMethod("fire", Executor0<NPC, lua_State*>(&NPC::fire)) |
---|
[10498] | 66 | ->addMethod("pause", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::pauseTrack)) |
---|
[10501] | 67 | ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide)) |
---|
| 68 | ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide)) |
---|
[10406] | 69 | ); |
---|
| 70 | |
---|
| 71 | NPC::NPC(const TiXmlElement* root) |
---|
| 72 | : weaponMan(this) |
---|
| 73 | { |
---|
| 74 | this->registerObject(this, NPC::_objectList); |
---|
| 75 | |
---|
| 76 | this->toList(OM_GROUP_01); |
---|
[10440] | 77 | this->bAIEnabled = false; |
---|
[10406] | 78 | |
---|
[10499] | 79 | |
---|
| 80 | |
---|
| 81 | // create the weapons and their manager |
---|
| 82 | Weapon* wpRight1 = new LightBlaster (); |
---|
| 83 | wpRight1->setName( "LightBlaster"); |
---|
| 84 | Weapon* wpLeft1 = new LightBlaster (); |
---|
| 85 | wpLeft1->setName( "LightBlaster"); |
---|
| 86 | |
---|
| 87 | Weapon* wpRight2 = new MediumBlaster (); |
---|
| 88 | wpRight2->setName( "MediumBlaster"); |
---|
| 89 | Weapon* wpLeft2 = new MediumBlaster (); |
---|
| 90 | wpLeft2->setName( "MediumBlaster"); |
---|
| 91 | |
---|
| 92 | Weapon* wpRight3 = new HeavyBlaster (1); |
---|
| 93 | wpRight3->setName( "HeavyBlaster"); |
---|
| 94 | Weapon* wpLeft3 = new HeavyBlaster (0); |
---|
| 95 | wpLeft3->setName( "HeavyBlaster"); |
---|
| 96 | |
---|
| 97 | Weapon* cannon = new SwarmLauncher(); |
---|
| 98 | cannon->setName( "SwarmLauncher"); |
---|
| 99 | |
---|
| 100 | Weapon* spike = new SpikeThrower(); |
---|
| 101 | spike->setName( "SpikeThrower" ); |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | Weapon* acid0 = new AcidLauncher(); |
---|
| 105 | acid0->setName( "AcidSplasher" ); |
---|
| 106 | |
---|
| 107 | Weapon* acid1 = new AcidLauncher(); |
---|
| 108 | acid1->setName( "AcidSplasher" ); |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | this->weaponMan.addWeapon( wpLeft1, 0, 0); |
---|
| 112 | this->weaponMan.addWeapon( wpRight1, 0, 1); |
---|
| 113 | |
---|
| 114 | this->weaponMan.addWeapon( wpLeft2, 1, 2); |
---|
| 115 | this->weaponMan.addWeapon( wpRight2, 1, 3); |
---|
| 116 | |
---|
| 117 | this->weaponMan.addWeapon( wpLeft3, 2, 4); |
---|
| 118 | this->weaponMan.addWeapon( wpRight3, 2, 5); |
---|
| 119 | /* |
---|
| 120 | this->weaponMan.addWeapon( wpLeft1, 3, 0); |
---|
| 121 | this->weaponMan.addWeapon( wpRight1, 3, 1); |
---|
| 122 | |
---|
| 123 | this->weaponMan.addWeapon( wpLeft2, 3, 2); |
---|
| 124 | this->weaponMan.addWeapon( wpRight2, 3, 3); |
---|
| 125 | |
---|
| 126 | this->weaponMan.addWeapon( wpLeft3, 3, 4); |
---|
| 127 | this->weaponMan.addWeapon( wpRight3, 3, 5); |
---|
| 128 | */ |
---|
| 129 | |
---|
| 130 | this->weaponMan.addWeapon( acid0, 3, 0); |
---|
| 131 | this->weaponMan.addWeapon( acid1, 3, 1); |
---|
| 132 | |
---|
| 133 | |
---|
[10406] | 134 | if( root != NULL) |
---|
[10440] | 135 | this->loadParams(root); |
---|
[10406] | 136 | |
---|
[10444] | 137 | if( this->bAIEnabled && ! this->entityTrack) |
---|
[10440] | 138 | { |
---|
| 139 | std::cout << "Team Number: " << teamNumber << "\n"; |
---|
| 140 | std::cout << "Swarm Number:" << swarmNumber << "\n"; |
---|
[10406] | 141 | |
---|
[10444] | 142 | AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance); |
---|
[10440] | 143 | } |
---|
[10406] | 144 | |
---|
| 145 | this->bFire = false; |
---|
[10444] | 146 | if( this->entityTrack) |
---|
[10448] | 147 | { |
---|
[10444] | 148 | this->setParent(this->entityTrack->getTrackNode()); |
---|
[10448] | 149 | this->setRelCoor(0,0,0); |
---|
| 150 | } |
---|
[10406] | 151 | |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | |
---|
[10499] | 155 | // this->secWeaponMan.addWeapon( acid0, 2, 2); |
---|
| 156 | // this->secWeaponMan.addWeapon( acid1, 2, 3); |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | // this->weaponMan.changeWeaponConfig(3); |
---|
| 160 | |
---|
| 161 | // this->getWeaponManager().changeWeaponConfig(1); |
---|
| 162 | |
---|
[10406] | 163 | this->setHealthMax(100); |
---|
| 164 | this->setHealth(80); |
---|
| 165 | |
---|
[10499] | 166 | this->getWeaponManager().setSlotCount(7); |
---|
[10406] | 167 | |
---|
[10499] | 168 | this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
| 169 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[10439] | 170 | |
---|
[10499] | 171 | this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
| 172 | this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[10439] | 173 | |
---|
[10499] | 174 | this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 175 | this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
[10440] | 176 | |
---|
[10499] | 177 | this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 178 | this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
| 179 | |
---|
| 180 | this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 181 | this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 182 | |
---|
| 183 | this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 184 | this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 185 | |
---|
| 186 | this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 187 | this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 188 | |
---|
| 189 | this->getWeaponManager().getFixedTarget()->setParent(this); |
---|
| 190 | this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | |
---|
[10406] | 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | NPC::~NPC () |
---|
| 199 | { |
---|
[10439] | 200 | if(! this->entityTrack) |
---|
[10406] | 201 | AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * loads the xml tags |
---|
| 208 | * @param root: root xml tag for this element |
---|
| 209 | */ |
---|
| 210 | void NPC::loadParams(const TiXmlElement* root) |
---|
| 211 | { |
---|
| 212 | WorldEntity::loadParams(root); |
---|
| 213 | |
---|
[10440] | 214 | LoadParam(root, "enableAI", this, NPC, enableAI) |
---|
| 215 | .describe("enables the AI algorithms"); |
---|
| 216 | |
---|
[10406] | 217 | LoadParam(root, "team", this, NPC, setTeamNumber) |
---|
| 218 | .describe("this sets the team number") |
---|
| 219 | .defaultValues(0); |
---|
| 220 | |
---|
| 221 | LoadParam(root, "swarm", this, NPC, setSwarmNumber) |
---|
| 222 | .describe("this sets the swarm number") |
---|
| 223 | .defaultValues(0); |
---|
| 224 | |
---|
| 225 | LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed) |
---|
| 226 | .describe("this sets the NPC max Speed") |
---|
| 227 | .defaultValues(0); |
---|
| 228 | |
---|
| 229 | LoadParam(root, "attackDistance", this, NPC, setAttackDistance) |
---|
| 230 | .describe("this sets the NPC distance to target") |
---|
| 231 | .defaultValues(0); |
---|
[10440] | 232 | |
---|
[10499] | 233 | LoadParam(root, "weapon-config", this, NPC, setWeaponConfig); |
---|
| 234 | |
---|
| 235 | // LoadParamXML(root, "Weapons", this, NPC, addWeapons) |
---|
| 236 | // .describe("creates and adds weapons"); |
---|
[10406] | 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
[10499] | 240 | void NPC::setWeaponConfig(int i) |
---|
| 241 | { |
---|
| 242 | this->weaponMan.changeWeaponConfig(i); |
---|
| 243 | } |
---|
| 244 | |
---|
[10440] | 245 | void NPC::addWeapons(const TiXmlElement* root) |
---|
| 246 | { |
---|
| 247 | if( root == NULL) |
---|
| 248 | return; |
---|
| 249 | |
---|
| 250 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 251 | { |
---|
| 252 | PRINTF(0)("got weapon: %s\n", element->Value()); |
---|
| 253 | BaseObject* obj = Factory::fabricate(element); |
---|
| 254 | if( obj != NULL && obj->isA( Weapon::staticClassID())) |
---|
| 255 | { |
---|
[10443] | 256 | Weapon* w = dynamic_cast<Weapon*>(obj); |
---|
[10440] | 257 | PRINTF(0)("created a weapon\n"); |
---|
[10443] | 258 | int preferedSlot = w->getPreferedSlot(); |
---|
| 259 | int preferedSide = w->getPreferedSide(); |
---|
| 260 | |
---|
| 261 | this->addWeapon( w, preferedSide, preferedSlot); |
---|
[10440] | 262 | } |
---|
| 263 | } |
---|
| 264 | LOAD_PARAM_END_CYCLE(element); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
[10406] | 268 | /** |
---|
| 269 | * @brief adds a Weapon to the NPC. |
---|
| 270 | * @param weapon the Weapon to add. |
---|
| 271 | * @param configID the Configuration ID to add this weapon to. |
---|
| 272 | * @param slotID the slotID to add the Weapon to. |
---|
| 273 | */ |
---|
| 274 | bool NPC::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
| 275 | { |
---|
| 276 | weapon->setOwner(this->getOwner()); |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | if(this->weaponMan.addWeapon(weapon, configID, slotID)) |
---|
| 280 | { |
---|
| 281 | return true; |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | if (weapon != NULL) |
---|
[10443] | 286 | PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n", |
---|
[10406] | 287 | weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName()); |
---|
| 288 | else |
---|
[10443] | 289 | PRINTF(1)("No weapon defined\n"); |
---|
[10406] | 290 | return false; |
---|
| 291 | |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | * @brief removes a Weapon. |
---|
| 297 | * @param weapon the Weapon to remove. |
---|
| 298 | */ |
---|
| 299 | void NPC::removeWeapon(Weapon* weapon) |
---|
| 300 | { |
---|
| 301 | this->weaponMan.removeWeapon(weapon); |
---|
| 302 | |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /** |
---|
| 306 | * @brief jumps to the next WeaponConfiguration |
---|
| 307 | */ |
---|
| 308 | void NPC::nextWeaponConfig() |
---|
| 309 | { |
---|
| 310 | this->weaponMan.nextWeaponConfig(); |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * @brief moves to the last WeaponConfiguration |
---|
| 315 | */ |
---|
| 316 | void NPC::previousWeaponConfig() |
---|
| 317 | { |
---|
| 318 | this->weaponMan.previousWeaponConfig(); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | * ticking |
---|
| 326 | * @param dt time since last tick |
---|
| 327 | */ |
---|
| 328 | void NPC::tick(float dt) |
---|
| 329 | { |
---|
| 330 | this->weaponMan.tick(dt); |
---|
| 331 | if (this->bFire) |
---|
[10499] | 332 | { |
---|
| 333 | std::cout << "fire..\n"; |
---|
[10406] | 334 | weaponMan.fire(); |
---|
[10499] | 335 | } |
---|
[10406] | 336 | this->bFire = false; |
---|
[10439] | 337 | |
---|
| 338 | if(this->entityTrack) |
---|
| 339 | this->entityTrack->tick(dt); |
---|
| 340 | |
---|
[10406] | 341 | } |
---|
[10447] | 342 | |
---|
| 343 | void NPC::draw() const |
---|
| 344 | { |
---|
[10449] | 345 | if( this->entityTrack != NULL && this->isDrawTrack()) |
---|
| 346 | this->entityTrack->drawGraph(); |
---|
| 347 | |
---|
[10447] | 348 | WorldEntity::draw(); |
---|
[10498] | 349 | } |
---|