1 #include "scripting.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
7 #include <common/state.qh>
8 #include <common/physics/player.qh>
9 #include <common/wepent.qh>
15 .float bot_cmdqueuebuf_allocated;
16 .float bot_cmdqueuebuf;
17 .float bot_cmdqueuebuf_start;
18 .float bot_cmdqueuebuf_end;
20 void bot_clearqueue(entity bot)
22 if(!bot.bot_cmdqueuebuf_allocated)
24 buf_del(bot.bot_cmdqueuebuf);
25 bot.bot_cmdqueuebuf_allocated = false;
26 LOG_TRACE("bot ", bot.netname, " queue cleared");
29 void bot_queuecommand(entity bot, string cmdstring)
31 if(!bot.bot_cmdqueuebuf_allocated)
33 bot.bot_cmdqueuebuf = buf_create();
34 bot.bot_cmdqueuebuf_allocated = true;
35 bot.bot_cmdqueuebuf_start = 0;
36 bot.bot_cmdqueuebuf_end = 0;
39 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
41 // if the command was a "sound" command, precache the sound NOW
42 // this prevents lagging!
48 sp = strstrofs(cmdstring, " ", 0);
51 parm = substring(cmdstring, sp + 1, -1);
52 cmdstr = substring(cmdstring, 0, sp);
58 sp = strstrofs(parm, " ", 0);
61 parm = substring(parm, sp + 1, -1);
68 bot.bot_cmdqueuebuf_end += 1;
71 void bot_dequeuecommand(entity bot, float idx)
73 if(!bot.bot_cmdqueuebuf_allocated)
74 error("dequeuecommand but no queue allocated");
75 if(idx < bot.bot_cmdqueuebuf_start)
76 error("dequeueing a command in the past");
77 if(idx >= bot.bot_cmdqueuebuf_end)
78 error("dequeueing a command in the future");
79 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
80 if(idx == bot.bot_cmdqueuebuf_start)
81 bot.bot_cmdqueuebuf_start += 1;
82 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
86 string bot_readcommand(entity bot, float idx)
88 if(!bot.bot_cmdqueuebuf_allocated)
89 error("readcommand but no queue allocated");
90 if(idx < bot.bot_cmdqueuebuf_start)
91 error("reading a command in the past");
92 if(idx >= bot.bot_cmdqueuebuf_end)
93 error("reading a command in the future");
94 return bufstr_get(bot.bot_cmdqueuebuf, idx);
97 bool bot_havecommand(entity this, int idx)
99 if(!this.bot_cmdqueuebuf_allocated)
101 if(idx < this.bot_cmdqueuebuf_start)
103 if(idx >= this.bot_cmdqueuebuf_end)
108 const int MAX_BOT_PLACES = 4;
109 .float bot_places_count;
110 .entity bot_places[MAX_BOT_PLACES];
111 .string bot_placenames[MAX_BOT_PLACES];
112 entity bot_getplace(entity this, string placename)
115 if(substring(placename, 0, 1) == "@")
118 placename = substring(placename, 1, -1);
120 for(i = 0; i < this.bot_places_count; ++i)
121 if(this.(bot_placenames[i]) == placename)
122 return this.(bot_places[i]);
123 // now: i == this.bot_places_count
124 s = s2 = cvar_string(placename);
125 p = strstrofs(s2, " ", 0);
128 s = substring(s2, 0, p);
129 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
130 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
131 //print("places: ", placename, " := ", cvar_string(placename), "\n");
133 e = find(NULL, targetname, s);
135 LOG_INFO("invalid place ", s);
136 if(i < MAX_BOT_PLACES)
138 this.(bot_placenames[i]) = strzone(placename);
139 this.(bot_places[i]) = e;
140 this.bot_places_count += 1;
146 e = find(NULL, targetname, placename);
148 LOG_INFO("invalid place ", placename);
154 // Initialize global commands list
155 // NOTE: New commands should be initialized here
156 void bot_commands_init()
158 bot_cmd_string[BOT_CMD_NULL] = "";
159 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
161 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
162 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
164 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
165 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
167 bot_cmd_string[BOT_CMD_WAIT] = "wait";
168 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
170 bot_cmd_string[BOT_CMD_TURN] = "turn";
171 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
173 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
174 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
176 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
177 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
179 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
180 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
182 bot_cmd_string[BOT_CMD_CC] = "cc";
183 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
185 bot_cmd_string[BOT_CMD_IF] = "if";
186 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
188 bot_cmd_string[BOT_CMD_ELSE] = "else";
189 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
191 bot_cmd_string[BOT_CMD_FI] = "fi";
192 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
194 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
195 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
197 bot_cmd_string[BOT_CMD_AIM] = "aim";
198 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
200 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
201 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
203 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
204 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
206 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
207 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
209 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
210 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
212 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
213 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
215 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
216 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
218 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
219 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
221 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
222 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
224 bot_cmd_string[BOT_CMD_SOUND] = "sound";
225 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
227 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
228 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
230 bot_cmds_initialized = true;
233 // Returns first bot with matching name
234 entity find_bot_by_name(string name)
236 FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
244 // Returns a bot by number on list
245 entity find_bot_by_number(float number)
253 bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
256 if(IS_BOT_CLIENT(bot))
267 float bot_decodecommand(string cmdstring)
273 sp = strstrofs(cmdstring, " ", 0);
280 parm = substring(cmdstring, sp + 1, -1);
281 cmdstring = substring(cmdstring, 0, sp);
284 if(!bot_cmds_initialized)
288 for(i=1;i<BOT_CMD_COUNTER;++i)
290 if(bot_cmd_string[i]!=cmdstring)
293 cmd_parm_type = bot_cmd_parm_type[i];
295 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
297 LOG_INFO("ERROR: A parameter is required for this command");
301 // Load command into queue
302 bot_cmd.bot_cmd_type = i;
305 switch(cmd_parm_type)
307 case BOT_CMD_PARAMETER_FLOAT:
308 bot_cmd.bot_cmd_parm_float = stof(parm);
310 case BOT_CMD_PARAMETER_STRING:
311 if(bot_cmd.bot_cmd_parm_string)
312 strunzone(bot_cmd.bot_cmd_parm_string);
313 bot_cmd.bot_cmd_parm_string = strzone(parm);
315 case BOT_CMD_PARAMETER_VECTOR:
316 if(substring(parm, 0, 1) != "\'")
318 LOG_INFOF("ERROR: expected vector type \'x y z\', got %s", parm);
321 bot_cmd.bot_cmd_parm_vector = stov(parm);
328 LOG_INFO("ERROR: No such command '", cmdstring, "'");
332 void bot_cmdhelp(string scmd)
337 if(!bot_cmds_initialized)
340 for(i=1;i<BOT_CMD_COUNTER;++i)
342 if(bot_cmd_string[i]!=scmd)
345 ntype = bot_cmd_parm_type[i];
349 case BOT_CMD_PARAMETER_FLOAT:
350 stype = "float number";
352 case BOT_CMD_PARAMETER_STRING:
355 case BOT_CMD_PARAMETER_VECTOR:
363 string prelude = strcat(
364 "Command: ", bot_cmd_string[i], "\n",
365 "Parameter: <", stype, ">", "\n",
371 LOG_INFO(prelude, "Stops the bot completely. Any command other than 'continue' will be ignored.");
373 case BOT_CMD_CONTINUE:
374 LOG_INFO(prelude, "Disable paused status");
377 LOG_INFO(prelude, "Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
379 case BOT_CMD_WAIT_UNTIL:
380 LOG_INFO(prelude, "Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
382 case BOT_CMD_BARRIER:
383 LOG_INFO(prelude, "Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
386 LOG_INFO(prelude, "Look to the right or left N degrees. For turning to the left use positive numbers.");
389 LOG_INFO(prelude, "Walk to an specific coordinate on the map. Usage: moveto \'x y z\'");
391 case BOT_CMD_MOVETOTARGET:
392 LOG_INFO(prelude, "Walk to the specific target on the map");
394 case BOT_CMD_RESETGOAL:
395 LOG_INFO(prelude, "Resets the goal stack");
398 LOG_INFO(prelude, "Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
401 LOG_INFO(prelude, "Perform simple conditional execution.\n"
403 " sv_cmd .. if \"condition\"\n"
404 " sv_cmd .. <instruction if true>\n"
405 " sv_cmd .. <instruction if true>\n"
407 " sv_cmd .. <instruction if false>\n"
408 " sv_cmd .. <instruction if false>\n"
410 "Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n"
411 " Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n"
412 "Fields: health, speed, flagcarrier\n"
413 "Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;"
416 case BOT_CMD_RESETAIM:
417 LOG_INFO(prelude, "Points the aim to the coordinates x,y 0,0");
420 LOG_INFO(prelude, "Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n"
421 "There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n"
422 "Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n"
423 " aim \"0 90 2\" // Will gradually look to the sky in the next two seconds"
426 case BOT_CMD_AIMTARGET:
427 LOG_INFO(prelude, "Points the aim to given target");
429 case BOT_CMD_PRESSKEY:
430 LOG_INFO(prelude, "Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use");
431 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
432 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
434 case BOT_CMD_RELEASEKEY:
435 LOG_INFO(prelude, "Release previoulsy used keys. Use the parameter \"all\" to release all keys");
438 LOG_INFO(prelude, "play sound file at bot location");
440 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
441 LOG_INFO(prelude, "verify the state of the weapon entity");
444 LOG_INFO(prelude, "This command has no description yet.");
450 void bot_list_commands()
455 if(!bot_cmds_initialized)
459 "List of all available commands:\n"
460 " Command - Parameter Type\n"
463 for(i=1;i<BOT_CMD_COUNTER;++i)
465 switch(bot_cmd_parm_type[i])
467 case BOT_CMD_PARAMETER_FLOAT:
468 ptype = "float number";
470 case BOT_CMD_PARAMETER_STRING:
473 case BOT_CMD_PARAMETER_VECTOR:
480 LOG_INFO(" ", bot_cmd_string[i]," - <", ptype, ">");
485 .int bot_exec_status;
487 float bot_cmd_cc(entity this)
489 SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
490 return CMD_STATUS_FINISHED;
493 float bot_cmd_impulse(entity this)
495 CS(this).impulse = bot_cmd.bot_cmd_parm_float;
496 return CMD_STATUS_FINISHED;
499 float bot_cmd_continue(entity this)
501 bot_relinkplayerlist();
502 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
503 return CMD_STATUS_FINISHED;
506 .float bot_cmd_wait_time;
507 float bot_cmd_wait(entity this)
509 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
511 if(time>=this.bot_cmd_wait_time)
513 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
514 return CMD_STATUS_FINISHED;
517 return CMD_STATUS_EXECUTING;
520 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
521 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
522 return CMD_STATUS_EXECUTING;
525 float bot_cmd_wait_until(entity this)
527 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
529 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
530 return CMD_STATUS_EXECUTING;
532 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
533 return CMD_STATUS_FINISHED;
536 float bot_cmd_barrier(entity this)
538 // 0 = no barrier, 1 = waiting, 2 = waiting finished
540 if(this.bot_barrier == 0) // initialization
542 this.bot_barrier = 1;
544 //this.colormod = '4 4 0';
547 if(this.bot_barrier == 1) // find other bots
549 FOREACH_CLIENT(it.isbot, {
550 if(it.bot_cmdqueuebuf_allocated)
551 if(it.bot_barrier != 1)
552 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
555 // all bots hit the barrier!
557 // acknowledge barrier
558 FOREACH_CLIENT(it.isbot, { it.bot_barrier = 2; });
560 bot_barriertime = time;
563 // if we get here, the barrier is finished
565 this.bot_barrier = 0;
566 //this.colormod = '0 0 0';
568 return CMD_STATUS_FINISHED;
571 float bot_cmd_turn(entity this)
573 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
574 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
575 return CMD_STATUS_FINISHED;
578 float bot_cmd_select_weapon(entity this)
580 float id = bot_cmd.bot_cmd_parm_float;
582 if(id < WEP_FIRST || id > WEP_LAST)
583 return CMD_STATUS_ERROR;
585 bool success = false;
587 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
589 .entity weaponentity = weaponentities[slot];
590 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
593 if(client_hasweapon(this, Weapons_from(id), weaponentity, true, false))
596 this.(weaponentity).m_switchweapon = Weapons_from(id);
601 return CMD_STATUS_ERROR;
603 return CMD_STATUS_FINISHED;
606 .int bot_cmd_condition_status;
608 const int CMD_CONDITION_NONE = 0;
609 const int CMD_CONDITION_true = 1;
610 const int CMD_CONDITION_false = 2;
611 const int CMD_CONDITION_true_BLOCK = 4;
612 const int CMD_CONDITION_false_BLOCK = 8;
614 float bot_cmd_eval(entity this, string expr)
616 // Search for numbers
617 if(IS_DIGIT(substring(expr, 0, 1)))
621 if(substring(expr, 0, 5)=="cvar.")
622 return cvar(substring(expr, 5, strlen(expr)));
630 return vlen(this.velocity);
632 return ((this.flagcarried!=NULL));
635 LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
639 float bot_cmd_if(entity this)
641 string expr, val_a, val_b;
644 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
646 // Only one "if" block is allowed at time
647 LOG_INFO("ERROR: Only one conditional block can be processed at time");
648 bot_clearqueue(this);
649 return CMD_STATUS_ERROR;
652 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
654 // search for operators
655 expr = bot_cmd.bot_cmd_parm_string;
657 cmpofs = strstrofs(expr,"=",0);
661 val_a = substring(expr,0,cmpofs);
662 val_b = substring(expr,cmpofs+1,strlen(expr));
664 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
665 this.bot_cmd_condition_status |= CMD_CONDITION_true;
667 this.bot_cmd_condition_status |= CMD_CONDITION_false;
669 return CMD_STATUS_FINISHED;
672 cmpofs = strstrofs(expr,">",0);
676 val_a = substring(expr,0,cmpofs);
677 val_b = substring(expr,cmpofs+1,strlen(expr));
679 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
680 this.bot_cmd_condition_status |= CMD_CONDITION_true;
682 this.bot_cmd_condition_status |= CMD_CONDITION_false;
684 return CMD_STATUS_FINISHED;
687 cmpofs = strstrofs(expr,"<",0);
691 val_a = substring(expr,0,cmpofs);
692 val_b = substring(expr,cmpofs+1,strlen(expr));
694 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
695 this.bot_cmd_condition_status |= CMD_CONDITION_true;
697 this.bot_cmd_condition_status |= CMD_CONDITION_false;
699 return CMD_STATUS_FINISHED;
702 if(bot_cmd_eval(this, expr))
703 this.bot_cmd_condition_status |= CMD_CONDITION_true;
705 this.bot_cmd_condition_status |= CMD_CONDITION_false;
707 return CMD_STATUS_FINISHED;
710 float bot_cmd_else(entity this)
712 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
713 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
714 return CMD_STATUS_FINISHED;
717 float bot_cmd_fi(entity this)
719 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
720 return CMD_STATUS_FINISHED;
723 float bot_cmd_resetaim(entity this)
725 this.v_angle = '0 0 0';
726 return CMD_STATUS_FINISHED;
729 .float bot_cmd_aim_begintime;
730 .float bot_cmd_aim_endtime;
731 .vector bot_cmd_aim_begin;
732 .vector bot_cmd_aim_end;
734 float bot_cmd_aim(entity this)
737 if(this.bot_cmd_aim_endtime)
741 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
742 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
744 if(time>=this.bot_cmd_aim_endtime)
746 this.bot_cmd_aim_endtime = 0;
747 return CMD_STATUS_FINISHED;
750 return CMD_STATUS_EXECUTING;
753 // New aiming direction
757 parms = bot_cmd.bot_cmd_parm_string;
759 tokens = tokenizebyseparator(parms, " ");
761 if(tokens<2||tokens>3)
762 return CMD_STATUS_ERROR;
764 step = (tokens == 3) ? stof(argv(2)) : 0;
768 this.v_angle_x -= stof(argv(1));
769 this.v_angle_y += stof(argv(0));
770 return CMD_STATUS_FINISHED;
773 this.bot_cmd_aim_begin = this.v_angle;
775 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
776 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
777 this.bot_cmd_aim_end_z = 0;
779 this.bot_cmd_aim_begintime = time;
780 this.bot_cmd_aim_endtime = time + step;
782 return CMD_STATUS_EXECUTING;
785 float bot_cmd_aimtarget(entity this)
787 if(this.bot_cmd_aim_endtime)
789 return bot_cmd_aim(this);
797 parms = bot_cmd.bot_cmd_parm_string;
799 tokens = tokenizebyseparator(parms, " ");
801 e = bot_getplace(this, argv(0));
803 return CMD_STATUS_ERROR;
805 v = e.origin + (e.mins + e.maxs) * 0.5;
809 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
810 this.v_angle_x = -this.v_angle.x;
811 return CMD_STATUS_FINISHED;
814 if(tokens<1||tokens>2)
815 return CMD_STATUS_ERROR;
817 step = stof(argv(1));
819 this.bot_cmd_aim_begin = this.v_angle;
820 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
821 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
823 this.bot_cmd_aim_begintime = time;
824 this.bot_cmd_aim_endtime = time + step;
826 return CMD_STATUS_EXECUTING;
831 const int BOT_CMD_KEY_NONE = 0;
832 const int BOT_CMD_KEY_FORWARD = BIT(0);
833 const int BOT_CMD_KEY_BACKWARD = BIT(1);
834 const int BOT_CMD_KEY_RIGHT = BIT(2);
835 const int BOT_CMD_KEY_LEFT = BIT(3);
836 const int BOT_CMD_KEY_JUMP = BIT(4);
837 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
838 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
839 const int BOT_CMD_KEY_USE = BIT(7);
840 const int BOT_CMD_KEY_HOOK = BIT(8);
841 const int BOT_CMD_KEY_CROUCH = BIT(9);
842 const int BOT_CMD_KEY_CHAT = BIT(10);
844 bool bot_presskeys(entity this)
846 CS(this).movement = '0 0 0';
847 PHYS_INPUT_BUTTON_JUMP(this) = false;
848 PHYS_INPUT_BUTTON_CROUCH(this) = false;
849 PHYS_INPUT_BUTTON_ATCK(this) = false;
850 PHYS_INPUT_BUTTON_ATCK2(this) = false;
851 PHYS_INPUT_BUTTON_USE(this) = false;
852 PHYS_INPUT_BUTTON_HOOK(this) = false;
853 PHYS_INPUT_BUTTON_CHAT(this) = false;
855 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
858 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
859 CS(this).movement_x = autocvar_sv_maxspeed;
860 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
861 CS(this).movement_x = -autocvar_sv_maxspeed;
863 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
864 CS(this).movement_y = autocvar_sv_maxspeed;
865 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
866 CS(this).movement_y = -autocvar_sv_maxspeed;
868 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
869 PHYS_INPUT_BUTTON_JUMP(this) = true;
871 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
872 PHYS_INPUT_BUTTON_CROUCH(this) = true;
874 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
875 PHYS_INPUT_BUTTON_ATCK(this) = true;
877 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
878 PHYS_INPUT_BUTTON_ATCK2(this) = true;
880 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
881 PHYS_INPUT_BUTTON_USE(this) = true;
883 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
884 PHYS_INPUT_BUTTON_HOOK(this) = true;
886 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
887 PHYS_INPUT_BUTTON_CHAT(this) = true;
893 float bot_cmd_keypress_handler(entity this, string key, float enabled)
899 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
901 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
905 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
906 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
909 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
914 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
915 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
918 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
923 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
924 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
927 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
932 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
933 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
936 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
940 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
942 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
946 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
948 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
952 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
954 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
958 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
960 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
964 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
966 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
970 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
972 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
976 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
978 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
984 return CMD_STATUS_FINISHED;
987 float bot_cmd_presskey(entity this)
991 key = bot_cmd.bot_cmd_parm_string;
993 bot_cmd_keypress_handler(this, key,true);
995 return CMD_STATUS_FINISHED;
998 float bot_cmd_releasekey(entity this)
1002 key = bot_cmd.bot_cmd_parm_string;
1004 return bot_cmd_keypress_handler(this, key,false);
1007 float bot_cmd_pause(entity this)
1009 PHYS_INPUT_BUTTON_DRAG(this) = false;
1010 PHYS_INPUT_BUTTON_USE(this) = false;
1011 PHYS_INPUT_BUTTON_ATCK(this) = false;
1012 PHYS_INPUT_BUTTON_JUMP(this) = false;
1013 PHYS_INPUT_BUTTON_HOOK(this) = false;
1014 PHYS_INPUT_BUTTON_CHAT(this) = false;
1015 PHYS_INPUT_BUTTON_ATCK2(this) = false;
1016 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1018 CS(this).movement = '0 0 0';
1019 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1022 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1023 return CMD_STATUS_FINISHED;
1026 float bot_cmd_moveto(entity this)
1028 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1031 float bot_cmd_movetotarget(entity this)
1034 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1036 return CMD_STATUS_ERROR;
1037 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1040 float bot_cmd_resetgoal(entity this)
1042 return this.cmd_resetgoal(this);
1046 float bot_cmd_sound(entity this)
1049 f = bot_cmd.bot_cmd_parm_string;
1051 float n = tokenizebyseparator(f, " ");
1054 float chan = CH_WEAPON_B;
1055 float vol = VOL_BASE;
1056 float atten = ATTEN_MIN;
1059 sample = argv(n - 1);
1061 chan = stof(argv(0));
1063 vol = stof(argv(1));
1065 atten = stof(argv(2));
1068 _sound(this, chan, sample, vol, atten);
1070 return CMD_STATUS_FINISHED;
1074 float bot_cmd_debug_assert_canfire(entity this)
1076 float f = bot_cmd.bot_cmd_parm_float;
1078 int slot = 0; // TODO: unhardcode?
1079 .entity weaponentity = weaponentities[slot];
1080 if(this.(weaponentity).state != WS_READY)
1084 this.colormod = '0 8 8';
1085 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1088 else if(ATTACK_FINISHED(this, slot) > time)
1092 this.colormod = '8 0 8';
1093 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left)");
1096 else if(this.(weaponentity).tuba_note)
1100 this.colormod = '8 0 0';
1101 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1108 this.colormod = '8 8 0';
1109 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");
1113 return CMD_STATUS_FINISHED;
1118 void bot_command_executed(entity this, bool rm)
1125 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1127 this.bot_cmd_execution_index++;
1130 void bot_setcurrentcommand(entity this)
1134 if(!this.bot_cmd_current)
1136 this.bot_cmd_current = new_pure(bot_cmd);
1139 bot_cmd = this.bot_cmd_current;
1140 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1142 if(bot_havecommand(this, this.bot_cmd_execution_index))
1145 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1146 if(bot_decodecommand(cmdstring))
1148 bot_cmd.owner = this;
1149 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1153 // Invalid command, remove from queue
1155 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1156 this.bot_cmd_execution_index++;
1164 void bot_resetqueues()
1166 FOREACH_CLIENT(it.isbot, {
1167 it.bot_cmd_execution_index = 0;
1169 // also, cancel all barriers
1171 for(int i = 0; i < it.bot_places_count; ++i)
1173 strunzone(it.(bot_placenames[i]));
1174 it.(bot_placenames[i]) = string_null;
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);