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