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("readcommand 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);
24 bot.bot_cmdqueuebuf_end += 1;
27 void bot_dequeuecommand(entity bot, float idx)
29 if(!bot.bot_cmdqueuebuf_allocated)
30 error("dequeuecommand but no queue allocated");
31 if(idx < bot.bot_cmdqueuebuf_start)
32 error("dequeueing a command in the past");
33 if(idx >= bot.bot_cmdqueuebuf_end)
34 error("dequeueing a command in the future");
35 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
36 if(idx == bot.bot_cmdqueuebuf_start)
37 bot.bot_cmdqueuebuf_start += 1;
38 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
42 string bot_readcommand(entity bot, float idx)
44 if(!bot.bot_cmdqueuebuf_allocated)
45 error("readcommand but no queue allocated");
46 if(idx < bot.bot_cmdqueuebuf_start)
47 error("reading a command in the past");
48 if(idx >= bot.bot_cmdqueuebuf_end)
49 error("reading a command in the future");
50 return bufstr_get(bot.bot_cmdqueuebuf, idx);
53 float bot_havecommand(entity bot, float idx)
55 if(!bot.bot_cmdqueuebuf_allocated)
57 if(idx < bot.bot_cmdqueuebuf_start)
59 if(idx >= bot.bot_cmdqueuebuf_end)
64 #define MAX_BOT_PLACES 4
65 .float bot_places_count;
66 .entity bot_places[MAX_BOT_PLACES]; FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(bot_places);
67 .string bot_placenames[MAX_BOT_PLACES]; FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(bot_placenames);
68 entity bot_getplace(string placename)
71 if(substring(placename, 0, 1) == "@")
74 placename = substring(placename, 1, -1);
76 for(i = 0; i < self.bot_places_count; ++i)
77 if(self.(bot_placenames[i]) == placename)
78 return self.(bot_places[i]);
79 // now: i == self.bot_places_count
80 s = s2 = cvar_string(placename);
81 p = strstrofs(s2, " ", 0);
84 s = substring(s2, 0, p);
85 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
86 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
87 //print("places: ", placename, " := ", cvar_string(placename), "\n");
89 e = find(world, targetname, s);
91 print("invalid place ", s, "\n");
92 if(i < MAX_BOT_PLACES)
94 self.(bot_placenames[i]) = strzone(placename);
95 self.(bot_places[i]) = e;
96 self.bot_places_count += 1;
102 e = find(world, targetname, placename);
104 print("invalid place ", s, "\n");
110 // NOTE: New commands should be added here. Do not forget to update BOT_CMD_COUNTER
111 #define BOT_CMD_NULL 0
112 #define BOT_CMD_PAUSE 1
113 #define BOT_CMD_CONTINUE 2
114 #define BOT_CMD_WAIT 3
115 #define BOT_CMD_TURN 4
116 #define BOT_CMD_MOVETO 5
117 #define BOT_CMD_RESETGOAL 6 // Not implemented yet
120 #define BOT_CMD_ELSE 9
121 #define BOT_CMD_FI 10
122 #define BOT_CMD_RESETAIM 11
123 #define BOT_CMD_AIM 12
124 #define BOT_CMD_PRESSKEY 13
125 #define BOT_CMD_RELEASEKEY 14
126 #define BOT_CMD_SELECTWEAPON 15
127 #define BOT_CMD_IMPULSE 16
128 #define BOT_CMD_WAIT_UNTIL 17
129 #define BOT_CMD_MOVETOTARGET 18
130 #define BOT_CMD_AIMTARGET 19
131 #define BOT_CMD_BARRIER 20
132 #define BOT_CMD_CONSOLE 21
133 #define BOT_CMD_WHILE 22 // TODO: Not implemented yet
134 #define BOT_CMD_WEND 23 // TODO: Not implemented yet
135 #define BOT_CMD_CHASE 24 // TODO: Not implemented yet
137 #define BOT_CMD_COUNTER 22 // Update this value if you add/remove a command
139 // NOTE: Following commands should be implemented on the bot ai
140 // If a new command should be handled by the target ai(s) please declare it here
141 .float(vector) cmd_moveto;
142 .float() cmd_resetgoal;
145 #define BOT_CMD_PARAMETER_NONE 0
146 #define BOT_CMD_PARAMETER_FLOAT 1
147 #define BOT_CMD_PARAMETER_STRING 2
148 #define BOT_CMD_PARAMETER_VECTOR 3
150 float bot_cmds_initialized;
151 float bot_cmd_parm_type[BOT_CMD_COUNTER];
152 string bot_cmd_string[BOT_CMD_COUNTER];
154 // Bots command queue
155 entity bot_cmd; // global current command
156 .entity bot_cmd_current; // current command of this bot
158 .float is_bot_cmd; // Tells if the entity is a bot command
159 .float bot_cmd_index; // Position of the command in the queue
160 .float bot_cmd_type; // If of command (see the BOT_CMD_* defines)
161 .float bot_cmd_parm_float; // Field to store a float parameter
162 .string bot_cmd_parm_string; // Field to store a string parameter
163 .vector bot_cmd_parm_vector; // Field to store a vector parameter
165 float bot_barriertime;
168 .float bot_cmd_execution_index; // Position in the queue of the command to be executed
170 // Initialize global commands list
171 // NOTE: New commands should be initialized here
172 void bot_commands_init()
174 bot_cmd_string[BOT_CMD_NULL] = "";
175 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
177 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
178 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
180 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
181 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
183 bot_cmd_string[BOT_CMD_WAIT] = "wait";
184 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
186 bot_cmd_string[BOT_CMD_TURN] = "turn";
187 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
189 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
190 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
192 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
193 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
195 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
196 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
198 bot_cmd_string[BOT_CMD_CC] = "cc";
199 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
201 bot_cmd_string[BOT_CMD_IF] = "if";
202 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
204 bot_cmd_string[BOT_CMD_ELSE] = "else";
205 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
207 bot_cmd_string[BOT_CMD_FI] = "fi";
208 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
210 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
211 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
213 bot_cmd_string[BOT_CMD_AIM] = "aim";
214 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
216 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
217 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
219 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
220 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
222 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
223 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
225 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
226 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
228 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
229 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
231 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
232 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
234 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
235 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
237 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
238 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
240 bot_cmds_initialized = TRUE;
243 // Returns first bot with matching name
244 entity find_bot_by_name(string name)
248 bot = findchainflags(flags, FL_CLIENT);
251 if(clienttype(bot) == CLIENTTYPE_BOT)
252 if(bot.netname==name)
261 // Returns a bot by number on list
262 entity find_bot_by_number(float number)
270 bot = findchainflags(flags, FL_CLIENT);
273 if(clienttype(bot) == CLIENTTYPE_BOT)
284 float bot_decodecommand(string cmdstring)
286 local float cmd_parm_type, i;
290 sp = strstrofs(cmdstring, " ", 0);
297 parm = substring(cmdstring, sp + 1, -1);
298 cmdstring = substring(cmdstring, 0, sp);
301 if(!bot_cmds_initialized)
304 for(i=1;i<BOT_CMD_COUNTER;++i)
306 if(bot_cmd_string[i]!=cmdstring)
309 cmd_parm_type = bot_cmd_parm_type[i];
311 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
313 print("ERROR: A parameter is required for this command\n");
317 // Load command into queue
318 bot_cmd.bot_cmd_type = i;
321 switch(cmd_parm_type)
323 case BOT_CMD_PARAMETER_FLOAT:
324 bot_cmd.bot_cmd_parm_float = stof(parm);
326 case BOT_CMD_PARAMETER_STRING:
327 if(bot_cmd.bot_cmd_parm_string)
328 strunzone(bot_cmd.bot_cmd_parm_string);
329 bot_cmd.bot_cmd_parm_string = strzone(parm);
331 case BOT_CMD_PARAMETER_VECTOR:
332 bot_cmd.bot_cmd_parm_vector = stov(parm);
339 print("ERROR: No such command '", cmdstring, "'\n");
343 void bot_cmdhelp(string scmd)
345 local float i, ntype;
348 if(!bot_cmds_initialized)
351 for(i=1;i<BOT_CMD_COUNTER;++i)
353 if(bot_cmd_string[i]!=scmd)
356 ntype = bot_cmd_parm_type[i];
360 case BOT_CMD_PARAMETER_FLOAT:
361 stype = "float number";
363 case BOT_CMD_PARAMETER_STRING:
366 case BOT_CMD_PARAMETER_VECTOR:
374 print(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
376 print("Description: ");
380 print("Stops the bot completely. Any command other than 'continue' will be ignored.");
382 case BOT_CMD_CONTINUE:
383 print("Disable paused status");
386 print("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
388 case BOT_CMD_WAIT_UNTIL:
389 print("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
391 case BOT_CMD_BARRIER:
392 print("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
395 print("Look to the right or left N degrees. For turning to the left use positive numbers.");
398 print("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
400 case BOT_CMD_MOVETOTARGET:
401 print("Walk to the specific target on the map");
403 case BOT_CMD_RESETGOAL:
404 print("Resets the goal stack");
407 print("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
410 print("Perform simple conditional execution.\n");
412 print(" sv_cmd .. if \"condition\"\n");
413 print(" sv_cmd .. <instruction if true>\n");
414 print(" sv_cmd .. <instruction if true>\n");
415 print(" sv_cmd .. else\n");
416 print(" sv_cmd .. <instruction if false>\n");
417 print(" sv_cmd .. <instruction if false>\n");
418 print(" sv_cmd .. fi\n");
419 print("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
420 print(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
421 print("Fields: health, speed, flagcarrier\n");
422 print("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
424 case BOT_CMD_RESETAIM:
425 print("Points the aim to the coordinates x,y 0,0");
428 print("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
429 print("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
430 print("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
431 print(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
433 case BOT_CMD_AIMTARGET:
434 print("Points the aim to given target");
436 case BOT_CMD_PRESSKEY:
437 print("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
438 print("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
439 print("Note: The script will not return the control to the bot ai until all keys are released");
441 case BOT_CMD_RELEASEKEY:
442 print("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
445 print("This command has no description yet.");
452 void bot_list_commands()
457 if(!bot_cmds_initialized)
460 print("List of all available commands:\n");
461 print(" Command\t\t\t\tParameter Type\n");
463 for(i=1;i<BOT_CMD_COUNTER;++i)
465 switch(bot_cmd_parm_type[i])
467 case BOT_CMD_PARAMETER_FLOAT:
468 ptype = "float number";
470 case BOT_CMD_PARAMETER_STRING:
473 case BOT_CMD_PARAMETER_VECTOR:
480 print(strcat(" ",bot_cmd_string[i],"\t\t\t\t<",ptype,"> \n"));
485 .float bot_exec_status;
487 #define BOT_EXEC_STATUS_IDLE 0
488 #define BOT_EXEC_STATUS_PAUSED 1
489 #define BOT_EXEC_STATUS_WAITING 2
491 #define CMD_STATUS_EXECUTING 0
492 #define CMD_STATUS_FINISHED 1
493 #define CMD_STATUS_ERROR 2
495 void SV_ParseClientCommand(string s);
498 SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
499 return CMD_STATUS_FINISHED;
502 float bot_cmd_impulse()
504 self.impulse = bot_cmd.bot_cmd_parm_float;
505 return CMD_STATUS_FINISHED;
508 float bot_cmd_continue()
510 self.bot_exec_status &~= BOT_EXEC_STATUS_PAUSED;
511 return CMD_STATUS_FINISHED;
514 .float bot_cmd_wait_time;
517 if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
519 if(time>=self.bot_cmd_wait_time)
521 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
522 return CMD_STATUS_FINISHED;
525 return CMD_STATUS_EXECUTING;
528 self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
529 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
530 return CMD_STATUS_EXECUTING;
533 float bot_cmd_wait_until()
535 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
537 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
538 return CMD_STATUS_EXECUTING;
540 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
541 return CMD_STATUS_FINISHED;
544 float bot_cmd_barrier()
548 // 0 = no barrier, 1 = waiting, 2 = waiting finished
550 if(self.bot_barrier == 0) // initialization
552 self.bot_barrier = 1;
554 //self.colormod = '4 4 0';
557 if(self.bot_barrier == 1) // find other bots
559 FOR_EACH_CLIENT(cl) if(cl.isbot)
561 if(cl.bot_cmdqueuebuf_allocated)
562 if(cl.bot_barrier != 1)
563 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
566 // all bots hit the barrier!
567 FOR_EACH_CLIENT(cl) if(cl.isbot)
569 cl.bot_barrier = 2; // acknowledge barrier
572 bot_barriertime = time;
575 // if we get here, the barrier is finished
577 self.bot_barrier = 0;
578 //self.colormod = '0 0 0';
580 return CMD_STATUS_FINISHED;
585 self.v_angle_y = self.v_angle_y + bot_cmd.bot_cmd_parm_float;
586 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
587 return CMD_STATUS_FINISHED;
590 float bot_cmd_select_weapon()
594 id = bot_cmd.bot_cmd_parm_float;
596 if(id < WEP_FIRST || id > WEP_LAST)
597 return CMD_STATUS_ERROR;
599 if(client_hasweapon(self, id, TRUE, FALSE))
600 self.switchweapon = id;
602 return CMD_STATUS_ERROR;
604 return CMD_STATUS_FINISHED;
607 .float bot_cmd_condition_status;
609 #define CMD_CONDITION_NONE 0
610 #define CMD_CONDITION_TRUE 1
611 #define CMD_CONDITION_FALSE 2
612 #define CMD_CONDITION_TRUE_BLOCK 4
613 #define CMD_CONDITION_FALSE_BLOCK 8
615 float bot_cmd_eval(string expr)
617 // Search for numbers
618 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
624 if(substring(expr, 0, 5)=="cvar.")
626 return cvar(substring(expr, 5, strlen(expr)));
635 return vlen(self.velocity);
637 return ((self.flagcarried!=world));
640 print(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
646 local string expr, val_a, val_b;
649 if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
651 // Only one "if" block is allowed at time
652 print("ERROR: Only one conditional block can be processed at time");
653 bot_clearqueue(self);
654 return CMD_STATUS_ERROR;
657 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE_BLOCK;
659 // search for operators
660 expr = bot_cmd.bot_cmd_parm_string;
662 cmpofs = strstrofs(expr,"=",0);
666 val_a = substring(expr,0,cmpofs);
667 val_b = substring(expr,cmpofs+1,strlen(expr));
669 if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
670 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
672 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
674 return CMD_STATUS_FINISHED;
677 cmpofs = strstrofs(expr,">",0);
681 val_a = substring(expr,0,cmpofs);
682 val_b = substring(expr,cmpofs+1,strlen(expr));
684 if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
685 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
687 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
689 return CMD_STATUS_FINISHED;
692 cmpofs = strstrofs(expr,"<",0);
696 val_a = substring(expr,0,cmpofs);
697 val_b = substring(expr,cmpofs+1,strlen(expr));
699 if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
700 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
702 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
704 return CMD_STATUS_FINISHED;
707 if(bot_cmd_eval(expr))
708 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
710 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
712 return CMD_STATUS_FINISHED;
717 self.bot_cmd_condition_status &~= CMD_CONDITION_TRUE_BLOCK;
718 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE_BLOCK;
719 return CMD_STATUS_FINISHED;
724 self.bot_cmd_condition_status = CMD_CONDITION_NONE;
725 return CMD_STATUS_FINISHED;
728 float bot_cmd_resetaim()
730 self.v_angle = '0 0 0';
731 return CMD_STATUS_FINISHED;
734 .float bot_cmd_aim_begintime;
735 .float bot_cmd_aim_endtime;
736 .vector bot_cmd_aim_begin;
737 .vector bot_cmd_aim_end;
742 if(self.bot_cmd_aim_endtime)
744 local float progress;
746 progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
747 self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
749 if(time>=self.bot_cmd_aim_endtime)
751 self.bot_cmd_aim_endtime = 0;
752 return CMD_STATUS_FINISHED;
755 return CMD_STATUS_EXECUTING;
758 // New aiming direction
760 local float tokens, step;
762 parms = bot_cmd.bot_cmd_parm_string;
764 tokens = tokenizebyseparator(parms, " ");
768 self.v_angle_x -= stof(argv(1));
769 self.v_angle_y += stof(argv(0));
770 return CMD_STATUS_FINISHED;
773 if(tokens<2||tokens>3)
774 return CMD_STATUS_ERROR;
776 step = stof(argv(2));
778 self.bot_cmd_aim_begin = self.v_angle;
780 self.bot_cmd_aim_end_x = self.v_angle_x - stof(argv(1));
781 self.bot_cmd_aim_end_y = self.v_angle_y + stof(argv(0));
782 self.bot_cmd_aim_end_z = 0;
784 self.bot_cmd_aim_begintime = time;
785 self.bot_cmd_aim_endtime = time + step;
787 return CMD_STATUS_EXECUTING;
790 float bot_cmd_aimtarget()
792 if(self.bot_cmd_aim_endtime)
794 return bot_cmd_aim();
800 local float tokens, step;
802 parms = bot_cmd.bot_cmd_parm_string;
804 tokens = tokenizebyseparator(parms, " ");
806 e = bot_getplace(argv(0));
808 return CMD_STATUS_ERROR;
810 v = e.origin + (e.mins + e.maxs) * 0.5;
814 self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
815 self.v_angle_x = -self.v_angle_x;
816 return CMD_STATUS_FINISHED;
819 if(tokens<1||tokens>2)
820 return CMD_STATUS_ERROR;
822 step = stof(argv(1));
824 self.bot_cmd_aim_begin = self.v_angle;
825 self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
826 self.bot_cmd_aim_end_x = -self.bot_cmd_aim_end_x;
828 self.bot_cmd_aim_begintime = time;
829 self.bot_cmd_aim_endtime = time + step;
831 return CMD_STATUS_EXECUTING;
836 #define BOT_CMD_KEY_NONE 0
837 #define BOT_CMD_KEY_FORWARD 1
838 #define BOT_CMD_KEY_BACKWARD 2
839 #define BOT_CMD_KEY_RIGHT 4
840 #define BOT_CMD_KEY_LEFT 8
841 #define BOT_CMD_KEY_JUMP 16
842 #define BOT_CMD_KEY_ATTACK1 32
843 #define BOT_CMD_KEY_ATTACK2 64
844 #define BOT_CMD_KEY_USE 128
845 #define BOT_CMD_KEY_HOOK 256
846 #define BOT_CMD_KEY_CROUCH 512
847 #define BOT_CMD_KEY_CHAT 1024
849 float bot_presskeys()
851 self.movement = '0 0 0';
853 if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
856 if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
857 self.movement_x = cvar("sv_maxspeed");
858 else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
859 self.movement_x = -cvar("sv_maxspeed");
861 if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
862 self.movement_y = cvar("sv_maxspeed");
863 else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
864 self.movement_y = -cvar("sv_maxspeed");
866 if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
867 self.BUTTON_JUMP = TRUE;
869 if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
870 self.BUTTON_CROUCH = TRUE;
872 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
873 self.BUTTON_ATCK = TRUE;
875 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
876 self.BUTTON_ATCK2 = TRUE;
878 if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
879 self.BUTTON_USE = TRUE;
881 if(self.bot_cmd_keys & BOT_CMD_KEY_HOOK)
882 self.BUTTON_HOOK = TRUE;
884 if(self.bot_cmd_keys & BOT_CMD_KEY_CHAT)
885 self.BUTTON_CHAT = TRUE;
891 float bot_cmd_keypress_handler(string key, float enabled)
897 self.bot_cmd_keys = power2of(20) - 1; // >:)
899 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
903 self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
904 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
907 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
912 self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
913 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
916 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
921 self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
922 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
925 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
930 self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
931 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
934 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
938 self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
940 self.bot_cmd_keys &~= BOT_CMD_KEY_JUMP;
944 self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
946 self.bot_cmd_keys &~= BOT_CMD_KEY_CROUCH;
950 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
952 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK1;
956 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
958 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK2;
962 self.bot_cmd_keys |= BOT_CMD_KEY_USE;
964 self.bot_cmd_keys &~= BOT_CMD_KEY_USE;
968 self.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
970 self.bot_cmd_keys &~= BOT_CMD_KEY_HOOK;
974 self.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
976 self.bot_cmd_keys &~= BOT_CMD_KEY_CHAT;
982 return CMD_STATUS_FINISHED;
985 float bot_cmd_presskey()
989 key = bot_cmd.bot_cmd_parm_string;
991 bot_cmd_keypress_handler(key,TRUE);
993 return CMD_STATUS_FINISHED;
996 float bot_cmd_releasekey()
1000 key = bot_cmd.bot_cmd_parm_string;
1002 return bot_cmd_keypress_handler(key,FALSE);
1005 float bot_cmd_pause()
1009 self.BUTTON_USE = 0;
1010 self.BUTTON_ATCK = 0;
1011 self.BUTTON_JUMP = 0;
1012 self.BUTTON_HOOK = 0;
1013 self.BUTTON_CHAT = 0;
1014 self.BUTTON_ATCK2 = 0;
1015 self.BUTTON_CROUCH = 0;
1017 self.movement = '0 0 0';
1018 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
1020 self.bot_exec_status = self.bot_exec_status | BOT_EXEC_STATUS_PAUSED;
1021 return CMD_STATUS_FINISHED;
1024 float bot_cmd_moveto()
1026 return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
1029 float bot_cmd_movetotarget()
1032 e = bot_getplace(bot_cmd.bot_cmd_parm_string);
1034 return CMD_STATUS_ERROR;
1035 return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
1038 float bot_cmd_resetgoal()
1040 return self.cmd_resetgoal();
1045 void bot_command_executed(float rm)
1052 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1054 self.bot_cmd_execution_index++;
1057 void bot_setcurrentcommand()
1061 if(!self.bot_cmd_current)
1063 self.bot_cmd_current = spawn();
1064 self.bot_cmd_current.classname = "bot_cmd";
1065 self.bot_cmd_current.is_bot_cmd = 1;
1068 bot_cmd = self.bot_cmd_current;
1069 if(bot_cmd.bot_cmd_index != self.bot_cmd_execution_index || self.bot_cmd_execution_index == 0)
1071 if(bot_havecommand(self, self.bot_cmd_execution_index))
1074 cmdstring = bot_readcommand(self, self.bot_cmd_execution_index);
1075 if(bot_decodecommand(cmdstring))
1077 bot_cmd.owner = self;
1078 bot_cmd.bot_cmd_index = self.bot_cmd_execution_index;
1082 // Invalid command, remove from queue
1084 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1085 self.bot_cmd_execution_index++;
1093 void bot_resetqueues()
1098 FOR_EACH_CLIENT(cl) if(cl.isbot)
1100 if(cl.bot_cmdqueuebuf_allocated)
1102 // also, cancel all barriers
1104 for(i = 0; i < cl.bot_places_count; ++i)
1106 strunzone(cl.(bot_placenames[i]));
1107 cl.(bot_placenames[i]) = string_null;
1109 cl.bot_places_count = 0;
1112 bot_barriertime = time;
1115 // Here we map commands to functions and deal with complex interactions between commands and execution states
1116 // NOTE: Of course you need to include your commands here too :)
1117 float bot_execute_commands_once()
1119 local float status, ispressingkey;
1121 if(self.deadflag!=DEAD_NO)
1125 bot_setcurrentcommand();
1127 // Keep pressing keys raised by the "presskey" command
1128 ispressingkey = !!bot_presskeys();
1131 return ispressingkey;
1133 // Ignore all commands except continue when the bot is paused
1134 if(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1135 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1137 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1139 bot_command_executed(TRUE);
1140 print( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1145 // Handle conditions
1146 if not(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE)
1147 if(self.bot_cmd_condition_status & CMD_CONDITION_TRUE && self.bot_cmd_condition_status & CMD_CONDITION_FALSE_BLOCK)
1149 bot_command_executed(TRUE);
1152 else if(self.bot_cmd_condition_status & CMD_CONDITION_FALSE && self.bot_cmd_condition_status & CMD_CONDITION_TRUE_BLOCK)
1154 bot_command_executed(TRUE);
1158 // Map commands to functions
1159 switch(bot_cmd.bot_cmd_type)
1162 return ispressingkey;
1165 status = bot_cmd_pause();
1167 case BOT_CMD_CONTINUE:
1168 status = bot_cmd_continue();
1171 status = bot_cmd_wait();
1173 case BOT_CMD_WAIT_UNTIL:
1174 status = bot_cmd_wait_until();
1177 status = bot_cmd_turn();
1179 case BOT_CMD_MOVETO:
1180 status = bot_cmd_moveto();
1182 case BOT_CMD_MOVETOTARGET:
1183 status = bot_cmd_movetotarget();
1185 case BOT_CMD_RESETGOAL:
1186 status = bot_cmd_resetgoal();
1189 status = bot_cmd_cc();
1192 status = bot_cmd_if();
1195 status = bot_cmd_else();
1198 status = bot_cmd_fi();
1200 case BOT_CMD_RESETAIM:
1201 status = bot_cmd_resetaim();
1204 status = bot_cmd_aim();
1206 case BOT_CMD_AIMTARGET:
1207 status = bot_cmd_aimtarget();
1209 case BOT_CMD_PRESSKEY:
1210 status = bot_cmd_presskey();
1212 case BOT_CMD_RELEASEKEY:
1213 status = bot_cmd_releasekey();
1215 case BOT_CMD_SELECTWEAPON:
1216 status = bot_cmd_select_weapon();
1218 case BOT_CMD_IMPULSE:
1219 status = bot_cmd_impulse();
1221 case BOT_CMD_BARRIER:
1222 status = bot_cmd_barrier();
1224 case BOT_CMD_CONSOLE:
1225 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1226 status = CMD_STATUS_FINISHED;
1229 print(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1233 if (status==CMD_STATUS_ERROR)
1234 print(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1236 // Move execution pointer
1237 if(status==CMD_STATUS_EXECUTING)
1243 if(cvar("g_debug_bot_commands"))
1247 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1249 case BOT_CMD_PARAMETER_FLOAT:
1250 parms = ftos(bot_cmd.bot_cmd_parm_float);
1252 case BOT_CMD_PARAMETER_STRING:
1253 parms = bot_cmd.bot_cmd_parm_string;
1255 case BOT_CMD_PARAMETER_VECTOR:
1256 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1262 clientcommand(self,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1265 bot_command_executed(TRUE);
1268 if(status == CMD_STATUS_FINISHED)
1271 return CMD_STATUS_ERROR;
1274 // This function should be (the only) called directly from the bot ai loop
1275 float bot_execute_commands()
1280 f = bot_execute_commands_once();