1 .float bot_cmdqueuebuf_allocated;
2 .float bot_cmdqueuebuf;
3 .float bot_cmdqueuebuf_start;
4 .float bot_cmdqueuebuf_end;
6 void bot_clearqueue(entity bot)
8 if(!bot.bot_cmdqueuebuf_allocated)
9 error("clearqueue but no queue allocated");
10 buf_del(bot.bot_cmdqueuebuf);
11 bot.bot_cmdqueuebuf_allocated = FALSE;
12 dprint("bot ", bot.netname, " queue cleared\n");
15 void bot_queuecommand(entity bot, string cmdstring)
17 if(!bot.bot_cmdqueuebuf_allocated)
19 bot.bot_cmdqueuebuf = buf_create();
20 bot.bot_cmdqueuebuf_allocated = TRUE;
23 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
25 // if the command was a "sound" command, precache the sound NOW
26 // this prevents lagging!
32 sp = strstrofs(cmdstr, " ", 0);
39 parm = substring(cmdstr, sp + 1, -1);
40 cmdstr = substring(cmdstr, 0, sp);
43 precache_sound(cmdstr);
46 bot.bot_cmdqueuebuf_end += 1;
49 void bot_dequeuecommand(entity bot, float idx)
51 if(!bot.bot_cmdqueuebuf_allocated)
52 error("dequeuecommand but no queue allocated");
53 if(idx < bot.bot_cmdqueuebuf_start)
54 error("dequeueing a command in the past");
55 if(idx >= bot.bot_cmdqueuebuf_end)
56 error("dequeueing a command in the future");
57 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
58 if(idx == bot.bot_cmdqueuebuf_start)
59 bot.bot_cmdqueuebuf_start += 1;
60 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
64 string bot_readcommand(entity bot, float idx)
66 if(!bot.bot_cmdqueuebuf_allocated)
67 error("readcommand but no queue allocated");
68 if(idx < bot.bot_cmdqueuebuf_start)
69 error("reading a command in the past");
70 if(idx >= bot.bot_cmdqueuebuf_end)
71 error("reading a command in the future");
72 return bufstr_get(bot.bot_cmdqueuebuf, idx);
75 float bot_havecommand(entity bot, float idx)
77 if(!bot.bot_cmdqueuebuf_allocated)
79 if(idx < bot.bot_cmdqueuebuf_start)
81 if(idx >= bot.bot_cmdqueuebuf_end)
86 #define MAX_BOT_PLACES 4
87 .float bot_places_count;
88 .entity bot_places[MAX_BOT_PLACES];
89 .string bot_placenames[MAX_BOT_PLACES];
90 entity bot_getplace(string placename)
93 if(substring(placename, 0, 1) == "@")
96 placename = substring(placename, 1, -1);
98 for(i = 0; i < self.bot_places_count; ++i)
99 if(self.(bot_placenames[i]) == placename)
100 return self.(bot_places[i]);
101 // now: i == self.bot_places_count
102 s = s2 = cvar_string(placename);
103 p = strstrofs(s2, " ", 0);
106 s = substring(s2, 0, p);
107 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
108 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
109 //print("places: ", placename, " := ", cvar_string(placename), "\n");
111 e = find(world, targetname, s);
113 print("invalid place ", s, "\n");
114 if(i < MAX_BOT_PLACES)
116 self.(bot_placenames[i]) = strzone(placename);
117 self.(bot_places[i]) = e;
118 self.bot_places_count += 1;
124 e = find(world, targetname, placename);
126 print("invalid place ", placename, "\n");
132 // NOTE: New commands should be added here. Do not forget to update BOT_CMD_COUNTER
133 #define BOT_CMD_NULL 0
134 #define BOT_CMD_PAUSE 1
135 #define BOT_CMD_CONTINUE 2
136 #define BOT_CMD_WAIT 3
137 #define BOT_CMD_TURN 4
138 #define BOT_CMD_MOVETO 5
139 #define BOT_CMD_RESETGOAL 6 // Not implemented yet
142 #define BOT_CMD_ELSE 9
143 #define BOT_CMD_FI 10
144 #define BOT_CMD_RESETAIM 11
145 #define BOT_CMD_AIM 12
146 #define BOT_CMD_PRESSKEY 13
147 #define BOT_CMD_RELEASEKEY 14
148 #define BOT_CMD_SELECTWEAPON 15
149 #define BOT_CMD_IMPULSE 16
150 #define BOT_CMD_WAIT_UNTIL 17
151 #define BOT_CMD_MOVETOTARGET 18
152 #define BOT_CMD_AIMTARGET 19
153 #define BOT_CMD_BARRIER 20
154 #define BOT_CMD_CONSOLE 21
155 #define BOT_CMD_SOUND 22
156 #define BOT_CMD_DEBUG_ASSERT_CANFIRE 23
157 #define BOT_CMD_WHILE 24 // TODO: Not implemented yet
158 #define BOT_CMD_WEND 25 // TODO: Not implemented yet
159 #define BOT_CMD_CHASE 26 // TODO: Not implemented yet
161 #define BOT_CMD_COUNTER 24 // Update this value if you add/remove a command
163 // NOTE: Following commands should be implemented on the bot ai
164 // If a new command should be handled by the target ai(s) please declare it here
165 .float(vector) cmd_moveto;
166 .float() cmd_resetgoal;
169 #define BOT_CMD_PARAMETER_NONE 0
170 #define BOT_CMD_PARAMETER_FLOAT 1
171 #define BOT_CMD_PARAMETER_STRING 2
172 #define BOT_CMD_PARAMETER_VECTOR 3
174 float bot_cmds_initialized;
175 float bot_cmd_parm_type[BOT_CMD_COUNTER];
176 string bot_cmd_string[BOT_CMD_COUNTER];
178 // Bots command queue
179 entity bot_cmd; // global current command
180 .entity bot_cmd_current; // current command of this bot
182 .float is_bot_cmd; // Tells if the entity is a bot command
183 .float bot_cmd_index; // Position of the command in the queue
184 .float bot_cmd_type; // If of command (see the BOT_CMD_* defines)
185 .float bot_cmd_parm_float; // Field to store a float parameter
186 .string bot_cmd_parm_string; // Field to store a string parameter
187 .vector bot_cmd_parm_vector; // Field to store a vector parameter
189 float bot_barriertime;
192 .float bot_cmd_execution_index; // Position in the queue of the command to be executed
194 // Initialize global commands list
195 // NOTE: New commands should be initialized here
196 void bot_commands_init()
198 bot_cmd_string[BOT_CMD_NULL] = "";
199 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
201 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
202 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
204 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
205 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
207 bot_cmd_string[BOT_CMD_WAIT] = "wait";
208 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
210 bot_cmd_string[BOT_CMD_TURN] = "turn";
211 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
213 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
214 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
216 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
217 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
219 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
220 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
222 bot_cmd_string[BOT_CMD_CC] = "cc";
223 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
225 bot_cmd_string[BOT_CMD_IF] = "if";
226 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
228 bot_cmd_string[BOT_CMD_ELSE] = "else";
229 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
231 bot_cmd_string[BOT_CMD_FI] = "fi";
232 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
234 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
235 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
237 bot_cmd_string[BOT_CMD_AIM] = "aim";
238 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
240 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
241 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
243 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
244 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
246 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
247 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
249 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
250 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
252 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
253 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
255 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
256 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
258 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
259 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
261 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
262 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
264 bot_cmd_string[BOT_CMD_SOUND] = "sound";
265 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
267 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
268 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
270 bot_cmds_initialized = TRUE;
273 // Returns first bot with matching name
274 entity find_bot_by_name(string name)
278 bot = findchainflags(flags, FL_CLIENT);
281 if(clienttype(bot) == CLIENTTYPE_BOT)
282 if(bot.netname==name)
291 // Returns a bot by number on list
292 entity find_bot_by_number(float number)
300 bot = findchainflags(flags, FL_CLIENT);
303 if(clienttype(bot) == CLIENTTYPE_BOT)
314 float bot_decodecommand(string cmdstring)
316 float cmd_parm_type, i;
320 sp = strstrofs(cmdstring, " ", 0);
327 parm = substring(cmdstring, sp + 1, -1);
328 cmdstring = substring(cmdstring, 0, sp);
331 if(!bot_cmds_initialized)
334 for(i=1;i<BOT_CMD_COUNTER;++i)
336 if(bot_cmd_string[i]!=cmdstring)
339 cmd_parm_type = bot_cmd_parm_type[i];
341 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
343 print("ERROR: A parameter is required for this command\n");
347 // Load command into queue
348 bot_cmd.bot_cmd_type = i;
351 switch(cmd_parm_type)
353 case BOT_CMD_PARAMETER_FLOAT:
354 bot_cmd.bot_cmd_parm_float = stof(parm);
356 case BOT_CMD_PARAMETER_STRING:
357 if(bot_cmd.bot_cmd_parm_string)
358 strunzone(bot_cmd.bot_cmd_parm_string);
359 bot_cmd.bot_cmd_parm_string = strzone(parm);
361 case BOT_CMD_PARAMETER_VECTOR:
362 bot_cmd.bot_cmd_parm_vector = stov(parm);
369 print("ERROR: No such command '", cmdstring, "'\n");
373 void bot_cmdhelp(string scmd)
378 if(!bot_cmds_initialized)
381 for(i=1;i<BOT_CMD_COUNTER;++i)
383 if(bot_cmd_string[i]!=scmd)
386 ntype = bot_cmd_parm_type[i];
390 case BOT_CMD_PARAMETER_FLOAT:
391 stype = "float number";
393 case BOT_CMD_PARAMETER_STRING:
396 case BOT_CMD_PARAMETER_VECTOR:
404 print(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
406 print("Description: ");
410 print("Stops the bot completely. Any command other than 'continue' will be ignored.");
412 case BOT_CMD_CONTINUE:
413 print("Disable paused status");
416 print("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
418 case BOT_CMD_WAIT_UNTIL:
419 print("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
421 case BOT_CMD_BARRIER:
422 print("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
425 print("Look to the right or left N degrees. For turning to the left use positive numbers.");
428 print("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
430 case BOT_CMD_MOVETOTARGET:
431 print("Walk to the specific target on the map");
433 case BOT_CMD_RESETGOAL:
434 print("Resets the goal stack");
437 print("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
440 print("Perform simple conditional execution.\n");
442 print(" sv_cmd .. if \"condition\"\n");
443 print(" sv_cmd .. <instruction if true>\n");
444 print(" sv_cmd .. <instruction if true>\n");
445 print(" sv_cmd .. else\n");
446 print(" sv_cmd .. <instruction if false>\n");
447 print(" sv_cmd .. <instruction if false>\n");
448 print(" sv_cmd .. fi\n");
449 print("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
450 print(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
451 print("Fields: health, speed, flagcarrier\n");
452 print("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
454 case BOT_CMD_RESETAIM:
455 print("Points the aim to the coordinates x,y 0,0");
458 print("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
459 print("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
460 print("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
461 print(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
463 case BOT_CMD_AIMTARGET:
464 print("Points the aim to given target");
466 case BOT_CMD_PRESSKEY:
467 print("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
468 print("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
469 print("Note: The script will not return the control to the bot ai until all keys are released");
471 case BOT_CMD_RELEASEKEY:
472 print("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
475 print("play sound file at bot location");
477 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
478 print("verify the state of the weapon entity");
481 print("This command has no description yet.");
488 void bot_list_commands()
493 if(!bot_cmds_initialized)
496 print("List of all available commands:\n");
497 print(" Command - Parameter Type\n");
499 for(i=1;i<BOT_CMD_COUNTER;++i)
501 switch(bot_cmd_parm_type[i])
503 case BOT_CMD_PARAMETER_FLOAT:
504 ptype = "float number";
506 case BOT_CMD_PARAMETER_STRING:
509 case BOT_CMD_PARAMETER_VECTOR:
516 print(strcat(" ",bot_cmd_string[i]," - <",ptype,"> \n"));
521 .float bot_exec_status;
523 #define BOT_EXEC_STATUS_IDLE 0
524 #define BOT_EXEC_STATUS_PAUSED 1
525 #define BOT_EXEC_STATUS_WAITING 2
527 #define CMD_STATUS_EXECUTING 0
528 #define CMD_STATUS_FINISHED 1
529 #define CMD_STATUS_ERROR 2
531 void SV_ParseClientCommand(string s);
534 SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
535 return CMD_STATUS_FINISHED;
538 float bot_cmd_impulse()
540 self.impulse = bot_cmd.bot_cmd_parm_float;
541 return CMD_STATUS_FINISHED;
544 float bot_cmd_continue()
546 self.bot_exec_status &~= BOT_EXEC_STATUS_PAUSED;
547 return CMD_STATUS_FINISHED;
550 .float bot_cmd_wait_time;
553 if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
555 if(time>=self.bot_cmd_wait_time)
557 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
558 return CMD_STATUS_FINISHED;
561 return CMD_STATUS_EXECUTING;
564 self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
565 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
566 return CMD_STATUS_EXECUTING;
569 float bot_cmd_wait_until()
571 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
573 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
574 return CMD_STATUS_EXECUTING;
576 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
577 return CMD_STATUS_FINISHED;
580 float bot_cmd_barrier()
584 // 0 = no barrier, 1 = waiting, 2 = waiting finished
586 if(self.bot_barrier == 0) // initialization
588 self.bot_barrier = 1;
590 //self.colormod = '4 4 0';
593 if(self.bot_barrier == 1) // find other bots
595 FOR_EACH_CLIENT(cl) if(cl.isbot)
597 if(cl.bot_cmdqueuebuf_allocated)
598 if(cl.bot_barrier != 1)
599 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
602 // all bots hit the barrier!
603 FOR_EACH_CLIENT(cl) if(cl.isbot)
605 cl.bot_barrier = 2; // acknowledge barrier
608 bot_barriertime = time;
611 // if we get here, the barrier is finished
613 self.bot_barrier = 0;
614 //self.colormod = '0 0 0';
616 return CMD_STATUS_FINISHED;
621 self.v_angle_y = self.v_angle_y + bot_cmd.bot_cmd_parm_float;
622 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
623 return CMD_STATUS_FINISHED;
626 float bot_cmd_select_weapon()
630 id = bot_cmd.bot_cmd_parm_float;
632 if(id < WEP_FIRST || id > WEP_LAST)
633 return CMD_STATUS_ERROR;
635 if(client_hasweapon(self, id, TRUE, FALSE))
636 self.switchweapon = id;
638 return CMD_STATUS_ERROR;
640 return CMD_STATUS_FINISHED;
643 .float bot_cmd_condition_status;
645 #define CMD_CONDITION_NONE 0
646 #define CMD_CONDITION_TRUE 1
647 #define CMD_CONDITION_FALSE 2
648 #define CMD_CONDITION_TRUE_BLOCK 4
649 #define CMD_CONDITION_FALSE_BLOCK 8
651 float bot_cmd_eval(string expr)
653 // Search for numbers
654 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
660 if(substring(expr, 0, 5)=="cvar.")
662 return cvar(substring(expr, 5, strlen(expr)));
671 return vlen(self.velocity);
673 return ((self.flagcarried!=world));
676 print(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
682 string expr, val_a, val_b;
685 if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
687 // Only one "if" block is allowed at time
688 print("ERROR: Only one conditional block can be processed at time");
689 bot_clearqueue(self);
690 return CMD_STATUS_ERROR;
693 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE_BLOCK;
695 // search for operators
696 expr = bot_cmd.bot_cmd_parm_string;
698 cmpofs = strstrofs(expr,"=",0);
702 val_a = substring(expr,0,cmpofs);
703 val_b = substring(expr,cmpofs+1,strlen(expr));
705 if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
706 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
708 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
710 return CMD_STATUS_FINISHED;
713 cmpofs = strstrofs(expr,">",0);
717 val_a = substring(expr,0,cmpofs);
718 val_b = substring(expr,cmpofs+1,strlen(expr));
720 if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
721 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
723 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
725 return CMD_STATUS_FINISHED;
728 cmpofs = strstrofs(expr,"<",0);
732 val_a = substring(expr,0,cmpofs);
733 val_b = substring(expr,cmpofs+1,strlen(expr));
735 if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
736 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
738 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
740 return CMD_STATUS_FINISHED;
743 if(bot_cmd_eval(expr))
744 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
746 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
748 return CMD_STATUS_FINISHED;
753 self.bot_cmd_condition_status &~= CMD_CONDITION_TRUE_BLOCK;
754 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE_BLOCK;
755 return CMD_STATUS_FINISHED;
760 self.bot_cmd_condition_status = CMD_CONDITION_NONE;
761 return CMD_STATUS_FINISHED;
764 float bot_cmd_resetaim()
766 self.v_angle = '0 0 0';
767 return CMD_STATUS_FINISHED;
770 .float bot_cmd_aim_begintime;
771 .float bot_cmd_aim_endtime;
772 .vector bot_cmd_aim_begin;
773 .vector bot_cmd_aim_end;
778 if(self.bot_cmd_aim_endtime)
782 progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
783 self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
785 if(time>=self.bot_cmd_aim_endtime)
787 self.bot_cmd_aim_endtime = 0;
788 return CMD_STATUS_FINISHED;
791 return CMD_STATUS_EXECUTING;
794 // New aiming direction
798 parms = bot_cmd.bot_cmd_parm_string;
800 tokens = tokenizebyseparator(parms, " ");
804 self.v_angle_x -= stof(argv(1));
805 self.v_angle_y += stof(argv(0));
806 return CMD_STATUS_FINISHED;
809 if(tokens<2||tokens>3)
810 return CMD_STATUS_ERROR;
812 step = stof(argv(2));
814 self.bot_cmd_aim_begin = self.v_angle;
816 self.bot_cmd_aim_end_x = self.v_angle_x - stof(argv(1));
817 self.bot_cmd_aim_end_y = self.v_angle_y + stof(argv(0));
818 self.bot_cmd_aim_end_z = 0;
820 self.bot_cmd_aim_begintime = time;
821 self.bot_cmd_aim_endtime = time + step;
823 return CMD_STATUS_EXECUTING;
826 float bot_cmd_aimtarget()
828 if(self.bot_cmd_aim_endtime)
830 return bot_cmd_aim();
838 parms = bot_cmd.bot_cmd_parm_string;
840 tokens = tokenizebyseparator(parms, " ");
842 e = bot_getplace(argv(0));
844 return CMD_STATUS_ERROR;
846 v = e.origin + (e.mins + e.maxs) * 0.5;
850 self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
851 self.v_angle_x = -self.v_angle_x;
852 return CMD_STATUS_FINISHED;
855 if(tokens<1||tokens>2)
856 return CMD_STATUS_ERROR;
858 step = stof(argv(1));
860 self.bot_cmd_aim_begin = self.v_angle;
861 self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
862 self.bot_cmd_aim_end_x = -self.bot_cmd_aim_end_x;
864 self.bot_cmd_aim_begintime = time;
865 self.bot_cmd_aim_endtime = time + step;
867 return CMD_STATUS_EXECUTING;
872 #define BOT_CMD_KEY_NONE 0
873 #define BOT_CMD_KEY_FORWARD 1
874 #define BOT_CMD_KEY_BACKWARD 2
875 #define BOT_CMD_KEY_RIGHT 4
876 #define BOT_CMD_KEY_LEFT 8
877 #define BOT_CMD_KEY_JUMP 16
878 #define BOT_CMD_KEY_ATTACK1 32
879 #define BOT_CMD_KEY_ATTACK2 64
880 #define BOT_CMD_KEY_USE 128
881 #define BOT_CMD_KEY_HOOK 256
882 #define BOT_CMD_KEY_CROUCH 512
883 #define BOT_CMD_KEY_CHAT 1024
885 float bot_presskeys()
887 self.movement = '0 0 0';
888 self.BUTTON_JUMP = FALSE;
889 self.BUTTON_CROUCH = FALSE;
890 self.BUTTON_ATCK = FALSE;
891 self.BUTTON_ATCK2 = FALSE;
892 self.BUTTON_USE = FALSE;
893 self.BUTTON_HOOK = FALSE;
894 self.BUTTON_CHAT = FALSE;
896 if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
899 if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
900 self.movement_x = autocvar_sv_maxspeed;
901 else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
902 self.movement_x = -autocvar_sv_maxspeed;
904 if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
905 self.movement_y = autocvar_sv_maxspeed;
906 else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
907 self.movement_y = -autocvar_sv_maxspeed;
909 if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
910 self.BUTTON_JUMP = TRUE;
912 if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
913 self.BUTTON_CROUCH = TRUE;
915 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
916 self.BUTTON_ATCK = TRUE;
918 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
919 self.BUTTON_ATCK2 = TRUE;
921 if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
922 self.BUTTON_USE = TRUE;
924 if(self.bot_cmd_keys & BOT_CMD_KEY_HOOK)
925 self.BUTTON_HOOK = TRUE;
927 if(self.bot_cmd_keys & BOT_CMD_KEY_CHAT)
928 self.BUTTON_CHAT = TRUE;
934 float bot_cmd_keypress_handler(string key, float enabled)
940 self.bot_cmd_keys = power2of(20) - 1; // >:)
942 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
946 self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
947 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
950 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
955 self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
956 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
959 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
964 self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
965 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
968 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
973 self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
974 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
977 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
981 self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
983 self.bot_cmd_keys &~= BOT_CMD_KEY_JUMP;
987 self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
989 self.bot_cmd_keys &~= BOT_CMD_KEY_CROUCH;
993 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
995 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK1;
999 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
1001 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK2;
1005 self.bot_cmd_keys |= BOT_CMD_KEY_USE;
1007 self.bot_cmd_keys &~= BOT_CMD_KEY_USE;
1011 self.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
1013 self.bot_cmd_keys &~= BOT_CMD_KEY_HOOK;
1017 self.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
1019 self.bot_cmd_keys &~= BOT_CMD_KEY_CHAT;
1025 return CMD_STATUS_FINISHED;
1028 float bot_cmd_presskey()
1032 key = bot_cmd.bot_cmd_parm_string;
1034 bot_cmd_keypress_handler(key,TRUE);
1036 return CMD_STATUS_FINISHED;
1039 float bot_cmd_releasekey()
1043 key = bot_cmd.bot_cmd_parm_string;
1045 return bot_cmd_keypress_handler(key,FALSE);
1048 float bot_cmd_pause()
1052 self.BUTTON_USE = 0;
1053 self.BUTTON_ATCK = 0;
1054 self.BUTTON_JUMP = 0;
1055 self.BUTTON_HOOK = 0;
1056 self.BUTTON_CHAT = 0;
1057 self.BUTTON_ATCK2 = 0;
1058 self.BUTTON_CROUCH = 0;
1060 self.movement = '0 0 0';
1061 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
1063 self.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1064 return CMD_STATUS_FINISHED;
1067 float bot_cmd_moveto()
1069 return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
1072 float bot_cmd_movetotarget()
1075 e = bot_getplace(bot_cmd.bot_cmd_parm_string);
1077 return CMD_STATUS_ERROR;
1078 return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
1081 float bot_cmd_resetgoal()
1083 return self.cmd_resetgoal();
1087 float bot_cmd_sound()
1090 f = bot_cmd.bot_cmd_parm_string;
1093 sound(self, CH_WEAPON_B, f, VOL_BASE, ATTN_MIN);
1095 return CMD_STATUS_FINISHED;
1099 float bot_cmd_debug_assert_canfire()
1102 f = bot_cmd.bot_cmd_parm_float;
1104 if(self.weaponentity.state != WS_READY)
1108 self.colormod = '0 8 8';
1109 print("Bot wants to fire, inhibited by weaponentity state\n");
1112 else if(ATTACK_FINISHED(self) > time)
1116 self.colormod = '8 0 8';
1117 print("Bot wants to fire, inhibited by ATTACK_FINISHED\n");
1120 else if(self.tuba_note)
1124 self.colormod = '8 0 0';
1125 print("Bot wants to fire, bot still has an active tuba note\n");
1132 self.colormod = '8 8 0';
1133 print("Bot thinks it has fired, but apparently did not\n");
1137 return CMD_STATUS_FINISHED;
1142 void bot_command_executed(float rm)
1149 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1151 self.bot_cmd_execution_index++;
1154 void bot_setcurrentcommand()
1158 if(!self.bot_cmd_current)
1160 self.bot_cmd_current = spawn();
1161 self.bot_cmd_current.classname = "bot_cmd";
1162 self.bot_cmd_current.is_bot_cmd = 1;
1165 bot_cmd = self.bot_cmd_current;
1166 if(bot_cmd.bot_cmd_index != self.bot_cmd_execution_index || self.bot_cmd_execution_index == 0)
1168 if(bot_havecommand(self, self.bot_cmd_execution_index))
1171 cmdstring = bot_readcommand(self, self.bot_cmd_execution_index);
1172 if(bot_decodecommand(cmdstring))
1174 bot_cmd.owner = self;
1175 bot_cmd.bot_cmd_index = self.bot_cmd_execution_index;
1179 // Invalid command, remove from queue
1181 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1182 self.bot_cmd_execution_index++;
1190 void bot_resetqueues()
1195 FOR_EACH_CLIENT(cl) if(cl.isbot)
1197 if(cl.bot_cmdqueuebuf_allocated)
1199 // also, cancel all barriers
1201 for(i = 0; i < cl.bot_places_count; ++i)
1203 strunzone(cl.(bot_placenames[i]));
1204 cl.(bot_placenames[i]) = string_null;
1206 cl.bot_places_count = 0;
1209 bot_barriertime = time;
1212 // Here we map commands to functions and deal with complex interactions between commands and execution states
1213 // NOTE: Of course you need to include your commands here too :)
1214 float bot_execute_commands_once()
1216 float status, ispressingkey;
1219 bot_setcurrentcommand();
1221 // if we have no bot command, better return
1222 // old logic kept pressing previously pressed keys, but that has problems
1223 // (namely, it means you cannot make a bot "normal" ever again)
1224 // to keep a bot walking for a while, use the "wait" bot command
1225 if(bot_cmd == world)
1228 // Ignore all commands except continue when the bot is paused
1229 if(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1230 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1232 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1234 bot_command_executed(TRUE);
1235 print( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1240 // Keep pressing keys raised by the "presskey" command
1241 ispressingkey = !!bot_presskeys();
1243 // Handle conditions
1244 if not(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE)
1245 if(self.bot_cmd_condition_status & CMD_CONDITION_TRUE && self.bot_cmd_condition_status & CMD_CONDITION_FALSE_BLOCK)
1247 bot_command_executed(TRUE);
1250 else if(self.bot_cmd_condition_status & CMD_CONDITION_FALSE && self.bot_cmd_condition_status & CMD_CONDITION_TRUE_BLOCK)
1252 bot_command_executed(TRUE);
1256 // Map commands to functions
1257 switch(bot_cmd.bot_cmd_type)
1260 return ispressingkey;
1263 status = bot_cmd_pause();
1265 case BOT_CMD_CONTINUE:
1266 status = bot_cmd_continue();
1269 status = bot_cmd_wait();
1271 case BOT_CMD_WAIT_UNTIL:
1272 status = bot_cmd_wait_until();
1275 status = bot_cmd_turn();
1277 case BOT_CMD_MOVETO:
1278 status = bot_cmd_moveto();
1280 case BOT_CMD_MOVETOTARGET:
1281 status = bot_cmd_movetotarget();
1283 case BOT_CMD_RESETGOAL:
1284 status = bot_cmd_resetgoal();
1287 status = bot_cmd_cc();
1290 status = bot_cmd_if();
1293 status = bot_cmd_else();
1296 status = bot_cmd_fi();
1298 case BOT_CMD_RESETAIM:
1299 status = bot_cmd_resetaim();
1302 status = bot_cmd_aim();
1304 case BOT_CMD_AIMTARGET:
1305 status = bot_cmd_aimtarget();
1307 case BOT_CMD_PRESSKEY:
1308 status = bot_cmd_presskey();
1310 case BOT_CMD_RELEASEKEY:
1311 status = bot_cmd_releasekey();
1313 case BOT_CMD_SELECTWEAPON:
1314 status = bot_cmd_select_weapon();
1316 case BOT_CMD_IMPULSE:
1317 status = bot_cmd_impulse();
1319 case BOT_CMD_BARRIER:
1320 status = bot_cmd_barrier();
1322 case BOT_CMD_CONSOLE:
1323 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1324 status = CMD_STATUS_FINISHED;
1327 status = bot_cmd_sound();
1329 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1330 status = bot_cmd_debug_assert_canfire();
1333 print(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1337 if (status==CMD_STATUS_ERROR)
1338 print(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1340 // Move execution pointer
1341 if(status==CMD_STATUS_EXECUTING)
1347 if(autocvar_g_debug_bot_commands)
1351 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1353 case BOT_CMD_PARAMETER_FLOAT:
1354 parms = ftos(bot_cmd.bot_cmd_parm_float);
1356 case BOT_CMD_PARAMETER_STRING:
1357 parms = bot_cmd.bot_cmd_parm_string;
1359 case BOT_CMD_PARAMETER_VECTOR:
1360 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1366 clientcommand(self,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1369 bot_command_executed(TRUE);
1372 if(status == CMD_STATUS_FINISHED)
1375 return CMD_STATUS_ERROR;
1378 // This function should be (the only) called directly from the bot ai loop
1379 float bot_execute_commands()
1384 f = bot_execute_commands_once();