Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/npcs/adm_turret.cc @ 10708

Last change on this file since 10708 was 10708, checked in by rennerc, 17 years ago

improved damage handling for adm

File size: 9.4 KB
Line 
1/*
2* orxonox - the future of 3D-vertical-scrollers
3*
4* Copyright (C) 2004 orx
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation; either version 2, or (at your option)
9* any later version.
10*
11*     ### File Specific:
12*     main-programmer: Reto Luechinger
13*/
14
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17#include "adm_turret.h"
18#include "weapons/weapon_manager.h"
19#include "weapons/weapon.h"
20#include "lib/util/loading/factory.h"
21#include "world_entities/projectiles/projectile.h"
22#include "loading/load_param.h"
23#include "debug.h"
24#include "loading/load_param_xml.h"
25
26#include "environments/bsp_entity.h"
27#include "effects/explosion.h"
28
29ObjectListDefinition(AdmTurret);
30CREATE_FACTORY(AdmTurret);
31
32/**
33*  Standard constructor
34*/
35AdmTurret::AdmTurret ()
36{
37        this->init();
38}
39
40/**
41* destructs the turret, deletes allocated memory
42*/
43AdmTurret::~AdmTurret ()
44{
45          // will be deleted
46}
47
48/**
49* Constructor with XML Element
50*/
51AdmTurret::AdmTurret (const  TiXmlElement* root)
52{
53        this->init();
54        if (root != NULL)
55        {
56                this->loadParams(root);
57        }
58}
59/**
60* XML Loader
61*/
62void AdmTurret::loadParams(const TiXmlElement* root)
63{
64        if (root != NULL)
65        {
66                WorldEntity::loadParams(root);
67
68                LoadParam(root, "target", this, AdmTurret, setTarget)
69                        .describe("The filename of the World Entity, that is to be shot at")
70                        .defaultValues("");
71    LoadParam(root, "type", this, AdmTurret, setType)
72      .describe("floor|ceil"); 
73                LoadParamXML(root, "Cannons", this, AdmTurret, addCannons)
74                        .describe("add cannons to ADM");
75                LoadParamXML(root, "Sensor", this, AdmTurret, addSensor)
76                        .describe("add sensor to ADM");
77                LoadParamXML(root, "Weapon", this, AdmTurret, addWeapon)
78                        .describe("add weapon to ADM");
79        }
80//         this->removeNodeFlags( PNODE_ALL );
81        this->addNodeFlags( PNODE_ROTATE_AND_MOVE );
82        this->addNodeFlags( PNODE_REPARENT_KEEP_POSITION );
83       
84        //HACK this is needed to get the correct coordinates instead (0, 0, 0)
85        this->updateNode( 0 );
86       
87        if ( this->isCeil )
88        {
89          Vector a = this->sensor->getRelCoor();
90          this->sensor->setRelCoor( -a.x, -a.y, -a.z );
91          a = this->cannons->getRelCoor();
92          this->cannons->setRelCoor( -a.x, -a.y, -a.z );
93        }
94}
95
96/**
97* Sets Target onto the World Entity with filename "target", given as String
98*/
99void AdmTurret::setTarget(const std::string& target)
100{
101        WorldEntity* targetEntity = WorldEntity::objectList().getObject(target);
102        if (targetEntity != NULL) 
103        {
104                this->myTarget = targetEntity;
105        }
106        else
107        {
108                PRINTF(1)("ERROR ADMTURRET : Target %s does not exist\n", target.c_str());
109        }
110}
111
112void AdmTurret::setType( const std::string& type )
113{
114  if ( type == "floor" )
115  {
116    this->isCeil = false;
117    return;
118  }
119 
120  if ( type == "ceil" )
121  {
122    this->isCeil = true;
123    return;
124  }
125 
126  //invalid argument
127  PRINTF(1)("%s is not a valid type for AdmTurret\n", type.c_str());
128  assert("false");
129}
130
131void AdmTurret::init()
132{
133        this->registerObject(this, AdmTurret::_objectList);
134        this->setHealthMax(100);
135        this->setHealth(100);
136        this->bodyAngle = this->cannonAngle = 0.0f;
137        this->isActive = true;
138        this->range = 400;
139        this->isCeil = false;
140        this->playerVisible = false;
141        this->weapon = NULL;
142
143       
144}
145
146void AdmTurret::tick(float dt)
147{
148  WorldEntity::tick(dt);
149  if (this->weapon) this->weapon->fire(false);
150  if ( this->getHealth() <= 0 )
151  {
152    this->moveTowards( Vector(0, -1, 0), dt*2 );
153  }
154  else
155  {
156 
157   
158    this->updatePlayerVisible();
159 
160    //rotate sensor 2PI/sec
161    this->sensor->setAbsDir( this->sensor->getAbsDir() * Quaternion( PI*2*dt, Vector( 0, -1, 0 ) ) );
162 
163 
164    Vector playerPos = this->myTarget->getAbsCoor() + Vector(0, 12, 0);
165    Vector ds = playerPos - ( this->cannons->getAbsCoor() );
166    if ( isActive && ds.len() <= range && playerVisible )
167    {
168      this->moveTowards( ds,  dt);
169     
170      //if target within +/- 2.5 degrees of aim -> fire
171      Vector dv1 = ds;
172      dv1.normalize();
173      Vector dv2 = this->cannons->getAbsDir().apply( Vector(-1, 0, 0));
174      dv2.normalize();
175      float angle = dv1.dot(dv2);
176      if (angle > 0.999)
177      {
178        if (this->weapon) this->weapon->fire(true);
179      }
180    }
181    else
182    {
183      if ( !( isActive && ds.len() <= range ) )
184        this->moveTowards( Vector(0, -1, 0), dt );
185    }
186  }
187}
188
189void AdmTurret::draw() const
190{
191  WorldEntity::draw();
192
193#if 0
194  glMatrixMode(GL_MODELVIEW);
195  glPushMatrix();
196
197  glPushAttrib(GL_ENABLE_BIT);
198
199  glDisable(GL_LIGHTING);
200  glDisable(GL_TEXTURE_2D);
201  glDisable(GL_BLEND);
202  glLineWidth(2.0);
203
204
205  Vector mp = this->cannons->getAbsCoor();
206  Vector op = this->cannons->getAbsDir().apply( Vector(-1, 0, 0) );
207  op *= 100;
208  op += mp;
209
210  glColor3f(1.0, 0.0, 0.0 );
211  glBegin(GL_LINE_STRIP);
212    glVertex3f(mp.x, mp.y, mp.z);
213    glVertex3f(op.x, op.y, op.z);
214  glEnd();
215
216  op = this->myTarget->getAbsCoor() - ( this->cannons->getAbsCoor() );
217  op += mp;
218
219  glColor3f(0.0, 1.0, 0.0 );
220  glBegin(GL_LINE_STRIP);
221    glVertex3f(mp.x, mp.y, mp.z);
222    glVertex3f(op.x, op.y, op.z);
223  glEnd();
224 
225  glPopAttrib();
226  glPopMatrix();
227#endif
228}
229
230void AdmTurret::collidesWith(WorldEntity* entity, const Vector& location)
231{
232}
233
234void AdmTurret::activate()
235{
236}
237
238void AdmTurret::deactivate()
239{
240}
241
242void AdmTurret::addCannons( const TiXmlElement * root )
243{
244  this->cannons = new DamageForwardingWorldEntity( this );
245        this->cannons->setParent(this);
246        this->cannons->loadParams( root );
247
248        //this->cannons->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
249        //this->cannons->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
250
251        this->cannons->toList( getOMListNumber() );
252        this->cannons->setForwardDamageToParent( true );
253}
254
255void AdmTurret::addWeapon( const TiXmlElement * root )
256{
257        this->weapon = new BspWeapon(getOMListNumber());
258        this->weapon->setParent( this->cannons );
259        this->weapon->loadParams(root);
260        this->weapon->toList( getOMListNumber() );
261        //this->weapon->setAbsCoor( this->cannons->getAbsCoor() );
262        this->weapon->setAbsDir( this->weapon->getAbsDir() * Quaternion( PI, Vector(0, 1, 0) ) );
263       
264        this->weapon->setForwardDamageToParent( true );
265}
266
267void AdmTurret::addSensor( const TiXmlElement * root )
268{
269  this->sensor = new DamageForwardingWorldEntity( this );
270        this->sensor->setParent(this);
271        this->sensor->loadParams( root );
272
273        //this->sensor->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
274        //this->sensor->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
275       
276}
277
278void AdmTurret::moveTowards( Vector targetDir, float dt )
279{
280  if ( this->cannons->getParent() != NullParent::getNullParent() )
281  {
282    this->cannons->setParent( NullParent::getNullParent() );
283    this->cannons->shiftCoor( this->getAbsCoor() );
284
285    this->sensor->setParent( NullParent::getNullParent() );
286    this->sensor->shiftCoor( this->getAbsCoor() );
287  }
288
289  targetDir.normalize();
290
291  float dAngle = dt;
292
293  float bestResult = -1.0f;
294  Quaternion bestBodyRot;
295  Quaternion bestCannonRot;
296  float bestBodyAngle = 0.0f;
297  float bestCannonAngle = 0.0f;
298
299   Quaternion baseRot(0, Vector(1, 0, 0));
300   if ( isCeil )
301   {
302     baseRot = Quaternion( PI, Vector( 1, 0, 0 ) );
303   }
304
305  for ( int dBodyAngle = -1; dBodyAngle<2; dBodyAngle++ )
306  {
307    for ( int dCannonAngle = -1; dCannonAngle<2; dCannonAngle++ )
308    {
309      float bodyAngle = this->bodyAngle + dBodyAngle*dAngle;
310      float cannonAngle = this->cannonAngle + dCannonAngle*dAngle;
311
312      while ( bodyAngle > 2*PI )
313        bodyAngle -= 2*PI;
314      while ( bodyAngle < 0 )
315        bodyAngle += 2*PI;
316      while ( cannonAngle > 2*PI )
317        cannonAngle -= 2*PI;
318      while ( cannonAngle < 0 )
319        cannonAngle += 2*PI;
320
321      Quaternion bodyRot( bodyAngle, Vector( 0, 1, 0 ) );
322      Quaternion cannonRot( cannonAngle, Vector( 0, 0, 1) );
323
324       bodyRot = baseRot * bodyRot;
325       cannonRot = baseRot * cannonRot;
326
327      float result = (bodyRot * cannonRot).apply( Vector( -1, 0, 0 ) ).dot( targetDir );
328
329      if ( result > bestResult )
330      {
331        bestResult = result;
332        bestBodyRot = bodyRot;
333        bestBodyAngle = bodyAngle;
334        bestCannonRot = cannonRot;
335        bestCannonAngle = cannonAngle;
336      }
337    }
338  }
339
340  this->bodyAngle = bestBodyAngle;
341  this->cannonAngle = bestCannonAngle;
342
343  this->setAbsDir( bestBodyRot );
344  this->cannons->setAbsDir( bestBodyRot * bestCannonRot );
345}
346
347void AdmTurret::updatePlayerVisible( )
348{
349  std::list<WorldEntity*>::iterator entityIterator;
350  // for all bsp managers check all entities
351  Vector pos = this->cannons->getAbsCoor();
352  Vector dir = this->myTarget->getAbsCoor() - pos  + Vector(0, 12, 0);
353
354  this->playerVisible = true;
355  for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin();
356       bspIterator != BspEntity::objectList().end();
357       bspIterator++) {
358      float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 );
359
360      if ( res < dir.len() ) this->playerVisible = false;
361  }
362}
363
364void AdmTurret::destroy( WorldEntity * killer )
365{
366  Explosion().explode(this->cannons,Vector(1,1,1));
367}
Note: See TracBrowser for help on using the repository browser.