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