1 #include "scripting.qh"
3 #include <common/state.qh>
4 #include <common/physics/player.qh>
10 .float bot_cmdqueuebuf_allocated;
11 .float bot_cmdqueuebuf;
12 .float bot_cmdqueuebuf_start;
13 .float bot_cmdqueuebuf_end;
15 void bot_clearqueue(entity bot)
17 if(!bot.bot_cmdqueuebuf_allocated)
19 buf_del(bot.bot_cmdqueuebuf);
20 bot.bot_cmdqueuebuf_allocated = false;
21 LOG_TRACE("bot ", bot.netname, " queue cleared\n");
24 void bot_queuecommand(entity bot, string cmdstring)
26 if(!bot.bot_cmdqueuebuf_allocated)
28 bot.bot_cmdqueuebuf = buf_create();
29 bot.bot_cmdqueuebuf_allocated = true;
30 bot.bot_cmdqueuebuf_start = 0;
31 bot.bot_cmdqueuebuf_end = 0;
34 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
36 // if the command was a "sound" command, precache the sound NOW
37 // this prevents lagging!
43 sp = strstrofs(cmdstring, " ", 0);
46 parm = substring(cmdstring, sp + 1, -1);
47 cmdstr = substring(cmdstring, 0, sp);
53 sp = strstrofs(parm, " ", 0);
56 parm = substring(parm, sp + 1, -1);
63 bot.bot_cmdqueuebuf_end += 1;
66 void bot_dequeuecommand(entity bot, float idx)
68 if(!bot.bot_cmdqueuebuf_allocated)
69 error("dequeuecommand but no queue allocated");
70 if(idx < bot.bot_cmdqueuebuf_start)
71 error("dequeueing a command in the past");
72 if(idx >= bot.bot_cmdqueuebuf_end)
73 error("dequeueing a command in the future");
74 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
75 if(idx == bot.bot_cmdqueuebuf_start)
76 bot.bot_cmdqueuebuf_start += 1;
77 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
81 string bot_readcommand(entity bot, float idx)
83 if(!bot.bot_cmdqueuebuf_allocated)
84 error("readcommand but no queue allocated");
85 if(idx < bot.bot_cmdqueuebuf_start)
86 error("reading a command in the past");
87 if(idx >= bot.bot_cmdqueuebuf_end)
88 error("reading a command in the future");
89 return bufstr_get(bot.bot_cmdqueuebuf, idx);
92 bool bot_havecommand(entity this, int idx)
94 if(!this.bot_cmdqueuebuf_allocated)
96 if(idx < this.bot_cmdqueuebuf_start)
98 if(idx >= this.bot_cmdqueuebuf_end)
103 const int MAX_BOT_PLACES = 4;
104 .float bot_places_count;
105 .entity bot_places[MAX_BOT_PLACES];
106 .string bot_placenames[MAX_BOT_PLACES];
107 entity bot_getplace(entity this, string placename)
110 if(substring(placename, 0, 1) == "@")
113 placename = substring(placename, 1, -1);
115 for(i = 0; i < this.bot_places_count; ++i)
116 if(this.(bot_placenames[i]) == placename)
117 return this.(bot_places[i]);
118 // now: i == this.bot_places_count
119 s = s2 = cvar_string(placename);
120 p = strstrofs(s2, " ", 0);
123 s = substring(s2, 0, p);
124 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
125 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
126 //print("places: ", placename, " := ", cvar_string(placename), "\n");
128 e = find(NULL, targetname, s);
130 LOG_INFO("invalid place ", s, "\n");
131 if(i < MAX_BOT_PLACES)
133 this.(bot_placenames[i]) = strzone(placename);
134 this.(bot_places[i]) = e;
135 this.bot_places_count += 1;
141 e = find(NULL, targetname, placename);
143 LOG_INFO("invalid place ", placename, "\n");
149 // Initialize global commands list
150 // NOTE: New commands should be initialized here
151 void bot_commands_init()
153 bot_cmd_string[BOT_CMD_NULL] = "";
154 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
156 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
157 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
159 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
160 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
162 bot_cmd_string[BOT_CMD_WAIT] = "wait";
163 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
165 bot_cmd_string[BOT_CMD_TURN] = "turn";
166 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
168 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
169 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
171 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
172 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
174 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
175 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
177 bot_cmd_string[BOT_CMD_CC] = "cc";
178 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
180 bot_cmd_string[BOT_CMD_IF] = "if";
181 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
183 bot_cmd_string[BOT_CMD_ELSE] = "else";
184 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
186 bot_cmd_string[BOT_CMD_FI] = "fi";
187 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
189 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
190 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
192 bot_cmd_string[BOT_CMD_AIM] = "aim";
193 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
195 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
196 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
198 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
199 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
201 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
202 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
204 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
205 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
207 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
208 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
210 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
211 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
213 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
214 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
216 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
217 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
219 bot_cmd_string[BOT_CMD_SOUND] = "sound";
220 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
222 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
223 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
225 bot_cmds_initialized = true;
228 // Returns first bot with matching name
229 entity find_bot_by_name(string name)
233 bot = findchainflags(flags, FL_CLIENT);
236 if(IS_BOT_CLIENT(bot))
237 if(bot.netname==name)
246 // Returns a bot by number on list
247 entity find_bot_by_number(float number)
255 bot = findchainflags(flags, FL_CLIENT);
258 if(IS_BOT_CLIENT(bot))
269 float bot_decodecommand(string cmdstring)
275 sp = strstrofs(cmdstring, " ", 0);
282 parm = substring(cmdstring, sp + 1, -1);
283 cmdstring = substring(cmdstring, 0, sp);
286 if(!bot_cmds_initialized)
290 for(i=1;i<BOT_CMD_COUNTER;++i)
292 if(bot_cmd_string[i]!=cmdstring)
295 cmd_parm_type = bot_cmd_parm_type[i];
297 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
299 LOG_INFO("ERROR: A parameter is required for this command\n");
303 // Load command into queue
304 bot_cmd.bot_cmd_type = i;
307 switch(cmd_parm_type)
309 case BOT_CMD_PARAMETER_FLOAT:
310 bot_cmd.bot_cmd_parm_float = stof(parm);
312 case BOT_CMD_PARAMETER_STRING:
313 if(bot_cmd.bot_cmd_parm_string)
314 strunzone(bot_cmd.bot_cmd_parm_string);
315 bot_cmd.bot_cmd_parm_string = strzone(parm);
317 case BOT_CMD_PARAMETER_VECTOR:
318 bot_cmd.bot_cmd_parm_vector = stov(parm);
325 LOG_INFO("ERROR: No such command '", cmdstring, "'\n");
329 void bot_cmdhelp(string scmd)
334 if(!bot_cmds_initialized)
337 for(i=1;i<BOT_CMD_COUNTER;++i)
339 if(bot_cmd_string[i]!=scmd)
342 ntype = bot_cmd_parm_type[i];
346 case BOT_CMD_PARAMETER_FLOAT:
347 stype = "float number";
349 case BOT_CMD_PARAMETER_STRING:
352 case BOT_CMD_PARAMETER_VECTOR:
360 LOG_INFO(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
362 LOG_INFO("Description: ");
366 LOG_INFO("Stops the bot completely. Any command other than 'continue' will be ignored.");
368 case BOT_CMD_CONTINUE:
369 LOG_INFO("Disable paused status");
372 LOG_INFO("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
374 case BOT_CMD_WAIT_UNTIL:
375 LOG_INFO("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
377 case BOT_CMD_BARRIER:
378 LOG_INFO("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
381 LOG_INFO("Look to the right or left N degrees. For turning to the left use positive numbers.");
384 LOG_INFO("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
386 case BOT_CMD_MOVETOTARGET:
387 LOG_INFO("Walk to the specific target on the map");
389 case BOT_CMD_RESETGOAL:
390 LOG_INFO("Resets the goal stack");
393 LOG_INFO("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
396 LOG_INFO("Perform simple conditional execution.\n");
397 LOG_INFO("Syntax: \n");
398 LOG_INFO(" sv_cmd .. if \"condition\"\n");
399 LOG_INFO(" sv_cmd .. <instruction if true>\n");
400 LOG_INFO(" sv_cmd .. <instruction if true>\n");
401 LOG_INFO(" sv_cmd .. else\n");
402 LOG_INFO(" sv_cmd .. <instruction if false>\n");
403 LOG_INFO(" sv_cmd .. <instruction if false>\n");
404 LOG_INFO(" sv_cmd .. fi\n");
405 LOG_INFO("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
406 LOG_INFO(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
407 LOG_INFO("Fields: health, speed, flagcarrier\n");
408 LOG_INFO("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
410 case BOT_CMD_RESETAIM:
411 LOG_INFO("Points the aim to the coordinates x,y 0,0");
414 LOG_INFO("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
415 LOG_INFO("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
416 LOG_INFO("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
417 LOG_INFO(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
419 case BOT_CMD_AIMTARGET:
420 LOG_INFO("Points the aim to given target");
422 case BOT_CMD_PRESSKEY:
423 LOG_INFO("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
424 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
425 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
427 case BOT_CMD_RELEASEKEY:
428 LOG_INFO("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
431 LOG_INFO("play sound file at bot location");
433 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
434 LOG_INFO("verify the state of the weapon entity");
437 LOG_INFO("This command has no description yet.");
444 void bot_list_commands()
449 if(!bot_cmds_initialized)
452 LOG_INFO("List of all available commands:\n");
453 LOG_INFO(" Command - Parameter Type\n");
455 for(i=1;i<BOT_CMD_COUNTER;++i)
457 switch(bot_cmd_parm_type[i])
459 case BOT_CMD_PARAMETER_FLOAT:
460 ptype = "float number";
462 case BOT_CMD_PARAMETER_STRING:
465 case BOT_CMD_PARAMETER_VECTOR:
472 LOG_INFO(strcat(" ",bot_cmd_string[i]," - <",ptype,"> \n"));
477 .int bot_exec_status;
479 void SV_ParseClientCommand(string s);
480 float bot_cmd_cc(entity this)
482 WITHSELF(this, SV_ParseClientCommand(bot_cmd.bot_cmd_parm_string));
483 return CMD_STATUS_FINISHED;
486 float bot_cmd_impulse(entity this)
488 this.impulse = bot_cmd.bot_cmd_parm_float;
489 return CMD_STATUS_FINISHED;
492 float bot_cmd_continue(entity this)
494 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
495 return CMD_STATUS_FINISHED;
498 .float bot_cmd_wait_time;
499 float bot_cmd_wait(entity this)
501 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
503 if(time>=this.bot_cmd_wait_time)
505 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
506 return CMD_STATUS_FINISHED;
509 return CMD_STATUS_EXECUTING;
512 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
513 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
514 return CMD_STATUS_EXECUTING;
517 float bot_cmd_wait_until(entity this)
519 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
521 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
522 return CMD_STATUS_EXECUTING;
524 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
525 return CMD_STATUS_FINISHED;
528 float bot_cmd_barrier(entity this)
530 // 0 = no barrier, 1 = waiting, 2 = waiting finished
532 if(this.bot_barrier == 0) // initialization
534 this.bot_barrier = 1;
536 //this.colormod = '4 4 0';
539 if(this.bot_barrier == 1) // find other bots
541 FOREACH_CLIENT(it.isbot, LAMBDA(
542 if(it.bot_cmdqueuebuf_allocated)
543 if(it.bot_barrier != 1)
544 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
547 // all bots hit the barrier!
549 // acknowledge barrier
550 FOREACH_CLIENT(it.isbot, LAMBDA(it.bot_barrier = 2));
552 bot_barriertime = time;
555 // if we get here, the barrier is finished
557 this.bot_barrier = 0;
558 //this.colormod = '0 0 0';
560 return CMD_STATUS_FINISHED;
563 float bot_cmd_turn(entity this)
565 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
566 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
567 return CMD_STATUS_FINISHED;
570 float bot_cmd_select_weapon(entity this)
572 float id = bot_cmd.bot_cmd_parm_float;
574 if(id < WEP_FIRST || id > WEP_LAST)
575 return CMD_STATUS_ERROR;
577 if(client_hasweapon(this, Weapons_from(id), true, false))
578 PS(this).m_switchweapon = Weapons_from(id);
580 return CMD_STATUS_ERROR;
582 return CMD_STATUS_FINISHED;
585 .int bot_cmd_condition_status;
587 const int CMD_CONDITION_NONE = 0;
588 const int CMD_CONDITION_true = 1;
589 const int CMD_CONDITION_false = 2;
590 const int CMD_CONDITION_true_BLOCK = 4;
591 const int CMD_CONDITION_false_BLOCK = 8;
593 float bot_cmd_eval(entity this, string expr)
595 // Search for numbers
596 if(strstrofs("0123456789", substring(expr, 0, 1), 0) >= 0)
602 if(substring(expr, 0, 5)=="cvar.")
604 return cvar(substring(expr, 5, strlen(expr)));
613 return vlen(this.velocity);
615 return ((this.flagcarried!=NULL));
618 LOG_INFO(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
622 float bot_cmd_if(entity this)
624 string expr, val_a, val_b;
627 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
629 // Only one "if" block is allowed at time
630 LOG_INFO("ERROR: Only one conditional block can be processed at time");
631 bot_clearqueue(this);
632 return CMD_STATUS_ERROR;
635 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
637 // search for operators
638 expr = bot_cmd.bot_cmd_parm_string;
640 cmpofs = strstrofs(expr,"=",0);
644 val_a = substring(expr,0,cmpofs);
645 val_b = substring(expr,cmpofs+1,strlen(expr));
647 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
648 this.bot_cmd_condition_status |= CMD_CONDITION_true;
650 this.bot_cmd_condition_status |= CMD_CONDITION_false;
652 return CMD_STATUS_FINISHED;
655 cmpofs = strstrofs(expr,">",0);
659 val_a = substring(expr,0,cmpofs);
660 val_b = substring(expr,cmpofs+1,strlen(expr));
662 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
663 this.bot_cmd_condition_status |= CMD_CONDITION_true;
665 this.bot_cmd_condition_status |= CMD_CONDITION_false;
667 return CMD_STATUS_FINISHED;
670 cmpofs = strstrofs(expr,"<",0);
674 val_a = substring(expr,0,cmpofs);
675 val_b = substring(expr,cmpofs+1,strlen(expr));
677 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
678 this.bot_cmd_condition_status |= CMD_CONDITION_true;
680 this.bot_cmd_condition_status |= CMD_CONDITION_false;
682 return CMD_STATUS_FINISHED;
685 if(bot_cmd_eval(this, expr))
686 this.bot_cmd_condition_status |= CMD_CONDITION_true;
688 this.bot_cmd_condition_status |= CMD_CONDITION_false;
690 return CMD_STATUS_FINISHED;
693 float bot_cmd_else(entity this)
695 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
696 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
697 return CMD_STATUS_FINISHED;
700 float bot_cmd_fi(entity this)
702 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
703 return CMD_STATUS_FINISHED;
706 float bot_cmd_resetaim(entity this)
708 this.v_angle = '0 0 0';
709 return CMD_STATUS_FINISHED;
712 .float bot_cmd_aim_begintime;
713 .float bot_cmd_aim_endtime;
714 .vector bot_cmd_aim_begin;
715 .vector bot_cmd_aim_end;
717 float bot_cmd_aim(entity this)
720 if(this.bot_cmd_aim_endtime)
724 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
725 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
727 if(time>=this.bot_cmd_aim_endtime)
729 this.bot_cmd_aim_endtime = 0;
730 return CMD_STATUS_FINISHED;
733 return CMD_STATUS_EXECUTING;
736 // New aiming direction
740 parms = bot_cmd.bot_cmd_parm_string;
742 tokens = tokenizebyseparator(parms, " ");
744 if(tokens<2||tokens>3)
745 return CMD_STATUS_ERROR;
747 step = (tokens == 3) ? stof(argv(2)) : 0;
751 this.v_angle_x -= stof(argv(1));
752 this.v_angle_y += stof(argv(0));
753 return CMD_STATUS_FINISHED;
756 this.bot_cmd_aim_begin = this.v_angle;
758 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
759 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
760 this.bot_cmd_aim_end_z = 0;
762 this.bot_cmd_aim_begintime = time;
763 this.bot_cmd_aim_endtime = time + step;
765 return CMD_STATUS_EXECUTING;
768 float bot_cmd_aimtarget(entity this)
770 if(this.bot_cmd_aim_endtime)
772 return bot_cmd_aim(this);
780 parms = bot_cmd.bot_cmd_parm_string;
782 tokens = tokenizebyseparator(parms, " ");
784 e = bot_getplace(this, argv(0));
786 return CMD_STATUS_ERROR;
788 v = e.origin + (e.mins + e.maxs) * 0.5;
792 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
793 this.v_angle_x = -this.v_angle.x;
794 return CMD_STATUS_FINISHED;
797 if(tokens<1||tokens>2)
798 return CMD_STATUS_ERROR;
800 step = stof(argv(1));
802 this.bot_cmd_aim_begin = this.v_angle;
803 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
804 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
806 this.bot_cmd_aim_begintime = time;
807 this.bot_cmd_aim_endtime = time + step;
809 return CMD_STATUS_EXECUTING;
814 const int BOT_CMD_KEY_NONE = 0;
815 const int BOT_CMD_KEY_FORWARD = BIT(0);
816 const int BOT_CMD_KEY_BACKWARD = BIT(1);
817 const int BOT_CMD_KEY_RIGHT = BIT(2);
818 const int BOT_CMD_KEY_LEFT = BIT(3);
819 const int BOT_CMD_KEY_JUMP = BIT(4);
820 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
821 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
822 const int BOT_CMD_KEY_USE = BIT(7);
823 const int BOT_CMD_KEY_HOOK = BIT(8);
824 const int BOT_CMD_KEY_CROUCH = BIT(9);
825 const int BOT_CMD_KEY_CHAT = BIT(10);
827 bool bot_presskeys(entity this)
829 this.movement = '0 0 0';
830 PHYS_INPUT_BUTTON_JUMP(this) = false;
831 PHYS_INPUT_BUTTON_CROUCH(this) = false;
832 PHYS_INPUT_BUTTON_ATCK(this) = false;
833 PHYS_INPUT_BUTTON_ATCK2(this) = false;
834 PHYS_INPUT_BUTTON_USE(this) = false;
835 PHYS_INPUT_BUTTON_HOOK(this) = false;
836 PHYS_INPUT_BUTTON_CHAT(this) = false;
838 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
841 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
842 this.movement_x = autocvar_sv_maxspeed;
843 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
844 this.movement_x = -autocvar_sv_maxspeed;
846 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
847 this.movement_y = autocvar_sv_maxspeed;
848 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
849 this.movement_y = -autocvar_sv_maxspeed;
851 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
852 PHYS_INPUT_BUTTON_JUMP(this) = true;
854 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
855 PHYS_INPUT_BUTTON_CROUCH(this) = true;
857 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
858 PHYS_INPUT_BUTTON_ATCK(this) = true;
860 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
861 PHYS_INPUT_BUTTON_ATCK2(this) = true;
863 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
864 PHYS_INPUT_BUTTON_USE(this) = true;
866 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
867 PHYS_INPUT_BUTTON_HOOK(this) = true;
869 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
870 PHYS_INPUT_BUTTON_CHAT(this) = true;
876 float bot_cmd_keypress_handler(entity this, string key, float enabled)
882 this.bot_cmd_keys = power2of(20) - 1; // >:)
884 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
888 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
889 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
892 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
897 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
898 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
901 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
906 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
907 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
910 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
915 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
916 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
919 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
923 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
925 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
929 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
931 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
935 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
937 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
941 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
943 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
947 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
949 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
953 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
955 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
959 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
961 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
967 return CMD_STATUS_FINISHED;
970 float bot_cmd_presskey(entity this)
974 key = bot_cmd.bot_cmd_parm_string;
976 bot_cmd_keypress_handler(this, key,true);
978 return CMD_STATUS_FINISHED;
981 float bot_cmd_releasekey(entity this)
985 key = bot_cmd.bot_cmd_parm_string;
987 return bot_cmd_keypress_handler(this, key,false);
990 float bot_cmd_pause(entity this)
992 PHYS_INPUT_BUTTON_DRAG(this) = false;
993 PHYS_INPUT_BUTTON_USE(this) = false;
994 PHYS_INPUT_BUTTON_ATCK(this) = false;
995 PHYS_INPUT_BUTTON_JUMP(this) = false;
996 PHYS_INPUT_BUTTON_HOOK(this) = false;
997 PHYS_INPUT_BUTTON_CHAT(this) = false;
998 PHYS_INPUT_BUTTON_ATCK2(this) = false;
999 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1001 this.movement = '0 0 0';
1002 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1004 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1005 return CMD_STATUS_FINISHED;
1008 float bot_cmd_moveto(entity this)
1010 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1013 float bot_cmd_movetotarget(entity this)
1016 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1018 return CMD_STATUS_ERROR;
1019 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1022 float bot_cmd_resetgoal(entity this)
1024 return this.cmd_resetgoal(this);
1028 float bot_cmd_sound(entity this)
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(this, chan, sample, vol, atten);
1052 return CMD_STATUS_FINISHED;
1056 float bot_cmd_debug_assert_canfire(entity this)
1058 float f = bot_cmd.bot_cmd_parm_float;
1061 .entity weaponentity = weaponentities[slot];
1062 if(this.(weaponentity).state != WS_READY)
1066 this.colormod = '0 8 8';
1067 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " wants to fire, inhibited by weaponentity state\n");
1070 else if(ATTACK_FINISHED(this, slot) > time)
1074 this.colormod = '8 0 8';
1075 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left)\n");
1078 else if(this.tuba_note)
1082 this.colormod = '8 0 0';
1083 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " wants to fire, bot still has an active tuba note\n");
1090 this.colormod = '8 8 0';
1091 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left\n");
1095 return CMD_STATUS_FINISHED;
1100 void bot_command_executed(entity this, bool rm)
1107 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1109 this.bot_cmd_execution_index++;
1112 void bot_setcurrentcommand(entity this)
1116 if(!this.bot_cmd_current)
1118 this.bot_cmd_current = new_pure(bot_cmd);
1119 this.bot_cmd_current.is_bot_cmd = true;
1122 bot_cmd = this.bot_cmd_current;
1123 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1125 if(bot_havecommand(this, this.bot_cmd_execution_index))
1128 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1129 if(bot_decodecommand(cmdstring))
1131 bot_cmd.owner = this;
1132 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1136 // Invalid command, remove from queue
1138 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1139 this.bot_cmd_execution_index++;
1147 void bot_resetqueues()
1149 FOREACH_CLIENT(it.isbot, LAMBDA(
1150 it.bot_cmd_execution_index = 0;
1152 // also, cancel all barriers
1154 for(int i = 0; i < it.bot_places_count; ++i)
1156 strunzone(it.(bot_placenames[i]));
1157 it.(bot_placenames[i]) = string_null;
1159 it.bot_places_count = 0;
1162 bot_barriertime = time;
1165 // Here we map commands to functions and deal with complex interactions between commands and execution states
1166 // NOTE: Of course you need to include your commands here too :)
1167 float bot_execute_commands_once(entity this)
1169 float status, ispressingkey;
1172 bot_setcurrentcommand(this);
1174 // if we have no bot command, better return
1175 // old logic kept pressing previously pressed keys, but that has problems
1176 // (namely, it means you cannot make a bot "normal" ever again)
1177 // to keep a bot walking for a while, use the "wait" bot command
1181 // Ignore all commands except continue when the bot is paused
1182 if(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1183 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1185 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1187 bot_command_executed(this, true);
1188 LOG_INFO( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1193 // Keep pressing keys raised by the "presskey" command
1194 ispressingkey = boolean(bot_presskeys(this));
1196 // Handle conditions
1197 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1198 if(this.bot_cmd_condition_status & CMD_CONDITION_true && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1200 bot_command_executed(this, true);
1203 else if(this.bot_cmd_condition_status & CMD_CONDITION_false && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1205 bot_command_executed(this, true);
1209 // Map commands to functions
1210 switch(bot_cmd.bot_cmd_type)
1213 return ispressingkey;
1216 status = bot_cmd_pause(this);
1218 case BOT_CMD_CONTINUE:
1219 status = bot_cmd_continue(this);
1222 status = bot_cmd_wait(this);
1224 case BOT_CMD_WAIT_UNTIL:
1225 status = bot_cmd_wait_until(this);
1228 status = bot_cmd_turn(this);
1230 case BOT_CMD_MOVETO:
1231 status = bot_cmd_moveto(this);
1233 case BOT_CMD_MOVETOTARGET:
1234 status = bot_cmd_movetotarget(this);
1236 case BOT_CMD_RESETGOAL:
1237 status = bot_cmd_resetgoal(this);
1240 status = bot_cmd_cc(this);
1243 status = bot_cmd_if(this);
1246 status = bot_cmd_else(this);
1249 status = bot_cmd_fi(this);
1251 case BOT_CMD_RESETAIM:
1252 status = bot_cmd_resetaim(this);
1255 status = bot_cmd_aim(this);
1257 case BOT_CMD_AIMTARGET:
1258 status = bot_cmd_aimtarget(this);
1260 case BOT_CMD_PRESSKEY:
1261 status = bot_cmd_presskey(this);
1263 case BOT_CMD_RELEASEKEY:
1264 status = bot_cmd_releasekey(this);
1266 case BOT_CMD_SELECTWEAPON:
1267 status = bot_cmd_select_weapon(this);
1269 case BOT_CMD_IMPULSE:
1270 status = bot_cmd_impulse(this);
1272 case BOT_CMD_BARRIER:
1273 status = bot_cmd_barrier(this);
1275 case BOT_CMD_CONSOLE:
1276 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1277 status = CMD_STATUS_FINISHED;
1280 status = bot_cmd_sound(this);
1282 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1283 status = bot_cmd_debug_assert_canfire(this);
1286 LOG_INFO(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1290 if (status==CMD_STATUS_ERROR)
1291 LOG_INFO(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1293 // Move execution pointer
1294 if(status==CMD_STATUS_EXECUTING)
1300 if(autocvar_g_debug_bot_commands)
1304 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1306 case BOT_CMD_PARAMETER_FLOAT:
1307 parms = ftos(bot_cmd.bot_cmd_parm_float);
1309 case BOT_CMD_PARAMETER_STRING:
1310 parms = bot_cmd.bot_cmd_parm_string;
1312 case BOT_CMD_PARAMETER_VECTOR:
1313 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1319 clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1322 bot_command_executed(this, true);
1325 if(status == CMD_STATUS_FINISHED)
1328 return CMD_STATUS_ERROR;
1331 // This function should be (the only) called directly from the bot ai loop
1332 int bot_execute_commands(entity this)
1337 f = bot_execute_commands_once(this);