1 #include "scripting.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include <server/weapons/selection.qh>
6 #include <server/weapons/weaponsystem.qh>
9 #include <common/state.qh>
10 #include <common/gamemodes/gamemode/ctf/sv_ctf.qh>
11 #include <common/physics/player.qh>
12 #include <common/wepent.qh>
18 .float bot_cmdqueuebuf_allocated;
19 .float bot_cmdqueuebuf;
20 .float bot_cmdqueuebuf_start;
21 .float bot_cmdqueuebuf_end;
23 void bot_clearqueue(entity bot)
25 if(!bot.bot_cmdqueuebuf_allocated)
27 buf_del(bot.bot_cmdqueuebuf);
28 bot.bot_cmdqueuebuf_allocated = false;
29 LOG_TRACE("bot ", bot.netname, " queue cleared");
32 void bot_queuecommand(entity bot, string cmdstring)
34 if(!bot.bot_cmdqueuebuf_allocated)
36 bot.bot_cmdqueuebuf = buf_create();
37 bot.bot_cmdqueuebuf_allocated = true;
38 bot.bot_cmdqueuebuf_start = 0;
39 bot.bot_cmdqueuebuf_end = 0;
42 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
44 // if the command was a "sound" command, precache the sound NOW
45 // this prevents lagging!
51 sp = strstrofs(cmdstring, " ", 0);
54 parm = substring(cmdstring, sp + 1, -1);
55 cmdstr = substring(cmdstring, 0, sp);
61 sp = strstrofs(parm, " ", 0);
64 parm = substring(parm, sp + 1, -1);
71 bot.bot_cmdqueuebuf_end += 1;
74 void bot_dequeuecommand(entity bot, float idx)
76 if(!bot.bot_cmdqueuebuf_allocated)
77 error("dequeuecommand but no queue allocated");
78 if(idx < bot.bot_cmdqueuebuf_start)
79 error("dequeueing a command in the past");
80 if(idx >= bot.bot_cmdqueuebuf_end)
81 error("dequeueing a command in the future");
82 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
83 if(idx == bot.bot_cmdqueuebuf_start)
84 bot.bot_cmdqueuebuf_start += 1;
85 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
89 string bot_readcommand(entity bot, float idx)
91 if(!bot.bot_cmdqueuebuf_allocated)
92 error("readcommand but no queue allocated");
93 if(idx < bot.bot_cmdqueuebuf_start)
94 error("reading a command in the past");
95 if(idx >= bot.bot_cmdqueuebuf_end)
96 error("reading a command in the future");
97 return bufstr_get(bot.bot_cmdqueuebuf, idx);
100 bool bot_havecommand(entity this, int idx)
102 if(!this.bot_cmdqueuebuf_allocated)
104 if(idx < this.bot_cmdqueuebuf_start)
106 if(idx >= this.bot_cmdqueuebuf_end)
111 const int MAX_BOT_PLACES = 4;
112 .float bot_places_count;
113 .entity bot_places[MAX_BOT_PLACES];
114 .string bot_placenames[MAX_BOT_PLACES];
115 entity bot_getplace(entity this, string placename)
118 if(substring(placename, 0, 1) == "@")
121 placename = substring(placename, 1, -1);
123 for(i = 0; i < this.bot_places_count; ++i)
124 if(this.(bot_placenames[i]) == placename)
125 return this.(bot_places[i]);
126 // now: i == this.bot_places_count
127 s = s2 = cvar_string(placename);
128 p = strstrofs(s2, " ", 0);
131 s = substring(s2, 0, p);
132 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
133 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
134 //print("places: ", placename, " := ", cvar_string(placename), "\n");
136 e = find(NULL, targetname, s);
138 LOG_INFO("invalid place ", s);
139 if(i < MAX_BOT_PLACES)
141 this.(bot_placenames[i]) = strzone(placename);
142 this.(bot_places[i]) = e;
143 this.bot_places_count += 1;
149 e = find(NULL, targetname, placename);
151 LOG_INFO("invalid place ", placename);
157 // Initialize global commands list
158 // NOTE: New commands should be initialized here
159 void bot_commands_init()
161 bot_cmd_string[BOT_CMD_NULL] = "";
162 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
164 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
165 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
167 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
168 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
170 bot_cmd_string[BOT_CMD_WAIT] = "wait";
171 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
173 bot_cmd_string[BOT_CMD_TURN] = "turn";
174 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
176 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
177 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
179 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
180 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
182 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
183 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
185 bot_cmd_string[BOT_CMD_CC] = "cc";
186 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
188 bot_cmd_string[BOT_CMD_IF] = "if";
189 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
191 bot_cmd_string[BOT_CMD_ELSE] = "else";
192 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
194 bot_cmd_string[BOT_CMD_FI] = "fi";
195 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
197 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
198 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
200 bot_cmd_string[BOT_CMD_AIM] = "aim";
201 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
203 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
204 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
206 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
207 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
209 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
210 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
212 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
213 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
215 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
216 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
218 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
219 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
221 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
222 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
224 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
225 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
227 bot_cmd_string[BOT_CMD_SOUND] = "sound";
228 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
230 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
231 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
233 bot_cmds_initialized = true;
236 // Returns first bot with matching name
237 entity find_bot_by_name(string name)
239 FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
247 // Returns a bot by number on list
248 entity find_bot_by_number(float number)
256 bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
259 if(IS_BOT_CLIENT(bot))
270 float bot_decodecommand(string cmdstring)
276 sp = strstrofs(cmdstring, " ", 0);
283 parm = substring(cmdstring, sp + 1, -1);
284 cmdstring = substring(cmdstring, 0, sp);
287 if(!bot_cmds_initialized)
291 for(i=1;i<BOT_CMD_COUNTER;++i)
293 if(bot_cmd_string[i]!=cmdstring)
296 cmd_parm_type = bot_cmd_parm_type[i];
298 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
300 LOG_INFO("ERROR: A parameter is required for this command");
304 // Load command into queue
305 bot_cmd.bot_cmd_type = i;
308 switch(cmd_parm_type)
310 case BOT_CMD_PARAMETER_FLOAT:
311 bot_cmd.bot_cmd_parm_float = stof(parm);
313 case BOT_CMD_PARAMETER_STRING:
314 strcpy(bot_cmd.bot_cmd_parm_string, parm);
316 case BOT_CMD_PARAMETER_VECTOR:
317 if(substring(parm, 0, 1) != "\'")
319 LOG_INFOF("ERROR: expected vector type \'x y z\', got %s", parm);
322 bot_cmd.bot_cmd_parm_vector = stov(parm);
329 LOG_INFO("ERROR: No such command '", cmdstring, "'");
333 void bot_cmdhelp(string scmd)
338 if(!bot_cmds_initialized)
341 for(i=1;i<BOT_CMD_COUNTER;++i)
343 if(bot_cmd_string[i]!=scmd)
346 ntype = bot_cmd_parm_type[i];
350 case BOT_CMD_PARAMETER_FLOAT:
351 stype = "float number";
353 case BOT_CMD_PARAMETER_STRING:
356 case BOT_CMD_PARAMETER_VECTOR:
368 desc = "Stops the bot completely. Any command other than 'continue' will be ignored.";
370 case BOT_CMD_CONTINUE:
371 desc = "Disable paused status";
374 desc = "Pause command parsing and bot ai for N seconds. Pressed key will remain pressed";
376 case BOT_CMD_WAIT_UNTIL:
377 desc = "Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed";
379 case BOT_CMD_BARRIER:
380 desc = "Waits till all bots that have a command queue reach this command. Pressed key will remain pressed";
383 desc = "Look to the right or left N degrees. For turning to the left use positive numbers.";
386 desc = "Walk to an specific coordinate on the map. Usage: moveto \'x y z\'";
388 case BOT_CMD_MOVETOTARGET:
389 desc = "Walk to the specific target on the map";
391 case BOT_CMD_RESETGOAL:
392 desc = "Resets the goal stack";
395 desc = "Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;";
398 desc = "Perform simple conditional execution.\n"
400 " sv_cmd .. if \"condition\"\n"
401 " sv_cmd .. <instruction if true>\n"
402 " sv_cmd .. <instruction if true>\n"
404 " sv_cmd .. <instruction if false>\n"
405 " sv_cmd .. <instruction if false>\n"
407 "Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n"
408 " Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n"
409 "Fields: health, speed, flagcarrier\n"
410 "Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;";
412 case BOT_CMD_RESETAIM:
413 desc = "Points the aim to the coordinates x,y 0,0";
416 desc = "Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n"
417 "There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n"
418 "Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n"
419 " aim \"0 90 2\" // Will gradually look to the sky in the next two seconds";
421 case BOT_CMD_AIMTARGET:
422 desc = "Points the aim to given target";
424 case BOT_CMD_PRESSKEY:
425 desc = "Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use"
426 "Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called"
427 "Note: The script will not return the control to the bot ai until all keys are released";
429 case BOT_CMD_RELEASEKEY:
430 desc = "Release previoulsy used keys. Use the parameter \"all\" to release all keys";
433 desc = "play sound file at bot location";
435 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
436 desc = "verify the state of the weapon entity";
439 desc = "This command has no description yet.";
442 LOG_HELP("Command: ", bot_cmd_string[i], "\nParameter: <", stype, ">", "\nDescription: ", desc);
446 void bot_list_commands()
451 if(!bot_cmds_initialized)
455 "List of all available commands:\n"
456 " Command - Parameter Type\n"
459 for(i=1;i<BOT_CMD_COUNTER;++i)
461 switch(bot_cmd_parm_type[i])
463 case BOT_CMD_PARAMETER_FLOAT:
464 ptype = "float number";
466 case BOT_CMD_PARAMETER_STRING:
469 case BOT_CMD_PARAMETER_VECTOR:
476 LOG_INFO(" ", bot_cmd_string[i]," - <", ptype, ">");
481 .int bot_exec_status;
483 float bot_cmd_cc(entity this)
485 SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
486 return CMD_STATUS_FINISHED;
489 float bot_cmd_impulse(entity this)
491 CS(this).impulse = bot_cmd.bot_cmd_parm_float;
492 return CMD_STATUS_FINISHED;
495 float bot_cmd_continue(entity this)
497 bot_relinkplayerlist();
498 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
499 return CMD_STATUS_FINISHED;
502 .float bot_cmd_wait_time;
503 float bot_cmd_wait(entity this)
505 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
507 if(time>=this.bot_cmd_wait_time)
509 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
510 return CMD_STATUS_FINISHED;
513 return CMD_STATUS_EXECUTING;
516 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
517 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
518 return CMD_STATUS_EXECUTING;
521 float bot_cmd_wait_until(entity this)
523 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
525 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
526 return CMD_STATUS_EXECUTING;
528 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
529 return CMD_STATUS_FINISHED;
532 float bot_cmd_barrier(entity this)
534 // 0 = no barrier, 1 = waiting, 2 = waiting finished
536 if(this.bot_barrier == 0) // initialization
538 this.bot_barrier = 1;
540 //this.colormod = '4 4 0';
543 if(this.bot_barrier == 1) // find other bots
545 FOREACH_CLIENT(it.isbot, {
546 if(it.bot_cmdqueuebuf_allocated)
547 if(it.bot_barrier != 1)
548 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
551 // all bots hit the barrier!
553 // acknowledge barrier
554 FOREACH_CLIENT(it.isbot, { it.bot_barrier = 2; });
556 bot_barriertime = time;
559 // if we get here, the barrier is finished
561 this.bot_barrier = 0;
562 //this.colormod = '0 0 0';
564 return CMD_STATUS_FINISHED;
567 float bot_cmd_turn(entity this)
569 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
570 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
571 return CMD_STATUS_FINISHED;
574 float bot_cmd_select_weapon(entity this)
576 float id = bot_cmd.bot_cmd_parm_float;
578 if(id < WEP_FIRST || id > WEP_LAST)
579 return CMD_STATUS_ERROR;
581 bool success = false;
583 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
585 .entity weaponentity = weaponentities[slot];
586 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
589 if(client_hasweapon(this, REGISTRY_GET(Weapons, id), weaponentity, true, false))
592 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, id);
597 return CMD_STATUS_ERROR;
599 return CMD_STATUS_FINISHED;
602 .int bot_cmd_condition_status;
604 const int CMD_CONDITION_NONE = 0;
605 const int CMD_CONDITION_true = 1;
606 const int CMD_CONDITION_false = 2;
607 const int CMD_CONDITION_true_BLOCK = 4;
608 const int CMD_CONDITION_false_BLOCK = 8;
610 float bot_cmd_eval(entity this, string expr)
612 // Search for numbers
613 if(IS_DIGIT(substring(expr, 0, 1)))
617 if(substring(expr, 0, 5)=="cvar.")
618 return cvar(substring(expr, 5, strlen(expr)));
621 // TODO: expand with support for more fields (key carrier, ball carrier, armor etc)
625 return GetResource(this, RES_HEALTH);
627 return vlen(this.velocity);
629 return ((this.flagcarried!=NULL));
632 LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
636 float bot_cmd_if(entity this)
638 string expr, val_a, val_b;
641 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
643 // Only one "if" block is allowed at time
644 LOG_INFO("ERROR: Only one conditional block can be processed at time");
645 bot_clearqueue(this);
646 return CMD_STATUS_ERROR;
649 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
651 // search for operators
652 expr = bot_cmd.bot_cmd_parm_string;
654 cmpofs = strstrofs(expr,"=",0);
658 val_a = substring(expr,0,cmpofs);
659 val_b = substring(expr,cmpofs+1,strlen(expr));
661 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
662 this.bot_cmd_condition_status |= CMD_CONDITION_true;
664 this.bot_cmd_condition_status |= CMD_CONDITION_false;
666 return CMD_STATUS_FINISHED;
669 cmpofs = strstrofs(expr,">",0);
673 val_a = substring(expr,0,cmpofs);
674 val_b = substring(expr,cmpofs+1,strlen(expr));
676 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
677 this.bot_cmd_condition_status |= CMD_CONDITION_true;
679 this.bot_cmd_condition_status |= CMD_CONDITION_false;
681 return CMD_STATUS_FINISHED;
684 cmpofs = strstrofs(expr,"<",0);
688 val_a = substring(expr,0,cmpofs);
689 val_b = substring(expr,cmpofs+1,strlen(expr));
691 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
692 this.bot_cmd_condition_status |= CMD_CONDITION_true;
694 this.bot_cmd_condition_status |= CMD_CONDITION_false;
696 return CMD_STATUS_FINISHED;
699 if(bot_cmd_eval(this, expr))
700 this.bot_cmd_condition_status |= CMD_CONDITION_true;
702 this.bot_cmd_condition_status |= CMD_CONDITION_false;
704 return CMD_STATUS_FINISHED;
707 float bot_cmd_else(entity this)
709 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
710 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
711 return CMD_STATUS_FINISHED;
714 float bot_cmd_fi(entity this)
716 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
717 return CMD_STATUS_FINISHED;
720 float bot_cmd_resetaim(entity this)
722 this.v_angle = '0 0 0';
723 return CMD_STATUS_FINISHED;
726 .float bot_cmd_aim_begintime;
727 .float bot_cmd_aim_endtime;
728 .vector bot_cmd_aim_begin;
729 .vector bot_cmd_aim_end;
731 float bot_cmd_aim(entity this)
734 if(this.bot_cmd_aim_endtime)
738 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
739 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
741 if(time>=this.bot_cmd_aim_endtime)
743 this.bot_cmd_aim_endtime = 0;
744 return CMD_STATUS_FINISHED;
747 return CMD_STATUS_EXECUTING;
750 // New aiming direction
754 parms = bot_cmd.bot_cmd_parm_string;
756 tokens = tokenizebyseparator(parms, " ");
758 if(tokens<2||tokens>3)
759 return CMD_STATUS_ERROR;
761 step = (tokens == 3) ? stof(argv(2)) : 0;
765 this.v_angle_x -= stof(argv(1));
766 this.v_angle_y += stof(argv(0));
767 return CMD_STATUS_FINISHED;
770 this.bot_cmd_aim_begin = this.v_angle;
772 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
773 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
774 this.bot_cmd_aim_end_z = 0;
776 this.bot_cmd_aim_begintime = time;
777 this.bot_cmd_aim_endtime = time + step;
779 return CMD_STATUS_EXECUTING;
782 float bot_cmd_aimtarget(entity this)
784 if(this.bot_cmd_aim_endtime)
786 return bot_cmd_aim(this);
794 parms = bot_cmd.bot_cmd_parm_string;
796 tokens = tokenizebyseparator(parms, " ");
798 e = bot_getplace(this, argv(0));
800 return CMD_STATUS_ERROR;
802 v = e.origin + (e.mins + e.maxs) * 0.5;
806 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
807 this.v_angle_x = -this.v_angle.x;
808 return CMD_STATUS_FINISHED;
811 if(tokens<1||tokens>2)
812 return CMD_STATUS_ERROR;
814 step = stof(argv(1));
816 this.bot_cmd_aim_begin = this.v_angle;
817 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
818 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
820 this.bot_cmd_aim_begintime = time;
821 this.bot_cmd_aim_endtime = time + step;
823 return CMD_STATUS_EXECUTING;
828 const int BOT_CMD_KEY_NONE = 0;
829 const int BOT_CMD_KEY_FORWARD = BIT(0);
830 const int BOT_CMD_KEY_BACKWARD = BIT(1);
831 const int BOT_CMD_KEY_RIGHT = BIT(2);
832 const int BOT_CMD_KEY_LEFT = BIT(3);
833 const int BOT_CMD_KEY_JUMP = BIT(4);
834 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
835 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
836 const int BOT_CMD_KEY_USE = BIT(7);
837 const int BOT_CMD_KEY_HOOK = BIT(8);
838 const int BOT_CMD_KEY_CROUCH = BIT(9);
839 const int BOT_CMD_KEY_CHAT = BIT(10);
841 bool bot_presskeys(entity this)
843 CS(this).movement = '0 0 0';
844 PHYS_INPUT_BUTTON_JUMP(this) = false;
845 PHYS_INPUT_BUTTON_CROUCH(this) = false;
846 PHYS_INPUT_BUTTON_ATCK(this) = false;
847 PHYS_INPUT_BUTTON_ATCK2(this) = false;
848 PHYS_INPUT_BUTTON_USE(this) = false;
849 PHYS_INPUT_BUTTON_HOOK(this) = false;
850 PHYS_INPUT_BUTTON_CHAT(this) = false;
852 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
855 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
856 CS(this).movement_x = autocvar_sv_maxspeed;
857 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
858 CS(this).movement_x = -autocvar_sv_maxspeed;
860 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
861 CS(this).movement_y = autocvar_sv_maxspeed;
862 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
863 CS(this).movement_y = -autocvar_sv_maxspeed;
865 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
866 PHYS_INPUT_BUTTON_JUMP(this) = true;
868 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
869 PHYS_INPUT_BUTTON_CROUCH(this) = true;
871 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
872 PHYS_INPUT_BUTTON_ATCK(this) = true;
874 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
875 PHYS_INPUT_BUTTON_ATCK2(this) = true;
877 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
878 PHYS_INPUT_BUTTON_USE(this) = true;
880 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
881 PHYS_INPUT_BUTTON_HOOK(this) = true;
883 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
884 PHYS_INPUT_BUTTON_CHAT(this) = true;
890 float bot_cmd_keypress_handler(entity this, string key, float enabled)
896 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
898 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
902 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
903 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
906 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
911 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
912 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
915 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
920 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
921 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
924 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
929 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
930 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
933 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
937 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
939 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
943 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
945 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
949 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
951 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
955 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
957 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
961 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
963 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
967 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
969 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
973 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
975 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
981 return CMD_STATUS_FINISHED;
984 float bot_cmd_presskey(entity this)
988 key = bot_cmd.bot_cmd_parm_string;
990 bot_cmd_keypress_handler(this, key,true);
992 return CMD_STATUS_FINISHED;
995 float bot_cmd_releasekey(entity this)
999 key = bot_cmd.bot_cmd_parm_string;
1001 return bot_cmd_keypress_handler(this, key,false);
1004 float bot_cmd_pause(entity this)
1006 PHYS_INPUT_BUTTON_DRAG(this) = false;
1007 PHYS_INPUT_BUTTON_USE(this) = false;
1008 PHYS_INPUT_BUTTON_ATCK(this) = false;
1009 PHYS_INPUT_BUTTON_JUMP(this) = false;
1010 PHYS_INPUT_BUTTON_HOOK(this) = false;
1011 PHYS_INPUT_BUTTON_CHAT(this) = false;
1012 PHYS_INPUT_BUTTON_ATCK2(this) = false;
1013 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1015 CS(this).movement = '0 0 0';
1016 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1019 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1020 return CMD_STATUS_FINISHED;
1023 float bot_cmd_moveto(entity this)
1025 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1028 float bot_cmd_movetotarget(entity this)
1031 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1033 return CMD_STATUS_ERROR;
1034 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1037 float bot_cmd_resetgoal(entity this)
1039 return this.cmd_resetgoal(this);
1043 float bot_cmd_sound(entity this)
1046 f = bot_cmd.bot_cmd_parm_string;
1048 float n = tokenizebyseparator(f, " ");
1051 float chan = CH_WEAPON_B;
1052 float vol = VOL_BASE;
1053 float atten = ATTEN_MIN;
1056 sample = argv(n - 1);
1058 chan = stof(argv(0));
1060 vol = stof(argv(1));
1062 atten = stof(argv(2));
1065 _sound(this, chan, sample, vol, atten);
1067 return CMD_STATUS_FINISHED;
1071 float bot_cmd_debug_assert_canfire(entity this)
1073 float f = bot_cmd.bot_cmd_parm_float;
1075 int slot = 0; // TODO: unhardcode?
1076 .entity weaponentity = weaponentities[slot];
1077 if(this.(weaponentity).state != WS_READY)
1081 this.colormod = '0 8 8';
1082 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1085 else if(ATTACK_FINISHED(this, weaponentity) > time)
1089 this.colormod = '8 0 8';
1090 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left)");
1093 else if(this.(weaponentity).tuba_note)
1097 this.colormod = '8 0 0';
1098 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1105 this.colormod = '8 8 0';
1106 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");
1110 return CMD_STATUS_FINISHED;
1115 void bot_command_executed(entity this, bool rm)
1122 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1124 this.bot_cmd_execution_index++;
1127 void bot_setcurrentcommand(entity this)
1131 if(!this.bot_cmd_current)
1133 this.bot_cmd_current = new_pure(bot_cmd);
1136 bot_cmd = this.bot_cmd_current;
1137 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1139 if(bot_havecommand(this, this.bot_cmd_execution_index))
1142 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1143 if(bot_decodecommand(cmdstring))
1145 bot_cmd.owner = this;
1146 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1150 // Invalid command, remove from queue
1152 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1153 this.bot_cmd_execution_index++;
1161 void bot_resetqueues()
1163 FOREACH_CLIENT(it.isbot, {
1164 it.bot_cmd_execution_index = 0;
1166 // also, cancel all barriers
1168 for(int i = 0; i < it.bot_places_count; ++i)
1170 strfree(it.(bot_placenames[i]));
1172 it.bot_places_count = 0;
1175 bot_barriertime = time;
1178 // Here we map commands to functions and deal with complex interactions between commands and execution states
1179 // NOTE: Of course you need to include your commands here too :)
1180 float bot_execute_commands_once(entity this)
1182 float status, ispressingkey;
1185 bot_setcurrentcommand(this);
1187 // Ignore all commands except continue when the bot is paused
1188 if(!(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1190 // if we have no bot command, better return
1191 // old logic kept pressing previously pressed keys, but that has problems
1192 // (namely, it means you cannot make a bot "normal" ever again)
1193 // to keep a bot walking for a while, use the "wait" bot command
1194 if(bot_cmd == world)
1197 else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1199 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1201 bot_command_executed(this, true);
1202 LOG_INFO("WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.");
1207 // Keep pressing keys raised by the "presskey" command
1208 ispressingkey = boolean(bot_presskeys(this));
1210 // Handle conditions
1211 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1212 if((this.bot_cmd_condition_status & CMD_CONDITION_true) && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1214 bot_command_executed(this, true);
1217 else if((this.bot_cmd_condition_status & CMD_CONDITION_false) && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1219 bot_command_executed(this, true);
1223 // Map commands to functions
1224 switch(bot_cmd.bot_cmd_type)
1227 return ispressingkey;
1230 status = bot_cmd_pause(this);
1232 case BOT_CMD_CONTINUE:
1233 status = bot_cmd_continue(this);
1236 status = bot_cmd_wait(this);
1238 case BOT_CMD_WAIT_UNTIL:
1239 status = bot_cmd_wait_until(this);
1242 status = bot_cmd_turn(this);
1244 case BOT_CMD_MOVETO:
1245 status = bot_cmd_moveto(this);
1247 case BOT_CMD_MOVETOTARGET:
1248 status = bot_cmd_movetotarget(this);
1250 case BOT_CMD_RESETGOAL:
1251 status = bot_cmd_resetgoal(this);
1254 status = bot_cmd_cc(this);
1257 status = bot_cmd_if(this);
1260 status = bot_cmd_else(this);
1263 status = bot_cmd_fi(this);
1265 case BOT_CMD_RESETAIM:
1266 status = bot_cmd_resetaim(this);
1269 status = bot_cmd_aim(this);
1271 case BOT_CMD_AIMTARGET:
1272 status = bot_cmd_aimtarget(this);
1274 case BOT_CMD_PRESSKEY:
1275 status = bot_cmd_presskey(this);
1277 case BOT_CMD_RELEASEKEY:
1278 status = bot_cmd_releasekey(this);
1280 case BOT_CMD_SELECTWEAPON:
1281 status = bot_cmd_select_weapon(this);
1283 case BOT_CMD_IMPULSE:
1284 status = bot_cmd_impulse(this);
1286 case BOT_CMD_BARRIER:
1287 status = bot_cmd_barrier(this);
1289 case BOT_CMD_CONSOLE:
1290 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1291 status = CMD_STATUS_FINISHED;
1294 status = bot_cmd_sound(this);
1296 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1297 status = bot_cmd_debug_assert_canfire(this);
1300 LOG_INFOF("ERROR: Invalid command on queue with id '%s'", ftos(bot_cmd.bot_cmd_type));
1304 if (status==CMD_STATUS_ERROR)
1305 LOG_INFOF("ERROR: The command '%s' returned an error status", bot_cmd_string[bot_cmd.bot_cmd_type]);
1307 // Move execution pointer
1308 if(status==CMD_STATUS_EXECUTING)
1314 if(autocvar_g_debug_bot_commands)
1318 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1320 case BOT_CMD_PARAMETER_FLOAT:
1321 parms = ftos(bot_cmd.bot_cmd_parm_float);
1323 case BOT_CMD_PARAMETER_STRING:
1324 parms = bot_cmd.bot_cmd_parm_string;
1326 case BOT_CMD_PARAMETER_VECTOR:
1327 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1333 clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1336 bot_command_executed(this, true);
1339 if(status == CMD_STATUS_FINISHED)
1342 return CMD_STATUS_ERROR;
1345 // This function should be (the only) called directly from the bot ai loop
1346 int bot_execute_commands(entity this)
1351 f = bot_execute_commands_once(this);