1 #include "scripting.qh"
3 #include <common/gamemodes/gamemode/ctf/sv_ctf.qh>
4 #include <common/physics/player.qh>
5 #include <common/state.qh>
6 #include <common/stats.qh>
7 #include <common/weapons/_all.qh>
8 #include <common/wepent.qh>
9 #include <server/bot/default/bot.qh>
10 #include <server/bot/default/cvars.qh>
11 #include <server/weapons/selection.qh>
12 #include <server/weapons/weaponsystem.qh>
16 .float bot_cmdqueuebuf_allocated;
17 .float bot_cmdqueuebuf;
18 .float bot_cmdqueuebuf_start;
19 .float bot_cmdqueuebuf_end;
21 void bot_clearqueue(entity bot)
23 if(!bot.bot_cmdqueuebuf_allocated)
25 buf_del(bot.bot_cmdqueuebuf);
26 bot.bot_cmdqueuebuf_allocated = false;
27 LOG_TRACE("bot ", bot.netname, " queue cleared");
30 void bot_queuecommand(entity bot, string cmdstring)
32 if(!bot.bot_cmdqueuebuf_allocated)
34 bot.bot_cmdqueuebuf = buf_create();
35 bot.bot_cmdqueuebuf_allocated = true;
36 bot.bot_cmdqueuebuf_start = 0;
37 bot.bot_cmdqueuebuf_end = 0;
40 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
42 // if the command was a "sound" command, precache the sound NOW
43 // this prevents lagging!
49 sp = strstrofs(cmdstring, " ", 0);
52 parm = substring(cmdstring, sp + 1, -1);
53 cmdstr = substring(cmdstring, 0, sp);
59 sp = strstrofs(parm, " ", 0);
62 parm = substring(parm, sp + 1, -1);
69 bot.bot_cmdqueuebuf_end += 1;
72 void bot_dequeuecommand(entity bot, float idx)
74 if(!bot.bot_cmdqueuebuf_allocated)
75 error("dequeuecommand but no queue allocated");
76 if(idx < bot.bot_cmdqueuebuf_start)
77 error("dequeueing a command in the past");
78 if(idx >= bot.bot_cmdqueuebuf_end)
79 error("dequeueing a command in the future");
80 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
81 if(idx == bot.bot_cmdqueuebuf_start)
82 bot.bot_cmdqueuebuf_start += 1;
83 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
87 string bot_readcommand(entity bot, float idx)
89 if(!bot.bot_cmdqueuebuf_allocated)
90 error("readcommand but no queue allocated");
91 if(idx < bot.bot_cmdqueuebuf_start)
92 error("reading a command in the past");
93 if(idx >= bot.bot_cmdqueuebuf_end)
94 error("reading a command in the future");
95 return bufstr_get(bot.bot_cmdqueuebuf, idx);
98 bool bot_havecommand(entity this, int idx)
100 if(!this.bot_cmdqueuebuf_allocated)
102 if(idx < this.bot_cmdqueuebuf_start)
104 if(idx >= this.bot_cmdqueuebuf_end)
109 const int MAX_BOT_PLACES = 4;
110 .float bot_places_count;
111 .entity bot_places[MAX_BOT_PLACES];
112 .string bot_placenames[MAX_BOT_PLACES];
113 entity bot_getplace(entity this, string placename)
116 if(substring(placename, 0, 1) == "@")
119 placename = substring(placename, 1, -1);
121 for(i = 0; i < this.bot_places_count; ++i)
122 if(this.(bot_placenames[i]) == placename)
123 return this.(bot_places[i]);
124 // now: i == this.bot_places_count
125 s = s2 = cvar_string(placename);
126 p = strstrofs(s2, " ", 0);
129 s = substring(s2, 0, p);
130 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
131 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
132 //print("places: ", placename, " := ", cvar_string(placename), "\n");
134 e = find(NULL, targetname, s);
136 LOG_INFO("invalid place ", s);
137 if(i < MAX_BOT_PLACES)
139 this.(bot_placenames[i]) = strzone(placename);
140 this.(bot_places[i]) = e;
141 this.bot_places_count += 1;
147 e = find(NULL, targetname, placename);
149 LOG_INFO("invalid place ", placename);
155 // Initialize global commands list
156 // NOTE: New commands should be initialized here
157 void bot_commands_init()
159 bot_cmd_string[BOT_CMD_NULL] = "";
160 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
162 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
163 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
165 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
166 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
168 bot_cmd_string[BOT_CMD_WAIT] = "wait";
169 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
171 bot_cmd_string[BOT_CMD_TURN] = "turn";
172 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
174 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
175 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
177 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
178 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
180 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
181 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
183 bot_cmd_string[BOT_CMD_CC] = "cc";
184 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
186 bot_cmd_string[BOT_CMD_IF] = "if";
187 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
189 bot_cmd_string[BOT_CMD_ELSE] = "else";
190 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
192 bot_cmd_string[BOT_CMD_FI] = "fi";
193 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
195 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
196 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
198 bot_cmd_string[BOT_CMD_AIM] = "aim";
199 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
201 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
202 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
204 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
205 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
207 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
208 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
210 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
211 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
213 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
214 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
216 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
217 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
219 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
220 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
222 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
223 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
225 bot_cmd_string[BOT_CMD_SOUND] = "sound";
226 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
228 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
229 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
231 bot_cmds_initialized = true;
234 // Returns first bot with matching name
235 entity find_bot_by_name(string name)
237 FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
245 // Returns a bot by number on list
246 entity find_bot_by_number(float number)
254 bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
257 if(IS_BOT_CLIENT(bot))
268 float bot_decodecommand(string cmdstring)
274 sp = strstrofs(cmdstring, " ", 0);
281 parm = substring(cmdstring, sp + 1, -1);
282 cmdstring = substring(cmdstring, 0, sp);
285 if(!bot_cmds_initialized)
289 for(i=1;i<BOT_CMD_COUNTER;++i)
291 if(bot_cmd_string[i]!=cmdstring)
294 cmd_parm_type = bot_cmd_parm_type[i];
296 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
298 LOG_INFO("ERROR: A parameter is required for this command");
302 // Load command into queue
303 bot_cmd.bot_cmd_type = i;
306 switch(cmd_parm_type)
308 case BOT_CMD_PARAMETER_FLOAT:
309 bot_cmd.bot_cmd_parm_float = stof(parm);
311 case BOT_CMD_PARAMETER_STRING:
312 strcpy(bot_cmd.bot_cmd_parm_string, parm);
314 case BOT_CMD_PARAMETER_VECTOR:
315 if(substring(parm, 0, 1) != "\'")
317 LOG_INFOF("ERROR: expected vector type \'x y z\', got %s", parm);
320 bot_cmd.bot_cmd_parm_vector = stov(parm);
327 LOG_INFO("ERROR: No such command '", cmdstring, "'");
331 void bot_cmdhelp(string scmd)
336 if(!bot_cmds_initialized)
339 for(i=1;i<BOT_CMD_COUNTER;++i)
341 if(bot_cmd_string[i]!=scmd)
344 ntype = bot_cmd_parm_type[i];
348 case BOT_CMD_PARAMETER_FLOAT:
351 case BOT_CMD_PARAMETER_STRING:
354 case BOT_CMD_PARAMETER_VECTOR:
366 desc = "Stops the bot completely. Any command other than 'continue' will be ignored.";
368 case BOT_CMD_CONTINUE:
369 desc = "Disable paused status";
372 desc = "Pause command parsing and bot ai for N seconds. Pressed key will remain pressed";
374 case BOT_CMD_WAIT_UNTIL:
375 desc = "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 desc = "Waits till all bots that have a command queue reach this command. Pressed key will remain pressed";
381 desc = "Look to the right or left N degrees. For turning to the left use positive numbers.";
384 desc = "Walk to an specific coordinate on the map. Usage: moveto \'x y z\'";
386 case BOT_CMD_MOVETOTARGET:
387 desc = "Walk to the specific target on the map";
389 case BOT_CMD_RESETGOAL:
390 desc = "Resets the goal stack";
393 desc = "Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;";
396 desc = "Perform simple conditional execution.\n"
398 " sv_cmd .. if \"condition\"\n"
399 " sv_cmd .. <instruction if true>\n"
400 " sv_cmd .. <instruction if true>\n"
402 " sv_cmd .. <instruction if false>\n"
403 " sv_cmd .. <instruction if false>\n"
405 "Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n"
406 " Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n"
407 "Fields: health, speed, flagcarrier\n"
408 "Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;";
410 case BOT_CMD_RESETAIM:
411 desc = "Points the aim to the coordinates x,y 0,0";
414 desc = "Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n"
415 "There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n"
416 "Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n"
417 " aim \"0 90 2\" // Will gradually look to the sky in the next two seconds";
419 case BOT_CMD_AIMTARGET:
420 desc = "Points the aim to given target";
422 case BOT_CMD_PRESSKEY:
423 desc = "Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use"
424 "Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called"
425 "Note: The script will not return the control to the bot ai until all keys are released";
427 case BOT_CMD_RELEASEKEY:
428 desc = "Release previoulsy used keys. Use the parameter \"all\" to release all keys";
431 desc = "play sound file at bot location";
433 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
434 desc = "verify the state of the weapon entity";
437 desc = "This command has no description yet.";
440 LOG_HELP("Command: ", bot_cmd_string[i], "\nParameter: <", stype, ">", "\nDescription: ", desc);
444 void bot_list_commands()
449 if(!bot_cmds_initialized)
452 LOG_HELP("Bot commands:");
454 for(i=1;i<BOT_CMD_COUNTER;++i)
456 switch(bot_cmd_parm_type[i])
458 case BOT_CMD_PARAMETER_FLOAT:
461 case BOT_CMD_PARAMETER_STRING:
464 case BOT_CMD_PARAMETER_VECTOR:
472 LOG_HELP(" ^2", bot_cmd_string[i]," ^7<", ptype, ">");
474 LOG_HELP(" ^2", bot_cmd_string[i]);
476 LOG_HELP("For help about a specific command, type bot_cmd help <command>");
480 .int bot_exec_status;
482 float bot_cmd_cc(entity this)
484 SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
485 return CMD_STATUS_FINISHED;
488 float bot_cmd_impulse(entity this)
490 CS(this).impulse = bot_cmd.bot_cmd_parm_float;
491 return CMD_STATUS_FINISHED;
494 float bot_cmd_continue(entity this)
496 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
497 bot_relinkplayerlist();
498 return CMD_STATUS_FINISHED;
501 .float bot_cmd_wait_time;
502 float bot_cmd_wait(entity this)
504 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
506 if(time>=this.bot_cmd_wait_time)
508 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
509 return CMD_STATUS_FINISHED;
512 return CMD_STATUS_EXECUTING;
515 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
516 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
517 return CMD_STATUS_EXECUTING;
520 float bot_cmd_wait_until(entity this)
522 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
524 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
525 return CMD_STATUS_EXECUTING;
527 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
528 return CMD_STATUS_FINISHED;
531 float bot_cmd_barrier(entity this)
533 // 0 = no barrier, 1 = waiting, 2 = waiting finished
535 if(this.bot_barrier == 0) // initialization
537 this.bot_barrier = 1;
539 //this.colormod = '4 4 0';
542 if(this.bot_barrier == 1) // find other bots
544 FOREACH_CLIENT(it.isbot, {
545 if(it.bot_cmdqueuebuf_allocated)
546 if(it.bot_barrier != 1)
547 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
550 // all bots hit the barrier!
552 // acknowledge barrier
553 FOREACH_CLIENT(it.isbot, { it.bot_barrier = 2; });
555 bot_barriertime = time;
558 // if we get here, the barrier is finished
560 this.bot_barrier = 0;
561 //this.colormod = '0 0 0';
563 return CMD_STATUS_FINISHED;
566 float bot_cmd_turn(entity this)
568 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
569 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
570 return CMD_STATUS_FINISHED;
573 float bot_cmd_select_weapon(entity this)
575 float id = bot_cmd.bot_cmd_parm_float;
577 if(id < WEP_FIRST || id > WEP_LAST)
578 return CMD_STATUS_ERROR;
580 bool success = false;
582 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
584 .entity weaponentity = weaponentities[slot];
585 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
588 if(client_hasweapon(this, REGISTRY_GET(Weapons, id), weaponentity, true, false))
591 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, id);
596 return CMD_STATUS_ERROR;
598 return CMD_STATUS_FINISHED;
601 .int bot_cmd_condition_status;
603 const int CMD_CONDITION_NONE = 0;
604 const int CMD_CONDITION_true = 1;
605 const int CMD_CONDITION_false = 2;
606 const int CMD_CONDITION_true_BLOCK = 4;
607 const int CMD_CONDITION_false_BLOCK = 8;
609 float bot_cmd_eval(entity this, string expr)
611 // Search for numbers
612 if(IS_DIGIT(substring(expr, 0, 1)))
616 if(substring(expr, 0, 5)=="cvar.")
617 return cvar(substring(expr, 5, strlen(expr)));
620 // TODO: expand with support for more fields (key carrier, ball carrier, armor etc)
624 return GetResource(this, RES_HEALTH);
626 return vlen(this.velocity);
628 return ((this.flagcarried!=NULL));
631 LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
635 float bot_cmd_if(entity this)
637 string expr, val_a, val_b;
640 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
642 // Only one "if" block is allowed at time
643 LOG_INFO("ERROR: Only one conditional block can be processed at time");
644 bot_clearqueue(this);
645 return CMD_STATUS_ERROR;
648 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
650 // search for operators
651 expr = bot_cmd.bot_cmd_parm_string;
653 cmpofs = strstrofs(expr,"=",0);
657 val_a = substring(expr,0,cmpofs);
658 val_b = substring(expr,cmpofs+1,strlen(expr));
660 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
661 this.bot_cmd_condition_status |= CMD_CONDITION_true;
663 this.bot_cmd_condition_status |= CMD_CONDITION_false;
665 return CMD_STATUS_FINISHED;
668 cmpofs = strstrofs(expr,">",0);
672 val_a = substring(expr,0,cmpofs);
673 val_b = substring(expr,cmpofs+1,strlen(expr));
675 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
676 this.bot_cmd_condition_status |= CMD_CONDITION_true;
678 this.bot_cmd_condition_status |= CMD_CONDITION_false;
680 return CMD_STATUS_FINISHED;
683 cmpofs = strstrofs(expr,"<",0);
687 val_a = substring(expr,0,cmpofs);
688 val_b = substring(expr,cmpofs+1,strlen(expr));
690 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
691 this.bot_cmd_condition_status |= CMD_CONDITION_true;
693 this.bot_cmd_condition_status |= CMD_CONDITION_false;
695 return CMD_STATUS_FINISHED;
698 if(bot_cmd_eval(this, expr))
699 this.bot_cmd_condition_status |= CMD_CONDITION_true;
701 this.bot_cmd_condition_status |= CMD_CONDITION_false;
703 return CMD_STATUS_FINISHED;
706 float bot_cmd_else(entity this)
708 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
709 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
710 return CMD_STATUS_FINISHED;
713 float bot_cmd_fi(entity this)
715 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
716 return CMD_STATUS_FINISHED;
719 float bot_cmd_resetaim(entity this)
721 this.v_angle = '0 0 0';
722 return CMD_STATUS_FINISHED;
725 .float bot_cmd_aim_begintime;
726 .float bot_cmd_aim_endtime;
727 .vector bot_cmd_aim_begin;
728 .vector bot_cmd_aim_end;
730 float bot_cmd_aim(entity this)
733 if(this.bot_cmd_aim_endtime)
737 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
738 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
740 if(time>=this.bot_cmd_aim_endtime)
742 this.bot_cmd_aim_endtime = 0;
743 return CMD_STATUS_FINISHED;
746 return CMD_STATUS_EXECUTING;
749 // New aiming direction
753 parms = bot_cmd.bot_cmd_parm_string;
755 tokens = tokenizebyseparator(parms, " ");
757 if(tokens<2||tokens>3)
758 return CMD_STATUS_ERROR;
760 step = (tokens == 3) ? stof(argv(2)) : 0;
764 this.v_angle_x -= stof(argv(1));
765 this.v_angle_y += stof(argv(0));
766 return CMD_STATUS_FINISHED;
769 this.bot_cmd_aim_begin = this.v_angle;
771 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
772 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
773 this.bot_cmd_aim_end_z = 0;
775 this.bot_cmd_aim_begintime = time;
776 this.bot_cmd_aim_endtime = time + step;
778 return CMD_STATUS_EXECUTING;
781 float bot_cmd_aimtarget(entity this)
783 if(this.bot_cmd_aim_endtime)
785 return bot_cmd_aim(this);
793 parms = bot_cmd.bot_cmd_parm_string;
795 tokens = tokenizebyseparator(parms, " ");
797 e = bot_getplace(this, argv(0));
799 return CMD_STATUS_ERROR;
801 v = e.origin + (e.mins + e.maxs) * 0.5;
805 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
806 this.v_angle_x = -this.v_angle.x;
807 return CMD_STATUS_FINISHED;
810 if(tokens<1||tokens>2)
811 return CMD_STATUS_ERROR;
813 step = stof(argv(1));
815 this.bot_cmd_aim_begin = this.v_angle;
816 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
817 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
819 this.bot_cmd_aim_begintime = time;
820 this.bot_cmd_aim_endtime = time + step;
822 return CMD_STATUS_EXECUTING;
827 const int BOT_CMD_KEY_NONE = 0;
828 const int BOT_CMD_KEY_FORWARD = BIT(0);
829 const int BOT_CMD_KEY_BACKWARD = BIT(1);
830 const int BOT_CMD_KEY_RIGHT = BIT(2);
831 const int BOT_CMD_KEY_LEFT = BIT(3);
832 const int BOT_CMD_KEY_JUMP = BIT(4);
833 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
834 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
835 const int BOT_CMD_KEY_USE = BIT(7);
836 const int BOT_CMD_KEY_HOOK = BIT(8);
837 const int BOT_CMD_KEY_CROUCH = BIT(9);
838 const int BOT_CMD_KEY_CHAT = BIT(10);
840 bool bot_presskeys(entity this)
842 CS(this).movement = '0 0 0';
843 PHYS_INPUT_BUTTON_JUMP(this) = false;
844 PHYS_INPUT_BUTTON_CROUCH(this) = false;
845 PHYS_INPUT_BUTTON_ATCK(this) = false;
846 PHYS_INPUT_BUTTON_ATCK2(this) = false;
847 PHYS_INPUT_BUTTON_USE(this) = false;
848 PHYS_INPUT_BUTTON_HOOK(this) = false;
849 PHYS_INPUT_BUTTON_CHAT(this) = false;
851 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
854 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
855 CS(this).movement_x = autocvar_sv_maxspeed;
856 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
857 CS(this).movement_x = -autocvar_sv_maxspeed;
859 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
860 CS(this).movement_y = autocvar_sv_maxspeed;
861 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
862 CS(this).movement_y = -autocvar_sv_maxspeed;
864 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
865 PHYS_INPUT_BUTTON_JUMP(this) = true;
867 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
868 PHYS_INPUT_BUTTON_CROUCH(this) = true;
870 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
871 PHYS_INPUT_BUTTON_ATCK(this) = true;
873 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
874 PHYS_INPUT_BUTTON_ATCK2(this) = true;
876 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
877 PHYS_INPUT_BUTTON_USE(this) = true;
879 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
880 PHYS_INPUT_BUTTON_HOOK(this) = true;
882 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
883 PHYS_INPUT_BUTTON_CHAT(this) = true;
889 float bot_cmd_keypress_handler(entity this, string key, float enabled)
895 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
897 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
901 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
902 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
905 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
910 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
911 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
914 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
919 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
920 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
923 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
928 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
929 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
932 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
936 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
938 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
942 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
944 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
948 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
950 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
954 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
956 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
960 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
962 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
966 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
968 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
972 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
974 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
980 return CMD_STATUS_FINISHED;
983 float bot_cmd_presskey(entity this)
987 key = bot_cmd.bot_cmd_parm_string;
989 bot_cmd_keypress_handler(this, key,true);
991 return CMD_STATUS_FINISHED;
994 float bot_cmd_releasekey(entity this)
998 key = bot_cmd.bot_cmd_parm_string;
1000 return bot_cmd_keypress_handler(this, key,false);
1003 bool bot_ispaused(entity this)
1005 return(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED);
1008 float bot_cmd_pause(entity this)
1010 PHYS_INPUT_BUTTON_DRAG(this) = false;
1011 PHYS_INPUT_BUTTON_USE(this) = false;
1012 PHYS_INPUT_BUTTON_ATCK(this) = false;
1013 PHYS_INPUT_BUTTON_JUMP(this) = false;
1014 PHYS_INPUT_BUTTON_HOOK(this) = false;
1015 PHYS_INPUT_BUTTON_CHAT(this) = false;
1016 PHYS_INPUT_BUTTON_ATCK2(this) = false;
1017 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1019 CS(this).movement = '0 0 0';
1020 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1022 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1023 bot_relinkplayerlist();
1024 return CMD_STATUS_FINISHED;
1027 float bot_cmd_moveto(entity this)
1029 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1032 float bot_cmd_movetotarget(entity this)
1035 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1037 return CMD_STATUS_ERROR;
1038 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1041 float bot_cmd_resetgoal(entity this)
1043 return this.cmd_resetgoal(this);
1047 float bot_cmd_sound(entity this)
1050 f = bot_cmd.bot_cmd_parm_string;
1052 float n = tokenizebyseparator(f, " ");
1055 float chan = CH_WEAPON_B;
1056 float vol = VOL_BASE;
1057 float atten = ATTEN_MIN;
1060 sample = argv(n - 1);
1062 chan = stof(argv(0));
1064 vol = stof(argv(1));
1066 atten = stof(argv(2));
1069 _sound(this, chan, sample, vol, atten);
1071 return CMD_STATUS_FINISHED;
1075 float bot_cmd_debug_assert_canfire(entity this)
1077 float f = bot_cmd.bot_cmd_parm_float;
1079 int slot = 0; // TODO: unhardcode?
1080 .entity weaponentity = weaponentities[slot];
1081 if(this.(weaponentity).state != WS_READY)
1085 this.colormod = '0 8 8';
1086 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1089 else if(ATTACK_FINISHED(this, weaponentity) > time)
1093 this.colormod = '8 0 8';
1094 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left)");
1097 else if(this.(weaponentity).tuba_note)
1101 this.colormod = '8 0 0';
1102 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1109 this.colormod = '8 8 0';
1110 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left");
1114 return CMD_STATUS_FINISHED;
1119 void bot_command_executed(entity this, bool rm)
1126 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1128 this.bot_cmd_execution_index++;
1131 void bot_setcurrentcommand(entity this)
1135 if(!this.bot_cmd_current)
1137 this.bot_cmd_current = new_pure(bot_cmd);
1140 bot_cmd = this.bot_cmd_current;
1141 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1143 if(bot_havecommand(this, this.bot_cmd_execution_index))
1146 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1147 if(bot_decodecommand(cmdstring))
1149 bot_cmd.owner = this;
1150 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1154 // Invalid command, remove from queue
1156 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1157 this.bot_cmd_execution_index++;
1165 void bot_resetqueues()
1167 FOREACH_CLIENT(it.isbot, {
1168 it.bot_cmd_execution_index = 0;
1170 // also, cancel all barriers
1172 for(int i = 0; i < it.bot_places_count; ++i)
1174 strfree(it.(bot_placenames[i]));
1176 it.bot_places_count = 0;
1179 bot_barriertime = time;
1182 // Here we map commands to functions and deal with complex interactions between commands and execution states
1183 // NOTE: Of course you need to include your commands here too :)
1184 float bot_execute_commands_once(entity this)
1186 float status, ispressingkey;
1189 bot_setcurrentcommand(this);
1191 // Ignore all commands except continue when the bot is paused
1192 if(!(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1194 // if we have no bot command, better return
1195 // old logic kept pressing previously pressed keys, but that has problems
1196 // (namely, it means you cannot make a bot "normal" ever again)
1197 // to keep a bot walking for a while, use the "wait" bot command
1198 if(bot_cmd == world)
1201 else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1203 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1205 bot_command_executed(this, true);
1206 LOG_INFO("WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.");
1211 // Keep pressing keys raised by the "presskey" command
1212 ispressingkey = boolean(bot_presskeys(this));
1214 // Handle conditions
1215 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1216 if((this.bot_cmd_condition_status & CMD_CONDITION_true) && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1218 bot_command_executed(this, true);
1221 else if((this.bot_cmd_condition_status & CMD_CONDITION_false) && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1223 bot_command_executed(this, true);
1227 // Map commands to functions
1228 switch(bot_cmd.bot_cmd_type)
1231 return ispressingkey;
1234 status = bot_cmd_pause(this);
1236 case BOT_CMD_CONTINUE:
1237 status = bot_cmd_continue(this);
1240 status = bot_cmd_wait(this);
1242 case BOT_CMD_WAIT_UNTIL:
1243 status = bot_cmd_wait_until(this);
1246 status = bot_cmd_turn(this);
1248 case BOT_CMD_MOVETO:
1249 status = bot_cmd_moveto(this);
1251 case BOT_CMD_MOVETOTARGET:
1252 status = bot_cmd_movetotarget(this);
1254 case BOT_CMD_RESETGOAL:
1255 status = bot_cmd_resetgoal(this);
1258 status = bot_cmd_cc(this);
1261 status = bot_cmd_if(this);
1264 status = bot_cmd_else(this);
1267 status = bot_cmd_fi(this);
1269 case BOT_CMD_RESETAIM:
1270 status = bot_cmd_resetaim(this);
1273 status = bot_cmd_aim(this);
1275 case BOT_CMD_AIMTARGET:
1276 status = bot_cmd_aimtarget(this);
1278 case BOT_CMD_PRESSKEY:
1279 status = bot_cmd_presskey(this);
1281 case BOT_CMD_RELEASEKEY:
1282 status = bot_cmd_releasekey(this);
1284 case BOT_CMD_SELECTWEAPON:
1285 status = bot_cmd_select_weapon(this);
1287 case BOT_CMD_IMPULSE:
1288 status = bot_cmd_impulse(this);
1290 case BOT_CMD_BARRIER:
1291 status = bot_cmd_barrier(this);
1293 case BOT_CMD_CONSOLE:
1294 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1295 status = CMD_STATUS_FINISHED;
1298 status = bot_cmd_sound(this);
1300 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1301 status = bot_cmd_debug_assert_canfire(this);
1304 LOG_INFOF("ERROR: Invalid command on queue with id '%s'", ftos(bot_cmd.bot_cmd_type));
1308 if (status==CMD_STATUS_ERROR)
1309 LOG_INFOF("ERROR: The command '%s' returned an error status", bot_cmd_string[bot_cmd.bot_cmd_type]);
1311 // Move execution pointer
1312 if(status==CMD_STATUS_EXECUTING)
1318 if(autocvar_g_debug_bot_commands)
1322 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1324 case BOT_CMD_PARAMETER_FLOAT:
1325 parms = ftos(bot_cmd.bot_cmd_parm_float);
1327 case BOT_CMD_PARAMETER_STRING:
1328 parms = bot_cmd.bot_cmd_parm_string;
1330 case BOT_CMD_PARAMETER_VECTOR:
1331 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1337 clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1340 bot_command_executed(this, true);
1343 if(status == CMD_STATUS_FINISHED)
1346 return CMD_STATUS_ERROR;
1349 // This function should be (the only) called directly from the bot ai loop
1350 int bot_execute_commands(entity this)
1355 f = bot_execute_commands_once(this);