Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

less magic

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