| 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: Thomas Fahrni | 
|---|
| 13 | co-programmer: | 
|---|
| 14 | */ | 
|---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI | 
|---|
| 16 |  | 
|---|
| 17 | #include "movement_module.h" | 
|---|
| 18 | #include "ai_engine.h" | 
|---|
| 19 | #include "state.h" | 
|---|
| 20 | #include "debug.h" | 
|---|
| 21 | #include "player.h" | 
|---|
| 22 | #include "playable.h" | 
|---|
| 23 | #include "npcs/npc_test.h" | 
|---|
| 24 | #include "weapons/weapon.h" | 
|---|
| 25 | #include "projectiles/projectile.h" | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | #include "shell_command.h" | 
|---|
| 29 | SHELL_COMMAND(setDistanceToPlayer, MovementModule, setDistanceToPlayer); | 
|---|
| 30 | SHELL_COMMAND(setDistanceToNPC, MovementModule, setDistanceToNPC); | 
|---|
| 31 | SHELL_COMMAND(setMaxAccleartion, MovementModule, setMaxAccleartion); | 
|---|
| 32 | SHELL_COMMAND(setTestValue, MovementModule, setTestValue); | 
|---|
| 33 | SHELL_COMMAND(setTestValue2, MovementModule, setTestValue2); | 
|---|
| 34 |  | 
|---|
| 35 | float MovementModule::distanceToPlayer=30; | 
|---|
| 36 | float MovementModule::distanceToNPC=2; | 
|---|
| 37 | float MovementModule::maxAccleration=250.0f; | 
|---|
| 38 | float MovementModule::testValue=2; | 
|---|
| 39 | float MovementModule::testValue2=40; | 
|---|
| 40 |  | 
|---|
| 41 | void MovementModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; } | 
|---|
| 42 | void MovementModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; } | 
|---|
| 43 | void MovementModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; } | 
|---|
| 44 | void MovementModule::setTestValue(float newValue){ testValue=newValue; } | 
|---|
| 45 | void MovementModule::setTestValue2(float newValue){ testValue2=newValue; } | 
|---|
| 46 |  | 
|---|
| 47 | MovementModule::MovementModule() | 
|---|
| 48 | { | 
|---|
| 49 | tickCount=0; | 
|---|
| 50 | randomFreq=100; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | MovementModule::MovementModule(WorldEntity* object) | 
|---|
| 54 | { | 
|---|
| 55 | this->npc=object; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | void MovementModule::process(float dt) | 
|---|
| 59 | { | 
|---|
| 60 | if(npc == NULL)return; | 
|---|
| 61 |  | 
|---|
| 62 | Vector tmpVector; | 
|---|
| 63 | float tmpFloat; | 
|---|
| 64 | Vector npcCollision; | 
|---|
| 65 | Vector playerCollision; | 
|---|
| 66 |  | 
|---|
| 67 | weight=1; | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | //get information about player | 
|---|
| 71 | Player* pl = State::getPlayer(); | 
|---|
| 72 | if( pl == NULL)return; | 
|---|
| 73 | WorldEntity* playable = pl->getPlayable(); | 
|---|
| 74 | if( playable == NULL)return; | 
|---|
| 75 |  | 
|---|
| 76 | Vector playerPosition = pl->getPlayable()->getAbsCoor(); | 
|---|
| 77 | float playerRadius=getRadius( pl->getPlayable() ); | 
|---|
| 78 |  | 
|---|
| 79 | //get information about myself | 
|---|
| 80 | Vector myPosition = npc->getAbsCoor(); | 
|---|
| 81 | float myRadius = getRadius(npc); | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | float aMax=maxAccleration; | 
|---|
| 85 | //float vMax=1000.0f/myRadius; | 
|---|
| 86 | float vMax=maxSpeed; | 
|---|
| 87 |  | 
|---|
| 88 | //anti player collision | 
|---|
| 89 | Vector vectorToPlayer = playerPosition - myPosition; | 
|---|
| 90 |  | 
|---|
| 91 | tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer; | 
|---|
| 92 | if(tmpFloat<0.1)tmpFloat=0.1; | 
|---|
| 93 | playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1); | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 | //anti NPC collision | 
|---|
| 97 | for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it) | 
|---|
| 98 | { | 
|---|
| 99 | if((*it)->isA(Weapon::staticClassID()) )continue; | 
|---|
| 100 | if((*it)->isA(Projectile::staticClassID()) )continue; | 
|---|
| 101 | if(*it==npc)continue; | 
|---|
| 102 |  | 
|---|
| 103 | tmpVector=myPosition-(*it)->getAbsCoor(); | 
|---|
| 104 | tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it); | 
|---|
| 105 |  | 
|---|
| 106 | if(tmpFloat<0.1)tmpFloat=0.1; | 
|---|
| 107 | tmpVector=tmpVector/(tmpFloat*tmpFloat); | 
|---|
| 108 |  | 
|---|
| 109 | npcCollision=npcCollision+tmpVector; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 | //calculate correction vector | 
|---|
| 115 | Vector vectorToDestination=destination-myPosition; | 
|---|
| 116 |  | 
|---|
| 117 | Vector correction=              playerCollision*50*3 *6/myRadius | 
|---|
| 118 | +       npcCollision*50*3 *6/myRadius | 
|---|
| 119 | +       Vector(0,0,0) | 
|---|
| 120 | +       destinationMovement*2//-movement | 
|---|
| 121 | +       (vectorToDestination-movement)*3; | 
|---|
| 122 |  | 
|---|
| 123 | correction.y=0; | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | //limit accleration | 
|---|
| 127 | float correctionLen=correction.len(); | 
|---|
| 128 | if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt; | 
|---|
| 129 | movement+=correction; | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | //limit speed | 
|---|
| 133 | float movementLen=movement.len(); | 
|---|
| 134 | if(movementLen>vMax)movement=movement/movementLen*vMax; | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 | //move NPC... | 
|---|
| 138 | npc->shiftCoor(movement * dt); | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | //rotate NPC | 
|---|
| 142 | view = movement.cross( Vector(0,1,0) ).getNormalized(); | 
|---|
| 143 | npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),10/myRadius); | 
|---|
| 144 |  | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 |  | 
|---|