Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/npcs/generic_npc.cc @ 8822

Last change on this file since 8822 was 8822, checked in by snellen, 18 years ago

fixed bug in ExecutorLua3ret

File size: 11.0 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific
14   main-programmer: Patrick Boenzli
15   co-programmer:
16*/
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19
20#include "util/loading/factory.h"
21#include "util/loading/load_param.h"
22
23#include "interactive_model.h"
24#include "md2/md2Model.h"
25
26#include "sound_buffer.h"
27
28#include "loading/resource_manager.h"
29
30#include "generic_npc.h"
31
32#include "animation/animation3d.h"
33
34using namespace std;
35
36
37
38CREATE_FACTORY(GenericNPC, CL_GENERIC_NPC);
39
40#include "script_class.h"
41CREATE_SCRIPTABLE_CLASS(GenericNPC, CL_GENERIC_NPC,
42                        //addMethod("walkTo", ExecutorLua7ret<GenericNPC,float, float, float, float, float, float, float, float>(&GenericNPC::walkTo))
43                        addMethod("walkTo", ExecutorLua3ret<GenericNPC,float,float,float,float>(&GenericNPC::walkTo))
44                       );
45
46
47
48/**
49 * constructor
50 */
51GenericNPC::GenericNPC(const TiXmlElement* root)
52  : NPC(root)
53{
54  this->init();
55
56  if (root != NULL)
57    this->loadParams(root);
58}
59
60
61GenericNPC::GenericNPC()
62  : NPC(NULL)
63{
64
65}
66
67/**
68 * deconstructor
69 */
70GenericNPC::~GenericNPC ()
71{
72  if( this->currentAnim != NULL)
73    delete this->currentAnim;
74}
75
76
77/**
78 * initializing the npc enity
79 */
80void GenericNPC::init()
81{
82  this->setClassID(CL_GENERIC_NPC, "GenericNPC");
83  this->toList(OM_GROUP_00);
84
85  if (this->soundBuffer != NULL)
86    ResourceManager::getInstance()->unload(this->soundBuffer);
87  this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
88
89  // collision reaction registration
90//   this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
91}
92
93
94/**
95 * loads the Settings of a MD2Creature from an XML-element.
96 * @param root the XML-element to load the MD2Creature's properties from
97 */
98void GenericNPC::loadParams(const TiXmlElement* root)
99{
100  NPC::loadParams(root);
101
102}
103
104
105/**
106 * sets the animation of this npc
107 * @param anumationIndex: the animation index
108 * @param anumPlaybackMode: the playback mode
109 */
110void GenericNPC::setAnimation(int animationIndex, int animPlaybackMode)
111{
112  if( likely(this->getModel(0) != NULL))
113    ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode);
114}
115
116
117/**
118 * sets the animation of this npc
119 * @param anumationIndex: the animation index
120 * @param anumPlaybackMode: the playback mode
121 */
122void GenericNPC::playAnimation(int animationIndex, int animPlaybackMode)
123{
124  if( likely(this->getModel(0) != NULL))
125    ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode);
126
127}
128
129
130/**
131 * play a sound
132 * @param filename: name of the file
133 */
134void GenericNPC::playSound(std::string filename)
135{
136
137}
138
139
140/**
141 * walt to
142 * @param coordinate: coordinate to go to
143 */
144float GenericNPC::walkTo(float x, float y, float z, float qu, float qx, float qy, float qz)
145{
146  Vector destCoor = Vector(x, y, z);
147  Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu);
148
149  // check if this is the current goal
150  if( this->destCoor != destCoor || this->destDir != destDir)
151  {
152    this->destCoor = destCoor;
153    this->destDir = destDir;
154
155    float time = 10.0f;
156
157    if( this->currentAnim != NULL)
158      delete this->currentAnim;
159
160    this->currentAnim = new Animation3D(this);
161    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), 0.1f, ANIM_LINEAR, ANIM_LINEAR);
162    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR);
163    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), time, ANIM_LINEAR, ANIM_LINEAR);
164    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR);
165
166    this->currentAnim->setInfinity(ANIM_INF_CONSTANT);
167    this->currentAnim->play();
168
169    this->setAnimation(RUN, MD2_ANIM_LOOP);
170  }
171
172  // calculate the distance
173  Vector distance = this->getAbsCoor() - this->destCoor;
174  return distance.len();
175}
176
177
178/**
179 * walk to a specific place with direction
180 *
181 * @param x: x coordinate to go to
182 * @param y: y coordinate to go to
183 * @param z: z coordinate to go to
184 *
185 * without turning itself
186 */
187float GenericNPC::walkTo(float x, float y, float z)
188{
189  Quaternion q = this->getAbsDir();
190
191  //printf("%s moving to %f, %f, %f \n",this->getName(),x,y,z);
192
193  return this->walkTo(x, y, z, q.w, q.v.x, q.v.y, q.v.z);
194}
195
196/**
197 * walk to a specific place with direction
198 *
199 * @param x: x coordinate to go to
200 * @param y: y coordinate to go to
201 * @param qu: angle to rotate
202 * @param qx: x coordinate of rotation vector
203 * @param qy: y coordinate of rotation vector
204 * @param qz: z coordinate of rotation vector
205 *
206 */
207float GenericNPC::walkTo(float x, float y, float qu, float qx, float qy, float qz)
208{
209  return this->walkTo(x, y, 0.0f, qu, qx, qy, qz);
210}
211
212
213/**
214 * walk to a specific place with direction
215 *
216 * @param coor: vector place
217 * @param dir: direction
218 *
219 */
220float GenericNPC::walkTo(const Vector& coor, const Quaternion& dir)
221{
222  return this->walkTo(coor.x, coor.y, coor.z, dir.w, dir.v.x, dir.v.y, dir.v.z);
223}
224
225
226
227/**
228 * run to a specific place with direction
229 *
230 * @param x: x coordinate to go to
231 * @param y: y coordinate to go to
232 * @param z: z coordinate to go to
233 * @param qu: angle to rotate
234 * @param qx: x coordinate of rotation vector
235 * @param qy: y coordinate of rotation vector
236 * @param qz: z coordinate of rotation vector
237 *
238 */
239float GenericNPC::runTo(float x, float y, float z, float qu, float qx, float qy, float qz)
240{
241  Vector destCoor = Vector(x, y, z);
242  Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu);
243
244  // check if this is the current goal
245  if( this->destCoor != destCoor || this->destDir != destDir)
246  {
247    this->destCoor = destCoor;
248    this->destDir = destDir;
249
250    float time = 5.0f;
251
252    if( this->currentAnim != NULL)
253      delete this->currentAnim;
254
255    this->currentAnim = new Animation3D(this);
256    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), 0.1f, ANIM_LINEAR, ANIM_LINEAR);
257    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR);
258    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), time, ANIM_LINEAR, ANIM_LINEAR);
259    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR);
260
261    this->currentAnim->setInfinity(ANIM_INF_CONSTANT);
262    this->currentAnim->play();
263
264    this->setAnimation(RUN, MD2_ANIM_LOOP);
265  }
266
267  // calculate the distance
268  Vector distance = this->getAbsCoor() - this->destCoor;
269  return distance.len();
270}
271
272
273/**
274 * run to a specific place with direction
275 *
276 * @param x: x coordinate to go to
277 * @param y: y coordinate to go to
278 * @param qu: angle to rotate
279 * @param qx: x coordinate of rotation vector
280 * @param qy: y coordinate of rotation vector
281 * @param qz: z coordinate of rotation vector
282 *
283 */
284float GenericNPC::runTo(float x, float y, float qu, float qx, float qy, float qz)
285{
286  this->runTo(x, y, 0.0f, qu, qx, qy, qz);
287}
288
289
290/**
291 * run to a specific place with direction
292 *
293 * @param coor: vector place
294 * @param dir: direction
295 *
296 */
297float GenericNPC::runTo(const Vector& coordinate, const Quaternion& dir)
298{
299  this->runTo(coordinate.x, coordinate.y, coordinate.z, dir.w, dir.v.x, dir.v.y, dir.v.z);
300}
301
302
303/**
304 * crouch to a specific place with direction
305 *
306 * @param x: x coordinate to go to
307 * @param y: y coordinate to go to
308 * @param z: z coordinate to go to
309 * @param qu: angle to rotate
310 * @param qx: x coordinate of rotation vector
311 * @param qy: y coordinate of rotation vector
312 * @param qz: z coordinate of rotation vector
313 *
314 */
315float GenericNPC::crouchTo(float x, float y, float z, float qu, float qx, float qy, float qz)
316{
317  Vector destCoor = Vector(x, y, z);
318  Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu);
319
320  // check if this is the current goal
321  if( this->destCoor != destCoor || this->destDir != destDir)
322  {
323    this->destCoor = destCoor;
324    this->destDir = destDir;
325
326    float time = 5.0f;
327
328    if( this->currentAnim != NULL)
329      delete this->currentAnim;
330
331    this->currentAnim = new Animation3D(this);
332    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), 0.1f, ANIM_LINEAR, ANIM_LINEAR);
333    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR);
334    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), time, ANIM_LINEAR, ANIM_LINEAR);
335    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR);
336
337    this->currentAnim->setInfinity(ANIM_INF_CONSTANT);
338    this->currentAnim->play();
339
340    this->setAnimation(CROUCH_WALK, MD2_ANIM_LOOP);
341  }
342
343  // calculate the distance
344  Vector distance = this->getAbsCoor() - this->destCoor;
345  return distance.len();
346}
347
348
349/**
350 * couch to a specific place with direction
351 *
352 * @param x: x coordinate to go to
353 * @param y: y coordinate to go to
354 * @param qu: angle to rotate
355 * @param qx: x coordinate of rotation vector
356 * @param qy: y coordinate of rotation vector
357 * @param qz: z coordinate of rotation vector
358 *
359 */
360float GenericNPC::crouchTo(float x, float y, float qu, float qx, float qy, float qz)
361{
362  this->crouchTo(x, y, 0.0f, qu, qx, qy, qz);
363}
364
365
366
367/**
368 * crouch to a specific place with direction
369 *
370 * @param coor: vector place
371 * @param dir: direction
372 *
373 */
374float GenericNPC::crouchTo(const Vector& coordinate, const Quaternion& dir)
375{
376  this->crouchTo(coordinate.x, coordinate.y, coordinate.z, dir.w, dir.v.x, dir.v.y, dir.v.z);
377}
378
379
380/**
381 * stops the generic animation
382 */
383void GenericNPC::stop()
384{
385  if( this->currentAnim != NULL)
386  {
387    this->currentAnim->stop();
388    delete this->currentAnim;
389    this->currentAnim = NULL;
390
391    this->setAnimation(STAND, MD2_ANIM_LOOP);
392  }
393}
394
395
396/**
397 * lookat a world entity
398 * @param worldEntity: the worldentity to look at
399 */
400float GenericNPC::lookAt(WorldEntity* worldEntity)
401{}
402
403
404/**
405 * talk to a world entity and play a sound/music/voice
406 * @param worldEntity: entity
407 * @param dialogNr: sound nr to be played (from the xml load tags)
408 */
409float GenericNPC::talkTo(WorldEntity* worldEntity, int dialogNr)
410{}
411
412
413/**
414 * world entity to shoot at if there is any weapon on the npc
415 * @param entity: entity to shoot entity
416 */
417void GenericNPC::shootAt(WorldEntity* entity)
418{}
419
420
421
422
423
424
425
426
427
428
429/**
430 * tick this world entity
431 * @param time: time in seconds expirded since the last tick
432 */
433void GenericNPC::tick (float time)
434{
435  if( likely(this->getModel(0) != NULL))
436    ((InteractiveModel*)this->getModel(0))->tick(time);
437
438  // tick this animation
439  if( this->currentAnim != NULL)
440    this->currentAnim->tick(time);
441}
442
443
444
445void GenericNPC::destroy()
446{
447  int randi = (int)(5.0f * (float)rand()/(float)RAND_MAX);
448
449  if( randi == 1)
450    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
451  else if( randi == 2)
452    this->setAnimation(DEATH_FALLFORWARD, MD2_ANIM_ONCE);
453  else if( randi == 3)
454    this->setAnimation(DEATH_FALLBACKSLOW, MD2_ANIM_ONCE);
455  else if( randi == 4)
456    this->setAnimation(CROUCH_DEATH, MD2_ANIM_ONCE);
457  else
458    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
459}
460
Note: See TracBrowser for help on using the repository browser.