Debian Codegraph @

code/botlib/be_aas_move.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /*****************************************************************************
24 * name: be_aas_move.c
25 *
26 * desc: AAS
27 *
28 * $Archive: /MissionPack/code/botlib/be_aas_move.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_script.h"
35 #include "l_precomp.h"
36 #include "l_struct.h"
37 #include "l_libvar.h"
38 #include "aasfile.h"
39 #include "botlib.h"
40 #include "be_aas.h"
41 #include "be_aas_funcs.h"
42 #include "be_aas_def.h"
43
44 extern botlib_import_t botimport;
45
46 aas_settings_t aassettings;
47
48 //#define AAS_MOVE_DEBUG
49
50 //===========================================================================
51 //
52 // Parameter: -
53 // Returns: -
54 // Changes Globals: -
55 //===========================================================================
56 int AAS_DropToFloor(vec3_t origin, vec3_t mins, vec3_t maxs)
57 {
58 vec3_t end;
59 bsp_trace_t trace;
60
61 VectorCopy(origin, end);
62 end[2] -= 100;
63 trace = AAS_Trace(origin, mins, maxs, end, 0, CONTENTS_SOLID);
64 if (trace.startsolid) return qfalse;
65 VectorCopy(trace.endpos, origin);
66 return qtrue;
67 } //end of the function AAS_DropToFloor
68 //===========================================================================
69 //
70 // Parameter: -
71 // Returns: -
72 // Changes Globals: -
73 //===========================================================================
74 void AAS_InitSettings(void)
75 {
76 aassettings.phys_gravitydirection[0] = 0;
77 aassettings.phys_gravitydirection[1] = 0;
78 aassettings.phys_gravitydirection[2] = -1;
79 aassettings.phys_friction = LibVarValue("phys_friction", "6");
80 aassettings.phys_stopspeed = LibVarValue("phys_stopspeed", "100");
81 aassettings.phys_gravity = LibVarValue("phys_gravity", "800");
82 aassettings.phys_waterfriction = LibVarValue("phys_waterfriction", "1");
83 aassettings.phys_watergravity = LibVarValue("phys_watergravity", "400");
84 aassettings.phys_maxvelocity = LibVarValue("phys_maxvelocity", "320");
85 aassettings.phys_maxwalkvelocity = LibVarValue("phys_maxwalkvelocity", "320");
86 aassettings.phys_maxcrouchvelocity = LibVarValue("phys_maxcrouchvelocity", "100");
87 aassettings.phys_maxswimvelocity = LibVarValue("phys_maxswimvelocity", "150");
88 aassettings.phys_walkaccelerate = LibVarValue("phys_walkaccelerate", "10");
89 aassettings.phys_airaccelerate = LibVarValue("phys_airaccelerate", "1");
90 aassettings.phys_swimaccelerate = LibVarValue("phys_swimaccelerate", "4");
91 aassettings.phys_maxstep = LibVarValue("phys_maxstep", "19");
92 aassettings.phys_maxsteepness = LibVarValue("phys_maxsteepness", "0.7");
93 aassettings.phys_maxwaterjump = LibVarValue("phys_maxwaterjump", "18");
94 aassettings.phys_maxbarrier = LibVarValue("phys_maxbarrier", "33");
95 aassettings.phys_jumpvel = LibVarValue("phys_jumpvel", "270");
96 aassettings.phys_falldelta5 = LibVarValue("phys_falldelta5", "40");
97 aassettings.phys_falldelta10 = LibVarValue("phys_falldelta10", "60");
98 aassettings.rs_waterjump = LibVarValue("rs_waterjump", "400");
99 aassettings.rs_teleport = LibVarValue("rs_teleport", "50");
100 aassettings.rs_barrierjump = LibVarValue("rs_barrierjump", "100");
101 aassettings.rs_startcrouch = LibVarValue("rs_startcrouch", "300");
102 aassettings.rs_startgrapple = LibVarValue("rs_startgrapple", "500");
103 aassettings.rs_startwalkoffledge = LibVarValue("rs_startwalkoffledge", "70");
104 aassettings.rs_startjump = LibVarValue("rs_startjump", "300");
105 aassettings.rs_rocketjump = LibVarValue("rs_rocketjump", "500");
106 aassettings.rs_bfgjump = LibVarValue("rs_bfgjump", "500");
107 aassettings.rs_jumppad = LibVarValue("rs_jumppad", "250");
108 aassettings.rs_aircontrolledjumppad = LibVarValue("rs_aircontrolledjumppad", "300");
109 aassettings.rs_funcbob = LibVarValue("rs_funcbob", "300");
110 aassettings.rs_startelevator = LibVarValue("rs_startelevator", "50");
111 aassettings.rs_falldamage5 = LibVarValue("rs_falldamage5", "300");
112 aassettings.rs_falldamage10 = LibVarValue("rs_falldamage10", "500");
113 aassettings.rs_maxfallheight = LibVarValue("rs_maxfallheight", "0");
114 aassettings.rs_maxjumpfallheight = LibVarValue("rs_maxjumpfallheight", "450");
115 } //end of the function AAS_InitSettings
116 //===========================================================================
117 // returns qtrue if the bot is against a ladder
118 //
119 // Parameter: -
120 // Returns: -
121 // Changes Globals: -
122 //===========================================================================
123 int AAS_AgainstLadder(vec3_t origin)
124 {
125 int areanum, i, facenum, side;
126 vec3_t org;
127 aas_plane_t *plane;
128 aas_face_t *face;
129 aas_area_t *area;
130
131 VectorCopy(origin, org);
132 areanum = AAS_PointAreaNum(org);
133 if (!areanum)
134 {
135 org[0] += 1;
136 areanum = AAS_PointAreaNum(org);
137 if (!areanum)
138 {
139 org[1] += 1;
140 areanum = AAS_PointAreaNum(org);
141 if (!areanum)
142 {
143 org[0] -= 2;
144 areanum = AAS_PointAreaNum(org);
145 if (!areanum)
146 {
147 org[1] -= 2;
148 areanum = AAS_PointAreaNum(org);
149 } //end if
150 } //end if
151 } //end if
152 } //end if
153 //if in solid... wrrr shouldn't happen
154 if (!areanum) return qfalse;
155 //if not in a ladder area
156 if (!(aasworld.areasettings[areanum].areaflags & AREA_LADDER)) return qfalse;
157 //if a crouch only area
158 if (!(aasworld.areasettings[areanum].presencetype & PRESENCE_NORMAL)) return qfalse;
159 //
160 area = &aasworld.areas[areanum];
161 for (i = 0; i < area->numfaces; i++)
162 {
163 facenum = aasworld.faceindex[area->firstface + i];
164 side = facenum < 0;
165 face = &aasworld.faces[abs(facenum)];
166 //if the face isn't a ladder face
167 if (!(face->faceflags & FACE_LADDER)) continue;
168 //get the plane the face is in
169 plane = &aasworld.planes[face->planenum ^ side];
170 //if the origin is pretty close to the plane
171 if (fabsf(DotProduct(plane->normal, origin) - plane->dist) < 3)
172 {
173 if (AAS_PointInsideFace(abs(facenum), origin, 0.1f)) return qtrue;
174 } //end if
175 } //end for
176 return qfalse;
177 } //end of the function AAS_AgainstLadder
178 //===========================================================================
179 // returns qtrue if the bot is on the ground
180 //
181 // Parameter: -
182 // Returns: -
183 // Changes Globals: -
184 //===========================================================================
185 int AAS_OnGround(vec3_t origin, int presencetype, int passent)
186 {
187 aas_trace_t trace;
188 vec3_t end, up = {0, 0, 1};
189 aas_plane_t *plane;
190
191 VectorCopy(origin, end);
192 end[2] -= 10;
193
194 trace = AAS_TraceClientBBox(origin, end, presencetype, passent);
195
196 //if in solid
197 if (trace.startsolid) return qfalse;
198 //if nothing hit at all
199 if (trace.fraction >= 1.0) return qfalse;
200 //if too far from the hit plane
201 if (origin[2] - trace.endpos[2] > 10) return qfalse;
202 //check if the plane isn't too steep
203 plane = AAS_PlaneFromNum(trace.planenum);
204 if (DotProduct(plane->normal, up) < aassettings.phys_maxsteepness) return qfalse;
205 //the bot is on the ground
206 return qtrue;
207 } //end of the function AAS_OnGround
208 //===========================================================================
209 // returns qtrue if a bot at the given position is swimming
210 //
211 // Parameter: -
212 // Returns: -
213 // Changes Globals: -
214 //===========================================================================
215 int AAS_Swimming(vec3_t origin)
216 {
217 vec3_t testorg;
218
219 VectorCopy(origin, testorg);
220 testorg[2] -= 2;
221 if (AAS_PointContents(testorg) & (CONTENTS_LAVA|CONTENTS_SLIME|CONTENTS_WATER)) return qtrue;
222 return qfalse;
223 } //end of the function AAS_Swimming
224 //===========================================================================
225 //
226 // Parameter: -
227 // Returns: -
228 // Changes Globals: -
229 //===========================================================================
230 static vec3_t VEC_UP = {0, -1, 0};
231 static vec3_t MOVEDIR_UP = {0, 0, 1};
232 static vec3_t VEC_DOWN = {0, -2, 0};
233 static vec3_t MOVEDIR_DOWN = {0, 0, -1};
234
235 void AAS_SetMovedir(vec3_t angles, vec3_t movedir)
236 {
237 if (VectorCompare(angles, VEC_UP))
238 {
239 VectorCopy(MOVEDIR_UP, movedir);
240 } //end if
241 else if (VectorCompare(angles, VEC_DOWN))
242 {
243 VectorCopy(MOVEDIR_DOWN, movedir);
244 } //end else if
245 else
246 {
247 AngleVectors(angles, movedir, NULL, NULL);
248 } //end else
249 } //end of the function AAS_SetMovedir
250 //===========================================================================
251 //
252 // Parameter: -
253 // Returns: -
254 // Changes Globals: -
255 //===========================================================================
256 void AAS_JumpReachRunStart(aas_reachability_t *reach, vec3_t runstart)
257 {
258 vec3_t hordir, start, cmdmove;
259 aas_clientmove_t move;
260
261 //
262 hordir[0] = reach->start[0] - reach->end[0];
263 hordir[1] = reach->start[1] - reach->end[1];
264 hordir[2] = 0;
265 VectorNormalize(hordir);
266 //start point
267 VectorCopy(reach->start, start);
268 start[2] += 1;
269 //get command movement
270 VectorScale(hordir, 400, cmdmove);
271 //
272 AAS_PredictClientMovement(&move, -1, start, PRESENCE_NORMAL, qtrue,
273 vec3_origin, cmdmove, 1, 2, 0.1f,
274 SE_ENTERWATER|SE_ENTERSLIME|SE_ENTERLAVA|
275 SE_HITGROUNDDAMAGE|SE_GAP, 0, qfalse);
276 VectorCopy(move.endpos, runstart);
277 //don't enter slime or lava and don't fall from too high
278 if (move.stopevent & (SE_ENTERSLIME|SE_ENTERLAVA|SE_HITGROUNDDAMAGE))
279 {
280 VectorCopy(start, runstart);
281 } //end if
282 } //end of the function AAS_JumpReachRunStart
283 //===========================================================================
284 // returns the Z velocity when rocket jumping at the origin
285 //
286 // Parameter: -
287 // Returns: -
288 // Changes Globals: -
289 //===========================================================================
290 float AAS_WeaponJumpZVelocity(vec3_t origin, float radiusdamage)
291 {
292 vec3_t kvel, v, start, end, forward, right, viewangles, dir;
293 float mass, knockback, points;
294 vec3_t rocketoffset = {8, 8, -8};
295 vec3_t botmins = {-16, -16, -24};
296 vec3_t botmaxs = {16, 16, 32};
297 bsp_trace_t bsptrace;
298
299 //look down (90 degrees)
300 viewangles[PITCH] = 90;
301 viewangles[YAW] = 0;
302 viewangles[ROLL] = 0;
303 //get the start point shooting from
304 VectorCopy(origin, start);
305 start[2] += 8; //view offset Z
306 AngleVectors(viewangles, forward, right, NULL);
307 start[0] += forward[0] * rocketoffset[0] + right[0] * rocketoffset[1];
308 start[1] += forward[1] * rocketoffset[0] + right[1] * rocketoffset[1];
309 start[2] += forward[2] * rocketoffset[0] + right[2] * rocketoffset[1] + rocketoffset[2];
310 //end point of the trace
311 VectorMA(start, 500, forward, end);
312 //trace a line to get the impact point
313 bsptrace = AAS_Trace(start, NULL, NULL, end, 1, CONTENTS_SOLID);
314 //calculate the damage the bot will get from the rocket impact
315 VectorAdd(botmins, botmaxs, v);
316 VectorMA(origin, 0.5, v, v);
317 VectorSubtract(bsptrace.endpos, v, v);
318 //
319 points = radiusdamage - 0.5 * VectorLength(v);
320 if (points < 0) points = 0;
321 //the owner of the rocket gets half the damage
322 points *= 0.5;
323 //mass of the bot (p_client.c: PutClientInServer)
324 mass = 200;
325 //knockback is the same as the damage points
326 knockback = points;
327 //direction of the damage (from trace.endpos to bot origin)
328 VectorSubtract(origin, bsptrace.endpos, dir);
329 VectorNormalize(dir);
330 //damage velocity
331 VectorScale(dir, 1600.0 * (float)knockback / mass, kvel); //the rocket jump hack...
332 //rocket impact velocity + jump velocity
333 return kvel[2] + aassettings.phys_jumpvel;
334 } //end of the function AAS_WeaponJumpZVelocity
335 //===========================================================================
336 //
337 // Parameter: -
338 // Returns: -
339 // Changes Globals: -
340 //===========================================================================
341 float AAS_RocketJumpZVelocity(vec3_t origin)
342 {
343 //rocket radius damage is 120 (p_weapon.c: Weapon_RocketLauncher_Fire)
344 return AAS_WeaponJumpZVelocity(origin, 120);
345 } //end of the function AAS_RocketJumpZVelocity
346 //===========================================================================
347 //
348 // Parameter: -
349 // Returns: -
350 // Changes Globals: -
351 //===========================================================================
352 float AAS_BFGJumpZVelocity(vec3_t origin)
353 {
354 //bfg radius damage is 1000 (p_weapon.c: weapon_bfg_fire)
355 return AAS_WeaponJumpZVelocity(origin, 120);
356 } //end of the function AAS_BFGJumpZVelocity
357 //===========================================================================
358 // applies ground friction to the given velocity
359 //
360 // Parameter: -
361 // Returns: -
362 // Changes Globals: -
363 //===========================================================================
364 void AAS_Accelerate(vec3_t velocity, float frametime, vec3_t wishdir, float wishspeed, float accel)
365 {
366 // q2 style
367 int i;
368 float addspeed, accelspeed, currentspeed;
369
370 currentspeed = DotProduct(velocity, wishdir);
371 addspeed = wishspeed - currentspeed;
372 if (addspeed <= 0) {
373 return;
374 }
375 accelspeed = accel*frametime*wishspeed;
376 if (accelspeed > addspeed) {
377 accelspeed = addspeed;
378 }
379
380 for (i=0 ; i<3 ; i++) {
381 velocity[i] += accelspeed*wishdir[i];
382 }
383 } //end of the function AAS_Accelerate
384 //===========================================================================
385 // applies ground friction to the given velocity
386 //
387 // Parameter: -
388 // Returns: -
389 // Changes Globals: -
390 //===========================================================================
391 void AAS_ApplyFriction(vec3_t vel, float friction, float stopspeed,
392 float frametime)
393 {
394 float speed, control, newspeed;
395
396 //horizontal speed
397 speed = sqrt(vel[0] * vel[0] + vel[1] * vel[1]);
398 if (speed)
399 {
400 control = speed < stopspeed ? stopspeed : speed;
401 newspeed = speed - frametime * control * friction;
402 if (newspeed < 0) newspeed = 0;
403 newspeed /= speed;
404 vel[0] *= newspeed;
405 vel[1] *= newspeed;
406 } //end if
407 } //end of the function AAS_ApplyFriction
408 //===========================================================================
409 //
410 // Parameter: -
411 // Returns: -
412 // Changes Globals: -
413 //===========================================================================
414 int AAS_ClipToBBox(aas_trace_t *trace, vec3_t start, vec3_t end, int presencetype, vec3_t mins, vec3_t maxs)
415 {
416 int i, j, side;
417 float front, back, frac, planedist;
418 vec3_t bboxmins, bboxmaxs, absmins, absmaxs, dir, mid;
419
420 AAS_PresenceTypeBoundingBox(presencetype, bboxmins, bboxmaxs);
421 VectorSubtract(mins, bboxmaxs, absmins);
422 VectorSubtract(maxs, bboxmins, absmaxs);
423 //
424 VectorCopy(end, trace->endpos);
425 trace->fraction = 1;
426 for (i = 0; i < 3; i++)
427 {
428 if (start[i] < absmins[i] && end[i] < absmins[i]) return qfalse;
429 if (start[i] > absmaxs[i] && end[i] > absmaxs[i]) return qfalse;
430 } //end for
431 //check bounding box collision
432 VectorSubtract(end, start, dir);
433 frac = 1;
434 for (i = 0; i < 3; i++)
435 {
436 //get plane to test collision with for the current axis direction
437 if (dir[i] > 0) planedist = absmins[i];
438 else planedist = absmaxs[i];
439 //calculate collision fraction
440 front = start[i] - planedist;
441 back = end[i] - planedist;
442 frac = front / (front-back);
443 //check if between bounding planes of next axis
444 side = i + 1;
445 if (side > 2) side = 0;
446 mid[side] = start[side] + dir[side] * frac;
447 if (mid[side] > absmins[side] && mid[side] < absmaxs[side])
448 {
449 //check if between bounding planes of next axis
450 side++;
451 if (side > 2) side = 0;
452 mid[side] = start[side] + dir[side] * frac;
453 if (mid[side] > absmins[side] && mid[side] < absmaxs[side])
454 {
455 mid[i] = planedist;
456 break;
457 } //end if
458 } //end if
459 } //end for
460 //if there was a collision
461 if (i != 3)
462 {
463 trace->startsolid = qfalse;
464 trace->fraction = frac;
465 trace->ent = 0;
466 trace->planenum = 0;
467 trace->area = 0;
468 trace->lastarea = 0;
469 //trace endpos
470 for (j = 0; j < 3; j++) trace->endpos[j] = start[j] + dir[j] * frac;
471 return qtrue;
472 } //end if
473 return qfalse;
474 } //end of the function AAS_ClipToBBox
475 //===========================================================================
476 // predicts the movement
477 // assumes regular bounding box sizes
478 // NOTE: out of water jumping is not included
479 // NOTE: grappling hook is not included
480 //
481 // Parameter: origin : origin to start with
482 // presencetype : presence type to start with
483 // velocity : velocity to start with
484 // cmdmove : client command movement
485 // cmdframes : number of frame cmdmove is valid
486 // maxframes : maximum number of predicted frames
487 // frametime : duration of one predicted frame
488 // stopevent : events that stop the prediction
489 // stopareanum : stop as soon as entered this area
490 // Returns: aas_clientmove_t
491 // Changes Globals: -
492 //===========================================================================
493 int AAS_ClientMovementPrediction(struct aas_clientmove_s *move,
494 int entnum, vec3_t origin,
495 int presencetype, int onground,
496 vec3_t velocity, vec3_t cmdmove,
497 int cmdframes,
498 int maxframes, float frametime,
499 int stopevent, int stopareanum,
500 vec3_t mins, vec3_t maxs, int visualize)
501 {
502 float phys_friction, phys_stopspeed, phys_gravity, phys_waterfriction;
503 float phys_watergravity;
504 float phys_walkaccelerate, phys_airaccelerate, phys_swimaccelerate;
505 float phys_maxwalkvelocity, phys_maxcrouchvelocity, phys_maxswimvelocity;
506 float phys_maxstep, phys_maxsteepness, phys_jumpvel, friction;
507 float gravity, delta, maxvel, wishspeed, accelerate;
508 //float velchange, newvel;
509 //int ax;
510 int n, i, j, pc, step, swimming, crouch, event, jump_frame, areanum;
511 int areas[20], numareas;
512 vec3_t points[20];
513 vec3_t org, end, feet, start, stepend, lastorg, wishdir;
514 vec3_t frame_test_vel, old_frame_test_vel, left_test_vel;
515 vec3_t up = {0, 0, 1};
516 aas_plane_t *plane, *plane2;
517 aas_trace_t trace, steptrace;
518
519 if (frametime <= 0) frametime = 0.1f;
520 //
521 phys_friction = aassettings.phys_friction;
522 phys_stopspeed = aassettings.phys_stopspeed;
523 phys_gravity = aassettings.phys_gravity;
524 phys_waterfriction = aassettings.phys_waterfriction;
525 phys_watergravity = aassettings.phys_watergravity;
526 phys_maxwalkvelocity = aassettings.phys_maxwalkvelocity;// * frametime;
527 phys_maxcrouchvelocity = aassettings.phys_maxcrouchvelocity;// * frametime;
528 phys_maxswimvelocity = aassettings.phys_maxswimvelocity;// * frametime;
529 phys_walkaccelerate = aassettings.phys_walkaccelerate;
530 phys_airaccelerate = aassettings.phys_airaccelerate;
531 phys_swimaccelerate = aassettings.phys_swimaccelerate;
532 phys_maxstep = aassettings.phys_maxstep;
533 phys_maxsteepness = aassettings.phys_maxsteepness;
534 phys_jumpvel = aassettings.phys_jumpvel * frametime;
535 //
536 Com_Memset(move, 0, sizeof(aas_clientmove_t));
537 Com_Memset(&trace, 0, sizeof(aas_trace_t));
538 //start at the current origin
539 VectorCopy(origin, org);
540 org[2] += 0.25;
541 //velocity to test for the first frame
542 VectorScale(velocity, frametime, frame_test_vel);
543 //
544 jump_frame = -1;
545 //predict a maximum of 'maxframes' ahead
546 for (n = 0; n < maxframes; n++)
547 {
548 swimming = AAS_Swimming(org);
549 //get gravity depending on swimming or not
550 gravity = swimming ? phys_watergravity : phys_gravity;
551 //apply gravity at the START of the frame
552 frame_test_vel[2] = frame_test_vel[2] - (gravity * 0.1 * frametime);
553 //if on the ground or swimming
554 if (onground || swimming)
555 {
556 friction = swimming ? phys_waterfriction : phys_friction;
557 //apply friction
558 VectorScale(frame_test_vel, 1/frametime, frame_test_vel);
559 AAS_ApplyFriction(frame_test_vel, friction, phys_stopspeed, frametime);
560 VectorScale(frame_test_vel, frametime, frame_test_vel);
561 } //end if
562 crouch = qfalse;
563 //apply command movement
564 if (n < cmdframes)
565 {
566 //ax = 0;
567 maxvel = phys_maxwalkvelocity;
568 accelerate = phys_airaccelerate;
569 VectorCopy(cmdmove, wishdir);
570 if (onground)
571 {
572 if (cmdmove[2] < -300)
573 {
574 crouch = qtrue;
575 maxvel = phys_maxcrouchvelocity;
576 } //end if
577 //if not swimming and upmove is positive then jump
578 if (!swimming && cmdmove[2] > 1)
579 {
580 //jump velocity minus the gravity for one frame + 5 for safety
581 frame_test_vel[2] = phys_jumpvel - (gravity * 0.1 * frametime) + 5;
582 jump_frame = n;
583 //jumping so air accelerate
584 accelerate = phys_airaccelerate;
585 } //end if
586 else
587 {
588 accelerate = phys_walkaccelerate;
589 } //end else
590 //ax = 2;
591 } //end if
592 if (swimming)
593 {
594 maxvel = phys_maxswimvelocity;
595 accelerate = phys_swimaccelerate;
596 //ax = 3;
597 } //end if
598 else
599 {
600 wishdir[2] = 0;
601 } //end else
602 //
603 wishspeed = VectorNormalize(wishdir);
604 if (wishspeed > maxvel) wishspeed = maxvel;
605 VectorScale(frame_test_vel, 1/frametime, frame_test_vel);
606 AAS_Accelerate(frame_test_vel, frametime, wishdir, wishspeed, accelerate);
607 VectorScale(frame_test_vel, frametime, frame_test_vel);
608 /*
609 for (i = 0; i < ax; i++)
610 {
611 velchange = (cmdmove[i] * frametime) - frame_test_vel[i];
612 if (velchange > phys_maxacceleration) velchange = phys_maxacceleration;
613 else if (velchange < -phys_maxacceleration) velchange = -phys_maxacceleration;
614 newvel = frame_test_vel[i] + velchange;
615 //
616 if (frame_test_vel[i] <= maxvel && newvel > maxvel) frame_test_vel[i] = maxvel;
617 else if (frame_test_vel[i] >= -maxvel && newvel < -maxvel) frame_test_vel[i] = -maxvel;
618 else frame_test_vel[i] = newvel;
619 } //end for
620 */
621 } //end if
622 if (crouch)
623 {
624 presencetype = PRESENCE_CROUCH;
625 } //end if
626 else if (presencetype == PRESENCE_CROUCH)
627 {
628 if (AAS_PointPresenceType(org) & PRESENCE_NORMAL)
629 {
630 presencetype = PRESENCE_NORMAL;
631 } //end if
632 } //end else
633 //save the current origin
634 VectorCopy(org, lastorg);
635 //move linear during one frame
636 VectorCopy(frame_test_vel, left_test_vel);
637 j = 0;
638 do
639 {
640 VectorAdd(org, left_test_vel, end);
641 //trace a bounding box
642 trace = AAS_TraceClientBBox(org, end, presencetype, entnum);
643 //
644 //#ifdef AAS_MOVE_DEBUG
645 if (visualize)
646 {
647 if (trace.startsolid) botimport.Print(PRT_MESSAGE, "PredictMovement: start solid\n");
648 AAS_DebugLine(org, trace.endpos, LINECOLOR_RED);
649 } //end if
650 //#endif //AAS_MOVE_DEBUG
651 //
652 if (stopevent & (SE_ENTERAREA|SE_TOUCHJUMPPAD|SE_TOUCHTELEPORTER|SE_TOUCHCLUSTERPORTAL))
653 {
654 numareas = AAS_TraceAreas(org, trace.endpos, areas, points, 20);
655 for (i = 0; i < numareas; i++)
656 {
657 if (stopevent & SE_ENTERAREA)
658 {
659 if (areas[i] == stopareanum)
660 {
661 VectorCopy(points[i], move->endpos);
662 VectorScale(frame_test_vel, 1/frametime, move->velocity);
663 move->endarea = areas[i];
664 move->trace = trace;
665 move->stopevent = SE_ENTERAREA;
666 move->presencetype = presencetype;
667 move->endcontents = 0;
668 move->time = n * frametime;
669 move->frames = n;
670 return qtrue;
671 } //end if
672 } //end if
673 //NOTE: if not the first frame
674 if ((stopevent & SE_TOUCHJUMPPAD) && n)
675 {
676 if (aasworld.areasettings[areas[i]].contents & AREACONTENTS_JUMPPAD)
677 {
678 VectorCopy(points[i], move->endpos);
679 VectorScale(frame_test_vel, 1/frametime, move->velocity);
680 move->endarea = areas[i];
681 move->trace = trace;
682 move->stopevent = SE_TOUCHJUMPPAD;
683 move->presencetype = presencetype;
684 move->endcontents = 0;
685 move->time = n * frametime;
686 move->frames = n;
687 return qtrue;
688 } //end if
689 } //end if
690 if (stopevent & SE_TOUCHTELEPORTER)
691 {
692 if (aasworld.areasettings[areas[i]].contents & AREACONTENTS_TELEPORTER)
693 {
694 VectorCopy(points[i], move->endpos);
695 move->endarea = areas[i];
696 VectorScale(frame_test_vel, 1/frametime, move->velocity);
697 move->trace = trace;
698 move->stopevent = SE_TOUCHTELEPORTER;
699 move->presencetype = presencetype;
700 move->endcontents = 0;
701 move->time = n * frametime;
702 move->frames = n;
703 return qtrue;
704 } //end if
705 } //end if
706 if (stopevent & SE_TOUCHCLUSTERPORTAL)
707 {
708 if (aasworld.areasettings[areas[i]].contents & AREACONTENTS_CLUSTERPORTAL)
709 {
710 VectorCopy(points[i], move->endpos);
711 move->endarea = areas[i];
712 VectorScale(frame_test_vel, 1/frametime, move->velocity);
713 move->trace = trace;
714 move->stopevent = SE_TOUCHCLUSTERPORTAL;
715 move->presencetype = presencetype;
716 move->endcontents = 0;
717 move->time = n * frametime;
718 move->frames = n;
719 return qtrue;
720 } //end if
721 } //end if
722 } //end for
723 } //end if
724 //
725 if (stopevent & SE_HITBOUNDINGBOX)
726 {
727 if (AAS_ClipToBBox(&trace, org, trace.endpos, presencetype, mins, maxs))
728 {
729 VectorCopy(trace.endpos, move->endpos);
730 move->endarea = AAS_PointAreaNum(move->endpos);
731 VectorScale(frame_test_vel, 1/frametime, move->velocity);
732 move->trace = trace;
733 move->stopevent = SE_HITBOUNDINGBOX;
734 move->presencetype = presencetype;
735 move->endcontents = 0;
736 move->time = n * frametime;
737 move->frames = n;
738 return qtrue;
739 } //end if
740 } //end if
741 //move the entity to the trace end point
742 VectorCopy(trace.endpos, org);
743 //if there was a collision
744 if (trace.fraction < 1.0)
745 {
746 //get the plane the bounding box collided with
747 plane = AAS_PlaneFromNum(trace.planenum);
748 //
749 if (stopevent & SE_HITGROUNDAREA)
750 {
751 if (DotProduct(plane->normal, up) > phys_maxsteepness)
752 {
753 VectorCopy(org, start);
754 start[2] += 0.5;
755 if (AAS_PointAreaNum(start) == stopareanum)
756 {
757 VectorCopy(start, move->endpos);
758 move->endarea = stopareanum;
759 VectorScale(frame_test_vel, 1/frametime, move->velocity);
760 move->trace = trace;
761 move->stopevent = SE_HITGROUNDAREA;
762 move->presencetype = presencetype;
763 move->endcontents = 0;
764 move->time = n * frametime;
765 move->frames = n;
766 return qtrue;
767 } //end if
768 } //end if
769 } //end if
770 //assume there's no step
771 step = qfalse;
772 //if it is a vertical plane and the bot didn't jump recently
773 if (plane->normal[2] == 0 && (jump_frame < 0 || n - jump_frame > 2))
774 {
775 //check for a step
776 VectorMA(org, -0.25, plane->normal, start);
777 VectorCopy(start, stepend);
778 start[2] += phys_maxstep;
779 steptrace = AAS_TraceClientBBox(start, stepend, presencetype, entnum);
780 //
781 if (!steptrace.startsolid)
782 {
783 plane2 = AAS_PlaneFromNum(steptrace.planenum);
784 if (DotProduct(plane2->normal, up) > phys_maxsteepness)
785 {
786 VectorSubtract(end, steptrace.endpos, left_test_vel);
787 left_test_vel[2] = 0;
788 frame_test_vel[2] = 0;
789 //#ifdef AAS_MOVE_DEBUG
790 if (visualize)
791 {
792 if (steptrace.endpos[2] - org[2] > 0.125)
793 {
794 VectorCopy(org, start);
795 start[2] = steptrace.endpos[2];
796 AAS_DebugLine(org, start, LINECOLOR_BLUE);
797 } //end if
798 } //end if
799 //#endif //AAS_MOVE_DEBUG
800 org[2] = steptrace.endpos[2];
801 step = qtrue;
802 } //end if
803 } //end if
804 } //end if
805 //
806 if (!step)
807 {
808 //velocity left to test for this frame is the projection
809 //of the current test velocity into the hit plane
810 VectorMA(left_test_vel, -DotProduct(left_test_vel, plane->normal),
811 plane->normal, left_test_vel);
812 //store the old velocity for landing check
813 VectorCopy(frame_test_vel, old_frame_test_vel);
814 //test velocity for the next frame is the projection
815 //of the velocity of the current frame into the hit plane
816 VectorMA(frame_test_vel, -DotProduct(frame_test_vel, plane->normal),
817 plane->normal, frame_test_vel);
818 //check for a landing on an almost horizontal floor
819 if (DotProduct(plane->normal, up) > phys_maxsteepness)
820 {
821 onground = qtrue;
822 } //end if
823 if (stopevent & SE_HITGROUNDDAMAGE)
824 {
825 delta = 0;
826 if (old_frame_test_vel[2] < 0 &&
827 frame_test_vel[2] > old_frame_test_vel[2] &&
828 !onground)
829 {
830 delta = old_frame_test_vel[2];
831 } //end if
832 else if (onground)
833 {
834 delta = frame_test_vel[2] - old_frame_test_vel[2];
835 } //end else
836 if (delta)
837 {
838 delta = delta * 10;
839 delta = delta * delta * 0.0001;
840 if (swimming) delta = 0;
841 // never take falling damage if completely underwater
842 /*
843 if (ent->waterlevel == 3) return;
844 if (ent->waterlevel == 2) delta *= 0.25;
845 if (ent->waterlevel == 1) delta *= 0.5;
846 */
847 if (delta > 40)
848 {
849 VectorCopy(org, move->endpos);
850 move->endarea = AAS_PointAreaNum(org);
851 VectorCopy(frame_test_vel, move->velocity);
852 move->trace = trace;
853 move->stopevent = SE_HITGROUNDDAMAGE;
854 move->presencetype = presencetype;
855 move->endcontents = 0;
856 move->time = n * frametime;
857 move->frames = n;
858 return qtrue;
859 } //end if
860 } //end if
861 } //end if
862 } //end if
863 } //end if
864 //extra check to prevent endless loop
865 if (++j > 20) return qfalse;
866 //while there is a plane hit
867 } while(trace.fraction < 1.0);
868 //if going down
869 if (frame_test_vel[2] <= 10)
870 {
871 //check for a liquid at the feet of the bot
872 VectorCopy(org, feet);
873 feet[2] -= 22;
874 pc = AAS_PointContents(feet);
875 //get event from pc
876 event = SE_NONE;
877 if (pc & CONTENTS_LAVA) event |= SE_ENTERLAVA;
878 if (pc & CONTENTS_SLIME) event |= SE_ENTERSLIME;
879 if (pc & CONTENTS_WATER) event |= SE_ENTERWATER;
880 //
881 areanum = AAS_PointAreaNum(org);
882 if (aasworld.areasettings[areanum].contents & AREACONTENTS_LAVA)
883 event |= SE_ENTERLAVA;
884 if (aasworld.areasettings[areanum].contents & AREACONTENTS_SLIME)
885 event |= SE_ENTERSLIME;
886 if (aasworld.areasettings[areanum].contents & AREACONTENTS_WATER)
887 event |= SE_ENTERWATER;
888 //if in lava or slime
889 if (event & stopevent)
890 {
891 VectorCopy(org, move->endpos);
892 move->endarea = areanum;
893 VectorScale(frame_test_vel, 1/frametime, move->velocity);
894 move->stopevent = event & stopevent;
895 move->presencetype = presencetype;
896 move->endcontents = pc;
897 move->time = n * frametime;
898 move->frames = n;
899 return qtrue;
900 } //end if
901 } //end if
902 //
903 onground = AAS_OnGround(org, presencetype, entnum);
904 //if onground and on the ground for at least one whole frame
905 if (onground)
906 {
907 if (stopevent & SE_HITGROUND)
908 {
909 VectorCopy(org, move->endpos);
910 move->endarea = AAS_PointAreaNum(org);
911 VectorScale(frame_test_vel, 1/frametime, move->velocity);
912 move->trace = trace;
913 move->stopevent = SE_HITGROUND;
914 move->presencetype = presencetype;
915 move->endcontents = 0;
916 move->time = n * frametime;
917 move->frames = n;
918 return qtrue;
919 } //end if
920 } //end if
921 else if (stopevent & SE_LEAVEGROUND)
922 {
923 VectorCopy(org, move->endpos);
924 move->endarea = AAS_PointAreaNum(org);
925 VectorScale(frame_test_vel, 1/frametime, move->velocity);
926 move->trace = trace;
927 move->stopevent = SE_LEAVEGROUND;
928 move->presencetype = presencetype;
929 move->endcontents = 0;
930 move->time = n * frametime;
931 move->frames = n;
932 return qtrue;
933 } //end else if
934 else if (stopevent & SE_GAP)
935 {
936 aas_trace_t gaptrace;
937
938 VectorCopy(org, start);
939 VectorCopy(start, end);
940 end[2] -= 48 + aassettings.phys_maxbarrier;
941 gaptrace = AAS_TraceClientBBox(start, end, PRESENCE_CROUCH, -1);
942 //if solid is found the bot cannot walk any further and will not fall into a gap
943 if (!gaptrace.startsolid)
944 {
945 //if it is a gap (lower than one step height)
946 if (gaptrace.endpos[2] < org[2] - aassettings.phys_maxstep - 1)
947 {
948 if (!(AAS_PointContents(end) & CONTENTS_WATER))
949 {
950 VectorCopy(lastorg, move->endpos);
951 move->endarea = AAS_PointAreaNum(lastorg);
952 VectorScale(frame_test_vel, 1/frametime, move->velocity);
953 move->trace = trace;
954 move->stopevent = SE_GAP;
955 move->presencetype = presencetype;
956 move->endcontents = 0;
957 move->time = n * frametime;
958 move->frames = n;
959 return qtrue;
960 } //end if
961 } //end if
962 } //end if
963 } //end else if
964 } //end for
965 //
966 VectorCopy(org, move->endpos);
967 move->endarea = AAS_PointAreaNum(org);
968 VectorScale(frame_test_vel, 1/frametime, move->velocity);
969 move->stopevent = SE_NONE;
970 move->presencetype = presencetype;
971 move->endcontents = 0;
972 move->time = n * frametime;
973 move->frames = n;
974 //
975 return qtrue;
976 } //end of the function AAS_ClientMovementPrediction
977 //===========================================================================
978 //
979 // Parameter: -
980 // Returns: -
981 // Changes Globals: -
982 //===========================================================================
983 int AAS_PredictClientMovement(struct aas_clientmove_s *move,
984 int entnum, vec3_t origin,
985 int presencetype, int onground,
986 vec3_t velocity, vec3_t cmdmove,
987 int cmdframes,
988 int maxframes, float frametime,
989 int stopevent, int stopareanum, int visualize)
990 {
991 vec3_t mins, maxs;
992 return AAS_ClientMovementPrediction(move, entnum, origin, presencetype, onground,
993 velocity, cmdmove, cmdframes, maxframes,
994 frametime, stopevent, stopareanum,
995 mins, maxs, visualize);
996 } //end of the function AAS_PredictClientMovement
997 //===========================================================================
998 //
999 // Parameter: -
1000 // Returns: -
1001 // Changes Globals: -
1002 //===========================================================================
1003 int AAS_ClientMovementHitBBox(struct aas_clientmove_s *move,
1004 int entnum, vec3_t origin,
1005 int presencetype, int onground,
1006 vec3_t velocity, vec3_t cmdmove,
1007 int cmdframes,
1008 int maxframes, float frametime,
1009 vec3_t mins, vec3_t maxs, int visualize)
1010 {
1011 return AAS_ClientMovementPrediction(move, entnum, origin, presencetype, onground,
1012 velocity, cmdmove, cmdframes, maxframes,
1013 frametime, SE_HITBOUNDINGBOX, 0,
1014 mins, maxs, visualize);
1015 } //end of the function AAS_ClientMovementHitBBox
1016 //===========================================================================
1017 //
1018 // Parameter: -
1019 // Returns: -
1020 // Changes Globals: -
1021 //===========================================================================
1022 void AAS_TestMovementPrediction(int entnum, vec3_t origin, vec3_t dir)
1023 {
1024 vec3_t velocity, cmdmove;
1025 aas_clientmove_t move;
1026
1027 VectorClear(velocity);
1028 if (!AAS_Swimming(origin)) dir[2] = 0;
1029 VectorNormalize(dir);
1030 VectorScale(dir, 400, cmdmove);
1031 cmdmove[2] = 224;
1032 AAS_ClearShownDebugLines();
1033 AAS_PredictClientMovement(&move, entnum, origin, PRESENCE_NORMAL, qtrue,
1034 velocity, cmdmove, 13, 13, 0.1f, SE_HITGROUND, 0, qtrue);//SE_LEAVEGROUND);
1035 if (move.stopevent & SE_LEAVEGROUND)
1036 {
1037 botimport.Print(PRT_MESSAGE, "leave ground\n");
1038 } //end if
1039 } //end of the function TestMovementPrediction
1040 //===========================================================================
1041 // calculates the horizontal velocity needed to perform a jump from start
1042 // to end
1043 //
1044 // Parameter: zvel : z velocity for jump
1045 // start : start position of jump
1046 // end : end position of jump
1047 // *speed : returned speed for jump
1048 // Returns: qfalse if too high or too far from start to end
1049 // Changes Globals: -
1050 //===========================================================================
1051 int AAS_HorizontalVelocityForJump(float zvel, vec3_t start, vec3_t end, float *velocity)
1052 {
1053 float phys_gravity, phys_maxvelocity;
1054 float maxjump, height2fall, t, top;
1055 vec3_t dir;
1056
1057 phys_gravity = aassettings.phys_gravity;
1058 phys_maxvelocity = aassettings.phys_maxvelocity;
1059
1060 //maximum height a player can jump with the given initial z velocity
1061 maxjump = 0.5 * phys_gravity * (zvel / phys_gravity) * (zvel / phys_gravity);
1062 //top of the parabolic jump
1063 top = start[2] + maxjump;
1064 //height the bot will fall from the top
1065 height2fall = top - end[2];
1066 //if the goal is to high to jump to
1067 if (height2fall < 0)
1068 {
1069 *velocity = phys_maxvelocity;
1070 return 0;
1071 } //end if
1072 //time a player takes to fall the height
1073 t = sqrt(height2fall / (0.5 * phys_gravity));
1074 //direction from start to end
1075 VectorSubtract(end, start, dir);
1076 //
1077 if ( (t + zvel / phys_gravity) == 0.0f ) {
1078 *velocity = phys_maxvelocity;
1079 return 0;
1080 }
1081 //calculate horizontal speed
1082 *velocity = sqrt(dir[0]*dir[0] + dir[1]*dir[1]) / (t + zvel / phys_gravity);
1083 //the horizontal speed must be lower than the max speed
1084 if (*velocity > phys_maxvelocity)
1085 {
1086 *velocity = phys_maxvelocity;
1087 return 0;
1088 } //end if
1089 return 1;
1090 } //end of the function AAS_HorizontalVelocityForJump