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