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