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