]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/scripting.qc
pow(a, b) -> a ** b
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / scripting.qc
1 #include "scripting.qh"
2
3 #include "cvars.qh"
4
5 #include <common/state.qh>
6 #include <common/physics/player.qh>
7 #include <common/wepent.qh>
8
9 #include "bot.qh"
10
11 .int state;
12
13 .float bot_cmdqueuebuf_allocated;
14 .float bot_cmdqueuebuf;
15 .float bot_cmdqueuebuf_start;
16 .float bot_cmdqueuebuf_end;
17
18 void bot_clearqueue(entity bot)
19 {
20         if(!bot.bot_cmdqueuebuf_allocated)
21                 return;
22         buf_del(bot.bot_cmdqueuebuf);
23         bot.bot_cmdqueuebuf_allocated = false;
24         LOG_TRACE("bot ", bot.netname, " queue cleared");
25 }
26
27 void bot_queuecommand(entity bot, string cmdstring)
28 {
29         if(!bot.bot_cmdqueuebuf_allocated)
30         {
31                 bot.bot_cmdqueuebuf = buf_create();
32                 bot.bot_cmdqueuebuf_allocated = true;
33                 bot.bot_cmdqueuebuf_start = 0;
34                 bot.bot_cmdqueuebuf_end = 0;
35         }
36
37         bufstr_set(bot.bot_cmdqueuebuf, bot.bot_cmdqueuebuf_end, cmdstring);
38
39         // if the command was a "sound" command, precache the sound NOW
40         // this prevents lagging!
41         {
42                 float sp;
43                 string parm;
44                 string cmdstr;
45
46                 sp = strstrofs(cmdstring, " ", 0);
47                 if(sp >= 0)
48                 {
49                         parm = substring(cmdstring, sp + 1, -1);
50                         cmdstr = substring(cmdstring, 0, sp);
51                         if(cmdstr == "sound")
52                         {
53                                 // find the LAST word
54                                 for (;;)
55                                 {
56                                         sp = strstrofs(parm, " ", 0);
57                                         if(sp < 0)
58                                                 break;
59                                         parm = substring(parm, sp + 1, -1);
60                                 }
61                                 precache_sound(parm);
62                         }
63                 }
64         }
65
66         bot.bot_cmdqueuebuf_end += 1;
67 }
68
69 void bot_dequeuecommand(entity bot, float idx)
70 {
71         if(!bot.bot_cmdqueuebuf_allocated)
72                 error("dequeuecommand but no queue allocated");
73         if(idx < bot.bot_cmdqueuebuf_start)
74                 error("dequeueing a command in the past");
75         if(idx >= bot.bot_cmdqueuebuf_end)
76                 error("dequeueing a command in the future");
77         bufstr_set(bot.bot_cmdqueuebuf, idx, "");
78         if(idx == bot.bot_cmdqueuebuf_start)
79                 bot.bot_cmdqueuebuf_start += 1;
80         if(bot.bot_cmdqueuebuf_start >= bot.bot_cmdqueuebuf_end)
81                 bot_clearqueue(bot);
82 }
83
84 string bot_readcommand(entity bot, float idx)
85 {
86         if(!bot.bot_cmdqueuebuf_allocated)
87                 error("readcommand but no queue allocated");
88         if(idx < bot.bot_cmdqueuebuf_start)
89                 error("reading a command in the past");
90         if(idx >= bot.bot_cmdqueuebuf_end)
91                 error("reading a command in the future");
92         return bufstr_get(bot.bot_cmdqueuebuf, idx);
93 }
94
95 bool bot_havecommand(entity this, int idx)
96 {
97         if(!this.bot_cmdqueuebuf_allocated)
98                 return false;
99         if(idx < this.bot_cmdqueuebuf_start)
100                 return false;
101         if(idx >= this.bot_cmdqueuebuf_end)
102                 return false;
103         return true;
104 }
105
106 const int MAX_BOT_PLACES = 4;
107 .float bot_places_count;
108 .entity bot_places[MAX_BOT_PLACES];
109 .string bot_placenames[MAX_BOT_PLACES];
110 entity bot_getplace(entity this, string placename)
111 {
112         entity e;
113         if(substring(placename, 0, 1) == "@")
114         {
115                 int i, p;
116                 placename = substring(placename, 1, -1);
117                 string s, s2;
118                 for(i = 0; i < this.bot_places_count; ++i)
119                         if(this.(bot_placenames[i]) == placename)
120                                 return this.(bot_places[i]);
121                 // now: i == this.bot_places_count
122                 s = s2 = cvar_string(placename);
123                 p = strstrofs(s2, " ", 0);
124                 if(p >= 0)
125                 {
126                         s = substring(s2, 0, p);
127                         //print("places: ", placename, " -> ", cvar_string(placename), "\n");
128                         cvar_set(placename, strcat(substring(s2, p+1, -1), " ", s));
129                         //print("places: ", placename, " := ", cvar_string(placename), "\n");
130                 }
131                 e = find(NULL, targetname, s);
132                 if(!e)
133                         LOG_INFO("invalid place ", s, "\n");
134                 if(i < MAX_BOT_PLACES)
135                 {
136                         this.(bot_placenames[i]) = strzone(placename);
137                         this.(bot_places[i]) = e;
138                         this.bot_places_count += 1;
139                 }
140                 return e;
141         }
142         else
143         {
144                 e = find(NULL, targetname, placename);
145                 if(!e)
146                         LOG_INFO("invalid place ", placename, "\n");
147                 return e;
148         }
149 }
150
151
152 // Initialize global commands list
153 // NOTE: New commands should be initialized here
154 void bot_commands_init()
155 {
156         bot_cmd_string[BOT_CMD_NULL] = "";
157         bot_cmd_parm_type[BOT_CMD_NULL] = BOT_CMD_PARAMETER_NONE;
158
159         bot_cmd_string[BOT_CMD_PAUSE] = "pause";
160         bot_cmd_parm_type[BOT_CMD_PAUSE] = BOT_CMD_PARAMETER_NONE;
161
162         bot_cmd_string[BOT_CMD_CONTINUE] = "continue";
163         bot_cmd_parm_type[BOT_CMD_CONTINUE] = BOT_CMD_PARAMETER_NONE;
164
165         bot_cmd_string[BOT_CMD_WAIT] = "wait";
166         bot_cmd_parm_type[BOT_CMD_WAIT] = BOT_CMD_PARAMETER_FLOAT;
167
168         bot_cmd_string[BOT_CMD_TURN] = "turn";
169         bot_cmd_parm_type[BOT_CMD_TURN] = BOT_CMD_PARAMETER_FLOAT;
170
171         bot_cmd_string[BOT_CMD_MOVETO] = "moveto";
172         bot_cmd_parm_type[BOT_CMD_MOVETO] = BOT_CMD_PARAMETER_VECTOR;
173
174         bot_cmd_string[BOT_CMD_MOVETOTARGET] = "movetotarget";
175         bot_cmd_parm_type[BOT_CMD_MOVETOTARGET] = BOT_CMD_PARAMETER_STRING;
176
177         bot_cmd_string[BOT_CMD_RESETGOAL] = "resetgoal";
178         bot_cmd_parm_type[BOT_CMD_RESETGOAL] = BOT_CMD_PARAMETER_NONE;
179
180         bot_cmd_string[BOT_CMD_CC] = "cc";
181         bot_cmd_parm_type[BOT_CMD_CC] = BOT_CMD_PARAMETER_STRING;
182
183         bot_cmd_string[BOT_CMD_IF] = "if";
184         bot_cmd_parm_type[BOT_CMD_IF] = BOT_CMD_PARAMETER_STRING;
185
186         bot_cmd_string[BOT_CMD_ELSE] = "else";
187         bot_cmd_parm_type[BOT_CMD_ELSE] = BOT_CMD_PARAMETER_NONE;
188
189         bot_cmd_string[BOT_CMD_FI] = "fi";
190         bot_cmd_parm_type[BOT_CMD_FI] = BOT_CMD_PARAMETER_NONE;
191
192         bot_cmd_string[BOT_CMD_RESETAIM] = "resetaim";
193         bot_cmd_parm_type[BOT_CMD_RESETAIM] = BOT_CMD_PARAMETER_NONE;
194
195         bot_cmd_string[BOT_CMD_AIM] = "aim";
196         bot_cmd_parm_type[BOT_CMD_AIM] = BOT_CMD_PARAMETER_STRING;
197
198         bot_cmd_string[BOT_CMD_AIMTARGET] = "aimtarget";
199         bot_cmd_parm_type[BOT_CMD_AIMTARGET] = BOT_CMD_PARAMETER_STRING;
200
201         bot_cmd_string[BOT_CMD_PRESSKEY] = "presskey";
202         bot_cmd_parm_type[BOT_CMD_PRESSKEY] = BOT_CMD_PARAMETER_STRING;
203
204         bot_cmd_string[BOT_CMD_RELEASEKEY] = "releasekey";
205         bot_cmd_parm_type[BOT_CMD_RELEASEKEY] = BOT_CMD_PARAMETER_STRING;
206
207         bot_cmd_string[BOT_CMD_SELECTWEAPON] = "selectweapon";
208         bot_cmd_parm_type[BOT_CMD_SELECTWEAPON] = BOT_CMD_PARAMETER_FLOAT;
209
210         bot_cmd_string[BOT_CMD_IMPULSE] = "impulse";
211         bot_cmd_parm_type[BOT_CMD_IMPULSE] = BOT_CMD_PARAMETER_FLOAT;
212
213         bot_cmd_string[BOT_CMD_WAIT_UNTIL] = "wait_until";
214         bot_cmd_parm_type[BOT_CMD_WAIT_UNTIL] = BOT_CMD_PARAMETER_FLOAT;
215
216         bot_cmd_string[BOT_CMD_BARRIER] = "barrier";
217         bot_cmd_parm_type[BOT_CMD_BARRIER] = BOT_CMD_PARAMETER_NONE;
218
219         bot_cmd_string[BOT_CMD_CONSOLE] = "console";
220         bot_cmd_parm_type[BOT_CMD_CONSOLE] = BOT_CMD_PARAMETER_STRING;
221
222         bot_cmd_string[BOT_CMD_SOUND] = "sound";
223         bot_cmd_parm_type[BOT_CMD_SOUND] = BOT_CMD_PARAMETER_STRING;
224
225         bot_cmd_string[BOT_CMD_DEBUG_ASSERT_CANFIRE] = "debug_assert_canfire";
226         bot_cmd_parm_type[BOT_CMD_DEBUG_ASSERT_CANFIRE] = BOT_CMD_PARAMETER_FLOAT;
227
228         bot_cmds_initialized = true;
229 }
230
231 // Returns first bot with matching name
232 entity find_bot_by_name(string name)
233 {
234         FOREACH_CLIENT(IS_BOT_CLIENT(it) && it.netname == name,
235         {
236                 return it;
237         });
238
239         return NULL;
240 }
241
242 // Returns a bot by number on list
243 entity find_bot_by_number(float number)
244 {
245         entity bot;
246         float c = 0;
247
248         if(!number)
249                 return NULL;
250
251         bot = findchainflags(flags, FL_CLIENT); // TODO: doesn't findchainflags loop backwards through entities?
252         while (bot)
253         {
254                 if(IS_BOT_CLIENT(bot))
255                 {
256                         if(++c==number)
257                                 return bot;
258                 }
259                 bot = bot.chain;
260         }
261
262         return NULL;
263 }
264
265 float bot_decodecommand(string cmdstring)
266 {
267         float cmd_parm_type;
268         float sp;
269         string parm;
270
271         sp = strstrofs(cmdstring, " ", 0);
272         if(sp < 0)
273         {
274                 parm = "";
275         }
276         else
277         {
278                 parm = substring(cmdstring, sp + 1, -1);
279                 cmdstring = substring(cmdstring, 0, sp);
280         }
281
282         if(!bot_cmds_initialized)
283                 bot_commands_init();
284
285         int i;
286         for(i=1;i<BOT_CMD_COUNTER;++i)
287         {
288                 if(bot_cmd_string[i]!=cmdstring)
289                         continue;
290
291                 cmd_parm_type = bot_cmd_parm_type[i];
292
293                 if(cmd_parm_type!=BOT_CMD_PARAMETER_NONE&&parm=="")
294                 {
295                         LOG_INFO("ERROR: A parameter is required for this command\n");
296                         return 0;
297                 }
298
299                 // Load command into queue
300                 bot_cmd.bot_cmd_type = i;
301
302                 // Attach parameter
303                 switch(cmd_parm_type)
304                 {
305                         case BOT_CMD_PARAMETER_FLOAT:
306                                 bot_cmd.bot_cmd_parm_float = stof(parm);
307                                 break;
308                         case BOT_CMD_PARAMETER_STRING:
309                                 if(bot_cmd.bot_cmd_parm_string)
310                                         strunzone(bot_cmd.bot_cmd_parm_string);
311                                 bot_cmd.bot_cmd_parm_string = strzone(parm);
312                                 break;
313                         case BOT_CMD_PARAMETER_VECTOR:
314                                 if(substring(parm, 0, 1) != "\'")
315                                 {
316                                         LOG_INFOF("ERROR: expected vector type \'x y z\', got %s\n", parm);
317                                         return 0;
318                                 }
319                                 bot_cmd.bot_cmd_parm_vector = stov(parm);
320                                 break;
321                         default:
322                                 break;
323                 }
324                 return 1;
325         }
326         LOG_INFO("ERROR: No such command '", cmdstring, "'\n");
327         return 0;
328 }
329
330 void bot_cmdhelp(string scmd)
331 {
332         int i, ntype;
333         string stype;
334
335         if(!bot_cmds_initialized)
336                 bot_commands_init();
337
338         for(i=1;i<BOT_CMD_COUNTER;++i)
339         {
340                 if(bot_cmd_string[i]!=scmd)
341                         continue;
342
343                 ntype = bot_cmd_parm_type[i];
344
345                 switch(ntype)
346                 {
347                         case BOT_CMD_PARAMETER_FLOAT:
348                                 stype = "float number";
349                                 break;
350                         case BOT_CMD_PARAMETER_STRING:
351                                 stype = "string";
352                                 break;
353                         case BOT_CMD_PARAMETER_VECTOR:
354                                 stype = "vector";
355                                 break;
356                         default:
357                                 stype = "none";
358                                 break;
359                 }
360
361                 LOG_INFO(strcat("Command: ",bot_cmd_string[i],"\nParameter: <",stype,"> \n"));
362
363                 LOG_INFO("Description: ");
364                 switch(i)
365                 {
366                         case BOT_CMD_PAUSE:
367                                 LOG_INFO("Stops the bot completely. Any command other than 'continue' will be ignored.");
368                                 break;
369                         case BOT_CMD_CONTINUE:
370                                 LOG_INFO("Disable paused status");
371                                 break;
372                         case BOT_CMD_WAIT:
373                                 LOG_INFO("Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
374                                 break;
375                         case BOT_CMD_WAIT_UNTIL:
376                                 LOG_INFO("Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
377                                 break;
378                         case BOT_CMD_BARRIER:
379                                 LOG_INFO("Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
380                                 break;
381                         case BOT_CMD_TURN:
382                                 LOG_INFO("Look to the right or left N degrees. For turning to the left use positive numbers.");
383                                 break;
384                         case BOT_CMD_MOVETO:
385                                 LOG_INFO("Walk to an specific coordinate on the map. Usage: moveto \'x y z\'");
386                                 break;
387                         case BOT_CMD_MOVETOTARGET:
388                                 LOG_INFO("Walk to the specific target on the map");
389                                 break;
390                         case BOT_CMD_RESETGOAL:
391                                 LOG_INFO("Resets the goal stack");
392                                 break;
393                         case BOT_CMD_CC:
394                                 LOG_INFO("Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
395                                 break;
396                         case BOT_CMD_IF:
397                                 LOG_INFO("Perform simple conditional execution.\n");
398                                 LOG_INFO("Syntax: \n");
399                                 LOG_INFO("        sv_cmd .. if \"condition\"\n");
400                                 LOG_INFO("        sv_cmd ..     <instruction if true>\n");
401                                 LOG_INFO("        sv_cmd ..     <instruction if true>\n");
402                                 LOG_INFO("        sv_cmd .. else\n");
403                                 LOG_INFO("        sv_cmd ..     <instruction if false>\n");
404                                 LOG_INFO("        sv_cmd ..     <instruction if false>\n");
405                                 LOG_INFO("        sv_cmd .. fi\n");
406                                 LOG_INFO("Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n");
407                                 LOG_INFO("            Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n");
408                                 LOG_INFO("Fields: health, speed, flagcarrier\n");
409                                 LOG_INFO("Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;");
410                                 break;
411                         case BOT_CMD_RESETAIM:
412                                 LOG_INFO("Points the aim to the coordinates x,y 0,0");
413                                 break;
414                         case BOT_CMD_AIM:
415                                 LOG_INFO("Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n");
416                                 LOG_INFO("There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n");
417                                 LOG_INFO("Examples: aim \"90 0\"        // Turn 90 degrees inmediately (positive numbers move to the left/up)\n");
418                                 LOG_INFO("          aim \"0 90 2\"      // Will gradually look to the sky in the next two seconds");
419                                 break;
420                         case BOT_CMD_AIMTARGET:
421                                 LOG_INFO("Points the aim to given target");
422                                 break;
423                         case BOT_CMD_PRESSKEY:
424                                 LOG_INFO("Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use\n");
425                                 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
426                                 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
427                                 break;
428                         case BOT_CMD_RELEASEKEY:
429                                 LOG_INFO("Release previoulsy used keys. Use the parameter \"all\" to release all keys");
430                                 break;
431                         case BOT_CMD_SOUND:
432                                 LOG_INFO("play sound file at bot location");
433                                 break;
434                         case BOT_CMD_DEBUG_ASSERT_CANFIRE:
435                                 LOG_INFO("verify the state of the weapon entity");
436                                 break;
437                         default:
438                                 LOG_INFO("This command has no description yet.");
439                                 break;
440                 }
441                 LOG_INFO("\n");
442         }
443 }
444
445 void bot_list_commands()
446 {
447         int i;
448         string ptype;
449
450         if(!bot_cmds_initialized)
451                 bot_commands_init();
452
453         LOG_INFO("List of all available commands:\n");
454         LOG_INFO("  Command - Parameter Type\n");
455
456         for(i=1;i<BOT_CMD_COUNTER;++i)
457         {
458                 switch(bot_cmd_parm_type[i])
459                 {
460                         case BOT_CMD_PARAMETER_FLOAT:
461                                 ptype = "float number";
462                                 break;
463                         case BOT_CMD_PARAMETER_STRING:
464                                 ptype = "string";
465                                 break;
466                         case BOT_CMD_PARAMETER_VECTOR:
467                                 ptype = "vector";
468                                 break;
469                         default:
470                                 ptype = "none";
471                                 break;
472                 }
473                 LOG_INFO(strcat("  ",bot_cmd_string[i]," - <",ptype,"> \n"));
474         }
475 }
476
477 // Commands code
478 .int bot_exec_status;
479
480 float bot_cmd_cc(entity this)
481 {
482         SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
483         return CMD_STATUS_FINISHED;
484 }
485
486 float bot_cmd_impulse(entity this)
487 {
488         this.impulse = bot_cmd.bot_cmd_parm_float;
489         return CMD_STATUS_FINISHED;
490 }
491
492 float bot_cmd_continue(entity this)
493 {
494         bot_relinkplayerlist();
495         this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
496         return CMD_STATUS_FINISHED;
497 }
498
499 .float bot_cmd_wait_time;
500 float bot_cmd_wait(entity this)
501 {
502         if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
503         {
504                 if(time>=this.bot_cmd_wait_time)
505                 {
506                         this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
507                         return CMD_STATUS_FINISHED;
508                 }
509                 else
510                         return CMD_STATUS_EXECUTING;
511         }
512
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;
516 }
517
518 float bot_cmd_wait_until(entity this)
519 {
520         if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
521         {
522                 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
523                 return CMD_STATUS_EXECUTING;
524         }
525         this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
526         return CMD_STATUS_FINISHED;
527 }
528
529 float bot_cmd_barrier(entity this)
530 {
531         // 0 = no barrier, 1 = waiting, 2 = waiting finished
532
533         if(this.bot_barrier == 0) // initialization
534         {
535                 this.bot_barrier = 1;
536
537                 //this.colormod = '4 4 0';
538         }
539
540         if(this.bot_barrier == 1) // find other bots
541         {
542                 FOREACH_CLIENT(it.isbot, LAMBDA(
543                         if(it.bot_cmdqueuebuf_allocated)
544                         if(it.bot_barrier != 1)
545                                 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
546                 ));
547
548                 // all bots hit the barrier!
549
550                 // acknowledge barrier
551                 FOREACH_CLIENT(it.isbot, LAMBDA(it.bot_barrier = 2));
552
553                 bot_barriertime = time;
554         }
555
556         // if we get here, the barrier is finished
557         // so end it...
558         this.bot_barrier = 0;
559         //this.colormod = '0 0 0';
560
561         return CMD_STATUS_FINISHED;
562 }
563
564 float bot_cmd_turn(entity this)
565 {
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;
569 }
570
571 float bot_cmd_select_weapon(entity this)
572 {
573         float id = bot_cmd.bot_cmd_parm_float;
574
575         if(id < WEP_FIRST || id > WEP_LAST)
576                 return CMD_STATUS_ERROR;
577
578         bool success = false;
579
580         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
581         {
582                 .entity weaponentity = weaponentities[slot];
583                 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
584                         continue;
585
586                 if(client_hasweapon(this, Weapons_from(id), weaponentity, true, false))
587                 {
588                         success = true;
589                         this.(weaponentity).m_switchweapon = Weapons_from(id);
590                 }
591         }
592
593         if(!success)
594                 return CMD_STATUS_ERROR;
595
596         return CMD_STATUS_FINISHED;
597 }
598
599 .int bot_cmd_condition_status;
600
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;
606
607 float bot_cmd_eval(entity this, string expr)
608 {
609         // Search for numbers
610         if(IS_DIGIT(substring(expr, 0, 1)))
611                 return stof(expr);
612
613         // Search for cvars
614         if(substring(expr, 0, 5)=="cvar.")
615                 return cvar(substring(expr, 5, strlen(expr)));
616
617         // Search for fields
618         switch(expr)
619         {
620                 case "health":
621                         return this.health;
622                 case "speed":
623                         return vlen(this.velocity);
624                 case "flagcarrier":
625                         return ((this.flagcarried!=NULL));
626         }
627
628         LOG_INFO(strcat("ERROR: Unable to convert the expression '",expr,"' into a numeric value\n"));
629         return 0;
630 }
631
632 float bot_cmd_if(entity this)
633 {
634         string expr, val_a, val_b;
635         float cmpofs;
636
637         if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
638         {
639                 // Only one "if" block is allowed at time
640                 LOG_INFO("ERROR: Only one conditional block can be processed at time");
641                 bot_clearqueue(this);
642                 return CMD_STATUS_ERROR;
643         }
644
645         this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
646
647         // search for operators
648         expr = bot_cmd.bot_cmd_parm_string;
649
650         cmpofs = strstrofs(expr,"=",0);
651
652         if(cmpofs>0)
653         {
654                 val_a = substring(expr,0,cmpofs);
655                 val_b = substring(expr,cmpofs+1,strlen(expr));
656
657                 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
658                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
659                 else
660                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
661
662                 return CMD_STATUS_FINISHED;
663         }
664
665         cmpofs = strstrofs(expr,">",0);
666
667         if(cmpofs>0)
668         {
669                 val_a = substring(expr,0,cmpofs);
670                 val_b = substring(expr,cmpofs+1,strlen(expr));
671
672                 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
673                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
674                 else
675                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
676
677                 return CMD_STATUS_FINISHED;
678         }
679
680         cmpofs = strstrofs(expr,"<",0);
681
682         if(cmpofs>0)
683         {
684                 val_a = substring(expr,0,cmpofs);
685                 val_b = substring(expr,cmpofs+1,strlen(expr));
686
687                 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
688                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
689                 else
690                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
691
692                 return CMD_STATUS_FINISHED;
693         }
694
695         if(bot_cmd_eval(this, expr))
696                 this.bot_cmd_condition_status |= CMD_CONDITION_true;
697         else
698                 this.bot_cmd_condition_status |= CMD_CONDITION_false;
699
700         return CMD_STATUS_FINISHED;
701 }
702
703 float bot_cmd_else(entity this)
704 {
705         this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
706         this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
707         return CMD_STATUS_FINISHED;
708 }
709
710 float bot_cmd_fi(entity this)
711 {
712         this.bot_cmd_condition_status = CMD_CONDITION_NONE;
713         return CMD_STATUS_FINISHED;
714 }
715
716 float bot_cmd_resetaim(entity this)
717 {
718         this.v_angle = '0 0 0';
719         return CMD_STATUS_FINISHED;
720 }
721
722 .float bot_cmd_aim_begintime;
723 .float bot_cmd_aim_endtime;
724 .vector bot_cmd_aim_begin;
725 .vector bot_cmd_aim_end;
726
727 float bot_cmd_aim(entity this)
728 {
729         // Current direction
730         if(this.bot_cmd_aim_endtime)
731         {
732                 float progress;
733
734                 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
735                 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
736
737                 if(time>=this.bot_cmd_aim_endtime)
738                 {
739                         this.bot_cmd_aim_endtime = 0;
740                         return CMD_STATUS_FINISHED;
741                 }
742                 else
743                         return CMD_STATUS_EXECUTING;
744         }
745
746         // New aiming direction
747         string parms;
748         float tokens, step;
749
750         parms = bot_cmd.bot_cmd_parm_string;
751
752         tokens = tokenizebyseparator(parms, " ");
753
754         if(tokens<2||tokens>3)
755                 return CMD_STATUS_ERROR;
756
757         step = (tokens == 3) ? stof(argv(2)) : 0;
758
759         if(step == 0)
760         {
761                 this.v_angle_x -= stof(argv(1));
762                 this.v_angle_y += stof(argv(0));
763                 return CMD_STATUS_FINISHED;
764         }
765
766         this.bot_cmd_aim_begin = this.v_angle;
767
768         this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
769         this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
770         this.bot_cmd_aim_end_z = 0;
771
772         this.bot_cmd_aim_begintime = time;
773         this.bot_cmd_aim_endtime = time + step;
774
775         return CMD_STATUS_EXECUTING;
776 }
777
778 float bot_cmd_aimtarget(entity this)
779 {
780         if(this.bot_cmd_aim_endtime)
781         {
782                 return bot_cmd_aim(this);
783         }
784
785         entity e;
786         string parms;
787         vector v;
788         float tokens, step;
789
790         parms = bot_cmd.bot_cmd_parm_string;
791
792         tokens = tokenizebyseparator(parms, " ");
793
794         e = bot_getplace(this, argv(0));
795         if(!e)
796                 return CMD_STATUS_ERROR;
797
798         v = e.origin + (e.mins + e.maxs) * 0.5;
799
800         if(tokens==1)
801         {
802                 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
803                 this.v_angle_x = -this.v_angle.x;
804                 return CMD_STATUS_FINISHED;
805         }
806
807         if(tokens<1||tokens>2)
808                 return CMD_STATUS_ERROR;
809
810         step = stof(argv(1));
811
812         this.bot_cmd_aim_begin = this.v_angle;
813         this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
814         this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
815
816         this.bot_cmd_aim_begintime = time;
817         this.bot_cmd_aim_endtime = time + step;
818
819         return CMD_STATUS_EXECUTING;
820 }
821
822 .int bot_cmd_keys;
823
824 const int BOT_CMD_KEY_NONE              = 0;
825 const int BOT_CMD_KEY_FORWARD   = BIT(0);
826 const int BOT_CMD_KEY_BACKWARD  = BIT(1);
827 const int BOT_CMD_KEY_RIGHT     = BIT(2);
828 const int BOT_CMD_KEY_LEFT              = BIT(3);
829 const int BOT_CMD_KEY_JUMP              = BIT(4);
830 const int BOT_CMD_KEY_ATTACK1   = BIT(5);
831 const int BOT_CMD_KEY_ATTACK2   = BIT(6);
832 const int BOT_CMD_KEY_USE               = BIT(7);
833 const int BOT_CMD_KEY_HOOK              = BIT(8);
834 const int BOT_CMD_KEY_CROUCH    = BIT(9);
835 const int BOT_CMD_KEY_CHAT              = BIT(10);
836
837 bool bot_presskeys(entity this)
838 {
839         this.movement = '0 0 0';
840         PHYS_INPUT_BUTTON_JUMP(this) = false;
841         PHYS_INPUT_BUTTON_CROUCH(this) = false;
842         PHYS_INPUT_BUTTON_ATCK(this) = false;
843         PHYS_INPUT_BUTTON_ATCK2(this) = false;
844         PHYS_INPUT_BUTTON_USE(this) = false;
845         PHYS_INPUT_BUTTON_HOOK(this) = false;
846         PHYS_INPUT_BUTTON_CHAT(this) = false;
847
848         if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
849                 return false;
850
851         if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
852                 this.movement_x = autocvar_sv_maxspeed;
853         else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
854                 this.movement_x = -autocvar_sv_maxspeed;
855
856         if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
857                 this.movement_y = autocvar_sv_maxspeed;
858         else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
859                 this.movement_y = -autocvar_sv_maxspeed;
860
861         if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
862                 PHYS_INPUT_BUTTON_JUMP(this) = true;
863
864         if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
865                 PHYS_INPUT_BUTTON_CROUCH(this) = true;
866
867         if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
868                 PHYS_INPUT_BUTTON_ATCK(this) = true;
869
870         if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
871                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
872
873         if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
874                 PHYS_INPUT_BUTTON_USE(this) = true;
875
876         if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
877                 PHYS_INPUT_BUTTON_HOOK(this) = true;
878
879         if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
880                 PHYS_INPUT_BUTTON_CHAT(this) = true;
881
882         return true;
883 }
884
885
886 float bot_cmd_keypress_handler(entity this, string key, float enabled)
887 {
888         switch(key)
889         {
890                 case "all":
891                         if(enabled)
892                                 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
893                         else
894                                 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
895                 case "forward":
896                         if(enabled)
897                         {
898                                 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
899                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
900                         }
901                         else
902                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
903                         break;
904                 case "backward":
905                         if(enabled)
906                         {
907                                 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
908                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
909                         }
910                         else
911                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
912                         break;
913                 case "left":
914                         if(enabled)
915                         {
916                                 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
917                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
918                         }
919                         else
920                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
921                         break;
922                 case "right":
923                         if(enabled)
924                         {
925                                 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
926                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
927                         }
928                         else
929                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
930                         break;
931                 case "jump":
932                         if(enabled)
933                                 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
934                         else
935                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
936                         break;
937                 case "crouch":
938                         if(enabled)
939                                 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
940                         else
941                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
942                         break;
943                 case "attack1":
944                         if(enabled)
945                                 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
946                         else
947                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
948                         break;
949                 case "attack2":
950                         if(enabled)
951                                 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
952                         else
953                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
954                         break;
955                 case "use":
956                         if(enabled)
957                                 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
958                         else
959                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
960                         break;
961                 case "hook":
962                         if(enabled)
963                                 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
964                         else
965                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
966                         break;
967                 case "chat":
968                         if(enabled)
969                                 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
970                         else
971                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
972                         break;
973                 default:
974                         break;
975         }
976
977         return CMD_STATUS_FINISHED;
978 }
979
980 float bot_cmd_presskey(entity this)
981 {
982         string key;
983
984         key = bot_cmd.bot_cmd_parm_string;
985
986         bot_cmd_keypress_handler(this, key,true);
987
988         return CMD_STATUS_FINISHED;
989 }
990
991 float bot_cmd_releasekey(entity this)
992 {
993         string key;
994
995         key = bot_cmd.bot_cmd_parm_string;
996
997         return bot_cmd_keypress_handler(this, key,false);
998 }
999
1000 float bot_cmd_pause(entity this)
1001 {
1002         PHYS_INPUT_BUTTON_DRAG(this) = false;
1003         PHYS_INPUT_BUTTON_USE(this) = false;
1004         PHYS_INPUT_BUTTON_ATCK(this) = false;
1005         PHYS_INPUT_BUTTON_JUMP(this) = false;
1006         PHYS_INPUT_BUTTON_HOOK(this) = false;
1007         PHYS_INPUT_BUTTON_CHAT(this) = false;
1008         PHYS_INPUT_BUTTON_ATCK2(this) = false;
1009         PHYS_INPUT_BUTTON_CROUCH(this) = false;
1010
1011         this.movement = '0 0 0';
1012         this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1013
1014         bot_clear(this);
1015         this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1016         return CMD_STATUS_FINISHED;
1017 }
1018
1019 float bot_cmd_moveto(entity this)
1020 {
1021         return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1022 }
1023
1024 float bot_cmd_movetotarget(entity this)
1025 {
1026         entity e;
1027         e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1028         if(!e)
1029                 return CMD_STATUS_ERROR;
1030         return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1031 }
1032
1033 float bot_cmd_resetgoal(entity this)
1034 {
1035         return this.cmd_resetgoal(this);
1036 }
1037
1038
1039 float bot_cmd_sound(entity this)
1040 {
1041         string f;
1042         f = bot_cmd.bot_cmd_parm_string;
1043
1044         float n = tokenizebyseparator(f, " ");
1045
1046         string sample = f;
1047         float chan = CH_WEAPON_B;
1048         float vol = VOL_BASE;
1049         float atten = ATTEN_MIN;
1050
1051         if(n >= 1)
1052                 sample = argv(n - 1);
1053         if(n >= 2)
1054                 chan = stof(argv(0));
1055         if(n >= 3)
1056                 vol = stof(argv(1));
1057         if(n >= 4)
1058                 atten = stof(argv(2));
1059
1060         precache_sound(f);
1061         _sound(this, chan, sample, vol, atten);
1062
1063         return CMD_STATUS_FINISHED;
1064 }
1065
1066 .entity tuba_note;
1067 float bot_cmd_debug_assert_canfire(entity this)
1068 {
1069         float f = bot_cmd.bot_cmd_parm_float;
1070
1071         int slot = 0; // TODO: unhardcode?
1072         .entity weaponentity = weaponentities[slot];
1073         if(this.(weaponentity).state != WS_READY)
1074         {
1075                 if(f)
1076                 {
1077                         this.colormod = '0 8 8';
1078                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state\n");
1079                 }
1080         }
1081         else if(ATTACK_FINISHED(this, slot) > time)
1082         {
1083                 if(f)
1084                 {
1085                         this.colormod = '8 0 8';
1086                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left)\n");
1087                 }
1088         }
1089         else if(this.(weaponentity).tuba_note)
1090         {
1091                 if(f)
1092                 {
1093                         this.colormod = '8 0 0';
1094                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note\n");
1095                 }
1096         }
1097         else
1098         {
1099                 if(!f)
1100                 {
1101                         this.colormod = '8 8 0';
1102                         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\n");
1103                 }
1104         }
1105
1106         return CMD_STATUS_FINISHED;
1107 }
1108
1109 //
1110
1111 void bot_command_executed(entity this, bool rm)
1112 {
1113         entity cmd;
1114
1115         cmd = bot_cmd;
1116
1117         if(rm)
1118                 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1119
1120         this.bot_cmd_execution_index++;
1121 }
1122
1123 void bot_setcurrentcommand(entity this)
1124 {
1125         bot_cmd = NULL;
1126
1127         if(!this.bot_cmd_current)
1128         {
1129                 this.bot_cmd_current = new_pure(bot_cmd);
1130         }
1131
1132         bot_cmd = this.bot_cmd_current;
1133         if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1134         {
1135                 if(bot_havecommand(this, this.bot_cmd_execution_index))
1136                 {
1137                         string cmdstring;
1138                         cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1139                         if(bot_decodecommand(cmdstring))
1140                         {
1141                                 bot_cmd.owner = this;
1142                                 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1143                         }
1144                         else
1145                         {
1146                                 // Invalid command, remove from queue
1147                                 bot_cmd = NULL;
1148                                 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1149                                 this.bot_cmd_execution_index++;
1150                         }
1151                 }
1152                 else
1153                         bot_cmd = NULL;
1154         }
1155 }
1156
1157 void bot_resetqueues()
1158 {
1159         FOREACH_CLIENT(it.isbot, LAMBDA(
1160                 it.bot_cmd_execution_index = 0;
1161                 bot_clearqueue(it);
1162                 // also, cancel all barriers
1163                 it.bot_barrier = 0;
1164                 for(int i = 0; i < it.bot_places_count; ++i)
1165                 {
1166                         strunzone(it.(bot_placenames[i]));
1167                         it.(bot_placenames[i]) = string_null;
1168                 }
1169                 it.bot_places_count = 0;
1170         ));
1171
1172         bot_barriertime = time;
1173 }
1174
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)
1178 {
1179         float status, ispressingkey;
1180
1181         // Find command
1182         bot_setcurrentcommand(this);
1183
1184         // Ignore all commands except continue when the bot is paused
1185         if(!(self.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1186         {
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)
1192                         return 0;
1193         }
1194         else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1195         {
1196                 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1197                 {
1198                         bot_command_executed(this, true);
1199                         LOG_INFO( "WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.\n");
1200                 }
1201                 return 1;
1202         }
1203
1204         // Keep pressing keys raised by the "presskey" command
1205         ispressingkey = boolean(bot_presskeys(this));
1206
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)
1210         {
1211                 bot_command_executed(this, true);
1212                 return -1;
1213         }
1214         else if(this.bot_cmd_condition_status & CMD_CONDITION_false && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1215         {
1216                 bot_command_executed(this, true);
1217                 return -1;
1218         }
1219
1220         // Map commands to functions
1221         switch(bot_cmd.bot_cmd_type)
1222         {
1223                 case BOT_CMD_NULL:
1224                         return ispressingkey;
1225                         //break;
1226                 case BOT_CMD_PAUSE:
1227                         status = bot_cmd_pause(this);
1228                         break;
1229                 case BOT_CMD_CONTINUE:
1230                         status = bot_cmd_continue(this);
1231                         break;
1232                 case BOT_CMD_WAIT:
1233                         status = bot_cmd_wait(this);
1234                         break;
1235                 case BOT_CMD_WAIT_UNTIL:
1236                         status = bot_cmd_wait_until(this);
1237                         break;
1238                 case BOT_CMD_TURN:
1239                         status = bot_cmd_turn(this);
1240                         break;
1241                 case BOT_CMD_MOVETO:
1242                         status = bot_cmd_moveto(this);
1243                         break;
1244                 case BOT_CMD_MOVETOTARGET:
1245                         status = bot_cmd_movetotarget(this);
1246                         break;
1247                 case BOT_CMD_RESETGOAL:
1248                         status = bot_cmd_resetgoal(this);
1249                         break;
1250                 case BOT_CMD_CC:
1251                         status = bot_cmd_cc(this);
1252                         break;
1253                 case BOT_CMD_IF:
1254                         status = bot_cmd_if(this);
1255                         break;
1256                 case BOT_CMD_ELSE:
1257                         status = bot_cmd_else(this);
1258                         break;
1259                 case BOT_CMD_FI:
1260                         status = bot_cmd_fi(this);
1261                         break;
1262                 case BOT_CMD_RESETAIM:
1263                         status = bot_cmd_resetaim(this);
1264                         break;
1265                 case BOT_CMD_AIM:
1266                         status = bot_cmd_aim(this);
1267                         break;
1268                 case BOT_CMD_AIMTARGET:
1269                         status = bot_cmd_aimtarget(this);
1270                         break;
1271                 case BOT_CMD_PRESSKEY:
1272                         status = bot_cmd_presskey(this);
1273                         break;
1274                 case BOT_CMD_RELEASEKEY:
1275                         status = bot_cmd_releasekey(this);
1276                         break;
1277                 case BOT_CMD_SELECTWEAPON:
1278                         status = bot_cmd_select_weapon(this);
1279                         break;
1280                 case BOT_CMD_IMPULSE:
1281                         status = bot_cmd_impulse(this);
1282                         break;
1283                 case BOT_CMD_BARRIER:
1284                         status = bot_cmd_barrier(this);
1285                         break;
1286                 case BOT_CMD_CONSOLE:
1287                         localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1288                         status = CMD_STATUS_FINISHED;
1289                         break;
1290                 case BOT_CMD_SOUND:
1291                         status = bot_cmd_sound(this);
1292                         break;
1293                 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1294                         status = bot_cmd_debug_assert_canfire(this);
1295                         break;
1296                 default:
1297                         LOG_INFO(strcat("ERROR: Invalid command on queue with id '",ftos(bot_cmd.bot_cmd_type),"'\n"));
1298                         return 0;
1299         }
1300
1301         if (status==CMD_STATUS_ERROR)
1302                 LOG_INFO(strcat("ERROR: The command '",bot_cmd_string[bot_cmd.bot_cmd_type],"' returned an error status\n"));
1303
1304         // Move execution pointer
1305         if(status==CMD_STATUS_EXECUTING)
1306         {
1307                 return 1;
1308         }
1309         else
1310         {
1311                 if(autocvar_g_debug_bot_commands)
1312                 {
1313                         string parms;
1314
1315                         switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1316                         {
1317                                 case BOT_CMD_PARAMETER_FLOAT:
1318                                         parms = ftos(bot_cmd.bot_cmd_parm_float);
1319                                         break;
1320                                 case BOT_CMD_PARAMETER_STRING:
1321                                         parms = bot_cmd.bot_cmd_parm_string;
1322                                         break;
1323                                 case BOT_CMD_PARAMETER_VECTOR:
1324                                         parms = vtos(bot_cmd.bot_cmd_parm_vector);
1325                                         break;
1326                                 default:
1327                                         parms = "";
1328                                         break;
1329                         }
1330                         clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1331                 }
1332
1333                 bot_command_executed(this, true);
1334         }
1335
1336         if(status == CMD_STATUS_FINISHED)
1337                 return -1;
1338
1339         return CMD_STATUS_ERROR;
1340 }
1341
1342 // This function should be (the only) called directly from the bot ai loop
1343 int bot_execute_commands(entity this)
1344 {
1345         int f;
1346         do
1347         {
1348                 f = bot_execute_commands_once(this);
1349         }
1350         while(f < 0);
1351         return f;
1352 }