Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/npcs/npc.cc @ 10569

Last change on this file since 10569 was 10552, checked in by patrick, 18 years ago

trail removed

File size: 12.5 KB
RevLine 
[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
[10540]55#include "mount_point.h"
56#include "weapons/weapon_slot.h"
57
[10406]58#include "npc.h"
59
60ObjectListDefinition(NPC);
61CREATE_FACTORY(NPC);
62
63
64#include "script_class.h"
65CREATE_SCRIPTABLE_CLASS(NPC,
66                        addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
67                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
68                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
69                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
70                        ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir))
[10459]71                        ->addMethod("fire", Executor0<NPC, lua_State*>(&NPC::fire))
[10498]72                        ->addMethod("pause", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::pauseTrack))
[10512]73                        ->addMethod("setAI", Executor1<NPC, lua_State*, bool>(&NPC::setAI))
[10511]74                        ->addMethod("setVisibility", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::setVisibility))
[10545]75                        ->addMethod("destroy", Executor0<NPC, lua_State*/*, WorldEntity**/>(&NPC::destroyThis))
[10406]76                       );
77
78NPC::NPC(const TiXmlElement* root)
79  : weaponMan(this)
80{
81  this->registerObject(this, NPC::_objectList);
82
[10552]83  this->toList(OM_GROUP_00);
[10440]84  this->bAIEnabled = false;
[10406]85
[10499]86
87
88   // create the weapons and their manager
89  Weapon* wpRight1 = new LightBlaster ();
90  wpRight1->setName( "LightBlaster");
91  Weapon* wpLeft1 = new LightBlaster ();
92  wpLeft1->setName( "LightBlaster");
93
94  Weapon* wpRight2 = new MediumBlaster ();
95  wpRight2->setName( "MediumBlaster");
96  Weapon* wpLeft2 = new MediumBlaster ();
97  wpLeft2->setName( "MediumBlaster");
98
99  Weapon* wpRight3 = new HeavyBlaster (1);
100  wpRight3->setName( "HeavyBlaster");
101  Weapon* wpLeft3 = new HeavyBlaster (0);
102  wpLeft3->setName( "HeavyBlaster");
103
104  Weapon* cannon = new SwarmLauncher();
105  cannon->setName( "SwarmLauncher");
106
107  Weapon* spike = new SpikeThrower();
108  spike->setName( "SpikeThrower" );
109
110
111  Weapon* acid0 = new AcidLauncher();
112  acid0->setName( "AcidSplasher" );
113
114  Weapon* acid1 = new AcidLauncher();
115  acid1->setName( "AcidSplasher" );
116
117
118  this->weaponMan.addWeapon( wpLeft1, 0, 0);
119  this->weaponMan.addWeapon( wpRight1, 0, 1);
120
121  this->weaponMan.addWeapon( wpLeft2, 1, 2);
122  this->weaponMan.addWeapon( wpRight2, 1, 3);
123
124  this->weaponMan.addWeapon( wpLeft3, 2, 4);
125  this->weaponMan.addWeapon( wpRight3, 2, 5);
126/*
127  this->weaponMan.addWeapon( wpLeft1, 3, 0);
128  this->weaponMan.addWeapon( wpRight1, 3, 1);
129
130  this->weaponMan.addWeapon( wpLeft2, 3, 2);
131  this->weaponMan.addWeapon( wpRight2, 3, 3);
132
133  this->weaponMan.addWeapon( wpLeft3, 3, 4);
134  this->weaponMan.addWeapon( wpRight3, 3, 5);
135*/
136
137  this->weaponMan.addWeapon( acid0, 3, 0);
138  this->weaponMan.addWeapon( acid1, 3, 1);
139
140
[10406]141  if( root != NULL)
[10440]142    this->loadParams(root);
[10406]143
[10444]144  if( this->bAIEnabled && ! this->entityTrack)
[10440]145  {
146    std::cout << "Team Number: " << teamNumber << "\n";
147    std::cout << "Swarm Number:" << swarmNumber << "\n";
[10406]148
[10444]149    AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance);
[10440]150  }
[10406]151
152  this->bFire = false;
[10444]153  if( this->entityTrack)
[10448]154  {
[10444]155      this->setParent(this->entityTrack->getTrackNode());
[10448]156      this->setRelCoor(0,0,0);
157  }
[10406]158
159
160
161
[10499]162//   this->secWeaponMan.addWeapon( acid0, 2, 2);
163//   this->secWeaponMan.addWeapon( acid1, 2, 3);
164
165
166//   this->weaponMan.changeWeaponConfig(3);
167
168//   this->getWeaponManager().changeWeaponConfig(1);
169
[10406]170  this->setHealthMax(100);
171  this->setHealth(80);
172
[10499]173  this->getWeaponManager().setSlotCount(7);
[10406]174
[10499]175  this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0));
176  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[10439]177
[10499]178  this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0));
179  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[10439]180
[10499]181  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
182  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[10440]183
[10499]184  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
185  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
186
187  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
188  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
189
190  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
191  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
192
193  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
194  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
195
[10542]196
197
[10499]198  this->getWeaponManager().getFixedTarget()->setParent(this);
199  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
[10546]200  this->bInit = false;
[10406]201}
202
203
204NPC::~NPC ()
205{
[10439]206 if(! this->entityTrack)
[10406]207  AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
208}
209
210
211
212/**
213 * loads the xml tags
214 * @param root: root xml tag for this element
215 */
216void NPC::loadParams(const TiXmlElement* root)
217{
218   WorldEntity::loadParams(root);
219
[10440]220  LoadParam(root, "enableAI", this, NPC, enableAI)
221      .describe("enables the AI algorithms");
222
[10406]223  LoadParam(root, "team", this, NPC, setTeamNumber)
224  .describe("this sets the team number")
225  .defaultValues(0);
226
227  LoadParam(root, "swarm", this, NPC, setSwarmNumber)
228  .describe("this sets the swarm number")
229  .defaultValues(0);
230
231  LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed)
232  .describe("this sets the NPC max Speed")
233  .defaultValues(0);
234
235  LoadParam(root, "attackDistance", this, NPC, setAttackDistance)
236  .describe("this sets the NPC distance to target")
237  .defaultValues(0);
[10440]238
[10499]239  LoadParam(root, "weapon-config", this, NPC, setWeaponConfig);
240
241//   LoadParamXML(root, "Weapons", this, NPC, addWeapons)
242//   .describe("creates and adds weapons");
[10406]243}
244
[10512]245void NPC::setAI(bool activate)
246{
247     if(activate) AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance);
248     else AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
249}
[10406]250
[10545]251void NPC::destroy( WorldEntity* killer )
[10531]252{
[10512]253
[10531]254  PRINTF(5)("NPC destroy\n");
255
256  EmitterNode* node  = NULL;
257  DotEmitter* emitter = NULL;
258  SpriteParticles*  explosionParticles  = NULL;
259
260  explosionParticles = new SpriteParticles(200);
261  explosionParticles->setName("SpaceShipExplosionParticles");
262  explosionParticles->setLifeSpan(.2, .3);
263  explosionParticles->setRadius(0.0, 10.0);
264  explosionParticles->setRadius(.5, 6.0);
265  explosionParticles->setRadius(1.0, 3.0);
266  explosionParticles->setColor(0.0, 1,1,1,.9);
267  explosionParticles->setColor(0.1,  1,1,0,.9);
268  explosionParticles->setColor(0.5, .8,.4,0,.5);
269  explosionParticles->setColor(1.0, .2,.2,.2,.5);
270
271
272  emitter = new DotEmitter( 2000, 70, 360);
273  //emitter->setSpread( 0, M_2_PI);
274  emitter->setEmissionRate( 200.0);
275  //emitter->setEmissionVelocity( 200.0);
276  //emitter->setSystem( explosionParticles);
277  //emitter->setAbsCoor( this->getAbsCoor());
278
279  node  = new EmitterNode( .1f);
280  node->setupParticle( emitter, explosionParticles);
281  node->setAbsDir( this->getAbsDir());
282  node->setVelocity( this->getVelocity() * .9f);
283  node->setAbsCoor( this->getAbsCoor());
284  if( !node->start())
285    PRINTF(0)("Explosion node not correctly started!");
286/*
287  PNode* node          = new PNode();
288  node->setAbsCoor(this->getAbsCoor());
289  Explosion* explosion = new Explosion();
290  explosion->explode( node, Vector(5,5,5));
291*/
292/*
293  if( this->hasPlayer())
294  {
295        this->setAbsCoor(Vector(-10000,10000,10000));
296        this->hide();
297  }
298  else
299  {*/
300    //this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
301  //}
302
[10545]303  this->toList(OM_NULL);
304  this->setAI( false);
305  this->setAbsCoor(-10000,-10000,-10000);
306
[10549]307
[10531]308}
309
[10499]310void NPC::setWeaponConfig(int i)
311{
312  this->weaponMan.changeWeaponConfig(i);
313}
314
[10440]315void NPC::addWeapons(const TiXmlElement* root)
316{
317  if( root == NULL)
318    return;
319
[10550]320//   LOAD_PARAM_START_CYCLE(root, element);
321//   {
322// //     PRINTF(0)("got weapon: %s\n", element->Value());
323//     BaseObject* obj = Factory::fabricate(element);
324//     if( obj != NULL && obj->isA( Weapon::staticClassID()))
325//     {
326//       Weapon* w = dynamic_cast<Weapon*>(obj);
327// //       PRINTF(0)("created a weapon\n");
328//       int preferedSlot = w->getPreferedSlot();
329//       int preferedSide = w->getPreferedSide();
330//
331//       this->addWeapon( w, preferedSide, preferedSlot);
332//     }
333//   }
334//   LOAD_PARAM_END_CYCLE(element);
[10440]335}
336
337
[10406]338/**
339 * @brief adds a Weapon to the NPC.
340 * @param weapon the Weapon to add.
341 * @param configID the Configuration ID to add this weapon to.
342 * @param slotID the slotID to add the Weapon to.
343 */
344bool NPC::addWeapon(Weapon* weapon, int configID, int slotID)
345{
346  weapon->setOwner(this->getOwner());
347
348
349  if(this->weaponMan.addWeapon(weapon, configID, slotID))
350  {
351    return true;
352  }
353  else
354  {
355    if (weapon != NULL)
[10443]356      PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n",
[10406]357                weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName());
358    else
[10443]359      PRINTF(1)("No weapon defined\n");
[10406]360    return false;
361
362  }
363}
364
365/**
366 * @brief removes a Weapon.
367 * @param weapon the Weapon to remove.
368 */
369void NPC::removeWeapon(Weapon* weapon)
370{
371  this->weaponMan.removeWeapon(weapon);
372
373}
374
375/**
376 * @brief jumps to the next WeaponConfiguration
377 */
378void NPC::nextWeaponConfig()
379{
380  this->weaponMan.nextWeaponConfig();
381}
382
383/**
384 * @brief moves to the last WeaponConfiguration
385 */
386void NPC::previousWeaponConfig()
387{
388  this->weaponMan.previousWeaponConfig();
389}
390
391
392
393
394/**
395 * ticking
396 * @param dt  time since last tick
397 */
398void NPC::tick(float dt)
399{
[10546]400  if( !this->bInit)
401  {
402    // now get slots from the mount points
403    std::map<int, MountPoint*>::iterator it = this->mountPointMap.begin();
404    for( ;it != this->mountPointMap.end(); it++)
405    {
406      WeaponSlot* ws = dynamic_cast<WeaponSlot*>((*it).second->getMount());
407      if( ws != NULL && ws->isA(WeaponSlot::staticClassID()))
408      {
409        int slot = ws->getWeaponSlot();
410        int side = ws->getWeaponSide();
[10549]411        this->getWeaponManager().setSlotPosition(slot, (*it).second->getCenter());
[10546]412        this->getWeaponManager().setSlotDirection(slot, ws->getRelDir());
[10550]413//         PRINTF(0)("setting slot %i\n", slot);
414//         (*it).second->getCenter().debug();
[10546]415      }
416    }
417  this->bInit = true;
418  }
419
420
421
[10406]422  this->weaponMan.tick(dt);
423  if (this->bFire)
[10499]424  {
[10406]425    weaponMan.fire();
[10499]426  }
[10406]427  this->bFire = false;
[10439]428
429 if(this->entityTrack)
430    this->entityTrack->tick(dt);
431
[10406]432}
[10447]433
434void NPC::draw() const
435{
[10449]436 if( this->entityTrack != NULL && this->isDrawTrack())
437  this->entityTrack->drawGraph();
438
[10447]439 WorldEntity::draw();
[10498]440}
[10539]441
442// HACK just to make sure they explode as nice as possible :)
443void NPC::hit( float damage, WorldEntity* killer)
444{
[10545]445  this->destroy( killer);
[10539]446  this->setDamage(killer->getDamage());
[10545]447}
Note: See TracBrowser for help on using the repository browser.