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