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