Debian Codegraph @

code/botlib/be_ai_weight.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_weight.c
25 *
26 * desc: fuzzy logic
27 *
28 * $Archive: /MissionPack/code/botlib/be_ai_weight.c $
29 *
30 *****************************************************************************/
31
32 #include "../qcommon/q_shared.h"
33 #include "l_memory.h"
34 #include "l_log.h"
35 #include "l_utils.h"
36 #include "l_script.h"
37 #include "l_precomp.h"
38 #include "l_struct.h"
39 #include "l_libvar.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
47 #define MAX_INVENTORYVALUE 999999
48 #define EVALUATERECURSIVELY
49
50 #define MAX_WEIGHT_FILES 128
51 weightconfig_t *weightFileList[MAX_WEIGHT_FILES];
52
53 //===========================================================================
54 //
55 // Parameter: -
56 // Returns: -
57 // Changes Globals: -
58 //===========================================================================
59 int ReadValue(source_t *source, float *value)
60 {
61 token_t token;
62
63 if (!PC_ExpectAnyToken(source, &token)) return qfalse;
64 if (!strcmp(token.string, "-"))
65 {
66 SourceWarning(source, "negative value set to zero");
67
68 if(!PC_ExpectAnyToken(source, &token))
69 {
70 SourceError(source, "Missing return value");
71 return qfalse;
72 }
73 }
74
75 if (token.type != TT_NUMBER)
76 {
77 SourceError(source, "invalid return value %s", token.string);
78 return qfalse;
79 }
80
81 *value = token.floatvalue;
82 return qtrue;
83 } //end of the function ReadValue
84 //===========================================================================
85 //
86 // Parameter: -
87 // Returns: -
88 // Changes Globals: -
89 //===========================================================================
90 int ReadFuzzyWeight(source_t *source, fuzzyseperator_t *fs)
91 {
92 if (PC_CheckTokenString(source, "balance"))
93 {
94 fs->type = WT_BALANCE;
95 if (!PC_ExpectTokenString(source, "(")) return qfalse;
96 if (!ReadValue(source, &fs->weight)) return qfalse;
97 if (!PC_ExpectTokenString(source, ",")) return qfalse;
98 if (!ReadValue(source, &fs->minweight)) return qfalse;
99 if (!PC_ExpectTokenString(source, ",")) return qfalse;
100 if (!ReadValue(source, &fs->maxweight)) return qfalse;
101 if (!PC_ExpectTokenString(source, ")")) return qfalse;
102 } //end if
103 else
104 {
105 fs->type = 0;
106 if (!ReadValue(source, &fs->weight)) return qfalse;
107 fs->minweight = fs->weight;
108 fs->maxweight = fs->weight;
109 } //end if
110 if (!PC_ExpectTokenString(source, ";")) return qfalse;
111 return qtrue;
112 } //end of the function ReadFuzzyWeight
113 //===========================================================================
114 //
115 // Parameter: -
116 // Returns: -
117 // Changes Globals: -
118 //===========================================================================
119 void FreeFuzzySeperators_r(fuzzyseperator_t *fs)
120 {
121 if (!fs) return;
122 if (fs->child) FreeFuzzySeperators_r(fs->child);
123 if (fs->next) FreeFuzzySeperators_r(fs->next);
124 FreeMemory(fs);
125 } //end of the function FreeFuzzySeperators
126 //===========================================================================
127 //
128 // Parameter: -
129 // Returns: -
130 // Changes Globals: -
131 //===========================================================================
132 void FreeWeightConfig2(weightconfig_t *config)
133 {
134 int i;
135
136 for (i = 0; i < config->numweights; i++)
137 {
138 FreeFuzzySeperators_r(config->weights[i].firstseperator);
139 if (config->weights[i].name) FreeMemory(config->weights[i].name);
140 } //end for
141 FreeMemory(config);
142 } //end of the function FreeWeightConfig2
143 //===========================================================================
144 //
145 // Parameter: -
146 // Returns: -
147 // Changes Globals: -
148 //===========================================================================
149 void FreeWeightConfig(weightconfig_t *config)
150 {
151 if (!LibVarGetValue("bot_reloadcharacters")) return;
152 FreeWeightConfig2(config);
153 } //end of the function FreeWeightConfig
154 //===========================================================================
155 //
156 // Parameter: -
157 // Returns: -
158 // Changes Globals: -
159 //===========================================================================
160 fuzzyseperator_t *ReadFuzzySeperators_r(source_t *source)
161 {
162 int newindent, index, def, founddefault;
163 token_t token;
164 fuzzyseperator_t *fs, *lastfs, *firstfs;
165
166 founddefault = qfalse;
167 firstfs = NULL;
168 lastfs = NULL;
169 if (!PC_ExpectTokenString(source, "(")) return NULL;
170 if (!PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token)) return NULL;
171 index = token.intvalue;
172 if (!PC_ExpectTokenString(source, ")")) return NULL;
173 if (!PC_ExpectTokenString(source, "{")) return NULL;
174 if (!PC_ExpectAnyToken(source, &token)) return NULL;
175 do
176 {
177 def = !strcmp(token.string, "default");
178 if (def || !strcmp(token.string, "case"))
179 {
180 fs = (fuzzyseperator_t *) GetClearedMemory(sizeof(fuzzyseperator_t));
181 fs->index = index;
182 if (lastfs) lastfs->next = fs;
183 else firstfs = fs;
184 lastfs = fs;
185 if (def)
186 {
187 if (founddefault)
188 {
189 SourceError(source, "switch already has a default");
190 FreeFuzzySeperators_r(firstfs);
191 return NULL;
192 } //end if
193 fs->value = MAX_INVENTORYVALUE;
194 founddefault = qtrue;
195 } //end if
196 else
197 {
198 if (!PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token))
199 {
200 FreeFuzzySeperators_r(firstfs);
201 return NULL;
202 } //end if
203 fs->value = token.intvalue;
204 } //end else
205 if (!PC_ExpectTokenString(source, ":") || !PC_ExpectAnyToken(source, &token))
206 {
207 FreeFuzzySeperators_r(firstfs);
208 return NULL;
209 } //end if
210 newindent = qfalse;
211 if (!strcmp(token.string, "{"))
212 {
213 newindent = qtrue;
214 if (!PC_ExpectAnyToken(source, &token))
215 {
216 FreeFuzzySeperators_r(firstfs);
217 return NULL;
218 } //end if
219 } //end if
220 if (!strcmp(token.string, "return"))
221 {
222 if (!ReadFuzzyWeight(source, fs))
223 {
224 FreeFuzzySeperators_r(firstfs);
225 return NULL;
226 } //end if
227 } //end if
228 else if (!strcmp(token.string, "switch"))
229 {
230 fs->child = ReadFuzzySeperators_r(source);
231 if (!fs->child)
232 {
233 FreeFuzzySeperators_r(firstfs);
234 return NULL;
235 } //end if
236 } //end else if
237 else
238 {
239 SourceError(source, "invalid name %s", token.string);
240 return NULL;
241 } //end else
242 if (newindent)
243 {
244 if (!PC_ExpectTokenString(source, "}"))
245 {
246 FreeFuzzySeperators_r(firstfs);
247 return NULL;
248 } //end if
249 } //end if
250 } //end if
251 else
252 {
253 FreeFuzzySeperators_r(firstfs);
254 SourceError(source, "invalid name %s", token.string);
255 return NULL;
256 } //end else
257 if (!PC_ExpectAnyToken(source, &token))
258 {
259 FreeFuzzySeperators_r(firstfs);
260 return NULL;
261 } //end if
262 } while(strcmp(token.string, "}"));
263 //
264 if (!founddefault)
265 {
266 SourceWarning(source, "switch without default");
267 fs = (fuzzyseperator_t *) GetClearedMemory(sizeof(fuzzyseperator_t));
268 fs->index = index;
269 fs->value = MAX_INVENTORYVALUE;
270 fs->weight = 0;
271 fs->next = NULL;
272 fs->child = NULL;
273 if (lastfs) lastfs->next = fs;
274 else firstfs = fs;
275 } //end if
276 //
277 return firstfs;
278 } //end of the function ReadFuzzySeperators_r
279 //===========================================================================
280 //
281 // Parameter: -
282 // Returns: -
283 // Changes Globals: -
284 //===========================================================================
285 weightconfig_t *ReadWeightConfig(char *filename)
286 {
287 int newindent, avail = 0, n;
288 token_t token;
289 source_t *source;
290 fuzzyseperator_t *fs;
291 weightconfig_t *config = NULL;
292 #ifdef DEBUG
293 int starttime;
294
295 starttime = Sys_MilliSeconds();
296 #endif //DEBUG
297
298 if (!LibVarGetValue("bot_reloadcharacters"))
299 {
300 avail = -1;
301 for( n = 0; n < MAX_WEIGHT_FILES; n++ )
302 {
303 config = weightFileList[n];
304 if( !config )
305 {
306 if( avail == -1 )
307 {
308 avail = n;
309 } //end if
310 continue;
311 } //end if
312 if( strcmp( filename, config->filename ) == 0 )
313 {
314 //botimport.Print( PRT_MESSAGE, "retained %s\n", filename );
315 return config;
316 } //end if
317 } //end for
318
319 if( avail == -1 )
320 {
321 botimport.Print( PRT_ERROR, "weightFileList was full trying to load %s\n", filename );
322 return NULL;
323 } //end if
324 } //end if
325
326 PC_SetBaseFolder(BOTFILESBASEFOLDER);
327 source = LoadSourceFile(filename);
328 if (!source)
329 {
330 botimport.Print(PRT_ERROR, "couldn't load %s\n", filename);
331 return NULL;
332 } //end if
333 //
334 config = (weightconfig_t *) GetClearedMemory(sizeof(weightconfig_t));
335 config->numweights = 0;
336 Q_strncpyz( config->filename, filename, sizeof(config->filename) );
337 //parse the item config file
338 while(PC_ReadToken(source, &token))
339 {
340 if (!strcmp(token.string, "weight"))
341 {
342 if (config->numweights >= MAX_WEIGHTS)
343 {
344 SourceWarning(source, "too many fuzzy weights");
345 break;
346 } //end if
347 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token))
348 {
349 FreeWeightConfig(config);
350 FreeSource(source);
351 return NULL;
352 } //end if
353 StripDoubleQuotes(token.string);
354 config->weights[config->numweights].name = (char *) GetClearedMemory(strlen(token.string) + 1);
355 strcpy(config->weights[config->numweights].name, token.string);
356 if (!PC_ExpectAnyToken(source, &token))
357 {
358 FreeWeightConfig(config);
359 FreeSource(source);
360 return NULL;
361 } //end if
362 newindent = qfalse;
363 if (!strcmp(token.string, "{"))
364 {
365 newindent = qtrue;
366 if (!PC_ExpectAnyToken(source, &token))
367 {
368 FreeWeightConfig(config);
369 FreeSource(source);
370 return NULL;
371 } //end if
372 } //end if
373 if (!strcmp(token.string, "switch"))
374 {
375 fs = ReadFuzzySeperators_r(source);
376 if (!fs)
377 {
378 FreeWeightConfig(config);
379 FreeSource(source);
380 return NULL;
381 } //end if
382 config->weights[config->numweights].firstseperator = fs;
383 } //end if
384 else if (!strcmp(token.string, "return"))
385 {
386 fs = (fuzzyseperator_t *) GetClearedMemory(sizeof(fuzzyseperator_t));
387 fs->index = 0;
388 fs->value = MAX_INVENTORYVALUE;
389 fs->next = NULL;
390 fs->child = NULL;
391 if (!ReadFuzzyWeight(source, fs))
392 {
393 FreeMemory(fs);
394 FreeWeightConfig(config);
395 FreeSource(source);
396 return NULL;
397 } //end if
398 config->weights[config->numweights].firstseperator = fs;
399 } //end else if
400 else
401 {
402 SourceError(source, "invalid name %s", token.string);
403 FreeWeightConfig(config);
404 FreeSource(source);
405 return NULL;
406 } //end else
407 if (newindent)
408 {
409 if (!PC_ExpectTokenString(source, "}"))
410 {
411 FreeWeightConfig(config);
412 FreeSource(source);
413 return NULL;
414 } //end if
415 } //end if
416 config->numweights++;
417 } //end if
418 else
419 {
420 SourceError(source, "invalid name %s", token.string);
421 FreeWeightConfig(config);
422 FreeSource(source);
423 return NULL;
424 } //end else
425 } //end while
426 //free the source at the end of a pass
427 FreeSource(source);
428 //if the file was located in a pak file
429 botimport.Print(PRT_MESSAGE, "loaded %s\n", filename);
430 #ifdef DEBUG
431 if (botDeveloper)
432 {
433 botimport.Print(PRT_MESSAGE, "weights loaded in %d msec\n", Sys_MilliSeconds() - starttime);
434 } //end if
435 #endif //DEBUG
436 //
437 if (!LibVarGetValue("bot_reloadcharacters"))
438 {
439 weightFileList[avail] = config;
440 } //end if
441 //
442 return config;
443 } //end of the function ReadWeightConfig
444 #if 0
445 //===========================================================================
446 //
447 // Parameter: -
448 // Returns: -
449 // Changes Globals: -
450 //===========================================================================
451 qboolean WriteFuzzyWeight(FILE *fp, fuzzyseperator_t *fs)
452 {
453 if (fs->type == WT_BALANCE)
454 {
455 if (fprintf(fp, " return balance(") < 0) return qfalse;
456 if (!WriteFloat(fp, fs->weight)) return qfalse;
457 if (fprintf(fp, ",") < 0) return qfalse;
458 if (!WriteFloat(fp, fs->minweight)) return qfalse;
459 if (fprintf(fp, ",") < 0) return qfalse;
460 if (!WriteFloat(fp, fs->maxweight)) return qfalse;
461 if (fprintf(fp, ");\n") < 0) return qfalse;
462 } //end if
463 else
464 {
465 if (fprintf(fp, " return ") < 0) return qfalse;
466 if (!WriteFloat(fp, fs->weight)) return qfalse;
467 if (fprintf(fp, ";\n") < 0) return qfalse;
468 } //end else
469 return qtrue;
470 } //end of the function WriteFuzzyWeight
471 //===========================================================================
472 //
473 // Parameter: -
474 // Returns: -
475 // Changes Globals: -
476 //===========================================================================
477 qboolean WriteFuzzySeperators_r(FILE *fp, fuzzyseperator_t *fs, int indent)
478 {
479 if (!WriteIndent(fp, indent)) return qfalse;
480 if (fprintf(fp, "switch(%d)\n", fs->index) < 0) return qfalse;
481 if (!WriteIndent(fp, indent)) return qfalse;
482 if (fprintf(fp, "{\n") < 0) return qfalse;
483 indent++;
484 do
485 {
486 if (!WriteIndent(fp, indent)) return qfalse;
487 if (fs->next)
488 {
489 if (fprintf(fp, "case %d:", fs->value) < 0) return qfalse;
490 } //end if
491 else
492 {
493 if (fprintf(fp, "default:") < 0) return qfalse;
494 } //end else
495 if (fs->child)
496 {
497 if (fprintf(fp, "\n") < 0) return qfalse;
498 if (!WriteIndent(fp, indent)) return qfalse;
499 if (fprintf(fp, "{\n") < 0) return qfalse;
500 if (!WriteFuzzySeperators_r(fp, fs->child, indent + 1)) return qfalse;
501 if (!WriteIndent(fp, indent)) return qfalse;
502 if (fs->next)
503 {
504 if (fprintf(fp, "} //end case\n") < 0) return qfalse;
505 } //end if
506 else
507 {
508 if (fprintf(fp, "} //end default\n") < 0) return qfalse;
509 } //end else
510 } //end if
511 else
512 {
513 if (!WriteFuzzyWeight(fp, fs)) return qfalse;
514 } //end else
515 fs = fs->next;
516 } while(fs);
517 indent--;
518 if (!WriteIndent(fp, indent)) return qfalse;
519 if (fprintf(fp, "} //end switch\n") < 0) return qfalse;
520 return qtrue;
521 } //end of the function WriteItemFuzzyWeights_r
522 //===========================================================================
523 //
524 // Parameter: -
525 // Returns: -
526 // Changes Globals: -
527 //===========================================================================
528 qboolean WriteWeightConfig(char *filename, weightconfig_t *config)
529 {
530 int i;
531 FILE *fp;
532 weight_t *ifw;
533
534 fp = fopen(filename, "wb");
535 if (!fp) return qfalse;
536
537 for (i = 0; i < config->numweights; i++)
538 {
539 ifw = &config->weights[i];
540 if (fprintf(fp, "\nweight \"%s\"\n", ifw->name) < 0) return qfalse;
541 if (fprintf(fp, "{\n") < 0) return qfalse;
542 if (ifw->firstseperator->index > 0)
543 {
544 if (!WriteFuzzySeperators_r(fp, ifw->firstseperator, 1)) return qfalse;
545 } //end if
546 else
547 {
548 if (!WriteIndent(fp, 1)) return qfalse;
549 if (!WriteFuzzyWeight(fp, ifw->firstseperator)) return qfalse;
550 } //end else
551 if (fprintf(fp, "} //end weight\n") < 0) return qfalse;
552 } //end for
553 fclose(fp);
554 return qtrue;
555 } //end of the function WriteWeightConfig
556 #endif
557 //===========================================================================
558 //
559 // Parameter: -
560 // Returns: -
561 // Changes Globals: -
562 //===========================================================================
563 int FindFuzzyWeight(weightconfig_t *wc, char *name)
564 {
565 int i;
566
567 for (i = 0; i < wc->numweights; i++)
568 {
569 if (!strcmp(wc->weights[i].name, name))
570 {
571 return i;
572 } //end if
573 } //end if
574 return -1;
575 } //end of the function FindFuzzyWeight
576 //===========================================================================
577 //
578 // Parameter: -
579 // Returns: -
580 // Changes Globals: -
581 //===========================================================================
582 float FuzzyWeight_r(int *inventory, fuzzyseperator_t *fs)
583 {
584 float scale, w1, w2;
585
586 if (inventory[fs->index] < fs->value)
587 {
588 if (fs->child) return FuzzyWeight_r(inventory, fs->child);
589 else return fs->weight;
590 } //end if
591 else if (fs->next)
592 {
593 if (inventory[fs->index] < fs->next->value)
594 {
595 //first weight
596 if (fs->child) w1 = FuzzyWeight_r(inventory, fs->child);
597 else w1 = fs->weight;
598 //second weight
599 if (fs->next->child) w2 = FuzzyWeight_r(inventory, fs->next->child);
600 else w2 = fs->next->weight;
601 //the scale factor
602 if(fs->next->value == MAX_INVENTORYVALUE) // is fs->next the default case?
603 return w2; // can't interpolate, return default weight
604 else
605 scale = (float) (inventory[fs->index] - fs->value) / (fs->next->value - fs->value);
606 //scale between the two weights
607 return (1 - scale) * w1 + scale * w2;
608 } //end if
609 return FuzzyWeight_r(inventory, fs->next);
610 } //end else if
611 return fs->weight;
612 } //end of the function FuzzyWeight_r
613 //===========================================================================
614 //
615 // Parameter: -
616 // Returns: -
617 // Changes Globals: -
618 //===========================================================================
619 float FuzzyWeightUndecided_r(int *inventory, fuzzyseperator_t *fs)
620 {
621 float scale, w1, w2;
622
623 if (inventory[fs->index] < fs->value)
624 {
625 if (fs->child) return FuzzyWeightUndecided_r(inventory, fs->child);
626 else return fs->minweight + random() * (fs->maxweight - fs->minweight);
627 } //end if
628 else if (fs->next)
629 {
630 if (inventory[fs->index] < fs->next->value)
631 {
632 //first weight
633 if (fs->child) w1 = FuzzyWeightUndecided_r(inventory, fs->child);
634 else w1 = fs->minweight + random() * (fs->maxweight - fs->minweight);
635 //second weight
636 if (fs->next->child) w2 = FuzzyWeight_r(inventory, fs->next->child);
637 else w2 = fs->next->minweight + random() * (fs->next->maxweight - fs->next->minweight);
638 //the scale factor
639 if(fs->next->value == MAX_INVENTORYVALUE) // is fs->next the default case?
640 return w2; // can't interpolate, return default weight
641 else
642 scale = (float) (inventory[fs->index] - fs->value) / (fs->next->value - fs->value);
643 //scale between the two weights
644 return (1 - scale) * w1 + scale * w2;
645 } //end if
646 return FuzzyWeightUndecided_r(inventory, fs->next);
647 } //end else if
648 return fs->weight;
649 } //end of the function FuzzyWeightUndecided_r
650 //===========================================================================
651 //
652 // Parameter: -
653 // Returns: -
654 // Changes Globals: -
655 //===========================================================================
656 float FuzzyWeight(int *inventory, weightconfig_t *wc, int weightnum)
657 {
658 #ifdef EVALUATERECURSIVELY
659 return FuzzyWeight_r(inventory, wc->weights[weightnum].firstseperator);
660 #else
661 fuzzyseperator_t *s;
662
663 s = wc->weights[weightnum].firstseperator;
664 if (!s) return 0;
665 while(1)
666 {
667 if (inventory[s->index] < s->value)
668 {
669 if (s->child) s = s->child;
670 else return s->weight;
671 } //end if
672 else
673 {
674 if (s->next) s = s->next;
675 else return s->weight;
676 } //end else
677 } //end if
678 return 0;
679 #endif
680 } //end of the function FuzzyWeight
681 //===========================================================================
682 //
683 // Parameter: -
684 // Returns: -
685 // Changes Globals: -
686 //===========================================================================
687 float FuzzyWeightUndecided(int *inventory, weightconfig_t *wc, int weightnum)
688 {
689 #ifdef EVALUATERECURSIVELY
690 return FuzzyWeightUndecided_r(inventory, wc->weights[weightnum].firstseperator);
691 #else
692 fuzzyseperator_t *s;
693
694 s = wc->weights[weightnum].firstseperator;
695 if (!s) return 0;
696 while(1)
697 {
698 if (inventory[s->index] < s->value)
699 {
700 if (s->child) s = s->child;
701 else return s->minweight + random() * (s->maxweight - s->minweight);
702 } //end if
703 else
704 {
705 if (s->next) s = s->next;
706 else return s->minweight + random() * (s->maxweight - s->minweight);
707 } //end else
708 } //end if
709 return 0;
710 #endif
711 } //end of the function FuzzyWeightUndecided
712 //===========================================================================
713 //
714 // Parameter: -
715 // Returns: -
716 // Changes Globals: -
717 //===========================================================================
718 void EvolveFuzzySeperator_r(fuzzyseperator_t *fs)
719 {
720 if (fs->child)
721 {
722 EvolveFuzzySeperator_r(fs->child);
723 } //end if
724 else if (fs->type == WT_BALANCE)
725 {
726 //every once in a while an evolution leap occurs, mutation
727 if (random() < 0.01) fs->weight += crandom() * (fs->maxweight - fs->minweight);
728 else fs->weight += crandom() * (fs->maxweight - fs->minweight) * 0.5;
729 //modify bounds if necessary because of mutation
730 if (fs->weight < fs->minweight) fs->minweight = fs->weight;
731 else if (fs->weight > fs->maxweight) fs->maxweight = fs->weight;
732 } //end else if
733 if (fs->next) EvolveFuzzySeperator_r(fs->next);
734 } //end of the function EvolveFuzzySeperator_r
735 //===========================================================================
736 //
737 // Parameter: -
738 // Returns: -
739 // Changes Globals: -
740 //===========================================================================
741 void EvolveWeightConfig(weightconfig_t *config)
742 {
743 int i;
744
745 for (i = 0; i < config->numweights; i++)
746 {
747 EvolveFuzzySeperator_r(config->weights[i].firstseperator);
748 } //end for
749 } //end of the function EvolveWeightConfig
750 //===========================================================================
751 //
752 // Parameter: -
753 // Returns: -
754 // Changes Globals: -
755 //===========================================================================
756 void ScaleFuzzySeperator_r(fuzzyseperator_t *fs, float scale)
757 {
758 if (fs->child)
759 {
760 ScaleFuzzySeperator_r(fs->child, scale);
761 } //end if
762 else if (fs->type == WT_BALANCE)
763 {
764 //
765 fs->weight = (float) (fs->maxweight + fs->minweight) * scale;
766 //get the weight between bounds
767 if (fs->weight < fs->minweight) fs->weight = fs->minweight;
768 else if (fs->weight > fs->maxweight) fs->weight = fs->maxweight;
769 } //end else if
770 if (fs->next) ScaleFuzzySeperator_r(fs->next, scale);
771 } //end of the function ScaleFuzzySeperator_r
772 //===========================================================================
773 //
774 // Parameter: -
775 // Returns: -
776 // Changes Globals: -
777 //===========================================================================
778 void ScaleWeight(weightconfig_t *config, char *name, float scale)
779 {
780 int i;
781
782 if (scale < 0) scale = 0;
783 else if (scale > 1) scale = 1;
784 for (i = 0; i < config->numweights; i++)
785 {
786 if (!strcmp(name, config->weights[i].name))
787 {
788 ScaleFuzzySeperator_r(config->weights[i].firstseperator, scale);
789 break;
790 } //end if
791 } //end for
792 } //end of the function ScaleWeight
793 //===========================================================================
794 //
795 // Parameter: -
796 // Returns: -
797 // Changes Globals: -
798 //===========================================================================
799 void ScaleFuzzySeperatorBalanceRange_r(fuzzyseperator_t *fs, float scale)
800 {
801 if (fs->child)
802 {
803 ScaleFuzzySeperatorBalanceRange_r(fs->child, scale);
804 } //end if
805 else if (fs->type == WT_BALANCE)
806 {
807 float mid = (fs->minweight + fs->maxweight) * 0.5;
808 //get the weight between bounds
809 fs->maxweight = mid + (fs->maxweight - mid) * scale;
810 fs->minweight = mid + (fs->minweight - mid) * scale;
811 if (fs->maxweight < fs->minweight)
812 {
813 fs->maxweight = fs->minweight;
814 } //end if
815 } //end else if
816 if (fs->next) ScaleFuzzySeperatorBalanceRange_r(fs->next, scale);
817 } //end of the function ScaleFuzzySeperatorBalanceRange_r
818 //===========================================================================
819 //
820 // Parameter: -
821 // Returns: -
822 // Changes Globals: -
823 //===========================================================================
824 void ScaleFuzzyBalanceRange(weightconfig_t *config, float scale)
825 {
826 int i;
827
828 if (scale < 0) scale = 0;
829 else if (scale > 100) scale = 100;
830 for (i = 0; i < config->numweights; i++)
831 {
832 ScaleFuzzySeperatorBalanceRange_r(config->weights[i].firstseperator, scale);
833 } //end for
834 } //end of the function ScaleFuzzyBalanceRange
835 //===========================================================================
836 //
837 // Parameter: -
838 // Returns: -
839 // Changes Globals: -
840 //===========================================================================
841 int InterbreedFuzzySeperator_r(fuzzyseperator_t *fs1, fuzzyseperator_t *fs2,
842 fuzzyseperator_t *fsout)
843 {
844 if (fs1->child)
845 {
846 if (!fs2->child || !fsout->child)
847 {
848 botimport.Print(PRT_ERROR, "cannot interbreed weight configs, unequal child\n");
849 return qfalse;
850 } //end if
851 if (!InterbreedFuzzySeperator_r(fs2->child, fs2->child, fsout->child))
852 {
853 return qfalse;
854 } //end if
855 } //end if
856 else if (fs1->type == WT_BALANCE)
857 {
858 if (fs2->type != WT_BALANCE || fsout->type != WT_BALANCE)
859 {
860 botimport.Print(PRT_ERROR, "cannot interbreed weight configs, unequal balance\n");
861 return qfalse;
862 } //end if
863 fsout->weight = (fs1->weight + fs2->weight) / 2;
864 if (fsout->weight > fsout->maxweight) fsout->maxweight = fsout->weight;
865 if (fsout->weight > fsout->minweight) fsout->minweight = fsout->weight;
866 } //end else if
867 if (fs1->next)
868 {
869 if (!fs2->next || !fsout->next)
870 {
871 botimport.Print(PRT_ERROR, "cannot interbreed weight configs, unequal next\n");
872 return qfalse;
873 } //end if
874 if (!InterbreedFuzzySeperator_r(fs1->next, fs2->next, fsout->next))
875 {
876 return qfalse;
877 } //end if
878 } //end if
879 return qtrue;
880 } //end of the function InterbreedFuzzySeperator_r
881 //===========================================================================
882 // config1 and config2 are interbreeded and stored in configout
883 //
884 // Parameter: -
885 // Returns: -
886 // Changes Globals: -
887 //===========================================================================
888 void InterbreedWeightConfigs(weightconfig_t *config1, weightconfig_t *config2,
889 weightconfig_t *configout)
890 {
891 int i;
892
893 if (config1->numweights != config2->numweights ||
894 config1->numweights != configout->numweights)
895 {
896 botimport.Print(PRT_ERROR, "cannot interbreed weight configs, unequal numweights\n");
897 return;
898 } //end if
899 for (i = 0; i < config1->numweights; i++)
900 {
901 InterbreedFuzzySeperator_r(config1->weights[i].firstseperator,
902 config2->weights[i].firstseperator,
903 configout->weights[i].firstseperator);
904 } //end for
905 } //end of the function InterbreedWeightConfigs
906 //===========================================================================
907 //
908 // Parameter: -
909 // Returns: -
910 // Changes Globals: -
911 //===========================================================================
912 void BotShutdownWeights(void)
913 {
914 int i;
915
916 for( i = 0; i < MAX_WEIGHT_FILES; i++ )
917 {
918 if (weightFileList[i])
919 {
920 FreeWeightConfig2(weightFileList[i]);
921 weightFileList[i] = NULL;
922 } //end if
923 } //end for
924 } //end of the function BotShutdownWeights