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