- Timestamp:
- Dec 19, 2006, 11:38:01 PM (18 years ago)
- Location:
- branches/ai/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ai/src/ai/ai_module.h
r10062 r10112 3 3 #define _AI_MODULE_H 4 4 5 6 class NPC2; 5 7 class AITeamMember; 8 class WorldEntity; 9 6 10 7 11 class AIModule{ … … 10 14 virtual ~AIModule() {} 11 15 virtual void process() {} 16 virtual void process(float dt) {} 12 17 void setDifficulty(int newDifficulty); 13 18 void setOwner(AITeamMember* newOwner); … … 15 20 int difficulty; 16 21 AITeamMember* owner; 22 NPC2* myNPC; 17 23 }; 18 24 -
branches/ai/src/ai/movement_module.cc
r10075 r10112 25 25 26 26 #include "shell_command.h" 27 SHELL_COMMAND(aiacc, MovementModule, setAccleration); 28 29 30 //class AIEngine; 31 32 MovementModule::MovementModule(){ 33 //MovementModule::aa=100.0f; 34 //a=100.0f; 35 //std::cout << "MovementModule created...\n"; 36 } 37 38 MovementModule::~MovementModule(){ 39 } 40 41 42 void MovementModule::setAccleration(float newValue) 43 { 44 std::cout << "Setting a to: "<< newValue << "\n"; 45 //MovementModule::aa=newValue; 46 } 47 48 49 50 51 float MovementModule::getSize(WorldEntity* object) 52 { 53 //does not work... 27 SHELL_COMMAND(setDistanceToPlayer, MovementModule, setDistanceToPlayer); 28 SHELL_COMMAND(setDistanceToNPC, MovementModule, setDistanceToNPC); 29 SHELL_COMMAND(setMaxAccleartion, MovementModule, setMaxAccleartion); 30 31 float MovementModule::distanceToPlayer=15; 32 float MovementModule::distanceToNPC=2; 33 float MovementModule::maxAccleration=200.0f; 34 35 void MovementModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; } 36 void MovementModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; } 37 void MovementModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; } 38 39 40 41 42 43 44 std::vector<Vector> MovementModule::hidingPoint; 45 std::vector<float> MovementModule::hidingPointSize; 46 47 std::vector<NPC2*> MovementModule::npcList; 48 std::vector<Vector> MovementModule::npcPosition; 49 std::vector<float> MovementModule::npcRadius; 50 std::vector<int> MovementModule::npcSwarm; 51 std::vector<int> MovementModule::npcTeam; 52 53 Vector MovementModule::playerPosition; 54 Vector MovementModule::playerMovement; 55 float MovementModule::playerRadius; 56 57 std::vector<Vector> MovementModule::swarmCenter; 58 std::vector<int> MovementModule::swarmMemberCount; 59 60 61 62 63 64 MovementModule::MovementModule(NPC2* object){ this->myNPC=object; } 65 MovementModule::~MovementModule(){} 66 67 68 69 70 float MovementModule::getRadius(WorldEntity* object) 71 { 54 72 AABB* aabb = object->getModelAABB(); 55 Vector a = aabb->getAxisX(); 56 Vector b = aabb->getAxisY(); 57 Vector c = aabb->getAxisZ(); 58 59 float da=a.len(); 60 float db=b.len(); 61 float dc=c.len(); 62 float size; 63 64 if(da>db){ 65 size=(dc>da)?dc:da; 73 if( aabb == NULL)return -1; 74 75 float a = aabb->halfLength[0]; 76 float b = aabb->halfLength[1]; 77 float c = aabb->halfLength[2]; 78 79 if(a>b){ 80 return (c>a)?c:a; 66 81 }else{ 67 size=(dc>db)?dc:db; 68 } 69 70 return size; 71 } 82 return (c>b)?c:b; 83 } 84 } 85 86 87 88 89 void MovementModule::collectInformation(float dt) 90 { 91 //return if already processed.. 92 if(dt==this->oldDT)return; 93 this->oldDT=dt; 94 95 96 //clear old Information.. 97 hidingPoint.clear(); 98 hidingPointSize.clear(); 99 100 npcList.clear(); 101 npcPosition.clear(); 102 npcRadius.clear(); 103 npcSwarm.clear(); 104 npcTeam.clear(); 105 106 swarmCenter.clear(); 107 swarmMemberCount.clear(); 108 109 110 //get all NPCs.. 111 for(ObjectList<NPC2>::const_iterator it = NPC2::objectList().begin(); it != NPC2::objectList().end(); ++it) 112 { 113 npcList.push_back(*it); 114 npcRadius.push_back(getRadius(*it)); 115 npcPosition.push_back((*it)->getAbsCoor()); 116 npcSwarm.push_back((*it)->swarm); 117 npcTeam.push_back((*it)->team); 118 } 119 120 121 //Swarm Information 122 unsigned int tmpSwarm; 123 for(unsigned int i=0;i<npcList.size();i++){ 124 tmpSwarm = npcSwarm.at(i); 125 if(tmpSwarm > swarmMemberCount.size()){ 126 //swarmMemberCount.insert(swarmMemberCount.size(), tmpSwarm - swarmMemberCount.size(), 0); 127 //swarmCenter.insert(swarmCenter.size(), tmpSwarm - swarmCenter.size(), Vector(0,0,0)); 128 } 129 //swarmMemberCount.at(tmpSwarm)++; 130 //swarmCenter.at(tmpSwarm)=swarmCenter.at(tmpSwarm)+npcPosition.at(i); 131 } 132 for(unsigned int i=0;i<swarmCenter.size();i++){ 133 //swarmCenter.at(i)=swarmCenter.at(i)/swarmMemberCount.at(i); 134 } 135 136 137 //get information about Player 138 Player* pl = State::getPlayer(); 139 if( pl != NULL){ 140 playerPosition = pl->getPlayable()->getAbsCoor(); 141 playerRadius=getRadius( pl->getPlayable() ); 142 //PRINTF(0)("Player Radius: %f\n",playerRadius); 143 } 144 145 146 //calculate hiding Points.. 147 148 } 149 150 151 152 153 154 155 156 157 158 72 159 73 160 … … 76 163 void MovementModule::process() 77 164 { 78 float dt=AIEngine::getInstance()->dtS; 79 if( owner == NULL) return; 80 Vector myAbsPos = owner->getAbsCoor(); 81 82 //PRINTF(0)(" NPC abs coor: %f, %f, %f\n", myAbsPos.x, myAbsPos.y, myAbsPos.z); 83 84 Player* pl = State::getPlayer(); 85 if( pl == NULL) return; 86 Vector playerAbsPos = pl->getPlayable()->getAbsCoor(); 87 88 //PRINTF(0)(" Player abs coor: %f, %f, %f\n", playerAbsPos.x, playerAbsPos.y, playerAbsPos.z); 89 90 //////////// 91 92 //if(aa<0)aa=100.0f; 93 float a=200.0f; 165 this->process(AIEngine::getInstance()->dtS); 166 } 167 168 169 void MovementModule::process(float dt) 170 { 171 collectInformation(dt); 172 173 if(myNPC == NULL)return; 174 175 Vector myPosition = myNPC->getAbsCoor(); 176 float myRadius = getRadius(myNPC); 177 int mySwarm = myNPC->swarm; 178 int myTeam = myNPC->team; 179 180 Vector vectorToPlayer = playerPosition - myPosition; 181 182 Vector tmpVector; 183 float tmpFloat; 184 185 Vector npcCollision; 186 Vector swarmVector; 187 Vector playerCollision; 188 189 190 191 //float a=200.0f; 94 192 float vMax=200.0f; 95 96 //float keepDist= this->getSize(owner);//+ this->getSize(pl->getPlayable()); 97 //std::cout << "Distance: " << keepDist << "\n"; 98 99 100 Vector vectorToPlayer = playerAbsPos - myAbsPos; 101 float dist=vectorToPlayer.len(); 102 //Vector nVectorToPlayer=vectorToPlayer/dist*(dist-keepDist); 103 104 105 // std::vector<Vector> collisonCorrection; 106 // get all npcs 107 108 Vector tmpNPCpos; 109 float tmpDist; 110 Vector tmpCorrectionVect; 111 Vector antiNPCCollision; 112 113 for (ObjectList<NPC2>::const_iterator it = NPC2::objectList().begin(); 114 it != NPC2::objectList().end(); 115 ++it) 193 //float safetyDistance=2.0f; 194 195 196 //Anti Player Collision 197 tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer; 198 if(tmpFloat<0.1)tmpFloat=0.1; 199 playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1); 200 201 202 203 for (unsigned int i=0;i<npcList.size();i++) 116 204 { 117 if(*it==owner)continue; 118 tmpNPCpos=(*it)->getAbsCoor(); 119 tmpCorrectionVect = myAbsPos-tmpNPCpos; 120 121 tmpDist=tmpCorrectionVect.len()-16; 122 if(tmpDist<0.1)tmpDist=0.1; 123 tmpCorrectionVect=tmpCorrectionVect/(tmpDist*tmpDist); 124 125 antiNPCCollision=antiNPCCollision+tmpCorrectionVect; 126 } 127 128 129 130 Vector antiPlayerCollision=vectorToPlayer*(-1); 131 tmpDist=antiPlayerCollision.len()-45; 132 if(tmpDist<0.1)tmpDist=0.1; 133 antiPlayerCollision=antiPlayerCollision/(tmpDist*tmpDist); 134 135 Vector correction=antiPlayerCollision*50+antiNPCCollision*50+vectorToPlayer-v; 136 //Vector nCorrection= 205 if(npcList.at(i)==myNPC)continue; 206 207 //Anti NPC Collision 208 tmpVector=myPosition-npcPosition.at(i); 209 tmpFloat=tmpVector.len()-myRadius-npcRadius.at(i)-distanceToNPC; 210 211 if(tmpFloat<0.1)tmpFloat=0.1; 212 tmpVector=tmpVector/(tmpFloat*tmpFloat); 213 214 npcCollision=npcCollision+tmpVector; 215 216 //Schwarmverhalten 217 if(npcSwarm.at(i) == mySwarm){ 218 219 220 } 221 } 222 223 224 225 226 227 Vector correction=playerCollision*50+npcCollision*50+vectorToPlayer-myMovement; 137 228 correction.y=0; 138 229 float correctionLen=correction.len(); 139 if(correctionLen> a*dt)correction=correction/correctionLen*a*dt;140 v+=correction;141 142 float vLen=v.len();143 if( vLen>vMax)v/vLen*vMax;230 if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt; 231 myMovement+=correction; 232 233 float movementLen=myMovement.len(); 234 if(movementLen>vMax)myMovement/movementLen*vMax; 144 235 145 236 //Move NPC... 146 owner->shiftCoor(v*dt);237 myNPC->shiftCoor(myMovement * dt); 147 238 148 239 //Rotate NPC 149 Vector view = v+correction;240 Vector view = myMovement; 150 241 //if(vectorToPlayer.dot(v)<0){ 151 242 // view = v.cross( Vector(0,1,0) ).getNormalized(); 152 243 //}else{ 153 view = v.cross( Vector(0,-1,0) ).getNormalized();244 view = myMovement.cross( Vector(0,-1,0) ).getNormalized(); 154 245 //} 155 246 //if(dist<keepDist)view=view*-1; 156 // owner->setAbsDir( Quaternion( view, Vector(0,1,0)));157 owner->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),1.1);158 } 159 160 247 //myNPC->setAbsDir( Quaternion( view, Vector(0,1,0))); 248 myNPC->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3); 249 } 250 251 -
branches/ai/src/ai/movement_module.h
r10075 r10112 6 6 #include "player.h" 7 7 #include "playable.h" 8 //#include "npcs/npc_test.h" 9 class NPC2; 10 8 11 9 12 class MovementModule : public AIModule{ 10 13 11 14 public: 12 MovementModule(); 15 MovementModule() {} 16 MovementModule(NPC2* object); 13 17 virtual ~MovementModule(); 14 18 virtual void process(); 15 static void setAccleration(float newValue); 19 virtual void process(float dt); 20 21 static void setDistanceToPlayer(float newValue); 22 static void setDistanceToNPC(float newValue); 23 static void setMaxAccleartion(float newValue); 24 16 25 private: 17 Vector v; 18 float getSize(WorldEntity* object); 19 static float aa; 26 void collectInformation(float dt); 27 28 29 static std::vector<Vector> hidingPoint; 30 static std::vector<float> hidingPointSize; 31 32 static std::vector<NPC2*> npcList; 33 static std::vector<Vector> npcPosition; 34 static std::vector<float> npcRadius; 35 static std::vector<int> npcSwarm; 36 static std::vector<int> npcTeam; 37 38 static Vector playerPosition; 39 static Vector playerMovement; 40 static float playerRadius; 41 42 static std::vector<Vector> swarmCenter; 43 static std::vector<int> swarmMemberCount; 44 45 Vector myMovement; 46 float myMaxAccleration; 47 float myMaxSpeed; 48 49 50 float getRadius(WorldEntity* object); 51 static float aa; 52 float oldDT; 53 54 static float maxAccleration; 55 static float distanceToPlayer; 56 static float distanceToNPC; 20 57 }; 21 58 -
branches/ai/src/world_entities/npcs/npc_test.cc
r10071 r10112 32 32 33 33 #include "class_id_DEPRECATED.h" 34 #include "movement_module.h" 35 #include "ai_module.h" 36 34 37 35 38 ObjectListDefinitionID(NPC2, CL_NPC_TEST2); … … 39 42 NPC2::NPC2(const TiXmlElement* root) : NPC(NULL) 40 43 { 41 this->registerObject(this, NPC2::_objectList); 42 43 if (root != NULL)this->loadParams(root); 44 45 addToTeam(0); 44 this->registerObject(this, NPC2::_objectList); 45 if (root != NULL)this->loadParams(root); 46 47 this->aiModule=new MovementModule(this); 46 48 } 47 49 … … 60 62 void NPC2::tick(float dt) 61 63 { 62 64 // animating the md2 model 63 65 if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(MD2Model::staticClassID())) 64 ((MD2Model*)this->getModel(0))->tick(dt); 66 ((MD2Model*)this->getModel(0))->tick(dt); 67 68 // processing AI 69 //PRINTF(0)("Processing NPC\n"); 70 if(this->aiModule!=NULL)this->aiModule->process(dt); 65 71 } 66 72 -
branches/ai/src/world_entities/npcs/npc_test.h
r10045 r10112 4 4 5 5 #include "npc.h" 6 #include "ai_module.h" 6 7 7 8 class AI; 8 9 class Shader; 10 class AIModule; 9 11 10 12 class NPC2 : public NPC { … … 17 19 virtual void tick(float dt); 18 20 19 private: 20 21 int team; 22 int swarm; 23 int difficulty; 24 AIModule* aiModule; 21 25 }; 22 26
Note: See TracChangeset
for help on using the changeset viewer.