Debian Codegraph @

Descent3/AIGoal.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/AIGoal.cpp $
21 * $Revision: 137 $
22 * $Date: 4/19/00 5:07p $
23 * $Author: Matt $
24 *
25 * AI Goal Functions
26 *
27 * $Log: /DescentIII/main/AIGoal.cpp $
28 *
29 * 137 4/19/00 5:07p Matt
30 * From Duane for 1.4
31 * Added checks, asserts, and fixes for bad return values
32 *
33 * 136 3/20/00 12:02p Matt
34 * Merge of Duane's post-1.3 changes.
35 * Added check for bad wb index
36 *
37 * 135 10/23/99 2:43a Chris
38 * Added the PutObjectOnObject AI Goal
39 *
40 * 134 10/21/99 2:17p Matt
41 * Mac merge
42 *
43 * 133 10/08/99 4:31p Nate
44 * Allow custom scripts to overide normal AI animation changes
45 *
46 * 132 7/27/99 4:16p Chris
47 * Now dodge goals can get killed
48 *
49 * 131 5/23/99 12:34a Chris
50 * Removed experimental code
51 *
52 * 130 5/22/99 3:36p Chris
53 * Further bulletproof the path stuff
54 *
55 * 129 5/22/99 3:12p Chris
56 *
57 * 128 5/22/99 3:11p Chris
58 * Fixed a bug in the wander code
59 *
60 * 127 5/21/99 7:46p Chris
61 *
62 * 126 5/21/99 7:22p Chris
63 * Fixed bugs in wander code
64 *
65 * 125 5/20/99 1:15a Chris
66 * Improved BNode Path Following for end points on the path. Fixed bugs
67 * with non-auto targetting
68 *
69 * 124 5/18/99 10:57a Chris
70 * Various bug fixes
71 *
72 * 123 5/17/99 6:06p Chris
73 * Adding robot debug code
74 *
75 * 122 5/09/99 8:11p Chris
76 * Made the whole see/hear distintion work (mostly)
77 *
78 * 121 5/01/99 2:21a Chris
79 * Use the GF_SPEED_XXX stuff
80 *
81 * 120 4/28/99 5:33a Chris
82 * Further improved the BNode system. (It actually works inside and
83 * outside now)
84 *
85 * 119 4/24/99 2:19a Chris
86 * Fixed Tubbs and paths
87 *
88 * 118 4/21/99 11:05a Kevin
89 * new ps_rand and ps_srand to replace rand & srand
90 *
91 * 117 4/20/99 8:55p Chris
92 * Fixed problem with robots not being able to open locked doors that a
93 * player has the key for.
94 *
95 * 116 4/18/99 5:38a Chris
96 * Drastically improved the find_random_room function and added the
97 * makenextroomlist function
98 *
99 * 115 4/14/99 2:50a Jeff
100 * fixed some case mismatched #includes
101 *
102 * 114 4/09/99 10:33a Chris
103 * Improvements to Freud
104 *
105 * 113 4/06/99 11:03p Chris
106 * Added goal unique id's. Improved FindRandomRoom (not so close to the
107 * edge of the terrain)
108 *
109 * 112 4/06/99 11:18a Chris
110 * Fixed a big bug in the attach system where object's properties where
111 * not correctly reset after the attach
112 *
113 * 111 4/05/99 5:38p Chris
114 * Fixed a bug where a wander_around goal could end up with a room 0 pos
115 * 0,0,0 path when first init'ed
116 *
117 * 110 4/05/99 3:18p Chris
118 * Patching wandering code
119 *
120 * 109 4/05/99 2:37p Chris
121 * Stalkers are now wandering robots
122 *
123 * 108 3/31/99 12:50p Chris
124 * Evader goal doesn't do avoid stuff for robots with a circle distance of
125 * zero
126 *
127 * 107 3/30/99 4:32p Chris
128 * Moved subtype to the main goal sturct (from goal_info). Made move
129 * relative object vec finishable. (Like get behind player)
130 *
131 * 106 3/27/99 3:00p Chris
132 * Fixed bug where high priority paths wouldn't reset the path if the
133 * previously active path was a static path
134 *
135 * 105 3/27/99 12:29p Chris
136 * Fixed problems with assigned lower priority goals that used paths (it
137 * would confuse the bot because the .path field was getting updated for
138 * the new goal even though it was lower priority)
139 *
140 * 104 3/23/99 11:51a Matt
141 * Made wander code not crash if the room selected was an outside room
142 * (from an inside wander) I.e. two rooms in two different mines with
143 * terrain between them.
144 *
145 * 103 3/22/99 5:47p Chris
146 * Improved AI difficulties when a goal is being deleted and, in script, a
147 * new goal uses that slot in middle of the delete code
148 *
149 * 102 3/22/99 10:58a Chris
150 * Awareness code was tweaked. Multisafe stuff added for objects.
151 *
152 * 101 3/17/99 5:23p Chris
153 * Fixed problems with low-priority paths
154 *
155 * 100 3/17/99 12:25p Chris
156 * Low priority paths
157 *
158 * 99 3/03/99 10:51a Chris
159 * Got rid of goal_ptr
160 *
161 * 98 2/15/99 9:03p Chris
162 * Added the base FOV off UVEC code and converted all the turrets
163 *
164 * 97 2/12/99 11:19a Chris
165 * If a path doesn't exist, look for it next frame
166 *
167 * 96 2/10/99 1:47p Matt
168 * Changed object handle symbolic constants
169 *
170 * 95 2/09/99 9:58a Chris
171 * Massive BOA update :) Terrain happy now. Vis happy now. Sound happy
172 * now.
173 *
174 * 94 2/02/99 3:52p Luke
175 * Fixed a circular loop in the AINotify stuff
176 *
177 * 93 1/30/99 4:41p Luke
178 * Make the guard goal work
179 *
180 * 92 1/26/99 2:51p Chris
181 * AIG_WANDER improvements
182 *
183 * 91 1/25/99 8:16a Chris
184 * Made sure the DALLAS is informed about code forced cleared goals that
185 * have GUIDs.
186 *
187 * 90 1/25/99 7:43a Chris
188 * Added the GUID (Goal Unique Id) and added the ability for weapon
189 * batteries to always fire exactly forward.
190 *
191 * 89 1/24/99 8:17p Chris
192 * Updated AI and OSIRIS. Externalized fireball constants for spew and
193 * sparks. Added CreateRandomSparks to OSIRIS, renamed a bunch of AI
194 * Notify names to use underscores. Fixed a memory access leak in the
195 * matcen effect code.
196 *
197 * 88 1/22/99 6:53p Chris
198 * Fixed LoadandBind Aux notify problems, fixed static path problems,
199 * fixed AIF_FORCE_AWARE Problems, improved path code
200 *
201 * 87 1/20/99 1:01a Chris
202 * Improved AI and OSIRIS intergration
203 *
204 * 86 1/18/99 8:07p Chris
205 * Added the no-collide same flag (for flocks and nests)
206 *
207 * 85 1/18/99 9:05a Chris
208 * Improved OSIRIS, AI, and ATTACH system, changed wiggle code, changed
209 * attach code for rad attaches, and added the AIG_ATTACH_OBJ goal
210 *
211 * 84 1/15/99 5:54p Chris
212 *
213 * 83 1/11/99 2:14p Chris
214 * Massive work on OSIRIS and AI
215 *
216 * 82 12/16/98 3:55p Chris
217 * Improved the sickles
218 *
219 * 81 12/14/98 1:07p Chris
220 * Allowed OSIRIS to change AI types
221 *
222 * 80 12/13/98 9:18p Chris
223 * Improved influence values for in-code goals (10000 to 1.0). Added
224 * GF_ORIENT stuff. :)
225 *
226 * 79 12/08/98 4:26p Chris
227 * Fixed problems with same anim animations (both with .to of zero no
228 * longer will do it)
229 *
230 * 78 12/03/98 5:45p Chris
231 * I just added full code support for OSIRIS/DLL controlled goals,
232 * enablers, and influence levels. :)
233 *
234 * 77 12/03/98 12:24p Chris
235 * Improved new anim code for instant updating
236 *
237 * 76 12/03/98 12:04p Chris
238 * Added the instant mode switching for shared frame anims!
239 *
240 * 75 12/02/98 5:50p Chris
241 * GoalDetermineTrackDist is more complete
242 *
243 * 74 12/01/98 6:17p Chris
244 * Height bias is only for outside and GoalDetermineTrackDist is closer to
245 * final
246 *
247 * 73 12/01/98 5:30p Chris
248 *
249 * 72 12/01/98 5:25p Chris
250 * fixed switch statement for AIT_ init stuff
251 *
252 * 71 12/01/98 4:29p Chris
253 * Checked in a massive amount of AI work. Improved flocking code. :)
254 * (Still hacked lightly). In addition, I am moving toward using the
255 * composite dir. :)
256 *
257 * 70 11/19/98 9:24p Chris
258 *
259 * 69 11/19/98 8:25p Chris
260 * Starting to add generic team avoidance code
261 *
262 * 68 11/18/98 3:59p Chris
263 * I added the avoid goal. I also improved the AIs for Evader1 and
264 * Evader2 by utilizing this goal when the target is well within the
265 * circle distance.
266 *
267 * 67 11/11/98 6:31p Chris
268 * AIF_DISABLE_FIRING and AIF_DISABLE_MELEE are now functional
269 *
270 * 66 11/06/98 11:39a Chris
271 * Robots with flamethrowers and Omega cannons work in single player
272 *
273 * 65 10/22/98 7:55a Chris
274 *
275 * 64 10/22/98 7:07a Chris
276 * Improved Evader2
277 *
278 * 63 10/21/98 7:33a Chris
279 * Dead objects are not-targetable
280 *
281 * 62 10/20/98 11:58p Chris
282 * AIGetToObj is not enabled when the target is NULL or OBJ_GHOST
283 *
284 * 61 10/13/98 1:08p Chris
285 * Greatly improved the AI's use of paths. Improved visability checking
286 * algorithm. Probably needs a second pass for further cleanup.
287 *
288 * 60 10/07/98 3:37p Chris
289 * Improved the melee attack code
290 *
291 * 59 9/28/98 4:09p Chris
292 * Added birth animations
293 *
294 * 58 9/17/98 11:11a Chris
295 * Worked on goal system and improved integration with OSIRIS (better
296 * EVT_AI_INIT event calling)
297 *
298 * 57 9/15/98 7:29p Chris
299 * Improved OSIRIS and AI intergration
300 *
301 * 56 9/01/98 3:50p Chris
302 *
303 * 55 8/17/98 4:55p Chris
304 * Added GF_FORCE_AWARENESS for goals
305 *
306 * 54 8/06/98 5:27p Chris
307 * Fixed a bug when AIs regenerate their paths (they where not storing the
308 * goal pos/room) with the AIGoalGetToPos goal.
309 *
310 * 53 6/29/98 5:34p Chris
311 * Do general goal stuff before the currently active goal
312 *
313 * 52 6/29/98 5:03p Chris
314 *
315 * 51 6/29/98 4:36p Chris
316 * Fixed a bug with melee robots
317 *
318 * 50 6/15/98 3:23p Chris
319 * Single point walker update
320 *
321 * 49 5/22/98 6:45p Chris
322 * Fixed a bug with flinching and melee attacks
323 *
324 * 48 5/22/98 6:22p Chris
325 * Improved Dynamic path allocation
326 *
327 * 47 5/22/98 4:44p Chris
328 * Added better melee hit prediction
329 *
330 * 46 5/20/98 10:19a Chris
331 * Fixed some bugs with status_reg's and circle distance
332 *
333 * 45 5/18/98 12:40p Chris
334 * Added a new flag (F_BLINE_IF_SEE_GOAL)
335 *
336 * 44 5/17/98 9:07p Chris
337 * Fixed melee attacks. Thanks to Ala. :)
338 *
339 * 43 5/17/98 7:54p Chris
340 * Correctly integrated the goal system and circle distance stuff. Added
341 * support for "target goals".
342 *
343 * 42 5/15/98 2:58p Chris
344 * More on the big AI update
345 *
346 * 41 5/14/98 3:01p Chris
347 * More new AI code
348 *
349 * 40 5/14/98 12:21p Chris
350 * Updating AI system... Incomplete
351 *
352 * 39 5/12/98 3:46p Chris
353 * Added some notify/goal system stuff and orientation to velocity
354 *
355 * 38 5/11/98 4:39p Chris
356 * Improved AI system (goals have notifies and are more flexable).
357 *
358 * 37 5/04/98 4:04p Chris
359 * Minestone checkin' -- energy effect and more AI functionality
360 *
361 * 36 5/01/98 3:41p Chris
362 *
363 * 35 5/31/98 3:05p Chris
364 * Allowed death anims (fixed problem with ctype union of CT_DEBRIS and
365 * CT_AI)
366 *
367 * 34 4/30/98 11:31a Chris
368 * Massive upgrades to the AI system
369 *
370 * 33 4/22/98 1:15p Chris
371 * Incremental work on paths and goals
372 *
373 * 32 4/21/98 11:25a Chris
374 * Improving GET_TO_OBJ goal
375 *
376 * 31 4/20/98 3:19p Chris
377 * Added more goal/path support
378 *
379 * 30 4/20/98 12:27p Chris
380 * Made AIG_GET_TO_OBJECT work.
381 *
382 * 29 4/19/98 9:56p Chris
383 * AI system is integrated with OSIRIS. Path system is integrated with
384 * the AI system. Bugs will ensue.
385 *
386 * 28 4/14/98 11:42a Chris
387 * Added quirks and a better way of getting from idle to alert and from
388 * alert to idle.
389 *
390 * 27 4/13/98 6:54p Chris
391 * Improved the animation system.
392 *
393 * 26 4/13/98 2:20p Chris
394 * IDLe works (kindof)
395 *
396 * 25 3/30/98 10:50a Chris
397 * AI's will not go into fire if a flinch is lined up.
398 *
399 * 24 3/25/98 5:51p Chris
400 * Added full model/body animations for weapon firing
401 *
402 * 23 3/25/98 11:02a Chris
403 * version 1.0 of the new AI ranged firing code.
404 *
405 * 22 3/13/98 5:55p Chris
406 * Fixed problems with animations
407 *
408 * 21 3/12/98 5:55p Chris
409 * Updated flinch
410 *
411 * 20 3/12/98 5:51p Chris
412 * Working on Bashing anim's to other anims
413 *
414 * 19 3/03/98 5:02p Chris
415 * Added a status register to the ai_frame. Also, I enabled a bunch of
416 * the fields from the AI Dialog. In addiition, I tweaked how melee
417 * attacks work and animate.
418 *
419 * 18 2/16/98 12:52p Chris
420 * Added some Flinch support
421 *
422 * 17 2/11/98 7:00p Chris
423 * Removed priority (changed to influence) and added activation_level
424 *
425 * 16 2/02/98 8:14p Chris
426 * Updated the AI system
427 *
428 * 15 1/19/98 10:04a Matt
429 * Added new object handle system
430 *
431 * 14 1/14/98 7:57p Chris
432 * Improving the awareness system
433 *
434 * 13 11/17/97 9:48p Chris
435 * Added guarding
436 *
437 * 12 11/17/97 5:46p Chris
438 * Added some support for a Theif-like robot
439 *
440 * 11 11/14/97 11:55p Chris
441 * Dodge goals are semi-functional
442 *
443 * 10 9/02/97 3:28p Matt
444 * Got rid of warnings
445 *
446 * 9 8/05/97 12:29p Chris
447 *
448 * 8 7/30/97 1:31p Chris
449 * Made helicopters slightly more interesting.
450 *
451 * 7 7/29/97 12:20p Chris
452 * Incremental improvements. Fixed a memory bug.
453 *
454 * 6 7/15/97 7:29p Chris
455 * Added a sound for helicopter blades.
456 *
457 * 5 7/15/97 5:35p Chris
458 * New AI code
459 *
460 * 4 7/15/97 4:59p Chris
461 * added support for AI and animations
462 *
463 * $NoKeywords: $
464 */
465
466 #include <stdlib.h>
467 #include "AIGoal.h"
468 #include "aistruct.h"
469 #include "aipath.h"
470 #include "object.h"
471 #include "game.h"
472 #include "terrain.h"
473 #include "objinfo.h"
474 #include "AIMain.h"
475 #include "room.h"
476 #include "psrand.h"
477 #include "BOA.h"
478 #include "findintersection.h"
479
480 extern int AI_unique_goal_id;
481
482 static void GoalInitWanderAround(object *obj, goal *goal_ptr);
483 static int GoalAllocSlot(object *obj, int level, float influence);
484
485 #define BASH_TO_ANIM_SCALER 10.0f
486 #define MAX_BASH_TO_FLINCH_TIME 2.5f
487
488 void GoalInitWanderAround(object *obj, goal *goal_ptr) {
489 ai_frame *ai_info = obj->ai_info;
490 int roomnum;
491 vector pos;
492
493 // mprintf(0, "Wander around\n");
494
495 goal *cur_goal = GoalGetCurrentGoal(obj);
496 if (cur_goal != goal_ptr)
497 return;
498
499 if (!ROOMNUM_OUTSIDE(obj->roomnum)) {
500 roomnum = AIFindRandomRoom(obj, ai_info, goal_ptr, obj->roomnum, -1, -1, true, false, NULL);
501 if (!ROOMNUM_OUTSIDE(roomnum)) {
502 pos = Rooms[roomnum].path_pnt;
503 } else {
504 ComputeTerrainSegmentCenter(&pos, CELLNUM(roomnum));
505 pos.y += (ai_info->biased_flight_max + ai_info->biased_flight_min) / 2.0f;
506 }
507 } else {
508 int tx = (ps_rand() % (TERRAIN_WIDTH - 56)) + (56 / 2);
509 int tz = (ps_rand() % (TERRAIN_DEPTH - 56)) + (56 / 2);
510
511 int cell = tx + TERRAIN_WIDTH * tz;
512 roomnum = 0x80000000 | cell;
513 ComputeTerrainSegmentCenter(&pos, cell);
514 pos.y += (ai_info->biased_flight_max + ai_info->biased_flight_min) / 2.0f;
515 }
516
517 goal_ptr->g_info.pos = pos;
518 goal_ptr->g_info.roomnum = roomnum;
519
520 goal_ptr->next_path_time = Gametime + MIN_NEXT_PATH_INTERVAL + ps_rand() / (float)D3_RAND_MAX;
521 goal_ptr->flags |= GF_HAS_PATH;
522 }
523
524 void GoalClearGoal(object *obj, goal *cur_goal, int reason) {
525 ai_frame *ai_info = obj->ai_info;
526 bool f_continue = true;
527
528 // Set the in clear flag so that we know if this goal is updated in script
529 cur_goal->flags |= GF_IN_CLEAR;
530
531 if (reason != AI_INVALID_INDEX) {
532 int goal_index = (cur_goal - ai_info->goals);
533
534 f_continue = AINotify(obj, reason, (void *)&goal_index);
535 } else if (cur_goal->guid != -1 && reason != AI_INVALID_INDEX) {
536 int goal_index = (cur_goal - ai_info->goals);
537 AINotify(obj, AIN_GOAL_FAIL, (void *)&goal_index);
538 }
539
540 // Don't continue if this is a different goal
541 if (!(cur_goal->flags & GF_IN_CLEAR))
542 return;
543
544 // Clear the in clear flag
545 cur_goal->flags &= ~GF_IN_CLEAR;
546
547 if ((reason == AIN_GOAL_COMPLETE) && (cur_goal->flags & GF_KEEP_AT_COMPLETION)) {
548 if (cur_goal->type == AIG_WANDER_AROUND) {
549 GoalInitWanderAround(obj, cur_goal);
550 }
551 return;
552 }
553
554 if ((cur_goal->flags & GF_NONFLUSHABLE) && (reason != AI_INVALID_INDEX))
555 return;
556
557 if (!f_continue)
558 return;
559
560 // cur_goal->goal_type = AIG_NOT_USED;
561 cur_goal->used = false;
562
563 if (obj->ai_info->path.goal_uid == cur_goal->goal_uid) {
564 #ifdef _DEBUG
565 if (AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index) {
566 mprintf(0, "AI Note: In free path\n");
567 }
568 #endif
569 AIPathFreePath(&ai_info->path);
570 }
571
572 // for(i = 0; i < MAX_ENDERS_PER_GOAL; i++)
573 // {
574 // cur_goal->enders[i].ender_type = AIE_NOT_USED;
575 // }
576 }
577
578 #define MAX_NEAR_OBJ_SEARCH_DIST 200.0f
579
580 float GoalDetermineTrackDist(object *obj) {
581 float dist = 0.0f;
582 int i;
583
584 for (i = 0; i < MAX_GOALS; i++) {
585 goal *cur_goal = &obj->ai_info->goals[i];
586
587 if (cur_goal->used && ISTRACKGOAL(cur_goal)) {
588 if (cur_goal->flags & GF_RAMPED_INFLUENCE) {
589 if (cur_goal->ramp_influence_dists[3] > dist) {
590 // Get the maximum distance this goal has influence
591 dist = cur_goal->ramp_influence_dists[3];
592 }
593 } else {
594 // Influence isn't distnace based; so, check out to max dist
595 dist = MAX_NEAR_OBJ_SEARCH_DIST;
596 }
597 }
598 }
599
600 if (dist > 0.0f)
601 return dist + obj->size;
602 else
603 return 0.0f;
604 }
605
606 void GoalDoFrame(object *obj) {
607 ai_frame *ai_info = obj->ai_info;
608 goal *cur_goal;
609 int i;
610
611 goal *cur_task_goal = GoalGetCurrentGoal(obj);
612 if (ai_info->path.num_paths > 0 && (cur_task_goal == NULL || (cur_task_goal->goal_uid != ai_info->path.goal_uid))) {
613 #ifdef _DEBUG
614 if (AI_debug_robot_do && OBJNUM(obj) == AI_debug_robot_index) {
615 mprintf(0, "AI Note: In free path\n");
616 }
617 #endif
618 AIPathFreePath(&ai_info->path);
619 }
620
621 // Process all goals
622 cur_goal = &ai_info->goals[0];
623 for (i = 0; i < MAX_GOALS; i++) {
624 if (cur_goal->used) {
625 if ((cur_goal->flags & GF_CLEAR_IF_NOT_CURRENT_GOAL) && cur_goal != cur_task_goal) {
626 GoalClearGoal(obj, cur_goal);
627 continue;
628 }
629
630 // Does the
631 if (cur_goal->flags & GF_OBJ_IS_TARGET) {
632 cur_goal->g_info.handle = ai_info->target_handle;
633
634 if (ai_info->status_reg & AISR_SEES_GOAL) {
635 cur_goal->status_reg |= AISR_SEES_GOAL;
636 } else {
637 cur_goal->status_reg &= ~AISR_SEES_GOAL;
638 }
639 } else {
640 // chrishack -- Currently, we can only see target based goals and position goals
641 cur_goal->status_reg &= ~AISR_SEES_GOAL;
642 if (POSGOAL(cur_goal) && cur_goal->g_info.roomnum == obj->roomnum) {
643 cur_goal->status_reg |= AISR_SEES_GOAL;
644 }
645 }
646
647 if (cur_goal->flags & GF_CIRCLE_OBJ) {
648 int handle = cur_goal->g_info.handle;
649 object *goal_obj = ObjGet(handle);
650
651 if (cur_goal->flags & GF_OBJ_IS_TARGET) {
652 cur_goal->dist_to_goal = ai_info->dist_to_target_actual;
653 } else {
654 if (goal_obj) {
655 cur_goal->dist_to_goal = vm_VectorDistance(&obj->pos, &goal_obj->pos) - obj->size - goal_obj->size;
656 if (cur_goal->dist_to_goal < 0.0f) {
657 cur_goal->dist_to_goal = 0.0f;
658 }
659 }
660 }
661
662 if (goal_obj) {
663 AIStatusCircleFrame(obj, goal_obj, cur_goal->dist_to_goal, cur_goal->circle_distance, &cur_goal->status_reg);
664 } else {
665 cur_goal->status_reg &= ~AISR_CIRCLE_DIST;
666 }
667 } else if (POSGOAL(cur_goal)) {
668 float dist = vm_VectorDistance(&obj->pos, &cur_goal->g_info.pos);
669
670 // mprintf(0, "Dist is %f\n", dist);
671 // mprintf(0, "Dist %f, %f, %f\n", XYZ(&cur_goal->g_info.pos));
672 // mprintf(0, "Obj %f, %f, %f\n", XYZ(&obj->pos));
673
674 cur_goal->dist_to_goal = dist;
675 AIStatusCircleFrame(obj, NULL, dist, cur_goal->circle_distance, &cur_goal->status_reg);
676 }
677 }
678
679 cur_goal++;
680 }
681
682 // cur_task_goal might get cleared out above, so we should just recompute the current task goal
683 cur_goal = GoalGetCurrentGoal(obj);
684
685 if (cur_goal) {
686 int handle = OBJECT_HANDLE_NONE;
687 object *goal_obj = NULL;
688
689 if (cur_goal->flags & GF_FORCE_AWARENESS) {
690 if (obj->ai_info->awareness < AWARE_MOSTLY)
691 obj->ai_info->awareness = AWARE_MOSTLY;
692 }
693
694 // Does the save and restore of path information for static paths (i.e. if the path_info is
695 // taken away by a higher priority goal and then returned at a later time
696 if (cur_goal->type == AIG_FOLLOW_PATH) {
697 if (ai_info->path.goal_uid == cur_goal->goal_uid && ai_info->path.num_paths > 0) {
698 cur_goal->g_info.static_path_info.cur_node = ai_info->path.cur_node;
699 cur_goal->g_info.static_path_info.start_node = ai_info->path.path_start_node[0];
700 cur_goal->g_info.static_path_info.end_node = ai_info->path.path_end_node[0];
701 } else {
702 AIPathSetAsStaticPath(obj, cur_goal, cur_goal->g_info.id, cur_goal->g_info.static_path_info.start_node,
703 cur_goal->g_info.static_path_info.end_node, cur_goal->g_info.static_path_info.cur_node);
704 }
705 }
706
707 if (cur_goal->type == AIG_WANDER_AROUND) {
708 if (!(cur_goal->flags & GF_HAS_PATH) || ai_info->path.num_paths == 0) {
709 GoalInitWanderAround(obj, cur_goal);
710 }
711 /*
712 else if ((cur_goal->flags & GF_HAS_PATH) && ai_info->path.num_paths != 0) {
713 vector *posp = &cur_goal->g_info.pos;
714 float dist = vm_VectorDistance(
715 &AIDynamicPath[ai_info->path.num_paths - 1].pos[ai_info->path.path_end_node[ai_info->path.num_paths - 1]],
716 posp);
717
718 if (dist > 5.0f) {
719 mprintf(0, "In wander path case for obj %d - %s....\n", OBJNUM(obj), Object_info[obj->id].name);
720 cur_goal->next_path_time = Gametime - 1.0f;
721 }
722 }
723 */
724 }
725
726 if (OBJGOAL(cur_goal)) {
727 handle = cur_goal->g_info.handle;
728 goal_obj = ObjGet(handle);
729 }
730
731 // Automatically end
732 if ((COMPLETEATOBJ(cur_goal)) && (goal_obj) && (cur_goal->status_reg & AISR_CIRCLE_DIST)) {
733 GoalClearGoal(obj, cur_goal, AIN_GOAL_COMPLETE);
734 } else if ((COMPLETEATPOS(cur_goal)) && (cur_goal->status_reg & AISR_CIRCLE_DIST)) {
735 GoalClearGoal(obj, cur_goal, AIN_GOAL_COMPLETE);
736 } else if ((Gametime > cur_goal->next_path_time || ai_info->path.num_paths == 0) &&
737 (cur_goal->type != AIG_FOLLOW_PATH)) {
738 bool f_make_path = false;
739 int ignore_obj = -1;
740 bool f_kill_goal = false;
741 int kill_reason;
742
743 vector *posp;
744 int roomnum;
745
746 if (OBJGOAL(cur_goal)) {
747 if (goal_obj) {
748 // if(cur_goal->g_info.pos != goal_obj->pos)
749 f_make_path = true;
750 ignore_obj = OBJNUM(goal_obj);
751
752 posp = &goal_obj->pos;
753 roomnum = goal_obj->roomnum;
754
755 float dist = vm_VectorDistance(
756 &AIDynamicPath[ai_info->path.num_paths - 1].pos[ai_info->path.path_end_node[ai_info->path.num_paths - 1]],
757 posp);
758
759 if (dist < 5.0f && ai_info->path.num_paths != 0) {
760 int obj_room = BOA_INDEX(obj->roomnum);
761 int path_room = BOA_INDEX(AIDynamicPath[ai_info->path.cur_path].roomnum[ai_info->path.cur_node]);
762 vector path_pos = AIDynamicPath[ai_info->path.cur_path].pos[ai_info->path.cur_node];
763
764 if (obj_room != path_room) {
765 fvi_query fq;
766 fvi_info hit_info;
767
768 fq.p0 = &obj->pos;
769 fq.startroom = obj->roomnum;
770 fq.p1 = &path_pos;
771 fq.rad = obj->size / 4.0f;
772 fq.thisobjnum = OBJNUM(obj);
773 fq.ignore_obj_list = NULL;
774 fq.flags = FQ_CHECK_OBJS | FQ_NO_RELINK | FQ_IGNORE_NON_LIGHTMAP_OBJECTS;
775
776 if (fvi_FindIntersection(&fq, &hit_info) == HIT_NONE) {
777 mprintf(0, "AI OBJ Path: No need to update the path for obj %d\n", OBJNUM(obj));
778 f_make_path = false;
779 }
780 }
781 }
782 } else {
783 kill_reason = AIN_GOAL_INVALID;
784 f_kill_goal = true;
785 }
786 } else if (POSGOAL(cur_goal)) {
787 f_make_path = true;
788
789 posp = &cur_goal->g_info.pos;
790 roomnum = cur_goal->g_info.roomnum;
791
792 float dist = vm_VectorDistance(
793 &AIDynamicPath[ai_info->path.num_paths - 1].pos[ai_info->path.path_end_node[ai_info->path.num_paths - 1]],
794 posp);
795
796 if (dist < 5.0f && ai_info->path.num_paths != 0) {
797 int obj_room = BOA_INDEX(obj->roomnum);
798 int path_room = BOA_INDEX(AIDynamicPath[ai_info->path.cur_path].roomnum[ai_info->path.cur_node]);
799 vector path_pos = AIDynamicPath[ai_info->path.cur_path].pos[ai_info->path.cur_node];
800
801 if (obj_room != path_room) {
802 fvi_query fq;
803 fvi_info hit_info;
804
805 fq.p0 = &obj->pos;
806 fq.startroom = obj->roomnum;
807 fq.p1 = &path_pos;
808 fq.rad = obj->size / 4.0f;
809 fq.thisobjnum = OBJNUM(obj);
810 fq.ignore_obj_list = NULL;
811 fq.flags = FQ_CHECK_OBJS | FQ_NO_RELINK | FQ_IGNORE_NON_LIGHTMAP_OBJECTS;
812
813 if (fvi_FindIntersection(&fq, &hit_info) == HIT_NONE) {
814 mprintf(0, "AI POS Path: No need to update the path for obj %d\n", OBJNUM(obj));
815 f_make_path = false;
816 }
817 }
818 }
819 }
820
821 // If we are supposed to make it, do it.
822 if (f_make_path) {
823 AIPathAllocPath(obj, ai_info, cur_goal, &obj->roomnum, &obj->pos, &roomnum, posp, 0.0f, 0, obj->handle,
824 ignore_obj);
825 }
826
827 // Kill it or update the time
828 if (f_kill_goal) {
829 GoalClearGoal(obj, cur_goal, kill_reason);
830 } else {
831 // Update time regardless of if we made the path (so dont don't do this every frame
832 cur_goal->next_path_time = Gametime + MIN_NEXT_PATH_INTERVAL + ps_rand() / (float)D3_RAND_MAX;
833 }
834 }
835 }
836 }
837
838 void GoalPathComplete(object *obj) {
839 ai_frame *ai_info = obj->ai_info;
840 goal *cur_goal = GoalGetCurrentGoal(obj);
841
842 if (!cur_goal)
843 return;
844
845 if (COMPLETEATOBJ(cur_goal)) {
846 int handle = cur_goal->g_info.handle;
847 object *goal_obj = ObjGet(handle);
848
849 if ((goal_obj) && (cur_goal->g_info.pos != goal_obj->pos)) {
850 int ignore_obj = OBJNUM(goal_obj);
851
852 if (AIPathAllocPath(obj, ai_info, cur_goal, &obj->roomnum, &obj->pos, &goal_obj->roomnum, &goal_obj->pos, 0.0f, 0,
853 obj->handle, ignore_obj)) {
854 cur_goal->next_path_time = Gametime + MIN_NEXT_PATH_INTERVAL + ps_rand() / (float)D3_RAND_MAX;
855 }
856 } else {
857 GoalClearGoal(obj, cur_goal, AIN_GOAL_COMPLETE);
858 }
859 } else {
860 GoalClearGoal(obj, cur_goal, AIN_GOAL_COMPLETE);
861 }
862 }
863
864 void GoalClearAll(object *obj) {
865 int i;
866 ai_frame *ai_info = obj->ai_info;
867
868 for (i = 0; i < MAX_GOALS; i++) {
869 GoalClearGoal(obj, &ai_info->goals[i]);
870 }
871 }
872
873 int GoalAllocSlot(object *obj, int level, float influence) {
874 float lowest_influence_slot = -1.0;
875 float lowest_influence = MAX_INFLUENCE + 1.0;
876 int alloc_slot = AI_INVALID_INDEX;
877 ai_frame *ai_info = obj->ai_info;
878 int cur_slot;
879
880 ASSERT((level >= 0 && level < NUM_ACTIVATION_LEVELS) || (level == ACTIVATION_BLEND_LEVEL));
881
882 if (influence > MAX_INFLUENCE) {
883 mprintf(0, "Goal added with too much influence -- bashing down\n");
884 influence = MAX_INFLUENCE;
885 } else if (influence < 0.0f) {
886 mprintf(0, "Goal added with negative influence -- bashing to zero\n");
887 influence = 0.0f;
888 }
889
890 if (level < NUM_ACTIVATION_LEVELS) {
891 cur_slot = level;
892
893 if (level < 0) {
894 mprintf(0, "AI: Bashed an invalid activation level to zero\n");
895 level = 0;
896 }
897
898 if (ai_info->goals[cur_slot].used) {
899 if (!(ai_info->goals[cur_slot].flags & GF_NONFLUSHABLE))
900 GoalClearGoal(obj, &ai_info->goals[cur_slot]);
901 else
902 cur_slot = AI_INVALID_INDEX;
903 }
904 } else {
905 cur_slot = NUM_ACTIVATION_LEVELS;
906 goal *cur_goal = &ai_info->goals[cur_slot];
907
908 if (level != ACTIVATION_BLEND_LEVEL) {
909 mprintf(0, "AI: Bashed an invalid activation blend level to blend level\n");
910 level = ACTIVATION_BLEND_LEVEL;
911 }
912
913 while (cur_slot < MAX_GOALS && ai_info->goals[cur_slot].used == true) {
914
915 if (!(cur_goal->used)) {
916 lowest_influence_slot = cur_slot;
917 lowest_influence = -1.0f;
918 }
919
920 if (cur_goal->influence < lowest_influence && !(cur_goal->flags & GF_NONFLUSHABLE)) {
921 lowest_influence_slot = cur_slot;
922 lowest_influence = cur_goal->influence;
923 }
924
925 cur_goal++;
926 cur_slot++;
927 }
928
929 // If there are no free slots but the lowest influence goal has less than the new goal's
930 // influence, kill the old goal
931 if (cur_slot >= MAX_GOALS && lowest_influence < influence) {
932 cur_slot = lowest_influence_slot;
933 GoalClearGoal(obj, &ai_info->goals[cur_slot]);
934 }
935 }
936
937 if (cur_slot < MAX_GOALS) {
938 ai_info->goals[cur_slot].used = true;
939 } else {
940 cur_slot = AI_INVALID_INDEX;
941 }
942
943 return cur_slot;
944 }
945
946 // Adds a goal to the object
947 int GoalAddGoal(object *obj, uint32_t goal_type, void *arg_struct, int level, float influence, int f_goal, int guid,
948 char subtype) {
949 ai_frame *ai_info = obj->ai_info;
950 int goal_index = AI_INVALID_INDEX;
951 goal *goal_ptr = NULL;
952
953 if (!(goal_type & (AIG_SET_ANIM | AIG_DO_MELEE_ANIM | AIG_FIRE_AT_OBJ))) {
954 goal_index = GoalAllocSlot(obj, level, influence);
955 if (goal_index == AI_INVALID_INDEX) {
956 // mprintf(0, "WARNING: Object %d has too many goals\n", OBJNUM(obj));
957 return AI_INVALID_INDEX;
958 }
959
960 goal_ptr = &ai_info->goals[goal_index];
961
962 goal_ptr->type = goal_type;
963 goal_ptr->influence = influence;
964 goal_ptr->start_time = Gametime;
965 goal_ptr->flags = f_goal;
966 goal_ptr->status_reg = 0;
967 goal_ptr->num_enablers = 0;
968 goal_ptr->circle_distance = ai_info->circle_distance;
969 goal_ptr->guid = guid;
970 goal_ptr->creation_time = Gametime;
971 goal_ptr->subtype = subtype;
972 goal_ptr->goal_uid = AI_unique_goal_id++;
973 }
974
975 switch (goal_type) {
976 case AIG_WANDER_AROUND: {
977 goal_ptr->next_path_time = Gametime;
978 } break;
979
980 case AIG_SCRIPTED:
981 goal_ptr->g_info.scripted_data_ptr = arg_struct;
982 break;
983
984 case AIG_DODGE_OBJ:
985 case AIG_GET_AROUND_OBJ:
986 case AIG_MOVE_RELATIVE_OBJ:
987 goal_ptr->g_info.handle = *((int *)arg_struct);
988 break;
989
990 case AIG_MOVE_RELATIVE_OBJ_VEC:
991 goal_ptr->g_info.handle = ((goal_info *)arg_struct)->handle;
992 break;
993
994 case AIG_GUARD_AREA:
995 goal_ptr->g_info.roomnum = ((goal_info *)arg_struct)->roomnum;
996 goal_ptr->g_info.pos = ((goal_info *)arg_struct)->pos;
997 break;
998
999 case AIG_PLACE_OBJ_ON_OBJ: {
1000 int handle = ((goal_info *)arg_struct)->handle;
1001 object *goal_obj = ObjGet(handle);
1002
1003 goal_ptr->g_info.attach_info.flags = 0;
1004
1005 if (goal_obj) {
1006 goal_ptr->g_info = *((goal_info *)arg_struct);
1007 goal_ptr->g_info.pos = goal_obj->pos;
1008 int ignore_obj = OBJNUM(goal_obj);
1009
1010 if (AIPathAllocPath(obj, ai_info, &ai_info->goals[goal_index], &obj->roomnum, &obj->pos, &goal_obj->roomnum,
1011 &goal_obj->pos, 0.0f, 0, obj->handle, ignore_obj)) {
1012 goal_ptr->next_path_time = Gametime + MIN_NEXT_PATH_INTERVAL + ps_rand() / (float)D3_RAND_MAX;
1013 } else {
1014 goal_ptr->next_path_time = Gametime;
1015 }
1016 } else
1017 goal_ptr->g_info.handle = OBJECT_HANDLE_NONE;
1018 } break;
1019
1020 case AIG_ATTACH_TO_OBJ: {
1021 int handle = ((goal_info *)arg_struct)->handle;
1022 object *goal_obj = ObjGet(handle);
1023
1024 goal_ptr->g_info.attach_info.flags = 0;
1025
1026 if (goal_obj) {
1027 goal_ptr->g_info = *((goal_info *)arg_struct);
1028 goal_ptr->g_info.pos = goal_obj->pos;
1029 int ignore_obj = OBJNUM(goal_obj);
1030
1031 if (AIPathAllocPath(obj, ai_info, &ai_info->goals[goal_index], &obj->roomnum, &obj->pos, &goal_obj->roomnum,
1032 &goal_obj->pos, 0.0f, 0, obj->handle, ignore_obj)) {
1033 goal_ptr->next_path_time = Gametime + MIN_NEXT_PATH_INTERVAL + ps_rand() / (float)D3_RAND_MAX;
1034 } else {
1035 goal_ptr->next_path_time = Gametime;
1036 }
1037 } else
1038 goal_ptr->g_info.handle = OBJECT_HANDLE_NONE;
1039 } break;
1040
1041 case AIG_GET_TO_OBJ: {
1042 int handle = *((int *)arg_struct);
1043 object *goal_obj = ObjGet(handle);
1044
1045 if (goal_obj) {
1046 goal_ptr->g_info.handle = handle;
1047 goal_ptr->g_info.pos = goal_obj->pos;
1048 } else
1049 goal_ptr->g_info.handle = OBJECT_HANDLE_NONE;
1050
1051 goal_ptr->next_path_time = Gametime;
1052 } break;
1053
1054 case AIG_GET_TO_POS: {
1055 int roomnum = ((goal_info *)arg_struct)->roomnum;
1056 vector pos = ((goal_info *)arg_struct)->pos;
1057
1058 goal_ptr->g_info.pos = pos;
1059 goal_ptr->g_info.roomnum = roomnum;
1060
1061 goal_ptr->next_path_time = Gametime;
1062 } break;
1063
1064 case AIG_FOLLOW_PATH: {
1065 path_information path_info = *((path_information *)arg_struct);
1066
1067 goal_ptr->g_info.id = path_info.path_id;
1068 goal_ptr->g_info.static_path_info.cur_node = path_info.next_node;
1069 goal_ptr->g_info.static_path_info.start_node = path_info.start_node;
1070 goal_ptr->g_info.static_path_info.end_node = path_info.end_node;
1071
1072 goal_ptr->next_path_time = Gametime;
1073 } break;
1074
1075 case AIG_FIRE_AT_OBJ: {
1076 gi_fire *attack_info = (gi_fire *)arg_struct;
1077 if (attack_info->cur_wb > MAX_WBS_PER_OBJ) { // DAJ
1078 mprintf(2, "GoalAddGoal wb_index %d > MAX_WBS_PER_OBJ\n", attack_info->cur_wb);
1079 return 0;
1080 }
1081 if ((ai_info->animation_type == AS_ALERT && !(ai_info->next_animation_type == AS_FLINCH)) ||
1082 ai_info->animation_type == AS_RANGED_RECOIL) {
1083 ai_info->current_wb_firing = attack_info->cur_wb;
1084 obj->dynamic_wb[attack_info->cur_wb].wb_anim_mask = attack_info->cur_mask;
1085
1086 obj->rtype.pobj_info.anim_flags |= AIAF_NOTIFY;
1087 ai_info->next_animation_type = AS_RANGED_ATTACK;
1088
1089 if (Object_info[obj->id].static_wb[attack_info->cur_wb].flags & (WBF_SPRAY | WBF_ON_OFF)) {
1090 ai_info->last_special_wb_firing = (char)attack_info->cur_wb;
1091 }
1092
1093 return 0;
1094 } else if (ai_info->animation_type == AS_IDLE) {
1095 obj->rtype.pobj_info.anim_flags |= AIAF_NOTIFY;
1096 ai_info->next_animation_type = AS_GOTO_ALERT_OFFSET + ai_info->movement_type;
1097 }
1098 } break;
1099
1100 case AIG_DO_MELEE_ANIM: {
1101 gi_fire *attack_info = (gi_fire *)arg_struct;
1102 if ((ai_info->animation_type == AS_ALERT) || (ai_info->animation_type == AS_MELEE1 + 1) ||
1103 (ai_info->animation_type == AS_MELEE2 + 1)) {
1104 uint8_t m_num = attack_info->melee_number;
1105 char new_anim;
1106
1107 if (m_num)
1108 new_anim = AS_MELEE1;
1109 else
1110 new_anim = AS_MELEE2;
1111
1112 obj->rtype.pobj_info.anim_flags |= AIAF_NOTIFY;
1113 ai_info->next_animation_type = new_anim;
1114 return 0;
1115 } else if (ai_info->animation_type == AS_IDLE) {
1116 obj->rtype.pobj_info.anim_flags |= AIAF_NOTIFY;
1117 ai_info->next_animation_type = AS_GOTO_ALERT_OFFSET + ai_info->movement_type;
1118 }
1119 } break;
1120
1121 case AIG_MELEE_TARGET:
1122 break;
1123
1124 case AIG_SET_ANIM: {
1125 char new_anim = *((int *)arg_struct);
1126 polyobj_info *p_info = &obj->rtype.pobj_info;
1127
1128 // mprintf(0, "Anim Goal %d\n", new_anim);
1129 // Custom animations cannot be overriden
1130 if (ai_info->next_animation_type == AS_CUSTOM) {
1131 return -1;
1132 }
1133
1134 if (new_anim >= 0 && new_anim < NUM_ANIMS_PER_CLASS) {
1135 if ((Object_info[obj->id].anim[ai_info->movement_type].elem[ai_info->animation_type].from ==
1136 Object_info[obj->id].anim[ai_info->movement_type].elem[new_anim].from) &&
1137 (Object_info[obj->id].anim[ai_info->movement_type].elem[ai_info->animation_type].to ==
1138 Object_info[obj->id].anim[ai_info->movement_type].elem[new_anim].to) &&
1139 (Object_info[obj->id].anim[ai_info->movement_type].elem[ai_info->animation_type].to) &&
1140 (Object_info[obj->id].anim[ai_info->movement_type].elem[new_anim].to)) {
1141 ai_info->next_animation_type = new_anim;
1142 AIUpdateAnim(obj);
1143 return 0;
1144 }
1145 }
1146
1147 if (ai_info->animation_type == AS_ALERT) {
1148 if (new_anim == AS_FLINCH) // Get to flinch quickly
1149 {
1150 if (p_info->anim_end_frame != p_info->anim_start_frame) {
1151 float time_per_frame = p_info->anim_time / (p_info->anim_end_frame - p_info->anim_start_frame);
1152 float time_left = time_per_frame * (p_info->anim_end_frame - p_info->anim_frame);
1153
1154 if (time_left > MAX_BASH_TO_FLINCH_TIME) {
1155 return -1;
1156 }
1157 }
1158 p_info->anim_time /= BASH_TO_ANIM_SCALER;
1159 } else if (new_anim == AS_TAUNT) {
1160 if (ai_info->next_animation_type != AI_INVALID_INDEX) {
1161 return -1;
1162 }
1163 } else if (new_anim == AS_IDLE) {
1164 new_anim = AS_GOTO_IDLE_OFFSET + ai_info->movement_type;
1165
1166 if (Object_info[obj->id].anim[ai_info->movement_type].elem[new_anim].to == 0.0f) {
1167 return -1;
1168 }
1169 }
1170
1171 obj->rtype.pobj_info.anim_flags |= AIAF_NOTIFY;
1172 ai_info->next_animation_type = new_anim;
1173 return 0;
1174 } else if (ai_info->animation_type == AS_IDLE) {
1175 if ((new_anim == AS_ALERT) ||
1176 (new_anim == AS_QUIRK && Object_info[obj->id].anim[ai_info->movement_type].elem[new_anim].to != 0.0f)) {
1177 obj->rtype.pobj_info.anim_flags |= AIAF_NOTIFY;
1178
1179 if (new_anim == AS_ALERT)
1180 ai_info->next_animation_type = AS_GOTO_ALERT_OFFSET + ai_info->movement_type;
1181 else
1182 ai_info->next_animation_type = new_anim;
1183 }
1184 return 0;
1185 } else if (ai_info->animation_type == AS_QUIRK || ai_info->animation_type == AS_BIRTH) {
1186 if (new_anim == AS_ALERT) {
1187 ai_info->next_animation_type = AS_GOTO_ALERT_OFFSET + ai_info->movement_type;
1188 return 0;
1189 }
1190
1191 return -1;
1192 }
1193 } break;
1194
1195 default:
1196 ASSERT(0);
1197 break;
1198 }
1199
1200 // Do init. stuff
1201 if (goal_index != AI_INVALID_INDEX) {
1202 if (OBJGOAL(&ai_info->goals[goal_index])) {
1203 goal_ptr->flags |= GF_CIRCLE_OBJ;
1204 } else if (POSGOAL(&ai_info->goals[goal_index])) {
1205 goal_ptr->flags |= GF_CIRCLE_POS;
1206 }
1207
1208 if (TARGETONLYGOAL(&ai_info->goals[goal_index])) {
1209 goal_ptr->flags |= GF_OBJ_IS_TARGET;
1210 }
1211
1212 if (goal_ptr->flags & GF_OBJ_IS_TARGET) {
1213 goal_ptr->g_info.handle = ai_info->target_handle;
1214 if (ai_info->status_reg & AISR_CIRCLE_DIST) {
1215 goal_ptr->status_reg |= AISR_CIRCLE_DIST;
1216 } else {
1217 goal_ptr->status_reg &= ~AISR_CIRCLE_DIST;
1218 }
1219
1220 goal_ptr->dist_to_goal = ai_info->dist_to_target_actual;
1221 }
1222
1223 goal *cur_goal = GoalGetCurrentGoal(obj);
1224 if (cur_goal != NULL && cur_goal == goal_ptr) {
1225 if (cur_goal->flags & GF_FORCE_AWARENESS) {
1226 if (obj->ai_info->awareness < AWARE_MOSTLY)
1227 obj->ai_info->awareness = AWARE_MOSTLY;
1228 }
1229 }
1230 }
1231
1232 return goal_index;
1233 }
1234
1235 int GoalAddEnabler(object *obj, int goal_index, uint8_t enabler_type, void *arg_struct, float percent, float interval) {
1236 ai_frame *ai_info = obj->ai_info;
1237 goal_enabler *e_info;
1238
1239 int enabler_index = ai_info->goals[goal_index].num_enablers;
1240
1241 if (ai_info->goals[goal_index].num_enablers >= MAX_ENABLERS_PER_GOAL) {
1242 mprintf(0, "Object %d with goal %d has to many enablers\n", OBJNUM(obj), goal_index);
1243 return AI_INVALID_INDEX;
1244 }
1245
1246 e_info = &ai_info->goals[goal_index].enabler[enabler_index];
1247
1248 e_info->enabler_type = enabler_type;
1249 e_info->percent_enable = percent;
1250 e_info->check_interval = interval;
1251 e_info->last_check_time = Gametime;
1252
1253 // Hmmm... What about 2 seconds to be enabled???? I think it is possible --chrishack
1254 switch (enabler_type) {
1255 case (AIE_AI_STATUS_FLAG):
1256 e_info->flags = *((int *)arg_struct);
1257 break;
1258 case (AIE_NEAR):
1259 case (AIE_FAR):
1260 e_info->dist = *((float *)arg_struct);
1261 break;
1262 case (AIE_SCRIPTED):
1263 break;
1264 case (AIE_GT_AWARENESS):
1265 case (AIE_LTE_AWARENESS):
1266 e_info->awareness = *((float *)arg_struct);
1267 break;
1268 case (AIE_GT_LAST_SEE_TARGET_INTERVAL):
1269 case (AIE_LTE_LAST_SEE_TARGET_INTERVAL):
1270 e_info->time = *((float *)arg_struct);
1271 break;
1272 case (AIE_FEAR):
1273 e_info->float_value = *((float *)arg_struct);
1274 break;
1275 case (AIE_ANGRY):
1276 e_info->float_value = *((float *)arg_struct);
1277 break;
1278 case (AIE_CURIOUS):
1279 e_info->float_value = *((float *)arg_struct);
1280 break;
1281 case (AIE_FRUSTRATED):
1282 e_info->float_value = *((float *)arg_struct);
1283 break;
1284 case (AIE_DELAY_TIME):
1285 e_info->time = *((float *)arg_struct);
1286 break;
1287 case (AIE_CLEAR_TIME):
1288 e_info->time = *((float *)arg_struct);
1289 break;
1290 default:
1291 ASSERT(0);
1292 break;
1293 }
1294
1295 ai_info->goals[goal_index].num_enablers++;
1296
1297 return enabler_index;
1298 }
1299
1300 void GoalInitTypeGoals(object *obj, int ai_type) {
1301 ai_frame *ai_info = obj->ai_info;
1302 float aware = AWARE_BARELY;
1303 float time = 25.0f;
1304
1305 GoalClearAll(obj);
1306
1307 // This goal has a proirity of 50. Mabye I should split up intermediate goals
1308 // and long term goals. -- Wander around within 2000 units of your starting
1309 // position (goal has priority of 100) The AI type decides what to do with
1310 // a goal when it is done.
1311 switch (ai_type) {
1312 case AIT_STALKER: {
1313 int goal_room;
1314
1315 goal_room = obj->roomnum;
1316
1317 GoalAddGoal(obj, AIG_WANDER_AROUND, (void *)&goal_room, 1, 1.0f, GF_NONFLUSHABLE | GF_KEEP_AT_COMPLETION);
1318 // GoalAddEnabler(obj, 1, AIE_GT_AWARENESS, (void *)&aware, 1.0, 0.0);
1319 // GoalAddEnabler(obj, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, (void *)&time, 1.0, 0.0);
1320 } break;
1321
1322 case AIT_EVADER2: {
1323 goal_info g_info;
1324
1325 g_info.roomnum = obj->roomnum;
1326 g_info.pos = obj->pos;
1327
1328 GoalAddGoal(obj, AIG_GUARD_AREA, (void *)&g_info, 1, 1.0f, GF_NONFLUSHABLE);
1329 obj->ai_info->goals[1].circle_distance = obj->size * 2.0f;
1330 GoalAddEnabler(obj, 1, AIE_GT_AWARENESS, (void *)&aware, 1.0, 0.0);
1331 GoalAddEnabler(obj, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, (void *)&time, 1.0, 0.0);
1332
1333 int handle = OBJECT_HANDLE_NONE;
1334
1335 int gi = GoalAddGoal(obj, AIG_GET_AROUND_OBJ, (void *)&handle, ACTIVATION_BLEND_LEVEL, 3.0f,
1336 GF_NONFLUSHABLE | GF_OBJ_IS_TARGET);
1337 if (gi != -1) { // DAJ -1FIX
1338 float temp = ai_info->circle_distance;
1339 GoalAddEnabler(obj, gi, AIE_NEAR, (void *)&temp, 1.0, 0.0);
1340 ai_info->goals[gi].circle_distance = ai_info->circle_distance;
1341 }
1342 } break;
1343
1344 case AIT_EVADER1: {
1345 goal_info g_info;
1346 g_info.handle = ai_info->target_handle;
1347
1348 GoalAddGoal(obj, AIG_MOVE_RELATIVE_OBJ, (void *)&g_info, 1, 1.0f, GF_SPEED_ATTACK | GF_NONFLUSHABLE);
1349 GoalAddEnabler(obj, 1, AIE_GT_AWARENESS, (void *)&aware, 1.0, 0.0);
1350 GoalAddEnabler(obj, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, (void *)&time, 1.0, 0.0);
1351 ai_info->goals[1].flags |= GF_OBJ_IS_TARGET;
1352
1353 if (ai_info->circle_distance > 0.0f) {
1354 int handle = OBJECT_HANDLE_NONE;
1355 int gi = GoalAddGoal(obj, AIG_GET_AROUND_OBJ, (void *)&handle, ACTIVATION_BLEND_LEVEL, 3.0f,
1356 GF_NONFLUSHABLE | GF_OBJ_IS_TARGET);
1357 if (gi != -1) { // DAJ -1FIX
1358 float temp = ai_info->circle_distance * 0.55f;
1359 GoalAddEnabler(obj, gi, AIE_NEAR, (void *)&temp, 1.0, 0.0);
1360 ai_info->goals[gi].circle_distance = ai_info->circle_distance * 0.55f;
1361 }
1362 }
1363 } break;
1364
1365 case AIT_MELEE1: {
1366 goal_info g_info;
1367 int flags = AISR_MELEE;
1368 int m_goal;
1369
1370 g_info.handle = ai_info->target_handle;
1371
1372 GoalAddGoal(obj, AIG_MOVE_RELATIVE_OBJ, (void *)&g_info, 1, 1.0f,
1373 GF_NONFLUSHABLE | GF_OBJ_IS_TARGET | GF_KEEP_AT_COMPLETION);
1374 GoalAddEnabler(obj, 1, AIE_GT_AWARENESS, (void *)&aware, 1.0, 0.0);
1375 GoalAddEnabler(obj, 1, AIE_LTE_LAST_SEE_TARGET_INTERVAL, (void *)&time, 1.0, 0.0);
1376
1377 GoalAddGoal(obj, AIG_GET_TO_OBJ, (void *)&ai_info->target_handle, 2, 1.0f,
1378 GF_SPEED_ATTACK | GF_NONFLUSHABLE | GF_OBJ_IS_TARGET | GF_USE_BLINE_IF_SEES_GOAL |
1379 GF_KEEP_AT_COMPLETION);
1380 GoalAddEnabler(obj, 2, AIE_AI_STATUS_FLAG, (void *)&flags, 1.0, 0.0);
1381 ai_info->goals[2].circle_distance = -100.0f;
1382 GoalAddEnabler(obj, 2, AIE_GT_AWARENESS, (void *)&aware, 1.0, 0.0);
1383 GoalAddEnabler(obj, 2, AIE_LTE_LAST_SEE_TARGET_INTERVAL, (void *)&time, 1.0, 0.0);
1384
1385 m_goal = GoalAddGoal(obj, AIG_MELEE_TARGET, NULL, ACTIVATION_BLEND_LEVEL, 1.0f, GF_NONFLUSHABLE);
1386 if (m_goal != -1) { // DAJ -1FIX
1387 ai_info->goals[m_goal].flags |= GF_OBJ_IS_TARGET | GF_KEEP_AT_COMPLETION;
1388 GoalAddEnabler(obj, m_goal, AIE_GT_AWARENESS, (void *)&aware, 1.0, 0.0);
1389 GoalAddEnabler(obj, m_goal, AIE_LTE_LAST_SEE_TARGET_INTERVAL, (void *)&time, 1.0, 0.0);
1390 }
1391 } break;
1392
1393 case AIT_BIRD_FLOCK1: {
1394 } break;
1395
1396 case AIT_HERD1: {
1397 } break;
1398
1399 case AIT_STATIONARY_TURRET:
1400 break;
1401
1402 case AIT_AIS:
1403 break;
1404
1405 case AIT_FLYLANDER:
1406 break;
1407
1408 default: {
1409 Int3();
1410 }
1411 }
1412 }
1413
1414 bool GoalIsGoalEnabled(object *obj, int goal_index) {
1415 ai_frame *ai_info = obj->ai_info;
1416 goal *cur_goal = &ai_info->goals[goal_index];
1417 bool f_enabled = true;
1418 int i;
1419 goal_enabler *e_info = &cur_goal->enabler[0];
1420
1421 ASSERT(goal_index >= 0 && goal_index < MAX_GOALS);
1422
1423 if (OBJGOAL(cur_goal)) {
1424 object *gobj = NULL;
1425
1426 if (cur_goal->flags & GF_OBJ_IS_TARGET) {
1427 gobj = ObjGet(obj->ai_info->target_handle);
1428 } else if (cur_goal->flags & (GF_OBJS_ARE_SPECIES | GF_OBJS_ARE_ENEMIES | GF_OBJS_ARE_FRIENDS)) {
1429 bool done = false;
1430
1431 if (cur_goal->flags & (GF_OBJS_ARE_SPECIES | GF_OBJS_ARE_FRIENDS)) {
1432 // chrishack -- not done
1433 }
1434 } else {
1435 gobj = ObjGet(cur_goal->g_info.handle);
1436 }
1437
1438 if (gobj == NULL && cur_goal->type == AIG_DODGE_OBJ)
1439 return true;
1440
1441 if (!gobj || gobj->type == OBJ_GHOST || (gobj->flags & (OF_DYING | OF_DESTROYED)) ||
1442 (gobj->type == OBJ_PLAYER && Players[gobj->id].flags & (PLAYER_FLAGS_DEAD + PLAYER_FLAGS_DYING))) {
1443 return false;
1444 }
1445 }
1446
1447 if (cur_goal->type == AIG_MELEE_TARGET) {
1448 if (ai_info->flags & AIF_DISABLE_MELEE) {
1449 return false;
1450 }
1451 }
1452
1453 if (cur_goal->num_enablers == 0) {
1454 return true;
1455 }
1456
1457 for (i = 0; i < cur_goal->num_enablers; i++) {
1458 // Hmmm... What about 2 seconds to be enabled???? I think it is possible --chrishack
1459 switch (e_info->enabler_type) {
1460 case (AIE_AI_STATUS_FLAG):
1461 f_enabled = ((e_info->flags) & (ai_info->status_reg)) != 0;
1462 break;
1463
1464 case (AIE_NEAR): {
1465 float a_dist = e_info->dist;
1466 f_enabled = (cur_goal->dist_to_goal <= a_dist) != 0;
1467 } break;
1468 case (AIE_FAR): {
1469 float a_dist = e_info->dist;
1470 f_enabled = (cur_goal->dist_to_goal >= a_dist) != 0;
1471 } break;
1472 case (AIE_SCRIPTED): {
1473 notify se_info;
1474 se_info.goal_num = goal_index;
1475 se_info.enabler_num = i;
1476
1477 f_enabled = AINotify(obj, AIN_SCRIPTED_ENABLER, &se_info);
1478 } break;
1479 case (AIE_GT_AWARENESS): {
1480 f_enabled = (ai_info->awareness > e_info->awareness);
1481 } break;
1482 case (AIE_LTE_AWARENESS): {
1483 f_enabled = (ai_info->awareness <= e_info->awareness);
1484 } break;
1485 case (AIE_LTE_LAST_SEE_TARGET_INTERVAL): {
1486 f_enabled = (Gametime - ai_info->last_see_target_time <= e_info->time) ||
1487 (Gametime - ai_info->last_hear_target_time <= e_info->time);
1488 } break;
1489 case (AIE_GT_LAST_SEE_TARGET_INTERVAL): {
1490 f_enabled = (Gametime - ai_info->last_see_target_time > e_info->time) ||
1491 (Gametime - ai_info->last_hear_target_time > e_info->time);
1492 } break;
1493 case (AIE_FEAR): {
1494 f_enabled = e_info->float_value <= ai_info->cur_life_preservation;
1495 } break;
1496 case (AIE_ANGRY): {
1497 f_enabled = e_info->float_value <= ai_info->cur_agression;
1498 } break;
1499 case (AIE_CURIOUS): {
1500 f_enabled = e_info->float_value <= ai_info->cur_curiousity;
1501 } break;
1502 case (AIE_FRUSTRATED): {
1503 f_enabled = e_info->float_value <= ai_info->cur_frustration;
1504 } break;
1505 case (AIE_DELAY_TIME): {
1506 f_enabled = e_info->time < Gametime - cur_goal->creation_time;
1507 } break;
1508 case (AIE_CLEAR_TIME): {
1509 f_enabled = e_info->time >= Gametime - cur_goal->creation_time;
1510 if (!f_enabled) {
1511 GoalClearGoal(obj, cur_goal);
1512 }
1513 } break;
1514 default:
1515 ASSERT(0);
1516 break;
1517 }
1518
1519 // We are currently and'ing enablers
1520 if (f_enabled == false)
1521 break;
1522
1523 e_info++;
1524 }
1525
1526 return f_enabled;
1527 }
1528
1529 goal *GoalGetCurrentGoal(object *obj) {
1530 ai_frame *ai_info = obj->ai_info;
1531 int i;
1532 goal *cur_goal = NULL;
1533
1534 for (i = NUM_ACTIVATION_LEVELS - 1; i >= 0; i--) {
1535 if (ai_info->goals[i].used && GoalIsGoalEnabled(obj, i)) {
1536 cur_goal = &ai_info->goals[i];
1537 break;
1538 }
1539 }
1540
1541 return cur_goal;
1542 }