1 #include "scripting.qh"
3 #include <common/weapons/_all.qh>
4 #include <common/stats.qh>
5 #include <server/miscfunctions.qh>
6 #include <server/weapons/selection.qh>
7 #include <server/weapons/weaponsystem.qh>
10 #include <common/state.qh>
11 #include <common/gamemodes/gamemode/ctf/sv_ctf.qh>
12 #include <common/physics/player.qh>
13 #include <common/wepent.qh>
19 .float bot_cmdqueuebuf_allocated;
20 .float bot_cmdqueuebuf;
21 .float bot_cmdqueuebuf_start;
22 .float bot_cmdqueuebuf_end;
24 void bot_clearqueue(entity bot)
26 if(!bot.bot_cmdqueuebuf_allocated)
28 buf_del(bot.bot_cmdqueuebuf);
29 bot.bot_cmdqueuebuf_allocated = false;
30 LOG_TRACE("bot ", bot.netname, " queue cleared");
33 void bot_queuecommand(entity bot, string cmdstring)
35 if(!bot.bot_cmdqueuebuf_allocated)
37 bot.bot_cmdqueuebuf = buf_create();
38 bot.bot_cmdqueuebuf_allocated = true;
39 bot.bot_cmdqueuebuf_start = 0;
40 bot.bot_cmdqueuebuf_end = 0;
43 bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
45 // if the command was a "sound" command, precache the sound NOW
46 // this prevents lagging!
52 sp = strstrofs(cmdstring, " ", 0);
55 parm = substring(cmdstring, sp + 1, -1);
56 cmdstr = substring(cmdstring, 0, sp);
62 sp = strstrofs(parm, " ", 0);
65 parm = substring(parm, sp + 1, -1);
72 bot.bot_cmdqueuebuf_end += 1;
75 void bot_dequeuecommand(entity bot, float idx)
77 if(!bot.bot_cmdqueuebuf_allocated)
78 error("dequeuecommand but no queue allocated");
79 if(idx < bot.bot_cmdqueuebuf_start)
80 error("dequeueing a command in the past");
81 if(idx >= bot.bot_cmdqueuebuf_end)
82 error("dequeueing a command in the future");
83 bufstr_set(bot.bot_cmdqueuebuf, idx, "");
84 if(idx == bot.bot_cmdqueuebuf_start)
85 bot.bot_cmdqueuebuf_start += 1;
86 if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
90 string bot_readcommand(entity bot, float idx)
92 if(!bot.bot_cmdqueuebuf_allocated)
93 error("readcommand but no queue allocated");
94 if(idx < bot.bot_cmdqueuebuf_start)
95 error("reading a command in the past");
96 if(idx >= bot.bot_cmdqueuebuf_end)
97 error("reading a command in the future");
98 return bufstr_get(bot.bot_cmdqueuebuf, idx);
101 bool bot_havecommand(entity this, int idx)
103 if(!this.bot_cmdqueuebuf_allocated)
105 if(idx < this.bot_cmdqueuebuf_start)
107 if(idx >= this.bot_cmdqueuebuf_end)
112 const int MAX_BOT_PLACES = 4;
113 .float bot_places_count;
114 .entity bot_places[MAX_BOT_PLACES];
115 .string bot_placenames[MAX_BOT_PLACES];
116 entity bot_getplace(entity this, string placename)
119 if(substring(placename, 0, 1) == "@")
122 placename = substring(placename, 1, -1);
124 for(i = 0; i < this.bot_places_count; ++i)
125 if(this.(bot_placenames[i]) == placename)
126 return this.(bot_places[i]);
127 // now: i == this.bot_places_count
128 s = s2 = cvar_string(placename);
129 p = strstrofs(s2, " ", 0);
132 s = substring(s2, 0, p);
133 //print("places: ", placename, " -> ", cvar_string(placename), "\n");
134 cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
135 //print("places: ", placename, " := ", cvar_string(placename), "\n");
137 e = find(NULL, targetname, s);
139 LOG_INFO("invalid place ", s);
140 if(i < MAX_BOT_PLACES)
142 this.(bot_placenames[i]) = strzone(placename);
143 this.(bot_places[i]) = e;
144 this.bot_places_count += 1;
150 e = find(NULL, targetname, placename);
152 LOG_INFO("invalid place ", placename);
158 // Initialize global commands list
159 // NOTE: New commands should be initialized here
160 void bot_commands_init()
162 bot_cmd_string[BOT_CMD_NULL] = "";
163 bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
165 bot_cmd_string[BOT_CMD_PAUSE] = "pause";
166 bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
168 bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
169 bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
171 bot_cmd_string[BOT_CMD_WAIT] = "wait";
172 bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
174 bot_cmd_string[BOT_CMD_TURN] = "turn";
175 bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
177 bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
178 bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
180 bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
181 bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
183 bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
184 bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
186 bot_cmd_string[BOT_CMD_CC] = "cc";
187 bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
189 bot_cmd_string[BOT_CMD_IF] = "if";
190 bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
192 bot_cmd_string[BOT_CMD_ELSE] = "else";
193 bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
195 bot_cmd_string[BOT_CMD_FI] = "fi";
196 bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
198 bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
199 bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
201 bot_cmd_string[BOT_CMD_AIM] = "aim";
202 bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
204 bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
205 bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
207 bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
208 bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
210 bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
211 bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
213 bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
214 bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
216 bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
217 bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
219 bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
220 bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
222 bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
223 bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
225 bot_cmd_string[BOT_CMD_CONSOLE] = "console";
226 bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
228 bot_cmd_string[BOT_CMD_SOUND] = "sound";
229 bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
231 bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
232 bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
234 bot_cmds_initialized = true;
237 // Returns first bot with matching name
238 entity find_bot_by_name(string name)
240 FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
248 // Returns a bot by number on list
249 entity find_bot_by_number(float number)
257 bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
260 if(IS_BOT_CLIENT(bot))
271 float bot_decodecommand(string cmdstring)
277 sp = strstrofs(cmdstring, " ", 0);
284 parm = substring(cmdstring, sp + 1, -1);
285 cmdstring = substring(cmdstring, 0, sp);
288 if(!bot_cmds_initialized)
292 for(i=1;i<BOT_CMD_COUNTER;++i)
294 if(bot_cmd_string[i]!=cmdstring)
297 cmd_parm_type = bot_cmd_parm_type[i];
299 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
301 LOG_INFO("ERROR: A parameter is required for this command");
305 // Load command into queue
306 bot_cmd.bot_cmd_type = i;
309 switch(cmd_parm_type)
311 case BOT_CMD_PARAMETER_FLOAT:
312 bot_cmd.bot_cmd_parm_float = stof(parm);
314 case BOT_CMD_PARAMETER_STRING:
315 strcpy(bot_cmd.bot_cmd_parm_string, parm);
317 case BOT_CMD_PARAMETER_VECTOR:
318 if(substring(parm, 0, 1) != "\'")
320 LOG_INFOF("ERROR: expected vector type \'x y z\', got %s", parm);
323 bot_cmd.bot_cmd_parm_vector = stov(parm);
330 LOG_INFO("ERROR: No such command '", cmdstring, "'");
334 void bot_cmdhelp(string scmd)
339 if(!bot_cmds_initialized)
342 for(i=1;i<BOT_CMD_COUNTER;++i)
344 if(bot_cmd_string[i]!=scmd)
347 ntype = bot_cmd_parm_type[i];
351 case BOT_CMD_PARAMETER_FLOAT:
352 stype = "float number";
354 case BOT_CMD_PARAMETER_STRING:
357 case BOT_CMD_PARAMETER_VECTOR:
369 desc = "Stops the bot completely. Any command other than 'continue' will be ignored.";
371 case BOT_CMD_CONTINUE:
372 desc = "Disable paused status";
375 desc = "Pause command parsing and bot ai for N seconds. Pressed key will remain pressed";
377 case BOT_CMD_WAIT_UNTIL:
378 desc = "Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed";
380 case BOT_CMD_BARRIER:
381 desc = "Waits till all bots that have a command queue reach this command. Pressed key will remain pressed";
384 desc = "Look to the right or left N degrees. For turning to the left use positive numbers.";
387 desc = "Walk to an specific coordinate on the map. Usage: moveto \'x y z\'";
389 case BOT_CMD_MOVETOTARGET:
390 desc = "Walk to the specific target on the map";
392 case BOT_CMD_RESETGOAL:
393 desc = "Resets the goal stack";
396 desc = "Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;";
399 desc = "Perform simple conditional execution.\n"
401 " sv_cmd .. if \"condition\"\n"
402 " sv_cmd .. <instruction if true>\n"
403 " sv_cmd .. <instruction if true>\n"
405 " sv_cmd .. <instruction if false>\n"
406 " sv_cmd .. <instruction if false>\n"
408 "Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n"
409 " Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n"
410 "Fields: health, speed, flagcarrier\n"
411 "Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;";
413 case BOT_CMD_RESETAIM:
414 desc = "Points the aim to the coordinates x,y 0,0";
417 desc = "Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n"
418 "There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n"
419 "Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n"
420 " aim \"0 90 2\" // Will gradually look to the sky in the next two seconds";
422 case BOT_CMD_AIMTARGET:
423 desc = "Points the aim to given target";
425 case BOT_CMD_PRESSKEY:
426 desc = "Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use"
427 "Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called"
428 "Note: The script will not return the control to the bot ai until all keys are released";
430 case BOT_CMD_RELEASEKEY:
431 desc = "Release previoulsy used keys. Use the parameter \"all\" to release all keys";
434 desc = "play sound file at bot location";
436 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
437 desc = "verify the state of the weapon entity";
440 desc = "This command has no description yet.";
443 LOG_HELP("Command: ", bot_cmd_string[i], "\nParameter: <", stype, ">", "\nDescription: ", desc);
447 void bot_list_commands()
452 if(!bot_cmds_initialized)
456 "List of all available commands:\n"
457 " Command - Parameter Type\n"
460 for(i=1;i<BOT_CMD_COUNTER;++i)
462 switch(bot_cmd_parm_type[i])
464 case BOT_CMD_PARAMETER_FLOAT:
465 ptype = "float number";
467 case BOT_CMD_PARAMETER_STRING:
470 case BOT_CMD_PARAMETER_VECTOR:
477 LOG_INFO(" ", bot_cmd_string[i]," - <", ptype, ">");
482 .int bot_exec_status;
484 float bot_cmd_cc(entity this)
486 SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
487 return CMD_STATUS_FINISHED;
490 float bot_cmd_impulse(entity this)
492 CS(this).impulse = bot_cmd.bot_cmd_parm_float;
493 return CMD_STATUS_FINISHED;
496 float bot_cmd_continue(entity this)
498 bot_relinkplayerlist();
499 this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
500 return CMD_STATUS_FINISHED;
503 .float bot_cmd_wait_time;
504 float bot_cmd_wait(entity this)
506 if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
508 if(time>=this.bot_cmd_wait_time)
510 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
511 return CMD_STATUS_FINISHED;
514 return CMD_STATUS_EXECUTING;
517 this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
518 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
519 return CMD_STATUS_EXECUTING;
522 float bot_cmd_wait_until(entity this)
524 if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
526 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
527 return CMD_STATUS_EXECUTING;
529 this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
530 return CMD_STATUS_FINISHED;
533 float bot_cmd_barrier(entity this)
535 // 0 = no barrier, 1 = waiting, 2 = waiting finished
537 if(this.bot_barrier == 0) // initialization
539 this.bot_barrier = 1;
541 //this.colormod = '4 4 0';
544 if(this.bot_barrier == 1) // find other bots
546 FOREACH_CLIENT(it.isbot, {
547 if(it.bot_cmdqueuebuf_allocated)
548 if(it.bot_barrier != 1)
549 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
552 // all bots hit the barrier!
554 // acknowledge barrier
555 FOREACH_CLIENT(it.isbot, { it.bot_barrier = 2; });
557 bot_barriertime = time;
560 // if we get here, the barrier is finished
562 this.bot_barrier = 0;
563 //this.colormod = '0 0 0';
565 return CMD_STATUS_FINISHED;
568 float bot_cmd_turn(entity this)
570 this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
571 this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
572 return CMD_STATUS_FINISHED;
575 float bot_cmd_select_weapon(entity this)
577 float id = bot_cmd.bot_cmd_parm_float;
579 if(id < WEP_FIRST || id > WEP_LAST)
580 return CMD_STATUS_ERROR;
582 bool success = false;
584 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
586 .entity weaponentity = weaponentities[slot];
587 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
590 if(client_hasweapon(this, REGISTRY_GET(Weapons, id), weaponentity, true, false))
593 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, id);
598 return CMD_STATUS_ERROR;
600 return CMD_STATUS_FINISHED;
603 .int bot_cmd_condition_status;
605 const int CMD_CONDITION_NONE = 0;
606 const int CMD_CONDITION_true = 1;
607 const int CMD_CONDITION_false = 2;
608 const int CMD_CONDITION_true_BLOCK = 4;
609 const int CMD_CONDITION_false_BLOCK = 8;
611 float bot_cmd_eval(entity this, string expr)
613 // Search for numbers
614 if(IS_DIGIT(substring(expr, 0, 1)))
618 if(substring(expr, 0, 5)=="cvar.")
619 return cvar(substring(expr, 5, strlen(expr)));
622 // TODO: expand with support for more fields (key carrier, ball carrier, armor etc)
626 return GetResource(this, RES_HEALTH);
628 return vlen(this.velocity);
630 return ((this.flagcarried!=NULL));
633 LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
637 float bot_cmd_if(entity this)
639 string expr, val_a, val_b;
642 if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
644 // Only one "if" block is allowed at time
645 LOG_INFO("ERROR: Only one conditional block can be processed at time");
646 bot_clearqueue(this);
647 return CMD_STATUS_ERROR;
650 this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
652 // search for operators
653 expr = bot_cmd.bot_cmd_parm_string;
655 cmpofs = strstrofs(expr,"=",0);
659 val_a = substring(expr,0,cmpofs);
660 val_b = substring(expr,cmpofs+1,strlen(expr));
662 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
663 this.bot_cmd_condition_status |= CMD_CONDITION_true;
665 this.bot_cmd_condition_status |= CMD_CONDITION_false;
667 return CMD_STATUS_FINISHED;
670 cmpofs = strstrofs(expr,">",0);
674 val_a = substring(expr,0,cmpofs);
675 val_b = substring(expr,cmpofs+1,strlen(expr));
677 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
678 this.bot_cmd_condition_status |= CMD_CONDITION_true;
680 this.bot_cmd_condition_status |= CMD_CONDITION_false;
682 return CMD_STATUS_FINISHED;
685 cmpofs = strstrofs(expr,"<",0);
689 val_a = substring(expr,0,cmpofs);
690 val_b = substring(expr,cmpofs+1,strlen(expr));
692 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
693 this.bot_cmd_condition_status |= CMD_CONDITION_true;
695 this.bot_cmd_condition_status |= CMD_CONDITION_false;
697 return CMD_STATUS_FINISHED;
700 if(bot_cmd_eval(this, expr))
701 this.bot_cmd_condition_status |= CMD_CONDITION_true;
703 this.bot_cmd_condition_status |= CMD_CONDITION_false;
705 return CMD_STATUS_FINISHED;
708 float bot_cmd_else(entity this)
710 this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
711 this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
712 return CMD_STATUS_FINISHED;
715 float bot_cmd_fi(entity this)
717 this.bot_cmd_condition_status = CMD_CONDITION_NONE;
718 return CMD_STATUS_FINISHED;
721 float bot_cmd_resetaim(entity this)
723 this.v_angle = '0 0 0';
724 return CMD_STATUS_FINISHED;
727 .float bot_cmd_aim_begintime;
728 .float bot_cmd_aim_endtime;
729 .vector bot_cmd_aim_begin;
730 .vector bot_cmd_aim_end;
732 float bot_cmd_aim(entity this)
735 if(this.bot_cmd_aim_endtime)
739 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
740 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
742 if(time>=this.bot_cmd_aim_endtime)
744 this.bot_cmd_aim_endtime = 0;
745 return CMD_STATUS_FINISHED;
748 return CMD_STATUS_EXECUTING;
751 // New aiming direction
755 parms = bot_cmd.bot_cmd_parm_string;
757 tokens = tokenizebyseparator(parms, " ");
759 if(tokens<2||tokens>3)
760 return CMD_STATUS_ERROR;
762 step = (tokens == 3) ? stof(argv(2)) : 0;
766 this.v_angle_x -= stof(argv(1));
767 this.v_angle_y += stof(argv(0));
768 return CMD_STATUS_FINISHED;
771 this.bot_cmd_aim_begin = this.v_angle;
773 this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
774 this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
775 this.bot_cmd_aim_end_z = 0;
777 this.bot_cmd_aim_begintime = time;
778 this.bot_cmd_aim_endtime = time + step;
780 return CMD_STATUS_EXECUTING;
783 float bot_cmd_aimtarget(entity this)
785 if(this.bot_cmd_aim_endtime)
787 return bot_cmd_aim(this);
795 parms = bot_cmd.bot_cmd_parm_string;
797 tokens = tokenizebyseparator(parms, " ");
799 e = bot_getplace(this, argv(0));
801 return CMD_STATUS_ERROR;
803 v = e.origin + (e.mins + e.maxs) * 0.5;
807 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
808 this.v_angle_x = -this.v_angle.x;
809 return CMD_STATUS_FINISHED;
812 if(tokens<1||tokens>2)
813 return CMD_STATUS_ERROR;
815 step = stof(argv(1));
817 this.bot_cmd_aim_begin = this.v_angle;
818 this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
819 this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
821 this.bot_cmd_aim_begintime = time;
822 this.bot_cmd_aim_endtime = time + step;
824 return CMD_STATUS_EXECUTING;
829 const int BOT_CMD_KEY_NONE = 0;
830 const int BOT_CMD_KEY_FORWARD = BIT(0);
831 const int BOT_CMD_KEY_BACKWARD = BIT(1);
832 const int BOT_CMD_KEY_RIGHT = BIT(2);
833 const int BOT_CMD_KEY_LEFT = BIT(3);
834 const int BOT_CMD_KEY_JUMP = BIT(4);
835 const int BOT_CMD_KEY_ATTACK1 = BIT(5);
836 const int BOT_CMD_KEY_ATTACK2 = BIT(6);
837 const int BOT_CMD_KEY_USE = BIT(7);
838 const int BOT_CMD_KEY_HOOK = BIT(8);
839 const int BOT_CMD_KEY_CROUCH = BIT(9);
840 const int BOT_CMD_KEY_CHAT = BIT(10);
842 bool bot_presskeys(entity this)
844 CS(this).movement = '0 0 0';
845 PHYS_INPUT_BUTTON_JUMP(this) = false;
846 PHYS_INPUT_BUTTON_CROUCH(this) = false;
847 PHYS_INPUT_BUTTON_ATCK(this) = false;
848 PHYS_INPUT_BUTTON_ATCK2(this) = false;
849 PHYS_INPUT_BUTTON_USE(this) = false;
850 PHYS_INPUT_BUTTON_HOOK(this) = false;
851 PHYS_INPUT_BUTTON_CHAT(this) = false;
853 if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
856 if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
857 CS(this).movement_x = autocvar_sv_maxspeed;
858 else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
859 CS(this).movement_x = -autocvar_sv_maxspeed;
861 if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
862 CS(this).movement_y = autocvar_sv_maxspeed;
863 else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
864 CS(this).movement_y = -autocvar_sv_maxspeed;
866 if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
867 PHYS_INPUT_BUTTON_JUMP(this) = true;
869 if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
870 PHYS_INPUT_BUTTON_CROUCH(this) = true;
872 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
873 PHYS_INPUT_BUTTON_ATCK(this) = true;
875 if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
876 PHYS_INPUT_BUTTON_ATCK2(this) = true;
878 if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
879 PHYS_INPUT_BUTTON_USE(this) = true;
881 if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
882 PHYS_INPUT_BUTTON_HOOK(this) = true;
884 if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
885 PHYS_INPUT_BUTTON_CHAT(this) = true;
891 float bot_cmd_keypress_handler(entity this, string key, float enabled)
897 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
899 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
903 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
904 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
907 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
912 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
913 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
916 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
921 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
922 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
925 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
930 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
931 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
934 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
938 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
940 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
944 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
946 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
950 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
952 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
956 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
958 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
962 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
964 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
968 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
970 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
974 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
976 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
982 return CMD_STATUS_FINISHED;
985 float bot_cmd_presskey(entity this)
989 key = bot_cmd.bot_cmd_parm_string;
991 bot_cmd_keypress_handler(this, key,true);
993 return CMD_STATUS_FINISHED;
996 float bot_cmd_releasekey(entity this)
1000 key = bot_cmd.bot_cmd_parm_string;
1002 return bot_cmd_keypress_handler(this, key,false);
1005 float bot_cmd_pause(entity this)
1007 PHYS_INPUT_BUTTON_DRAG(this) = false;
1008 PHYS_INPUT_BUTTON_USE(this) = false;
1009 PHYS_INPUT_BUTTON_ATCK(this) = false;
1010 PHYS_INPUT_BUTTON_JUMP(this) = false;
1011 PHYS_INPUT_BUTTON_HOOK(this) = false;
1012 PHYS_INPUT_BUTTON_CHAT(this) = false;
1013 PHYS_INPUT_BUTTON_ATCK2(this) = false;
1014 PHYS_INPUT_BUTTON_CROUCH(this) = false;
1016 CS(this).movement = '0 0 0';
1017 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1020 this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1021 return CMD_STATUS_FINISHED;
1024 float bot_cmd_moveto(entity this)
1026 return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1029 float bot_cmd_movetotarget(entity this)
1032 e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1034 return CMD_STATUS_ERROR;
1035 return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1038 float bot_cmd_resetgoal(entity this)
1040 return this.cmd_resetgoal(this);
1044 float bot_cmd_sound(entity this)
1047 f = bot_cmd.bot_cmd_parm_string;
1049 float n = tokenizebyseparator(f, " ");
1052 float chan = CH_WEAPON_B;
1053 float vol = VOL_BASE;
1054 float atten = ATTEN_MIN;
1057 sample = argv(n - 1);
1059 chan = stof(argv(0));
1061 vol = stof(argv(1));
1063 atten = stof(argv(2));
1066 _sound(this, chan, sample, vol, atten);
1068 return CMD_STATUS_FINISHED;
1072 float bot_cmd_debug_assert_canfire(entity this)
1074 float f = bot_cmd.bot_cmd_parm_float;
1076 int slot = 0; // TODO: unhardcode?
1077 .entity weaponentity = weaponentities[slot];
1078 if(this.(weaponentity).state != WS_READY)
1082 this.colormod = '0 8 8';
1083 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1086 else if(ATTACK_FINISHED(this, weaponentity) > time)
1090 this.colormod = '8 0 8';
1091 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left)");
1094 else if(this.(weaponentity).tuba_note)
1098 this.colormod = '8 0 0';
1099 LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1106 this.colormod = '8 8 0';
1107 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");
1111 return CMD_STATUS_FINISHED;
1116 void bot_command_executed(entity this, bool rm)
1123 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1125 this.bot_cmd_execution_index++;
1128 void bot_setcurrentcommand(entity this)
1132 if(!this.bot_cmd_current)
1134 this.bot_cmd_current = new_pure(bot_cmd);
1137 bot_cmd = this.bot_cmd_current;
1138 if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1140 if(bot_havecommand(this, this.bot_cmd_execution_index))
1143 cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1144 if(bot_decodecommand(cmdstring))
1146 bot_cmd.owner = this;
1147 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1151 // Invalid command, remove from queue
1153 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1154 this.bot_cmd_execution_index++;
1162 void bot_resetqueues()
1164 FOREACH_CLIENT(it.isbot, {
1165 it.bot_cmd_execution_index = 0;
1167 // also, cancel all barriers
1169 for(int i = 0; i < it.bot_places_count; ++i)
1171 strfree(it.(bot_placenames[i]));
1173 it.bot_places_count = 0;
1176 bot_barriertime = time;
1179 // Here we map commands to functions and deal with complex interactions between commands and execution states
1180 // NOTE: Of course you need to include your commands here too :)
1181 float bot_execute_commands_once(entity this)
1183 float status, ispressingkey;
1186 bot_setcurrentcommand(this);
1188 // Ignore all commands except continue when the bot is paused
1189 if(!(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1191 // if we have no bot command, better return
1192 // old logic kept pressing previously pressed keys, but that has problems
1193 // (namely, it means you cannot make a bot "normal" ever again)
1194 // to keep a bot walking for a while, use the "wait" bot command
1195 if(bot_cmd == world)
1198 else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1200 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1202 bot_command_executed(this, true);
1203 LOG_INFO("WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.");
1208 // Keep pressing keys raised by the "presskey" command
1209 ispressingkey = boolean(bot_presskeys(this));
1211 // Handle conditions
1212 if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1213 if((this.bot_cmd_condition_status & CMD_CONDITION_true) && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1215 bot_command_executed(this, true);
1218 else if((this.bot_cmd_condition_status & CMD_CONDITION_false) && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1220 bot_command_executed(this, true);
1224 // Map commands to functions
1225 switch(bot_cmd.bot_cmd_type)
1228 return ispressingkey;
1231 status = bot_cmd_pause(this);
1233 case BOT_CMD_CONTINUE:
1234 status = bot_cmd_continue(this);
1237 status = bot_cmd_wait(this);
1239 case BOT_CMD_WAIT_UNTIL:
1240 status = bot_cmd_wait_until(this);
1243 status = bot_cmd_turn(this);
1245 case BOT_CMD_MOVETO:
1246 status = bot_cmd_moveto(this);
1248 case BOT_CMD_MOVETOTARGET:
1249 status = bot_cmd_movetotarget(this);
1251 case BOT_CMD_RESETGOAL:
1252 status = bot_cmd_resetgoal(this);
1255 status = bot_cmd_cc(this);
1258 status = bot_cmd_if(this);
1261 status = bot_cmd_else(this);
1264 status = bot_cmd_fi(this);
1266 case BOT_CMD_RESETAIM:
1267 status = bot_cmd_resetaim(this);
1270 status = bot_cmd_aim(this);
1272 case BOT_CMD_AIMTARGET:
1273 status = bot_cmd_aimtarget(this);
1275 case BOT_CMD_PRESSKEY:
1276 status = bot_cmd_presskey(this);
1278 case BOT_CMD_RELEASEKEY:
1279 status = bot_cmd_releasekey(this);
1281 case BOT_CMD_SELECTWEAPON:
1282 status = bot_cmd_select_weapon(this);
1284 case BOT_CMD_IMPULSE:
1285 status = bot_cmd_impulse(this);
1287 case BOT_CMD_BARRIER:
1288 status = bot_cmd_barrier(this);
1290 case BOT_CMD_CONSOLE:
1291 localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1292 status = CMD_STATUS_FINISHED;
1295 status = bot_cmd_sound(this);
1297 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1298 status = bot_cmd_debug_assert_canfire(this);
1301 LOG_INFOF("ERROR: Invalid command on queue with id '%s'", ftos(bot_cmd.bot_cmd_type));
1305 if (status==CMD_STATUS_ERROR)
1306 LOG_INFOF("ERROR: The command '%s' returned an error status", bot_cmd_string[bot_cmd.bot_cmd_type]);
1308 // Move execution pointer
1309 if(status==CMD_STATUS_EXECUTING)
1315 if(autocvar_g_debug_bot_commands)
1319 switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1321 case BOT_CMD_PARAMETER_FLOAT:
1322 parms = ftos(bot_cmd.bot_cmd_parm_float);
1324 case BOT_CMD_PARAMETER_STRING:
1325 parms = bot_cmd.bot_cmd_parm_string;
1327 case BOT_CMD_PARAMETER_VECTOR:
1328 parms = vtos(bot_cmd.bot_cmd_parm_vector);
1334 clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1337 bot_command_executed(this, true);
1340 if(status == CMD_STATUS_FINISHED)
1343 return CMD_STATUS_ERROR;
1346 // This function should be (the only) called directly from the bot ai loop
1347 int bot_execute_commands(entity this)
1352 f = bot_execute_commands_once(this);