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