public static void moveToward(Unit unit, double x, double y, double range = 0, bool doDash = false) { if (unit.IsBusy || (unit.Moves == 0 && !doDash)) { return; } if (sunCollision(unit, x, y)) { var availableRoutePoints = AI.ROUTE_POINTS.Where(v => !sunCollision(unit, v.x, v.y)).ToArray(); if (availableRoutePoints.Length == 0) { Console.WriteLine("No available route points {0}", new Vector(unit.X, unit.Y)); return; } var bestRoutePoint = availableRoutePoints.MinByValue(v => Solver.distanceSquared(x - v.x, y - v.y)); moveToward(unit, bestRoutePoint.x, bestRoutePoint.y, 0, doDash); if (hasMovesE2(unit)) { moveToward(unit, x, y, range, doDash); } return; } var dx = x - unit.X; var dy = y - unit.Y; var distance = Solver.distance(dx, dy); if (inRangeE2(distance, range)) { return; } var netDistance = distance - range + ERROR2; if (unit.Moves > 0) { var magnitude = Math.Min(unit.Moves - ERROR, netDistance); unit.Move(unit.X + (dx / distance) * magnitude, unit.Y + (dy / distance) * magnitude); } if (doDash) { dx = x - unit.X; dy = y - unit.Y; distance = Solver.distance(dx, dy); if (inRangeE2(distance, range)) { return; } netDistance = distance - range + ERROR2; if (unit.canDash(netDistance)) { unit.Dash(unit.X + (dx / distance) * netDistance, unit.Y + (dy / distance) * netDistance); } } }
public static bool collision(double x1, double y1, double x2, double y2, double cx, double cy, double radius) { var v12 = new Vector(x2 - x1, y2 - y1); var v1C = new Vector(cx - x1, cy - y1); var radiusSquare = radius * radius; if (Solver.distanceSquared(cx - x1, cy - y1) <= radiusSquare) { return(true); } if (Solver.distanceSquared(cx - x2, cy - y2) <= radiusSquare) { return(true); } var unit12 = v12.unit(); var scalarProjection = v1C.dot(unit12); if (scalarProjection <= 0 || scalarProjection >= v12.length()) { return(false); } var projected = unit12.scale(scalarProjection); return(inRangeE1(Solver.distanceSquared(v1C.x - projected.x, v1C.y - projected.y), radiusSquare)); }
public double lengthSquared() { return(Solver.distanceSquared(x, y)); }