1 #include "scripting.qh"
3 .float bot_cmdqueuebuf_allocated;
4 .float bot_cmdqueuebuf;
5 .float bot_cmdqueuebuf_start;
6 .float bot_cmdqueuebuf_end;
8 void bot_clearqueue(entity bot)
10 if(!bot.bot_cmdqueuebuf_allocated)
12 buf_del(bot.bot_cmdqueuebuf);
13 bot.bot_cmdqueuebuf_allocated = false;
14 LOG_TRACE("bot ", bot.netname, " queue cleared\n");
17 void bot_queuecommand(entity bot, string cmdstring)
19 if(!bot.bot_cmdqueuebuf_allocated)
21 bot.bot_cmdqueuebuf = buf_create();
22 bot.bot_cmdqueuebuf_allocated = true;
23 bot.bot_cmdqueuebuf_start = 0;
24 bot.bot_cmdqueuebuf_end = 0;
27 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
29 // if the command was a "sound" command, precache the sound NOW
30 // this prevents lagging!
36 sp = strstrofs(cmdstring, " ", 0);
39 parm = substring(cmdstring, sp + 1, -1);
40 cmdstr = substring(cmdstring, 0, sp);
46 sp = strstrofs(parm, " ", 0);
49 parm = substring(parm, sp + 1, -1);
56 bot.bot_cmdqueuebuf_end += 1;
59 void bot_dequeuecommand(entity bot, float idx)
61 if(!bot.bot_cmdqueuebuf_allocated)
62 error("dequeuecommand but no queue allocated");
63 if(idx < bot.bot_cmdqueuebuf_start)
64 error("dequeueing a command in the past");
65 if(idx >= bot.bot_cmdqueuebuf_end)
66 error("dequeueing a command in the future");
67 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
68 if(idx == bot.bot_cmdqueuebuf_start)
69 bot.bot_cmdqueuebuf_start += 1;
70 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
74 string bot_readcommand(entity bot, float idx)
76 if(!bot.bot_cmdqueuebuf_allocated)
77 error("readcommand but no queue allocated");
78 if(idx < bot.bot_cmdqueuebuf_start)
79 error("reading a command in the past");
80 if(idx >= bot.bot_cmdqueuebuf_end)
81 error("reading a command in the future");
82 return bufstr_get(bot.bot_cmdqueuebuf, idx);
85 float bot_havecommand(entity bot, float idx)
87 if(!bot.bot_cmdqueuebuf_allocated)
89 if(idx < bot.bot_cmdqueuebuf_start)
91 if(idx >= bot.bot_cmdqueuebuf_end)
96 const int MAX_BOT_PLACES = 4;
97 .float bot_places_count;
98 .entity bot_places[MAX_BOT_PLACES];
99 .string bot_placenames[MAX_BOT_PLACES];
100 entity bot_getplace(string placename)
103 if(substring(placename, 0, 1) == "@")
106 placename = substring(placename, 1, -1);
108 for(i = 0; i < self.bot_places_count; ++i)
109 if(self.(bot_placenames[i]) == placename)
110 return self.(bot_places[i]);
111 // now: i == self.bot_places_count
112 s = s2 = cvar_string(placename);
113 p = strstrofs(s2, " ", 0);
116 s = substring(s2, 0, p);
117 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
118 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
119 //print("places: ", placename, " := ", cvar_string(placename), "\n");
121 e = find(world, targetname, s);
123 LOG_INFO("invalid place ", s, "\n");
124 if(i < MAX_BOT_PLACES)
126 self.(bot_placenames[i]) = strzone(placename);
127 self.(bot_places[i]) = e;
128 self.bot_places_count += 1;
134 e = find(world, targetname, placename);
136 LOG_INFO("invalid place ", placename, "\n");
142 // Initialize global commands list
143 // NOTE: New commands should be initialized here
144 void bot_commands_init()
146 bot_cmd_string[BOT_CMD_NULL] = "";
147 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
149 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
150 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
152 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
153 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
155 bot_cmd_string[BOT_CMD_WAIT] = "wait";
156 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
158 bot_cmd_string[BOT_CMD_TURN] = "turn";
159 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
161 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
162 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
164 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
165 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
167 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
168 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
170 bot_cmd_string[BOT_CMD_CC] = "cc";
171 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
173 bot_cmd_string[BOT_CMD_IF] = "if";
174 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
176 bot_cmd_string[BOT_CMD_ELSE] = "else";
177 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
179 bot_cmd_string[BOT_CMD_FI] = "fi";
180 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
182 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
183 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
185 bot_cmd_string[BOT_CMD_AIM] = "aim";
186 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
188 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
189 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
191 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
192 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
194 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
195 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
197 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
198 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
200 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
201 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
203 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
204 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
206 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
207 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
209 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
210 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
212 bot_cmd_string[BOT_CMD_SOUND] = "sound";
213 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
215 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
216 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
218 bot_cmds_initialized = true;
221 // Returns first bot with matching name
222 entity find_bot_by_name(string name)
226 bot = findchainflags(flags, FL_CLIENT);
229 if(IS_BOT_CLIENT(bot))
230 if(bot.netname==name)
239 // Returns a bot by number on list
240 entity find_bot_by_number(float number)
248 bot = findchainflags(flags, FL_CLIENT);
251 if(IS_BOT_CLIENT(bot))
262 float bot_decodecommand(string cmdstring)
268 sp = strstrofs(cmdstring, " ", 0);
275 parm = substring(cmdstring, sp + 1, -1);
276 cmdstring = substring(cmdstring, 0, sp);
279 if(!bot_cmds_initialized)
283 for(i=1;i<BOT_CMD_COUNTER;++i)
285 if(bot_cmd_string[i]!=cmdstring)
288 cmd_parm_type = bot_cmd_parm_type[i];
290 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
292 LOG_INFO("ERROR: A parameter is required for this command\n");
296 // Load command into queue
297 bot_cmd.bot_cmd_type = i;
300 switch(cmd_parm_type)
302 case BOT_CMD_PARAMETER_FLOAT:
303 bot_cmd.bot_cmd_parm_float = stof(parm);
305 case BOT_CMD_PARAMETER_STRING:
306 if(bot_cmd.bot_cmd_parm_string)
307 strunzone(bot_cmd.bot_cmd_parm_string);
308 bot_cmd.bot_cmd_parm_string = strzone(parm);
310 case BOT_CMD_PARAMETER_VECTOR:
311 bot_cmd.bot_cmd_parm_vector = stov(parm);
318 LOG_INFO("ERROR: No such command '", cmdstring, "'\n");
322 void bot_cmdhelp(string scmd)
327 if(!bot_cmds_initialized)
330 for(i=1;i<BOT_CMD_COUNTER;++i)
332 if(bot_cmd_string[i]!=scmd)
335 ntype = bot_cmd_parm_type[i];
339 case BOT_CMD_PARAMETER_FLOAT:
340 stype = "float number";
342 case BOT_CMD_PARAMETER_STRING:
345 case BOT_CMD_PARAMETER_VECTOR:
353 LOG_INFO(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
355 LOG_INFO("Description: ");
359 LOG_INFO("Stops the bot completely. Any command other than 'continue' will be ignored.");
361 case BOT_CMD_CONTINUE:
362 LOG_INFO("Disable paused status");
365 LOG_INFO("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
367 case BOT_CMD_WAIT_UNTIL:
368 LOG_INFO("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
370 case BOT_CMD_BARRIER:
371 LOG_INFO("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
374 LOG_INFO("Look to the right or left N degrees. For turning to the left use positive numbers.");
377 LOG_INFO("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
379 case BOT_CMD_MOVETOTARGET:
380 LOG_INFO("Walk to the specific target on the map");
382 case BOT_CMD_RESETGOAL:
383 LOG_INFO("Resets the goal stack");
386 LOG_INFO("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
389 LOG_INFO("Perform simple conditional execution.\n");
390 LOG_INFO("Syntax: \n");
391 LOG_INFO(" sv_cmd .. if \"condition\"\n");
392 LOG_INFO(" sv_cmd .. <instruction if true>\n");
393 LOG_INFO(" sv_cmd .. <instruction if true>\n");
394 LOG_INFO(" sv_cmd .. else\n");
395 LOG_INFO(" sv_cmd .. <instruction if false>\n");
396 LOG_INFO(" sv_cmd .. <instruction if false>\n");
397 LOG_INFO(" sv_cmd .. fi\n");
398 LOG_INFO("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
399 LOG_INFO(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
400 LOG_INFO("Fields: health, speed, flagcarrier\n");
401 LOG_INFO("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
403 case BOT_CMD_RESETAIM:
404 LOG_INFO("Points the aim to the coordinates x,y 0,0");
407 LOG_INFO("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
408 LOG_INFO("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
409 LOG_INFO("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
410 LOG_INFO(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
412 case BOT_CMD_AIMTARGET:
413 LOG_INFO("Points the aim to given target");
415 case BOT_CMD_PRESSKEY:
416 LOG_INFO("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
417 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
418 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
420 case BOT_CMD_RELEASEKEY:
421 LOG_INFO("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
424 LOG_INFO("play sound file at bot location");
426 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
427 LOG_INFO("verify the state of the weapon entity");
430 LOG_INFO("This command has no description yet.");
437 void bot_list_commands()
442 if(!bot_cmds_initialized)
445 LOG_INFO("List of all available commands:\n");
446 LOG_INFO(" Command - Parameter Type\n");
448 for(i=1;i<BOT_CMD_COUNTER;++i)
450 switch(bot_cmd_parm_type[i])
452 case BOT_CMD_PARAMETER_FLOAT:
453 ptype = "float number";
455 case BOT_CMD_PARAMETER_STRING:
458 case BOT_CMD_PARAMETER_VECTOR:
465 LOG_INFO(strcat(" ",bot_cmd_string[i]," - <",ptype,"> \n"));
470 .int bot_exec_status;
472 void SV_ParseClientCommand(string s);
475 SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
476 return CMD_STATUS_FINISHED;
479 float bot_cmd_impulse()
481 self.impulse = bot_cmd.bot_cmd_parm_float;
482 return CMD_STATUS_FINISHED;
485 float bot_cmd_continue()
487 self.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
488 return CMD_STATUS_FINISHED;
491 .float bot_cmd_wait_time;
494 if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
496 if(time>=self.bot_cmd_wait_time)
498 self.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
499 return CMD_STATUS_FINISHED;
502 return CMD_STATUS_EXECUTING;
505 self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
506 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
507 return CMD_STATUS_EXECUTING;
510 float bot_cmd_wait_until()
512 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
514 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
515 return CMD_STATUS_EXECUTING;
517 self.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
518 return CMD_STATUS_FINISHED;
521 float bot_cmd_barrier()
525 // 0 = no barrier, 1 = waiting, 2 = waiting finished
527 if(self.bot_barrier == 0) // initialization
529 self.bot_barrier = 1;
531 //self.colormod = '4 4 0';
534 if(self.bot_barrier == 1) // find other bots
536 FOR_EACH_CLIENT(cl) if(cl.isbot)
538 if(cl.bot_cmdqueuebuf_allocated)
539 if(cl.bot_barrier != 1)
540 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
543 // all bots hit the barrier!
544 FOR_EACH_CLIENT(cl) if(cl.isbot)
546 cl.bot_barrier = 2; // acknowledge barrier
549 bot_barriertime = time;
552 // if we get here, the barrier is finished
554 self.bot_barrier = 0;
555 //self.colormod = '0 0 0';
557 return CMD_STATUS_FINISHED;
562 self.v_angle_y = self.v_angle.y + bot_cmd.bot_cmd_parm_float;
563 self.v_angle_y = self.v_angle.y - floor(self.v_angle.y / 360) * 360;
564 return CMD_STATUS_FINISHED;
567 float bot_cmd_select_weapon()
571 id = bot_cmd.bot_cmd_parm_float;
573 if(id < WEP_FIRST || id > WEP_LAST)
574 return CMD_STATUS_ERROR;
576 if(client_hasweapon(self, id, true, false))
577 self.switchweapon = id;
579 return CMD_STATUS_ERROR;
581 return CMD_STATUS_FINISHED;
584 .int bot_cmd_condition_status;
586 const int CMD_CONDITION_NONE = 0;
587 const int CMD_CONDITION_true = 1;
588 const int CMD_CONDITION_false = 2;
589 const int CMD_CONDITION_true_BLOCK = 4;
590 const int CMD_CONDITION_false_BLOCK = 8;
592 float bot_cmd_eval(string expr)
594 // Search for numbers
595 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
601 if(substring(expr, 0, 5)=="cvar.")
603 return cvar(substring(expr, 5, strlen(expr)));
612 return vlen(self.velocity);
614 return ((self.flagcarried!=world));
617 LOG_INFO(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
623 string expr, val_a, val_b;
626 if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
628 // Only one "if" block is allowed at time
629 LOG_INFO("ERROR: Only one conditional block can be processed at time");
630 bot_clearqueue(self);
631 return CMD_STATUS_ERROR;
634 self.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
636 // search for operators
637 expr = bot_cmd.bot_cmd_parm_string;
639 cmpofs = strstrofs(expr,"=",0);
643 val_a = substring(expr,0,cmpofs);
644 val_b = substring(expr,cmpofs+1,strlen(expr));
646 if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
647 self.bot_cmd_condition_status |= CMD_CONDITION_true;
649 self.bot_cmd_condition_status |= CMD_CONDITION_false;
651 return CMD_STATUS_FINISHED;
654 cmpofs = strstrofs(expr,">",0);
658 val_a = substring(expr,0,cmpofs);
659 val_b = substring(expr,cmpofs+1,strlen(expr));
661 if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
662 self.bot_cmd_condition_status |= CMD_CONDITION_true;
664 self.bot_cmd_condition_status |= CMD_CONDITION_false;
666 return CMD_STATUS_FINISHED;
669 cmpofs = strstrofs(expr,"<",0);
673 val_a = substring(expr,0,cmpofs);
674 val_b = substring(expr,cmpofs+1,strlen(expr));
676 if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
677 self.bot_cmd_condition_status |= CMD_CONDITION_true;
679 self.bot_cmd_condition_status |= CMD_CONDITION_false;
681 return CMD_STATUS_FINISHED;
684 if(bot_cmd_eval(expr))
685 self.bot_cmd_condition_status |= CMD_CONDITION_true;
687 self.bot_cmd_condition_status |= CMD_CONDITION_false;
689 return CMD_STATUS_FINISHED;
694 self.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
695 self.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
696 return CMD_STATUS_FINISHED;
701 self.bot_cmd_condition_status = CMD_CONDITION_NONE;
702 return CMD_STATUS_FINISHED;
705 float bot_cmd_resetaim()
707 self.v_angle = '0 0 0';
708 return CMD_STATUS_FINISHED;
711 .float bot_cmd_aim_begintime;
712 .float bot_cmd_aim_endtime;
713 .vector bot_cmd_aim_begin;
714 .vector bot_cmd_aim_end;
719 if(self.bot_cmd_aim_endtime)
723 progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
724 self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
726 if(time>=self.bot_cmd_aim_endtime)
728 self.bot_cmd_aim_endtime = 0;
729 return CMD_STATUS_FINISHED;
732 return CMD_STATUS_EXECUTING;
735 // New aiming direction
739 parms = bot_cmd.bot_cmd_parm_string;
741 tokens = tokenizebyseparator(parms, " ");
743 if(tokens<2||tokens>3)
744 return CMD_STATUS_ERROR;
746 step = (tokens == 3) ? stof(argv(2)) : 0;
750 self.v_angle_x -= stof(argv(1));
751 self.v_angle_y += stof(argv(0));
752 return CMD_STATUS_FINISHED;
755 self.bot_cmd_aim_begin = self.v_angle;
757 self.bot_cmd_aim_end_x = self.v_angle.x - stof(argv(1));
758 self.bot_cmd_aim_end_y = self.v_angle.y + stof(argv(0));
759 self.bot_cmd_aim_end_z = 0;
761 self.bot_cmd_aim_begintime = time;
762 self.bot_cmd_aim_endtime = time + step;
764 return CMD_STATUS_EXECUTING;
767 float bot_cmd_aimtarget()
769 if(self.bot_cmd_aim_endtime)
771 return bot_cmd_aim();
779 parms = bot_cmd.bot_cmd_parm_string;
781 tokens = tokenizebyseparator(parms, " ");
783 e = bot_getplace(argv(0));
785 return CMD_STATUS_ERROR;
787 v = e.origin + (e.mins + e.maxs) * 0.5;
791 self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
792 self.v_angle_x = -self.v_angle.x;
793 return CMD_STATUS_FINISHED;
796 if(tokens<1||tokens>2)
797 return CMD_STATUS_ERROR;
799 step = stof(argv(1));
801 self.bot_cmd_aim_begin = self.v_angle;
802 self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
803 self.bot_cmd_aim_end_x = -self.bot_cmd_aim_end.x;
805 self.bot_cmd_aim_begintime = time;
806 self.bot_cmd_aim_endtime = time + step;
808 return CMD_STATUS_EXECUTING;
813 const int BOT_CMD_KEY_NONE = 0;
814 const int BOT_CMD_KEY_FORWARD = 1;
815 const int BOT_CMD_KEY_BACKWARD = 2;
816 const int BOT_CMD_KEY_RIGHT = 4;
817 const int BOT_CMD_KEY_LEFT = 8;
818 const int BOT_CMD_KEY_JUMP = 16;
819 const int BOT_CMD_KEY_ATTACK1 = 32;
820 const int BOT_CMD_KEY_ATTACK2 = 64;
821 const int BOT_CMD_KEY_USE = 128;
822 const int BOT_CMD_KEY_HOOK = 256;
823 const int BOT_CMD_KEY_CROUCH = 512;
824 const int BOT_CMD_KEY_CHAT = 1024;
826 float bot_presskeys()
828 self.movement = '0 0 0';
829 self.BUTTON_JUMP = false;
830 self.BUTTON_CROUCH = false;
831 self.BUTTON_ATCK = false;
832 self.BUTTON_ATCK2 = false;
833 self.BUTTON_USE = false;
834 self.BUTTON_HOOK = false;
835 self.BUTTON_CHAT = false;
837 if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
840 if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
841 self.movement_x = autocvar_sv_maxspeed;
842 else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
843 self.movement_x = -autocvar_sv_maxspeed;
845 if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
846 self.movement_y = autocvar_sv_maxspeed;
847 else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
848 self.movement_y = -autocvar_sv_maxspeed;
850 if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
851 self.BUTTON_JUMP = true;
853 if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
854 self.BUTTON_CROUCH = true;
856 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
857 self.BUTTON_ATCK = true;
859 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
860 self.BUTTON_ATCK2 = true;
862 if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
863 self.BUTTON_USE = true;
865 if(self.bot_cmd_keys & BOT_CMD_KEY_HOOK)
866 self.BUTTON_HOOK = true;
868 if(self.bot_cmd_keys & BOT_CMD_KEY_CHAT)
869 self.BUTTON_CHAT = true;
875 float bot_cmd_keypress_handler(string key, float enabled)
881 self.bot_cmd_keys = power2of(20) - 1; // >:)
883 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
887 self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
888 self.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
891 self.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
896 self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
897 self.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
900 self.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
905 self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
906 self.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
909 self.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
914 self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
915 self.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
918 self.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
922 self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
924 self.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
928 self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
930 self.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
934 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
936 self.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
940 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
942 self.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
946 self.bot_cmd_keys |= BOT_CMD_KEY_USE;
948 self.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
952 self.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
954 self.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
958 self.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
960 self.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
966 return CMD_STATUS_FINISHED;
969 float bot_cmd_presskey()
973 key = bot_cmd.bot_cmd_parm_string;
975 bot_cmd_keypress_handler(key,true);
977 return CMD_STATUS_FINISHED;
980 float bot_cmd_releasekey()
984 key = bot_cmd.bot_cmd_parm_string;
986 return bot_cmd_keypress_handler(key,false);
989 float bot_cmd_pause()
994 self.BUTTON_ATCK = 0;
995 self.BUTTON_JUMP = 0;
996 self.BUTTON_HOOK = 0;
997 self.BUTTON_CHAT = 0;
998 self.BUTTON_ATCK2 = 0;
999 self.BUTTON_CROUCH = 0;
1001 self.movement = '0 0 0';
1002 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
1004 self.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1005 return CMD_STATUS_FINISHED;
1008 float bot_cmd_moveto()
1010 return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
1013 float bot_cmd_movetotarget()
1016 e = bot_getplace(bot_cmd.bot_cmd_parm_string);
1018 return CMD_STATUS_ERROR;
1019 return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
1022 float bot_cmd_resetgoal()
1024 return self.cmd_resetgoal();
1028 float bot_cmd_sound()
1031 f = bot_cmd.bot_cmd_parm_string;
1033 float n = tokenizebyseparator(f, " ");
1036 float chan = CH_WEAPON_B;
1037 float vol = VOL_BASE;
1038 float atten = ATTEN_MIN;
1041 sample = argv(n - 1);
1043 chan = stof(argv(0));
1045 vol = stof(argv(1));
1047 atten = stof(argv(2));
1050 _sound(self, chan, sample, vol, atten);
1052 return CMD_STATUS_FINISHED;
1056 float bot_cmd_debug_assert_canfire()
1059 f = bot_cmd.bot_cmd_parm_float;
1061 if(self.weaponentity.state != WS_READY)
1065 self.colormod = '0 8 8';
1066 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " wants to fire, inhibited by weaponentity state\n");
1069 else if(ATTACK_FINISHED(self) > time)
1073 self.colormod = '8 0 8';
1074 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(self) - time), " seconds left)\n");
1077 else if(self.tuba_note)
1081 self.colormod = '8 0 0';
1082 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " wants to fire, bot still has an active tuba note\n");
1089 self.colormod = '8 8 0';
1090 LOG_INFO("Bot ", self.netname, " using ", self.weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(self) - time), " seconds left\n");
1094 return CMD_STATUS_FINISHED;
1099 void bot_command_executed(float rm)
1106 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1108 self.bot_cmd_execution_index++;
1111 void bot_setcurrentcommand()
1115 if(!self.bot_cmd_current)
1117 self.bot_cmd_current = spawn();
1118 self.bot_cmd_current.classname = "bot_cmd";
1119 self.bot_cmd_current.is_bot_cmd = 1;
1122 bot_cmd = self.bot_cmd_current;
1123 if(bot_cmd.bot_cmd_index != self.bot_cmd_execution_index || self.bot_cmd_execution_index == 0)
1125 if(bot_havecommand(self, self.bot_cmd_execution_index))
1128 cmdstring = bot_readcommand(self, self.bot_cmd_execution_index);
1129 if(bot_decodecommand(cmdstring))
1131 bot_cmd.owner = self;
1132 bot_cmd.bot_cmd_index = self.bot_cmd_execution_index;
1136 // Invalid command, remove from queue
1138 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1139 self.bot_cmd_execution_index++;
1147 void bot_resetqueues()
1151 FOR_EACH_CLIENT(cl) if(cl.isbot)
1153 cl.bot_cmd_execution_index = 0;
1155 // also, cancel all barriers
1157 for(int i = 0; i < cl.bot_places_count; ++i)
1159 strunzone(cl.(bot_placenames[i]));
1160 cl.(bot_placenames[i]) = string_null;
1162 cl.bot_places_count = 0;
1165 bot_barriertime = time;
1168 bool autocvar_g_debug_bot_commands;
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 = !!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();