| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#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 |
|
| 56 |
|
| 57 |
|
| 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 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
} |
| 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 |
} |
| 110 |
if (!PC_ExpectTokenString(source, ";")) return qfalse; |
| 111 |
return qtrue; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
} |
| 141 |
FreeMemory(config); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
void FreeWeightConfig(weightconfig_t *config) |
| 150 |
{ |
| 151 |
if (!LibVarGetValue("bot_reloadcharacters")) return; |
| 152 |
FreeWeightConfig2(config); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 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 |
} |
| 193 |
fs->value = MAX_INVENTORYVALUE; |
| 194 |
founddefault = qtrue; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
if (!PC_ExpectTokenType(source, TT_NUMBER, TT_INTEGER, &token)) |
| 199 |
{ |
| 200 |
FreeFuzzySeperators_r(firstfs); |
| 201 |
return NULL; |
| 202 |
} |
| 203 |
fs->value = token.intvalue; |
| 204 |
} |
| 205 |
if (!PC_ExpectTokenString(source, ":") || !PC_ExpectAnyToken(source, &token)) |
| 206 |
{ |
| 207 |
FreeFuzzySeperators_r(firstfs); |
| 208 |
return NULL; |
| 209 |
} |
| 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 |
} |
| 219 |
} |
| 220 |
if (!strcmp(token.string, "return")) |
| 221 |
{ |
| 222 |
if (!ReadFuzzyWeight(source, fs)) |
| 223 |
{ |
| 224 |
FreeFuzzySeperators_r(firstfs); |
| 225 |
return NULL; |
| 226 |
} |
| 227 |
} |
| 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 |
} |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
SourceError(source, "invalid name %s", token.string); |
| 240 |
return NULL; |
| 241 |
} |
| 242 |
if (newindent) |
| 243 |
{ |
| 244 |
if (!PC_ExpectTokenString(source, "}")) |
| 245 |
{ |
| 246 |
FreeFuzzySeperators_r(firstfs); |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
FreeFuzzySeperators_r(firstfs); |
| 254 |
SourceError(source, "invalid name %s", token.string); |
| 255 |
return NULL; |
| 256 |
} |
| 257 |
if (!PC_ExpectAnyToken(source, &token)) |
| 258 |
{ |
| 259 |
FreeFuzzySeperators_r(firstfs); |
| 260 |
return NULL; |
| 261 |
} |
| 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 |
} |
| 276 |
|
| 277 |
return firstfs; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 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 |
| 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 |
} |
| 310 |
continue; |
| 311 |
} |
| 312 |
if( strcmp( filename, config->filename ) == 0 ) |
| 313 |
{ |
| 314 |
|
| 315 |
return config; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if( avail == -1 ) |
| 320 |
{ |
| 321 |
botimport.Print( PRT_ERROR, "weightFileList was full trying to load %s\n", filename ); |
| 322 |
return NULL; |
| 323 |
} |
| 324 |
} |
| 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 |
} |
| 333 |
|
| 334 |
config = (weightconfig_t *) GetClearedMemory(sizeof(weightconfig_t)); |
| 335 |
config->numweights = 0; |
| 336 |
Q_strncpyz( config->filename, filename, sizeof(config->filename) ); |
| 337 |
|
| 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 |
} |
| 347 |
if (!PC_ExpectTokenType(source, TT_STRING, 0, &token)) |
| 348 |
{ |
| 349 |
FreeWeightConfig(config); |
| 350 |
FreeSource(source); |
| 351 |
return NULL; |
| 352 |
} |
| 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 |
} |
| 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 |
} |
| 372 |
} |
| 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 |
} |
| 382 |
config->weights[config->numweights].firstseperator = fs; |
| 383 |
} |
| 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 |
} |
| 398 |
config->weights[config->numweights].firstseperator = fs; |
| 399 |
} |
| 400 |
else |
| 401 |
{ |
| 402 |
SourceError(source, "invalid name %s", token.string); |
| 403 |
FreeWeightConfig(config); |
| 404 |
FreeSource(source); |
| 405 |
return NULL; |
| 406 |
} |
| 407 |
if (newindent) |
| 408 |
{ |
| 409 |
if (!PC_ExpectTokenString(source, "}")) |
| 410 |
{ |
| 411 |
FreeWeightConfig(config); |
| 412 |
FreeSource(source); |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
} |
| 416 |
config->numweights++; |
| 417 |
} |
| 418 |
else |
| 419 |
{ |
| 420 |
SourceError(source, "invalid name %s", token.string); |
| 421 |
FreeWeightConfig(config); |
| 422 |
FreeSource(source); |
| 423 |
return NULL; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
FreeSource(source); |
| 428 |
|
| 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 |
} |
| 435 |
#endif |
| 436 |
|
| 437 |
if (!LibVarGetValue("bot_reloadcharacters")) |
| 438 |
{ |
| 439 |
weightFileList[avail] = config; |
| 440 |
} |
| 441 |
|
| 442 |
return config; |
| 443 |
} |
| 444 |
#if 0 |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 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 |
} |
| 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 |
} |
| 469 |
return qtrue; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 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 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
if (fprintf(fp, "default:") < 0) return qfalse; |
| 494 |
} |
| 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 |
} |
| 506 |
else |
| 507 |
{ |
| 508 |
if (fprintf(fp, "} //end default\n") < 0) return qfalse; |
| 509 |
} |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
if (!WriteFuzzyWeight(fp, fs)) return qfalse; |
| 514 |
} |
| 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 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 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 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
if (!WriteIndent(fp, 1)) return qfalse; |
| 549 |
if (!WriteFuzzyWeight(fp, ifw->firstseperator)) return qfalse; |
| 550 |
} |
| 551 |
if (fprintf(fp, "} //end weight\n") < 0) return qfalse; |
| 552 |
} |
| 553 |
fclose(fp); |
| 554 |
return qtrue; |
| 555 |
} |
| 556 |
#endif |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 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 |
} |
| 573 |
} |
| 574 |
return -1; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 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 |
} |
| 591 |
else if (fs->next) |
| 592 |
{ |
| 593 |
if (inventory[fs->index] < fs->next->value) |
| 594 |
{ |
| 595 |
|
| 596 |
if (fs->child) w1 = FuzzyWeight_r(inventory, fs->child); |
| 597 |
else w1 = fs->weight; |
| 598 |
|
| 599 |
if (fs->next->child) w2 = FuzzyWeight_r(inventory, fs->next->child); |
| 600 |
else w2 = fs->next->weight; |
| 601 |
|
| 602 |
if(fs->next->value == MAX_INVENTORYVALUE) |
| 603 |
return w2; |
| 604 |
else |
| 605 |
scale = (float) (inventory[fs->index] - fs->value) / (fs->next->value - fs->value); |
| 606 |
|
| 607 |
return (1 - scale) * w1 + scale * w2; |
| 608 |
} |
| 609 |
return FuzzyWeight_r(inventory, fs->next); |
| 610 |
} |
| 611 |
return fs->weight; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 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 |
} |
| 628 |
else if (fs->next) |
| 629 |
{ |
| 630 |
if (inventory[fs->index] < fs->next->value) |
| 631 |
{ |
| 632 |
|
| 633 |
if (fs->child) w1 = FuzzyWeightUndecided_r(inventory, fs->child); |
| 634 |
else w1 = fs->minweight + random() * (fs->maxweight - fs->minweight); |
| 635 |
|
| 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 |
|
| 639 |
if(fs->next->value == MAX_INVENTORYVALUE) |
| 640 |
return w2; |
| 641 |
else |
| 642 |
scale = (float) (inventory[fs->index] - fs->value) / (fs->next->value - fs->value); |
| 643 |
|
| 644 |
return (1 - scale) * w1 + scale * w2; |
| 645 |
} |
| 646 |
return FuzzyWeightUndecided_r(inventory, fs->next); |
| 647 |
} |
| 648 |
return fs->weight; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 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 |
} |
| 672 |
else |
| 673 |
{ |
| 674 |
if (s->next) s = s->next; |
| 675 |
else return s->weight; |
| 676 |
} |
| 677 |
} |
| 678 |
return 0; |
| 679 |
#endif |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 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 |
} |
| 703 |
else |
| 704 |
{ |
| 705 |
if (s->next) s = s->next; |
| 706 |
else return s->minweight + random() * (s->maxweight - s->minweight); |
| 707 |
} |
| 708 |
} |
| 709 |
return 0; |
| 710 |
#endif |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
void EvolveFuzzySeperator_r(fuzzyseperator_t *fs) |
| 719 |
{ |
| 720 |
if (fs->child) |
| 721 |
{ |
| 722 |
EvolveFuzzySeperator_r(fs->child); |
| 723 |
} |
| 724 |
else if (fs->type == WT_BALANCE) |
| 725 |
{ |
| 726 |
|
| 727 |
if (random() < 0.01) fs->weight += crandom() * (fs->maxweight - fs->minweight); |
| 728 |
else fs->weight += crandom() * (fs->maxweight - fs->minweight) * 0.5; |
| 729 |
|
| 730 |
if (fs->weight < fs->minweight) fs->minweight = fs->weight; |
| 731 |
else if (fs->weight > fs->maxweight) fs->maxweight = fs->weight; |
| 732 |
} |
| 733 |
if (fs->next) EvolveFuzzySeperator_r(fs->next); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 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 |
} |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
void ScaleFuzzySeperator_r(fuzzyseperator_t *fs, float scale) |
| 757 |
{ |
| 758 |
if (fs->child) |
| 759 |
{ |
| 760 |
ScaleFuzzySeperator_r(fs->child, scale); |
| 761 |
} |
| 762 |
else if (fs->type == WT_BALANCE) |
| 763 |
{ |
| 764 |
|
| 765 |
fs->weight = (float) (fs->maxweight + fs->minweight) * scale; |
| 766 |
|
| 767 |
if (fs->weight < fs->minweight) fs->weight = fs->minweight; |
| 768 |
else if (fs->weight > fs->maxweight) fs->weight = fs->maxweight; |
| 769 |
} |
| 770 |
if (fs->next) ScaleFuzzySeperator_r(fs->next, scale); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 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 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
void ScaleFuzzySeperatorBalanceRange_r(fuzzyseperator_t *fs, float scale) |
| 800 |
{ |
| 801 |
if (fs->child) |
| 802 |
{ |
| 803 |
ScaleFuzzySeperatorBalanceRange_r(fs->child, scale); |
| 804 |
} |
| 805 |
else if (fs->type == WT_BALANCE) |
| 806 |
{ |
| 807 |
float mid = (fs->minweight + fs->maxweight) * 0.5; |
| 808 |
|
| 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 |
} |
| 815 |
} |
| 816 |
if (fs->next) ScaleFuzzySeperatorBalanceRange_r(fs->next, scale); |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 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 |
} |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 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 |
} |
| 851 |
if (!InterbreedFuzzySeperator_r(fs2->child, fs2->child, fsout->child)) |
| 852 |
{ |
| 853 |
return qfalse; |
| 854 |
} |
| 855 |
} |
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 874 |
if (!InterbreedFuzzySeperator_r(fs1->next, fs2->next, fsout->next)) |
| 875 |
{ |
| 876 |
return qfalse; |
| 877 |
} |
| 878 |
} |
| 879 |
return qtrue; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 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 |
} |
| 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 |
} |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 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 |
} |
| 923 |
} |
| 924 |
} |