]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/scripting.qc
Merge branch 'sev/luma-menu-xpm' into 'master'
[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                                 strcpy(bot_cmd.bot_cmd_parm_string, 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", 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, "'");
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                 string prelude = strcat(
362                     "Command: ", bot_cmd_string[i], "\n",
363                     "Parameter: <", stype, ">", "\n",
364                     "Description: "
365         );
366                 switch(i)
367                 {
368                         case BOT_CMD_PAUSE:
369                                 LOG_INFO(prelude, "Stops the bot completely. Any command other than 'continue' will be ignored.");
370                                 break;
371                         case BOT_CMD_CONTINUE:
372                                 LOG_INFO(prelude, "Disable paused status");
373                                 break;
374                         case BOT_CMD_WAIT:
375                                 LOG_INFO(prelude, "Pause command parsing and bot ai for N seconds. Pressed key will remain pressed");
376                                 break;
377                         case BOT_CMD_WAIT_UNTIL:
378                                 LOG_INFO(prelude, "Pause command parsing and bot ai until time is N from the last barrier. Pressed key will remain pressed");
379                                 break;
380                         case BOT_CMD_BARRIER:
381                                 LOG_INFO(prelude, "Waits till all bots that have a command queue reach this command. Pressed key will remain pressed");
382                                 break;
383                         case BOT_CMD_TURN:
384                                 LOG_INFO(prelude, "Look to the right or left N degrees. For turning to the left use positive numbers.");
385                                 break;
386                         case BOT_CMD_MOVETO:
387                                 LOG_INFO(prelude, "Walk to an specific coordinate on the map. Usage: moveto \'x y z\'");
388                                 break;
389                         case BOT_CMD_MOVETOTARGET:
390                                 LOG_INFO(prelude, "Walk to the specific target on the map");
391                                 break;
392                         case BOT_CMD_RESETGOAL:
393                                 LOG_INFO(prelude, "Resets the goal stack");
394                                 break;
395                         case BOT_CMD_CC:
396                                 LOG_INFO(prelude, "Execute client command. Examples: cc \"say something\"; cc god; cc \"name newnickname\"; cc kill;");
397                                 break;
398                         case BOT_CMD_IF:
399                                 LOG_INFO(prelude, "Perform simple conditional execution.\n"
400                     "Syntax: \n"
401                     "        sv_cmd .. if \"condition\"\n"
402                     "        sv_cmd ..  <instruction if true>\n"
403                     "        sv_cmd ..  <instruction if true>\n"
404                     "        sv_cmd .. else\n"
405                     "        sv_cmd ..  <instruction if false>\n"
406                     "        sv_cmd ..  <instruction if false>\n"
407                     "        sv_cmd .. fi\n"
408                     "Conditions: a=b, a>b, a<b, a\t\t(spaces not allowed)\n"
409                     "            Values in conditions can be numbers, cvars in the form cvar.cvar_string or special fields\n"
410                     "Fields: health, speed, flagcarrier\n"
411                     "Examples: if health>50; if health>cvar.g_balance_laser_primary_damage; if flagcarrier;"
412                                 );
413                                 break;
414                         case BOT_CMD_RESETAIM:
415                                 LOG_INFO(prelude, "Points the aim to the coordinates x,y 0,0");
416                                 break;
417                         case BOT_CMD_AIM:
418                                 LOG_INFO(prelude, "Move the aim x/y (horizontal/vertical) degrees relatives to the bot\n"
419                                         "There is a 3rd optional parameter telling in how many seconds the aim has to reach the new position\n"
420                                         "Examples: aim \"90 0\" // Turn 90 degrees inmediately (positive numbers move to the left/up)\n"
421                                         "          aim \"0 90 2\"       // Will gradually look to the sky in the next two seconds"
422                                 );
423                                 break;
424                         case BOT_CMD_AIMTARGET:
425                                 LOG_INFO(prelude, "Points the aim to given target");
426                                 break;
427                         case BOT_CMD_PRESSKEY:
428                                 LOG_INFO(prelude, "Press one of the following keys: forward, backward, left, right, jump, crouch, attack1, attack2, use");
429                                 LOG_INFO("Multiple keys can be pressed at time (with many presskey calls) and it will remain pressed until the command \"releasekey\" is called");
430                                 LOG_INFO("Note: The script will not return the control to the bot ai until all keys are released");
431                                 break;
432                         case BOT_CMD_RELEASEKEY:
433                                 LOG_INFO(prelude, "Release previoulsy used keys. Use the parameter \"all\" to release all keys");
434                                 break;
435                         case BOT_CMD_SOUND:
436                                 LOG_INFO(prelude, "play sound file at bot location");
437                                 break;
438                         case BOT_CMD_DEBUG_ASSERT_CANFIRE:
439                                 LOG_INFO(prelude, "verify the state of the weapon entity");
440                                 break;
441                         default:
442                                 LOG_INFO(prelude, "This command has no description yet.");
443                                 break;
444                 }
445         }
446 }
447
448 void bot_list_commands()
449 {
450         int i;
451         string ptype;
452
453         if(!bot_cmds_initialized)
454                 bot_commands_init();
455
456         LOG_INFO(
457             "List of all available commands:\n"
458             "  Command - Parameter Type\n"
459     );
460
461         for(i=1;i<BOT_CMD_COUNTER;++i)
462         {
463                 switch(bot_cmd_parm_type[i])
464                 {
465                         case BOT_CMD_PARAMETER_FLOAT:
466                                 ptype = "float number";
467                                 break;
468                         case BOT_CMD_PARAMETER_STRING:
469                                 ptype = "string";
470                                 break;
471                         case BOT_CMD_PARAMETER_VECTOR:
472                                 ptype = "vector";
473                                 break;
474                         default:
475                                 ptype = "none";
476                                 break;
477                 }
478                 LOG_INFO("  ", bot_cmd_string[i]," - <", ptype, ">");
479         }
480 }
481
482 // Commands code
483 .int bot_exec_status;
484
485 float bot_cmd_cc(entity this)
486 {
487         SV_ParseClientCommand(this, bot_cmd.bot_cmd_parm_string);
488         return CMD_STATUS_FINISHED;
489 }
490
491 float bot_cmd_impulse(entity this)
492 {
493         CS(this).impulse = bot_cmd.bot_cmd_parm_float;
494         return CMD_STATUS_FINISHED;
495 }
496
497 float bot_cmd_continue(entity this)
498 {
499         bot_relinkplayerlist();
500         this.bot_exec_status &= ~BOT_EXEC_STATUS_PAUSED;
501         return CMD_STATUS_FINISHED;
502 }
503
504 .float bot_cmd_wait_time;
505 float bot_cmd_wait(entity this)
506 {
507         if(this.bot_exec_status & BOT_EXEC_STATUS_WAITING)
508         {
509                 if(time>=this.bot_cmd_wait_time)
510                 {
511                         this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
512                         return CMD_STATUS_FINISHED;
513                 }
514                 else
515                         return CMD_STATUS_EXECUTING;
516         }
517
518         this.bot_cmd_wait_time = time + bot_cmd.bot_cmd_parm_float;
519         this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
520         return CMD_STATUS_EXECUTING;
521 }
522
523 float bot_cmd_wait_until(entity this)
524 {
525         if(time < bot_cmd.bot_cmd_parm_float + bot_barriertime)
526         {
527                 this.bot_exec_status |= BOT_EXEC_STATUS_WAITING;
528                 return CMD_STATUS_EXECUTING;
529         }
530         this.bot_exec_status &= ~BOT_EXEC_STATUS_WAITING;
531         return CMD_STATUS_FINISHED;
532 }
533
534 float bot_cmd_barrier(entity this)
535 {
536         // 0 = no barrier, 1 = waiting, 2 = waiting finished
537
538         if(this.bot_barrier == 0) // initialization
539         {
540                 this.bot_barrier = 1;
541
542                 //this.colormod = '4 4 0';
543         }
544
545         if(this.bot_barrier == 1) // find other bots
546         {
547                 FOREACH_CLIENT(it.isbot, {
548                         if(it.bot_cmdqueuebuf_allocated)
549                         if(it.bot_barrier != 1)
550                                 return CMD_STATUS_EXECUTING; // not all are at the barrier yet
551                 });
552
553                 // all bots hit the barrier!
554
555                 // acknowledge barrier
556                 FOREACH_CLIENT(it.isbot, { it.bot_barrier = 2; });
557
558                 bot_barriertime = time;
559         }
560
561         // if we get here, the barrier is finished
562         // so end it...
563         this.bot_barrier = 0;
564         //this.colormod = '0 0 0';
565
566         return CMD_STATUS_FINISHED;
567 }
568
569 float bot_cmd_turn(entity this)
570 {
571         this.v_angle_y = this.v_angle.y + bot_cmd.bot_cmd_parm_float;
572         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
573         return CMD_STATUS_FINISHED;
574 }
575
576 float bot_cmd_select_weapon(entity this)
577 {
578         float id = bot_cmd.bot_cmd_parm_float;
579
580         if(id < WEP_FIRST || id > WEP_LAST)
581                 return CMD_STATUS_ERROR;
582
583         bool success = false;
584
585         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
586         {
587                 .entity weaponentity = weaponentities[slot];
588                 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
589                         continue;
590
591                 if(client_hasweapon(this, Weapons_from(id), weaponentity, true, false))
592                 {
593                         success = true;
594                         this.(weaponentity).m_switchweapon = Weapons_from(id);
595                 }
596         }
597
598         if(!success)
599                 return CMD_STATUS_ERROR;
600
601         return CMD_STATUS_FINISHED;
602 }
603
604 .int bot_cmd_condition_status;
605
606 const int CMD_CONDITION_NONE = 0;
607 const int CMD_CONDITION_true = 1;
608 const int CMD_CONDITION_false = 2;
609 const int CMD_CONDITION_true_BLOCK = 4;
610 const int CMD_CONDITION_false_BLOCK = 8;
611
612 float bot_cmd_eval(entity this, string expr)
613 {
614         // Search for numbers
615         if(IS_DIGIT(substring(expr, 0, 1)))
616                 return stof(expr);
617
618         // Search for cvars
619         if(substring(expr, 0, 5)=="cvar.")
620                 return cvar(substring(expr, 5, strlen(expr)));
621
622         // Search for fields
623         // TODO: expand with support for more fields (key carrier, ball carrier, armor etc)
624         switch(expr)
625         {
626                 case "health":
627                         return GetResourceAmount(this, RESOURCE_HEALTH);
628                 case "speed":
629                         return vlen(this.velocity);
630                 case "flagcarrier":
631                         return ((this.flagcarried!=NULL));
632         }
633
634         LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
635         return 0;
636 }
637
638 float bot_cmd_if(entity this)
639 {
640         string expr, val_a, val_b;
641         float cmpofs;
642
643         if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
644         {
645                 // Only one "if" block is allowed at time
646                 LOG_INFO("ERROR: Only one conditional block can be processed at time");
647                 bot_clearqueue(this);
648                 return CMD_STATUS_ERROR;
649         }
650
651         this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
652
653         // search for operators
654         expr = bot_cmd.bot_cmd_parm_string;
655
656         cmpofs = strstrofs(expr,"=",0);
657
658         if(cmpofs>0)
659         {
660                 val_a = substring(expr,0,cmpofs);
661                 val_b = substring(expr,cmpofs+1,strlen(expr));
662
663                 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
664                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
665                 else
666                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
667
668                 return CMD_STATUS_FINISHED;
669         }
670
671         cmpofs = strstrofs(expr,">",0);
672
673         if(cmpofs>0)
674         {
675                 val_a = substring(expr,0,cmpofs);
676                 val_b = substring(expr,cmpofs+1,strlen(expr));
677
678                 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
679                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
680                 else
681                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
682
683                 return CMD_STATUS_FINISHED;
684         }
685
686         cmpofs = strstrofs(expr,"<",0);
687
688         if(cmpofs>0)
689         {
690                 val_a = substring(expr,0,cmpofs);
691                 val_b = substring(expr,cmpofs+1,strlen(expr));
692
693                 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
694                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
695                 else
696                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
697
698                 return CMD_STATUS_FINISHED;
699         }
700
701         if(bot_cmd_eval(this, expr))
702                 this.bot_cmd_condition_status |= CMD_CONDITION_true;
703         else
704                 this.bot_cmd_condition_status |= CMD_CONDITION_false;
705
706         return CMD_STATUS_FINISHED;
707 }
708
709 float bot_cmd_else(entity this)
710 {
711         this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
712         this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
713         return CMD_STATUS_FINISHED;
714 }
715
716 float bot_cmd_fi(entity this)
717 {
718         this.bot_cmd_condition_status = CMD_CONDITION_NONE;
719         return CMD_STATUS_FINISHED;
720 }
721
722 float bot_cmd_resetaim(entity this)
723 {
724         this.v_angle = '0 0 0';
725         return CMD_STATUS_FINISHED;
726 }
727
728 .float bot_cmd_aim_begintime;
729 .float bot_cmd_aim_endtime;
730 .vector bot_cmd_aim_begin;
731 .vector bot_cmd_aim_end;
732
733 float bot_cmd_aim(entity this)
734 {
735         // Current direction
736         if(this.bot_cmd_aim_endtime)
737         {
738                 float progress;
739
740                 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
741                 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
742
743                 if(time>=this.bot_cmd_aim_endtime)
744                 {
745                         this.bot_cmd_aim_endtime = 0;
746                         return CMD_STATUS_FINISHED;
747                 }
748                 else
749                         return CMD_STATUS_EXECUTING;
750         }
751
752         // New aiming direction
753         string parms;
754         float tokens, step;
755
756         parms = bot_cmd.bot_cmd_parm_string;
757
758         tokens = tokenizebyseparator(parms, " ");
759
760         if(tokens<2||tokens>3)
761                 return CMD_STATUS_ERROR;
762
763         step = (tokens == 3) ? stof(argv(2)) : 0;
764
765         if(step == 0)
766         {
767                 this.v_angle_x -= stof(argv(1));
768                 this.v_angle_y += stof(argv(0));
769                 return CMD_STATUS_FINISHED;
770         }
771
772         this.bot_cmd_aim_begin = this.v_angle;
773
774         this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
775         this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
776         this.bot_cmd_aim_end_z = 0;
777
778         this.bot_cmd_aim_begintime = time;
779         this.bot_cmd_aim_endtime = time + step;
780
781         return CMD_STATUS_EXECUTING;
782 }
783
784 float bot_cmd_aimtarget(entity this)
785 {
786         if(this.bot_cmd_aim_endtime)
787         {
788                 return bot_cmd_aim(this);
789         }
790
791         entity e;
792         string parms;
793         vector v;
794         float tokens, step;
795
796         parms = bot_cmd.bot_cmd_parm_string;
797
798         tokens = tokenizebyseparator(parms, " ");
799
800         e = bot_getplace(this, argv(0));
801         if(!e)
802                 return CMD_STATUS_ERROR;
803
804         v = e.origin + (e.mins + e.maxs) * 0.5;
805
806         if(tokens==1)
807         {
808                 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
809                 this.v_angle_x = -this.v_angle.x;
810                 return CMD_STATUS_FINISHED;
811         }
812
813         if(tokens<1||tokens>2)
814                 return CMD_STATUS_ERROR;
815
816         step = stof(argv(1));
817
818         this.bot_cmd_aim_begin = this.v_angle;
819         this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
820         this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
821
822         this.bot_cmd_aim_begintime = time;
823         this.bot_cmd_aim_endtime = time + step;
824
825         return CMD_STATUS_EXECUTING;
826 }
827
828 .int bot_cmd_keys;
829
830 const int BOT_CMD_KEY_NONE              = 0;
831 const int BOT_CMD_KEY_FORWARD   = BIT(0);
832 const int BOT_CMD_KEY_BACKWARD  = BIT(1);
833 const int BOT_CMD_KEY_RIGHT     = BIT(2);
834 const int BOT_CMD_KEY_LEFT              = BIT(3);
835 const int BOT_CMD_KEY_JUMP              = BIT(4);
836 const int BOT_CMD_KEY_ATTACK1   = BIT(5);
837 const int BOT_CMD_KEY_ATTACK2   = BIT(6);
838 const int BOT_CMD_KEY_USE               = BIT(7);
839 const int BOT_CMD_KEY_HOOK              = BIT(8);
840 const int BOT_CMD_KEY_CROUCH    = BIT(9);
841 const int BOT_CMD_KEY_CHAT              = BIT(10);
842
843 bool bot_presskeys(entity this)
844 {
845         CS(this).movement = '0 0 0';
846         PHYS_INPUT_BUTTON_JUMP(this) = false;
847         PHYS_INPUT_BUTTON_CROUCH(this) = false;
848         PHYS_INPUT_BUTTON_ATCK(this) = false;
849         PHYS_INPUT_BUTTON_ATCK2(this) = false;
850         PHYS_INPUT_BUTTON_USE(this) = false;
851         PHYS_INPUT_BUTTON_HOOK(this) = false;
852         PHYS_INPUT_BUTTON_CHAT(this) = false;
853
854         if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
855                 return false;
856
857         if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
858                 CS(this).movement_x = autocvar_sv_maxspeed;
859         else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
860                 CS(this).movement_x = -autocvar_sv_maxspeed;
861
862         if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
863                 CS(this).movement_y = autocvar_sv_maxspeed;
864         else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
865                 CS(this).movement_y = -autocvar_sv_maxspeed;
866
867         if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
868                 PHYS_INPUT_BUTTON_JUMP(this) = true;
869
870         if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
871                 PHYS_INPUT_BUTTON_CROUCH(this) = true;
872
873         if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
874                 PHYS_INPUT_BUTTON_ATCK(this) = true;
875
876         if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
877                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
878
879         if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
880                 PHYS_INPUT_BUTTON_USE(this) = true;
881
882         if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
883                 PHYS_INPUT_BUTTON_HOOK(this) = true;
884
885         if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
886                 PHYS_INPUT_BUTTON_CHAT(this) = true;
887
888         return true;
889 }
890
891
892 float bot_cmd_keypress_handler(entity this, string key, float enabled)
893 {
894         switch(key)
895         {
896                 case "all":
897                         if(enabled)
898                                 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
899                         else
900                                 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
901                 case "forward":
902                         if(enabled)
903                         {
904                                 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
905                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
906                         }
907                         else
908                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
909                         break;
910                 case "backward":
911                         if(enabled)
912                         {
913                                 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
914                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
915                         }
916                         else
917                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
918                         break;
919                 case "left":
920                         if(enabled)
921                         {
922                                 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
923                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
924                         }
925                         else
926                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
927                         break;
928                 case "right":
929                         if(enabled)
930                         {
931                                 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
932                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
933                         }
934                         else
935                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
936                         break;
937                 case "jump":
938                         if(enabled)
939                                 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
940                         else
941                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
942                         break;
943                 case "crouch":
944                         if(enabled)
945                                 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
946                         else
947                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
948                         break;
949                 case "attack1":
950                         if(enabled)
951                                 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
952                         else
953                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
954                         break;
955                 case "attack2":
956                         if(enabled)
957                                 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
958                         else
959                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
960                         break;
961                 case "use":
962                         if(enabled)
963                                 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
964                         else
965                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
966                         break;
967                 case "hook":
968                         if(enabled)
969                                 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
970                         else
971                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
972                         break;
973                 case "chat":
974                         if(enabled)
975                                 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
976                         else
977                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
978                         break;
979                 default:
980                         break;
981         }
982
983         return CMD_STATUS_FINISHED;
984 }
985
986 float bot_cmd_presskey(entity this)
987 {
988         string key;
989
990         key = bot_cmd.bot_cmd_parm_string;
991
992         bot_cmd_keypress_handler(this, key,true);
993
994         return CMD_STATUS_FINISHED;
995 }
996
997 float bot_cmd_releasekey(entity this)
998 {
999         string key;
1000
1001         key = bot_cmd.bot_cmd_parm_string;
1002
1003         return bot_cmd_keypress_handler(this, key,false);
1004 }
1005
1006 float bot_cmd_pause(entity this)
1007 {
1008         PHYS_INPUT_BUTTON_DRAG(this) = false;
1009         PHYS_INPUT_BUTTON_USE(this) = false;
1010         PHYS_INPUT_BUTTON_ATCK(this) = false;
1011         PHYS_INPUT_BUTTON_JUMP(this) = false;
1012         PHYS_INPUT_BUTTON_HOOK(this) = false;
1013         PHYS_INPUT_BUTTON_CHAT(this) = false;
1014         PHYS_INPUT_BUTTON_ATCK2(this) = false;
1015         PHYS_INPUT_BUTTON_CROUCH(this) = false;
1016
1017         CS(this).movement = '0 0 0';
1018         this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1019
1020         bot_clear(this);
1021         this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1022         return CMD_STATUS_FINISHED;
1023 }
1024
1025 float bot_cmd_moveto(entity this)
1026 {
1027         return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1028 }
1029
1030 float bot_cmd_movetotarget(entity this)
1031 {
1032         entity e;
1033         e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1034         if(!e)
1035                 return CMD_STATUS_ERROR;
1036         return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1037 }
1038
1039 float bot_cmd_resetgoal(entity this)
1040 {
1041         return this.cmd_resetgoal(this);
1042 }
1043
1044
1045 float bot_cmd_sound(entity this)
1046 {
1047         string f;
1048         f = bot_cmd.bot_cmd_parm_string;
1049
1050         float n = tokenizebyseparator(f, " ");
1051
1052         string sample = f;
1053         float chan = CH_WEAPON_B;
1054         float vol = VOL_BASE;
1055         float atten = ATTEN_MIN;
1056
1057         if(n >= 1)
1058                 sample = argv(n - 1);
1059         if(n >= 2)
1060                 chan = stof(argv(0));
1061         if(n >= 3)
1062                 vol = stof(argv(1));
1063         if(n >= 4)
1064                 atten = stof(argv(2));
1065
1066         precache_sound(f);
1067         _sound(this, chan, sample, vol, atten);
1068
1069         return CMD_STATUS_FINISHED;
1070 }
1071
1072 .entity tuba_note;
1073 float bot_cmd_debug_assert_canfire(entity this)
1074 {
1075         float f = bot_cmd.bot_cmd_parm_float;
1076
1077         int slot = 0; // TODO: unhardcode?
1078         .entity weaponentity = weaponentities[slot];
1079         if(this.(weaponentity).state != WS_READY)
1080         {
1081                 if(f)
1082                 {
1083                         this.colormod = '0 8 8';
1084                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1085                 }
1086         }
1087         else if(ATTACK_FINISHED(this, weaponentity) > time)
1088         {
1089                 if(f)
1090                 {
1091                         this.colormod = '8 0 8';
1092                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left)");
1093                 }
1094         }
1095         else if(this.(weaponentity).tuba_note)
1096         {
1097                 if(f)
1098                 {
1099                         this.colormod = '8 0 0';
1100                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1101                 }
1102         }
1103         else
1104         {
1105                 if(!f)
1106                 {
1107                         this.colormod = '8 8 0';
1108                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(this, weaponentity) - time), " seconds left");
1109                 }
1110         }
1111
1112         return CMD_STATUS_FINISHED;
1113 }
1114
1115 //
1116
1117 void bot_command_executed(entity this, bool rm)
1118 {
1119         entity cmd;
1120
1121         cmd = bot_cmd;
1122
1123         if(rm)
1124                 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1125
1126         this.bot_cmd_execution_index++;
1127 }
1128
1129 void bot_setcurrentcommand(entity this)
1130 {
1131         bot_cmd = NULL;
1132
1133         if(!this.bot_cmd_current)
1134         {
1135                 this.bot_cmd_current = new_pure(bot_cmd);
1136         }
1137
1138         bot_cmd = this.bot_cmd_current;
1139         if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1140         {
1141                 if(bot_havecommand(this, this.bot_cmd_execution_index))
1142                 {
1143                         string cmdstring;
1144                         cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1145                         if(bot_decodecommand(cmdstring))
1146                         {
1147                                 bot_cmd.owner = this;
1148                                 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1149                         }
1150                         else
1151                         {
1152                                 // Invalid command, remove from queue
1153                                 bot_cmd = NULL;
1154                                 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1155                                 this.bot_cmd_execution_index++;
1156                         }
1157                 }
1158                 else
1159                         bot_cmd = NULL;
1160         }
1161 }
1162
1163 void bot_resetqueues()
1164 {
1165         FOREACH_CLIENT(it.isbot, {
1166                 it.bot_cmd_execution_index = 0;
1167                 bot_clearqueue(it);
1168                 // also, cancel all barriers
1169                 it.bot_barrier = 0;
1170                 for(int i = 0; i < it.bot_places_count; ++i)
1171                 {
1172                         strfree(it.(bot_placenames[i]));
1173                 }
1174                 it.bot_places_count = 0;
1175         });
1176
1177         bot_barriertime = time;
1178 }
1179
1180 // Here we map commands to functions and deal with complex interactions between commands and execution states
1181 // NOTE: Of course you need to include your commands here too :)
1182 float bot_execute_commands_once(entity this)
1183 {
1184         float status, ispressingkey;
1185
1186         // Find command
1187         bot_setcurrentcommand(this);
1188
1189         // Ignore all commands except continue when the bot is paused
1190         if(!(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1191         {
1192                 // if we have no bot command, better return
1193                 // old logic kept pressing previously pressed keys, but that has problems
1194                 // (namely, it means you cannot make a bot "normal" ever again)
1195                 // to keep a bot walking for a while, use the "wait" bot command
1196                 if(bot_cmd == world)
1197                         return 0;
1198         }
1199         else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1200         {
1201                 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1202                 {
1203                         bot_command_executed(this, true);
1204                         LOG_INFO("WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.");
1205                 }
1206                 return 1;
1207         }
1208
1209         // Keep pressing keys raised by the "presskey" command
1210         ispressingkey = boolean(bot_presskeys(this));
1211
1212         // Handle conditions
1213         if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1214         if(this.bot_cmd_condition_status & CMD_CONDITION_true && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1215         {
1216                 bot_command_executed(this, true);
1217                 return -1;
1218         }
1219         else if(this.bot_cmd_condition_status & CMD_CONDITION_false && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1220         {
1221                 bot_command_executed(this, true);
1222                 return -1;
1223         }
1224
1225         // Map commands to functions
1226         switch(bot_cmd.bot_cmd_type)
1227         {
1228                 case BOT_CMD_NULL:
1229                         return ispressingkey;
1230                         //break;
1231                 case BOT_CMD_PAUSE:
1232                         status = bot_cmd_pause(this);
1233                         break;
1234                 case BOT_CMD_CONTINUE:
1235                         status = bot_cmd_continue(this);
1236                         break;
1237                 case BOT_CMD_WAIT:
1238                         status = bot_cmd_wait(this);
1239                         break;
1240                 case BOT_CMD_WAIT_UNTIL:
1241                         status = bot_cmd_wait_until(this);
1242                         break;
1243                 case BOT_CMD_TURN:
1244                         status = bot_cmd_turn(this);
1245                         break;
1246                 case BOT_CMD_MOVETO:
1247                         status = bot_cmd_moveto(this);
1248                         break;
1249                 case BOT_CMD_MOVETOTARGET:
1250                         status = bot_cmd_movetotarget(this);
1251                         break;
1252                 case BOT_CMD_RESETGOAL:
1253                         status = bot_cmd_resetgoal(this);
1254                         break;
1255                 case BOT_CMD_CC:
1256                         status = bot_cmd_cc(this);
1257                         break;
1258                 case BOT_CMD_IF:
1259                         status = bot_cmd_if(this);
1260                         break;
1261                 case BOT_CMD_ELSE:
1262                         status = bot_cmd_else(this);
1263                         break;
1264                 case BOT_CMD_FI:
1265                         status = bot_cmd_fi(this);
1266                         break;
1267                 case BOT_CMD_RESETAIM:
1268                         status = bot_cmd_resetaim(this);
1269                         break;
1270                 case BOT_CMD_AIM:
1271                         status = bot_cmd_aim(this);
1272                         break;
1273                 case BOT_CMD_AIMTARGET:
1274                         status = bot_cmd_aimtarget(this);
1275                         break;
1276                 case BOT_CMD_PRESSKEY:
1277                         status = bot_cmd_presskey(this);
1278                         break;
1279                 case BOT_CMD_RELEASEKEY:
1280                         status = bot_cmd_releasekey(this);
1281                         break;
1282                 case BOT_CMD_SELECTWEAPON:
1283                         status = bot_cmd_select_weapon(this);
1284                         break;
1285                 case BOT_CMD_IMPULSE:
1286                         status = bot_cmd_impulse(this);
1287                         break;
1288                 case BOT_CMD_BARRIER:
1289                         status = bot_cmd_barrier(this);
1290                         break;
1291                 case BOT_CMD_CONSOLE:
1292                         localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1293                         status = CMD_STATUS_FINISHED;
1294                         break;
1295                 case BOT_CMD_SOUND:
1296                         status = bot_cmd_sound(this);
1297                         break;
1298                 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1299                         status = bot_cmd_debug_assert_canfire(this);
1300                         break;
1301                 default:
1302                         LOG_INFOF("ERROR: Invalid command on queue with id '%s'", ftos(bot_cmd.bot_cmd_type));
1303                         return 0;
1304         }
1305
1306         if (status==CMD_STATUS_ERROR)
1307                 LOG_INFOF("ERROR: The command '%s' returned an error status", bot_cmd_string[bot_cmd.bot_cmd_type]);
1308
1309         // Move execution pointer
1310         if(status==CMD_STATUS_EXECUTING)
1311         {
1312                 return 1;
1313         }
1314         else
1315         {
1316                 if(autocvar_g_debug_bot_commands)
1317                 {
1318                         string parms;
1319
1320                         switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1321                         {
1322                                 case BOT_CMD_PARAMETER_FLOAT:
1323                                         parms = ftos(bot_cmd.bot_cmd_parm_float);
1324                                         break;
1325                                 case BOT_CMD_PARAMETER_STRING:
1326                                         parms = bot_cmd.bot_cmd_parm_string;
1327                                         break;
1328                                 case BOT_CMD_PARAMETER_VECTOR:
1329                                         parms = vtos(bot_cmd.bot_cmd_parm_vector);
1330                                         break;
1331                                 default:
1332                                         parms = "";
1333                                         break;
1334                         }
1335                         clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1336                 }
1337
1338                 bot_command_executed(this, true);
1339         }
1340
1341         if(status == CMD_STATUS_FINISHED)
1342                 return -1;
1343
1344         return CMD_STATUS_ERROR;
1345 }
1346
1347 // This function should be (the only) called directly from the bot ai loop
1348 int bot_execute_commands(entity this)
1349 {
1350         int f;
1351         do
1352         {
1353                 f = bot_execute_commands_once(this);
1354         }
1355         while(f < 0);
1356         return f;
1357 }