Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

first shooting of the turret

File size: 8.7 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
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       
135        this->bodyAngle = this->cannonAngle = 0.0f;
136        this->isActive = true;
137        this->range = 400;
138        this->isCeil = false;
139        this->bFire = false;
140        this->playerVisible = false;
141
142       
143}
144
145void AdmTurret::tick(float dt)
146{
147  WorldEntity::tick(dt);
148 
149  this->updatePlayerVisible();
150
151  //rotate sensor 2PI/sec
152  this->sensor->setAbsDir( this->sensor->getAbsDir() * Quaternion( PI*2*dt, Vector( 0, -1, 0 ) ) );
153
154
155  Vector playerPos = this->myTarget->getAbsCoor() + Vector(0, 12, 0);
156  Vector ds = playerPos - ( this->cannons->getAbsCoor() );
157  if ( isActive && ds.len() <= range && playerVisible )
158  {
159    this->moveTowards( ds,  dt);
160
161  //if target within +/- 2.5 degrees of aim -> fire
162  Vector dv1 = ds;
163  dv1.normalize();
164  Vector dv2 = this->cannons->getAbsDir().apply( Vector(-1, 0, 0));
165  dv2.normalize();
166  float angle = dv1.dot(dv2);
167  if (angle > 0.999)
168        {
169        this->fire();
170        }
171  }
172  else
173    this->moveTowards( Vector(0, -1, 0), dt );
174}
175
176void AdmTurret::draw() const
177{
178  WorldEntity::draw();
179
180
181  glMatrixMode(GL_MODELVIEW);
182  glPushMatrix();
183
184  glPushAttrib(GL_ENABLE_BIT);
185
186  glDisable(GL_LIGHTING);
187  glDisable(GL_TEXTURE_2D);
188  glDisable(GL_BLEND);
189  glLineWidth(2.0);
190
191
192  Vector mp = this->cannons->getAbsCoor();
193  Vector op = this->cannons->getAbsDir().apply( Vector(-1, 0, 0) );
194  op *= 100;
195  op += mp;
196
197  glColor3f(1.0, 0.0, 0.0 );
198  glBegin(GL_LINE_STRIP);
199    glVertex3f(mp.x, mp.y, mp.z);
200    glVertex3f(op.x, op.y, op.z);
201  glEnd();
202
203  op = this->myTarget->getAbsCoor() - ( this->cannons->getAbsCoor() );
204  op += mp;
205
206  glColor3f(0.0, 1.0, 0.0 );
207  glBegin(GL_LINE_STRIP);
208    glVertex3f(mp.x, mp.y, mp.z);
209    glVertex3f(op.x, op.y, op.z);
210  glEnd();
211 
212  glPopAttrib();
213  glPopMatrix();
214 
215}
216
217void AdmTurret::collidesWith(WorldEntity* entity, const Vector& location)
218{
219}
220
221void AdmTurret::activate()
222{
223}
224
225void AdmTurret::deactivate()
226{
227}
228
229void AdmTurret::fire()
230{
231}
232
233void AdmTurret::addCannons( const TiXmlElement * root )
234{
235        this->cannons = new WorldEntity();
236        this->cannons->setParent(this);
237        this->cannons->loadParams( root );
238
239        //this->cannons->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
240        //this->cannons->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
241       
242        this->cannons->toList( getOMListNumber() );
243
244        this->weapon = new BspWeapon();
245        this->weapon->setParent( this->cannons );
246        this->weapon->toList( getOMListNumber() );
247        this->weapon->setAbsCoor( this->cannons->getAbsCoor() );
248        this->weapon->setAbsDir( this->weapon->getAbsDir() * Quaternion( PI, Vector(0, 1, 0) ) );
249}
250
251void AdmTurret::addSensor( const TiXmlElement * root )
252{
253        this->sensor = new WorldEntity();
254        this->sensor->setParent(this);
255        this->sensor->loadParams( root );
256
257        //this->sensor->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
258        //this->sensor->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
259        this->sensor->toList( getOMListNumber() );
260}
261
262void AdmTurret::moveTowards( Vector targetDir, float dt )
263{
264  if ( this->cannons->getParent() != NullParent::getNullParent() )
265  {
266    this->cannons->setParent( NullParent::getNullParent() );
267    this->cannons->shiftCoor( this->getAbsCoor() );
268   
269    this->sensor->setParent( NullParent::getNullParent() );
270    this->sensor->shiftCoor( this->getAbsCoor() );
271  }
272 
273  targetDir.normalize();
274 
275  float dAngle = dt;
276 
277  float bestResult = -1.0f;
278  Quaternion bestBodyRot;
279  Quaternion bestCannonRot;
280  float bestBodyAngle = 0.0f;
281  float bestCannonAngle = 0.0f;
282 
283   Quaternion baseRot(0, Vector(1, 0, 0));
284   if ( isCeil )
285   {
286     printf( "ceil\n" );
287     baseRot = Quaternion( PI, Vector( 1, 0, 0 ) );
288   }
289 
290  for ( int dBodyAngle = -1; dBodyAngle<2; dBodyAngle++ )
291  {
292    for ( int dCannonAngle = -1; dCannonAngle<2; dCannonAngle++ )
293    {
294      float bodyAngle = this->bodyAngle + dBodyAngle*dAngle;
295      float cannonAngle = this->cannonAngle + dCannonAngle*dAngle;
296     
297      while ( bodyAngle > 2*PI )
298        bodyAngle -= 2*PI;
299      while ( bodyAngle < 0 )
300        bodyAngle += 2*PI;
301      while ( cannonAngle > 2*PI )
302        cannonAngle -= 2*PI;
303      while ( cannonAngle < 0 )
304        cannonAngle += 2*PI;
305     
306      Quaternion bodyRot( bodyAngle, Vector( 0, 1, 0 ) );
307      Quaternion cannonRot( cannonAngle, Vector( 0, 0, 1) );
308     
309       bodyRot = baseRot * bodyRot;
310       cannonRot = baseRot * cannonRot;
311     
312      float result = (bodyRot * cannonRot).apply( Vector( -1, 0, 0 ) ).dot( targetDir );
313     
314      if ( result > bestResult )
315      {
316        bestResult = result;
317        bestBodyRot = bodyRot;
318        bestBodyAngle = bodyAngle;
319        bestCannonRot = cannonRot;
320        bestCannonAngle = cannonAngle;
321      }
322    }
323  }
324 
325  this->bodyAngle = bestBodyAngle;
326  this->cannonAngle = bestCannonAngle;
327 
328  this->setAbsDir( bestBodyRot );
329  this->cannons->setAbsDir( bestBodyRot * bestCannonRot );
330}
331
332void AdmTurret::updatePlayerVisible( )
333{
334  std::list<WorldEntity*>::iterator entityIterator;
335  // for all bsp managers check all entities
336  Vector pos = this->cannons->getAbsCoor();
337  Vector dir = this->myTarget->getAbsCoor() - pos;
338
339
340  this->playerVisible = true;
341  for( ObjectList<BspEntity>::const_iterator bspIterator = BspEntity::objectList().begin();
342       bspIterator != BspEntity::objectList().end();
343       bspIterator++) {
344      float res = (dynamic_cast<BspEntity*>(*bspIterator)->getBspManager())->checkCollisionRay( pos, dir, dir.len() + 1 );
345     
346      if ( res < dir.len() )
347        this->playerVisible = false;
348  }
349}
Note: See TracBrowser for help on using the repository browser.