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