1 #include "scripting.qh"
5 #include <common/state.qh>
6 #include <common/physics/player.qh>
7 #include <common/wepent.qh>
13 .float bot_cmdqueuebuf_allocated;
14 .float bot_cmdqueuebuf;
15 .float bot_cmdqueuebuf_start;
16 .float bot_cmdqueuebuf_end;
18 void bot_clearqueue(entity bot)
20 if(!bot.bot_cmdqueuebuf_allocated)
22 buf_del(bot.bot_cmdqueuebuf);
23 bot.bot_cmdqueuebuf_allocated = false;
24 LOG_TRACE("bot ", bot.netname, " queue cleared");
27 void bot_queuecommand(entity bot, string cmdstring)
29 if(!bot.bot_cmdqueuebuf_allocated)
31 bot.bot_cmdqueuebuf = buf_create();
32 bot.bot_cmdqueuebuf_allocated = true;
33 bot.bot_cmdqueuebuf_start = 0;
34 bot.bot_cmdqueuebuf_end = 0;
37 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
39 // if the command was a "sound" command, precache the sound NOW
40 // this prevents lagging!
46 sp = strstrofs(cmdstring, " ", 0);
49 parm = substring(cmdstring, sp + 1, -1);
50 cmdstr = substring(cmdstring, 0, sp);
56 sp = strstrofs(parm, " ", 0);
59 parm = substring(parm, sp + 1, -1);
66 bot.bot_cmdqueuebuf_end += 1;
69 void bot_dequeuecommand(entity bot, float idx)
71 if(!bot.bot_cmdqueuebuf_allocated)
72 error("dequeuecommand but no queue allocated");
73 if(idx < bot.bot_cmdqueuebuf_start)
74 error("dequeueing a command in the past");
75 if(idx >= bot.bot_cmdqueuebuf_end)
76 error("dequeueing a command in the future");
77 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
78 if(idx == bot.bot_cmdqueuebuf_start)
79 bot.bot_cmdqueuebuf_start += 1;
80 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
84 string bot_readcommand(entity bot, float idx)
86 if(!bot.bot_cmdqueuebuf_allocated)
87 error("readcommand but no queue allocated");
88 if(idx < bot.bot_cmdqueuebuf_start)
89 error("reading a command in the past");
90 if(idx >= bot.bot_cmdqueuebuf_end)
91 error("reading a command in the future");
92 return bufstr_get(bot.bot_cmdqueuebuf, idx);
95 bool bot_havecommand(entity this, int idx)
97 if(!this.bot_cmdqueuebuf_allocated)
99 if(idx < this.bot_cmdqueuebuf_start)
101 if(idx >= this.bot_cmdqueuebuf_end)
106 const int MAX_BOT_PLACES = 4;
107 .float bot_places_count;
108 .entity bot_places[MAX_BOT_PLACES];
109 .string bot_placenames[MAX_BOT_PLACES];
110 entity bot_getplace(entity this, string placename)
113 if(substring(placename, 0, 1) == "@")
116 placename = substring(placename, 1, -1);
118 for(i = 0; i < this.bot_places_count; ++i)
119 if(this.(bot_placenames[i]) == placename)
120 return this.(bot_places[i]);
121 // now: i == this.bot_places_count
122 s = s2 = cvar_string(placename);
123 p = strstrofs(s2, " ", 0);
126 s = substring(s2, 0, p);
127 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
128 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
129 //print("places: ", placename, " := ", cvar_string(placename), "\n");
131 e = find(NULL, targetname, s);
133 LOG_INFO("invalid place ", s, "\n");
134 if(i < MAX_BOT_PLACES)
136 this.(bot_placenames[i]) = strzone(placename);
137 this.(bot_places[i]) = e;
138 this.bot_places_count += 1;
144 e = find(NULL, targetname, placename);
146 LOG_INFO("invalid place ", placename, "\n");
152 // Initialize global commands list
153 // NOTE: New commands should be initialized here
154 void bot_commands_init()
156 bot_cmd_string[BOT_CMD_NULL] = "";
157 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
159 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
160 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
162 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
163 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
165 bot_cmd_string[BOT_CMD_WAIT] = "wait";
166 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
168 bot_cmd_string[BOT_CMD_TURN] = "turn";
169 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
171 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
172 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
174 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
175 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
177 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
178 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
180 bot_cmd_string[BOT_CMD_CC] = "cc";
181 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
183 bot_cmd_string[BOT_CMD_IF] = "if";
184 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
186 bot_cmd_string[BOT_CMD_ELSE] = "else";
187 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
189 bot_cmd_string[BOT_CMD_FI] = "fi";
190 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
192 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
193 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
195 bot_cmd_string[BOT_CMD_AIM] = "aim";
196 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
198 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
199 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
201 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
202 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
204 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
205 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
207 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
208 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
210 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
211 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
213 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
214 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
216 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
217 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
219 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
220 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
222 bot_cmd_string[BOT_CMD_SOUND] = "sound";
223 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
225 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
226 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
228 bot_cmds_initialized = true;
231 // Returns first bot with matching name
232 entity find_bot_by_name(string name)
234 FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
242 // Returns a bot by number on list
243 entity find_bot_by_number(float number)
251 bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
254 if(IS_BOT_CLIENT(bot))
265 float bot_decodecommand(string cmdstring)
271 sp = strstrofs(cmdstring, " ", 0);
278 parm = substring(cmdstring, sp + 1, -1);
279 cmdstring = substring(cmdstring, 0, sp);
282 if(!bot_cmds_initialized)
286 for(i=1;i<BOT_CMD_COUNTER;++i)
288 if(bot_cmd_string[i]!=cmdstring)
291 cmd_parm_type = bot_cmd_parm_type[i];
293 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
295 LOG_INFO("ERROR: A parameter is required for this command\n");
299 // Load command into queue
300 bot_cmd.bot_cmd_type = i;
303 switch(cmd_parm_type)
305 case BOT_CMD_PARAMETER_FLOAT:
306 bot_cmd.bot_cmd_parm_float = stof(parm);
308 case BOT_CMD_PARAMETER_STRING:
309 if(bot_cmd.bot_cmd_parm_string)
310 strunzone(bot_cmd.bot_cmd_parm_string);
311 bot_cmd.bot_cmd_parm_string = strzone(parm);
313 case BOT_CMD_PARAMETER_VECTOR:
314 if(substring(parm, 0, 1) != "\'")
316 LOG_INFOF("ERROR: expected vector type \'x y z\', got %s\n", parm);
319 bot_cmd.bot_cmd_parm_vector = stov(parm);
326 LOG_INFO("ERROR: No such command '", cmdstring, "'\n");
330 void bot_cmdhelp(string scmd)
335 if(!bot_cmds_initialized)
338 for(i=1;i<BOT_CMD_COUNTER;++i)
340 if(bot_cmd_string[i]!=scmd)
343 ntype = bot_cmd_parm_type[i];
347 case BOT_CMD_PARAMETER_FLOAT:
348 stype = "float number";
350 case BOT_CMD_PARAMETER_STRING:
353 case BOT_CMD_PARAMETER_VECTOR:
361 LOG_INFO(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
363 LOG_INFO("Description: ");
367 LOG_INFO("Stops the bot completely. Any command other than 'continue' will be ignored.");
369 case BOT_CMD_CONTINUE:
370 LOG_INFO("Disable paused status");
373 LOG_INFO("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
375 case BOT_CMD_WAIT_UNTIL:
376 LOG_INFO("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
378 case BOT_CMD_BARRIER:
379 LOG_INFO("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
382 LOG_INFO("Look to the right or left N degrees. For turning to the left use positive numbers.");
385 LOG_INFO("Walk to an specific coordinate on the map. Usage: moveto \'x y z\'");
387 case BOT_CMD_MOVETOTARGET:
388 LOG_INFO("Walk to the specific target on the map");
390 case BOT_CMD_RESETGOAL:
391 LOG_INFO("Resets the goal stack");
394 LOG_INFO("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
397 LOG_INFO("Perform simple conditional execution.\n");
398 LOG_INFO("Syntax: \n");
399 LOG_INFO(" sv_cmd .. if \"condition\"\n");
400 LOG_INFO(" sv_cmd .. <instruction if true>\n");
401 LOG_INFO(" sv_cmd .. <instruction if true>\n");
402 LOG_INFO(" sv_cmd .. else\n");
403 LOG_INFO(" sv_cmd .. <instruction if false>\n");
404 LOG_INFO(" sv_cmd .. <instruction if false>\n");
405 LOG_INFO(" sv_cmd .. fi\n");
406 LOG_INFO("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
407 LOG_INFO(" Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
408 LOG_INFO("Fields: health, speed, flagcarrier\n");
409 LOG_INFO("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
411 case BOT_CMD_RESETAIM:
412 LOG_INFO("Points the aim to the coordinates x,y 0,0");
415 LOG_INFO("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
416 LOG_INFO("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
417 LOG_INFO("Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
418 LOG_INFO(" aim \"0 90 2\" // Will gradually look to the sky in the next two seconds");
420 case BOT_CMD_AIMTARGET:
421 LOG_INFO("Points the aim to given target");
423 case BOT_CMD_PRESSKEY:
424 LOG_INFO("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
425 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
426 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
428 case BOT_CMD_RELEASEKEY:
429 LOG_INFO("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
432 LOG_INFO("play sound file at bot location");
434 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
435 LOG_INFO("verify the state of the weapon entity");
438 LOG_INFO("This command has no description yet.");
445 void bot_list_commands()
450 if(!bot_cmds_initialized)
453 LOG_INFO("List of all available commands:\n");
454 LOG_INFO(" Command - Parameter Type\n");
456 for(i=1;i<BOT_CMD_COUNTER;++i)
458 switch(bot_cmd_parm_type[i])
460 case BOT_CMD_PARAMETER_FLOAT:
461 ptype = "float number";
463 case BOT_CMD_PARAMETER_STRING:
466 case BOT_CMD_PARAMETER_VECTOR:
473 LOG_INFO(strcat(" ",bot_cmd_string[i]," - <",ptype,"> \n"));
478 .int bot_exec_status;
480 float bot_cmd_cc(entity this)
482 SV_ParseClientCommand(this, 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 bot_relinkplayerlist();
495 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
496 return CMD_STATUS_FINISHED;
499 .float bot_cmd_wait_time;
500 float bot_cmd_wait(entity this)
502 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
504 if(time>=this.bot_cmd_wait_time)
506 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
507 return CMD_STATUS_FINISHED;
510 return CMD_STATUS_EXECUTING;
513 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
514 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
515 return CMD_STATUS_EXECUTING;
518 float bot_cmd_wait_until(entity this)
520 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
522 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
523 return CMD_STATUS_EXECUTING;
525 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
526 return CMD_STATUS_FINISHED;
529 float bot_cmd_barrier(entity this)
531 // 0 = no barrier, 1 = waiting, 2 = waiting finished
533 if(this.bot_barrier == 0) // initialization
535 this.bot_barrier = 1;
537 //this.colormod = '4 4 0';
540 if(this.bot_barrier == 1) // find other bots
542 FOREACH_CLIENT(it.isbot, LAMBDA(
543 if(it.bot_cmdqueuebuf_allocated)
544 if(it.bot_barrier != 1)
545 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
548 // all bots hit the barrier!
550 // acknowledge barrier
551 FOREACH_CLIENT(it.isbot, LAMBDA(it.bot_barrier = 2));
553 bot_barriertime = time;
556 // if we get here, the barrier is finished
558 this.bot_barrier = 0;
559 //this.colormod = '0 0 0';
561 return CMD_STATUS_FINISHED;
564 float bot_cmd_turn(entity this)
566 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
567 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
568 return CMD_STATUS_FINISHED;
571 float bot_cmd_select_weapon(entity this)
573 float id = bot_cmd.bot_cmd_parm_float;
575 if(id < WEP_FIRST || id > WEP_LAST)
576 return CMD_STATUS_ERROR;
578 bool success = false;
580 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
582 .entity weaponentity = weaponentities[slot];
583 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
586 if(client_hasweapon(this, Weapons_from(id), weaponentity, true, false))
589 this.(weaponentity).m_switchweapon = Weapons_from(id);
594 return CMD_STATUS_ERROR;
596 return CMD_STATUS_FINISHED;
599 .int bot_cmd_condition_status;
601 const int CMD_CONDITION_NONE = 0;
602 const int CMD_CONDITION_true = 1;
603 const int CMD_CONDITION_false = 2;
604 const int CMD_CONDITION_true_BLOCK = 4;
605 const int CMD_CONDITION_false_BLOCK = 8;
607 float bot_cmd_eval(entity this, string expr)
609 // Search for numbers
610 if(IS_DIGIT(substring(expr, 0, 1)))
614 if(substring(expr, 0, 5)=="cvar.")
615 return cvar(substring(expr, 5, strlen(expr)));
623 return vlen(this.velocity);
625 return ((this.flagcarried!=NULL));
628 LOG_INFO(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
632 float bot_cmd_if(entity this)
634 string expr, val_a, val_b;
637 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
639 // Only one "if" block is allowed at time
640 LOG_INFO("ERROR: Only one conditional block can be processed at time");
641 bot_clearqueue(this);
642 return CMD_STATUS_ERROR;
645 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
647 // search for operators
648 expr = bot_cmd.bot_cmd_parm_string;
650 cmpofs = strstrofs(expr,"=",0);
654 val_a = substring(expr,0,cmpofs);
655 val_b = substring(expr,cmpofs+1,strlen(expr));
657 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
658 this.bot_cmd_condition_status |= CMD_CONDITION_true;
660 this.bot_cmd_condition_status |= CMD_CONDITION_false;
662 return CMD_STATUS_FINISHED;
665 cmpofs = strstrofs(expr,">",0);
669 val_a = substring(expr,0,cmpofs);
670 val_b = substring(expr,cmpofs+1,strlen(expr));
672 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
673 this.bot_cmd_condition_status |= CMD_CONDITION_true;
675 this.bot_cmd_condition_status |= CMD_CONDITION_false;
677 return CMD_STATUS_FINISHED;
680 cmpofs = strstrofs(expr,"<",0);
684 val_a = substring(expr,0,cmpofs);
685 val_b = substring(expr,cmpofs+1,strlen(expr));
687 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
688 this.bot_cmd_condition_status |= CMD_CONDITION_true;
690 this.bot_cmd_condition_status |= CMD_CONDITION_false;
692 return CMD_STATUS_FINISHED;
695 if(bot_cmd_eval(this, expr))
696 this.bot_cmd_condition_status |= CMD_CONDITION_true;
698 this.bot_cmd_condition_status |= CMD_CONDITION_false;
700 return CMD_STATUS_FINISHED;
703 float bot_cmd_else(entity this)
705 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
706 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
707 return CMD_STATUS_FINISHED;
710 float bot_cmd_fi(entity this)
712 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
713 return CMD_STATUS_FINISHED;
716 float bot_cmd_resetaim(entity this)
718 this.v_angle = '0 0 0';
719 return CMD_STATUS_FINISHED;
722 .float bot_cmd_aim_begintime;
723 .float bot_cmd_aim_endtime;
724 .vector bot_cmd_aim_begin;
725 .vector bot_cmd_aim_end;
727 float bot_cmd_aim(entity this)
730 if(this.bot_cmd_aim_endtime)
734 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
735 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
737 if(time>=this.bot_cmd_aim_endtime)
739 this.bot_cmd_aim_endtime = 0;
740 return CMD_STATUS_FINISHED;
743 return CMD_STATUS_EXECUTING;
746 // New aiming direction
750 parms = bot_cmd.bot_cmd_parm_string;
752 tokens = tokenizebyseparator(parms, " ");
754 if(tokens<2||tokens>3)
755 return CMD_STATUS_ERROR;
757 step = (tokens == 3) ? stof(argv(2)) : 0;
761 this.v_angle_x -= stof(argv(1));
762 this.v_angle_y += stof(argv(0));
763 return CMD_STATUS_FINISHED;
766 this.bot_cmd_aim_begin = this.v_angle;
768 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
769 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
770 this.bot_cmd_aim_end_z = 0;
772 this.bot_cmd_aim_begintime = time;
773 this.bot_cmd_aim_endtime = time + step;
775 return CMD_STATUS_EXECUTING;
778 float bot_cmd_aimtarget(entity this)
780 if(this.bot_cmd_aim_endtime)
782 return bot_cmd_aim(this);
790 parms = bot_cmd.bot_cmd_parm_string;
792 tokens = tokenizebyseparator(parms, " ");
794 e = bot_getplace(this, argv(0));
796 return CMD_STATUS_ERROR;
798 v = e.origin + (e.mins + e.maxs) * 0.5;
802 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
803 this.v_angle_x = -this.v_angle.x;
804 return CMD_STATUS_FINISHED;
807 if(tokens<1||tokens>2)
808 return CMD_STATUS_ERROR;
810 step = stof(argv(1));
812 this.bot_cmd_aim_begin = this.v_angle;
813 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
814 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
816 this.bot_cmd_aim_begintime = time;
817 this.bot_cmd_aim_endtime = time + step;
819 return CMD_STATUS_EXECUTING;
824 const int BOT_CMD_KEY_NONE = 0;
825 const int BOT_CMD_KEY_FORWARD = BIT(0);
826 const int BOT_CMD_KEY_BACKWARD = BIT(1);
827 const int BOT_CMD_KEY_RIGHT = BIT(2);
828 const int BOT_CMD_KEY_LEFT = BIT(3);
829 const int BOT_CMD_KEY_JUMP = BIT(4);
830 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
831 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
832 const int BOT_CMD_KEY_USE = BIT(7);
833 const int BOT_CMD_KEY_HOOK = BIT(8);
834 const int BOT_CMD_KEY_CROUCH = BIT(9);
835 const int BOT_CMD_KEY_CHAT = BIT(10);
837 bool bot_presskeys(entity this)
839 this.movement = '0 0 0';
840 PHYS_INPUT_BUTTON_JUMP(this) = false;
841 PHYS_INPUT_BUTTON_CROUCH(this) = false;
842 PHYS_INPUT_BUTTON_ATCK(this) = false;
843 PHYS_INPUT_BUTTON_ATCK2(this) = false;
844 PHYS_INPUT_BUTTON_USE(this) = false;
845 PHYS_INPUT_BUTTON_HOOK(this) = false;
846 PHYS_INPUT_BUTTON_CHAT(this) = false;
848 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
851 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
852 this.movement_x = autocvar_sv_maxspeed;
853 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
854 this.movement_x = -autocvar_sv_maxspeed;
856 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
857 this.movement_y = autocvar_sv_maxspeed;
858 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
859 this.movement_y = -autocvar_sv_maxspeed;
861 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
862 PHYS_INPUT_BUTTON_JUMP(this) = true;
864 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
865 PHYS_INPUT_BUTTON_CROUCH(this) = true;
867 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
868 PHYS_INPUT_BUTTON_ATCK(this) = true;
870 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
871 PHYS_INPUT_BUTTON_ATCK2(this) = true;
873 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
874 PHYS_INPUT_BUTTON_USE(this) = true;
876 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
877 PHYS_INPUT_BUTTON_HOOK(this) = true;
879 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
880 PHYS_INPUT_BUTTON_CHAT(this) = true;
886 float bot_cmd_keypress_handler(entity this, string key, float enabled)
892 this.bot_cmd_keys = power2of(20) - 1; // >:)
894 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
898 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
899 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
902 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
907 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
908 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
911 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
916 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
917 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
920 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
925 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
926 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
929 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
933 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
935 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
939 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
941 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
945 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
947 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
951 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
953 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
957 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
959 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
963 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
965 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
969 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
971 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
977 return CMD_STATUS_FINISHED;
980 float bot_cmd_presskey(entity this)
984 key = bot_cmd.bot_cmd_parm_string;
986 bot_cmd_keypress_handler(this, key,true);
988 return CMD_STATUS_FINISHED;
991 float bot_cmd_releasekey(entity this)
995 key = bot_cmd.bot_cmd_parm_string;
997 return bot_cmd_keypress_handler(this, key,false);
1000 float bot_cmd_pause(entity this)
1002 PHYS_INPUT_BUTTON_DRAG(this) = false;
1003 PHYS_INPUT_BUTTON_USE(this) = false;
1004 PHYS_INPUT_BUTTON_ATCK(this) = false;
1005 PHYS_INPUT_BUTTON_JUMP(this) = false;
1006 PHYS_INPUT_BUTTON_HOOK(this) = false;
1007 PHYS_INPUT_BUTTON_CHAT(this) = false;
1008 PHYS_INPUT_BUTTON_ATCK2(this) = false;
1009 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1011 this.movement = '0 0 0';
1012 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1015 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1016 return CMD_STATUS_FINISHED;
1019 float bot_cmd_moveto(entity this)
1021 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1024 float bot_cmd_movetotarget(entity this)
1027 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1029 return CMD_STATUS_ERROR;
1030 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1033 float bot_cmd_resetgoal(entity this)
1035 return this.cmd_resetgoal(this);
1039 float bot_cmd_sound(entity this)
1042 f = bot_cmd.bot_cmd_parm_string;
1044 float n = tokenizebyseparator(f, " ");
1047 float chan = CH_WEAPON_B;
1048 float vol = VOL_BASE;
1049 float atten = ATTEN_MIN;
1052 sample = argv(n - 1);
1054 chan = stof(argv(0));
1056 vol = stof(argv(1));
1058 atten = stof(argv(2));
1061 _sound(this, chan, sample, vol, atten);
1063 return CMD_STATUS_FINISHED;
1067 float bot_cmd_debug_assert_canfire(entity this)
1069 float f = bot_cmd.bot_cmd_parm_float;
1071 int slot = 0; // TODO: unhardcode?
1072 .entity weaponentity = weaponentities[slot];
1073 if(this.(weaponentity).state != WS_READY)
1077 this.colormod = '0 8 8';
1078 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state\n");
1081 else if(ATTACK_FINISHED(this, slot) > time)
1085 this.colormod = '8 0 8';
1086 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left)\n");
1089 else if(this.(weaponentity).tuba_note)
1093 this.colormod = '8 0 0';
1094 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note\n");
1101 this.colormod = '8 8 0';
1102 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left\n");
1106 return CMD_STATUS_FINISHED;
1111 void bot_command_executed(entity this, bool rm)
1118 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1120 this.bot_cmd_execution_index++;
1123 void bot_setcurrentcommand(entity this)
1127 if(!this.bot_cmd_current)
1129 this.bot_cmd_current = new_pure(bot_cmd);
1132 bot_cmd = this.bot_cmd_current;
1133 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1135 if(bot_havecommand(this, this.bot_cmd_execution_index))
1138 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1139 if(bot_decodecommand(cmdstring))
1141 bot_cmd.owner = this;
1142 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1146 // Invalid command, remove from queue
1148 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1149 this.bot_cmd_execution_index++;
1157 void bot_resetqueues()
1159 FOREACH_CLIENT(it.isbot, LAMBDA(
1160 it.bot_cmd_execution_index = 0;
1162 // also, cancel all barriers
1164 for(int i = 0; i < it.bot_places_count; ++i)
1166 strunzone(it.(bot_placenames[i]));
1167 it.(bot_placenames[i]) = string_null;
1169 it.bot_places_count = 0;
1172 bot_barriertime = time;
1175 // Here we map commands to functions and deal with complex interactions between commands and execution states
1176 // NOTE: Of course you need to include your commands here too :)
1177 float bot_execute_commands_once(entity this)
1179 float status, ispressingkey;
1182 bot_setcurrentcommand(this);
1184 // Ignore all commands except continue when the bot is paused
1185 if(!(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1187 // if we have no bot command, better return
1188 // old logic kept pressing previously pressed keys, but that has problems
1189 // (namely, it means you cannot make a bot "normal" ever again)
1190 // to keep a bot walking for a while, use the "wait" bot command
1191 if(bot_cmd == world)
1194 else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1196 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1198 bot_command_executed(this, true);
1199 LOG_INFO( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1204 // Keep pressing keys raised by the "presskey" command
1205 ispressingkey = boolean(bot_presskeys(this));
1207 // Handle conditions
1208 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1209 if(this.bot_cmd_condition_status & CMD_CONDITION_true && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1211 bot_command_executed(this, true);
1214 else if(this.bot_cmd_condition_status & CMD_CONDITION_false && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1216 bot_command_executed(this, true);
1220 // Map commands to functions
1221 switch(bot_cmd.bot_cmd_type)
1224 return ispressingkey;
1227 status = bot_cmd_pause(this);
1229 case BOT_CMD_CONTINUE:
1230 status = bot_cmd_continue(this);
1233 status = bot_cmd_wait(this);
1235 case BOT_CMD_WAIT_UNTIL:
1236 status = bot_cmd_wait_until(this);
1239 status = bot_cmd_turn(this);
1241 case BOT_CMD_MOVETO:
1242 status = bot_cmd_moveto(this);
1244 case BOT_CMD_MOVETOTARGET:
1245 status = bot_cmd_movetotarget(this);
1247 case BOT_CMD_RESETGOAL:
1248 status = bot_cmd_resetgoal(this);
1251 status = bot_cmd_cc(this);
1254 status = bot_cmd_if(this);
1257 status = bot_cmd_else(this);
1260 status = bot_cmd_fi(this);
1262 case BOT_CMD_RESETAIM:
1263 status = bot_cmd_resetaim(this);
1266 status = bot_cmd_aim(this);
1268 case BOT_CMD_AIMTARGET:
1269 status = bot_cmd_aimtarget(this);
1271 case BOT_CMD_PRESSKEY:
1272 status = bot_cmd_presskey(this);
1274 case BOT_CMD_RELEASEKEY:
1275 status = bot_cmd_releasekey(this);
1277 case BOT_CMD_SELECTWEAPON:
1278 status = bot_cmd_select_weapon(this);
1280 case BOT_CMD_IMPULSE:
1281 status = bot_cmd_impulse(this);
1283 case BOT_CMD_BARRIER:
1284 status = bot_cmd_barrier(this);
1286 case BOT_CMD_CONSOLE:
1287 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1288 status = CMD_STATUS_FINISHED;
1291 status = bot_cmd_sound(this);
1293 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1294 status = bot_cmd_debug_assert_canfire(this);
1297 LOG_INFO(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1301 if (status==CMD_STATUS_ERROR)
1302 LOG_INFO(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1304 // Move execution pointer
1305 if(status==CMD_STATUS_EXECUTING)
1311 if(autocvar_g_debug_bot_commands)
1315 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1317 case BOT_CMD_PARAMETER_FLOAT:
1318 parms = ftos(bot_cmd.bot_cmd_parm_float);
1320 case BOT_CMD_PARAMETER_STRING:
1321 parms = bot_cmd.bot_cmd_parm_string;
1323 case BOT_CMD_PARAMETER_VECTOR:
1324 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1330 clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1333 bot_command_executed(this, true);
1336 if(status == CMD_STATUS_FINISHED)
1339 return CMD_STATUS_ERROR;
1342 // This function should be (the only) called directly from the bot ai loop
1343 int bot_execute_commands(entity this)
1348 f = bot_execute_commands_once(this);