//It checks if ball is in a hole //Return Value : 1 if ball is in a hole; 0 if ball is not in any hole. private static int ball_in_hole(Ball ball, int hole_type = Holes.BORDERS_8GAME) { for (int i = 0; i < Holes.GetHoleNum(hole_type); i++) { if (Maths.vec_abs(Holes.GetHole(hole_type, i).pos - ball.r) < Holes.GetHole(hole_type, i).r *3) { return(i); } } return(-1); }
public static Vector3 ai_get_stroke_dir_8ball(Frame frame) { BALL_TYPE full_half = Player.ball_type; Vector3 r_hit; float angle; float minangle = Mathf.PI; float minangle_beta = Mathf.PI; Ball bhit, bcue; Hole hole; int minball = 0, minball_beta = 0; int minhole = -1, minhole_beta = -1; int i, j; Vector3 ai_err = Vector3.zero; if (ai_level == AI_LEVEL.LOW_LEVEL) { ai_err = Maths.vec_unit(new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f))) * Constant.HOLE1_R * 1.5f; //ai_err=Maths.vec_unit (new Vector3(1.0f,0,-1.0f))*Constant.HOLE1_R*1.5f; } else if (ai_level == AI_LEVEL.MEDIUM_LEVEL) { ai_err = Maths.vec_unit(new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f))) * Constant.HOLE1_R * 1.0f; } bcue = new Ball(frame.Balls[0]); for (i = 1; i < 16; i++) { if (frame.Balls[i].in_game) { if ((full_half == BALL_TYPE.BALL_HALF && frame.Balls[i].nr > 8) || (full_half == BALL_TYPE.BALL_FULL && frame.Balls[i].nr < 8) || (full_half == BALL_TYPE.BALL_ANY && frame.Balls[i].nr != 8) || (frame.Balls[i].nr == 8 && frame.Balls_in_game(full_half) == 0)) { bhit = new Ball(frame.Balls[i]); for (j = 0; j < Holes.GetHoleNum(Holes.BORDERS_8GAME); j++) { hole = Holes.GetHole(Holes.BORDERS_8GAME, j); r_hit = Maths.vec_unit(bhit.r - hole.aim) * (bcue.d + bhit.d) / 2.0f; r_hit = bhit.r + r_hit; if (!ball_in_way(0, r_hit, frame) && !ball_in_way(i, hole.aim, frame)) { angle = Mathf.Abs(Maths.vec_angle(r_hit - bcue.r, hole.aim - r_hit)); if (angle < minangle) { minball = i; minhole = j; minangle = angle; } } else if (!ball_in_way(0, r_hit, frame)) { angle = Mathf.Abs(Maths.vec_angle(r_hit - bcue.r, hole.aim - r_hit)); if (angle < minangle_beta) { minball_beta = i; minhole_beta = j; minangle_beta = angle; } } } } } } if (minball == 0) /* no proper ball found */ { minball = minball_beta; minhole = minhole_beta; minangle = minangle_beta; /* switch(full_half) { * case BALL_TYPE.BALL_FULL: * if( BallEvents.BM_get_balls_out_full()!=7 ){ * minball=1+my_rand(7-BallEvents.BM_get_balls_out_full()); * } * break; * case BALL_TYPE.BALL_HALF: * if( BallEvents.BM_get_balls_out_half()!=7 ){ * minball=1+my_rand(7-BallEvents.BM_get_balls_out_half()); * } * break; * case BALL_TYPE.BALL_ANY: * if( BallEvents.BM_get_balls_out_total()!=15 ){ * minball=1+my_rand(15-BallEvents.BM_get_balls_out_total()); * } * break; * } * if( minball==0 ){ * minball = ind_ball_nr(8,frame); * } else { * minball = nth_in_game(minball,frame,full_half); * }*/ } bhit = frame.Balls[minball]; if (minhole != -1) { hole = Holes.GetHole(Holes.BORDERS_8GAME, minhole); r_hit = Maths.vec_unit(bhit.r - hole.aim + ai_err * Maths.vec_abs(hole.aim - bhit.r) / 10.0f) * (bcue.d + bhit.d) / 2.0f; r_hit = bhit.r + r_hit - bcue.r; } else /* no proper ball found */ { r_hit = bhit.r - bcue.r; minhole = Random.Range(0, 6); } GamePlayUI.instance.SetAIPocket(minhole); return(Maths.vec_unit(r_hit)); }