1 // NOTE: New commands should be added here. Do not forget to update BOT_CMD_COUNTER
3 #define BOT_CMD_PAUSE 1
4 #define BOT_CMD_CONTINUE 2
7 #define BOT_CMD_MOVETO 5 // Not implemented yet
8 #define BOT_CMD_RESETGOAL 6 // Not implemented yet
11 #define BOT_CMD_ELSE 9
13 #define BOT_CMD_RESETAIM 11
14 #define BOT_CMD_AIM 12
15 #define BOT_CMD_PRESSKEY 13
16 #define BOT_CMD_RELEASEKEY 14
17 #define BOT_CMD_SELECTWEAPON 15
18 #define BOT_CMD_IMPULSE 16
19 #define BOT_CMD_WAIT_UNTIL 17
20 #define BOT_CMD_RESETQUEUE 18
21 #define BOT_CMD_MOVETOTARGET 19
22 #define BOT_CMD_AIMTARGET 20
23 #define BOT_CMD_WHILE 21 // TODO: Not implemented yet
24 #define BOT_CMD_WEND 22 // TODO: Not implemented yet
25 #define BOT_CMD_CHASE 23 // TODO: Not implemented yet
27 #define BOT_CMD_COUNTER 21 // Update this value if you add/remove a command
29 // NOTE: Following commands should be implemented on the bot ai
30 // If a new command should be handled by the target ai(s) please declare it here
31 .float(vector) cmd_moveto;
32 .float() cmd_resetgoal;
35 #define BOT_CMD_PARAMETER_NONE 0
36 #define BOT_CMD_PARAMETER_FLOAT 1
37 #define BOT_CMD_PARAMETER_STRING 2
38 #define BOT_CMD_PARAMETER_VECTOR 3
39 #define BOT_CMD_PARAMETER_FLOAT_RELATIVE_TO_TIME 4
41 float bot_cmds_initialized;
42 float bot_cmd_parm_type[BOT_CMD_COUNTER];
43 string bot_cmd_string[BOT_CMD_COUNTER];
46 entity bot_cmd; // Current command
48 .float is_bot_cmd; // Tells if the entity is a bot command
49 .float bot_cmd_index; // Position of the command in the queue
50 .float bot_cmd_type; // If of command (see the BOT_CMD_* defines)
51 .float bot_cmd_parm_float; // Field to store a float parameter
52 .string bot_cmd_parm_string; // Field to store a string parameter
53 .vector bot_cmd_parm_vector; // Field to store a vector parameter
54 .float bot_cmd_execution_counter; // How many times this command on the queue was executed
56 .float bot_cmd_execution_index; // Position in the queue of the command to be executed
57 .float bot_cmd_queue_index; // Position of the last command in the queue
59 .entity bot_cmd_next; // next command of the same bot (field on a command), or next command for this bot to execute (field on a bot)
60 .entity bot_cmd_newest; // last command of this bot (field on a bot, used for adding new commands)
62 // Initialize global commands list
63 // NOTE: New commands should be initialized here
64 void bot_commands_init()
66 bot_cmd_string[BOT_CMD_NULL] = "";
67 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
69 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
70 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
72 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
73 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
75 bot_cmd_string[BOT_CMD_WAIT] = "wait";
76 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
78 bot_cmd_string[BOT_CMD_TURN] = "turn";
79 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
81 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
82 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
84 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
85 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
87 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
88 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
90 bot_cmd_string[BOT_CMD_CC] = "cc";
91 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
93 bot_cmd_string[BOT_CMD_IF] = "if";
94 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
96 bot_cmd_string[BOT_CMD_ELSE] = "else";
97 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
99 bot_cmd_string[BOT_CMD_FI] = "fi";
100 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
102 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
103 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
105 bot_cmd_string[BOT_CMD_AIM] = "aim";
106 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
108 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
109 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
111 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
112 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
114 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
115 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
117 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
118 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
120 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
121 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
123 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
124 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT_RELATIVE_TO_TIME;
126 bot_cmd_string[BOT_CMD_RESETQUEUE] = "resetqueue";
127 bot_cmd_parm_type[BOT_CMD_RESETQUEUE] = BOT_CMD_PARAMETER_NONE;
129 bot_cmds_initialized = TRUE;
132 // Returns first bot with matching name
133 entity find_bot_by_name(string name)
137 bot = findchainflags(flags, FL_CLIENT);
140 if(clienttype(bot) == CLIENTTYPE_BOT)
141 if(bot.netname==name)
150 // Returns a bot by number on list
151 entity find_bot_by_number(float number)
156 bot = findchainflags(flags, FL_CLIENT);
159 if(clienttype(bot) == CLIENTTYPE_BOT)
170 void bot_clearqueue()
172 entity head, newhead;
174 head = findchainfloat(is_bot_cmd, TRUE);
179 newhead = head.chain;
187 self.bot_cmd_queue_index = 0;
188 self.bot_cmd_execution_index = 0;
191 entity bot_spawncmd(entity bot, float type)
195 bot.bot_cmd_queue_index++;
199 cmd.is_bot_cmd = TRUE;
200 cmd.bot_cmd_type = type;
201 cmd.bot_cmd_index = bot.bot_cmd_queue_index;
203 if(bot.bot_cmd_newest)
204 bot.bot_cmd_newest.bot_cmd_next = cmd;
205 bot.bot_cmd_newest = cmd;
210 void bot_debugcmd(entity cmd)
212 print(strcat("Owner: ",cmd.owner.netname, "\n"));
213 print(strcat("Cmd Type: ",ftos(cmd.bot_cmd_type), "\n"));
214 print(strcat("Cmd Index: ",ftos(cmd.bot_cmd_index), "\n"));
216 print(strcat("Param Float: ",ftos(cmd.bot_cmd_parm_float), "\n"));
217 print(strcat("Param String: ",cmd.bot_cmd_parm_string, "\n"));
218 print(strcat("Param Vector: ",vtos(cmd.bot_cmd_parm_vector), "\n"));
220 print(strcat("Bot queue index: ", ftos(cmd.owner.bot_cmd_queue_index), "\n"));
221 print(strcat("Bot execution index: ", ftos(cmd.owner.bot_cmd_execution_index), "\n\n"));
224 void bot_queuecommand(entity bot, string cmdstring, string parm)
227 local float cmd_parm_type, i;
232 if(!bot_cmds_initialized)
235 for(i=1;i<BOT_CMD_COUNTER;++i)
237 if(bot_cmd_string[i]!=cmdstring)
240 cmd_parm_type = bot_cmd_parm_type[i];
242 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
244 print("ERROR: A parameter is required for this command\n");
248 // Load command into queue
249 cmd = bot_spawncmd(bot, i);
252 switch(cmd_parm_type)
254 case BOT_CMD_PARAMETER_FLOAT:
255 cmd.bot_cmd_parm_float = stof(parm);
257 case BOT_CMD_PARAMETER_FLOAT_RELATIVE_TO_TIME:
258 cmd.bot_cmd_parm_float = stof(parm) + time;
260 case BOT_CMD_PARAMETER_STRING:
261 cmd.bot_cmd_parm_string = strzone(parm);
263 case BOT_CMD_PARAMETER_VECTOR:
264 cmd.bot_cmd_parm_vector = stov(parm);
271 print("ERROR: No such command\n");
274 void bot_cmdhelp(string scmd)
276 local float i, ntype;
279 if(!bot_cmds_initialized)
282 for(i=1;i<BOT_CMD_COUNTER;++i)
284 if(bot_cmd_string[i]!=scmd)
287 ntype = bot_cmd_parm_type[i];
291 case BOT_CMD_PARAMETER_FLOAT:
292 case BOT_CMD_PARAMETER_FLOAT_RELATIVE_TO_TIME:
293 stype = "float number";
295 case BOT_CMD_PARAMETER_STRING:
298 case BOT_CMD_PARAMETER_VECTOR:
306 print(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
308 print("Description: ");
312 print("Stops the bot completely. Any command other than 'continue' will be ignored.");
314 case BOT_CMD_CONTINUE:
315 print("Disable paused status");
318 print("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
320 case BOT_CMD_WAIT_UNTIL:
321 print("Pause command parsing and bot ai until time is N from NOW (when the command is added). Pressed key will remain pressed");
324 print("Look to the right or left N degrees. For turning to the left use positive numbers.");
327 print("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
329 case BOT_CMD_MOVETOTARGET:
330 print("Walk to the specific target on the map");
332 case BOT_CMD_RESETGOAL:
333 print("Resets the goal stack");
336 print("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
339 print("Perform simple conditional execution.\n");
341 print(" sv_cmd .. if \"condition\"\n");
342 print(" sv_cmd .. <instruction if true>\n");
343 print(" sv_cmd .. <instruction if true>\n");
344 print(" sv_cmd .. else\n");
345 print(" sv_cmd .. <instruction if false>\n");
346 print(" sv_cmd .. <instruction if false>\n");
347 print(" sv_cmd .. fi\n");
348 print("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
349 print(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
350 print("Fields: health, speed, flagcarrier\n");
351 print("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
353 case BOT_CMD_RESETAIM:
354 print("Points the aim to the coordinates x,y 0,0");
357 print("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
358 print("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
359 print("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
360 print(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
362 case BOT_CMD_AIMTARGET:
363 print("Points the aim to given target");
365 case BOT_CMD_PRESSKEY:
366 print("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
367 print("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
368 print("Note: The script will not return the control to the bot ai until all keys are released");
370 case BOT_CMD_RELEASEKEY:
371 print("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
374 print("This command has no description yet.");
381 void bot_list_commands()
386 if(!bot_cmds_initialized)
389 print("List of all available commands:\n");
390 print(" Command\t\t\t\tParameter Type\n");
392 for(i=1;i<BOT_CMD_COUNTER;++i)
394 switch(bot_cmd_parm_type[i])
396 case BOT_CMD_PARAMETER_FLOAT:
397 case BOT_CMD_PARAMETER_FLOAT_RELATIVE_TO_TIME:
398 ptype = "float number";
400 case BOT_CMD_PARAMETER_STRING:
403 case BOT_CMD_PARAMETER_VECTOR:
410 print(strcat(" ",bot_cmd_string[i],"\t\t\t\t<",ptype,"> \n"));
415 .float bot_exec_status;
417 #define BOT_EXEC_STATUS_IDLE 0
418 #define BOT_EXEC_STATUS_PAUSED 1
419 #define BOT_EXEC_STATUS_WAITING 2
421 #define CMD_STATUS_EXECUTING 0
422 #define CMD_STATUS_FINISHED 1
423 #define CMD_STATUS_ERROR 2
425 void SV_ParseClientCommand(string s);
428 SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
429 return CMD_STATUS_FINISHED;
432 float bot_cmd_impulse()
434 self.impulse = bot_cmd.bot_cmd_parm_float;
435 return CMD_STATUS_FINISHED;
438 float bot_cmd_continue()
440 self.bot_exec_status &~= BOT_EXEC_STATUS_PAUSED;
441 return CMD_STATUS_FINISHED;
444 .float bot_cmd_wait_time;
447 if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
449 if(time>=self.bot_cmd_wait_time)
451 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
452 return CMD_STATUS_FINISHED;
455 return CMD_STATUS_EXECUTING;
458 self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
459 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
460 return CMD_STATUS_EXECUTING;
463 float bot_cmd_wait_until()
465 if(time < bot_cmd.bot_cmd_parm_float)
467 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
468 return CMD_STATUS_EXECUTING;
470 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
471 return CMD_STATUS_FINISHED;
476 self.v_angle_y = self.v_angle_y + bot_cmd.bot_cmd_parm_float;
477 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
478 return CMD_STATUS_FINISHED;
481 float bot_cmd_select_weapon()
485 id = bot_cmd.bot_cmd_parm_float;
487 if(id < WEP_FIRST || id > WEP_LAST)
488 return CMD_STATUS_ERROR;
490 if(client_hasweapon(self, id, TRUE, FALSE))
491 self.switchweapon = id;
493 return CMD_STATUS_FINISHED;
496 .float bot_cmd_condition_status;
498 #define CMD_CONDITION_NONE 0
499 #define CMD_CONDITION_TRUE 1
500 #define CMD_CONDITION_FALSE 2
501 #define CMD_CONDITION_TRUE_BLOCK 4
502 #define CMD_CONDITION_FALSE_BLOCK 8
504 float bot_cmd_eval(string expr)
506 // Search for numbers
507 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
513 if(substring(expr, 0, 5)=="cvar.")
515 return cvar(substring(expr, 5, strlen(expr)));
524 return vlen(self.velocity);
526 return ((self.flagcarried!=world));
529 print(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
535 local string expr, val_a, val_b;
538 if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
540 // Only one "if" block is allowed at time
541 print("ERROR: Only one conditional block can be processed at time");
543 return CMD_STATUS_ERROR;
546 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE_BLOCK;
548 // search for operators
549 expr = bot_cmd.bot_cmd_parm_string;
551 cmpofs = strstrofs(expr,"=",0);
555 val_a = substring(expr,0,cmpofs);
556 val_b = substring(expr,cmpofs+1,strlen(expr));
558 if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
559 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
561 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
563 return CMD_STATUS_FINISHED;
566 cmpofs = strstrofs(expr,">",0);
570 val_a = substring(expr,0,cmpofs);
571 val_b = substring(expr,cmpofs+1,strlen(expr));
573 if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
574 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
576 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
578 return CMD_STATUS_FINISHED;
581 cmpofs = strstrofs(expr,"<",0);
585 val_a = substring(expr,0,cmpofs);
586 val_b = substring(expr,cmpofs+1,strlen(expr));
588 if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
589 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
591 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
593 return CMD_STATUS_FINISHED;
596 if(bot_cmd_eval(expr))
597 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
599 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
601 return CMD_STATUS_FINISHED;
606 self.bot_cmd_condition_status &~= CMD_CONDITION_TRUE_BLOCK;
607 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE_BLOCK;
608 return CMD_STATUS_FINISHED;
613 self.bot_cmd_condition_status = CMD_CONDITION_NONE;
614 return CMD_STATUS_FINISHED;
617 float bot_cmd_resetaim()
619 self.v_angle = '0 0 0';
620 return CMD_STATUS_FINISHED;
623 .float bot_cmd_aim_begintime;
624 .float bot_cmd_aim_endtime;
625 .vector bot_cmd_aim_begin;
626 .vector bot_cmd_aim_end;
631 if(self.bot_cmd_aim_endtime)
633 local float progress;
635 progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
636 self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
638 if(time>=self.bot_cmd_aim_endtime)
640 self.bot_cmd_aim_endtime = 0;
641 return CMD_STATUS_FINISHED;
644 return CMD_STATUS_EXECUTING;
647 // New aiming direction
649 local float tokens, step;
651 parms = bot_cmd.bot_cmd_parm_string;
653 tokens = tokenizebyseparator(parms, " ");
657 self.v_angle_x -= stof(argv(1));
658 self.v_angle_y += stof(argv(0));
659 return CMD_STATUS_FINISHED;
662 if(tokens<2||tokens>3)
663 return CMD_STATUS_ERROR;
665 step = stof(argv(2));
667 self.bot_cmd_aim_begin = self.v_angle;
669 self.bot_cmd_aim_end_x = self.v_angle_x - stof(argv(1));
670 self.bot_cmd_aim_end_y = self.v_angle_y + stof(argv(0));
671 self.bot_cmd_aim_end_z = 0;
673 self.bot_cmd_aim_begintime = time;
674 self.bot_cmd_aim_endtime = time + step;
676 return CMD_STATUS_EXECUTING;
679 float bot_cmd_aimtarget()
681 if(self.bot_cmd_aim_endtime)
683 return bot_cmd_aim();
689 local float tokens, step;
691 parms = bot_cmd.bot_cmd_parm_string;
693 tokens = tokenizebyseparator(parms, " ");
695 e = find(world, targetname, argv(0));
697 return CMD_STATUS_ERROR;
699 v = e.origin + (e.mins + e.maxs) * 0.5;
703 self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
704 return CMD_STATUS_FINISHED;
707 if(tokens<1||tokens>2)
708 return CMD_STATUS_ERROR;
710 step = stof(argv(1));
712 self.bot_cmd_aim_begin = self.v_angle;
713 self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
715 self.bot_cmd_aim_begintime = time;
716 self.bot_cmd_aim_endtime = time + step;
718 return CMD_STATUS_EXECUTING;
723 #define BOT_CMD_KEY_NONE 0
724 #define BOT_CMD_KEY_FORWARD 1
725 #define BOT_CMD_KEY_BACKWARD 2
726 #define BOT_CMD_KEY_RIGHT 4
727 #define BOT_CMD_KEY_LEFT 8
728 #define BOT_CMD_KEY_JUMP 16
729 #define BOT_CMD_KEY_ATTACK1 32
730 #define BOT_CMD_KEY_ATTACK2 64
731 #define BOT_CMD_KEY_USE 128
732 #define BOT_CMD_KEY_HOOK 256
733 #define BOT_CMD_KEY_CROUCH 512
735 float bot_presskeys()
737 self.movement = '0 0 0';
739 if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
742 if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
743 self.movement_x = cvar("sv_maxspeed");
744 else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
745 self.movement_x = -cvar("sv_maxspeed");
747 if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
748 self.movement_y = cvar("sv_maxspeed");
749 else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
750 self.movement_y = -cvar("sv_maxspeed");
752 if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
753 self.BUTTON_JUMP = TRUE;
755 if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
756 self.BUTTON_CROUCH = TRUE;
758 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
759 self.BUTTON_ATCK = TRUE;
761 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
762 self.BUTTON_ATCK2 = TRUE;
764 if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
765 self.BUTTON_USE = TRUE;
771 float bot_cmd_keypress_handler(string key, float enabled)
777 self.bot_cmd_keys = power2of(20) - 1; // >:)
779 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
783 self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
784 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
787 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
792 self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
793 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
796 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
801 self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
802 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
805 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
810 self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
811 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
814 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
818 self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
820 self.bot_cmd_keys &~= BOT_CMD_KEY_JUMP;
824 self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
826 self.bot_cmd_keys &~= BOT_CMD_KEY_CROUCH;
830 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
832 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK1;
836 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
838 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK2;
842 self.bot_cmd_keys |= BOT_CMD_KEY_USE;
844 self.bot_cmd_keys &~= BOT_CMD_KEY_USE;
850 return CMD_STATUS_FINISHED;
853 float bot_cmd_presskey()
857 key = bot_cmd.bot_cmd_parm_string;
859 bot_cmd_keypress_handler(key,TRUE);
861 return CMD_STATUS_FINISHED;
864 float bot_cmd_releasekey()
868 key = bot_cmd.bot_cmd_parm_string;
870 return bot_cmd_keypress_handler(key,FALSE);
873 float bot_cmd_pause()
878 self.BUTTON_ATCK = 0;
879 self.BUTTON_JUMP = 0;
880 self.BUTTON_HOOK = 0;
881 self.BUTTON_CHAT = 0;
882 self.BUTTON_ATCK2 = 0;
883 self.BUTTON_CROUCH = 0;
885 self.movement = '0 0 0';
886 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
888 self.bot_exec_status = self.bot_exec_status | BOT_EXEC_STATUS_PAUSED;
889 return CMD_STATUS_FINISHED;
892 float bot_cmd_moveto()
894 return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
897 float bot_cmd_movetotarget()
900 e = find(world, targetname, bot_cmd.bot_cmd_parm_string);
902 return CMD_STATUS_ERROR;
903 return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
906 float bot_cmd_resetgoal()
908 return self.cmd_resetgoal();
911 void bot_command_executed(float rm)
917 self.bot_cmd_execution_index++;
921 if(bot_cmd_parm_type[cmd.bot_cmd_type]==BOT_CMD_PARAMETER_STRING)
923 strunzone(cmd.bot_cmd_parm_string);
925 self.bot_cmd_next = cmd.bot_cmd_next;
926 if(cmd == self.bot_cmd_newest)
927 self.bot_cmd_newest = world;
932 cmd.bot_cmd_execution_counter++;
935 void bot_setcurrentcommand()
941 if(!self.bot_cmd_newest) // no command in the queue?
944 if(self.bot_cmd_execution_index==0)
945 self.bot_cmd_execution_index=1;
947 cmd = self.bot_cmd_next;
949 if(cmd.owner == self)
950 if(cmd.bot_cmd_index == self.bot_cmd_execution_index)
959 print("next bot cmd not set\n");
962 if(cmd && cmd.owner != self)
964 print("next bot cmd has wrong owner ", etos(cmd.owner), " for ", etos(self), "\n");
967 if(cmd && cmd.owner == self && cmd.bot_cmd_index != self.bot_cmd_execution_index)
969 print("next bot cmd has wrong index ", ftos(cmd.bot_cmd_execution_index), " for ", ftos(self.bot_cmd_execution_index), "\n");
973 for (cmd = findchainfloat(bot_cmd_index, self.bot_cmd_execution_index); cmd; cmd = cmd.chain)
978 self.bot_cmd_next = cmd;
979 //print(etos(self), " probably a jump...\n");
985 // This function should be (the only) called directly from the bot ai loop
986 // It maps commands to functions and deal with complex interactions between commands and execution states
987 // NOTE: Of course you need to include your commands here too :)
988 float bot_execute_commands()
990 local float status, ispressingkey;
995 if(self.deadflag!=DEAD_NO)
999 bot_setcurrentcommand();
1001 // Keep pressing keys raised by the "presskey" command
1002 ispressingkey = bot_presskeys();
1005 return ispressingkey;
1007 // Ignore all commands except continue when the bot is paused
1008 if(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1009 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1011 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1013 bot_command_executed(TRUE);
1014 print( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1019 // Handle conditions
1020 if not(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE)
1021 if(self.bot_cmd_condition_status & CMD_CONDITION_TRUE && self.bot_cmd_condition_status & CMD_CONDITION_FALSE_BLOCK)
1023 bot_command_executed(TRUE);
1026 else if(self.bot_cmd_condition_status & CMD_CONDITION_FALSE && self.bot_cmd_condition_status & CMD_CONDITION_TRUE_BLOCK)
1028 bot_command_executed(TRUE);
1032 // Map commands to functions
1033 switch(bot_cmd.bot_cmd_type)
1036 return ispressingkey;
1039 status = bot_cmd_pause();
1041 case BOT_CMD_CONTINUE:
1042 status = bot_cmd_continue();
1045 status = bot_cmd_wait();
1047 case BOT_CMD_WAIT_UNTIL:
1048 status = bot_cmd_wait_until();
1051 status = bot_cmd_turn();
1053 case BOT_CMD_MOVETO:
1054 status = bot_cmd_moveto();
1056 case BOT_CMD_MOVETOTARGET:
1057 status = bot_cmd_movetotarget();
1059 case BOT_CMD_RESETGOAL:
1060 status = bot_cmd_resetgoal();
1063 status = bot_cmd_cc();
1066 status = bot_cmd_if();
1069 status = bot_cmd_else();
1072 status = bot_cmd_fi();
1074 case BOT_CMD_RESETAIM:
1075 status = bot_cmd_resetaim();
1078 status = bot_cmd_aim();
1080 case BOT_CMD_AIMTARGET:
1081 status = bot_cmd_aimtarget();
1083 case BOT_CMD_PRESSKEY:
1084 status = bot_cmd_presskey();
1086 case BOT_CMD_RELEASEKEY:
1087 status = bot_cmd_releasekey();
1089 case BOT_CMD_SELECTWEAPON:
1090 status = bot_cmd_select_weapon();
1092 case BOT_CMD_IMPULSE:
1093 status = bot_cmd_impulse();
1095 case BOT_CMD_RESETQUEUE:
1097 status = CMD_STATUS_FINISHED;
1100 print(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1104 if (status==CMD_STATUS_ERROR)
1105 print(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1107 // Move execution pointer
1108 if(status==CMD_STATUS_EXECUTING)
1114 if(cvar("g_debug_bot_commands"))
1118 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1120 case BOT_CMD_PARAMETER_FLOAT:
1121 case BOT_CMD_PARAMETER_FLOAT_RELATIVE_TO_TIME:
1122 parms = ftos(bot_cmd.bot_cmd_parm_float);
1124 case BOT_CMD_PARAMETER_STRING:
1125 parms = bot_cmd.bot_cmd_parm_string;
1127 case BOT_CMD_PARAMETER_VECTOR:
1128 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1134 clientcommand(self,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1137 bot_command_executed(TRUE);
1140 } while(status==CMD_STATUS_FINISHED);