home *** CD-ROM | disk | FTP | other *** search
/ 300 Favorite Games / 300GAMES.iso / 204 / corners.prg < prev    next >
Text File  |  1995-08-14  |  4KB  |  176 lines

  1. #############################################################################
  2. #                               SMART CORNER                                #
  3. #                                                                           #
  4. # This robot demonstrates the ability to learn over a match. This robot     # 
  5. # changes its behavior from one game to the next depending upon how it did  #
  6. # in the previous game. An even smarter robot might modify its behavior by  #
  7. # tracking all of the games it has played in during a match. There are many #
  8. # other statistical functions available that this robot does not even use!  #
  9. #                                                                           #
  10. #############################################################################
  11.  
  12.  
  13. Init
  14. {
  15.     Name( "Smart Corner" )
  16.     
  17.     # Figure out how many robots are in the game!
  18.     GetOthers( )
  19.     others = result
  20.     
  21.     # Set detection section to "Travel Mode"
  22.     RegDtcRobot( TrvFoundRobot, 1 )
  23.     RegCore( MyCore )
  24.     RegAScan( AutoScan, 3 )
  25.  
  26.     
  27.     Gosub( GoCorner )
  28.  
  29.     # Don't want to shot cookies on our way to the
  30.     # corner so register the detection handler here
  31.     RegDtcCookie( FoundCookie, 2 )
  32. }
  33.  
  34. MyCore
  35. {
  36.     Scan()
  37.     GunLeft( guninc )
  38. }  
  39.  
  40. GoCorner
  41. {
  42.     LockAll( ON )
  43.  
  44.     # Go to the corner determined by our stored value.
  45.     # Will always be upper left for first game of match.
  46.     # Hitting a wall will stop the robot, but causes no damage
  47.  
  48.     BodyRight( corner )
  49.     Ahead( 400 )
  50.     BodyLeft( 90 )
  51.     Ahead( 400 )
  52.  
  53.     # Change robot detection section to "Corner Mode"
  54.     RegDtcRobot( CrnFoundRobot, 1 )
  55.  
  56.     LockAll( OFF )
  57.     LockGun( ON )
  58.     GunLeft( 90 )
  59.     
  60.     # Calculate the "ends" of the robot's radar sweep
  61.  
  62.     gun_start = gunaim
  63.     
  64.     if( gunaim == 0 )
  65.         gun_end = 270
  66.     else
  67.         gun_end = gunaim - 90
  68.     endif
  69.         
  70.     # Registering event may seem a bit over-kill. This could be
  71.     # done with if statements in MyCore, but this is more interesting!
  72.     # Notice that having two events with the same priority produces 
  73.     # unknow results only when it is possible for both events to occur
  74.     # at the same time.
  75.     
  76.     RegCustom( GunAtStart, 3, gunaim == gun_start )
  77.     RegCustom( GunAtEnd, 3, gunaim == gun_end )
  78.     
  79. }
  80.  
  81. GunAtStart
  82. {
  83.     # Event is called when we hit gun_start.
  84.     # scan() now so we check along the gun_start angle.
  85.  
  86.     Scan()
  87.     
  88.     # Since we used == in our events above, the guninc value
  89.     # must causes us to hit gun_start and gun_end exactly!
  90.  
  91.     guninc = 3
  92.  
  93.     # If we don't change our gunaim, this event will never end!
  94.  
  95.     GunLeft( 3 )
  96. }
  97.  
  98. GunAtEnd
  99. {
  100.     Scan()
  101.     guninc = -3
  102.     GunRight( 3 )
  103. }
  104.  
  105. AutoScan
  106. {
  107.     Scan( )
  108. }
  109.  
  110. # This section is used while sitting in a corner
  111. # If another robot is detected fire until we don't see it.
  112.  
  113. CrnFoundRobot
  114. {
  115.     Gosub( Fire )
  116.     Scan( )
  117. }
  118.  
  119. # This section is used while traveling to a corner
  120. # If another robot is detected, stop, fire until gone,
  121. # then finish moving to the wall
  122.  
  123. TrvFoundRobot
  124. {
  125.     # Calling stop may times does not over-write continue info.
  126.     stop( )
  127.     Gosub( Fire )
  128.     Scan( )
  129.     
  130.     if( dtcrobot == 0 )
  131.         # Since the command moves us all the way to the wall,
  132.         # we will not be able to detect another robot. Check
  133.         # out the blocking() command for a better solution.
  134.         Continue( )
  135.     endif
  136. }
  137.  
  138. FoundCookie
  139. {
  140.     Fire( 1 )
  141. }
  142.  
  143. Fire
  144. {
  145.     if( scandist > 200 OR energy < 15 )
  146.         Fire( 1 )
  147.     elseif( scandist > 100 )
  148.         Fire( 3 )
  149.     elseif( scandist > 50 )
  150.         Fire( 5 )
  151.     else( )
  152.         Fire( 7 )
  153.     endif
  154. }
  155.  
  156. Dead
  157. {
  158.     GetOthers( )
  159.     
  160.     # Lets see how we did. This allows the robot to adapt
  161.     # to its enemies. If we were not in the top 75 percent,
  162.     # switch to another corner. The first if() is needed to prevent
  163.     # division by zero when we are the only player in a game.
  164.  
  165.     if( others )
  166.         if( (others - result) / others < 0.75 )
  167.             corner = corner + 90
  168.             if( corner == 270 )
  169.                 corner = -90
  170.             endif
  171.         endif
  172.     endif
  173.     
  174.     Store( corner )
  175. }
  176.