Josh's ULTRADUCK v2.0


SUBMITTED BY: Guest

DATE: Aug. 17, 2014, 11:11 p.m.

FORMAT: Text only

SIZE: 5.3 kB

HITS: 546

  1. // Humble Programmer's ULTRADUCK v2.0
  2. // For Blocky Games JSPond: https://blockly-games.appspot.com/pond-advanced?lang=en&level=10
  3. // Given a direction we are currently moving in (runDirection)
  4. // Choose a new direction to go in so that we make sure we're
  5. // not going to crash into a wall
  6. function chooseDirection(runDirection){
  7. // Get our current position:
  8. var x = loc_x();
  9. var y = loc_y();
  10. // Make sure values are between 0-360;
  11. // Convert values like -20 or 380 to
  12. // 340 or 20
  13. while(runDirection < 0)
  14. runDirection += 360;
  15. while(runDirection > 360)
  16. runDirection -= 360;
  17. if(x<30 && runDirection>90 && runDirection<270) {
  18. // If we're near the left edge and we're moving left
  19. // then turn 180 degrees so we don't hit the wall
  20. runDirection -= 180;
  21. } else if(x>70 && (runDirection<90 || runDirection>270)) {
  22. // If we're close to the right wall and we;re moving right
  23. // then our new direction should be the opposite way
  24. runDirection -= 180;
  25. }
  26. if(y>70 && runDirection>0 && runDirection<180) {
  27. // If we're near the top of the pond and moving up,
  28. // change directions and go down
  29. runDirection += 180;
  30. } else if(y<30 && runDirection>180 && runDirection<360) {
  31. // And the opposite if we're near the bottom
  32. runDirection -= 180;
  33. }
  34. return runDirection;
  35. }
  36. // This function returns the next angle to scan for enemies at
  37. // This is where the heart of the target acquision is
  38. // This is a closure: It has variables within it that hold their
  39. // valuse between calls to the function
  40. var nextScanAngle = (function(){
  41. // Closure variables: These will keep their state after calls
  42. // to nextScanAngle()
  43. // How many degrees to scan by on each pass
  44. var scanRate = 10;
  45. // What angle we're using as our base angle
  46. var baseAngle = 0;
  47. // iterator, to keep track of if this is an even/odd call
  48. var i = 0;
  49. // The actual function logic:
  50. return function() {
  51. // We want to alternate scanning forward and back, adjusting by
  52. // scanRate degrees each time. We use our itterator to do so.
  53. // Flip the iterator between 1 or 0:
  54. if((i = (i+1)%2)) {
  55. // If it's 1, return the opposite of the base angle
  56. return baseAngle + 180;
  57. } else {
  58. // If it's 0, adjust the base angle by the scan rate and return that
  59. return baseAngle+=scanRate;
  60. }
  61. };
  62. })();
  63. // Main program's variables:
  64. // Angle we're looking for enemies at:
  65. var angle = 0;
  66. // Distance to the nearest enemy:
  67. var distance = Infinity;
  68. // How much health we had last time we checked and if we got hurt
  69. var lastHealth = health(), gotHurt = false;
  70. // Are we running away, and in what direction?
  71. var running = 0, runDirection = 0;
  72. // How fast to run away when scared
  73. var runSpeed = 100;
  74. // Main program:
  75. while(1){
  76. // Have we been hit?
  77. if (health() < lastHealth) {
  78. // If so, choose a random direction and run for 25 loops
  79. runDirection = Math.random()*360;
  80. running = 25;
  81. // Make a note of our current health because we've been hurt
  82. lastHealth = health();
  83. }
  84. // If we're running away
  85. if(running) {
  86. // Make sure the direction we're running is a good choice,
  87. // and then swim in that direction
  88. runDirection = chooseDirection(runDirection);
  89. swim(runDirection,runSpeed);
  90. // While we're running away we might as well look for targets
  91. distance = scan(angle,20);
  92. if(distance == Infinity) {
  93. // We couldn't find a target at our last angle, so get a new angle
  94. angle = nextScanAngle();
  95. } else if(distance<40) {
  96. // We have a nearby enemy, so shoot at them!
  97. cannon(angle,distance);
  98. }
  99. // Decrement our running counter, when it hits zero we stop running
  100. running--;
  101. } else {
  102. // We're not running away, so let's find a target!
  103. // Start scanning and keep looking until we find a target, or get hit
  104. do {
  105. // Get our next scan angle, and see if we have a target
  106. angle = nextScanAngle();
  107. distance = scan(angle,10);
  108. // check to see if we got hurt
  109. gotHurt = health() < lastHealth
  110. } while(distance == Infinity && !gotHurt);
  111. // If we got hurt, restart the main loop so we start running away
  112. if(gotHurt)
  113. continue;
  114. // If we found a target that's far away
  115. if(distance>60) {
  116. // Swim towards the target
  117. swim(angle,100);
  118. } else {
  119. // Otherwise, stop moving (because we might have inerta)
  120. stop();
  121. }
  122. // Now as long as our target is within range and we're not taking damage
  123. while(((distance = scan(angle,10)) < 70) && health() == lastHealth) {
  124. // FIRE!!!
  125. cannon(angle,distance);
  126. // If we're not close to the targetm swim at them. Otherwise stop
  127. if(distance>5)
  128. swim(angle);
  129. else
  130. stop();
  131. }
  132. }
  133. }

comments powered by Disqus