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 print("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_start = 0;
21 bot.bot_cmdqueuebuf_end = 0;
22 bot.bot_cmdqueuebuf_allocated = TRUE;
25 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
26 bot.bot_cmdqueuebuf_end += 1;
29 void bot_dequeuecommand(entity bot, float idx)
31 if(!bot.bot_cmdqueuebuf_allocated)
32 error("dequeuecommand but no queue allocated");
33 if(idx < bot.bot_cmdqueuebuf_start)
34 error("dequeueing a command in the past");
35 if(idx >= bot.bot_cmdqueuebuf_end)
36 error("dequeueing a command in the future");
37 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
38 if(idx == bot.bot_cmdqueuebuf_start)
39 bot.bot_cmdqueuebuf_start += 1;
40 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
44 string bot_readcommand(entity bot, float idx)
46 if(!bot.bot_cmdqueuebuf_allocated)
47 error("readcommand but no queue allocated");
48 if(idx < bot.bot_cmdqueuebuf_start)
49 error("reading a command in the past");
50 if(idx >= bot.bot_cmdqueuebuf_end)
51 error("reading a command in the future");
52 return bufstr_get(bot.bot_cmdqueuebuf, idx);
55 float bot_havecommand(entity bot, float idx)
57 if(!bot.bot_cmdqueuebuf_allocated)
59 if(idx < bot.bot_cmdqueuebuf_start)
61 if(idx >= bot.bot_cmdqueuebuf_end)
66 #define MAX_BOT_PLACES 4
67 .float bot_places_count;
68 .entity bot_places[MAX_BOT_PLACES];
69 .string bot_placenames[MAX_BOT_PLACES];
70 entity bot_getplace(string placename)
73 if(substring(placename, 0, 1) == "$")
76 placename = substring(placename, 1, -1);
78 for(i = 0; i < self.bot_places_count; ++i)
79 if(self.(bot_placenames[i]) == placename)
80 return self.(bot_places[i]);
81 // now: i == self.bot_places_count
82 s = s2 = cvar_string(placename);
83 p = strstrofs(s2, " ", 0);
86 s = substring(s2, 0, p);
87 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
88 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
89 //print("places: ", placename, " := ", cvar_string(placename), "\n");
91 e = find(world, targetname, s);
93 print("invalid place ", s, "\n");
94 if(i < MAX_BOT_PLACES)
96 self.(bot_placenames[i]) = strzone(placename);
97 self.(bot_places[i]) = e;
98 self.bot_places_count += 1;
104 e = find(world, targetname, placename);
106 print("invalid place ", s, "\n");
112 // NOTE: New commands should be added here. Do not forget to update BOT_CMD_COUNTER
113 #define BOT_CMD_NULL 0
114 #define BOT_CMD_PAUSE 1
115 #define BOT_CMD_CONTINUE 2
116 #define BOT_CMD_WAIT 3
117 #define BOT_CMD_TURN 4
118 #define BOT_CMD_MOVETO 5 // Not implemented yet
119 #define BOT_CMD_RESETGOAL 6 // Not implemented yet
122 #define BOT_CMD_ELSE 9
123 #define BOT_CMD_FI 10
124 #define BOT_CMD_RESETAIM 11
125 #define BOT_CMD_AIM 12
126 #define BOT_CMD_PRESSKEY 13
127 #define BOT_CMD_RELEASEKEY 14
128 #define BOT_CMD_SELECTWEAPON 15
129 #define BOT_CMD_IMPULSE 16
130 #define BOT_CMD_WAIT_UNTIL 17
131 #define BOT_CMD_MOVETOTARGET 18
132 #define BOT_CMD_AIMTARGET 19
133 #define BOT_CMD_BARRIER 20
134 #define BOT_CMD_CONSOLE 21
135 #define BOT_CMD_WHILE 22 // TODO: Not implemented yet
136 #define BOT_CMD_WEND 23 // TODO: Not implemented yet
137 #define BOT_CMD_CHASE 24 // TODO: Not implemented yet
139 #define BOT_CMD_COUNTER 22 // Update this value if you add/remove a command
141 // NOTE: Following commands should be implemented on the bot ai
142 // If a new command should be handled by the target ai(s) please declare it here
143 .float(vector) cmd_moveto;
144 .float() cmd_resetgoal;
147 #define BOT_CMD_PARAMETER_NONE 0
148 #define BOT_CMD_PARAMETER_FLOAT 1
149 #define BOT_CMD_PARAMETER_STRING 2
150 #define BOT_CMD_PARAMETER_VECTOR 3
152 float bot_cmds_initialized;
153 float bot_cmd_parm_type[BOT_CMD_COUNTER];
154 string bot_cmd_string[BOT_CMD_COUNTER];
156 // Bots command queue
157 entity bot_cmd; // global current command
158 .entity bot_cmd_current; // current command of this bot
160 .float is_bot_cmd; // Tells if the entity is a bot command
161 .float bot_cmd_index; // Position of the command in the queue
162 .float bot_cmd_type; // If of command (see the BOT_CMD_* defines)
163 .float bot_cmd_parm_float; // Field to store a float parameter
164 .string bot_cmd_parm_string; // Field to store a string parameter
165 .vector bot_cmd_parm_vector; // Field to store a vector parameter
167 float bot_barriertime;
170 .float bot_cmd_execution_index; // Position in the queue of the command to be executed
172 // Initialize global commands list
173 // NOTE: New commands should be initialized here
174 void bot_commands_init()
176 bot_cmd_string[BOT_CMD_NULL] = "";
177 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
179 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
180 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
182 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
183 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
185 bot_cmd_string[BOT_CMD_WAIT] = "wait";
186 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
188 bot_cmd_string[BOT_CMD_TURN] = "turn";
189 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
191 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
192 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
194 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
195 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
197 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
198 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
200 bot_cmd_string[BOT_CMD_CC] = "cc";
201 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
203 bot_cmd_string[BOT_CMD_IF] = "if";
204 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
206 bot_cmd_string[BOT_CMD_ELSE] = "else";
207 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
209 bot_cmd_string[BOT_CMD_FI] = "fi";
210 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
212 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
213 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
215 bot_cmd_string[BOT_CMD_AIM] = "aim";
216 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
218 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
219 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
221 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
222 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
224 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
225 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
227 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
228 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
230 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
231 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
233 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
234 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
236 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
237 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
239 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
240 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
242 bot_cmds_initialized = TRUE;
245 // Returns first bot with matching name
246 entity find_bot_by_name(string name)
250 bot = findchainflags(flags, FL_CLIENT);
253 if(clienttype(bot) == CLIENTTYPE_BOT)
254 if(bot.netname==name)
263 // Returns a bot by number on list
264 entity find_bot_by_number(float number)
269 bot = findchainflags(flags, FL_CLIENT);
272 if(clienttype(bot) == CLIENTTYPE_BOT)
283 float bot_decodecommand(string cmdstring)
285 local float cmd_parm_type, i;
289 sp = strstrofs(cmdstring, " ", 0);
296 parm = substring(cmdstring, sp + 1, -1);
297 cmdstring = substring(cmdstring, 0, sp);
300 if(!bot_cmds_initialized)
303 for(i=1;i<BOT_CMD_COUNTER;++i)
305 if(bot_cmd_string[i]!=cmdstring)
308 cmd_parm_type = bot_cmd_parm_type[i];
310 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
312 print("ERROR: A parameter is required for this command\n");
316 // Load command into queue
317 bot_cmd.bot_cmd_type = i;
320 switch(cmd_parm_type)
322 case BOT_CMD_PARAMETER_FLOAT:
323 bot_cmd.bot_cmd_parm_float = stof(parm);
325 case BOT_CMD_PARAMETER_STRING:
326 if(bot_cmd.bot_cmd_parm_string)
327 strunzone(bot_cmd.bot_cmd_parm_string);
328 bot_cmd.bot_cmd_parm_string = strzone(parm);
330 case BOT_CMD_PARAMETER_VECTOR:
331 bot_cmd.bot_cmd_parm_vector = stov(parm);
338 print("ERROR: No such command '", cmdstring, "'\n");
342 void bot_cmdhelp(string scmd)
344 local float i, ntype;
347 if(!bot_cmds_initialized)
350 for(i=1;i<BOT_CMD_COUNTER;++i)
352 if(bot_cmd_string[i]!=scmd)
355 ntype = bot_cmd_parm_type[i];
359 case BOT_CMD_PARAMETER_FLOAT:
360 stype = "float number";
362 case BOT_CMD_PARAMETER_STRING:
365 case BOT_CMD_PARAMETER_VECTOR:
373 print(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
375 print("Description: ");
379 print("Stops the bot completely. Any command other than 'continue' will be ignored.");
381 case BOT_CMD_CONTINUE:
382 print("Disable paused status");
385 print("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
387 case BOT_CMD_WAIT_UNTIL:
388 print("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
390 case BOT_CMD_BARRIER:
391 print("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
394 print("Look to the right or left N degrees. For turning to the left use positive numbers.");
397 print("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
399 case BOT_CMD_MOVETOTARGET:
400 print("Walk to the specific target on the map");
402 case BOT_CMD_RESETGOAL:
403 print("Resets the goal stack");
406 print("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
409 print("Perform simple conditional execution.\n");
411 print(" sv_cmd .. if \"condition\"\n");
412 print(" sv_cmd .. <instruction if true>\n");
413 print(" sv_cmd .. <instruction if true>\n");
414 print(" sv_cmd .. else\n");
415 print(" sv_cmd .. <instruction if false>\n");
416 print(" sv_cmd .. <instruction if false>\n");
417 print(" sv_cmd .. fi\n");
418 print("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
419 print(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
420 print("Fields: health, speed, flagcarrier\n");
421 print("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
423 case BOT_CMD_RESETAIM:
424 print("Points the aim to the coordinates x,y 0,0");
427 print("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
428 print("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
429 print("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
430 print(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
432 case BOT_CMD_AIMTARGET:
433 print("Points the aim to given target");
435 case BOT_CMD_PRESSKEY:
436 print("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
437 print("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
438 print("Note: The script will not return the control to the bot ai until all keys are released");
440 case BOT_CMD_RELEASEKEY:
441 print("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
444 print("This command has no description yet.");
451 void bot_list_commands()
456 if(!bot_cmds_initialized)
459 print("List of all available commands:\n");
460 print(" Command\t\t\t\tParameter Type\n");
462 for(i=1;i<BOT_CMD_COUNTER;++i)
464 switch(bot_cmd_parm_type[i])
466 case BOT_CMD_PARAMETER_FLOAT:
467 ptype = "float number";
469 case BOT_CMD_PARAMETER_STRING:
472 case BOT_CMD_PARAMETER_VECTOR:
479 print(strcat(" ",bot_cmd_string[i],"\t\t\t\t<",ptype,"> \n"));
484 .float bot_exec_status;
486 #define BOT_EXEC_STATUS_IDLE 0
487 #define BOT_EXEC_STATUS_PAUSED 1
488 #define BOT_EXEC_STATUS_WAITING 2
490 #define CMD_STATUS_EXECUTING 0
491 #define CMD_STATUS_FINISHED 1
492 #define CMD_STATUS_ERROR 2
494 void SV_ParseClientCommand(string s);
497 SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string);
498 return CMD_STATUS_FINISHED;
501 float bot_cmd_impulse()
503 self.impulse = bot_cmd.bot_cmd_parm_float;
504 return CMD_STATUS_FINISHED;
507 float bot_cmd_continue()
509 self.bot_exec_status &~= BOT_EXEC_STATUS_PAUSED;
510 return CMD_STATUS_FINISHED;
513 .float bot_cmd_wait_time;
516 if(self.bot_exec_status & BOT_EXEC_STATUS_WAITING)
518 if(time>=self.bot_cmd_wait_time)
520 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
521 return CMD_STATUS_FINISHED;
524 return CMD_STATUS_EXECUTING;
527 self.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
528 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
529 return CMD_STATUS_EXECUTING;
532 float bot_cmd_wait_until()
534 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
536 self.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
537 return CMD_STATUS_EXECUTING;
539 self.bot_exec_status &~= BOT_EXEC_STATUS_WAITING;
540 return CMD_STATUS_FINISHED;
543 float bot_cmd_barrier()
547 // 0 = no barrier, 1 = waiting, 2 = waiting finished
549 if(self.bot_barrier == 0) // initialization
551 self.bot_barrier = 1;
553 //self.colormod = '4 4 0';
556 if(self.bot_barrier == 1) // find other bots
558 FOR_EACH_CLIENT(cl) if(cl.isbot)
560 if(cl.bot_cmdqueuebuf_allocated)
561 if(cl.bot_barrier != 1)
562 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
565 // all bots hit the barrier!
566 FOR_EACH_CLIENT(cl) if(cl.isbot)
568 cl.bot_barrier = 2; // acknowledge barrier
571 bot_barriertime = time;
574 // if we get here, the barrier is finished
576 self.bot_barrier = 0;
577 //self.colormod = '0 0 0';
579 return CMD_STATUS_FINISHED;
584 self.v_angle_y = self.v_angle_y + bot_cmd.bot_cmd_parm_float;
585 self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
586 return CMD_STATUS_FINISHED;
589 float bot_cmd_select_weapon()
593 id = bot_cmd.bot_cmd_parm_float;
595 if(id < WEP_FIRST || id > WEP_LAST)
596 return CMD_STATUS_ERROR;
598 if(client_hasweapon(self, id, TRUE, FALSE))
599 self.switchweapon = id;
601 return CMD_STATUS_ERROR;
603 return CMD_STATUS_FINISHED;
606 .float bot_cmd_condition_status;
608 #define CMD_CONDITION_NONE 0
609 #define CMD_CONDITION_TRUE 1
610 #define CMD_CONDITION_FALSE 2
611 #define CMD_CONDITION_TRUE_BLOCK 4
612 #define CMD_CONDITION_FALSE_BLOCK 8
614 float bot_cmd_eval(string expr)
616 // Search for numbers
617 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
623 if(substring(expr, 0, 5)=="cvar.")
625 return cvar(substring(expr, 5, strlen(expr)));
634 return vlen(self.velocity);
636 return ((self.flagcarried!=world));
639 print(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
645 local string expr, val_a, val_b;
648 if(self.bot_cmd_condition_status != CMD_CONDITION_NONE)
650 // Only one "if" block is allowed at time
651 print("ERROR: Only one conditional block can be processed at time");
652 bot_clearqueue(self);
653 return CMD_STATUS_ERROR;
656 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE_BLOCK;
658 // search for operators
659 expr = bot_cmd.bot_cmd_parm_string;
661 cmpofs = strstrofs(expr,"=",0);
665 val_a = substring(expr,0,cmpofs);
666 val_b = substring(expr,cmpofs+1,strlen(expr));
668 if(bot_cmd_eval(val_a)==bot_cmd_eval(val_b))
669 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
671 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
673 return CMD_STATUS_FINISHED;
676 cmpofs = strstrofs(expr,">",0);
680 val_a = substring(expr,0,cmpofs);
681 val_b = substring(expr,cmpofs+1,strlen(expr));
683 if(bot_cmd_eval(val_a)>bot_cmd_eval(val_b))
684 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
686 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
688 return CMD_STATUS_FINISHED;
691 cmpofs = strstrofs(expr,"<",0);
695 val_a = substring(expr,0,cmpofs);
696 val_b = substring(expr,cmpofs+1,strlen(expr));
698 if(bot_cmd_eval(val_a)<bot_cmd_eval(val_b))
699 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
701 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
703 return CMD_STATUS_FINISHED;
706 if(bot_cmd_eval(expr))
707 self.bot_cmd_condition_status |= CMD_CONDITION_TRUE;
709 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE;
711 return CMD_STATUS_FINISHED;
716 self.bot_cmd_condition_status &~= CMD_CONDITION_TRUE_BLOCK;
717 self.bot_cmd_condition_status |= CMD_CONDITION_FALSE_BLOCK;
718 return CMD_STATUS_FINISHED;
723 self.bot_cmd_condition_status = CMD_CONDITION_NONE;
724 return CMD_STATUS_FINISHED;
727 float bot_cmd_resetaim()
729 self.v_angle = '0 0 0';
730 return CMD_STATUS_FINISHED;
733 .float bot_cmd_aim_begintime;
734 .float bot_cmd_aim_endtime;
735 .vector bot_cmd_aim_begin;
736 .vector bot_cmd_aim_end;
741 if(self.bot_cmd_aim_endtime)
743 local float progress;
745 progress = min(1 - (self.bot_cmd_aim_endtime - time) / (self.bot_cmd_aim_endtime - self.bot_cmd_aim_begintime),1);
746 self.v_angle = self.bot_cmd_aim_begin + ((self.bot_cmd_aim_end - self.bot_cmd_aim_begin) * progress);
748 if(time>=self.bot_cmd_aim_endtime)
750 self.bot_cmd_aim_endtime = 0;
751 return CMD_STATUS_FINISHED;
754 return CMD_STATUS_EXECUTING;
757 // New aiming direction
759 local float tokens, step;
761 parms = bot_cmd.bot_cmd_parm_string;
763 tokens = tokenizebyseparator(parms, " ");
767 self.v_angle_x -= stof(argv(1));
768 self.v_angle_y += stof(argv(0));
769 return CMD_STATUS_FINISHED;
772 if(tokens<2||tokens>3)
773 return CMD_STATUS_ERROR;
775 step = stof(argv(2));
777 self.bot_cmd_aim_begin = self.v_angle;
779 self.bot_cmd_aim_end_x = self.v_angle_x - stof(argv(1));
780 self.bot_cmd_aim_end_y = self.v_angle_y + stof(argv(0));
781 self.bot_cmd_aim_end_z = 0;
783 self.bot_cmd_aim_begintime = time;
784 self.bot_cmd_aim_endtime = time + step;
786 return CMD_STATUS_EXECUTING;
789 float bot_cmd_aimtarget()
791 if(self.bot_cmd_aim_endtime)
793 return bot_cmd_aim();
799 local float tokens, step;
801 parms = bot_cmd.bot_cmd_parm_string;
803 tokens = tokenizebyseparator(parms, " ");
805 e = bot_getplace(argv(0));
807 return CMD_STATUS_ERROR;
809 v = e.origin + (e.mins + e.maxs) * 0.5;
813 self.v_angle = vectoangles(v - (self.origin + self.view_ofs));
814 self.v_angle_x = -self.v_angle_x;
815 return CMD_STATUS_FINISHED;
818 if(tokens<1||tokens>2)
819 return CMD_STATUS_ERROR;
821 step = stof(argv(1));
823 self.bot_cmd_aim_begin = self.v_angle;
824 self.bot_cmd_aim_end = vectoangles(v - (self.origin + self.view_ofs));
825 self.bot_cmd_aim_end_x = -self.bot_cmd_aim_end_x;
827 self.bot_cmd_aim_begintime = time;
828 self.bot_cmd_aim_endtime = time + step;
830 return CMD_STATUS_EXECUTING;
835 #define BOT_CMD_KEY_NONE 0
836 #define BOT_CMD_KEY_FORWARD 1
837 #define BOT_CMD_KEY_BACKWARD 2
838 #define BOT_CMD_KEY_RIGHT 4
839 #define BOT_CMD_KEY_LEFT 8
840 #define BOT_CMD_KEY_JUMP 16
841 #define BOT_CMD_KEY_ATTACK1 32
842 #define BOT_CMD_KEY_ATTACK2 64
843 #define BOT_CMD_KEY_USE 128
844 #define BOT_CMD_KEY_HOOK 256
845 #define BOT_CMD_KEY_CROUCH 512
846 #define BOT_CMD_KEY_CHAT 1024
848 float bot_presskeys()
850 self.movement = '0 0 0';
852 if(self.bot_cmd_keys == BOT_CMD_KEY_NONE)
855 if(self.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
856 self.movement_x = cvar("sv_maxspeed");
857 else if(self.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
858 self.movement_x = -cvar("sv_maxspeed");
860 if(self.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
861 self.movement_y = cvar("sv_maxspeed");
862 else if(self.bot_cmd_keys & BOT_CMD_KEY_LEFT)
863 self.movement_y = -cvar("sv_maxspeed");
865 if(self.bot_cmd_keys & BOT_CMD_KEY_JUMP)
866 self.BUTTON_JUMP = TRUE;
868 if(self.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
869 self.BUTTON_CROUCH = TRUE;
871 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
872 self.BUTTON_ATCK = TRUE;
874 if(self.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
875 self.BUTTON_ATCK2 = TRUE;
877 if(self.bot_cmd_keys & BOT_CMD_KEY_USE)
878 self.BUTTON_USE = TRUE;
880 if(self.bot_cmd_keys & BOT_CMD_KEY_HOOK)
881 self.BUTTON_HOOK = TRUE;
883 if(self.bot_cmd_keys & BOT_CMD_KEY_CHAT)
884 self.BUTTON_CHAT = TRUE;
890 float bot_cmd_keypress_handler(string key, float enabled)
896 self.bot_cmd_keys = power2of(20) - 1; // >:)
898 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
902 self.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
903 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
906 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
911 self.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
912 self.bot_cmd_keys &~= BOT_CMD_KEY_FORWARD;
915 self.bot_cmd_keys &~= BOT_CMD_KEY_BACKWARD;
920 self.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
921 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
924 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
929 self.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
930 self.bot_cmd_keys &~= BOT_CMD_KEY_LEFT;
933 self.bot_cmd_keys &~= BOT_CMD_KEY_RIGHT;
937 self.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
939 self.bot_cmd_keys &~= BOT_CMD_KEY_JUMP;
943 self.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
945 self.bot_cmd_keys &~= BOT_CMD_KEY_CROUCH;
949 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
951 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK1;
955 self.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
957 self.bot_cmd_keys &~= BOT_CMD_KEY_ATTACK2;
961 self.bot_cmd_keys |= BOT_CMD_KEY_USE;
963 self.bot_cmd_keys &~= BOT_CMD_KEY_USE;
967 self.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
969 self.bot_cmd_keys &~= BOT_CMD_KEY_HOOK;
973 self.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
975 self.bot_cmd_keys &~= BOT_CMD_KEY_CHAT;
981 return CMD_STATUS_FINISHED;
984 float bot_cmd_presskey()
988 key = bot_cmd.bot_cmd_parm_string;
990 bot_cmd_keypress_handler(key,TRUE);
992 return CMD_STATUS_FINISHED;
995 float bot_cmd_releasekey()
999 key = bot_cmd.bot_cmd_parm_string;
1001 return bot_cmd_keypress_handler(key,FALSE);
1004 float bot_cmd_pause()
1008 self.BUTTON_USE = 0;
1009 self.BUTTON_ATCK = 0;
1010 self.BUTTON_JUMP = 0;
1011 self.BUTTON_HOOK = 0;
1012 self.BUTTON_CHAT = 0;
1013 self.BUTTON_ATCK2 = 0;
1014 self.BUTTON_CROUCH = 0;
1016 self.movement = '0 0 0';
1017 self.bot_cmd_keys = BOT_CMD_KEY_NONE;
1019 self.bot_exec_status = self.bot_exec_status | BOT_EXEC_STATUS_PAUSED;
1020 return CMD_STATUS_FINISHED;
1023 float bot_cmd_moveto()
1025 return self.cmd_moveto(bot_cmd.bot_cmd_parm_vector);
1028 float bot_cmd_movetotarget()
1031 e = bot_getplace(bot_cmd.bot_cmd_parm_string);
1033 return CMD_STATUS_ERROR;
1034 return self.cmd_moveto(e.origin + (e.mins + e.maxs) * 0.5);
1037 float bot_cmd_resetgoal()
1039 return self.cmd_resetgoal();
1050 void bot_command_executed(float rm)
1057 bot_dequeuecommand(self, self.bot_cmd_execution_index);
1059 self.bot_cmd_execution_index++;
1062 void bot_setcurrentcommand()
1066 if(self.bot_cmd_execution_index == 0)
1067 self.bot_cmd_execution_index = 1;
1069 if(!self.bot_cmd_current)
1071 self.bot_cmd_current = spawn();
1072 self.bot_cmd_current.classname = "bot_cmd";
1073 self.bot_cmd_current.is_bot_cmd = 1;
1074 self.bot_cmd_current.bot_cmd_index = 0;
1077 bot_cmd = self.bot_cmd_current;
1078 if(bot_cmd.bot_cmd_index != self.bot_cmd_execution_index)
1080 if(bot_havecommand(self, self.bot_cmd_execution_index))
1083 cmdstring = bot_readcommand(self, self.bot_cmd_execution_index);
1084 if(bot_decodecommand(cmdstring))
1086 bot_cmd.owner = self;
1087 bot_cmd.bot_cmd_index = self.bot_cmd_execution_index;
1097 void bot_resetqueues()
1102 FOR_EACH_CLIENT(cl) if(cl.isbot)
1105 // also, cancel all barriers
1107 for(i = 0; i < cl.bot_places_count; ++i)
1109 strunzone(cl.(bot_placenames[i]));
1110 cl.(bot_placenames[i]) = string_null;
1112 cl.bot_places_count = 0;
1115 bot_barriertime = time;
1118 // This function should be (the only) called directly from the bot ai loop
1119 // It maps commands to functions and deal with complex interactions between commands and execution states
1120 // NOTE: Of course you need to include your commands here too :)
1121 float bot_execute_commands()
1123 local float status, ispressingkey;
1128 if(self.deadflag!=DEAD_NO)
1132 bot_setcurrentcommand();
1134 // Keep pressing keys raised by the "presskey" command
1135 ispressingkey = bot_presskeys();
1138 return ispressingkey;
1140 // Ignore all commands except continue when the bot is paused
1141 if(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1142 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1144 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1146 bot_command_executed(TRUE);
1147 print( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1152 // Handle conditions
1153 if not(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE)
1154 if(self.bot_cmd_condition_status & CMD_CONDITION_TRUE && self.bot_cmd_condition_status & CMD_CONDITION_FALSE_BLOCK)
1156 bot_command_executed(TRUE);
1159 else if(self.bot_cmd_condition_status & CMD_CONDITION_FALSE && self.bot_cmd_condition_status & CMD_CONDITION_TRUE_BLOCK)
1161 bot_command_executed(TRUE);
1165 // Map commands to functions
1166 switch(bot_cmd.bot_cmd_type)
1169 return ispressingkey;
1172 status = bot_cmd_pause();
1174 case BOT_CMD_CONTINUE:
1175 status = bot_cmd_continue();
1178 status = bot_cmd_wait();
1180 case BOT_CMD_WAIT_UNTIL:
1181 status = bot_cmd_wait_until();
1184 status = bot_cmd_turn();
1186 case BOT_CMD_MOVETO:
1187 status = bot_cmd_moveto();
1189 case BOT_CMD_MOVETOTARGET:
1190 status = bot_cmd_movetotarget();
1192 case BOT_CMD_RESETGOAL:
1193 status = bot_cmd_resetgoal();
1196 status = bot_cmd_cc();
1199 status = bot_cmd_if();
1202 status = bot_cmd_else();
1205 status = bot_cmd_fi();
1207 case BOT_CMD_RESETAIM:
1208 status = bot_cmd_resetaim();
1211 status = bot_cmd_aim();
1213 case BOT_CMD_AIMTARGET:
1214 status = bot_cmd_aimtarget();
1216 case BOT_CMD_PRESSKEY:
1217 status = bot_cmd_presskey();
1219 case BOT_CMD_RELEASEKEY:
1220 status = bot_cmd_releasekey();
1222 case BOT_CMD_SELECTWEAPON:
1223 status = bot_cmd_select_weapon();
1225 case BOT_CMD_IMPULSE:
1226 status = bot_cmd_impulse();
1228 case BOT_CMD_BARRIER:
1229 status = bot_cmd_barrier();
1231 case BOT_CMD_CONSOLE:
1232 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1233 status = CMD_STATUS_FINISHED;
1236 print(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1240 if (status==CMD_STATUS_ERROR)
1241 print(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1243 // Move execution pointer
1244 if(status==CMD_STATUS_EXECUTING)
1250 if(cvar("g_debug_bot_commands"))
1254 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1256 case BOT_CMD_PARAMETER_FLOAT:
1257 parms = ftos(bot_cmd.bot_cmd_parm_float);
1259 case BOT_CMD_PARAMETER_STRING:
1260 parms = bot_cmd.bot_cmd_parm_string;
1262 case BOT_CMD_PARAMETER_VECTOR:
1263 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1269 clientcommand(self,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1272 bot_command_executed(TRUE);
1275 } while(status==CMD_STATUS_FINISHED);