1 #include "scripting.qh"
5 #include <common/state.qh>
6 #include <common/physics/player.qh>
12 .float bot_cmdqueuebuf_allocated;
13 .float bot_cmdqueuebuf;
14 .float bot_cmdqueuebuf_start;
15 .float bot_cmdqueuebuf_end;
17 void bot_clearqueue(entity bot)
19 if(!bot.bot_cmdqueuebuf_allocated)
21 buf_del(bot.bot_cmdqueuebuf);
22 bot.bot_cmdqueuebuf_allocated = false;
23 LOG_TRACE("bot ", bot.netname, " queue cleared");
26 void bot_queuecommand(entity bot, string cmdstring)
28 if(!bot.bot_cmdqueuebuf_allocated)
30 bot.bot_cmdqueuebuf = buf_create();
31 bot.bot_cmdqueuebuf_allocated = true;
32 bot.bot_cmdqueuebuf_start = 0;
33 bot.bot_cmdqueuebuf_end = 0;
36 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
38 // if the command was a "sound" command, precache the sound NOW
39 // this prevents lagging!
45 sp = strstrofs(cmdstring, " ", 0);
48 parm = substring(cmdstring, sp + 1, -1);
49 cmdstr = substring(cmdstring, 0, sp);
55 sp = strstrofs(parm, " ", 0);
58 parm = substring(parm, sp + 1, -1);
65 bot.bot_cmdqueuebuf_end += 1;
68 void bot_dequeuecommand(entity bot, float idx)
70 if(!bot.bot_cmdqueuebuf_allocated)
71 error("dequeuecommand but no queue allocated");
72 if(idx < bot.bot_cmdqueuebuf_start)
73 error("dequeueing a command in the past");
74 if(idx >= bot.bot_cmdqueuebuf_end)
75 error("dequeueing a command in the future");
76 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
77 if(idx == bot.bot_cmdqueuebuf_start)
78 bot.bot_cmdqueuebuf_start += 1;
79 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
83 string bot_readcommand(entity bot, float idx)
85 if(!bot.bot_cmdqueuebuf_allocated)
86 error("readcommand but no queue allocated");
87 if(idx < bot.bot_cmdqueuebuf_start)
88 error("reading a command in the past");
89 if(idx >= bot.bot_cmdqueuebuf_end)
90 error("reading a command in the future");
91 return bufstr_get(bot.bot_cmdqueuebuf, idx);
94 bool bot_havecommand(entity this, int idx)
96 if(!this.bot_cmdqueuebuf_allocated)
98 if(idx < this.bot_cmdqueuebuf_start)
100 if(idx >= this.bot_cmdqueuebuf_end)
105 const int MAX_BOT_PLACES = 4;
106 .float bot_places_count;
107 .entity bot_places[MAX_BOT_PLACES];
108 .string bot_placenames[MAX_BOT_PLACES];
109 entity bot_getplace(entity this, string placename)
112 if(substring(placename, 0, 1) == "@")
115 placename = substring(placename, 1, -1);
117 for(i = 0; i < this.bot_places_count; ++i)
118 if(this.(bot_placenames[i]) == placename)
119 return this.(bot_places[i]);
120 // now: i == this.bot_places_count
121 s = s2 = cvar_string(placename);
122 p = strstrofs(s2, " ", 0);
125 s = substring(s2, 0, p);
126 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
127 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
128 //print("places: ", placename, " := ", cvar_string(placename), "\n");
130 e = find(NULL, targetname, s);
132 LOG_INFO("invalid place ", s, "\n");
133 if(i < MAX_BOT_PLACES)
135 this.(bot_placenames[i]) = strzone(placename);
136 this.(bot_places[i]) = e;
137 this.bot_places_count += 1;
143 e = find(NULL, targetname, placename);
145 LOG_INFO("invalid place ", placename, "\n");
151 // Initialize global commands list
152 // NOTE: New commands should be initialized here
153 void bot_commands_init()
155 bot_cmd_string[BOT_CMD_NULL] = "";
156 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
158 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
159 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
161 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
162 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
164 bot_cmd_string[BOT_CMD_WAIT] = "wait";
165 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
167 bot_cmd_string[BOT_CMD_TURN] = "turn";
168 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
170 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
171 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
173 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
174 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
176 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
177 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
179 bot_cmd_string[BOT_CMD_CC] = "cc";
180 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
182 bot_cmd_string[BOT_CMD_IF] = "if";
183 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
185 bot_cmd_string[BOT_CMD_ELSE] = "else";
186 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
188 bot_cmd_string[BOT_CMD_FI] = "fi";
189 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
191 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
192 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
194 bot_cmd_string[BOT_CMD_AIM] = "aim";
195 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
197 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
198 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
200 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
201 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
203 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
204 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
206 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
207 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
209 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
210 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
212 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
213 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
215 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
216 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
218 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
219 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
221 bot_cmd_string[BOT_CMD_SOUND] = "sound";
222 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
224 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
225 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
227 bot_cmds_initialized = true;
230 // Returns first bot with matching name
231 entity find_bot_by_name(string name)
233 FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
241 // Returns a bot by number on list
242 entity find_bot_by_number(float number)
250 bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
253 if(IS_BOT_CLIENT(bot))
264 float bot_decodecommand(string cmdstring)
270 sp = strstrofs(cmdstring, " ", 0);
277 parm = substring(cmdstring, sp + 1, -1);
278 cmdstring = substring(cmdstring, 0, sp);
281 if(!bot_cmds_initialized)
285 for(i=1;i<BOT_CMD_COUNTER;++i)
287 if(bot_cmd_string[i]!=cmdstring)
290 cmd_parm_type = bot_cmd_parm_type[i];
292 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
294 LOG_INFO("ERROR: A parameter is required for this command\n");
298 // Load command into queue
299 bot_cmd.bot_cmd_type = i;
302 switch(cmd_parm_type)
304 case BOT_CMD_PARAMETER_FLOAT:
305 bot_cmd.bot_cmd_parm_float = stof(parm);
307 case BOT_CMD_PARAMETER_STRING:
308 if(bot_cmd.bot_cmd_parm_string)
309 strunzone(bot_cmd.bot_cmd_parm_string);
310 bot_cmd.bot_cmd_parm_string = strzone(parm);
312 case BOT_CMD_PARAMETER_VECTOR:
313 bot_cmd.bot_cmd_parm_vector = stov(parm);
320 LOG_INFO("ERROR: No such command '", cmdstring, "'\n");
324 void bot_cmdhelp(string scmd)
329 if(!bot_cmds_initialized)
332 for(i=1;i<BOT_CMD_COUNTER;++i)
334 if(bot_cmd_string[i]!=scmd)
337 ntype = bot_cmd_parm_type[i];
341 case BOT_CMD_PARAMETER_FLOAT:
342 stype = "float number";
344 case BOT_CMD_PARAMETER_STRING:
347 case BOT_CMD_PARAMETER_VECTOR:
355 LOG_INFO(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
357 LOG_INFO("Description: ");
361 LOG_INFO("Stops the bot completely. Any command other than 'continue' will be ignored.");
363 case BOT_CMD_CONTINUE:
364 LOG_INFO("Disable paused status");
367 LOG_INFO("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
369 case BOT_CMD_WAIT_UNTIL:
370 LOG_INFO("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
372 case BOT_CMD_BARRIER:
373 LOG_INFO("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
376 LOG_INFO("Look to the right or left N degrees. For turning to the left use positive numbers.");
379 LOG_INFO("Walk to an specific coordinate on the map. Usage: moveto \"x y z\"");
381 case BOT_CMD_MOVETOTARGET:
382 LOG_INFO("Walk to the specific target on the map");
384 case BOT_CMD_RESETGOAL:
385 LOG_INFO("Resets the goal stack");
388 LOG_INFO("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
391 LOG_INFO("Perform simple conditional execution.\n");
392 LOG_INFO("Syntax: \n");
393 LOG_INFO(" sv_cmd .. if \"condition\"\n");
394 LOG_INFO(" sv_cmd .. <instruction if true>\n");
395 LOG_INFO(" sv_cmd .. <instruction if true>\n");
396 LOG_INFO(" sv_cmd .. else\n");
397 LOG_INFO(" sv_cmd .. <instruction if false>\n");
398 LOG_INFO(" sv_cmd .. <instruction if false>\n");
399 LOG_INFO(" sv_cmd .. fi\n");
400 LOG_INFO("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
401 LOG_INFO(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
402 LOG_INFO("Fields: health, speed, flagcarrier\n");
403 LOG_INFO("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
405 case BOT_CMD_RESETAIM:
406 LOG_INFO("Points the aim to the coordinates x,y 0,0");
409 LOG_INFO("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
410 LOG_INFO("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
411 LOG_INFO("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
412 LOG_INFO(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
414 case BOT_CMD_AIMTARGET:
415 LOG_INFO("Points the aim to given target");
417 case BOT_CMD_PRESSKEY:
418 LOG_INFO("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
419 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
420 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
422 case BOT_CMD_RELEASEKEY:
423 LOG_INFO("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
426 LOG_INFO("play sound file at bot location");
428 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
429 LOG_INFO("verify the state of the weapon entity");
432 LOG_INFO("This command has no description yet.");
439 void bot_list_commands()
444 if(!bot_cmds_initialized)
447 LOG_INFO("List of all available commands:\n");
448 LOG_INFO(" Command - Parameter Type\n");
450 for(i=1;i<BOT_CMD_COUNTER;++i)
452 switch(bot_cmd_parm_type[i])
454 case BOT_CMD_PARAMETER_FLOAT:
455 ptype = "float number";
457 case BOT_CMD_PARAMETER_STRING:
460 case BOT_CMD_PARAMETER_VECTOR:
467 LOG_INFO(strcat(" ",bot_cmd_string[i]," - <",ptype,"> \n"));
472 .int bot_exec_status;
474 float bot_cmd_cc(entity this)
476 SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
477 return CMD_STATUS_FINISHED;
480 float bot_cmd_impulse(entity this)
482 this.impulse = bot_cmd.bot_cmd_parm_float;
483 return CMD_STATUS_FINISHED;
486 float bot_cmd_continue(entity this)
488 bot_relinkplayerlist();
489 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
490 return CMD_STATUS_FINISHED;
493 .float bot_cmd_wait_time;
494 float bot_cmd_wait(entity this)
496 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
498 if(time>=this.bot_cmd_wait_time)
500 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
501 return CMD_STATUS_FINISHED;
504 return CMD_STATUS_EXECUTING;
507 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
508 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
509 return CMD_STATUS_EXECUTING;
512 float bot_cmd_wait_until(entity this)
514 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
516 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
517 return CMD_STATUS_EXECUTING;
519 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
520 return CMD_STATUS_FINISHED;
523 float bot_cmd_barrier(entity this)
525 // 0 = no barrier, 1 = waiting, 2 = waiting finished
527 if(this.bot_barrier == 0) // initialization
529 this.bot_barrier = 1;
531 //this.colormod = '4 4 0';
534 if(this.bot_barrier == 1) // find other bots
536 FOREACH_CLIENT(it.isbot, LAMBDA(
537 if(it.bot_cmdqueuebuf_allocated)
538 if(it.bot_barrier != 1)
539 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
542 // all bots hit the barrier!
544 // acknowledge barrier
545 FOREACH_CLIENT(it.isbot, LAMBDA(it.bot_barrier = 2));
547 bot_barriertime = time;
550 // if we get here, the barrier is finished
552 this.bot_barrier = 0;
553 //this.colormod = '0 0 0';
555 return CMD_STATUS_FINISHED;
558 float bot_cmd_turn(entity this)
560 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
561 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
562 return CMD_STATUS_FINISHED;
565 float bot_cmd_select_weapon(entity this)
567 float id = bot_cmd.bot_cmd_parm_float;
569 if(id < WEP_FIRST || id > WEP_LAST)
570 return CMD_STATUS_ERROR;
572 if(client_hasweapon(this, Weapons_from(id), true, false))
573 PS(this).m_switchweapon = Weapons_from(id);
575 return CMD_STATUS_ERROR;
577 return CMD_STATUS_FINISHED;
580 .int bot_cmd_condition_status;
582 const int CMD_CONDITION_NONE = 0;
583 const int CMD_CONDITION_true = 1;
584 const int CMD_CONDITION_false = 2;
585 const int CMD_CONDITION_true_BLOCK = 4;
586 const int CMD_CONDITION_false_BLOCK = 8;
588 float bot_cmd_eval(entity this, string expr)
590 // Search for numbers
591 if(IS_DIGIT(substring(expr, 0, 1)))
595 if(substring(expr, 0, 5)=="cvar.")
596 return cvar(substring(expr, 5, strlen(expr)));
604 return vlen(this.velocity);
606 return ((this.flagcarried!=NULL));
609 LOG_INFO(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
613 float bot_cmd_if(entity this)
615 string expr, val_a, val_b;
618 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
620 // Only one "if" block is allowed at time
621 LOG_INFO("ERROR: Only one conditional block can be processed at time");
622 bot_clearqueue(this);
623 return CMD_STATUS_ERROR;
626 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
628 // search for operators
629 expr = bot_cmd.bot_cmd_parm_string;
631 cmpofs = strstrofs(expr,"=",0);
635 val_a = substring(expr,0,cmpofs);
636 val_b = substring(expr,cmpofs+1,strlen(expr));
638 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
639 this.bot_cmd_condition_status |= CMD_CONDITION_true;
641 this.bot_cmd_condition_status |= CMD_CONDITION_false;
643 return CMD_STATUS_FINISHED;
646 cmpofs = strstrofs(expr,">",0);
650 val_a = substring(expr,0,cmpofs);
651 val_b = substring(expr,cmpofs+1,strlen(expr));
653 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
654 this.bot_cmd_condition_status |= CMD_CONDITION_true;
656 this.bot_cmd_condition_status |= CMD_CONDITION_false;
658 return CMD_STATUS_FINISHED;
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(this, val_a)<bot_cmd_eval(this, val_b))
669 this.bot_cmd_condition_status |= CMD_CONDITION_true;
671 this.bot_cmd_condition_status |= CMD_CONDITION_false;
673 return CMD_STATUS_FINISHED;
676 if(bot_cmd_eval(this, expr))
677 this.bot_cmd_condition_status |= CMD_CONDITION_true;
679 this.bot_cmd_condition_status |= CMD_CONDITION_false;
681 return CMD_STATUS_FINISHED;
684 float bot_cmd_else(entity this)
686 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
687 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
688 return CMD_STATUS_FINISHED;
691 float bot_cmd_fi(entity this)
693 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
694 return CMD_STATUS_FINISHED;
697 float bot_cmd_resetaim(entity this)
699 this.v_angle = '0 0 0';
700 return CMD_STATUS_FINISHED;
703 .float bot_cmd_aim_begintime;
704 .float bot_cmd_aim_endtime;
705 .vector bot_cmd_aim_begin;
706 .vector bot_cmd_aim_end;
708 float bot_cmd_aim(entity this)
711 if(this.bot_cmd_aim_endtime)
715 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
716 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
718 if(time>=this.bot_cmd_aim_endtime)
720 this.bot_cmd_aim_endtime = 0;
721 return CMD_STATUS_FINISHED;
724 return CMD_STATUS_EXECUTING;
727 // New aiming direction
731 parms = bot_cmd.bot_cmd_parm_string;
733 tokens = tokenizebyseparator(parms, " ");
735 if(tokens<2||tokens>3)
736 return CMD_STATUS_ERROR;
738 step = (tokens == 3) ? stof(argv(2)) : 0;
742 this.v_angle_x -= stof(argv(1));
743 this.v_angle_y += stof(argv(0));
744 return CMD_STATUS_FINISHED;
747 this.bot_cmd_aim_begin = this.v_angle;
749 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
750 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
751 this.bot_cmd_aim_end_z = 0;
753 this.bot_cmd_aim_begintime = time;
754 this.bot_cmd_aim_endtime = time + step;
756 return CMD_STATUS_EXECUTING;
759 float bot_cmd_aimtarget(entity this)
761 if(this.bot_cmd_aim_endtime)
763 return bot_cmd_aim(this);
771 parms = bot_cmd.bot_cmd_parm_string;
773 tokens = tokenizebyseparator(parms, " ");
775 e = bot_getplace(this, argv(0));
777 return CMD_STATUS_ERROR;
779 v = e.origin + (e.mins + e.maxs) * 0.5;
783 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
784 this.v_angle_x = -this.v_angle.x;
785 return CMD_STATUS_FINISHED;
788 if(tokens<1||tokens>2)
789 return CMD_STATUS_ERROR;
791 step = stof(argv(1));
793 this.bot_cmd_aim_begin = this.v_angle;
794 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
795 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
797 this.bot_cmd_aim_begintime = time;
798 this.bot_cmd_aim_endtime = time + step;
800 return CMD_STATUS_EXECUTING;
805 const int BOT_CMD_KEY_NONE = 0;
806 const int BOT_CMD_KEY_FORWARD = BIT(0);
807 const int BOT_CMD_KEY_BACKWARD = BIT(1);
808 const int BOT_CMD_KEY_RIGHT = BIT(2);
809 const int BOT_CMD_KEY_LEFT = BIT(3);
810 const int BOT_CMD_KEY_JUMP = BIT(4);
811 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
812 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
813 const int BOT_CMD_KEY_USE = BIT(7);
814 const int BOT_CMD_KEY_HOOK = BIT(8);
815 const int BOT_CMD_KEY_CROUCH = BIT(9);
816 const int BOT_CMD_KEY_CHAT = BIT(10);
818 bool bot_presskeys(entity this)
820 this.movement = '0 0 0';
821 PHYS_INPUT_BUTTON_JUMP(this) = false;
822 PHYS_INPUT_BUTTON_CROUCH(this) = false;
823 PHYS_INPUT_BUTTON_ATCK(this) = false;
824 PHYS_INPUT_BUTTON_ATCK2(this) = false;
825 PHYS_INPUT_BUTTON_USE(this) = false;
826 PHYS_INPUT_BUTTON_HOOK(this) = false;
827 PHYS_INPUT_BUTTON_CHAT(this) = false;
829 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
832 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
833 this.movement_x = autocvar_sv_maxspeed;
834 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
835 this.movement_x = -autocvar_sv_maxspeed;
837 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
838 this.movement_y = autocvar_sv_maxspeed;
839 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
840 this.movement_y = -autocvar_sv_maxspeed;
842 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
843 PHYS_INPUT_BUTTON_JUMP(this) = true;
845 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
846 PHYS_INPUT_BUTTON_CROUCH(this) = true;
848 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
849 PHYS_INPUT_BUTTON_ATCK(this) = true;
851 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
852 PHYS_INPUT_BUTTON_ATCK2(this) = true;
854 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
855 PHYS_INPUT_BUTTON_USE(this) = true;
857 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
858 PHYS_INPUT_BUTTON_HOOK(this) = true;
860 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
861 PHYS_INPUT_BUTTON_CHAT(this) = true;
867 float bot_cmd_keypress_handler(entity this, string key, float enabled)
873 this.bot_cmd_keys = power2of(20) - 1; // >:)
875 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
879 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
880 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
883 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
888 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
889 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
892 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
897 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
898 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
901 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
906 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
907 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
910 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
914 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
916 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
920 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
922 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
926 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
928 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
932 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
934 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
938 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
940 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
944 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
946 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
950 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
952 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
958 return CMD_STATUS_FINISHED;
961 float bot_cmd_presskey(entity this)
965 key = bot_cmd.bot_cmd_parm_string;
967 bot_cmd_keypress_handler(this, key,true);
969 return CMD_STATUS_FINISHED;
972 float bot_cmd_releasekey(entity this)
976 key = bot_cmd.bot_cmd_parm_string;
978 return bot_cmd_keypress_handler(this, key,false);
981 float bot_cmd_pause(entity this)
983 PHYS_INPUT_BUTTON_DRAG(this) = false;
984 PHYS_INPUT_BUTTON_USE(this) = false;
985 PHYS_INPUT_BUTTON_ATCK(this) = false;
986 PHYS_INPUT_BUTTON_JUMP(this) = false;
987 PHYS_INPUT_BUTTON_HOOK(this) = false;
988 PHYS_INPUT_BUTTON_CHAT(this) = false;
989 PHYS_INPUT_BUTTON_ATCK2(this) = false;
990 PHYS_INPUT_BUTTON_CROUCH(this) = false;
992 this.movement = '0 0 0';
993 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
996 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
997 return CMD_STATUS_FINISHED;
1000 float bot_cmd_moveto(entity this)
1002 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1005 float bot_cmd_movetotarget(entity this)
1008 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1010 return CMD_STATUS_ERROR;
1011 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1014 float bot_cmd_resetgoal(entity this)
1016 return this.cmd_resetgoal(this);
1020 float bot_cmd_sound(entity this)
1023 f = bot_cmd.bot_cmd_parm_string;
1025 float n = tokenizebyseparator(f, " ");
1028 float chan = CH_WEAPON_B;
1029 float vol = VOL_BASE;
1030 float atten = ATTEN_MIN;
1033 sample = argv(n - 1);
1035 chan = stof(argv(0));
1037 vol = stof(argv(1));
1039 atten = stof(argv(2));
1042 _sound(this, chan, sample, vol, atten);
1044 return CMD_STATUS_FINISHED;
1048 float bot_cmd_debug_assert_canfire(entity this)
1050 float f = bot_cmd.bot_cmd_parm_float;
1052 int slot = 0; // TODO: unhardcode?
1053 .entity weaponentity = weaponentities[slot];
1054 if(this.(weaponentity).state != WS_READY)
1058 this.colormod = '0 8 8';
1059 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " wants to fire, inhibited by weaponentity state\n");
1062 else if(ATTACK_FINISHED(this, slot) > time)
1066 this.colormod = '8 0 8';
1067 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left)\n");
1070 else if(this.tuba_note)
1074 this.colormod = '8 0 0';
1075 LOG_INFO("Bot ", this.netname, " using ", this.weaponname, " wants to fire, bot still has an active tuba note\n");
1082 this.colormod = '8 8 0';
1083 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");
1087 return CMD_STATUS_FINISHED;
1092 void bot_command_executed(entity this, bool rm)
1099 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1101 this.bot_cmd_execution_index++;
1104 void bot_setcurrentcommand(entity this)
1108 if(!this.bot_cmd_current)
1110 this.bot_cmd_current = new_pure(bot_cmd);
1113 bot_cmd = this.bot_cmd_current;
1114 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1116 if(bot_havecommand(this, this.bot_cmd_execution_index))
1119 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1120 if(bot_decodecommand(cmdstring))
1122 bot_cmd.owner = this;
1123 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1127 // Invalid command, remove from queue
1129 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1130 this.bot_cmd_execution_index++;
1138 void bot_resetqueues()
1140 FOREACH_CLIENT(it.isbot, LAMBDA(
1141 it.bot_cmd_execution_index = 0;
1143 // also, cancel all barriers
1145 for(int i = 0; i < it.bot_places_count; ++i)
1147 strunzone(it.(bot_placenames[i]));
1148 it.(bot_placenames[i]) = string_null;
1150 it.bot_places_count = 0;
1153 bot_barriertime = time;
1156 // Here we map commands to functions and deal with complex interactions between commands and execution states
1157 // NOTE: Of course you need to include your commands here too :)
1158 float bot_execute_commands_once(entity this)
1160 float status, ispressingkey;
1163 bot_setcurrentcommand(this);
1165 // if we have no bot command, better return
1166 // old logic kept pressing previously pressed keys, but that has problems
1167 // (namely, it means you cannot make a bot "normal" ever again)
1168 // to keep a bot walking for a while, use the "wait" bot command
1172 // Ignore all commands except continue when the bot is paused
1173 if(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED)
1174 if(bot_cmd.bot_cmd_type!=BOT_CMD_CONTINUE)
1176 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1178 bot_command_executed(this, true);
1179 LOG_INFO( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1184 // Keep pressing keys raised by the "presskey" command
1185 ispressingkey = boolean(bot_presskeys(this));
1187 // Handle conditions
1188 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1189 if(this.bot_cmd_condition_status & CMD_CONDITION_true && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1191 bot_command_executed(this, true);
1194 else if(this.bot_cmd_condition_status & CMD_CONDITION_false && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1196 bot_command_executed(this, true);
1200 // Map commands to functions
1201 switch(bot_cmd.bot_cmd_type)
1204 return ispressingkey;
1207 status = bot_cmd_pause(this);
1209 case BOT_CMD_CONTINUE:
1210 status = bot_cmd_continue(this);
1213 status = bot_cmd_wait(this);
1215 case BOT_CMD_WAIT_UNTIL:
1216 status = bot_cmd_wait_until(this);
1219 status = bot_cmd_turn(this);
1221 case BOT_CMD_MOVETO:
1222 status = bot_cmd_moveto(this);
1224 case BOT_CMD_MOVETOTARGET:
1225 status = bot_cmd_movetotarget(this);
1227 case BOT_CMD_RESETGOAL:
1228 status = bot_cmd_resetgoal(this);
1231 status = bot_cmd_cc(this);
1234 status = bot_cmd_if(this);
1237 status = bot_cmd_else(this);
1240 status = bot_cmd_fi(this);
1242 case BOT_CMD_RESETAIM:
1243 status = bot_cmd_resetaim(this);
1246 status = bot_cmd_aim(this);
1248 case BOT_CMD_AIMTARGET:
1249 status = bot_cmd_aimtarget(this);
1251 case BOT_CMD_PRESSKEY:
1252 status = bot_cmd_presskey(this);
1254 case BOT_CMD_RELEASEKEY:
1255 status = bot_cmd_releasekey(this);
1257 case BOT_CMD_SELECTWEAPON:
1258 status = bot_cmd_select_weapon(this);
1260 case BOT_CMD_IMPULSE:
1261 status = bot_cmd_impulse(this);
1263 case BOT_CMD_BARRIER:
1264 status = bot_cmd_barrier(this);
1266 case BOT_CMD_CONSOLE:
1267 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1268 status = CMD_STATUS_FINISHED;
1271 status = bot_cmd_sound(this);
1273 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1274 status = bot_cmd_debug_assert_canfire(this);
1277 LOG_INFO(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1281 if (status==CMD_STATUS_ERROR)
1282 LOG_INFO(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1284 // Move execution pointer
1285 if(status==CMD_STATUS_EXECUTING)
1291 if(autocvar_g_debug_bot_commands)
1295 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1297 case BOT_CMD_PARAMETER_FLOAT:
1298 parms = ftos(bot_cmd.bot_cmd_parm_float);
1300 case BOT_CMD_PARAMETER_STRING:
1301 parms = bot_cmd.bot_cmd_parm_string;
1303 case BOT_CMD_PARAMETER_VECTOR:
1304 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1310 clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1313 bot_command_executed(this, true);
1316 if(status == CMD_STATUS_FINISHED)
1319 return CMD_STATUS_ERROR;
1322 // This function should be (the only) called directly from the bot ai loop
1323 int bot_execute_commands(entity this)
1328 f = bot_execute_commands_once(this);