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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
|
.float bot_cmdqueuebuf_allocated;
.float bot_cmdqueuebuf;
.float bot_cmdqueuebuf_start;
.float bot_cmdqueuebuf_end;
void bot_clearqueue(entity bot)
{
if(!bot.bot_cmdqueuebuf_allocated)
error("clearqueue but no queue allocated");
buf_del(bot.bot_cmdqueuebuf);
bot.bot_cmdqueuebuf_allocated = FALSE;
dprint("bot ", bot.netname, " queue cleared\n");
}
void bot_queuecommand(entity bot, string cmdstring)
{
if(!bot.bot_cmdqueuebuf_allocated)
{
bot.bot_cmdqueuebuf = buf_create();
bot.bot_cmdqueuebuf_allocated = TRUE;
}
bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
bot.bot_cmdqueuebuf_end += 1;
}
void bot_dequeuecommand(entity bot, float idx)
{
if(!bot.bot_cmdqueuebuf_allocated)
error("dequeuecommand but no queue allocated");
if(idx < bot.bot_cmdqueuebuf_start)
error("dequeueing a command in the past");
if(idx >= bot.bot_cmdqueuebuf_end)
error("dequeueing a command in the future");
bufstr_set(bot.bot_cmdqueuebuf, idx, "");
if(idx == bot.bot_cmdqueuebuf_start)
bot.bot_cmdqueuebuf_start += 1;
if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
bot_clearqueue(bot);
}
string bot_readcommand(entity bot, float idx)
{
if(!bot.bot_cmdqueuebuf_allocated)
error("readcommand but no queue allocated");
if(idx < bot.bot_cmdqueuebuf_start)
error("reading a command in the past");
if(idx >= bot.bot_cmdqueuebuf_end)
error("reading a command in the future");
return bufstr_get(bot.bot_cmdqueuebuf, idx);
}
float bot_havecommand(entity bot, float idx)
{
if(!bot.bot_cmdqueuebuf_allocated)
return 0;
if(idx < bot.bot_cmdqueuebuf_start)
return 0;
if(idx >= bot.bot_cmdqueuebuf_end)
return 0;
return 1;
}
#define MAX_BOT_PLACES 4
.float bot_places_count;
.entity bot_places[MAX_BOT_PLACES]; FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(bot_places);
.string bot_placenames[MAX_BOT_PLACES]; FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(bot_placenames);
entity bot_getplace(string placename)
{
entity e;
if(substring(placename, 0, 1) == "@")
{
float i, p;
placename = substring(placename, 1, -1);
string s, s2;
for(i = 0; i < self.bot_places_count; ++i)
if(self.(bot_placenames[i]) == placename)
return self.(bot_places[i]);
// now: i == self.bot_places_count
s = s2 = cvar_string(placename);
p = strstrofs(s2, " ", 0);
if(p >= 0)
{
s = substring(s2, 0, p);
//print("places: ", placename, " -> ", cvar_string(placename), "\n");
cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
//print("places: ", placename, " := ", cvar_string(placename), "\n");
}
e = find(world, targetname, s);
if(!e)
print("invalid place ", s, "\n");
if(i < MAX_BOT_PLACES)
{
self.(bot_placenames[i]) = strzone(placename);
self.(bot_places[i]) = e;
self.bot_places_count += 1;
}
return e;
}
else
{
e = find(world, targetname, placename);
if(!e)
print("invalid place ", s, "\n");
return e;
}
}
// NOTE: New commands should be added here. Do not forget to update BOT_CMD_COUNTER
#define BOT_CMD_NULL 0
#define BOT_CMD_PAUSE 1
#define BOT_CMD_CONTINUE 2
#define BOT_CMD_WAIT 3
#define BOT_CMD_TURN 4
#define BOT_CMD_MOVETO 5
#define BOT_CMD_RESETGOAL 6 // Not implemented yet
#define BOT_CMD_CC 7
#define BOT_CMD_IF 8
#define BOT_CMD_ELSE 9
#define BOT_CMD_FI 10
#define BOT_CMD_RESETAIM 11
#define BOT_CMD_AIM 12
#define BOT_CMD_PRESSKEY 13
#define BOT_CMD_RELEASEKEY 14
#define BOT_CMD_SELECTWEAPON 15
#define BOT_CMD_IMPULSE 16
#define BOT_CMD_WAIT_UNTIL 17
#define BOT_CMD_MOVETOTARGET 18
#define BOT_CMD_AIMTARGET 19
#define BOT_CMD_BARRIER 20
#define BOT_CMD_CONSOLE 21
#define BOT_CMD_SOUND 22
#define BOT_CMD_WHILE 23 // TODO: Not implemented yet
#define BOT_CMD_WEND 24 // TODO: Not implemented yet
#define BOT_CMD_CHASE 25 // TODO: Not implemented yet
#define BOT_CMD_COUNTER 23 // Update this value if you add/remove a command
// NOTE: Following commands should be implemented on the bot ai
// If a new command should be handled by the target ai(s) please declare it here
.float(vector) cmd_moveto;
.float() cmd_resetgoal;
//
#define BOT_CMD_PARAMETER_NONE 0
#define BOT_CMD_PARAMETER_FLOAT 1
#define BOT_CMD_PARAMETER_STRING 2
#define BOT_CMD_PARAMETER_VECTOR 3
float bot_cmds_initialized;
float bot_cmd_parm_type[BOT_CMD_COUNTER];
string bot_cmd_string[BOT_CMD_COUNTER];
// Bots command queue
entity bot_cmd; // global current command
.entity bot_cmd_current; // current command of this bot
.float is_bot_cmd; // Tells if the entity is a bot command
.float bot_cmd_index; // Position of the command in the queue
.float bot_cmd_type; // If of command (see the BOT_CMD_* defines)
.float bot_cmd_parm_float; // Field to store a float parameter
.string bot_cmd_parm_string; // Field to store a string parameter
.vector bot_cmd_parm_vector; // Field to store a vector parameter
float bot_barriertime;
.float bot_barrier;
.float bot_cmd_execution_index; // Position in the queue of the command to be executed
// Initialize global commands list
// NOTE: New commands should be initialized here
void bot_commands_init()
{
bot_cmd_string[BOT_CMD_NULL] = "";
bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_PAUSE] = "pause";
bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_WAIT] = "wait";
bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
bot_cmd_string[BOT_CMD_TURN] = "turn";
bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_CC] = "cc";
bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_IF] = "if";
bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_ELSE] = "else";
bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_FI] = "fi";
bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_AIM] = "aim";
bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
bot_cmd_string[BOT_CMD_CONSOLE] = "console";
bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
bot_cmd_string[BOT_CMD_SOUND] = "sound";
bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
bot_cmds_initialized = TRUE;
}
// Returns first bot with matching name
entity find_bot_by_name(string name)
{
local entity bot;
bot = findchainflags(flags, FL_CLIENT);
while (bot)
{
if(clienttype(bot) == CLIENTTYPE_BOT)
if(bot.netname==name)
return bot;
bot = bot.chain;
}
return world;
}
// Returns a bot by number on list
entity find_bot_by_number(float number)
{
local entity bot;
local float c;
if(!number)
return world;
bot = findchainflags(flags, FL_CLIENT);
while (bot)
{
if(clienttype(bot) == CLIENTTYPE_BOT)
{
if(++c==number)
return bot;
}
bot = bot.chain;
}
return world;
}
float bot_decodecommand(string cmdstring)
{
local float cmd_parm_type, i;
float sp;
string parm;
sp = strstrofs(cmdstring, " ", 0);
if(sp < 0)
{
parm = "";
}
else
{
parm = substring(cmdstring, sp + 1, -1);
cmdstring = substring(cmdstring, 0, sp);
}
if(!bot_cmds_initialized)
bot_commands_init();
for(i=1;i<BOT_CMD_COUNTER;++i)
{
if(bot_cmd_string[i]!=cmdstring)
continue;
cmd_parm_type = bot_cmd_parm_type[i];
if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
{
print("ERROR: A parameter is required for this command\n");
return 0;
}
// Load command into queue
bot_cmd.bot_cmd_type = i;
// Attach parameter
switch(cmd_parm_type)
{
case BOT_CMD_PARAMETER_FLOAT:
bot_cmd.bot_cmd_parm_float = stof(parm);
break;
case BOT_CMD_PARAMETER_STRING:
if(bot_cmd.bot_cmd_parm_string)
strunzone(bot_cmd.bot_cmd_parm_string);
bot_cmd.bot_cmd_parm_string = strzone(parm);
break;
case BOT_CMD_PARAMETER_VECTOR:
bot_cmd.bot_cmd_parm_vector = stov(parm);
break;
default:
break;
}
return 1;
}
print("ERROR: No such command '", cmdstring, "'\n");
return 0;
}
void bot_cmdhelp(string scmd)
{
local float i, ntype;
local string stype;
if(!bot_cmds_initialized)
bot_commands_init();
for(i=1;i<BOT_CMD_COUNTER;++i)
{
if(bot_cmd_string[i]!=scmd)
continue;
ntype = bot_cmd_parm_type[i];
switch(ntype)
{
case BOT_CMD_PARAMETER_FLOAT:
stype = "float number";
break;
case BOT_CMD_PARAMETER_STRING:
stype = "string";
break;
case BOT_CMD_PARAMETER_VECTOR:
stype = "vector";
break;
default:
stype = "none";
break;
}
print(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
print("Description: ");
switch(i)
{
case BOT_CMD_PAUSE:
print("Stops the bot completely. Any command other than 'continue' will be ignored.");
break;
case BOT_CMD_CONTINUE:
print("Disable paused status");
break;
case BOT_CMD_WAIT:
print("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
break;
case BOT_CMD_WAIT_UNTIL:
print("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
break;
case BOT_CMD_BARRIER:
print("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
break;
case BOT_CMD_TURN:
print("Look to the right or left N degrees. For turning to the left use positive numbers.");
break;
case BOT_CMD_MOVETO:
print("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
break;
case BOT_CMD_MOVETOTARGET:
print("Walk to the specific target on the map");
break;
case BOT_CMD_RESETGOAL:
print("Resets the goal stack");
break;
case BOT_CMD_CC:
print("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
break;
case BOT_CMD_IF:
print("Perform simple conditional execution.\n");
print("Syntax: \n");
print(" sv_cmd .. if \"condition\"\n");
print(" sv_cmd .. <instruction if true>\n");
print(" sv_cmd .. <instruction if true>\n");
print(" sv_cmd .. else\n");
print(" sv_cmd .. <instruction if false>\n");
print(" sv_cmd .. <instruction if false>\n");
print(" sv_cmd .. fi\n");
print("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
print(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
print("Fields: health, speed, flagcarrier\n");
print("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
break;
case BOT_CMD_RESETAIM:
print("Points the aim to the coordinates x,y 0,0");
break;
case BOT_CMD_AIM:
print("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
print("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
print("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
print(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
break;
case BOT_CMD_AIMTARGET:
print("Points the aim to given target");
break;
case BOT_CMD_PRESSKEY:
print("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
print("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
print("Note: The script will not return the control to the bot ai until all keys are released");
break;
case BOT_CMD_RELEASEKEY:
print("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
break;
case BOT_CMD_SOUND:
print("play sound file at bot location");
break;
default:
print("This command has no description yet.");
break;
}
print("\n");
}
}
void bot_list_commands()
{
local float i;
local string ptype;
if(!bot_cmds_initialized)
bot_commands_init();
print("List of all available commands:\n");
print(" Command\t\t\t\tParameter Type\n");
for(i=1;i<BOT_CMD_COUNTER;++i)
{
switch(bot_cmd_parm_type[i])
{
case BOT_CMD_PARAMETER_FLOAT:
ptype = "float number";
break;
case BOT_CMD_PARAMETER_STRING:
ptype = "string";
break;
case BOT_CMD_PARAMETER_VECTOR:
ptype = "vector";
break;
default:
ptype = "none";
break;
}
print(strcat(" ",bot_cmd_string[i],"\t\t\t\t<",ptype,"> \n"));
}
}
// Commands code
.float bot_exec_status;
#define BOT_EXEC_STATUS_IDLE 0
#define BOT_EXEC_STATUS_PAUSED 1
#define BOT_EXEC_STATUS_WAITING 2
#define CMD_STATUS_EXECUTING 0
#define CMD_STATUS_FINISHED 1
#define CMD_STATUS_ERROR 2
void SV_ParseClientCommand(string s);
float bot_cmd_cc()
{
SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
return CMD_STATUS_FINISHED;
}
float bot_cmd_impulse()
{
self.impulse = bot_cmd.bot_cmd_parm_float;
return CMD_STATUS_FINISHED;
}
float bot_cmd_continue()
{
self.bot_exec_status &~= BOT_EXEC_STATUS_PAUSED;
return CMD_STATUS_FINISHED;
}
.float bot_cmd_wait_time;
float bot_cmd_wait()
{
if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
{
if(time>=self.bot_cmd_wait_time)
{
self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
return CMD_STATUS_FINISHED;
}
else
return CMD_STATUS_EXECUTING;
}
self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
return CMD_STATUS_EXECUTING;
}
float bot_cmd_wait_until()
{
if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
{
self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
return CMD_STATUS_EXECUTING;
}
self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
return CMD_STATUS_FINISHED;
}
float bot_cmd_barrier()
{
entity cl;
// 0 = no barrier, 1 = waiting, 2 = waiting finished
if(self.bot_barrier == 0) // initialization
{
self.bot_barrier = 1;
//self.colormod = '4 4 0';
}
if(self.bot_barrier == 1) // find other bots
{
FOR_EACH_CLIENT(cl) if(cl.isbot)
{
if(cl.bot_cmdqueuebuf_allocated)
if(cl.bot_barrier != 1)
return CMD_STATUS_EXECUTING; // not all are at the barrier yet
}
// all bots hit the barrier!
FOR_EACH_CLIENT(cl) if(cl.isbot)
{
cl.bot_barrier = 2; // acknowledge barrier
}
bot_barriertime = time;
}
// if we get here, the barrier is finished
// so end it...
self.bot_barrier = 0;
//self.colormod = '0 0 0';
return CMD_STATUS_FINISHED;
}
float bot_cmd_turn()
{
self.v_angle_y = self.v_angle_y + bot_cmd.bot_cmd_parm_float;
self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
return CMD_STATUS_FINISHED;
}
float bot_cmd_select_weapon()
{
local float id;
id = bot_cmd.bot_cmd_parm_float;
if(id < WEP_FIRST || id > WEP_LAST)
return CMD_STATUS_ERROR;
if(client_hasweapon(self, id, TRUE, FALSE))
self.switchweapon = id;
else
return CMD_STATUS_ERROR;
return CMD_STATUS_FINISHED;
}
.float bot_cmd_condition_status;
#define CMD_CONDITION_NONE 0
#define CMD_CONDITION_TRUE 1
#define CMD_CONDITION_FALSE 2
#define CMD_CONDITION_TRUE_BLOCK 4
#define CMD_CONDITION_FALSE_BLOCK 8
float bot_cmd_eval(string expr)
{
// Search for numbers
if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
{
return stof(expr);
}
// Search for cvars
if(substring(expr, 0, 5)=="cvar.")
{
return cvar(substring(expr, 5, strlen(expr)));
}
// Search for fields
switch(expr)
{
case "health":
return self.health;
case "speed":
return vlen(self.velocity);
case "flagcarrier":
return ((self.flagcarried!=world));
}
print(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
return 0;
}
float bot_cmd_if()
{
local string expr, val_a, val_b;
local float cmpofs;
if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
{
// Only one "if" block is allowed at time
print("ERROR: Only one conditional block can be processed at time");
bot_clearqueue(self);
return CMD_STATUS_ERROR;
}
self.bot_cmd_condition_status |= CMD_CONDITION_TRUE_BLOCK;
// search for operators
expr = bot_cmd.bot_cmd_parm_string;
cmpofs = strstrofs(expr,"=",0);
if(cmpofs>0)
{
val_a = substring(expr,0,cmpofs);
val_b = substring(expr,cmpofs+1,strlen(expr));
if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
else
self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
return CMD_STATUS_FINISHED;
}
cmpofs = strstrofs(expr,">",0);
if(cmpofs>0)
{
val_a = substring(expr,0,cmpofs);
val_b = substring(expr,cmpofs+1,strlen(expr));
if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
else
self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
return CMD_STATUS_FINISHED;
}
cmpofs = strstrofs(expr,"<",0);
if(cmpofs>0)
{
val_a = substring(expr,0,cmpofs);
val_b = substring(expr,cmpofs+1,strlen(expr));
if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
else
self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
return CMD_STATUS_FINISHED;
}
if(bot_cmd_eval(expr))
self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
else
self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
return CMD_STATUS_FINISHED;
}
float bot_cmd_else()
{
self.bot_cmd_condition_status &~= CMD_CONDITION_TRUE_BLOCK;
self.bot_cmd_condition_status |= CMD_CONDITION_FALSE_BLOCK;
return CMD_STATUS_FINISHED;
}
float bot_cmd_fi()
{
self.bot_cmd_condition_status = CMD_CONDITION_NONE;
return CMD_STATUS_FINISHED;
}
float bot_cmd_resetaim()
{
self.v_angle = '0 0 0';
return CMD_STATUS_FINISHED;
}
.float bot_cmd_aim_begintime;
.float bot_cmd_aim_endtime;
.vector bot_cmd_aim_begin;
.vector bot_cmd_aim_end;
float bot_cmd_aim()
{
// Current direction
if(self.bot_cmd_aim_endtime)
{
local float progress;
progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
if(time>=self.bot_cmd_aim_endtime)
{
self.bot_cmd_aim_endtime = 0;
return CMD_STATUS_FINISHED;
}
else
return CMD_STATUS_EXECUTING;
}
// New aiming direction
local string parms;
local float tokens, step;
parms = bot_cmd.bot_cmd_parm_string;
tokens = tokenizebyseparator(parms, " ");
if(tokens==2)
{
self.v_angle_x -= stof(argv(1));
self.v_angle_y += stof(argv(0));
return CMD_STATUS_FINISHED;
}
if(tokens<2||tokens>3)
return CMD_STATUS_ERROR;
step = stof(argv(2));
self.bot_cmd_aim_begin = self.v_angle;
self.bot_cmd_aim_end_x = self.v_angle_x - stof(argv(1));
self.bot_cmd_aim_end_y = self.v_angle_y + stof(argv(0));
self.bot_cmd_aim_end_z = 0;
self.bot_cmd_aim_begintime = time;
self.bot_cmd_aim_endtime = time + step;
return CMD_STATUS_EXECUTING;
}
float bot_cmd_aimtarget()
{
if(self.bot_cmd_aim_endtime)
{
return bot_cmd_aim();
}
local entity e;
local string parms;
local vector v;
local float tokens, step;
parms = bot_cmd.bot_cmd_parm_string;
tokens = tokenizebyseparator(parms, " ");
e = bot_getplace(argv(0));
if(!e)
return CMD_STATUS_ERROR;
v = e.origin + (e.mins + e.maxs) * 0.5;
if(tokens==1)
{
self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
self.v_angle_x = -self.v_angle_x;
return CMD_STATUS_FINISHED;
}
if(tokens<1||tokens>2)
return CMD_STATUS_ERROR;
step = stof(argv(1));
self.bot_cmd_aim_begin = self.v_angle;
self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
self.bot_cmd_aim_end_x = -self.bot_cmd_aim_end_x;
self.bot_cmd_aim_begintime = time;
self.bot_cmd_aim_endtime = time + step;
return CMD_STATUS_EXECUTING;
}
.float bot_cmd_keys;
#define BOT_CMD_KEY_NONE 0
#define BOT_CMD_KEY_FORWARD 1
#define BOT_CMD_KEY_BACKWARD 2
#define BOT_CMD_KEY_RIGHT 4
#define BOT_CMD_KEY_LEFT 8
#define BOT_CMD_KEY_JUMP 16
#define BOT_CMD_KEY_ATTACK1 32
#define BOT_CMD_KEY_ATTACK2 64
#define BOT_CMD_KEY_USE 128
#define BOT_CMD_KEY_HOOK 256
#define BOT_CMD_KEY_CROUCH 512
#define BOT_CMD_KEY_CHAT 1024
float bot_presskeys()
{
self.movement = '0 0 0';
if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
return FALSE;
if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
self.movement_x = cvar("sv_maxspeed");
else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
self.movement_x = -cvar("sv_maxspeed");
if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
self.movement_y = cvar("sv_maxspeed");
else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
self.movement_y = -cvar("sv_maxspeed");
if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
self.BUTTON_JUMP = TRUE;
if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
self.BUTTON_CROUCH = TRUE;
if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
self.BUTTON_ATCK = TRUE;
if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
self.BUTTON_ATCK2 = TRUE;
if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
self.BUTTON_USE = TRUE;
if(self.bot_cmd_keys & BOT_CMD_KEY_HOOK)
self.BUTTON_HOOK = TRUE;
if(self.bot_cmd_keys & BOT_CMD_KEY_CHAT)
self.BUTTON_CHAT = TRUE;
return TRUE;
}
float bot_cmd_keypress_handler(string key, float enabled)
{
switch(key)
{
case "all":
if(enabled)
self.bot_cmd_keys = power2of(20) - 1; // >:)
else
self.bot_cmd_keys = BOT_CMD_KEY_NONE;
case "forward":
if(enabled)
{
self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
}
else
self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
break;
case "backward":
if(enabled)
{
self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
}
else
self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
break;
case "left":
if(enabled)
{
self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
}
else
self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
break;
case "right":
if(enabled)
{
self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
}
else
self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
break;
case "jump":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_JUMP;
break;
case "crouch":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_CROUCH;
break;
case "attack1":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK1;
break;
case "attack2":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK2;
break;
case "use":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_USE;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_USE;
break;
case "hook":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_HOOK;
break;
case "chat":
if(enabled)
self.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
else
self.bot_cmd_keys &~= BOT_CMD_KEY_CHAT;
break;
default:
break;
}
return CMD_STATUS_FINISHED;
}
float bot_cmd_presskey()
{
local string key;
key = bot_cmd.bot_cmd_parm_string;
bot_cmd_keypress_handler(key,TRUE);
return CMD_STATUS_FINISHED;
}
float bot_cmd_releasekey()
{
local string key;
key = bot_cmd.bot_cmd_parm_string;
return bot_cmd_keypress_handler(key,FALSE);
}
float bot_cmd_pause()
{
self.button1 = 0;
self.button8 = 0;
self.BUTTON_USE = 0;
self.BUTTON_ATCK = 0;
self.BUTTON_JUMP = 0;
self.BUTTON_HOOK = 0;
self.BUTTON_CHAT = 0;
self.BUTTON_ATCK2 = 0;
self.BUTTON_CROUCH = 0;
self.movement = '0 0 0';
self.bot_cmd_keys = BOT_CMD_KEY_NONE;
self.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
return CMD_STATUS_FINISHED;
}
float bot_cmd_moveto()
{
return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
}
float bot_cmd_movetotarget()
{
entity e;
e = bot_getplace(bot_cmd.bot_cmd_parm_string);
if(!e)
return CMD_STATUS_ERROR;
return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
}
float bot_cmd_resetgoal()
{
return self.cmd_resetgoal();
}
float bot_cmd_sound()
{
string f;
f = bot_cmd.bot_cmd_parm_string;
precache_sound(f);
sound(self, CHAN_WEAPON2, f, VOL_BASE, ATTN_MIN);
return CMD_STATUS_FINISHED;
}
//
void bot_command_executed(float rm)
{
entity cmd;
cmd = bot_cmd;
if(rm)
bot_dequeuecommand(self, self.bot_cmd_execution_index);
self.bot_cmd_execution_index++;
}
void bot_setcurrentcommand()
{
bot_cmd = world;
if(!self.bot_cmd_current)
{
self.bot_cmd_current = spawn();
self.bot_cmd_current.classname = "bot_cmd";
self.bot_cmd_current.is_bot_cmd = 1;
}
bot_cmd = self.bot_cmd_current;
if(bot_cmd.bot_cmd_index != self.bot_cmd_execution_index || self.bot_cmd_execution_index == 0)
{
if(bot_havecommand(self, self.bot_cmd_execution_index))
{
string cmdstring;
cmdstring = bot_readcommand(self, self.bot_cmd_execution_index);
if(bot_decodecommand(cmdstring))
{
bot_cmd.owner = self;
bot_cmd.bot_cmd_index = self.bot_cmd_execution_index;
}
else
{
// Invalid command, remove from queue
bot_cmd = world;
bot_dequeuecommand(self, self.bot_cmd_execution_index);
self.bot_cmd_execution_index++;
}
}
else
bot_cmd = world;
}
}
void bot_resetqueues()
{
entity cl;
float i;
FOR_EACH_CLIENT(cl) if(cl.isbot)
{
if(cl.bot_cmdqueuebuf_allocated)
bot_clearqueue(cl);
// also, cancel all barriers
cl.bot_barrier = 0;
for(i = 0; i < cl.bot_places_count; ++i)
{
strunzone(cl.(bot_placenames[i]));
cl.(bot_placenames[i]) = string_null;
}
cl.bot_places_count = 0;
}
bot_barriertime = time;
}
// Here we map commands to functions and deal with complex interactions between commands and execution states
// NOTE: Of course you need to include your commands here too :)
float bot_execute_commands_once()
{
local float status, ispressingkey;
if(self.deadflag!=DEAD_NO)
return 0;
// Find command
bot_setcurrentcommand();
// Ignore all commands except continue when the bot is paused
if(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
{
if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
{
bot_command_executed(TRUE);
print( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
}
return 1;
}
// Keep pressing keys raised by the "presskey" command
ispressingkey = !!bot_presskeys();
if(bot_cmd==world)
return ispressingkey;
// Handle conditions
if not(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE)
if(self.bot_cmd_condition_status & CMD_CONDITION_TRUE && self.bot_cmd_condition_status & CMD_CONDITION_FALSE_BLOCK)
{
bot_command_executed(TRUE);
return -1;
}
else if(self.bot_cmd_condition_status & CMD_CONDITION_FALSE && self.bot_cmd_condition_status & CMD_CONDITION_TRUE_BLOCK)
{
bot_command_executed(TRUE);
return -1;
}
// Map commands to functions
switch(bot_cmd.bot_cmd_type)
{
case BOT_CMD_NULL:
return ispressingkey;
//break;
case BOT_CMD_PAUSE:
status = bot_cmd_pause();
break;
case BOT_CMD_CONTINUE:
status = bot_cmd_continue();
break;
case BOT_CMD_WAIT:
status = bot_cmd_wait();
break;
case BOT_CMD_WAIT_UNTIL:
status = bot_cmd_wait_until();
break;
case BOT_CMD_TURN:
status = bot_cmd_turn();
break;
case BOT_CMD_MOVETO:
status = bot_cmd_moveto();
break;
case BOT_CMD_MOVETOTARGET:
status = bot_cmd_movetotarget();
break;
case BOT_CMD_RESETGOAL:
status = bot_cmd_resetgoal();
break;
case BOT_CMD_CC:
status = bot_cmd_cc();
break;
case BOT_CMD_IF:
status = bot_cmd_if();
break;
case BOT_CMD_ELSE:
status = bot_cmd_else();
break;
case BOT_CMD_FI:
status = bot_cmd_fi();
break;
case BOT_CMD_RESETAIM:
status = bot_cmd_resetaim();
break;
case BOT_CMD_AIM:
status = bot_cmd_aim();
break;
case BOT_CMD_AIMTARGET:
status = bot_cmd_aimtarget();
break;
case BOT_CMD_PRESSKEY:
status = bot_cmd_presskey();
break;
case BOT_CMD_RELEASEKEY:
status = bot_cmd_releasekey();
break;
case BOT_CMD_SELECTWEAPON:
status = bot_cmd_select_weapon();
break;
case BOT_CMD_IMPULSE:
status = bot_cmd_impulse();
break;
case BOT_CMD_BARRIER:
status = bot_cmd_barrier();
break;
case BOT_CMD_CONSOLE:
localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
status = CMD_STATUS_FINISHED;
break;
case BOT_CMD_SOUND:
status = bot_cmd_sound();
break;
default:
print(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
return 0;
}
if (status==CMD_STATUS_ERROR)
print(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
// Move execution pointer
if(status==CMD_STATUS_EXECUTING)
{
return 1;
}
else
{
if(cvar("g_debug_bot_commands"))
{
local string parms;
switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
{
case BOT_CMD_PARAMETER_FLOAT:
parms = ftos(bot_cmd.bot_cmd_parm_float);
break;
case BOT_CMD_PARAMETER_STRING:
parms = bot_cmd.bot_cmd_parm_string;
break;
case BOT_CMD_PARAMETER_VECTOR:
parms = vtos(bot_cmd.bot_cmd_parm_vector);
break;
default:
parms = "";
break;
}
clientcommand(self,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
}
bot_command_executed(TRUE);
}
if(status == CMD_STATUS_FINISHED)
return -1;
return CMD_STATUS_ERROR;
}
// This function should be (the only) called directly from the bot ai loop
float bot_execute_commands()
{
float f;
do
{
f = bot_execute_commands_once();
}
while(f < 0);
return f;
}
|