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 strcpy(bot_cmd.bot_cmd_parm_string, 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", parm);
319 bot_cmd.bot_cmd_parm_vector = stov(parm);
326 LOG_INFO("ERROR: No such command '", cmdstring, "'");
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:
365 desc = "Stops the bot completely. Any command other than 'continue' will be ignored.";
367 case BOT_CMD_CONTINUE:
368 desc = "Disable paused status";
371 desc = "Pause command parsing and bot ai for N seconds. Pressed key will remain pressed";
373 case BOT_CMD_WAIT_UNTIL:
374 desc = "Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed";
376 case BOT_CMD_BARRIER:
377 desc = "Waits till all bots that have a command queue reach this command. Pressed key will remain pressed";
380 desc = "Look to the right or left N degrees. For turning to the left use positive numbers.";
383 desc = "Walk to an specific coordinate on the map. Usage: moveto \'x y z\'";
385 case BOT_CMD_MOVETOTARGET:
386 desc = "Walk to the specific target on the map";
388 case BOT_CMD_RESETGOAL:
389 desc = "Resets the goal stack";
392 desc = "Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;";
395 desc = "Perform simple conditional execution.\n"
397 " sv_cmd .. if \"condition\"\n"
398 " sv_cmd .. <instruction if true>\n"
399 " sv_cmd .. <instruction if true>\n"
401 " sv_cmd .. <instruction if false>\n"
402 " sv_cmd .. <instruction if false>\n"
404 "Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n"
405 " Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n"
406 "Fields: health, speed, flagcarrier\n"
407 "Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;";
409 case BOT_CMD_RESETAIM:
410 desc = "Points the aim to the coordinates x,y 0,0";
413 desc = "Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n"
414 "There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n"
415 "Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n"
416 " aim \"0 90 2\" // Will gradually look to the sky in the next two seconds";
418 case BOT_CMD_AIMTARGET:
419 desc = "Points the aim to given target";
421 case BOT_CMD_PRESSKEY:
422 desc = "Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use"
423 "Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called"
424 "Note: The script will not return the control to the bot ai until all keys are released";
426 case BOT_CMD_RELEASEKEY:
427 desc = "Release previoulsy used keys. Use the parameter \"all\" to release all keys";
430 desc = "play sound file at bot location";
432 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
433 desc = "verify the state of the weapon entity";
436 desc = "This command has no description yet.";
439 LOG_HELP("Command: ", bot_cmd_string[i], "\nParameter: <", stype, ">", "\nDescription: ", desc);
443 void bot_list_commands()
448 if(!bot_cmds_initialized)
452 "List of all available commands:\n"
453 " 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(" ", bot_cmd_string[i]," - <", ptype, ">");
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 CS(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, {
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, { 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, REGISTRY_GET(Weapons, id), weaponentity, true, false))
589 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, 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)));
618 // TODO: expand with support for more fields (key carrier, ball carrier, armor etc)
622 return GetResource(this, RES_HEALTH);
624 return vlen(this.velocity);
626 return ((this.flagcarried!=NULL));
629 LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
633 float bot_cmd_if(entity this)
635 string expr, val_a, val_b;
638 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
640 // Only one "if" block is allowed at time
641 LOG_INFO("ERROR: Only one conditional block can be processed at time");
642 bot_clearqueue(this);
643 return CMD_STATUS_ERROR;
646 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
648 // search for operators
649 expr = bot_cmd.bot_cmd_parm_string;
651 cmpofs = strstrofs(expr,"=",0);
655 val_a = substring(expr,0,cmpofs);
656 val_b = substring(expr,cmpofs+1,strlen(expr));
658 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
659 this.bot_cmd_condition_status |= CMD_CONDITION_true;
661 this.bot_cmd_condition_status |= CMD_CONDITION_false;
663 return CMD_STATUS_FINISHED;
666 cmpofs = strstrofs(expr,">",0);
670 val_a = substring(expr,0,cmpofs);
671 val_b = substring(expr,cmpofs+1,strlen(expr));
673 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
674 this.bot_cmd_condition_status |= CMD_CONDITION_true;
676 this.bot_cmd_condition_status |= CMD_CONDITION_false;
678 return CMD_STATUS_FINISHED;
681 cmpofs = strstrofs(expr,"<",0);
685 val_a = substring(expr,0,cmpofs);
686 val_b = substring(expr,cmpofs+1,strlen(expr));
688 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
689 this.bot_cmd_condition_status |= CMD_CONDITION_true;
691 this.bot_cmd_condition_status |= CMD_CONDITION_false;
693 return CMD_STATUS_FINISHED;
696 if(bot_cmd_eval(this, expr))
697 this.bot_cmd_condition_status |= CMD_CONDITION_true;
699 this.bot_cmd_condition_status |= CMD_CONDITION_false;
701 return CMD_STATUS_FINISHED;
704 float bot_cmd_else(entity this)
706 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
707 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
708 return CMD_STATUS_FINISHED;
711 float bot_cmd_fi(entity this)
713 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
714 return CMD_STATUS_FINISHED;
717 float bot_cmd_resetaim(entity this)
719 this.v_angle = '0 0 0';
720 return CMD_STATUS_FINISHED;
723 .float bot_cmd_aim_begintime;
724 .float bot_cmd_aim_endtime;
725 .vector bot_cmd_aim_begin;
726 .vector bot_cmd_aim_end;
728 float bot_cmd_aim(entity this)
731 if(this.bot_cmd_aim_endtime)
735 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
736 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
738 if(time>=this.bot_cmd_aim_endtime)
740 this.bot_cmd_aim_endtime = 0;
741 return CMD_STATUS_FINISHED;
744 return CMD_STATUS_EXECUTING;
747 // New aiming direction
751 parms = bot_cmd.bot_cmd_parm_string;
753 tokens = tokenizebyseparator(parms, " ");
755 if(tokens<2||tokens>3)
756 return CMD_STATUS_ERROR;
758 step = (tokens == 3) ? stof(argv(2)) : 0;
762 this.v_angle_x -= stof(argv(1));
763 this.v_angle_y += stof(argv(0));
764 return CMD_STATUS_FINISHED;
767 this.bot_cmd_aim_begin = this.v_angle;
769 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
770 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
771 this.bot_cmd_aim_end_z = 0;
773 this.bot_cmd_aim_begintime = time;
774 this.bot_cmd_aim_endtime = time + step;
776 return CMD_STATUS_EXECUTING;
779 float bot_cmd_aimtarget(entity this)
781 if(this.bot_cmd_aim_endtime)
783 return bot_cmd_aim(this);
791 parms = bot_cmd.bot_cmd_parm_string;
793 tokens = tokenizebyseparator(parms, " ");
795 e = bot_getplace(this, argv(0));
797 return CMD_STATUS_ERROR;
799 v = e.origin + (e.mins + e.maxs) * 0.5;
803 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
804 this.v_angle_x = -this.v_angle.x;
805 return CMD_STATUS_FINISHED;
808 if(tokens<1||tokens>2)
809 return CMD_STATUS_ERROR;
811 step = stof(argv(1));
813 this.bot_cmd_aim_begin = this.v_angle;
814 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
815 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
817 this.bot_cmd_aim_begintime = time;
818 this.bot_cmd_aim_endtime = time + step;
820 return CMD_STATUS_EXECUTING;
825 const int BOT_CMD_KEY_NONE = 0;
826 const int BOT_CMD_KEY_FORWARD = BIT(0);
827 const int BOT_CMD_KEY_BACKWARD = BIT(1);
828 const int BOT_CMD_KEY_RIGHT = BIT(2);
829 const int BOT_CMD_KEY_LEFT = BIT(3);
830 const int BOT_CMD_KEY_JUMP = BIT(4);
831 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
832 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
833 const int BOT_CMD_KEY_USE = BIT(7);
834 const int BOT_CMD_KEY_HOOK = BIT(8);
835 const int BOT_CMD_KEY_CROUCH = BIT(9);
836 const int BOT_CMD_KEY_CHAT = BIT(10);
838 bool bot_presskeys(entity this)
840 CS(this).movement = '0 0 0';
841 PHYS_INPUT_BUTTON_JUMP(this) = false;
842 PHYS_INPUT_BUTTON_CROUCH(this) = false;
843 PHYS_INPUT_BUTTON_ATCK(this) = false;
844 PHYS_INPUT_BUTTON_ATCK2(this) = false;
845 PHYS_INPUT_BUTTON_USE(this) = false;
846 PHYS_INPUT_BUTTON_HOOK(this) = false;
847 PHYS_INPUT_BUTTON_CHAT(this) = false;
849 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
852 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
853 CS(this).movement_x = autocvar_sv_maxspeed;
854 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
855 CS(this).movement_x = -autocvar_sv_maxspeed;
857 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
858 CS(this).movement_y = autocvar_sv_maxspeed;
859 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
860 CS(this).movement_y = -autocvar_sv_maxspeed;
862 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
863 PHYS_INPUT_BUTTON_JUMP(this) = true;
865 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
866 PHYS_INPUT_BUTTON_CROUCH(this) = true;
868 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
869 PHYS_INPUT_BUTTON_ATCK(this) = true;
871 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
872 PHYS_INPUT_BUTTON_ATCK2(this) = true;
874 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
875 PHYS_INPUT_BUTTON_USE(this) = true;
877 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
878 PHYS_INPUT_BUTTON_HOOK(this) = true;
880 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
881 PHYS_INPUT_BUTTON_CHAT(this) = true;
887 float bot_cmd_keypress_handler(entity this, string key, float enabled)
893 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
895 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
899 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
900 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
903 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
908 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
909 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
912 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
917 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
918 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
921 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
926 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
927 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
930 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
934 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
936 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
940 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
942 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
946 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
948 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
952 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
954 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
958 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
960 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
964 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
966 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
970 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
972 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
978 return CMD_STATUS_FINISHED;
981 float bot_cmd_presskey(entity this)
985 key = bot_cmd.bot_cmd_parm_string;
987 bot_cmd_keypress_handler(this, key,true);
989 return CMD_STATUS_FINISHED;
992 float bot_cmd_releasekey(entity this)
996 key = bot_cmd.bot_cmd_parm_string;
998 return bot_cmd_keypress_handler(this, key,false);
1001 float bot_cmd_pause(entity this)
1003 PHYS_INPUT_BUTTON_DRAG(this) = false;
1004 PHYS_INPUT_BUTTON_USE(this) = false;
1005 PHYS_INPUT_BUTTON_ATCK(this) = false;
1006 PHYS_INPUT_BUTTON_JUMP(this) = false;
1007 PHYS_INPUT_BUTTON_HOOK(this) = false;
1008 PHYS_INPUT_BUTTON_CHAT(this) = false;
1009 PHYS_INPUT_BUTTON_ATCK2(this) = false;
1010 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1012 CS(this).movement = '0 0 0';
1013 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1016 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1017 return CMD_STATUS_FINISHED;
1020 float bot_cmd_moveto(entity this)
1022 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1025 float bot_cmd_movetotarget(entity this)
1028 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1030 return CMD_STATUS_ERROR;
1031 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1034 float bot_cmd_resetgoal(entity this)
1036 return this.cmd_resetgoal(this);
1040 float bot_cmd_sound(entity this)
1043 f = bot_cmd.bot_cmd_parm_string;
1045 float n = tokenizebyseparator(f, " ");
1048 float chan = CH_WEAPON_B;
1049 float vol = VOL_BASE;
1050 float atten = ATTEN_MIN;
1053 sample = argv(n - 1);
1055 chan = stof(argv(0));
1057 vol = stof(argv(1));
1059 atten = stof(argv(2));
1062 _sound(this, chan, sample, vol, atten);
1064 return CMD_STATUS_FINISHED;
1068 float bot_cmd_debug_assert_canfire(entity this)
1070 float f = bot_cmd.bot_cmd_parm_float;
1072 int slot = 0; // TODO: unhardcode?
1073 .entity weaponentity = weaponentities[slot];
1074 if(this.(weaponentity).state != WS_READY)
1078 this.colormod = '0 8 8';
1079 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1082 else if(ATTACK_FINISHED(this, weaponentity) > time)
1086 this.colormod = '8 0 8';
1087 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left)");
1090 else if(this.(weaponentity).tuba_note)
1094 this.colormod = '8 0 0';
1095 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1102 this.colormod = '8 8 0';
1103 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");
1107 return CMD_STATUS_FINISHED;
1112 void bot_command_executed(entity this, bool rm)
1119 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1121 this.bot_cmd_execution_index++;
1124 void bot_setcurrentcommand(entity this)
1128 if(!this.bot_cmd_current)
1130 this.bot_cmd_current = new_pure(bot_cmd);
1133 bot_cmd = this.bot_cmd_current;
1134 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1136 if(bot_havecommand(this, this.bot_cmd_execution_index))
1139 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1140 if(bot_decodecommand(cmdstring))
1142 bot_cmd.owner = this;
1143 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1147 // Invalid command, remove from queue
1149 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1150 this.bot_cmd_execution_index++;
1158 void bot_resetqueues()
1160 FOREACH_CLIENT(it.isbot, {
1161 it.bot_cmd_execution_index = 0;
1163 // also, cancel all barriers
1165 for(int i = 0; i < it.bot_places_count; ++i)
1167 strfree(it.(bot_placenames[i]));
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(!(this.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.");
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_INFOF("ERROR: Invalid command on queue with id '%s'", ftos(bot_cmd.bot_cmd_type));
1301 if (status==CMD_STATUS_ERROR)
1302 LOG_INFOF("ERROR: The command '%s' returned an error status", bot_cmd_string[bot_cmd.bot_cmd_type]);
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);