[8514] | 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 | |
---|
[8516] | 26 | #include "sound_buffer.h" |
---|
| 27 | |
---|
| 28 | #include "loading/resource_manager.h" |
---|
| 29 | |
---|
[8514] | 30 | #include "generic_npc.h" |
---|
| 31 | |
---|
[8802] | 32 | #include "animation/animation3d.h" |
---|
| 33 | |
---|
[8514] | 34 | using namespace std; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | CREATE_FACTORY(GenericNPC, CL_GENERIC_NPC); |
---|
| 39 | |
---|
[8811] | 40 | #include "script_class.h" |
---|
| 41 | CREATE_SCRIPTABLE_CLASS(GenericNPC, CL_GENERIC_NPC, |
---|
[8815] | 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)) |
---|
[8811] | 44 | ); |
---|
[8514] | 45 | |
---|
| 46 | |
---|
[8811] | 47 | |
---|
[8514] | 48 | /** |
---|
| 49 | * constructor |
---|
| 50 | */ |
---|
| 51 | GenericNPC::GenericNPC(const TiXmlElement* root) |
---|
| 52 | : NPC(root) |
---|
| 53 | { |
---|
| 54 | this->init(); |
---|
| 55 | |
---|
| 56 | if (root != NULL) |
---|
| 57 | this->loadParams(root); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
[8811] | 61 | GenericNPC::GenericNPC() |
---|
| 62 | : NPC(NULL) |
---|
| 63 | { |
---|
[8814] | 64 | |
---|
[8811] | 65 | } |
---|
| 66 | |
---|
[8514] | 67 | /** |
---|
| 68 | * deconstructor |
---|
| 69 | */ |
---|
| 70 | GenericNPC::~GenericNPC () |
---|
[8802] | 71 | { |
---|
| 72 | if( this->currentAnim != NULL) |
---|
| 73 | delete this->currentAnim; |
---|
| 74 | } |
---|
[8514] | 75 | |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * initializing the npc enity |
---|
| 79 | */ |
---|
| 80 | void GenericNPC::init() |
---|
| 81 | { |
---|
| 82 | this->setClassID(CL_GENERIC_NPC, "GenericNPC"); |
---|
| 83 | this->toList(OM_GROUP_00); |
---|
[8516] | 84 | |
---|
| 85 | if (this->soundBuffer != NULL) |
---|
| 86 | ResourceManager::getInstance()->unload(this->soundBuffer); |
---|
| 87 | this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV); |
---|
[8705] | 88 | |
---|
| 89 | // collision reaction registration |
---|
[8706] | 90 | // this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY); |
---|
[8514] | 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 | */ |
---|
| 98 | void GenericNPC::loadParams(const TiXmlElement* root) |
---|
| 99 | { |
---|
[8705] | 100 | NPC::loadParams(root); |
---|
[8514] | 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 | */ |
---|
| 110 | void 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 | */ |
---|
[8705] | 122 | void GenericNPC::playAnimation(int animationIndex, int animPlaybackMode) |
---|
[8514] | 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 | */ |
---|
[8705] | 134 | void GenericNPC::playSound(std::string filename) |
---|
[8514] | 135 | { |
---|
[8705] | 136 | |
---|
[8514] | 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * walt to |
---|
| 142 | * @param coordinate: coordinate to go to |
---|
| 143 | */ |
---|
[8783] | 144 | float GenericNPC::walkTo(float x, float y, float z, float qu, float qx, float qy, float qz) |
---|
[8590] | 145 | { |
---|
[8802] | 146 | Vector destCoor = Vector(x, y, z); |
---|
| 147 | Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu); |
---|
[8590] | 148 | |
---|
[8802] | 149 | // check if this is the current goal |
---|
[8816] | 150 | if( this->destCoor != destCoor || this->destDir != destDir) |
---|
[8802] | 151 | { |
---|
[8810] | 152 | this->destCoor = destCoor; |
---|
| 153 | this->destDir = destDir; |
---|
[8802] | 154 | |
---|
[8817] | 155 | float time = 10.0f; |
---|
[8802] | 156 | |
---|
| 157 | if( this->currentAnim != NULL) |
---|
| 158 | delete this->currentAnim; |
---|
| 159 | |
---|
| 160 | this->currentAnim = new Animation3D(this); |
---|
[8816] | 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); |
---|
[8817] | 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); |
---|
[8812] | 165 | |
---|
[8816] | 166 | this->currentAnim->setInfinity(ANIM_INF_CONSTANT); |
---|
| 167 | this->currentAnim->play(); |
---|
| 168 | |
---|
[8812] | 169 | this->setAnimation(RUN, MD2_ANIM_LOOP); |
---|
[8802] | 170 | } |
---|
| 171 | |
---|
| 172 | // calculate the distance |
---|
| 173 | Vector distance = this->getAbsCoor() - this->destCoor; |
---|
| 174 | return distance.len(); |
---|
[8590] | 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
[8810] | 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 | */ |
---|
| 187 | float GenericNPC::walkTo(float x, float y, float z) |
---|
| 188 | { |
---|
| 189 | Quaternion q = this->getAbsDir(); |
---|
[8816] | 190 | |
---|
[8822] | 191 | //printf("%s moving to %f, %f, %f \n",this->getName(),x,y,z); |
---|
[8802] | 192 | |
---|
[8810] | 193 | return this->walkTo(x, y, z, q.w, q.v.x, q.v.y, q.v.z); |
---|
| 194 | } |
---|
| 195 | |
---|
[8590] | 196 | /** |
---|
[8802] | 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 | */ |
---|
| 207 | float 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 | /** |
---|
[8805] | 214 | * walk to a specific place with direction |
---|
| 215 | * |
---|
| 216 | * @param coor: vector place |
---|
| 217 | * @param dir: direction |
---|
| 218 | * |
---|
| 219 | */ |
---|
| 220 | float 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 | |
---|
[8814] | 226 | |
---|
[8805] | 227 | /** |
---|
[8814] | 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 | */ |
---|
| 239 | float GenericNPC::runTo(float x, float y, float z, float qu, float qx, float qy, float qz) |
---|
[8817] | 240 | { |
---|
| 241 | Vector destCoor = Vector(x, y, z); |
---|
| 242 | Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu); |
---|
[8814] | 243 | |
---|
[8817] | 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; |
---|
[8814] | 249 | |
---|
[8817] | 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 | |
---|
[8814] | 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 | */ |
---|
| 284 | float GenericNPC::runTo(float x, float y, float qu, float qx, float qy, float qz) |
---|
[8817] | 285 | { |
---|
| 286 | this->runTo(x, y, 0.0f, qu, qx, qy, qz); |
---|
| 287 | } |
---|
[8814] | 288 | |
---|
| 289 | |
---|
[8817] | 290 | /** |
---|
| 291 | * run to a specific place with direction |
---|
| 292 | * |
---|
| 293 | * @param coor: vector place |
---|
| 294 | * @param dir: direction |
---|
| 295 | * |
---|
| 296 | */ |
---|
| 297 | float 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 | } |
---|
[8814] | 301 | |
---|
[8817] | 302 | |
---|
[8814] | 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 | */ |
---|
| 315 | float GenericNPC::crouchTo(float x, float y, float z, float qu, float qx, float qy, float qz) |
---|
[8818] | 316 | { |
---|
| 317 | Vector destCoor = Vector(x, y, z); |
---|
| 318 | Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu); |
---|
[8814] | 319 | |
---|
[8818] | 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; |
---|
[8814] | 325 | |
---|
[8818] | 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 | |
---|
[8814] | 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 | */ |
---|
| 360 | float GenericNPC::crouchTo(float x, float y, float qu, float qx, float qy, float qz) |
---|
[8818] | 361 | { |
---|
| 362 | this->crouchTo(x, y, 0.0f, qu, qx, qy, qz); |
---|
| 363 | } |
---|
[8814] | 364 | |
---|
| 365 | |
---|
| 366 | |
---|
[8817] | 367 | /** |
---|
[8818] | 368 | * crouch to a specific place with direction |
---|
| 369 | * |
---|
| 370 | * @param coor: vector place |
---|
| 371 | * @param dir: direction |
---|
| 372 | * |
---|
| 373 | */ |
---|
| 374 | float 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 | /** |
---|
[8817] | 381 | * stops the generic animation |
---|
| 382 | */ |
---|
| 383 | void GenericNPC::stop() |
---|
[8818] | 384 | { |
---|
| 385 | if( this->currentAnim != NULL) |
---|
| 386 | { |
---|
| 387 | this->currentAnim->stop(); |
---|
| 388 | delete this->currentAnim; |
---|
| 389 | this->currentAnim = NULL; |
---|
[8817] | 390 | |
---|
[8818] | 391 | this->setAnimation(STAND, MD2_ANIM_LOOP); |
---|
| 392 | } |
---|
| 393 | } |
---|
[8817] | 394 | |
---|
[8818] | 395 | |
---|
[8819] | 396 | /** |
---|
| 397 | * lookat a world entity |
---|
| 398 | * @param worldEntity: the worldentity to look at |
---|
| 399 | */ |
---|
[8814] | 400 | float GenericNPC::lookAt(WorldEntity* worldEntity) |
---|
| 401 | {} |
---|
| 402 | |
---|
| 403 | |
---|
[8819] | 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 | */ |
---|
[8814] | 409 | float GenericNPC::talkTo(WorldEntity* worldEntity, int dialogNr) |
---|
| 410 | {} |
---|
| 411 | |
---|
[8819] | 412 | |
---|
| 413 | /** |
---|
| 414 | * world entity to shoot at if there is any weapon on the npc |
---|
| 415 | * @param entity: entity to shoot entity |
---|
| 416 | */ |
---|
[8814] | 417 | void GenericNPC::shootAt(WorldEntity* entity) |
---|
| 418 | {} |
---|
| 419 | |
---|
| 420 | |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | |
---|
| 428 | |
---|
| 429 | /** |
---|
[8514] | 430 | * tick this world entity |
---|
| 431 | * @param time: time in seconds expirded since the last tick |
---|
| 432 | */ |
---|
| 433 | void GenericNPC::tick (float time) |
---|
| 434 | { |
---|
| 435 | if( likely(this->getModel(0) != NULL)) |
---|
| 436 | ((InteractiveModel*)this->getModel(0))->tick(time); |
---|
[8816] | 437 | |
---|
| 438 | // tick this animation |
---|
| 439 | if( this->currentAnim != NULL) |
---|
| 440 | this->currentAnim->tick(time); |
---|
[8514] | 441 | } |
---|
| 442 | |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | void 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 | |
---|