Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. /**
  7.  * This code automatically collects game data in an infinite loop.
  8.  * It uses the standard input to place data into the game variables such as x and y.
  9.  * YOU DO NOT NEED TO MODIFY THE INITIALIZATION OF THE GAME VARIABLES.
  10.  **/
  11.  
  12. int main()
  13. {
  14.     int prev_x = 0;
  15.     int prev_y = 0;
  16.     int frame = 0;
  17.     int last_ram_frame = 0;
  18.    
  19.     // game loop
  20.     while (1) {
  21.         int ship_x, ship_y;
  22.         int cp_x, cp_y, cp_dist, cp_angle;
  23.         int opp_x, opp_y;
  24.         scanf("%d%d%d%d%d%d%d%d", &ship_x, &ship_y, &cp_x, &cp_y, &cp_dist, &cp_angle, &opp_x, &opp_y);
  25.        
  26.         // Don't care about which direction we're rotated
  27.         cp_angle = abs(cp_angle);
  28.        
  29.         // Opponent distance from me
  30.         int opp_dx_me = opp_x - ship_x;
  31.         int opp_dy_me = opp_y - ship_y;
  32.         int opp_dist_me = sqrt(opp_dx_me*opp_dx_me + opp_dy_me*opp_dy_me);
  33.        
  34.         // Opponent distance from my next checkpoint
  35.         int opp_dx_cp = opp_x - cp_x;
  36.         int opp_dy_cp = opp_y - cp_y;
  37.         int opp_dist_cp = sqrt(opp_dx_cp*opp_dx_cp + opp_dy_cp*opp_dy_cp);
  38.  
  39.         // My ship's velocity
  40.         int vx = ship_x - prev_x;
  41.         int vy = ship_y - prev_y;
  42.         int vel2 = vx*vx + vy*vy;
  43.  
  44.         int cp_radius = 600;  // Checkpoint radius
  45.         int slow_zone_radius = 1500;  // how close to next checkpoint to slow down
  46.         int boost_zone_radius = 10000;  // how far from next checkpoint to boost
  47.         bool on_course_slow_zone = cp_angle < 10;  // max angle to consider my ship head on when in slow zone
  48.         bool on_course_far_away = cp_angle < 30;  // max angle to consider my ship head on when far away
  49.         bool slow_zone = cp_dist < slow_zone_radius;
  50.         bool boost_zone = cp_dist > boost_zone_radius;
  51.         bool going_too_fast = vel2 > 300*300;  // empirical velocity "too fast" in slow zone (slow down to make turning around easier)
  52.        
  53.         int target_x = cp_x;
  54.         int target_y = cp_y;
  55.        
  56.         int thrust;
  57.         if (slow_zone) {
  58.             float slow_zone_scale = (float)cp_dist / (slow_zone_radius - cp_radius);
  59.             if (slow_zone_scale < 0.0f) slow_zone_scale = 0.0f;
  60.             if (on_course_slow_zone) {
  61.                 if (going_too_fast) {
  62.                     thrust = 15;
  63.                 } else {
  64.                     float by_dist = 30.0f + 30.0f * slow_zone_scale;
  65.                     thrust = (int)by_dist;
  66.                 }
  67.             } else {
  68.                 float by_dist = 10.0f + 10.0f * slow_zone_scale;
  69.                 thrust = (int)by_dist;
  70.             }
  71.         } else if (boost_zone && on_course_far_away) {
  72.             thrust = -1;
  73.         } else {
  74. #if 0
  75.             // RAM the opponent ship if:
  76.             // 1) I'm facing away from my next checkpoint
  77.             // 2) Enemy is within ram radius from me
  78.             // 3) Enemy is measurably farther away from next checkpoint than me
  79.             // 4) We haven't rammed opponent recently
  80.             bool opp_farther_away = opp_dist_cp > cp_dist;
  81.             if (frame - last_ram_frame > 2000 && opp_farther_away) {
  82.                 target_x = opp_x;
  83.                 target_y = opp_y;
  84.                 thrust = 100;
  85.                 fprintf(stderr, "!!! RAM !!!");
  86.                 last_ram_frame = frame;
  87.             } else {
  88.                 thrust = on_course_far_away ? 100 : 50;
  89.             }
  90. #else
  91.             thrust = on_course_far_away ? 100 : 50;
  92. #endif
  93.         }
  94.        
  95.         if (thrust >= 0) {
  96.             printf("%d %d %d\n", target_x, target_y, thrust);
  97.         } else {
  98.             printf("%d %d BOOST\n", target_x, target_y);
  99.         }
  100.         prev_x = ship_x;
  101.         prev_y = ship_y;
  102.         frame++;
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement