]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/scripting.qc
Merge branch 'master' into martin-t/rpc-acc
[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         switch(expr)
624         {
625                 case "health":
626                         return this.health;
627                 case "speed":
628                         return vlen(this.velocity);
629                 case "flagcarrier":
630                         return ((this.flagcarried!=NULL));
631         }
632
633         LOG_INFO("ERROR: Unable to convert the expression '", expr, "' into a numeric value");
634         return 0;
635 }
636
637 float bot_cmd_if(entity this)
638 {
639         string expr, val_a, val_b;
640         float cmpofs;
641
642         if(this.bot_cmd_condition_status != CMD_CONDITION_NONE)
643         {
644                 // Only one "if" block is allowed at time
645                 LOG_INFO("ERROR: Only one conditional block can be processed at time");
646                 bot_clearqueue(this);
647                 return CMD_STATUS_ERROR;
648         }
649
650         this.bot_cmd_condition_status |= CMD_CONDITION_true_BLOCK;
651
652         // search for operators
653         expr = bot_cmd.bot_cmd_parm_string;
654
655         cmpofs = strstrofs(expr,"=",0);
656
657         if(cmpofs>0)
658         {
659                 val_a = substring(expr,0,cmpofs);
660                 val_b = substring(expr,cmpofs+1,strlen(expr));
661
662                 if(bot_cmd_eval(this, val_a)==bot_cmd_eval(this, val_b))
663                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
664                 else
665                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
666
667                 return CMD_STATUS_FINISHED;
668         }
669
670         cmpofs = strstrofs(expr,">",0);
671
672         if(cmpofs>0)
673         {
674                 val_a = substring(expr,0,cmpofs);
675                 val_b = substring(expr,cmpofs+1,strlen(expr));
676
677                 if(bot_cmd_eval(this, val_a)>bot_cmd_eval(this, val_b))
678                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
679                 else
680                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
681
682                 return CMD_STATUS_FINISHED;
683         }
684
685         cmpofs = strstrofs(expr,"<",0);
686
687         if(cmpofs>0)
688         {
689                 val_a = substring(expr,0,cmpofs);
690                 val_b = substring(expr,cmpofs+1,strlen(expr));
691
692                 if(bot_cmd_eval(this, val_a)<bot_cmd_eval(this, val_b))
693                         this.bot_cmd_condition_status |= CMD_CONDITION_true;
694                 else
695                         this.bot_cmd_condition_status |= CMD_CONDITION_false;
696
697                 return CMD_STATUS_FINISHED;
698         }
699
700         if(bot_cmd_eval(this, expr))
701                 this.bot_cmd_condition_status |= CMD_CONDITION_true;
702         else
703                 this.bot_cmd_condition_status |= CMD_CONDITION_false;
704
705         return CMD_STATUS_FINISHED;
706 }
707
708 float bot_cmd_else(entity this)
709 {
710         this.bot_cmd_condition_status &= ~CMD_CONDITION_true_BLOCK;
711         this.bot_cmd_condition_status |= CMD_CONDITION_false_BLOCK;
712         return CMD_STATUS_FINISHED;
713 }
714
715 float bot_cmd_fi(entity this)
716 {
717         this.bot_cmd_condition_status = CMD_CONDITION_NONE;
718         return CMD_STATUS_FINISHED;
719 }
720
721 float bot_cmd_resetaim(entity this)
722 {
723         this.v_angle = '0 0 0';
724         return CMD_STATUS_FINISHED;
725 }
726
727 .float bot_cmd_aim_begintime;
728 .float bot_cmd_aim_endtime;
729 .vector bot_cmd_aim_begin;
730 .vector bot_cmd_aim_end;
731
732 float bot_cmd_aim(entity this)
733 {
734         // Current direction
735         if(this.bot_cmd_aim_endtime)
736         {
737                 float progress;
738
739                 progress = min(1 - (this.bot_cmd_aim_endtime - time) / (this.bot_cmd_aim_endtime - this.bot_cmd_aim_begintime),1);
740                 this.v_angle = this.bot_cmd_aim_begin + ((this.bot_cmd_aim_end - this.bot_cmd_aim_begin) * progress);
741
742                 if(time>=this.bot_cmd_aim_endtime)
743                 {
744                         this.bot_cmd_aim_endtime = 0;
745                         return CMD_STATUS_FINISHED;
746                 }
747                 else
748                         return CMD_STATUS_EXECUTING;
749         }
750
751         // New aiming direction
752         string parms;
753         float tokens, step;
754
755         parms = bot_cmd.bot_cmd_parm_string;
756
757         tokens = tokenizebyseparator(parms, " ");
758
759         if(tokens<2||tokens>3)
760                 return CMD_STATUS_ERROR;
761
762         step = (tokens == 3) ? stof(argv(2)) : 0;
763
764         if(step == 0)
765         {
766                 this.v_angle_x -= stof(argv(1));
767                 this.v_angle_y += stof(argv(0));
768                 return CMD_STATUS_FINISHED;
769         }
770
771         this.bot_cmd_aim_begin = this.v_angle;
772
773         this.bot_cmd_aim_end_x = this.v_angle.x - stof(argv(1));
774         this.bot_cmd_aim_end_y = this.v_angle.y + stof(argv(0));
775         this.bot_cmd_aim_end_z = 0;
776
777         this.bot_cmd_aim_begintime = time;
778         this.bot_cmd_aim_endtime = time + step;
779
780         return CMD_STATUS_EXECUTING;
781 }
782
783 float bot_cmd_aimtarget(entity this)
784 {
785         if(this.bot_cmd_aim_endtime)
786         {
787                 return bot_cmd_aim(this);
788         }
789
790         entity e;
791         string parms;
792         vector v;
793         float tokens, step;
794
795         parms = bot_cmd.bot_cmd_parm_string;
796
797         tokens = tokenizebyseparator(parms, " ");
798
799         e = bot_getplace(this, argv(0));
800         if(!e)
801                 return CMD_STATUS_ERROR;
802
803         v = e.origin + (e.mins + e.maxs) * 0.5;
804
805         if(tokens==1)
806         {
807                 this.v_angle = vectoangles(v - (this.origin + this.view_ofs));
808                 this.v_angle_x = -this.v_angle.x;
809                 return CMD_STATUS_FINISHED;
810         }
811
812         if(tokens<1||tokens>2)
813                 return CMD_STATUS_ERROR;
814
815         step = stof(argv(1));
816
817         this.bot_cmd_aim_begin = this.v_angle;
818         this.bot_cmd_aim_end = vectoangles(v - (this.origin + this.view_ofs));
819         this.bot_cmd_aim_end_x = -this.bot_cmd_aim_end.x;
820
821         this.bot_cmd_aim_begintime = time;
822         this.bot_cmd_aim_endtime = time + step;
823
824         return CMD_STATUS_EXECUTING;
825 }
826
827 .int bot_cmd_keys;
828
829 const int BOT_CMD_KEY_NONE              = 0;
830 const int BOT_CMD_KEY_FORWARD   = BIT(0);
831 const int BOT_CMD_KEY_BACKWARD  = BIT(1);
832 const int BOT_CMD_KEY_RIGHT     = BIT(2);
833 const int BOT_CMD_KEY_LEFT              = BIT(3);
834 const int BOT_CMD_KEY_JUMP              = BIT(4);
835 const int BOT_CMD_KEY_ATTACK1   = BIT(5);
836 const int BOT_CMD_KEY_ATTACK2   = BIT(6);
837 const int BOT_CMD_KEY_USE               = BIT(7);
838 const int BOT_CMD_KEY_HOOK              = BIT(8);
839 const int BOT_CMD_KEY_CROUCH    = BIT(9);
840 const int BOT_CMD_KEY_CHAT              = BIT(10);
841
842 bool bot_presskeys(entity this)
843 {
844         CS(this).movement = '0 0 0';
845         PHYS_INPUT_BUTTON_JUMP(this) = false;
846         PHYS_INPUT_BUTTON_CROUCH(this) = false;
847         PHYS_INPUT_BUTTON_ATCK(this) = false;
848         PHYS_INPUT_BUTTON_ATCK2(this) = false;
849         PHYS_INPUT_BUTTON_USE(this) = false;
850         PHYS_INPUT_BUTTON_HOOK(this) = false;
851         PHYS_INPUT_BUTTON_CHAT(this) = false;
852
853         if(this.bot_cmd_keys == BOT_CMD_KEY_NONE)
854                 return false;
855
856         if(this.bot_cmd_keys & BOT_CMD_KEY_FORWARD)
857                 CS(this).movement_x = autocvar_sv_maxspeed;
858         else if(this.bot_cmd_keys & BOT_CMD_KEY_BACKWARD)
859                 CS(this).movement_x = -autocvar_sv_maxspeed;
860
861         if(this.bot_cmd_keys & BOT_CMD_KEY_RIGHT)
862                 CS(this).movement_y = autocvar_sv_maxspeed;
863         else if(this.bot_cmd_keys & BOT_CMD_KEY_LEFT)
864                 CS(this).movement_y = -autocvar_sv_maxspeed;
865
866         if(this.bot_cmd_keys & BOT_CMD_KEY_JUMP)
867                 PHYS_INPUT_BUTTON_JUMP(this) = true;
868
869         if(this.bot_cmd_keys & BOT_CMD_KEY_CROUCH)
870                 PHYS_INPUT_BUTTON_CROUCH(this) = true;
871
872         if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK1)
873                 PHYS_INPUT_BUTTON_ATCK(this) = true;
874
875         if(this.bot_cmd_keys & BOT_CMD_KEY_ATTACK2)
876                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
877
878         if(this.bot_cmd_keys & BOT_CMD_KEY_USE)
879                 PHYS_INPUT_BUTTON_USE(this) = true;
880
881         if(this.bot_cmd_keys & BOT_CMD_KEY_HOOK)
882                 PHYS_INPUT_BUTTON_HOOK(this) = true;
883
884         if(this.bot_cmd_keys & BOT_CMD_KEY_CHAT)
885                 PHYS_INPUT_BUTTON_CHAT(this) = true;
886
887         return true;
888 }
889
890
891 float bot_cmd_keypress_handler(entity this, string key, float enabled)
892 {
893         switch(key)
894         {
895                 case "all":
896                         if(enabled)
897                                 this.bot_cmd_keys = (2 ** 20) - 1; // >:)
898                         else
899                                 this.bot_cmd_keys = BOT_CMD_KEY_NONE;
900                 case "forward":
901                         if(enabled)
902                         {
903                                 this.bot_cmd_keys |= BOT_CMD_KEY_FORWARD;
904                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
905                         }
906                         else
907                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
908                         break;
909                 case "backward":
910                         if(enabled)
911                         {
912                                 this.bot_cmd_keys |= BOT_CMD_KEY_BACKWARD;
913                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_FORWARD;
914                         }
915                         else
916                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_BACKWARD;
917                         break;
918                 case "left":
919                         if(enabled)
920                         {
921                                 this.bot_cmd_keys |= BOT_CMD_KEY_LEFT;
922                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
923                         }
924                         else
925                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
926                         break;
927                 case "right":
928                         if(enabled)
929                         {
930                                 this.bot_cmd_keys |= BOT_CMD_KEY_RIGHT;
931                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_LEFT;
932                         }
933                         else
934                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_RIGHT;
935                         break;
936                 case "jump":
937                         if(enabled)
938                                 this.bot_cmd_keys |= BOT_CMD_KEY_JUMP;
939                         else
940                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_JUMP;
941                         break;
942                 case "crouch":
943                         if(enabled)
944                                 this.bot_cmd_keys |= BOT_CMD_KEY_CROUCH;
945                         else
946                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_CROUCH;
947                         break;
948                 case "attack1":
949                         if(enabled)
950                                 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK1;
951                         else
952                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK1;
953                         break;
954                 case "attack2":
955                         if(enabled)
956                                 this.bot_cmd_keys |= BOT_CMD_KEY_ATTACK2;
957                         else
958                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_ATTACK2;
959                         break;
960                 case "use":
961                         if(enabled)
962                                 this.bot_cmd_keys |= BOT_CMD_KEY_USE;
963                         else
964                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_USE;
965                         break;
966                 case "hook":
967                         if(enabled)
968                                 this.bot_cmd_keys |= BOT_CMD_KEY_HOOK;
969                         else
970                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_HOOK;
971                         break;
972                 case "chat":
973                         if(enabled)
974                                 this.bot_cmd_keys |= BOT_CMD_KEY_CHAT;
975                         else
976                                 this.bot_cmd_keys &= ~BOT_CMD_KEY_CHAT;
977                         break;
978                 default:
979                         break;
980         }
981
982         return CMD_STATUS_FINISHED;
983 }
984
985 float bot_cmd_presskey(entity this)
986 {
987         string key;
988
989         key = bot_cmd.bot_cmd_parm_string;
990
991         bot_cmd_keypress_handler(this, key,true);
992
993         return CMD_STATUS_FINISHED;
994 }
995
996 float bot_cmd_releasekey(entity this)
997 {
998         string key;
999
1000         key = bot_cmd.bot_cmd_parm_string;
1001
1002         return bot_cmd_keypress_handler(this, key,false);
1003 }
1004
1005 float bot_cmd_pause(entity this)
1006 {
1007         PHYS_INPUT_BUTTON_DRAG(this) = false;
1008         PHYS_INPUT_BUTTON_USE(this) = false;
1009         PHYS_INPUT_BUTTON_ATCK(this) = false;
1010         PHYS_INPUT_BUTTON_JUMP(this) = false;
1011         PHYS_INPUT_BUTTON_HOOK(this) = false;
1012         PHYS_INPUT_BUTTON_CHAT(this) = false;
1013         PHYS_INPUT_BUTTON_ATCK2(this) = false;
1014         PHYS_INPUT_BUTTON_CROUCH(this) = false;
1015
1016         CS(this).movement = '0 0 0';
1017         this.bot_cmd_keys = BOT_CMD_KEY_NONE;
1018
1019         bot_clear(this);
1020         this.bot_exec_status |= BOT_EXEC_STATUS_PAUSED;
1021         return CMD_STATUS_FINISHED;
1022 }
1023
1024 float bot_cmd_moveto(entity this)
1025 {
1026         return this.cmd_moveto(this, bot_cmd.bot_cmd_parm_vector);
1027 }
1028
1029 float bot_cmd_movetotarget(entity this)
1030 {
1031         entity e;
1032         e = bot_getplace(this, bot_cmd.bot_cmd_parm_string);
1033         if(!e)
1034                 return CMD_STATUS_ERROR;
1035         return this.cmd_moveto(this, e.origin + (e.mins + e.maxs) * 0.5);
1036 }
1037
1038 float bot_cmd_resetgoal(entity this)
1039 {
1040         return this.cmd_resetgoal(this);
1041 }
1042
1043
1044 float bot_cmd_sound(entity this)
1045 {
1046         string f;
1047         f = bot_cmd.bot_cmd_parm_string;
1048
1049         float n = tokenizebyseparator(f, " ");
1050
1051         string sample = f;
1052         float chan = CH_WEAPON_B;
1053         float vol = VOL_BASE;
1054         float atten = ATTEN_MIN;
1055
1056         if(n >= 1)
1057                 sample = argv(n - 1);
1058         if(n >= 2)
1059                 chan = stof(argv(0));
1060         if(n >= 3)
1061                 vol = stof(argv(1));
1062         if(n >= 4)
1063                 atten = stof(argv(2));
1064
1065         precache_sound(f);
1066         _sound(this, chan, sample, vol, atten);
1067
1068         return CMD_STATUS_FINISHED;
1069 }
1070
1071 .entity tuba_note;
1072 float bot_cmd_debug_assert_canfire(entity this)
1073 {
1074         float f = bot_cmd.bot_cmd_parm_float;
1075
1076         int slot = 0; // TODO: unhardcode?
1077         .entity weaponentity = weaponentities[slot];
1078         if(this.(weaponentity).state != WS_READY)
1079         {
1080                 if(f)
1081                 {
1082                         this.colormod = '0 8 8';
1083                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by weaponentity state");
1084                 }
1085         }
1086         else if(ATTACK_FINISHED(this, slot) > time)
1087         {
1088                 if(f)
1089                 {
1090                         this.colormod = '8 0 8';
1091                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, inhibited by ATTACK_FINISHED (", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left)");
1092                 }
1093         }
1094         else if(this.(weaponentity).tuba_note)
1095         {
1096                 if(f)
1097                 {
1098                         this.colormod = '8 0 0';
1099                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " wants to fire, bot still has an active tuba note");
1100                 }
1101         }
1102         else
1103         {
1104                 if(!f)
1105                 {
1106                         this.colormod = '8 8 0';
1107                         LOG_INFO("Bot ", this.netname, " using ", this.(weaponentity).weaponname, " thinks it has fired, but apparently did not; ATTACK_FINISHED says ", ftos(ATTACK_FINISHED(this, slot) - time), " seconds left");
1108                 }
1109         }
1110
1111         return CMD_STATUS_FINISHED;
1112 }
1113
1114 //
1115
1116 void bot_command_executed(entity this, bool rm)
1117 {
1118         entity cmd;
1119
1120         cmd = bot_cmd;
1121
1122         if(rm)
1123                 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1124
1125         this.bot_cmd_execution_index++;
1126 }
1127
1128 void bot_setcurrentcommand(entity this)
1129 {
1130         bot_cmd = NULL;
1131
1132         if(!this.bot_cmd_current)
1133         {
1134                 this.bot_cmd_current = new_pure(bot_cmd);
1135         }
1136
1137         bot_cmd = this.bot_cmd_current;
1138         if(bot_cmd.bot_cmd_index != this.bot_cmd_execution_index || this.bot_cmd_execution_index == 0)
1139         {
1140                 if(bot_havecommand(this, this.bot_cmd_execution_index))
1141                 {
1142                         string cmdstring;
1143                         cmdstring = bot_readcommand(this, this.bot_cmd_execution_index);
1144                         if(bot_decodecommand(cmdstring))
1145                         {
1146                                 bot_cmd.owner = this;
1147                                 bot_cmd.bot_cmd_index = this.bot_cmd_execution_index;
1148                         }
1149                         else
1150                         {
1151                                 // Invalid command, remove from queue
1152                                 bot_cmd = NULL;
1153                                 bot_dequeuecommand(this, this.bot_cmd_execution_index);
1154                                 this.bot_cmd_execution_index++;
1155                         }
1156                 }
1157                 else
1158                         bot_cmd = NULL;
1159         }
1160 }
1161
1162 void bot_resetqueues()
1163 {
1164         FOREACH_CLIENT(it.isbot, {
1165                 it.bot_cmd_execution_index = 0;
1166                 bot_clearqueue(it);
1167                 // also, cancel all barriers
1168                 it.bot_barrier = 0;
1169                 for(int i = 0; i < it.bot_places_count; ++i)
1170                 {
1171                         strfree(it.(bot_placenames[i]));
1172                 }
1173                 it.bot_places_count = 0;
1174         });
1175
1176         bot_barriertime = time;
1177 }
1178
1179 // Here we map commands to functions and deal with complex interactions between commands and execution states
1180 // NOTE: Of course you need to include your commands here too :)
1181 float bot_execute_commands_once(entity this)
1182 {
1183         float status, ispressingkey;
1184
1185         // Find command
1186         bot_setcurrentcommand(this);
1187
1188         // Ignore all commands except continue when the bot is paused
1189         if(!(this.bot_exec_status & BOT_EXEC_STATUS_PAUSED))
1190         {
1191                 // if we have no bot command, better return
1192                 // old logic kept pressing previously pressed keys, but that has problems
1193                 // (namely, it means you cannot make a bot "normal" ever again)
1194                 // to keep a bot walking for a while, use the "wait" bot command
1195                 if(bot_cmd == world)
1196                         return 0;
1197         }
1198         else if(bot_cmd.bot_cmd_type != BOT_CMD_CONTINUE)
1199         {
1200                 if(bot_cmd.bot_cmd_type!=BOT_CMD_NULL)
1201                 {
1202                         bot_command_executed(this, true);
1203                         LOG_INFO("WARNING: Commands are ignored while the bot is paused. Use the command 'continue' instead.");
1204                 }
1205                 return 1;
1206         }
1207
1208         // Keep pressing keys raised by the "presskey" command
1209         ispressingkey = boolean(bot_presskeys(this));
1210
1211         // Handle conditions
1212         if (!(bot_cmd.bot_cmd_type==BOT_CMD_FI||bot_cmd.bot_cmd_type==BOT_CMD_ELSE))
1213         if(this.bot_cmd_condition_status & CMD_CONDITION_true && this.bot_cmd_condition_status & CMD_CONDITION_false_BLOCK)
1214         {
1215                 bot_command_executed(this, true);
1216                 return -1;
1217         }
1218         else if(this.bot_cmd_condition_status & CMD_CONDITION_false && this.bot_cmd_condition_status & CMD_CONDITION_true_BLOCK)
1219         {
1220                 bot_command_executed(this, true);
1221                 return -1;
1222         }
1223
1224         // Map commands to functions
1225         switch(bot_cmd.bot_cmd_type)
1226         {
1227                 case BOT_CMD_NULL:
1228                         return ispressingkey;
1229                         //break;
1230                 case BOT_CMD_PAUSE:
1231                         status = bot_cmd_pause(this);
1232                         break;
1233                 case BOT_CMD_CONTINUE:
1234                         status = bot_cmd_continue(this);
1235                         break;
1236                 case BOT_CMD_WAIT:
1237                         status = bot_cmd_wait(this);
1238                         break;
1239                 case BOT_CMD_WAIT_UNTIL:
1240                         status = bot_cmd_wait_until(this);
1241                         break;
1242                 case BOT_CMD_TURN:
1243                         status = bot_cmd_turn(this);
1244                         break;
1245                 case BOT_CMD_MOVETO:
1246                         status = bot_cmd_moveto(this);
1247                         break;
1248                 case BOT_CMD_MOVETOTARGET:
1249                         status = bot_cmd_movetotarget(this);
1250                         break;
1251                 case BOT_CMD_RESETGOAL:
1252                         status = bot_cmd_resetgoal(this);
1253                         break;
1254                 case BOT_CMD_CC:
1255                         status = bot_cmd_cc(this);
1256                         break;
1257                 case BOT_CMD_IF:
1258                         status = bot_cmd_if(this);
1259                         break;
1260                 case BOT_CMD_ELSE:
1261                         status = bot_cmd_else(this);
1262                         break;
1263                 case BOT_CMD_FI:
1264                         status = bot_cmd_fi(this);
1265                         break;
1266                 case BOT_CMD_RESETAIM:
1267                         status = bot_cmd_resetaim(this);
1268                         break;
1269                 case BOT_CMD_AIM:
1270                         status = bot_cmd_aim(this);
1271                         break;
1272                 case BOT_CMD_AIMTARGET:
1273                         status = bot_cmd_aimtarget(this);
1274                         break;
1275                 case BOT_CMD_PRESSKEY:
1276                         status = bot_cmd_presskey(this);
1277                         break;
1278                 case BOT_CMD_RELEASEKEY:
1279                         status = bot_cmd_releasekey(this);
1280                         break;
1281                 case BOT_CMD_SELECTWEAPON:
1282                         status = bot_cmd_select_weapon(this);
1283                         break;
1284                 case BOT_CMD_IMPULSE:
1285                         status = bot_cmd_impulse(this);
1286                         break;
1287                 case BOT_CMD_BARRIER:
1288                         status = bot_cmd_barrier(this);
1289                         break;
1290                 case BOT_CMD_CONSOLE:
1291                         localcmd(strcat(bot_cmd.bot_cmd_parm_string, "\n"));
1292                         status = CMD_STATUS_FINISHED;
1293                         break;
1294                 case BOT_CMD_SOUND:
1295                         status = bot_cmd_sound(this);
1296                         break;
1297                 case BOT_CMD_DEBUG_ASSERT_CANFIRE:
1298                         status = bot_cmd_debug_assert_canfire(this);
1299                         break;
1300                 default:
1301                         LOG_INFOF("ERROR: Invalid command on queue with id '%s'", ftos(bot_cmd.bot_cmd_type));
1302                         return 0;
1303         }
1304
1305         if (status==CMD_STATUS_ERROR)
1306                 LOG_INFOF("ERROR: The command '%s' returned an error status", bot_cmd_string[bot_cmd.bot_cmd_type]);
1307
1308         // Move execution pointer
1309         if(status==CMD_STATUS_EXECUTING)
1310         {
1311                 return 1;
1312         }
1313         else
1314         {
1315                 if(autocvar_g_debug_bot_commands)
1316                 {
1317                         string parms;
1318
1319                         switch(bot_cmd_parm_type[bot_cmd.bot_cmd_type])
1320                         {
1321                                 case BOT_CMD_PARAMETER_FLOAT:
1322                                         parms = ftos(bot_cmd.bot_cmd_parm_float);
1323                                         break;
1324                                 case BOT_CMD_PARAMETER_STRING:
1325                                         parms = bot_cmd.bot_cmd_parm_string;
1326                                         break;
1327                                 case BOT_CMD_PARAMETER_VECTOR:
1328                                         parms = vtos(bot_cmd.bot_cmd_parm_vector);
1329                                         break;
1330                                 default:
1331                                         parms = "";
1332                                         break;
1333                         }
1334                         clientcommand(this,strcat("say ^7", bot_cmd_string[bot_cmd.bot_cmd_type]," ",parms,"\n"));
1335                 }
1336
1337                 bot_command_executed(this, true);
1338         }
1339
1340         if(status == CMD_STATUS_FINISHED)
1341                 return -1;
1342
1343         return CMD_STATUS_ERROR;
1344 }
1345
1346 // This function should be (the only) called directly from the bot ai loop
1347 int bot_execute_commands(entity this)
1348 {
1349         int f;
1350         do
1351         {
1352                 f = bot_execute_commands_once(this);
1353         }
1354         while(f < 0);
1355         return f;
1356 }