home *** CD-ROM | disk | FTP | other *** search
/ Revista CD Expert 51 / PCGamer51_17Demos.iso / games / colobotdemo / colobotdemo10e.exe / help / Twasp2.TXT < prev    next >
Text File  |  2001-07-03  |  3KB  |  85 lines

  1. \b;Objective
  2. Bring down the flying wasps in a more efficient way than with the previous program.
  3.  
  4. \b;Program
  5. Here is again the program of the previous exercise that shoots down all the wasps after many, many unsuccessful attempts:
  6. \c;
  7. \s;extern void object::Wasp1()
  8. \s;{
  9. \s;    \l;object\u cbot\type;    item;
  10. \s;    
  11. \s;    \l;aim\u cbot\aim;(0);
  12. \s;    
  13. \s;    \l;while\u cbot\while; (true)
  14. \s;    {
  15. \s;        \l;while\u cbot\while; (\l;radar\u cbot\radar;(AlienWasp, 0, 360, 0, 20) == null)
  16. \s;        {
  17. \s;            item = \l;radar\u cbot\radar;(AlienWasp);
  18. \s;            \l;turn\u cbot\turn;(\l;direction\u cbot\direct;(item.position));
  19. \s;            \l;motor\u cbot\motor;(1,1);
  20. \s;            
  21. \s;            \l;jet\u cbot\jet;(0);
  22. \s;            \l;if\u cbot\if; (position.z > item.position.z)
  23. \s;            {
  24. \s;                \l;jet\u cbot\jet;(-0.3);
  25. \s;            }
  26. \s;            
  27. \s;            \l;if\u cbot\if; (position.z < item.position.z - 1)
  28. \s;            {
  29. \s;                \l;jet\u cbot\jet;(0.3);
  30. \s;            }
  31. \s;            
  32. \s;            \l;wait\u cbot\wait;(0.2);
  33. \s;        }
  34. \s;        item = \l;radar\u cbot\radar;(AlienWasp);
  35. \s;        \l;turn\u cbot\turn;(\l;direction\u cbot\direct;(item.position));
  36. \s;        \l;fire\u cbot\fire;(1);
  37. \s;    }
  38. \s;}
  39. \n;
  40. The many failures are due to the fact that the wasp is already gone before the bullets can reach it. The only way to improve the program consists in setting the power of the two motors and of the jet in such a way that the bot follows the movement of the target during the burst.
  41.  
  42. Just before the shot, the program adjusts a last time the direction with \c;turn(direction(item.position));\n;. In order to follow the wasp during the burst, you have to "remember" the angle of this last rotation: if the angle was positive (rotation to the left), the bot must continue to turn left during the burst; if the angle was negative, the bot must continue to turn right.
  43.  
  44. In order to "remember" the angle of the last rotation, we need a variable that can contain just one number. If we choose to call it \c;angle\n;, we must define the variable with the following line at the beginning of the program:
  45. \c;
  46. \s;    \l;float\u cbot\type; angle;
  47. \n;
  48. The variable type \l;float\u cbot\type; is the variable type that can contain any number, i.e. whole numbers or real numbers. Please refer to the \l;text about variable types\u cbot\type; if you want to know more about the different types of variables and what they can contain.
  49.  
  50. Just before the instruction \c;fire(1);\n;, instead of writing \c;\l;turn\u cbot\turn;(\l;direction\u cbot\direct;(item.position));\n;, we will put the rotation angle into the variable \c;angle\n;:
  51. \c;
  52. \s;    angle = direction(item.position);
  53. \n;
  54. Then we perform the rotation, and we set the power of the motors so that the bot continues the movement:
  55. \c;
  56. \s;    turn(angle);
  57. \s;    if (angle < 0)
  58. \s;    {
  59. \s;        motor(1,0.5);
  60. \s;    }
  61. \s;    else
  62. \s;    {
  63. \s;        motor(0.5,1);
  64. \s;    }
  65. \n;
  66. The instruction \c;else\n; determines what instructions the program should execute if the condition stated in the \c;if\n; instruction is false.
  67.  
  68. Then we must set the power of the jet so that the bot follows the wasp also in the vertical direction:
  69. \c;
  70. \s;    jet(0);
  71. \s;    if(position.z > item.position.z)
  72. \s;    {
  73. \s;        jet(-0.3);
  74. \s;    }
  75. \s;    
  76. \s;    if(position.z < item.position.z - 1)
  77. \s;    {
  78. \s;        jet(0.3);
  79. \s;    }
  80. \n;
  81. As you will see, this program is much more efficient than the previous one!
  82.  
  83. \t;See also
  84. \l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
  85.