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