| [10227] | 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 "attack_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" |
|---|
| [10275] | 24 | #include "weapons/weapon.h" |
|---|
| 25 | #include "projectiles/projectile.h" |
|---|
| [10227] | 26 | |
|---|
| 27 | #include "shell_command.h" |
|---|
| 28 | SHELL_COMMAND(setDistanceToPlayer, AttackModule, setDistanceToPlayer); |
|---|
| 29 | SHELL_COMMAND(setDistanceToNPC, AttackModule, setDistanceToNPC); |
|---|
| 30 | SHELL_COMMAND(setMaxAccleartion, AttackModule, setMaxAccleartion); |
|---|
| 31 | SHELL_COMMAND(setTestValue, AttackModule, setTestValue); |
|---|
| 32 | SHELL_COMMAND(setTestValue2, AttackModule, setTestValue2); |
|---|
| 33 | |
|---|
| 34 | float AttackModule::distanceToPlayer=15; |
|---|
| 35 | float AttackModule::distanceToNPC=2; |
|---|
| 36 | float AttackModule::maxAccleration=300.0f; |
|---|
| 37 | float AttackModule::testValue=2; |
|---|
| 38 | float AttackModule::testValue2=40; |
|---|
| 39 | |
|---|
| 40 | void AttackModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; } |
|---|
| 41 | void AttackModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; } |
|---|
| 42 | void AttackModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; } |
|---|
| 43 | void AttackModule::setTestValue(float newValue){ testValue=newValue; } |
|---|
| 44 | void AttackModule::setTestValue2(float newValue){ testValue2=newValue; } |
|---|
| 45 | |
|---|
| 46 | AttackModule::AttackModule() |
|---|
| 47 | { |
|---|
| 48 | tickCount=0; |
|---|
| [10244] | 49 | randomFreq=40; |
|---|
| [10275] | 50 | fireTimeout=1; |
|---|
| [10227] | 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | void AttackModule::process(float dt) |
|---|
| 55 | { |
|---|
| [10515] | 56 | //PRINTF(0)("attack process\n"); |
|---|
| 57 | |
|---|
| [10227] | 58 | if(npc == NULL)return; |
|---|
| 59 | |
|---|
| 60 | Vector tmpVector; |
|---|
| 61 | float tmpFloat; |
|---|
| 62 | Vector npcCollision; |
|---|
| 63 | Vector playerCollision; |
|---|
| 64 | |
|---|
| 65 | weight=1; |
|---|
| 66 | |
|---|
| 67 | //get information about player |
|---|
| 68 | Player* pl = State::getPlayer(); |
|---|
| 69 | if( pl == NULL)return; |
|---|
| 70 | Vector playerPosition = pl->getPlayable()->getAbsCoor(); |
|---|
| 71 | float playerRadius=getRadius( pl->getPlayable() ); |
|---|
| 72 | |
|---|
| 73 | //get information about myself |
|---|
| 74 | Vector myPosition = npc->getAbsCoor(); |
|---|
| 75 | float myRadius = getRadius(npc); |
|---|
| [10349] | 76 | //float vMax=1000.0f/myRadius; |
|---|
| 77 | float vMax=maxSpeed; |
|---|
| 78 | float aMax=1000/myRadius; |
|---|
| [10227] | 79 | |
|---|
| 80 | //anti player collision |
|---|
| 81 | Vector vectorToPlayer = playerPosition - myPosition; |
|---|
| 82 | |
|---|
| 83 | tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer; |
|---|
| 84 | if(tmpFloat<0.1)tmpFloat=0.1; |
|---|
| 85 | playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | //anti NPC collision |
|---|
| 89 | for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it) |
|---|
| 90 | { |
|---|
| [10275] | 91 | if((*it)->isA(Weapon::staticClassID()) )continue; |
|---|
| 92 | if((*it)->isA(Projectile::staticClassID()) )continue; |
|---|
| [10227] | 93 | if(*it==npc)continue; |
|---|
| 94 | |
|---|
| 95 | tmpVector=myPosition-(*it)->getAbsCoor(); |
|---|
| 96 | tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it); |
|---|
| 97 | |
|---|
| 98 | if(tmpFloat<0.1)tmpFloat=0.1; |
|---|
| 99 | tmpVector=tmpVector/(tmpFloat*tmpFloat); |
|---|
| 100 | |
|---|
| 101 | npcCollision=npcCollision+tmpVector; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | //random movement |
|---|
| 106 | //randomFreq=testValue2; |
|---|
| [10244] | 107 | if(++tickCount>=randomFreq){ |
|---|
| [10227] | 108 | tickCount=0; |
|---|
| 109 | int x = (rand()%101)-50; //-50-50 |
|---|
| 110 | int z = (rand()%101)-50; //-50-50 |
|---|
| 111 | randomVector=Vector(x,0,z); |
|---|
| 112 | randomFreq=(rand()%81)+70; //70-150 Ticks |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | //calculate correction vector |
|---|
| 118 | Vector vectorToDestination=destination-myPosition; |
|---|
| 119 | |
|---|
| [10515] | 120 | Vector correction= playerCollision*40*3 *6/myRadius |
|---|
| [10266] | 121 | + npcCollision*50*3 *6/myRadius |
|---|
| [10227] | 122 | + destinationMovement*2//-movement |
|---|
| 123 | + (vectorToDestination-movement)*3 |
|---|
| 124 | + (randomVector * testValue); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | correction.y=0; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | //limit accleration |
|---|
| 131 | float correctionLen=correction.len(); |
|---|
| 132 | if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt; |
|---|
| 133 | movement+=correction; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | //limit speed |
|---|
| 137 | float movementLen=movement.len(); |
|---|
| [10349] | 138 | if(movementLen>vMax)movement=movement/movementLen*vMax; |
|---|
| [10227] | 139 | |
|---|
| 140 | |
|---|
| 141 | //move NPC... |
|---|
| 142 | npc->shiftCoor(movement * dt); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | //rotate NPC |
|---|
| 146 | view = target->getAbsCoor()-myPosition; |
|---|
| [10283] | 147 | Vector randomView=view.cross(Vector(0,1,0)).getNormalized(); |
|---|
| 148 | randomView=randomView*((rand()%4)-2); |
|---|
| 149 | |
|---|
| 150 | view = target->getAbsCoor()+randomView-myPosition; |
|---|
| [10515] | 151 | view = view.cross( Vector(0,-1,0) ).getNormalized(); |
|---|
| [10227] | 152 | |
|---|
| [10349] | 153 | npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),8/myRadius); |
|---|
| [10275] | 154 | |
|---|
| 155 | |
|---|
| 156 | if(npc->isA(NPC::staticClassID()) ){ |
|---|
| 157 | fireTimeout-=dt; |
|---|
| 158 | if(fireTimeout<=0){ |
|---|
| [10279] | 159 | fireTimeout=(rand()%21)/10+1; |
|---|
| [10515] | 160 | //std::cout << "Fiiiiirrreee!\n"; |
|---|
| [10275] | 161 | NPC* npc2 = static_cast<NPC*>(npc); |
|---|
| 162 | npc2->fire(); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| [10227] | 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|