Debian Codegraph @

code/botlib/be_ai_goal.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_ai_goal.c
25 *
26 * desc: goal AI
27 *
28 * $Archive: /MissionPack/code/botlib/be_ai_goal.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_utils.h"
34 #include "l_libvar.h"
35 #include "l_memory.h"
36 #include "l_log.h"
37 #include "l_script.h"
38 #include "l_precomp.h"
39 #include "l_struct.h"
40 #include "aasfile.h"
41 #include "botlib.h"
42 #include "be_aas.h"
43 #include "be_aas_funcs.h"
44 #include "be_interface.h"
45 #include "be_ai_weight.h"
46 #include "be_ai_goal.h"
47 #include "be_ai_move.h"
48
49 //#define DEBUG_AI_GOAL
50 #ifdef RANDOMIZE
51 #define UNDECIDEDFUZZY
52 #endif //RANDOMIZE
53 #define DROPPEDWEIGHT
54 //minimum avoid goal time
55 #define AVOID_MINIMUM_TIME 10
56 //default avoid goal time
57 #define AVOID_DEFAULT_TIME 30
58 //avoid dropped goal time
59 #define AVOID_DROPPED_TIME 10
60 //
61 #define TRAVELTIME_SCALE 0.01
62 //item flags
63 #define IFL_NOTFREE 1 //not in free for all
64 #define IFL_NOTTEAM 2 //not in team play
65 #define IFL_NOTSINGLE 4 //not in single player
66 #define IFL_NOTBOT 8 //bot should never go for this
67 #define IFL_ROAM 16 //bot roam goal
68
69 //location in the map "target_location"
70 typedef struct maplocation_s
71 {
72 vec3_t origin;
73 int areanum;
74 char name[MAX_EPAIRKEY];
75 struct maplocation_s *next;
76 } maplocation_t;
77
78 //camp spots "info_camp"
79 typedef struct campspot_s
80 {
81 vec3_t origin;
82 int areanum;
83 char name[MAX_EPAIRKEY];
84 float range;
85 float weight;
86 float wait;
87 float random;
88 struct campspot_s *next;
89 } campspot_t;
90
91 //FIXME: these are game specific
92 typedef enum {
93 GT_FFA, // free for all
94 GT_TOURNAMENT, // one on one tournament
95 GT_SINGLE_PLAYER, // single player tournament
96
97 //-- team games go after this --
98
99 GT_TEAM, // team deathmatch
100 GT_CTF, // capture the flag
101 #ifdef MISSIONPACK
102 GT_1FCTF,
103 GT_OBELISK,
104 GT_HARVESTER,
105 #endif
106 GT_MAX_GAME_TYPE
107 } gametype_t;
108
109 typedef struct levelitem_s
110 {
111 int number; //number of the level item
112 int iteminfo; //index into the item info
113 int flags; //item flags
114 float weight; //fixed roam weight
115 vec3_t origin; //origin of the item
116 int goalareanum; //area the item is in
117 vec3_t goalorigin; //goal origin within the area
118 int entitynum; //entity number
119 float timeout; //item is removed after this time
120 struct levelitem_s *prev, *next;
121 } levelitem_t;
122
123 typedef struct iteminfo_s
124 {
125 char classname[32]; //classname of the item
126 char name[MAX_STRINGFIELD]; //name of the item
127 char model[MAX_STRINGFIELD]; //model of the item
128 int modelindex; //model index
129 int type; //item type
130 int index; //index in the inventory
131 float respawntime; //respawn time
132 vec3_t mins; //mins of the item
133 vec3_t maxs; //maxs of the item
134 int number; //number of the item info
135 } iteminfo_t;
136
137 #define ITEMINFO_OFS(x) (size_t)&(((iteminfo_t *)0)->x)
138
139 fielddef_t iteminfo_fields[] =
140 {
141 {"name", ITEMINFO_OFS(name), FT_STRING},
142 {"model", ITEMINFO_OFS(model), FT_STRING},
143 {"modelindex", ITEMINFO_OFS(modelindex), FT_INT},
144 {"type", ITEMINFO_OFS(type), FT_INT},
145 {"index", ITEMINFO_OFS(index), FT_INT},
146 {"respawntime", ITEMINFO_OFS(respawntime), FT_FLOAT},
147 {"mins", ITEMINFO_OFS(mins), FT_FLOAT|FT_ARRAY, 3},
148 {"maxs", ITEMINFO_OFS(maxs), FT_FLOAT|FT_ARRAY, 3},
149 {NULL, 0, 0}
150 };
151
152 structdef_t iteminfo_struct =
153 {
154 sizeof(iteminfo_t), iteminfo_fields
155 };
156
157 typedef struct itemconfig_s
158 {
159 int numiteminfo;
160 iteminfo_t *iteminfo;
161 } itemconfig_t;
162
163 //goal state
164 typedef struct bot_goalstate_s
165 {
166 struct weightconfig_s *itemweightconfig; //weight config
167 int *itemweightindex; //index from item to weight
168 //
169 int client; //client using this goal state
170 int lastreachabilityarea; //last area with reachabilities the bot was in
171 //
172 bot_goal_t goalstack[MAX_GOALSTACK]; //goal stack
173 int goalstacktop; //the top of the goal stack
174 //
175 int avoidgoals[MAX_AVOIDGOALS]; //goals to avoid
176 float avoidgoaltimes[MAX_AVOIDGOALS]; //times to avoid the goals
177 } bot_goalstate_t;
178
179 bot_goalstate_t *botgoalstates[MAX_CLIENTS + 1]; // FIXME: init?
180 //item configuration
181 itemconfig_t *itemconfig = NULL;
182 //level items
183 levelitem_t *levelitemheap = NULL;
184 levelitem_t *freelevelitems = NULL;
185 levelitem_t *levelitems = NULL;
186 int numlevelitems = 0;
187 //map locations
188 maplocation_t *maplocations = NULL;
189 //camp spots
190 campspot_t *campspots = NULL;
191 //the game type
192 int g_gametype = 0;
193 //additional dropped item weight
194 libvar_t *droppedweight = NULL;
195
196 //========================================================================
197 //
198 // Parameter: -
199 // Returns: -
200 // Changes Globals: -
201 //========================================================================
202 bot_goalstate_t *BotGoalStateFromHandle(int handle)
203 {
204 if (handle <= 0 || handle > MAX_CLIENTS)
205 {
206 botimport.Print(PRT_FATAL, "goal state handle %d out of range\n", handle);
207 return NULL;
208 } //end if
209 if (!botgoalstates[handle])
210 {
211 botimport.Print(PRT_FATAL, "invalid goal state %d\n", handle);
212 return NULL;
213 } //end if
214 return botgoalstates[handle];
215 } //end of the function BotGoalStateFromHandle
216 //===========================================================================
217 //
218 // Parameter: -
219 // Returns: -
220 // Changes Globals: -
221 //===========================================================================
222 void BotInterbreedGoalFuzzyLogic(int parent1, int parent2, int child)
223 {
224 bot_goalstate_t *p1, *p2, *c;
225
226 p1 = BotGoalStateFromHandle(parent1);
227 p2 = BotGoalStateFromHandle(parent2);
228 c = BotGoalStateFromHandle(child);
229
230 if (!p1 || !p2 || !c)
231 return;
232
233 InterbreedWeightConfigs(p1->itemweightconfig, p2->itemweightconfig,
234 c->itemweightconfig);
235 } //end of the function BotInterbreedingGoalFuzzyLogic
236 //===========================================================================
237 //
238 // Parameter: -
239 // Returns: -
240 // Changes Globals: -
241 //===========================================================================
242 void BotSaveGoalFuzzyLogic(int goalstate, char *filename)
243 {
244 //bot_goalstate_t *gs;
245
246 //gs = BotGoalStateFromHandle(goalstate);
247 //if (!gs) return;
248 //WriteWeightConfig(filename, gs->itemweightconfig);
249 } //end of the function BotSaveGoalFuzzyLogic
250 //===========================================================================
251 //
252 // Parameter: -
253 // Returns: -
254 // Changes Globals: -
255 //===========================================================================
256 void BotMutateGoalFuzzyLogic(int goalstate, float range)
257 {
258 bot_goalstate_t *gs;
259
260 gs = BotGoalStateFromHandle(goalstate);
261 if (!gs) return;
262 EvolveWeightConfig(gs->itemweightconfig);
263 } //end of the function BotMutateGoalFuzzyLogic
264 //===========================================================================
265 //
266 // Parameter: -
267 // Returns: -
268 // Changes Globals: -
269 //===========================================================================
270 itemconfig_t *LoadItemConfig(char *filename)
271 {
272 int max_iteminfo;
273 token_t token;
274 char path[MAX_QPATH];
275 source_t *source;
276 itemconfig_t *ic;
277 iteminfo_t *ii;
278
279 max_iteminfo = (int) LibVarValue("max_iteminfo", "256");
280 if (max_iteminfo < 0)
281 {
282 botimport.Print(PRT_ERROR, "max_iteminfo = %d\n", max_iteminfo);
283 max_iteminfo = 256;
284 LibVarSet( "max_iteminfo", "256" );
285 }
286
287 Q_strncpyz(path, filename, sizeof(path));
288 PC_SetBaseFolder(BOTFILESBASEFOLDER);
289 source = LoadSourceFile( path );
290 if( !source ) {
291 botimport.Print( PRT_ERROR, "couldn't load %s\n", path );
292 return NULL;
293 } //end if
294 //initialize item config
295 ic = (itemconfig_t *) GetClearedHunkMemory(sizeof(itemconfig_t) +
296 max_iteminfo * sizeof(iteminfo_t));
297 ic->iteminfo = (iteminfo_t *) ((char *) ic + sizeof(itemconfig_t));
298 ic->numiteminfo = 0;
299 //parse the item config file
300 while(PC_ReadToken(source, &token))
301 {
302 if (!strcmp(token.string, "iteminfo"))
303 {
304 if (ic->numiteminfo >= max_iteminfo)
305 {
306 SourceError(source, "more than %d item info defined", max_iteminfo);
307 FreeMemory(ic);
308 FreeSource(source);
309 return NULL;
310 } //end if
311 ii = &ic->iteminfo[ic->numiteminfo];
312 Com_Memset(ii, 0, sizeof(iteminfo_t));
313 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
314 {
315 FreeMemory(ic);
316 FreeSource(source);
317 return NULL;
318 } //end if
319 StripDoubleQuotes(token.string);
320 Q_strncpyz(ii->classname, token.string, sizeof(ii->classname));
321 if (!ReadStructure(source, &iteminfo_struct, (char *) ii))
322 {
323 FreeMemory(ic);
324 FreeSource(source);
325 return NULL;
326 } //end if
327 ii->number = ic->numiteminfo;
328 ic->numiteminfo++;
329 } //end if
330 else
331 {
332 SourceError(source, "unknown definition %s", token.string);
333 FreeMemory(ic);
334 FreeSource(source);
335 return NULL;
336 } //end else
337 } //end while
338 FreeSource(source);
339 //
340 if (!ic->numiteminfo) botimport.Print(PRT_WARNING, "no item info loaded\n");
341 botimport.Print(PRT_MESSAGE, "loaded %s\n", path);
342 return ic;
343 } //end of the function LoadItemConfig
344 //===========================================================================
345 // index to find the weight function of an iteminfo
346 //
347 // Parameter: -
348 // Returns: -
349 // Changes Globals: -
350 //===========================================================================
351 int *ItemWeightIndex(weightconfig_t *iwc, itemconfig_t *ic)
352 {
353 int *index, i;
354
355 //initialize item weight index
356 index = (int *) GetClearedMemory(sizeof(int) * ic->numiteminfo);
357
358 for (i = 0; i < ic->numiteminfo; i++)
359 {
360 index[i] = FindFuzzyWeight(iwc, ic->iteminfo[i].classname);
361 if (index[i] < 0)
362 {
363 Log_Write("item info %d \"%s\" has no fuzzy weight\r\n", i, ic->iteminfo[i].classname);
364 } //end if
365 } //end for
366 return index;
367 } //end of the function ItemWeightIndex
368 //===========================================================================
369 //
370 // Parameter: -
371 // Returns: -
372 // Changes Globals: -
373 //===========================================================================
374 void InitLevelItemHeap(void)
375 {
376 int i, max_levelitems;
377
378 if (levelitemheap) FreeMemory(levelitemheap);
379
380 max_levelitems = (int) LibVarValue("max_levelitems", "256");
381 levelitemheap = (levelitem_t *) GetClearedMemory(max_levelitems * sizeof(levelitem_t));
382
383 for (i = 0; i < max_levelitems-1; i++)
384 {
385 levelitemheap[i].next = &levelitemheap[i + 1];
386 } //end for
387 levelitemheap[max_levelitems-1].next = NULL;
388 //
389 freelevelitems = levelitemheap;
390 } //end of the function InitLevelItemHeap
391 //===========================================================================
392 //
393 // Parameter: -
394 // Returns: -
395 // Changes Globals: -
396 //===========================================================================
397 levelitem_t *AllocLevelItem(void)
398 {
399 levelitem_t *li;
400
401 li = freelevelitems;
402 if (!li)
403 {
404 botimport.Print(PRT_FATAL, "out of level items\n");
405 return NULL;
406 } //end if
407 //
408 freelevelitems = freelevelitems->next;
409 Com_Memset(li, 0, sizeof(levelitem_t));
410 return li;
411 } //end of the function AllocLevelItem
412 //===========================================================================
413 //
414 // Parameter: -
415 // Returns: -
416 // Changes Globals: -
417 //===========================================================================
418 void FreeLevelItem(levelitem_t *li)
419 {
420 li->next = freelevelitems;
421 freelevelitems = li;
422 } //end of the function FreeLevelItem
423 //===========================================================================
424 //
425 // Parameter: -
426 // Returns: -
427 // Changes Globals: -
428 //===========================================================================
429 void AddLevelItemToList(levelitem_t *li)
430 {
431 if (levelitems) levelitems->prev = li;
432 li->prev = NULL;
433 li->next = levelitems;
434 levelitems = li;
435 } //end of the function AddLevelItemToList
436 //===========================================================================
437 //
438 // Parameter: -
439 // Returns: -
440 // Changes Globals: -
441 //===========================================================================
442 void RemoveLevelItemFromList(levelitem_t *li)
443 {
444 if (li->prev) li->prev->next = li->next;
445 else levelitems = li->next;
446 if (li->next) li->next->prev = li->prev;
447 } //end of the function RemoveLevelItemFromList
448 //===========================================================================
449 //
450 // Parameter: -
451 // Returns: -
452 // Changes Globals: -
453 //===========================================================================
454 void BotFreeInfoEntities(void)
455 {
456 maplocation_t *ml, *nextml;
457 campspot_t *cs, *nextcs;
458
459 for (ml = maplocations; ml; ml = nextml)
460 {
461 nextml = ml->next;
462 FreeMemory(ml);
463 } //end for
464 maplocations = NULL;
465 for (cs = campspots; cs; cs = nextcs)
466 {
467 nextcs = cs->next;
468 FreeMemory(cs);
469 } //end for
470 campspots = NULL;
471 } //end of the function BotFreeInfoEntities
472 //===========================================================================
473 //
474 // Parameter: -
475 // Returns: -
476 // Changes Globals: -
477 //===========================================================================
478 void BotInitInfoEntities(void)
479 {
480 char classname[MAX_EPAIRKEY];
481 maplocation_t *ml;
482 campspot_t *cs;
483 int ent, numlocations, numcampspots;
484
485 BotFreeInfoEntities();
486 //
487 numlocations = 0;
488 numcampspots = 0;
489 for (ent = AAS_NextBSPEntity(0); ent; ent = AAS_NextBSPEntity(ent))
490 {
491 if (!AAS_ValueForBSPEpairKey(ent, "classname", classname, MAX_EPAIRKEY)) continue;
492
493 //map locations
494 if (!strcmp(classname, "target_location"))
495 {
496 ml = (maplocation_t *) GetClearedMemory(sizeof(maplocation_t));
497 AAS_VectorForBSPEpairKey(ent, "origin", ml->origin);
498 AAS_ValueForBSPEpairKey(ent, "message", ml->name, sizeof(ml->name));
499 ml->areanum = AAS_PointAreaNum(ml->origin);
500 ml->next = maplocations;
501 maplocations = ml;
502 numlocations++;
503 } //end if
504 //camp spots
505 else if (!strcmp(classname, "info_camp"))
506 {
507 cs = (campspot_t *) GetClearedMemory(sizeof(campspot_t));
508 AAS_VectorForBSPEpairKey(ent, "origin", cs->origin);
509 //cs->origin[2] += 16;
510 AAS_ValueForBSPEpairKey(ent, "message", cs->name, sizeof(cs->name));
511 AAS_FloatForBSPEpairKey(ent, "range", &cs->range);
512 AAS_FloatForBSPEpairKey(ent, "weight", &cs->weight);
513 AAS_FloatForBSPEpairKey(ent, "wait", &cs->wait);
514 AAS_FloatForBSPEpairKey(ent, "random", &cs->random);
515 cs->areanum = AAS_PointAreaNum(cs->origin);
516 if (!cs->areanum)
517 {
518 botimport.Print(PRT_MESSAGE, "camp spot at %1.1f %1.1f %1.1f in solid\n", cs->origin[0], cs->origin[1], cs->origin[2]);
519 FreeMemory(cs);
520 continue;
521 } //end if
522 cs->next = campspots;
523 campspots = cs;
524 //AAS_DrawPermanentCross(cs->origin, 4, LINECOLOR_YELLOW);
525 numcampspots++;
526 } //end else if
527 } //end for
528 if (botDeveloper)
529 {
530 botimport.Print(PRT_MESSAGE, "%d map locations\n", numlocations);
531 botimport.Print(PRT_MESSAGE, "%d camp spots\n", numcampspots);
532 } //end if
533 } //end of the function BotInitInfoEntities
534 //===========================================================================
535 //
536 // Parameter: -
537 // Returns: -
538 // Changes Globals: -
539 //===========================================================================
540 void BotInitLevelItems(void)
541 {
542 int i, spawnflags, value;
543 char classname[MAX_EPAIRKEY];
544 vec3_t origin, end;
545 int ent, goalareanum;
546 itemconfig_t *ic;
547 levelitem_t *li;
548 bsp_trace_t trace;
549
550 //initialize the map locations and camp spots
551 BotInitInfoEntities();
552
553 //initialize the level item heap
554 InitLevelItemHeap();
555 levelitems = NULL;
556 numlevelitems = 0;
557 //
558 ic = itemconfig;
559 if (!ic) return;
560
561 //if there's no AAS file loaded
562 if (!AAS_Loaded()) return;
563
564 //validate the modelindexes of the item info
565 for (i = 0; i < ic->numiteminfo; i++)
566 {
567 if (!ic->iteminfo[i].modelindex)
568 {
569 Log_Write("item %s has modelindex 0", ic->iteminfo[i].classname);
570 } //end if
571 } //end for
572
573 for (ent = AAS_NextBSPEntity(0); ent; ent = AAS_NextBSPEntity(ent))
574 {
575 if (!AAS_ValueForBSPEpairKey(ent, "classname", classname, MAX_EPAIRKEY)) continue;
576 //
577 spawnflags = 0;
578 AAS_IntForBSPEpairKey(ent, "spawnflags", &spawnflags);
579 //
580 for (i = 0; i < ic->numiteminfo; i++)
581 {
582 if (!strcmp(classname, ic->iteminfo[i].classname)) break;
583 } //end for
584 if (i >= ic->numiteminfo)
585 {
586 Log_Write("entity %s unknown item\r\n", classname);
587 continue;
588 } //end if
589 //get the origin of the item
590 if (!AAS_VectorForBSPEpairKey(ent, "origin", origin))
591 {
592 botimport.Print(PRT_ERROR, "item %s without origin\n", classname);
593 continue;
594 } //end else
595 //
596 goalareanum = 0;
597 //if it is a floating item
598 if (spawnflags & 1)
599 {
600 //if the item is not floating in water
601 if (!(AAS_PointContents(origin) & CONTENTS_WATER))
602 {
603 VectorCopy(origin, end);
604 end[2] -= 32;
605 trace = AAS_Trace(origin, ic->iteminfo[i].mins, ic->iteminfo[i].maxs, end, -1, CONTENTS_SOLID|CONTENTS_PLAYERCLIP);
606 //if the item not near the ground
607 if (trace.fraction >= 1)
608 {
609 //if the item is not reachable from a jumppad
610 goalareanum = AAS_BestReachableFromJumpPadArea(origin, ic->iteminfo[i].mins, ic->iteminfo[i].maxs);
611 Log_Write("item %s reachable from jumppad area %d\r\n", ic->iteminfo[i].classname, goalareanum);
612 //botimport.Print(PRT_MESSAGE, "item %s reachable from jumppad area %d\r\n", ic->iteminfo[i].classname, goalareanum);
613 if (!goalareanum) continue;
614 } //end if
615 } //end if
616 } //end if
617
618 li = AllocLevelItem();
619 if (!li) return;
620 //
621 li->number = ++numlevelitems;
622 li->timeout = 0;
623 li->entitynum = 0;
624 //
625 li->flags = 0;
626 AAS_IntForBSPEpairKey(ent, "notfree", &value);
627 if (value) li->flags |= IFL_NOTFREE;
628 AAS_IntForBSPEpairKey(ent, "notteam", &value);
629 if (value) li->flags |= IFL_NOTTEAM;
630 AAS_IntForBSPEpairKey(ent, "notsingle", &value);
631 if (value) li->flags |= IFL_NOTSINGLE;
632 AAS_IntForBSPEpairKey(ent, "notbot", &value);
633 if (value) li->flags |= IFL_NOTBOT;
634 if (!strcmp(classname, "item_botroam"))
635 {
636 li->flags |= IFL_ROAM;
637 AAS_FloatForBSPEpairKey(ent, "weight", &li->weight);
638 } //end if
639 //if not a stationary item
640 if (!(spawnflags & 1))
641 {
642 if (!AAS_DropToFloor(origin, ic->iteminfo[i].mins, ic->iteminfo[i].maxs))
643 {
644 botimport.Print(PRT_MESSAGE, "%s in solid at (%1.1f %1.1f %1.1f)\n",
645 classname, origin[0], origin[1], origin[2]);
646 } //end if
647 } //end if
648 //item info of the level item
649 li->iteminfo = i;
650 //origin of the item
651 VectorCopy(origin, li->origin);
652 //
653 if (goalareanum)
654 {
655 li->goalareanum = goalareanum;
656 VectorCopy(origin, li->goalorigin);
657 } //end if
658 else
659 {
660 //get the item goal area and goal origin
661 li->goalareanum = AAS_BestReachableArea(origin,
662 ic->iteminfo[i].mins, ic->iteminfo[i].maxs,
663 li->goalorigin);
664 if (!li->goalareanum)
665 {
666 botimport.Print(PRT_MESSAGE, "%s not reachable for bots at (%1.1f %1.1f %1.1f)\n",
667 classname, origin[0], origin[1], origin[2]);
668 } //end if
669 } //end else
670 //
671 AddLevelItemToList(li);
672 } //end for
673 botimport.Print(PRT_MESSAGE, "found %d level items\n", numlevelitems);
674 } //end of the function BotInitLevelItems
675 //===========================================================================
676 //
677 // Parameter: -
678 // Returns: -
679 // Changes Globals: -
680 //===========================================================================
681 void BotGoalName(int number, char *name, int size)
682 {
683 levelitem_t *li;
684
685 if (!itemconfig) return;
686 //
687 for (li = levelitems; li; li = li->next)
688 {
689 if (li->number == number)
690 {
691 Q_strncpyz(name, itemconfig->iteminfo[li->iteminfo].name, size);
692 return;
693 } //end for
694 } //end for
695 strcpy(name, "");
696 } //end of the function BotGoalName
697 //===========================================================================
698 //
699 // Parameter: -
700 // Returns: -
701 // Changes Globals: -
702 //===========================================================================
703 void BotResetAvoidGoals(int goalstate)
704 {
705 bot_goalstate_t *gs;
706
707 gs = BotGoalStateFromHandle(goalstate);
708 if (!gs) return;
709 Com_Memset(gs->avoidgoals, 0, MAX_AVOIDGOALS * sizeof(int));
710 Com_Memset(gs->avoidgoaltimes, 0, MAX_AVOIDGOALS * sizeof(float));
711 } //end of the function BotResetAvoidGoals
712 //===========================================================================
713 //
714 // Parameter: -
715 // Returns: -
716 // Changes Globals: -
717 //===========================================================================
718 void BotDumpAvoidGoals(int goalstate)
719 {
720 int i;
721 bot_goalstate_t *gs;
722 char name[32];
723
724 gs = BotGoalStateFromHandle(goalstate);
725 if (!gs) return;
726 for (i = 0; i < MAX_AVOIDGOALS; i++)
727 {
728 if (gs->avoidgoaltimes[i] >= AAS_Time())
729 {
730 BotGoalName(gs->avoidgoals[i], name, 32);
731 Log_Write("avoid goal %s, number %d for %f seconds", name,
732 gs->avoidgoals[i], gs->avoidgoaltimes[i] - AAS_Time());
733 } //end if
734 } //end for
735 } //end of the function BotDumpAvoidGoals
736 //===========================================================================
737 //
738 // Parameter: -
739 // Returns: -
740 // Changes Globals: -
741 //===========================================================================
742 void BotAddToAvoidGoals(bot_goalstate_t *gs, int number, float avoidtime)
743 {
744 int i;
745
746 for (i = 0; i < MAX_AVOIDGOALS; i++)
747 {
748 //if the avoid goal is already stored
749 if (gs->avoidgoals[i] == number)
750 {
751 gs->avoidgoals[i] = number;
752 gs->avoidgoaltimes[i] = AAS_Time() + avoidtime;
753 return;
754 } //end if
755 } //end for
756
757 for (i = 0; i < MAX_AVOIDGOALS; i++)
758 {
759 //if this avoid goal has expired
760 if (gs->avoidgoaltimes[i] < AAS_Time())
761 {
762 gs->avoidgoals[i] = number;
763 gs->avoidgoaltimes[i] = AAS_Time() + avoidtime;
764 return;
765 } //end if
766 } //end for
767 } //end of the function BotAddToAvoidGoals
768 //===========================================================================
769 //
770 // Parameter: -
771 // Returns: -
772 // Changes Globals: -
773 //===========================================================================
774 void BotRemoveFromAvoidGoals(int goalstate, int number)
775 {
776 int i;
777 bot_goalstate_t *gs;
778
779 gs = BotGoalStateFromHandle(goalstate);
780 if (!gs) return;
781 //don't use the goals the bot wants to avoid
782 for (i = 0; i < MAX_AVOIDGOALS; i++)
783 {
784 if (gs->avoidgoals[i] == number && gs->avoidgoaltimes[i] >= AAS_Time())
785 {
786 gs->avoidgoaltimes[i] = 0;
787 return;
788 } //end if
789 } //end for
790 } //end of the function BotRemoveFromAvoidGoals
791 //===========================================================================
792 //
793 // Parameter: -
794 // Returns: -
795 // Changes Globals: -
796 //===========================================================================
797 float BotAvoidGoalTime(int goalstate, int number)
798 {
799 int i;
800 bot_goalstate_t *gs;
801
802 gs = BotGoalStateFromHandle(goalstate);
803 if (!gs) return 0;
804 //don't use the goals the bot wants to avoid
805 for (i = 0; i < MAX_AVOIDGOALS; i++)
806 {
807 if (gs->avoidgoals[i] == number && gs->avoidgoaltimes[i] >= AAS_Time())
808 {
809 return gs->avoidgoaltimes[i] - AAS_Time();
810 } //end if
811 } //end for
812 return 0;
813 } //end of the function BotAvoidGoalTime
814 //===========================================================================
815 //
816 // Parameter: -
817 // Returns: -
818 // Changes Globals: -
819 //===========================================================================
820 void BotSetAvoidGoalTime(int goalstate, int number, float avoidtime)
821 {
822 bot_goalstate_t *gs;
823 levelitem_t *li;
824
825 gs = BotGoalStateFromHandle(goalstate);
826 if (!gs)
827 return;
828 if (avoidtime < 0)
829 {
830 if (!itemconfig)
831 return;
832 //
833 for (li = levelitems; li; li = li->next)
834 {
835 if (li->number == number)
836 {
837 avoidtime = itemconfig->iteminfo[li->iteminfo].respawntime;
838 if (!avoidtime)
839 avoidtime = AVOID_DEFAULT_TIME;
840 if (avoidtime < AVOID_MINIMUM_TIME)
841 avoidtime = AVOID_MINIMUM_TIME;
842 BotAddToAvoidGoals(gs, number, avoidtime);
843 return;
844 } //end for
845 } //end for
846 return;
847 } //end if
848 else
849 {
850 BotAddToAvoidGoals(gs, number, avoidtime);
851 } //end else
852 } //end of the function BotSetAvoidGoalTime
853 //===========================================================================
854 //
855 // Parameter: -
856 // Returns: -
857 // Changes Globals: -
858 //===========================================================================
859 int BotGetLevelItemGoal(int index, char *name, bot_goal_t *goal)
860 {
861 levelitem_t *li;
862
863 if (!itemconfig) return -1;
864 li = levelitems;
865 if (index >= 0)
866 {
867 for (; li; li = li->next)
868 {
869 if (li->number == index)
870 {
871 li = li->next;
872 break;
873 } //end if
874 } //end for
875 } //end for
876 for (; li; li = li->next)
877 {
878 //
879 if (g_gametype == GT_SINGLE_PLAYER) {
880 if (li->flags & IFL_NOTSINGLE) continue;
881 }
882 else if (g_gametype >= GT_TEAM) {
883 if (li->flags & IFL_NOTTEAM) continue;
884 }
885 else {
886 if (li->flags & IFL_NOTFREE) continue;
887 }
888 if (li->flags & IFL_NOTBOT) continue;
889 //
890 if (!Q_stricmp(name, itemconfig->iteminfo[li->iteminfo].name))
891 {
892 goal->areanum = li->goalareanum;
893 VectorCopy(li->goalorigin, goal->origin);
894 goal->entitynum = li->entitynum;
895 VectorCopy(itemconfig->iteminfo[li->iteminfo].mins, goal->mins);
896 VectorCopy(itemconfig->iteminfo[li->iteminfo].maxs, goal->maxs);
897 goal->number = li->number;
898 goal->flags = GFL_ITEM;
899 if (li->timeout) goal->flags |= GFL_DROPPED;
900 goal->iteminfo = li->iteminfo;
901 //botimport.Print(PRT_MESSAGE, "found li %s\n", itemconfig->iteminfo[li->iteminfo].name);
902 return li->number;
903 } //end if
904 } //end for
905 return -1;
906 } //end of the function BotGetLevelItemGoal
907 //===========================================================================
908 //
909 // Parameter: -
910 // Returns: -
911 // Changes Globals: -
912 //===========================================================================
913 int BotGetMapLocationGoal(char *name, bot_goal_t *goal)
914 {
915 maplocation_t *ml;
916 vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8};
917
918 for (ml = maplocations; ml; ml = ml->next)
919 {
920 if (!Q_stricmp(ml->name, name))
921 {
922 goal->areanum = ml->areanum;
923 VectorCopy(ml->origin, goal->origin);
924 goal->entitynum = 0;
925 VectorCopy(mins, goal->mins);
926 VectorCopy(maxs, goal->maxs);
927 goal->number = 0;
928 goal->flags = 0;
929 goal->iteminfo = 0;
930 return qtrue;
931 } //end if
932 } //end for
933 return qfalse;
934 } //end of the function BotGetMapLocationGoal
935 //===========================================================================
936 //
937 // Parameter: -
938 // Returns: -
939 // Changes Globals: -
940 //===========================================================================
941 int BotGetNextCampSpotGoal(int num, bot_goal_t *goal)
942 {
943 int i;
944 campspot_t *cs;
945 vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8};
946
947 if (num < 0) num = 0;
948 i = num;
949 for (cs = campspots; cs; cs = cs->next)
950 {
951 if (--i < 0)
952 {
953 goal->areanum = cs->areanum;
954 VectorCopy(cs->origin, goal->origin);
955 goal->entitynum = 0;
956 VectorCopy(mins, goal->mins);
957 VectorCopy(maxs, goal->maxs);
958 goal->number = 0;
959 goal->flags = 0;
960 goal->iteminfo = 0;
961 return num+1;
962 } //end if
963 } //end for
964 return 0;
965 } //end of the function BotGetNextCampSpotGoal
966 //===========================================================================
967 //
968 // Parameter: -
969 // Returns: -
970 // Changes Globals: -
971 //===========================================================================
972 void BotFindEntityForLevelItem(levelitem_t *li)
973 {
974 int ent, modelindex;
975 itemconfig_t *ic;
976 aas_entityinfo_t entinfo;
977 vec3_t dir;
978
979 ic = itemconfig;
980 if (!itemconfig) return;
981 for (ent = AAS_NextEntity(0); ent; ent = AAS_NextEntity(ent))
982 {
983 //get the model index of the entity
984 modelindex = AAS_EntityModelindex(ent);
985 //
986 if (!modelindex) continue;
987 //get info about the entity
988 AAS_EntityInfo(ent, &entinfo);
989 //if the entity is still moving
990 if (entinfo.origin[0] != entinfo.lastvisorigin[0] ||
991 entinfo.origin[1] != entinfo.lastvisorigin[1] ||
992 entinfo.origin[2] != entinfo.lastvisorigin[2]) continue;
993 //
994 if (ic->iteminfo[li->iteminfo].modelindex == modelindex)
995 {
996 //check if the entity is very close
997 VectorSubtract(li->origin, entinfo.origin, dir);
998 if (VectorLength(dir) < 30)
999 {
1000 //found an entity for this level item
1001 li->entitynum = ent;
1002 } //end if
1003 } //end if
1004 } //end for
1005 } //end of the function BotFindEntityForLevelItem
1006 //===========================================================================
1007 //
1008 // Parameter: -
1009 // Returns: -
1010 // Changes Globals: -
1011 //===========================================================================
1012
1013 //NOTE: enum entityType_t in bg_public.h
1014 #define ET_ITEM 2
1015
1016 void BotUpdateEntityItems(void)
1017 {
1018 int ent, i, modelindex;
1019 vec3_t dir;
1020 levelitem_t *li, *nextli;
1021 aas_entityinfo_t entinfo;
1022 itemconfig_t *ic;
1023
1024 //timeout current entity items if necessary
1025 for (li = levelitems; li; li = nextli)
1026 {
1027 nextli = li->next;
1028 //if it is an item that will time out
1029 if (li->timeout)
1030 {
1031 //timeout the item
1032 if (li->timeout < AAS_Time())
1033 {
1034 RemoveLevelItemFromList(li);
1035 FreeLevelItem(li);
1036 } //end if
1037 } //end if
1038 } //end for
1039 //find new entity items
1040 ic = itemconfig;
1041 if (!itemconfig) return;
1042 //
1043 for (ent = AAS_NextEntity(0); ent; ent = AAS_NextEntity(ent))
1044 {
1045 if (AAS_EntityType(ent) != ET_ITEM) continue;
1046 //get the model index of the entity
1047 modelindex = AAS_EntityModelindex(ent);
1048 //
1049 if (!modelindex) continue;
1050 //get info about the entity
1051 AAS_EntityInfo(ent, &entinfo);
1052 //FIXME: don't do this
1053 //skip all floating items for now
1054 //if (entinfo.groundent != ENTITYNUM_WORLD) continue;
1055 //if the entity is still moving
1056 if (entinfo.origin[0] != entinfo.lastvisorigin[0] ||
1057 entinfo.origin[1] != entinfo.lastvisorigin[1] ||
1058 entinfo.origin[2] != entinfo.lastvisorigin[2]) continue;
1059 //check if the entity is already stored as a level item
1060 for (li = levelitems; li; li = li->next)
1061 {
1062 //if the level item is linked to an entity
1063 if (li->entitynum && li->entitynum == ent)
1064 {
1065 //the entity is re-used if the models are different
1066 if (ic->iteminfo[li->iteminfo].modelindex != modelindex)
1067 {
1068 //remove this level item
1069 RemoveLevelItemFromList(li);
1070 FreeLevelItem(li);
1071 li = NULL;
1072 break;
1073 } //end if
1074 else
1075 {
1076 if (entinfo.origin[0] != li->origin[0] ||
1077 entinfo.origin[1] != li->origin[1] ||
1078 entinfo.origin[2] != li->origin[2])
1079 {
1080 VectorCopy(entinfo.origin, li->origin);
1081 //also update the goal area number
1082 li->goalareanum = AAS_BestReachableArea(li->origin,
1083 ic->iteminfo[li->iteminfo].mins, ic->iteminfo[li->iteminfo].maxs,
1084 li->goalorigin);
1085 } //end if
1086 break;
1087 } //end else
1088 } //end if
1089 } //end for
1090 if (li) continue;
1091 //try to link the entity to a level item
1092 for (li = levelitems; li; li = li->next)
1093 {
1094 //if this level item is already linked
1095 if (li->entitynum) continue;
1096 //
1097 if (g_gametype == GT_SINGLE_PLAYER) {
1098 if (li->flags & IFL_NOTSINGLE) continue;
1099 }
1100 else if (g_gametype >= GT_TEAM) {
1101 if (li->flags & IFL_NOTTEAM) continue;
1102 }
1103 else {
1104 if (li->flags & IFL_NOTFREE) continue;
1105 }
1106 //if the model of the level item and the entity are the same
1107 if (ic->iteminfo[li->iteminfo].modelindex == modelindex)
1108 {
1109 //check if the entity is very close
1110 VectorSubtract(li->origin, entinfo.origin, dir);
1111 if (VectorLength(dir) < 30)
1112 {
1113 //found an entity for this level item
1114 li->entitynum = ent;
1115 //if the origin is different
1116 if (entinfo.origin[0] != li->origin[0] ||
1117 entinfo.origin[1] != li->origin[1] ||
1118 entinfo.origin[2] != li->origin[2])
1119 {
1120 //update the level item origin
1121 VectorCopy(entinfo.origin, li->origin);
1122 //also update the goal area number
1123 li->goalareanum = AAS_BestReachableArea(li->origin,
1124 ic->iteminfo[li->iteminfo].mins, ic->iteminfo[li->iteminfo].maxs,
1125 li->goalorigin);
1126 } //end if
1127 #ifdef DEBUG
1128 Log_Write("linked item %s to an entity", ic->iteminfo[li->iteminfo].classname);
1129 #endif //DEBUG
1130 break;
1131 } //end if
1132 } //end else
1133 } //end for
1134 if (li) continue;
1135 //check if the model is from a known item
1136 for (i = 0; i < ic->numiteminfo; i++)
1137 {
1138 if (ic->iteminfo[i].modelindex == modelindex)
1139 {
1140 break;
1141 } //end if
1142 } //end for
1143 //if the model is not from a known item
1144 if (i >= ic->numiteminfo) continue;
1145 //allocate a new level item
1146 li = AllocLevelItem();
1147 //
1148 if (!li) continue;
1149 //entity number of the level item
1150 li->entitynum = ent;
1151 //number for the level item
1152 li->number = numlevelitems + ent;
1153 //set the item info index for the level item
1154 li->iteminfo = i;
1155 //origin of the item
1156 VectorCopy(entinfo.origin, li->origin);
1157 //get the item goal area and goal origin
1158 li->goalareanum = AAS_BestReachableArea(li->origin,
1159 ic->iteminfo[i].mins, ic->iteminfo[i].maxs,
1160 li->goalorigin);
1161 //never go for items dropped into jumppads
1162 if (AAS_AreaJumpPad(li->goalareanum))
1163 {
1164 FreeLevelItem(li);
1165 continue;
1166 } //end if
1167 //time this item out after 30 seconds
1168 //dropped items disappear after 30 seconds
1169 li->timeout = AAS_Time() + 30;
1170 //add the level item to the list
1171 AddLevelItemToList(li);
1172 //botimport.Print(PRT_MESSAGE, "found new level item %s\n", ic->iteminfo[i].classname);
1173 } //end for
1174 /*
1175 for (li = levelitems; li; li = li->next)
1176 {
1177 if (!li->entitynum)
1178 {
1179 BotFindEntityForLevelItem(li);
1180 } //end if
1181 } //end for*/
1182 } //end of the function BotUpdateEntityItems
1183 //===========================================================================
1184 //
1185 // Parameter: -
1186 // Returns: -
1187 // Changes Globals: -
1188 //===========================================================================
1189 void BotDumpGoalStack(int goalstate)
1190 {
1191 int i;
1192 bot_goalstate_t *gs;
1193 char name[32];
1194
1195 gs = BotGoalStateFromHandle(goalstate);
1196 if (!gs) return;
1197 for (i = 1; i <= gs->goalstacktop; i++)
1198 {
1199 BotGoalName(gs->goalstack[i].number, name, 32);
1200 Log_Write("%d: %s", i, name);
1201 } //end for
1202 } //end of the function BotDumpGoalStack
1203 //===========================================================================
1204 //
1205 // Parameter: -
1206 // Returns: -
1207 // Changes Globals: -
1208 //===========================================================================
1209 void BotPushGoal(int goalstate, bot_goal_t *goal)
1210 {
1211 bot_goalstate_t *gs;
1212
1213 gs = BotGoalStateFromHandle(goalstate);
1214 if (!gs) return;
1215 if (gs->goalstacktop >= MAX_GOALSTACK-1)
1216 {
1217 botimport.Print(PRT_ERROR, "goal heap overflow\n");
1218 BotDumpGoalStack(goalstate);
1219 return;
1220 } //end if
1221 gs->goalstacktop++;
1222 Com_Memcpy(&gs->goalstack[gs->goalstacktop], goal, sizeof(bot_goal_t));
1223 } //end of the function BotPushGoal
1224 //===========================================================================
1225 //
1226 // Parameter: -
1227 // Returns: -
1228 // Changes Globals: -
1229 //===========================================================================
1230 void BotPopGoal(int goalstate)
1231 {
1232 bot_goalstate_t *gs;
1233
1234 gs = BotGoalStateFromHandle(goalstate);
1235 if (!gs) return;
1236 if (gs->goalstacktop > 0) gs->goalstacktop--;
1237 } //end of the function BotPopGoal
1238 //===========================================================================
1239 //
1240 // Parameter: -
1241 // Returns: -
1242 // Changes Globals: -
1243 //===========================================================================
1244 void BotEmptyGoalStack(int goalstate)
1245 {
1246 bot_goalstate_t *gs;
1247
1248 gs = BotGoalStateFromHandle(goalstate);
1249 if (!gs) return;
1250 gs->goalstacktop = 0;
1251 } //end of the function BotEmptyGoalStack
1252 //===========================================================================
1253 //
1254 // Parameter: -
1255 // Returns: -
1256 // Changes Globals: -
1257 //===========================================================================
1258 int BotGetTopGoal(int goalstate, bot_goal_t *goal)
1259 {
1260 bot_goalstate_t *gs;
1261
1262 gs = BotGoalStateFromHandle(goalstate);
1263 if (!gs) return qfalse;
1264 if (!gs->goalstacktop) return qfalse;
1265 Com_Memcpy(goal, &gs->goalstack[gs->goalstacktop], sizeof(bot_goal_t));
1266 return qtrue;
1267 } //end of the function BotGetTopGoal
1268 //===========================================================================
1269 //
1270 // Parameter: -
1271 // Returns: -
1272 // Changes Globals: -
1273 //===========================================================================
1274 int BotGetSecondGoal(int goalstate, bot_goal_t *goal)
1275 {
1276 bot_goalstate_t *gs;
1277
1278 gs = BotGoalStateFromHandle(goalstate);
1279 if (!gs) return qfalse;
1280 if (gs->goalstacktop <= 1) return qfalse;
1281 Com_Memcpy(goal, &gs->goalstack[gs->goalstacktop-1], sizeof(bot_goal_t));
1282 return qtrue;
1283 } //end of the function BotGetSecondGoal
1284 //===========================================================================
1285 // pops a new long term goal on the goal stack in the goalstate
1286 //
1287 // Parameter: -
1288 // Returns: -
1289 // Changes Globals: -
1290 //===========================================================================
1291 int BotChooseLTGItem(int goalstate, vec3_t origin, int *inventory, int travelflags)
1292 {
1293 int areanum, t, weightnum;
1294 float weight, bestweight, avoidtime;
1295 iteminfo_t *iteminfo;
1296 itemconfig_t *ic;
1297 levelitem_t *li, *bestitem;
1298 bot_goal_t goal;
1299 bot_goalstate_t *gs;
1300
1301 gs = BotGoalStateFromHandle(goalstate);
1302 if (!gs)
1303 return qfalse;
1304 if (!gs->itemweightconfig)
1305 return qfalse;
1306 //get the area the bot is in
1307 areanum = BotReachabilityArea(origin, gs->client);
1308 //if the bot is in solid or if the area the bot is in has no reachability links
1309 if (!areanum || !AAS_AreaReachability(areanum))
1310 {
1311 //use the last valid area the bot was in
1312 areanum = gs->lastreachabilityarea;
1313 } //end if
1314 //remember the last area with reachabilities the bot was in
1315 gs->lastreachabilityarea = areanum;
1316 //if still in solid
1317 if (!areanum)
1318 return qfalse;
1319 //the item configuration
1320 ic = itemconfig;
1321 if (!itemconfig)
1322 return qfalse;
1323 //best weight and item so far
1324 bestweight = 0;
1325 bestitem = NULL;
1326 Com_Memset(&goal, 0, sizeof(bot_goal_t));
1327 //go through the items in the level
1328 for (li = levelitems; li; li = li->next)
1329 {
1330 if (g_gametype == GT_SINGLE_PLAYER) {
1331 if (li->flags & IFL_NOTSINGLE)
1332 continue;
1333 }
1334 else if (g_gametype >= GT_TEAM) {
1335 if (li->flags & IFL_NOTTEAM)
1336 continue;
1337 }
1338 else {
1339 if (li->flags & IFL_NOTFREE)
1340 continue;
1341 }
1342 if (li->flags & IFL_NOTBOT)
1343 continue;
1344 //if the item is not in a possible goal area
1345 if (!li->goalareanum)
1346 continue;
1347 //FIXME: is this a good thing? added this for items that never spawned into the game (f.i. CTF flags in obelisk)
1348 if (!li->entitynum && !(li->flags & IFL_ROAM))
1349 continue;
1350 //get the fuzzy weight function for this item
1351 iteminfo = &ic->iteminfo[li->iteminfo];
1352 weightnum = gs->itemweightindex[iteminfo->number];
1353 if (weightnum < 0)
1354 continue;
1355
1356 #ifdef UNDECIDEDFUZZY
1357 weight = FuzzyWeightUndecided(inventory, gs->itemweightconfig, weightnum);
1358 #else
1359 weight = FuzzyWeight(inventory, gs->itemweightconfig, weightnum);
1360 #endif //UNDECIDEDFUZZY
1361 #ifdef DROPPEDWEIGHT
1362 //HACK: to make dropped items more attractive
1363 if (li->timeout)
1364 weight += droppedweight->value;
1365 #endif //DROPPEDWEIGHT
1366 //use weight scale for item_botroam
1367 if (li->flags & IFL_ROAM) weight *= li->weight;
1368 //
1369 if (weight > 0)
1370 {
1371 //get the travel time towards the goal area
1372 t = AAS_AreaTravelTimeToGoalArea(areanum, origin, li->goalareanum, travelflags);
1373 //if the goal is reachable
1374 if (t > 0)
1375 {
1376 //if this item won't respawn before we get there
1377 avoidtime = BotAvoidGoalTime(goalstate, li->number);
1378 if (avoidtime - t * 0.009 > 0)
1379 continue;
1380 //
1381 weight /= (float) t * TRAVELTIME_SCALE;
1382 //
1383 if (weight > bestweight)
1384 {
1385 bestweight = weight;
1386 bestitem = li;
1387 } //end if
1388 } //end if
1389 } //end if
1390 } //end for
1391 //if no goal item found
1392 if (!bestitem)
1393 {
1394 /*
1395 //if not in lava or slime
1396 if (!AAS_AreaLava(areanum) && !AAS_AreaSlime(areanum))
1397 {
1398 if (AAS_RandomGoalArea(areanum, travelflags, &goal.areanum, goal.origin))
1399 {
1400 VectorSet(goal.mins, -15, -15, -15);
1401 VectorSet(goal.maxs, 15, 15, 15);
1402 goal.entitynum = 0;
1403 goal.number = 0;
1404 goal.flags = GFL_ROAM;
1405 goal.iteminfo = 0;
1406 //push the goal on the stack
1407 BotPushGoal(goalstate, &goal);
1408 //
1409 #ifdef DEBUG
1410 botimport.Print(PRT_MESSAGE, "chosen roam goal area %d\n", goal.areanum);
1411 #endif //DEBUG
1412 return qtrue;
1413 } //end if
1414 } //end if
1415 */
1416 return qfalse;
1417 } //end if
1418 //create a bot goal for this item
1419 iteminfo = &ic->iteminfo[bestitem->iteminfo];
1420 VectorCopy(bestitem->goalorigin, goal.origin);
1421 VectorCopy(iteminfo->mins, goal.mins);
1422 VectorCopy(iteminfo->maxs, goal.maxs);
1423 goal.areanum = bestitem->goalareanum;
1424 goal.entitynum = bestitem->entitynum;
1425 goal.number = bestitem->number;
1426 goal.flags = GFL_ITEM;
1427 if (bestitem->timeout)
1428 goal.flags |= GFL_DROPPED;
1429 if (bestitem->flags & IFL_ROAM)
1430 goal.flags |= GFL_ROAM;
1431 goal.iteminfo = bestitem->iteminfo;
1432 //if it's a dropped item
1433 if (bestitem->timeout)
1434 {
1435 avoidtime = AVOID_DROPPED_TIME;
1436 } //end if
1437 else
1438 {
1439 avoidtime = iteminfo->respawntime;
1440 if (!avoidtime)
1441 avoidtime = AVOID_DEFAULT_TIME;
1442 if (avoidtime < AVOID_MINIMUM_TIME)
1443 avoidtime = AVOID_MINIMUM_TIME;
1444 } //end else
1445 //add the chosen goal to the goals to avoid for a while
1446 BotAddToAvoidGoals(gs, bestitem->number, avoidtime);
1447 //push the goal on the stack
1448 BotPushGoal(goalstate, &goal);
1449 //
1450 return qtrue;
1451 } //end of the function BotChooseLTGItem
1452 //===========================================================================
1453 //
1454 // Parameter: -
1455 // Returns: -
1456 // Changes Globals: -
1457 //===========================================================================
1458 int BotChooseNBGItem(int goalstate, vec3_t origin, int *inventory, int travelflags,
1459 bot_goal_t *ltg, float maxtime)
1460 {
1461 int areanum, t, weightnum, ltg_time;
1462 float weight, bestweight, avoidtime;
1463 iteminfo_t *iteminfo;
1464 itemconfig_t *ic;
1465 levelitem_t *li, *bestitem;
1466 bot_goal_t goal;
1467 bot_goalstate_t *gs;
1468
1469 gs = BotGoalStateFromHandle(goalstate);
1470 if (!gs)
1471 return qfalse;
1472 if (!gs->itemweightconfig)
1473 return qfalse;
1474 //get the area the bot is in
1475 areanum = BotReachabilityArea(origin, gs->client);
1476 //if the bot is in solid or if the area the bot is in has no reachability links
1477 if (!areanum || !AAS_AreaReachability(areanum))
1478 {
1479 //use the last valid area the bot was in
1480 areanum = gs->lastreachabilityarea;
1481 } //end if
1482 //remember the last area with reachabilities the bot was in
1483 gs->lastreachabilityarea = areanum;
1484 //if still in solid
1485 if (!areanum)
1486 return qfalse;
1487 //
1488 if (ltg) ltg_time = AAS_AreaTravelTimeToGoalArea(areanum, origin, ltg->areanum, travelflags);
1489 else ltg_time = 99999;
1490 //the item configuration
1491 ic = itemconfig;
1492 if (!itemconfig)
1493 return qfalse;
1494 //best weight and item so far
1495 bestweight = 0;
1496 bestitem = NULL;
1497 Com_Memset(&goal, 0, sizeof(bot_goal_t));
1498 //go through the items in the level
1499 for (li = levelitems; li; li = li->next)
1500 {
1501 if (g_gametype == GT_SINGLE_PLAYER) {
1502 if (li->flags & IFL_NOTSINGLE)
1503 continue;
1504 }
1505 else if (g_gametype >= GT_TEAM) {
1506 if (li->flags & IFL_NOTTEAM)
1507 continue;
1508 }
1509 else {
1510 if (li->flags & IFL_NOTFREE)
1511 continue;
1512 }
1513 if (li->flags & IFL_NOTBOT)
1514 continue;
1515 //if the item is in a possible goal area
1516 if (!li->goalareanum)
1517 continue;
1518 //FIXME: is this a good thing? added this for items that never spawned into the game (f.i. CTF flags in obelisk)
1519 if (!li->entitynum && !(li->flags & IFL_ROAM))
1520 continue;
1521 //get the fuzzy weight function for this item
1522 iteminfo = &ic->iteminfo[li->iteminfo];
1523 weightnum = gs->itemweightindex[iteminfo->number];
1524 if (weightnum < 0)
1525 continue;
1526 //
1527 #ifdef UNDECIDEDFUZZY
1528 weight = FuzzyWeightUndecided(inventory, gs->itemweightconfig, weightnum);
1529 #else
1530 weight = FuzzyWeight(inventory, gs->itemweightconfig, weightnum);
1531 #endif //UNDECIDEDFUZZY
1532 #ifdef DROPPEDWEIGHT
1533 //HACK: to make dropped items more attractive
1534 if (li->timeout)
1535 weight += droppedweight->value;
1536 #endif //DROPPEDWEIGHT
1537 //use weight scale for item_botroam
1538 if (li->flags & IFL_ROAM) weight *= li->weight;
1539 //
1540 if (weight > 0)
1541 {
1542 //get the travel time towards the goal area
1543 t = AAS_AreaTravelTimeToGoalArea(areanum, origin, li->goalareanum, travelflags);
1544 //if the goal is reachable
1545 if (t > 0 && t < maxtime)
1546 {
1547 //if this item won't respawn before we get there
1548 avoidtime = BotAvoidGoalTime(goalstate, li->number);
1549 if (avoidtime - t * 0.009 > 0)
1550 continue;
1551 //
1552 weight /= (float) t * TRAVELTIME_SCALE;
1553 //
1554 if (weight > bestweight)
1555 {
1556 t = 0;
1557 if (ltg && !li->timeout)
1558 {
1559 //get the travel time from the goal to the long term goal
1560 t = AAS_AreaTravelTimeToGoalArea(li->goalareanum, li->goalorigin, ltg->areanum, travelflags);
1561 } //end if
1562 //if the travel back is possible and doesn't take too long
1563 if (t <= ltg_time)
1564 {
1565 bestweight = weight;
1566 bestitem = li;
1567 } //end if
1568 } //end if
1569 } //end if
1570 } //end if
1571 } //end for
1572 //if no goal item found
1573 if (!bestitem)
1574 return qfalse;
1575 //create a bot goal for this item
1576 iteminfo = &ic->iteminfo[bestitem->iteminfo];
1577 VectorCopy(bestitem->goalorigin, goal.origin);
1578 VectorCopy(iteminfo->mins, goal.mins);
1579 VectorCopy(iteminfo->maxs, goal.maxs);
1580 goal.areanum = bestitem->goalareanum;
1581 goal.entitynum = bestitem->entitynum;
1582 goal.number = bestitem->number;
1583 goal.flags = GFL_ITEM;
1584 if (bestitem->timeout)
1585 goal.flags |= GFL_DROPPED;
1586 if (bestitem->flags & IFL_ROAM)
1587 goal.flags |= GFL_ROAM;
1588 goal.iteminfo = bestitem->iteminfo;
1589 //if it's a dropped item
1590 if (bestitem->timeout)
1591 {
1592 avoidtime = AVOID_DROPPED_TIME;
1593 } //end if
1594 else
1595 {
1596 avoidtime = iteminfo->respawntime;
1597 if (!avoidtime)
1598 avoidtime = AVOID_DEFAULT_TIME;
1599 if (avoidtime < AVOID_MINIMUM_TIME)
1600 avoidtime = AVOID_MINIMUM_TIME;
1601 } //end else
1602 //add the chosen goal to the goals to avoid for a while
1603 BotAddToAvoidGoals(gs, bestitem->number, avoidtime);
1604 //push the goal on the stack
1605 BotPushGoal(goalstate, &goal);
1606 //
1607 return qtrue;
1608 } //end of the function BotChooseNBGItem
1609 //===========================================================================
1610 //
1611 // Parameter: -
1612 // Returns: -
1613 // Changes Globals: -
1614 //===========================================================================
1615 int BotTouchingGoal(vec3_t origin, bot_goal_t *goal)
1616 {
1617 int i;
1618 vec3_t boxmins, boxmaxs;
1619 vec3_t absmins, absmaxs;
1620 vec3_t safety_maxs = {0, 0, 0}; //{4, 4, 10};
1621 vec3_t safety_mins = {0, 0, 0}; //{-4, -4, 0};
1622
1623 AAS_PresenceTypeBoundingBox(PRESENCE_NORMAL, boxmins, boxmaxs);
1624 VectorSubtract(goal->mins, boxmaxs, absmins);
1625 VectorSubtract(goal->maxs, boxmins, absmaxs);
1626 VectorAdd(absmins, goal->origin, absmins);
1627 VectorAdd(absmaxs, goal->origin, absmaxs);
1628 //make the box a little smaller for safety
1629 VectorSubtract(absmaxs, safety_maxs, absmaxs);
1630 VectorSubtract(absmins, safety_mins, absmins);
1631
1632 for (i = 0; i < 3; i++)
1633 {
1634 if (origin[i] < absmins[i] || origin[i] > absmaxs[i]) return qfalse;
1635 } //end for
1636 return qtrue;
1637 } //end of the function BotTouchingGoal
1638 //===========================================================================
1639 //
1640 // Parameter: -
1641 // Returns: -
1642 // Changes Globals: -
1643 //===========================================================================
1644 int BotItemGoalInVisButNotVisible(int viewer, vec3_t eye, vec3_t viewangles, bot_goal_t *goal)
1645 {
1646 aas_entityinfo_t entinfo;
1647 bsp_trace_t trace;
1648 vec3_t middle;
1649
1650 if (!(goal->flags & GFL_ITEM)) return qfalse;
1651 //
1652 VectorAdd(goal->mins, goal->mins, middle);
1653 VectorScale(middle, 0.5, middle);
1654 VectorAdd(goal->origin, middle, middle);
1655 //
1656 trace = AAS_Trace(eye, NULL, NULL, middle, viewer, CONTENTS_SOLID);
1657 //if the goal middle point is visible
1658 if (trace.fraction >= 1)
1659 {
1660 //the goal entity number doesn't have to be valid
1661 //just assume it's valid
1662 if (goal->entitynum <= 0)
1663 return qfalse;
1664 //
1665 //if the entity data isn't valid
1666 AAS_EntityInfo(goal->entitynum, &entinfo);
1667 //NOTE: for some wacko reason entities are sometimes
1668 // not updated
1669 //if (!entinfo.valid) return qtrue;
1670 if (entinfo.ltime < AAS_Time() - 0.5)
1671 return qtrue;
1672 } //end if
1673 return qfalse;
1674 } //end of the function BotItemGoalInVisButNotVisible
1675 //===========================================================================
1676 //
1677 // Parameter: -
1678 // Returns: -
1679 // Changes Globals: -
1680 //===========================================================================
1681 void BotResetGoalState(int goalstate)
1682 {
1683 bot_goalstate_t *gs;
1684
1685 gs = BotGoalStateFromHandle(goalstate);
1686 if (!gs) return;
1687 Com_Memset(gs->goalstack, 0, MAX_GOALSTACK * sizeof(bot_goal_t));
1688 gs->goalstacktop = 0;
1689 BotResetAvoidGoals(goalstate);
1690 } //end of the function BotResetGoalState
1691 //===========================================================================
1692 //
1693 // Parameter: -
1694 // Returns: -
1695 // Changes Globals: -
1696 //===========================================================================
1697 int BotLoadItemWeights(int goalstate, char *filename)
1698 {
1699 bot_goalstate_t *gs;
1700
1701 gs = BotGoalStateFromHandle(goalstate);
1702 if (!gs) return BLERR_CANNOTLOADITEMWEIGHTS;
1703 //load the weight configuration
1704 gs->itemweightconfig = ReadWeightConfig(filename);
1705 if (!gs->itemweightconfig)
1706 {
1707 botimport.Print(PRT_FATAL, "couldn't load weights\n");
1708 return BLERR_CANNOTLOADITEMWEIGHTS;
1709 } //end if
1710 //if there's no item configuration
1711 if (!itemconfig) return BLERR_CANNOTLOADITEMWEIGHTS;
1712 //create the item weight index
1713 gs->itemweightindex = ItemWeightIndex(gs->itemweightconfig, itemconfig);
1714 //everything went ok
1715 return BLERR_NOERROR;
1716 } //end of the function BotLoadItemWeights
1717 //===========================================================================
1718 //
1719 // Parameter: -
1720 // Returns: -
1721 // Changes Globals: -
1722 //===========================================================================
1723 void BotFreeItemWeights(int goalstate)
1724 {
1725 bot_goalstate_t *gs;
1726
1727 gs = BotGoalStateFromHandle(goalstate);
1728 if (!gs) return;
1729 if (gs->itemweightconfig) FreeWeightConfig(gs->itemweightconfig);
1730 if (gs->itemweightindex) FreeMemory(gs->itemweightindex);
1731 } //end of the function BotFreeItemWeights
1732 //===========================================================================
1733 //
1734 // Parameter: -
1735 // Returns: -
1736 // Changes Globals: -
1737 //===========================================================================
1738 int BotAllocGoalState(int client)
1739 {
1740 int i;
1741
1742 for (i = 1; i <= MAX_CLIENTS; i++)
1743 {
1744 if (!botgoalstates[i])
1745 {
1746 botgoalstates[i] = GetClearedMemory(sizeof(bot_goalstate_t));
1747 botgoalstates[i]->client = client;
1748 return i;
1749 } //end if
1750 } //end for
1751 return 0;
1752 } //end of the function BotAllocGoalState
1753 //========================================================================
1754 //
1755 // Parameter: -
1756 // Returns: -
1757 // Changes Globals: -
1758 //========================================================================
1759 void BotFreeGoalState(int handle)
1760 {
1761 if (handle <= 0 || handle > MAX_CLIENTS)
1762 {
1763 botimport.Print(PRT_FATAL, "goal state handle %d out of range\n", handle);
1764 return;
1765 } //end if
1766 if (!botgoalstates[handle])
1767 {
1768 botimport.Print(PRT_FATAL, "invalid goal state handle %d\n", handle);
1769 return;
1770 } //end if
1771 BotFreeItemWeights(handle);
1772 FreeMemory(botgoalstates[handle]);
1773 botgoalstates[handle] = NULL;
1774 } //end of the function BotFreeGoalState
1775 //===========================================================================
1776 //
1777 // Parameter: -
1778 // Returns: -
1779 // Changes Globals: -
1780 //===========================================================================
1781 int BotSetupGoalAI(void)
1782 {
1783 char *filename;
1784
1785 //check if teamplay is on
1786 g_gametype = LibVarValue("g_gametype", "0");
1787 //item configuration file
1788 filename = LibVarString("itemconfig", "items.c");
1789 //load the item configuration
1790 itemconfig = LoadItemConfig(filename);
1791 if (!itemconfig)
1792 {
1793 botimport.Print(PRT_FATAL, "couldn't load item config\n");
1794 return BLERR_CANNOTLOADITEMCONFIG;
1795 } //end if
1796 //
1797 droppedweight = LibVar("droppedweight", "1000");
1798 //everything went ok
1799 return BLERR_NOERROR;
1800 } //end of the function BotSetupGoalAI
1801 //===========================================================================
1802 //
1803 // Parameter: -
1804 // Returns: -
1805 // Changes Globals: -
1806 //===========================================================================
1807 void BotShutdownGoalAI(void)
1808 {
1809 int i;
1810
1811 if (itemconfig) FreeMemory(itemconfig);
1812 itemconfig = NULL;
1813 if (levelitemheap) FreeMemory(levelitemheap);
1814 levelitemheap = NULL;
1815 freelevelitems = NULL;
1816 levelitems = NULL;
1817 numlevelitems = 0;
1818
1819 BotFreeInfoEntities();
1820
1821 for (i = 1; i <= MAX_CLIENTS; i++)
1822 {
1823 if (botgoalstates[i])
1824 {
1825 BotFreeGoalState(i);
1826 } //end if
1827 } //end for
1828 } //end of the function BotShutdownGoalAI