-
Notifications
You must be signed in to change notification settings - Fork 0
/
airules.c
executable file
·217 lines (182 loc) · 6.16 KB
/
airules.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Hostile Takeover:
* CS248 Final Project, Fall 2004
*
* Written by Jonathan Reeves
*
* A simple 3D video game demonstrating the use of OpenGL for graphics
* rendering with a number of advanced techniques and optimizations to make
* it interesting. See the README file which came with this package for more
* details. It also includes a list of sources which were consulted while
* building this software package.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "global.h"
#include "aiflock.h"
/* apply the cardinal rules of flocking for the normal case */
void AIFlockApplyRules(ai_t *bot, vector_t *heading, float speed,
vector_t *position, vector_t *accum){
vector_t temp;
player_t *enemy;
float ratio;
float sepDis = 10.0;
float minUrgency = 0.05;
float maxUrgency = 0.3;
accum->x = 0.0;
accum->y = 0.0;
accum->z = 0.0;
/* apply cruising */
enemy = AIGetEnemy();
VectorSubtract(&temp, &bot->destination, &bot->body->position);
VectorSetMagnitude(&temp, minUrgency*bot->cruising);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply separation */
VectorSubtract(&temp, &bot->mate->body->position, &bot->body->position);
ratio = bot->mate_dis / sepDis;
if(ratio < minUrgency)
ratio = minUrgency;
if(ratio > maxUrgency)
ratio = maxUrgency;
ratio = ratio*bot->separation;
if(bot->mate_dis < sepDis){ /* then move away, get the yaw to be opposite */
VectorSetMagnitude(&temp, -ratio);
}
else if(bot->mate_dis > sepDis){ /* then move closer */
VectorSetMagnitude(&temp, ratio);
}
else{ /* right on target */
VectorClear(&temp);
}
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply alignment */
temp = bot->mate->body->forward;
ratio = minUrgency*bot->alignment*speed;
VectorSetMagnitude(&temp, ratio);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply cohesion */
bot->destination = bot->mate->destination;
VectorSubtract(&temp, position, &bot->body->position);
VectorSetMagnitude(&temp, minUrgency*bot->cohesion);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply avoidance */
/* if nearest obstacle/enemy is less than avoidance distance */
/* compute a vector pointing away from the obstacle/enemy */
VectorSubtract(&temp, &enemy->position, &bot->body->position);
if(VectorMagnitude2(&temp) < 1000.0){
VectorSetMagnitude(&temp, maxUrgency*bot->avoidance);
VectorAdd(accum, accum, &temp);
}
}
/* apply the cardinal rules of flocking when you're scared and running */
void AIFlockApplyRulesScared(ai_t *bot, vector_t *heading, float speed,
vector_t *position, vector_t *accum){
vector_t temp;
player_t *enemy;
float ratio;
float sepDis = 10.0;
float minUrgency = 0.05;
float maxUrgency = 0.3;
accum->x = 0.0;
accum->y = 0.0;
accum->z = 0.0;
enemy = AIGetEnemy();
/* apply separation */
VectorSubtract(&temp, &bot->mate->body->position, &bot->body->position);
ratio = bot->mate_dis / sepDis;
if(ratio < minUrgency)
ratio = minUrgency;
if(ratio > maxUrgency)
ratio = maxUrgency;
/* turn down the separation because if we're scared we don't care */
ratio = 0.01*ratio*bot->separation;
if(bot->mate_dis < sepDis){ /* then move away, get the yaw to be opposite */
VectorSetMagnitude(&temp, -ratio);
}
else if(bot->mate_dis > sepDis){ /* then move closer */
VectorSetMagnitude(&temp, ratio);
}
else{ /* right on target */
VectorClear(&temp);
}
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply alignment */
temp = bot->mate->body->forward;
/* turn down the separation because if we're scared we don't care */
ratio = 0.01*minUrgency*bot->alignment*speed;
VectorSetMagnitude(&temp, ratio);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply cohesion turned down */
VectorAdd(&bot->destination, &bot->destination, &bot->mate->destination);
VectorSubtract(&temp, position, &bot->body->position);
VectorSetMagnitude(&temp, 0.01*minUrgency*bot->cohesion);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply avoidance */
/* compute a vector pointing away from the obstacle/enemy */
VectorSubtract(&temp, &enemy->position, &bot->body->position);
VectorSetMagnitude(&temp, maxUrgency*bot->avoidance);
VectorAdd(accum, accum, &temp);
}
/* never finished this... supposed to set up formations */
void AIFlockApplyRulesFormation(ai_t *bot, vector_t *heading, float speed,
vector_t *position, vector_t *accum){
vector_t temp;
player_t *enemy;
float ratio;
float sepDis = 10.0;
float minUrgency = 0.05;
float maxUrgency = 0.3;
accum->x = 0.0;
accum->y = 0.0;
accum->z = 0.0;
/* apply cruising */
enemy = AIGetEnemy();
VectorSubtract(&temp, &bot->destination, &bot->body->position);
VectorSetMagnitude(&temp, minUrgency*bot->cruising);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply separation */
VectorSubtract(&temp, &bot->mate->body->position, &bot->body->position);
ratio = bot->mate_dis / sepDis;
if(ratio < minUrgency)
ratio = minUrgency;
if(ratio > maxUrgency)
ratio = maxUrgency;
ratio = ratio*bot->separation;
if(bot->mate_dis < 1.5*sepDis){ /* then move away */
VectorSetMagnitude(&temp, -ratio);
}
else if(bot->mate_dis > 1.5*sepDis){ /* then move closer */
VectorSetMagnitude(&temp, ratio);
}
else{ /* right on target */
VectorClear(&temp);
}
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply cohesion by going to the correct offset */
bot->destination = bot->mate->destination;
VectorSubtract(&temp, position, &bot->body->position);
VectorSetMagnitude(&temp, minUrgency*bot->cohesion);
VectorAdd(accum, accum, &temp);
VectorNormalize(accum);
/* apply avoidance */
/* if nearest obstacle/enemy is less than avoidance distance */
/* compute a vector pointing away from the obstacle/enemy */
VectorSubtract(&temp, &enemy->position, &bot->body->position);
if(VectorMagnitude2(&temp) < 1000.0){
VectorSetMagnitude(&temp, maxUrgency*bot->avoidance);
VectorAdd(accum, accum, &temp);
}
}