Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/adm/src/world_entities/npcs/adm_turret.cc @ 10694

Last change on this file since 10694 was 10694, checked in by retolu, 17 years ago

adm working

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