Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10770 was 10769, checked in by rennerc, 18 years ago

sensor working

File size: 9.5 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 && this->myTarget->getOMListNumber()!=OM_DEAD )
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  if ( this->sensor )
193    this->sensor->draw();
194
195#if 0
196  glMatrixMode(GL_MODELVIEW);
197  glPushMatrix();
198
199  glPushAttrib(GL_ENABLE_BIT);
200
201  glDisable(GL_LIGHTING);
202  glDisable(GL_TEXTURE_2D);
203  glDisable(GL_BLEND);
204  glLineWidth(2.0);
205
206
207  Vector mp = this->cannons->getAbsCoor();
208  Vector op = this->cannons->getAbsDir().apply( Vector(-1, 0, 0) );
209  op *= 100;
210  op += mp;
211
212  glColor3f(1.0, 0.0, 0.0 );
213  glBegin(GL_LINE_STRIP);
214    glVertex3f(mp.x, mp.y, mp.z);
215    glVertex3f(op.x, op.y, op.z);
216  glEnd();
217
218  op = this->myTarget->getAbsCoor() - ( this->cannons->getAbsCoor() );
219  op += mp;
220
221  glColor3f(0.0, 1.0, 0.0 );
222  glBegin(GL_LINE_STRIP);
223    glVertex3f(mp.x, mp.y, mp.z);
224    glVertex3f(op.x, op.y, op.z);
225  glEnd();
226 
227  glPopAttrib();
228  glPopMatrix();
229#endif
230}
231
232void AdmTurret::collidesWith(WorldEntity* entity, const Vector& location)
233{
234}
235
236void AdmTurret::activate()
237{
238}
239
240void AdmTurret::deactivate()
241{
242}
243
244void AdmTurret::addCannons( const TiXmlElement * root )
245{
246  this->cannons = new DamageForwardingWorldEntity( this );
247        this->cannons->setParent(this);
248        this->cannons->loadParams( root );
249
250        //this->cannons->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
251        //this->cannons->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
252
253        this->cannons->toList( getOMListNumber() );
254        this->cannons->setForwardDamageToParent( true );
255}
256
257void AdmTurret::addWeapon( const TiXmlElement * root )
258{
259        this->weapon = new BspWeapon(getOMListNumber());
260        this->weapon->setParent( this->cannons );
261        this->weapon->loadParams(root);
262        this->weapon->toList( getOMListNumber() );
263        //this->weapon->setAbsCoor( this->cannons->getAbsCoor() );
264        this->weapon->setAbsDir( this->weapon->getAbsDir() * Quaternion( PI, Vector(0, 1, 0) ) );
265       
266        this->weapon->setForwardDamageToParent( true );
267}
268
269void AdmTurret::addSensor( const TiXmlElement * root )
270{
271  this->sensor = new DamageForwardingWorldEntity( this );
272        this->sensor->setParent(this);
273        this->sensor->loadParams( root );
274
275        //this->sensor->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
276        //this->sensor->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
277       
278}
279
280void AdmTurret::moveTowards( Vector targetDir, float dt )
281{
282  if ( this->cannons->getParent() != NullParent::getNullParent() )
283  {
284    this->cannons->setParent( NullParent::getNullParent() );
285    this->cannons->shiftCoor( this->getAbsCoor() );
286
287    this->sensor->setParent( NullParent::getNullParent() );
288    this->sensor->shiftCoor( this->getAbsCoor() );
289  }
290
291  targetDir.normalize();
292
293  float dAngle = dt;
294
295  float bestResult = -1.0f;
296  Quaternion bestBodyRot;
297  Quaternion bestCannonRot;
298  float bestBodyAngle = 0.0f;
299  float bestCannonAngle = 0.0f;
300
301   Quaternion baseRot(0, Vector(1, 0, 0));
302   if ( isCeil )
303   {
304     baseRot = Quaternion( PI, Vector( 1, 0, 0 ) );
305   }
306
307  for ( int dBodyAngle = -1; dBodyAngle<2; dBodyAngle++ )
308  {
309    for ( int dCannonAngle = -1; dCannonAngle<2; dCannonAngle++ )
310    {
311      float bodyAngle = this->bodyAngle + dBodyAngle*dAngle;
312      float cannonAngle = this->cannonAngle + dCannonAngle*dAngle;
313
314      while ( bodyAngle > 2*PI )
315        bodyAngle -= 2*PI;
316      while ( bodyAngle < 0 )
317        bodyAngle += 2*PI;
318      while ( cannonAngle > 2*PI )
319        cannonAngle -= 2*PI;
320      while ( cannonAngle < 0 )
321        cannonAngle += 2*PI;
322
323      Quaternion bodyRot( bodyAngle, Vector( 0, 1, 0 ) );
324      Quaternion cannonRot( cannonAngle, Vector( 0, 0, 1) );
325
326       bodyRot = baseRot * bodyRot;
327       cannonRot = baseRot * cannonRot;
328
329      float result = (bodyRot * cannonRot).apply( Vector( -1, 0, 0 ) ).dot( targetDir );
330
331      if ( result > bestResult )
332      {
333        bestResult = result;
334        bestBodyRot = bodyRot;
335        bestBodyAngle = bodyAngle;
336        bestCannonRot = cannonRot;
337        bestCannonAngle = cannonAngle;
338      }
339    }
340  }
341
342  this->bodyAngle = bestBodyAngle;
343  this->cannonAngle = bestCannonAngle;
344
345  this->setAbsDir( bestBodyRot );
346  this->cannons->setAbsDir( bestBodyRot * bestCannonRot );
347}
348
349void AdmTurret::updatePlayerVisible( )
350{
351  std::list<WorldEntity*>::iterator entityIterator;
352  // for all bsp managers check all entities
353  Vector pos = this->cannons->getAbsCoor();
354  Vector dir = this->myTarget->getAbsCoor() - pos  + Vector(0, 12, 0);
355
356  this->playerVisible = true;
357  for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin();
358       bspIterator != BspEntity::objectList().end();
359       bspIterator++) {
360      float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 );
361
362      if ( res < dir.len() ) this->playerVisible = false;
363  }
364}
365
366void AdmTurret::destroy( WorldEntity * killer )
367{
368  Explosion().explode(this->cannons,Vector(1,1,1));
369}
Note: See TracBrowser for help on using the repository browser.