1 #include "scripting.qh"
7 .float bot_cmdqueuebuf_allocated;
8 .float bot_cmdqueuebuf;
9 .float bot_cmdqueuebuf_start;
10 .float bot_cmdqueuebuf_end;
12 void bot_clearqueue(entity bot)
14 if(!bot.bot_cmdqueuebuf_allocated)
16 buf_del(bot.bot_cmdqueuebuf);
17 bot.bot_cmdqueuebuf_allocated = false;
18 LOG_TRACE("bot ", bot.netname, " queue cleared\n");
21 void bot_queuecommand(entity bot, string cmdstring)
23 if(!bot.bot_cmdqueuebuf_allocated)
25 bot.bot_cmdqueuebuf = buf_create();
26 bot.bot_cmdqueuebuf_allocated = true;
27 bot.bot_cmdqueuebuf_start = 0;
28 bot.bot_cmdqueuebuf_end = 0;
31 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
33 // if the command was a "sound" command, precache the sound NOW
34 // this prevents lagging!
40 sp = strstrofs(cmdstring, " ", 0);
43 parm = substring(cmdstring, sp + 1, -1);
44 cmdstr = substring(cmdstring, 0, sp);
50 sp = strstrofs(parm, " ", 0);
53 parm = substring(parm, sp + 1, -1);
60 bot.bot_cmdqueuebuf_end += 1;
63 void bot_dequeuecommand(entity bot, float idx)
65 if(!bot.bot_cmdqueuebuf_allocated)
66 error("dequeuecommand but no queue allocated");
67 if(idx < bot.bot_cmdqueuebuf_start)
68 error("dequeueing a command in the past");
69 if(idx >= bot.bot_cmdqueuebuf_end)
70 error("dequeueing a command in the future");
71 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
72 if(idx == bot.bot_cmdqueuebuf_start)
73 bot.bot_cmdqueuebuf_start += 1;
74 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
78 string bot_readcommand(entity bot, float idx)
80 if(!bot.bot_cmdqueuebuf_allocated)
81 error("readcommand but no queue allocated");
82 if(idx < bot.bot_cmdqueuebuf_start)
83 error("reading a command in the past");
84 if(idx >= bot.bot_cmdqueuebuf_end)
85 error("reading a command in the future");
86 return bufstr_get(bot.bot_cmdqueuebuf, idx);
89 float bot_havecommand(entity bot, float idx)
91 if(!bot.bot_cmdqueuebuf_allocated)
93 if(idx < bot.bot_cmdqueuebuf_start)
95 if(idx >= bot.bot_cmdqueuebuf_end)
100 const int MAX_BOT_PLACES = 4;
101 .float bot_places_count;
102 .entity bot_places[MAX_BOT_PLACES];
103 .string bot_placenames[MAX_BOT_PLACES];
104 entity bot_getplace(string placename)
107 if(substring(placename, 0, 1) == "@")
110 placename = substring(placename, 1, -1);
112 for(i = 0; i < self.bot_places_count; ++i)
113 if(self.(bot_placenames[i]) == placename)
114 return self.(bot_places[i]);
115 // now: i == self.bot_places_count
116 s = s2 = cvar_string(placename);
117 p = strstrofs(s2, " ", 0);
120 s = substring(s2, 0, p);
121 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
122 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
123 //print("places: ", placename, " := ", cvar_string(placename), "\n");
125 e = find(world, targetname, s);
127 LOG_INFO("invalid place ", s, "\n");
128 if(i < MAX_BOT_PLACES)
130 self.(bot_placenames[i]) = strzone(placename);
131 self.(bot_places[i]) = e;
132 self.bot_places_count += 1;
138 e = find(world, targetname, placename);
140 LOG_INFO("invalid place ", placename, "\n");
146 // Initialize global commands list
147 // NOTE: New commands should be initialized here
148 void bot_commands_init()
150 bot_cmd_string[BOT_CMD_NULL] = "";
151 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
153 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
154 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
156 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
157 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
159 bot_cmd_string[BOT_CMD_WAIT] = "wait";
160 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
162 bot_cmd_string[BOT_CMD_TURN] = "turn";
163 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
165 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
166 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
168 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
169 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
171 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
172 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
174 bot_cmd_string[BOT_CMD_CC] = "cc";
175 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
177 bot_cmd_string[BOT_CMD_IF] = "if";
178 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
180 bot_cmd_string[BOT_CMD_ELSE] = "else";
181 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
183 bot_cmd_string[BOT_CMD_FI] = "fi";
184 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
186 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
187 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
189 bot_cmd_string[BOT_CMD_AIM] = "aim";
190 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
192 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
193 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
195 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
196 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
198 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
199 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
201 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
202 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
204 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
205 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
207 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
208 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
210 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
211 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
213 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
214 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
216 bot_cmd_string[BOT_CMD_SOUND] = "sound";
217 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
219 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
220 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
222 bot_cmds_initialized = true;
225 // Returns first bot with matching name
226 entity find_bot_by_name(string name)
230 bot = findchainflags(flags, FL_CLIENT);
233 if(IS_BOT_CLIENT(bot))
234 if(bot.netname==name)
243 // Returns a bot by number on list
244 entity find_bot_by_number(float number)
252 bot = findchainflags(flags, FL_CLIENT);
255 if(IS_BOT_CLIENT(bot))
266 float bot_decodecommand(string cmdstring)
272 sp = strstrofs(cmdstring, " ", 0);
279 parm = substring(cmdstring, sp + 1, -1);
280 cmdstring = substring(cmdstring, 0, sp);
283 if(!bot_cmds_initialized)
287 for(i=1;i<BOT_CMD_COUNTER;++i)
289 if(bot_cmd_string[i]!=cmdstring)
292 cmd_parm_type = bot_cmd_parm_type[i];
294 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
296 LOG_INFO("ERROR: A parameter is required for this command\n");
300 // Load command into queue
301 bot_cmd.bot_cmd_type = i;
304 switch(cmd_parm_type)
306 case BOT_CMD_PARAMETER_FLOAT:
307 bot_cmd.bot_cmd_parm_float = stof(parm);
309 case BOT_CMD_PARAMETER_STRING:
310 if(bot_cmd.bot_cmd_parm_string)
311 strunzone(bot_cmd.bot_cmd_parm_string);
312 bot_cmd.bot_cmd_parm_string = strzone(parm);
314 case BOT_CMD_PARAMETER_VECTOR:
315 bot_cmd.bot_cmd_parm_vector = stov(parm);
322 LOG_INFO("ERROR: No such command '", cmdstring, "'\n");
326 void bot_cmdhelp(string scmd)
331 if(!bot_cmds_initialized)
334 for(i=1;i<BOT_CMD_COUNTER;++i)
336 if(bot_cmd_string[i]!=scmd)
339 ntype = bot_cmd_parm_type[i];
343 case BOT_CMD_PARAMETER_FLOAT:
344 stype = "float number";
346 case BOT_CMD_PARAMETER_STRING:
349 case BOT_CMD_PARAMETER_VECTOR:
357 LOG_INFO(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
359 LOG_INFO("Description: ");
363 LOG_INFO("Stops the bot completely. Any command other than 'continue' will be ignored.");
365 case BOT_CMD_CONTINUE:
366 LOG_INFO("Disable paused status");
369 LOG_INFO("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
371 case BOT_CMD_WAIT_UNTIL:
372 LOG_INFO("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
374 case BOT_CMD_BARRIER:
375 LOG_INFO("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
378 LOG_INFO("Look to the right or left N degrees. For turning to the left use positive numbers.");
381 LOG_INFO("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
383 case BOT_CMD_MOVETOTARGET:
384 LOG_INFO("Walk to the specific target on the map");
386 case BOT_CMD_RESETGOAL:
387 LOG_INFO("Resets the goal stack");
390 LOG_INFO("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
393 LOG_INFO("Perform simple conditional execution.\n");
394 LOG_INFO("Syntax: \n");
395 LOG_INFO(" sv_cmd .. if \"condition\"\n");
396 LOG_INFO(" sv_cmd .. <instruction if true>\n");
397 LOG_INFO(" sv_cmd .. <instruction if true>\n");
398 LOG_INFO(" sv_cmd .. else\n");
399 LOG_INFO(" sv_cmd .. <instruction if false>\n");
400 LOG_INFO(" sv_cmd .. <instruction if false>\n");
401 LOG_INFO(" sv_cmd .. fi\n");
402 LOG_INFO("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
403 LOG_INFO(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
404 LOG_INFO("Fields: health, speed, flagcarrier\n");
405 LOG_INFO("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
407 case BOT_CMD_RESETAIM:
408 LOG_INFO("Points the aim to the coordinates x,y 0,0");
411 LOG_INFO("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
412 LOG_INFO("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
413 LOG_INFO("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
414 LOG_INFO(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
416 case BOT_CMD_AIMTARGET:
417 LOG_INFO("Points the aim to given target");
419 case BOT_CMD_PRESSKEY:
420 LOG_INFO("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
421 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
422 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
424 case BOT_CMD_RELEASEKEY:
425 LOG_INFO("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
428 LOG_INFO("play sound file at bot location");
430 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
431 LOG_INFO("verify the state of the weapon entity");
434 LOG_INFO("This command has no description yet.");
441 void bot_list_commands()
446 if(!bot_cmds_initialized)
449 LOG_INFO("List of all available commands:\n");
450 LOG_INFO(" Command - Parameter Type\n");
452 for(i=1;i<BOT_CMD_COUNTER;++i)
454 switch(bot_cmd_parm_type[i])
456 case BOT_CMD_PARAMETER_FLOAT:
457 ptype = "float number";
459 case BOT_CMD_PARAMETER_STRING:
462 case BOT_CMD_PARAMETER_VECTOR:
469 LOG_INFO(strcat(" ",bot_cmd_string[i]," - <",ptype,"> \n"));
474 .int bot_exec_status;
476 void SV_ParseClientCommand(string s);
479 SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
480 return CMD_STATUS_FINISHED;
483 float bot_cmd_impulse()
485 self.impulse = bot_cmd.bot_cmd_parm_float;
486 return CMD_STATUS_FINISHED;
489 float bot_cmd_continue()
491 self.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
492 return CMD_STATUS_FINISHED;
495 .float bot_cmd_wait_time;
498 if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
500 if(time>=self.bot_cmd_wait_time)
502 self.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
503 return CMD_STATUS_FINISHED;
506 return CMD_STATUS_EXECUTING;
509 self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
510 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
511 return CMD_STATUS_EXECUTING;
514 float bot_cmd_wait_until()
516 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
518 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
519 return CMD_STATUS_EXECUTING;
521 self.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
522 return CMD_STATUS_FINISHED;
525 float bot_cmd_barrier()
529 // 0 = no barrier, 1 = waiting, 2 = waiting finished
531 if(self.bot_barrier == 0) // initialization
533 self.bot_barrier = 1;
535 //self.colormod = '4 4 0';
538 if(self.bot_barrier == 1) // find other bots
540 FOR_EACH_CLIENT(cl) if(cl.isbot)
542 if(cl.bot_cmdqueuebuf_allocated)
543 if(cl.bot_barrier != 1)
544 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
547 // all bots hit the barrier!
548 FOR_EACH_CLIENT(cl) if(cl.isbot)
550 cl.bot_barrier = 2; // acknowledge barrier
553 bot_barriertime = time;
556 // if we get here, the barrier is finished
558 self.bot_barrier = 0;
559 //self.colormod = '0 0 0';
561 return CMD_STATUS_FINISHED;
566 self.v_angle_y = self.v_angle.y + bot_cmd.bot_cmd_parm_float;
567 self.v_angle_y = self.v_angle.y - floor(self.v_angle.y / 360) * 360;
568 return CMD_STATUS_FINISHED;
571 float bot_cmd_select_weapon()
573 float id = bot_cmd.bot_cmd_parm_float;
575 if(id < WEP_FIRST || id > WEP_LAST)
576 return CMD_STATUS_ERROR;
578 if(client_hasweapon(self, Weapons_from(id), true, false))
579 self.switchweapon = id;
581 return CMD_STATUS_ERROR;
583 return CMD_STATUS_FINISHED;
586 .int bot_cmd_condition_status;
588 const int CMD_CONDITION_NONE = 0;
589 const int CMD_CONDITION_true = 1;
590 const int CMD_CONDITION_false = 2;
591 const int CMD_CONDITION_true_BLOCK = 4;
592 const int CMD_CONDITION_false_BLOCK = 8;
594 float bot_cmd_eval(string expr)
596 // Search for numbers
597 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
603 if(substring(expr, 0, 5)=="cvar.")
605 return cvar(substring(expr, 5, strlen(expr)));
614 return vlen(self.velocity);
616 return ((self.flagcarried!=world));
619 LOG_INFO(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
625 string expr, val_a, val_b;
628 if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
630 // Only one "if" block is allowed at time
631 LOG_INFO("ERROR: Only one conditional block can be processed at time");
632 bot_clearqueue(self);
633 return CMD_STATUS_ERROR;
636 self.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
638 // search for operators
639 expr = bot_cmd.bot_cmd_parm_string;
641 cmpofs = strstrofs(expr,"=",0);
645 val_a = substring(expr,0,cmpofs);
646 val_b = substring(expr,cmpofs+1,strlen(expr));
648 if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
649 self.bot_cmd_condition_status |= CMD_CONDITION_true;
651 self.bot_cmd_condition_status |= CMD_CONDITION_false;
653 return CMD_STATUS_FINISHED;
656 cmpofs = strstrofs(expr,">",0);
660 val_a = substring(expr,0,cmpofs);
661 val_b = substring(expr,cmpofs+1,strlen(expr));
663 if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
664 self.bot_cmd_condition_status |= CMD_CONDITION_true;
666 self.bot_cmd_condition_status |= CMD_CONDITION_false;
668 return CMD_STATUS_FINISHED;
671 cmpofs = strstrofs(expr,"<",0);
675 val_a = substring(expr,0,cmpofs);
676 val_b = substring(expr,cmpofs+1,strlen(expr));
678 if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
679 self.bot_cmd_condition_status |= CMD_CONDITION_true;
681 self.bot_cmd_condition_status |= CMD_CONDITION_false;
683 return CMD_STATUS_FINISHED;
686 if(bot_cmd_eval(expr))
687 self.bot_cmd_condition_status |= CMD_CONDITION_true;
689 self.bot_cmd_condition_status |= CMD_CONDITION_false;
691 return CMD_STATUS_FINISHED;
696 self.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
697 self.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
698 return CMD_STATUS_FINISHED;
703 self.bot_cmd_condition_status = CMD_CONDITION_NONE;
704 return CMD_STATUS_FINISHED;
707 float bot_cmd_resetaim()
709 self.v_angle = '0 0 0';
710 return CMD_STATUS_FINISHED;
713 .float bot_cmd_aim_begintime;
714 .float bot_cmd_aim_endtime;
715 .vector bot_cmd_aim_begin;
716 .vector bot_cmd_aim_end;
721 if(self.bot_cmd_aim_endtime)
725 progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
726 self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
728 if(time>=self.bot_cmd_aim_endtime)
730 self.bot_cmd_aim_endtime = 0;
731 return CMD_STATUS_FINISHED;
734 return CMD_STATUS_EXECUTING;
737 // New aiming direction
741 parms = bot_cmd.bot_cmd_parm_string;
743 tokens = tokenizebyseparator(parms, " ");
745 if(tokens<2||tokens>3)
746 return CMD_STATUS_ERROR;
748 step = (tokens == 3) ? stof(argv(2)) : 0;
752 self.v_angle_x -= stof(argv(1));
753 self.v_angle_y += stof(argv(0));
754 return CMD_STATUS_FINISHED;
757 self.bot_cmd_aim_begin = self.v_angle;
759 self.bot_cmd_aim_end_x = self.v_angle.x - stof(argv(1));
760 self.bot_cmd_aim_end_y = self.v_angle.y + stof(argv(0));
761 self.bot_cmd_aim_end_z = 0;
763 self.bot_cmd_aim_begintime = time;
764 self.bot_cmd_aim_endtime = time + step;
766 return CMD_STATUS_EXECUTING;
769 float bot_cmd_aimtarget()
771 if(self.bot_cmd_aim_endtime)
773 return bot_cmd_aim();
781 parms = bot_cmd.bot_cmd_parm_string;
783 tokens = tokenizebyseparator(parms, " ");
785 e = bot_getplace(argv(0));
787 return CMD_STATUS_ERROR;
789 v = e.origin + (e.mins + e.maxs) * 0.5;
793 self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
794 self.v_angle_x = -self.v_angle.x;
795 return CMD_STATUS_FINISHED;
798 if(tokens<1||tokens>2)
799 return CMD_STATUS_ERROR;
801 step = stof(argv(1));
803 self.bot_cmd_aim_begin = self.v_angle;
804 self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
805 self.bot_cmd_aim_end_x = -self.bot_cmd_aim_end.x;
807 self.bot_cmd_aim_begintime = time;
808 self.bot_cmd_aim_endtime = time + step;
810 return CMD_STATUS_EXECUTING;
815 const int BOT_CMD_KEY_NONE = 0;
816 const int BOT_CMD_KEY_FORWARD = BIT(0);
817 const int BOT_CMD_KEY_BACKWARD = BIT(1);
818 const int BOT_CMD_KEY_RIGHT = BIT(2);
819 const int BOT_CMD_KEY_LEFT = BIT(3);
820 const int BOT_CMD_KEY_JUMP = BIT(4);
821 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
822 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
823 const int BOT_CMD_KEY_USE = BIT(7);
824 const int BOT_CMD_KEY_HOOK = BIT(8);
825 const int BOT_CMD_KEY_CROUCH = BIT(9);
826 const int BOT_CMD_KEY_CHAT = BIT(10);
828 float bot_presskeys()
830 self.movement = '0 0 0';
831 self.BUTTON_JUMP = false;
832 self.BUTTON_CROUCH = false;
833 self.BUTTON_ATCK = false;
834 self.BUTTON_ATCK2 = false;
835 self.BUTTON_USE = false;
836 self.BUTTON_HOOK = false;
837 self.BUTTON_CHAT = false;
839 if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
842 if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
843 self.movement_x = autocvar_sv_maxspeed;
844 else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
845 self.movement_x = -autocvar_sv_maxspeed;
847 if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
848 self.movement_y = autocvar_sv_maxspeed;
849 else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
850 self.movement_y = -autocvar_sv_maxspeed;
852 if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
853 self.BUTTON_JUMP = true;
855 if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
856 self.BUTTON_CROUCH = true;
858 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
859 self.BUTTON_ATCK = true;
861 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
862 self.BUTTON_ATCK2 = true;
864 if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
865 self.BUTTON_USE = true;
867 if(self.bot_cmd_keys & BOT_CMD_KEY_HOOK)
868 self.BUTTON_HOOK = true;
870 if(self.bot_cmd_keys & BOT_CMD_KEY_CHAT)
871 self.BUTTON_CHAT = true;
877 float bot_cmd_keypress_handler(string key, float enabled)
883 self.bot_cmd_keys = power2of(20) - 1; // >:)
885 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
889 self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
890 self.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
893 self.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
898 self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
899 self.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
902 self.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
907 self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
908 self.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
911 self.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
916 self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
917 self.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
920 self.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
924 self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
926 self.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
930 self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
932 self.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
936 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
938 self.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
942 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
944 self.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
948 self.bot_cmd_keys |= BOT_CMD_KEY_USE;
950 self.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
954 self.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
956 self.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
960 self.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
962 self.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
968 return CMD_STATUS_FINISHED;
971 float bot_cmd_presskey()
975 key = bot_cmd.bot_cmd_parm_string;
977 bot_cmd_keypress_handler(key,true);
979 return CMD_STATUS_FINISHED;
982 float bot_cmd_releasekey()
986 key = bot_cmd.bot_cmd_parm_string;
988 return bot_cmd_keypress_handler(key,false);
991 float bot_cmd_pause()
996 self.BUTTON_ATCK = 0;
997 self.BUTTON_JUMP = 0;
998 self.BUTTON_HOOK = 0;
999 self.BUTTON_CHAT = 0;
1000 self.BUTTON_ATCK2 = 0;
1001 self.BUTTON_CROUCH = 0;
1003 self.movement = '0 0 0';
1004 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
1006 self.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1007 return CMD_STATUS_FINISHED;
1010 float bot_cmd_moveto()
1012 return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
1015 float bot_cmd_movetotarget()
1018 e = bot_getplace(bot_cmd.bot_cmd_parm_string);
1020 return CMD_STATUS_ERROR;
1021 return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
1024 float bot_cmd_resetgoal()
1026 return self.cmd_resetgoal();
1030 float bot_cmd_sound()
1033 f = bot_cmd.bot_cmd_parm_string;
1035 float n = tokenizebyseparator(f, " ");
1038 float chan = CH_WEAPON_B;
1039 float vol = VOL_BASE;
1040 float atten = ATTEN_MIN;
1043 sample = argv(n - 1);
1045 chan = stof(argv(0));
1047 vol = stof(argv(1));
1049 atten = stof(argv(2));
1052 _sound(self, chan, sample, vol, atten);
1054 return CMD_STATUS_FINISHED;
1058 float bot_cmd_debug_assert_canfire()
1060 float f = bot_cmd.bot_cmd_parm_float;
1063 .entity weaponentity = weaponentities[slot];
1064 if(self.(weaponentity).state != WS_READY)
1068 self.colormod = '0 8 8';
1069 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " wants to fire, inhibited by weaponentity state\n");
1072 else if(ATTACK_FINISHED(self, slot) > time)
1076 self.colormod = '8 0 8';
1077 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(self, slot) - time), " seconds left)\n");
1080 else if(self.tuba_note)
1084 self.colormod = '8 0 0';
1085 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " wants to fire, bot still has an active tuba note\n");
1092 self.colormod = '8 8 0';
1093 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(self, slot) - time), " seconds left\n");
1097 return CMD_STATUS_FINISHED;
1102 void bot_command_executed(float rm)
1109 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1111 self.bot_cmd_execution_index++;
1114 void bot_setcurrentcommand()
1118 if(!self.bot_cmd_current)
1120 self.bot_cmd_current = new(bot_cmd);
1121 self.bot_cmd_current.is_bot_cmd = true;
1124 bot_cmd = self.bot_cmd_current;
1125 if(bot_cmd.bot_cmd_index != self.bot_cmd_execution_index || self.bot_cmd_execution_index == 0)
1127 if(bot_havecommand(self, self.bot_cmd_execution_index))
1130 cmdstring = bot_readcommand(self, self.bot_cmd_execution_index);
1131 if(bot_decodecommand(cmdstring))
1133 bot_cmd.owner = self;
1134 bot_cmd.bot_cmd_index = self.bot_cmd_execution_index;
1138 // Invalid command, remove from queue
1140 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1141 self.bot_cmd_execution_index++;
1149 void bot_resetqueues()
1153 FOR_EACH_CLIENT(cl) if(cl.isbot)
1155 cl.bot_cmd_execution_index = 0;
1157 // also, cancel all barriers
1159 for(int i = 0; i < cl.bot_places_count; ++i)
1161 strunzone(cl.(bot_placenames[i]));
1162 cl.(bot_placenames[i]) = string_null;
1164 cl.bot_places_count = 0;
1167 bot_barriertime = time;
1170 // Here we map commands to functions and deal with complex interactions between commands and execution states
1171 // NOTE: Of course you need to include your commands here too :)
1172 float bot_execute_commands_once()
1174 float status, ispressingkey;
1177 bot_setcurrentcommand();
1179 // if we have no bot command, better return
1180 // old logic kept pressing previously pressed keys, but that has problems
1181 // (namely, it means you cannot make a bot "normal" ever again)
1182 // to keep a bot walking for a while, use the "wait" bot command
1183 if(bot_cmd == world)
1186 // Ignore all commands except continue when the bot is paused
1187 if(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1188 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1190 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1192 bot_command_executed(true);
1193 LOG_INFO( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1198 // Keep pressing keys raised by the "presskey" command
1199 ispressingkey = boolean(bot_presskeys());
1201 // Handle conditions
1202 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1203 if(self.bot_cmd_condition_status & CMD_CONDITION_true && self.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1205 bot_command_executed(true);
1208 else if(self.bot_cmd_condition_status & CMD_CONDITION_false && self.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1210 bot_command_executed(true);
1214 // Map commands to functions
1215 switch(bot_cmd.bot_cmd_type)
1218 return ispressingkey;
1221 status = bot_cmd_pause();
1223 case BOT_CMD_CONTINUE:
1224 status = bot_cmd_continue();
1227 status = bot_cmd_wait();
1229 case BOT_CMD_WAIT_UNTIL:
1230 status = bot_cmd_wait_until();
1233 status = bot_cmd_turn();
1235 case BOT_CMD_MOVETO:
1236 status = bot_cmd_moveto();
1238 case BOT_CMD_MOVETOTARGET:
1239 status = bot_cmd_movetotarget();
1241 case BOT_CMD_RESETGOAL:
1242 status = bot_cmd_resetgoal();
1245 status = bot_cmd_cc();
1248 status = bot_cmd_if();
1251 status = bot_cmd_else();
1254 status = bot_cmd_fi();
1256 case BOT_CMD_RESETAIM:
1257 status = bot_cmd_resetaim();
1260 status = bot_cmd_aim();
1262 case BOT_CMD_AIMTARGET:
1263 status = bot_cmd_aimtarget();
1265 case BOT_CMD_PRESSKEY:
1266 status = bot_cmd_presskey();
1268 case BOT_CMD_RELEASEKEY:
1269 status = bot_cmd_releasekey();
1271 case BOT_CMD_SELECTWEAPON:
1272 status = bot_cmd_select_weapon();
1274 case BOT_CMD_IMPULSE:
1275 status = bot_cmd_impulse();
1277 case BOT_CMD_BARRIER:
1278 status = bot_cmd_barrier();
1280 case BOT_CMD_CONSOLE:
1281 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1282 status = CMD_STATUS_FINISHED;
1285 status = bot_cmd_sound();
1287 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1288 status = bot_cmd_debug_assert_canfire();
1291 LOG_INFO(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1295 if (status==CMD_STATUS_ERROR)
1296 LOG_INFO(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1298 // Move execution pointer
1299 if(status==CMD_STATUS_EXECUTING)
1305 if(autocvar_g_debug_bot_commands)
1309 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1311 case BOT_CMD_PARAMETER_FLOAT:
1312 parms = ftos(bot_cmd.bot_cmd_parm_float);
1314 case BOT_CMD_PARAMETER_STRING:
1315 parms = bot_cmd.bot_cmd_parm_string;
1317 case BOT_CMD_PARAMETER_VECTOR:
1318 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1324 clientcommand(self,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1327 bot_command_executed(true);
1330 if(status == CMD_STATUS_FINISHED)
1333 return CMD_STATUS_ERROR;
1336 // This function should be (the only) called directly from the bot ai loop
1337 float bot_execute_commands()
1342 f = bot_execute_commands_once();