private RobotAction[] Expand(RobotAction action) { if (action == RobotAction.Go && (!Map.Explored(Position) || Map[Position] == MapTileResult.LeverA || Map[Position] == MapTileResult.LeverB)) { // reshitch lever return new[] { action, RobotAction.Left, RobotAction.Left, RobotAction.Go, RobotAction.Left, RobotAction.Left, RobotAction.Go }; } else return new[] { action }; }
public RobotResult Move(RobotAction action) { RobotResult r = RobotResult.Ok; if (action == RobotAction.Go) { if (Path.Count != 0 && Path.Last() == (Rotation)(((byte)Rot + 2) % 4)) { Path.RemoveAt(Path.Count - 1); } else { Path.Add(Rot); } } foreach (var a in Expand(action)) { r = MoveInternal(a); } Moved?.Invoke(); return r; }
public override void GoBack(double Distance) { //验证 _curAction = new RobotAction(RobotActionType.GoBack, Distance, null, null); }
public void CancelAction() { _curAction = null; _curActionSteps.Clear(); }
public void TurnRight(double Angle) { //验证 _curAction = new RobotAction(RobotActionType.TurnRight, Angle, null, null); }
/// <summary> /// Create a instruction /// </summary> /// <param name="actionType">the actionType</param> /// <param name="action">the action</param> /// <returns>the created instruction</returns> private Instruction <RobotAction> CreateInstruction(RobotActionType actionType, RobotAction action) { var instruction = new Instruction <RobotAction>(); instruction.ActionType = actionType; instruction.Parameters = action; return(instruction); }
private void MoveResult(RobotAction action) { if (action == RobotAction.Left) { Rot = (Rotation)(((byte)Rot + 3) % 4); } else if (action == RobotAction.Right) { Rot = (Rotation)(((byte)Rot + 1) % 4); } else if (action == RobotAction.Go) { Position = Position.Move((byte)Rot); } }
public RobotActionResult Execute(int number, RobotAction action) { writer.Write(number + " " + (char)action + '\n'); writer.Flush(); return Parse(reader.ReadLine()); }
private bool CountAction(RoundConfig config, RobotState selfRobot, RobotAction action, decimal step, decimal attackRadius, decimal attackPower, List <RobotState> nearRobots) { bool foundAlienToAtack = false; foreach (RobotState nrb in nearRobots) { if (nrb.defence <= attackPower) { decimal dist = GetDistance(nrb.X, nrb.Y, selfRobot.X, selfRobot.Y); decimal currentStep = 0; if (dist > attackRadius) { decimal dtoa = dist - attackRadius; if (step > dtoa) { currentStep = step - dtoa; } else { currentStep = step; } } if (currentStep > 0) { Coord deltaCoord = GetCoordXYStepToPoint(new Point() { X = nrb.X, Y = nrb.Y }, selfRobot, currentStep); bool checkStep = CheckStepFieldBorders(selfRobot, config, deltaCoord); decimal diff = step - currentStep; if (diff > 0) { decimal energyPart = selfRobot.energy / (decimal)config.max_energy; decimal healthPart = (config.max_radius * selfRobot.speed) / (decimal)config.max_health; decimal necessaryV = (config.max_health * (attackRadius - diff)) / (decimal)(energyPart * config.max_radius); } if (checkStep) { action.dX = deltaCoord.X; action.dY = deltaCoord.Y; action.targetId = nrb.id; foundAlienToAtack = true; break; } else { action.dY = 0; } } else { action.dX = 0; action.dY = 0; action.targetId = nrb.id; foundAlienToAtack = true; break; } } } return(foundAlienToAtack); }
private ActionResult TakeAction(RobotAction whatToDo, Board board) { try { switch (whatToDo) { case RobotAction.StayPut: return ActionResult.DidNothing; break; case RobotAction.MoveEast: { board.Move(this, Board.Direction.East); return ActionResult.SuccessfulMove; break; } case RobotAction.MoveNorth: { board.Move(this, Board.Direction.North); return ActionResult.SuccessfulMove; break; } case RobotAction.MoveSouth: { board.Move(this, Board.Direction.South); return ActionResult.SuccessfulMove; break; } case RobotAction.MoveWest: { board.Move(this, Board.Direction.West); return ActionResult.SuccessfulMove; break; } case RobotAction.PickUpCan: { var pos = board.GetCurrentPosition(this); var can = board.Contents(pos).OfType<Rubbish>().FirstOrDefault(); if (can == null) return ActionResult.TriedToPickupCanWhereThereWasNoCan; Pickup(board, can); return ActionResult.PickedUpCan; break; } case RobotAction.MoveRandom: { var randomDirection = (Board.Direction) random.Next(0, 4); board.Move(this, randomDirection); return ActionResult.SuccessfulMove; break; } default: throw new ArgumentException("Nothing specified for action", "whatToDo"); } } catch (InvalidMoveException e) { return ActionResult.HitWall; } }
public RobotAction Tick(int robotId, RoundConfig config, GameState state) { RobotState self = state.robots[robotId]; RobotAction action = new RobotAction(); action.targetId = -1; if (self.energy > 0 && self.isAlive) { int max_distance_attack = (int)Math.Round(10 * (float)config.max_radius * (float)self.speed / (float)config.max_health * (float)self.energy / (float)config.max_energy); int health = self.attack + self.defence + self.speed; int enemy_id = -1; if (self.energy > 0.7 * config.max_energy && health > 0.7 * config.max_health) { for (int id = 0; id < state.robots.Count; id++) { RobotState rs = state.robots[id]; if (rs.name != self.name && rs.isAlive) { int enemy_distance_attack = (int)Math.Round(10 * (float)config.max_radius * (float)rs.speed / (float)config.max_health * (float)rs.energy / (float)config.max_energy); int distance = CalcDistance(self.X, self.Y, rs.X, rs.Y); if (distance <= enemy_distance_attack && distance <= max_distance_attack) { enemy_id = id; } } } } if (enemy_id > 0) { HealthRedestribution(self, config, action, 0.4f, 0.4f, 0.2f); action.targetId = enemy_id; MoveTo(robotId, config, state, action, state.robots[enemy_id].X, state.robots[enemy_id].Y); } else { bool attack_mode = false; HealthRedestribution(self, config, action, 0.0f, 0.4f, 0.6f); if (self.energy < 0.95 * config.max_energy) { Point target = GetNearestStation(robotId, config, state, PointType.Energy); MoveTo(robotId, config, state, action, target.X, target.Y); } else { if (health < 0.8 * config.max_health) { Point target_station = GetNearestStation(robotId, config, state, PointType.Health); int X = target_station.X; int Y = target_station.Y; int target_robot_id = -1; if (health > 0.4 * config.max_health) { int target_robot_dist = config.width * config.height; for (int id = 0; id < state.robots.Count; id++) { RobotState rs = state.robots[id]; if (rs.isAlive == false && (rs.attack + rs.defence + rs.speed) > 0.4 * config.max_health) { int distance = CalcDistance(self.X, self.Y, rs.X, rs.Y); if (distance < target_robot_dist) { target_robot_id = id; target_robot_dist = distance; } } } } if (target_robot_id >= 0) { RobotState taregt_robot = state.robots[target_robot_id]; if (CalcDistance(self.X, self.Y, X, Y) > CalcDistance(self.X, self.Y, taregt_robot.X, taregt_robot.Y)) { X = taregt_robot.X; Y = taregt_robot.Y; } } MoveTo(robotId, config, state, action, X, Y); } else { int targetId = GetNearestRobot(robotId, config, state); if (targetId >= 0) { attack_mode = true; RobotState target = state.robots[targetId]; MoveTo(robotId, config, state, action, target.X, target.Y); int distance = CalcDistance(self.X + action.dX, self.Y + action.dY, target.X, target.Y); if (distance <= max_distance_attack) { action.targetId = targetId; } } } } if (!attack_mode) { int target_id = -1; for (int id = 0; id < state.robots.Count; id++) { RobotState rs = state.robots[id]; if (rs.name != self.name) { int distance = CalcDistance(self.X + action.dX, self.Y + action.dY, rs.X, rs.Y); if (distance <= max_distance_attack) { target_id = id; } } } if (target_id >= 0) { action.targetId = target_id; } } } } return(action); }
public RobotAction Tick(int robotId, RoundConfig config, GameState state) { RobotState self = state.robots[robotId]; RobotAction action = new RobotAction(); action.targetId = -1; if (self.energy > 0 && self.isAlive) { // find nearest energy station int pointId = 0; int pointDist = config.height * config.width; for (int ptId = 0; ptId < state.points.Count; ptId++) { Point pt = state.points[ptId]; if (pt.type == PointType.Energy) { int ptDist = CalcDistance(self.X, self.Y, pt.X, pt.Y); if (ptDist < pointDist) { pointDist = ptDist; pointId = ptId; } } } Point targetPoint = state.points[pointId]; int maxDistance = 10 * config.max_speed * self.speed / config.max_health * self.energy / config.max_energy; if (maxDistance > 0) { if (pointDist <= maxDistance) { action.dX = targetPoint.X - self.X; action.dY = targetPoint.Y - self.Y; } else { int steps = pointDist / maxDistance + 1; action.dX = (targetPoint.X - self.X) / steps; action.dY = (targetPoint.Y - self.Y) / steps; } } // defense int targetId = -1; int targetDist = config.width * config.height; for (int rsId = 0; rsId < state.robots.Count; rsId++) { RobotState rs = state.robots[rsId]; int rsDist = CalcDistance(self.X, self.Y, rs.X, rs.Y); if (rs.name != self.name && rs.energy > 0 && rsDist < targetDist) { targetDist = rsDist; targetId = rsId; } } int maxDistanceAttack = 10 * config.max_radius * self.speed / config.max_health * self.energy / config.max_energy; if (targetDist <= maxDistanceAttack) { action.targetId = targetId; } } return(action); }
void Start() { // Set the default method for the delegate myRobotAction = RobotWalk; }
// public method to tell the robot to walk public void DoRobotWalk() { // set the delegate method to the walk function myRobotAction = RobotWalk; }
// public method to tell the robot to run public void DoRobotRun() { // Set the delegate method to the run function myRobotAction = RobotRun; }
public void DoRobotWalk() { myRobotAction = RobotWalk; }
void Start() { myRobotAction = RobotWalk; }
public RobotAction Tick(int robotId, RoundConfig config, GameState state) { RobotState selfRobot = state.robots[robotId]; RobotAction action = new RobotAction(); if (!selfRobot.isAlive) { return(action); } List <string> friendRobots = new List <string>() { "Ryzhov", "Haritonov", "Nikandrov", "Sinyavsky", "Frolov", "Orlov", "Kamshilov" }; decimal MIN_ENERGY_TO_GO_RELOAD = 0.85m; decimal MIN_HEALTH_TO_GO_RELOAD = 0.85m; decimal MAX_ATTACK_DISTANCE_RATIO = 30m; List <Point> healthPoints = state.points.Where(p => p.type == PointType.Health).ToList(); List <Point> energyPoints = state.points.Where(p => p.type == PointType.Energy).ToList(); bool hasNearestHealth = true; bool hasNearestEnergy = true; Point nearestHealth = null; decimal minDistH = 10000000m; int indexH = -1; for (int i = 0; i < healthPoints.Count; i++) { Point p = healthPoints[i]; decimal dist = GetDistance(p.X, p.Y, selfRobot.X, selfRobot.Y); if (dist < minDistH) { minDistH = dist; indexH = i; } } if (indexH >= 0) { nearestHealth = healthPoints[indexH]; } else { hasNearestHealth = false; } Point nearestEnergy = null; decimal minDistE = 10000000m; int indexE = -1; for (int i = 0; i < energyPoints.Count; i++) { Point p = energyPoints[i]; decimal dist = GetDistance(p.X, p.Y, selfRobot.X, selfRobot.Y); if (dist < minDistE) { minDistE = dist; indexE = i; } } if (indexE >= 0) { nearestEnergy = energyPoints[indexE]; } else { hasNearestEnergy = false; } decimal energyRest = selfRobot.energy / (decimal)config.max_energy; decimal healthRest = (selfRobot.speed + selfRobot.defence + selfRobot.attack) / (decimal)config.max_health; decimal step = GetMaxCurrentStep(selfRobot, config); decimal attackRadius = GetMaxCurrentAttackRadius(selfRobot, config); decimal attackPower = GetMaxCurrentAttackPower(selfRobot, config); if ((energyRest <= MIN_ENERGY_TO_GO_RELOAD && hasNearestEnergy) || (healthRest <= MIN_HEALTH_TO_GO_RELOAD && hasNearestHealth) || !done) { int dx = 0; int dy = 0; done = false; if (energyRest >= 0.99m || healthRest >= 0.99m) { done = true; } if (healthRest <= MIN_HEALTH_TO_GO_RELOAD && nearestHealth != null) { Coord deltaCoord = GetCoordXYStepToPoint(nearestHealth, selfRobot, step); if (Math.Abs(selfRobot.Y - nearestHealth.Y) <= 1) { deltaCoord.Y = (selfRobot.Y - nearestHealth.Y) * -1; } if (Math.Abs(selfRobot.X - nearestHealth.X) <= 1) { deltaCoord.X = (selfRobot.X - nearestHealth.X) * -1; } bool checkStep = CheckStepFieldBorders(selfRobot, config, deltaCoord); if (checkStep) { action.dX = deltaCoord.X; action.dY = deltaCoord.Y; action.targetId = state.robots[robotId > 0 ? 0 : 1].id; } } else if (energyRest <= MIN_ENERGY_TO_GO_RELOAD && nearestEnergy != null) { Coord deltaCoord = GetCoordXYStepToPoint(nearestEnergy, selfRobot, step); if (Math.Abs(selfRobot.Y - nearestEnergy.Y) <= 1) { deltaCoord.Y = (selfRobot.Y - nearestEnergy.Y) * -1; } if (Math.Abs(selfRobot.X - nearestEnergy.X) <= 1) { deltaCoord.X = (selfRobot.X - nearestEnergy.X) * -1; } bool checkStep = CheckStepFieldBorders(selfRobot, config, deltaCoord); if (checkStep) { action.dX = deltaCoord.X; action.dY = deltaCoord.Y; action.targetId = state.robots[robotId > 0 ? 0 : 1].id; } } } else { decimal distToNearestAlienRobot = 10000000; List <RobotState> nearRobots = new List <RobotState>(); foreach (RobotState item in state.robots) { if (selfRobot.id != item.id && !friendRobots.Contains(item.name)) { decimal dist = GetDistance(item.X, item.Y, selfRobot.X, selfRobot.Y); if (dist <= attackRadius * MAX_ATTACK_DISTANCE_RATIO) { nearRobots.Add(item); } } } nearRobots = nearRobots.Where(r => r.isAlive == true).OrderBy(r => r.defence).ToList(); bool foundAlienToAtack = false; int prevA = selfRobot.attack; int prevD = selfRobot.defence; foundAlienToAtack = CountAction(config, selfRobot, action, step, attackRadius, attackPower, nearRobots); if (!foundAlienToAtack) { RobotState ar = GetNearestAlienRobotId(state, selfRobot, friendRobots); Coord deltaCoord = GetCoordXYStepToPoint(new Point() { X = ar.X, Y = ar.Y }, selfRobot, step); bool checkStep = CheckStepFieldBorders(selfRobot, config, deltaCoord); if (checkStep) { action.dX = deltaCoord.X; action.dY = deltaCoord.Y; action.targetId = ar.id; } else { action.dX = 0; } } } battlestep++; return(action); }
private void MoveBuf(RobotAction action) { if (BufferLocation == null) BufferLocation = new BufInfo() { Position = Position, Rot = Rot }; BufferLocation.Actions.Add(action); MoveResult(action); }
private IDictionary<Situation, RobotAction> GetSimpleStrategy(RobotAction robotAction) { var strategy = new Dictionary<Situation, RobotAction>(); var contentPossibilities = Enum.GetValues(typeof (Situation.BoardContents)).Cast<Situation.BoardContents>().ToArray(); foreach (var north in contentPossibilities) { foreach (var south in contentPossibilities) { foreach (var east in contentPossibilities) { foreach (var west in contentPossibilities) { foreach (var current in contentPossibilities) { Situation s = new Situation(current, north, south, east, west); strategy[s] = robotAction; } } } } } return strategy; }
/// <summary> /// 系统回合 /// </summary> public override void OnSysTurn() { if (_curActionSteps.Count > 0) { RobotStatus rs = _curActionSteps.Dequeue(); //设定机器人当前状态 X = rs.X; Y = rs.Y; Angle = rs.Angle; //AddFrameToMatrix(_world.Env.Ticket, rs); } else { _curAction = null; } }
/// <summary> /// 停止当前正在执行的动作 /// </summary> public override void Stop() { _curAction = null; _curActionSteps.Clear(); }
private RobotResult MoveInternal(RobotAction action) { //if (action != RobotAction.Go // && Map.Explored(Position.Move(0)) // && Map.Explored(Position.Move(1)) // && Map.Explored(Position.Move(2)) // && Map.Explored(Position.Move(3)) // ) //{ // MoveBuf(action); // return RobotResult.Ok; //} //var p = Position.Move((int)Rot); //if (Map.Explored(p) && action == RobotAction.Go) //{ // var t = Map[p]; // if (t.IsFree() // && Map.Explored(p.Move((int)Rot + 3)) // && Map.Explored(p.Move((int)Rot)) // && Map.Explored(p.Move((int)Rot + 1)) // ) // { // MoveBuf(RobotAction.Go); // return RobotResult.Ok; // } // else if (t == MapTileResult.Wall) // { // return RobotResult.GoDenied; // } //} Flush(); var result = Client.Execute(RobotId, action); if (result.Result == RobotResult.Ok) { MoveResult(action); } SaveResults(result); return result.Result; }
/// <summary> /// Charge les informations de la substrategy à partir du fichier spécifié en paramètre /// Le chargement d'un fichier détruit toutes les données précédemment sauvegardée /// </summary> /// <param name="filename">Nom du fichier pour charger le fichier</param> /// <param name="forcedID">ID à utiliser à la place de l'ID du fichier</param> /// <returns>Retourne le nombre d'action chargée, -1 en cas d'erreur</returns> public int Load(String filename, int forcedID) { int Ret = -1; StructuredFile inputFile = new StructuredFile(); StructuredFileGroup globalGroup = null; List <StructuredFileGroup> actionsGroup = null; StructuredFileKey currentKey = null; RobotAction currentAction = null; if (filename != null) { // Ouverture du fichier inputFile.Load(filename); // Destruction des données actuelles _subStrategyName = "Not Set"; _actions = null; // Chargement des données génériques globalGroup = inputFile.GetGroupByID(0); if (globalGroup != null) { currentKey = globalGroup.GetFirstKey("SubStrategyName"); if (currentKey != null) { _subStrategyName = currentKey.valueString; } if (forcedID > 0) { // Un ID a été imposé, nous l'utilisons _subStrategyID = forcedID; } else { // Nous utilisons l'ID du fichier currentKey = globalGroup.GetFirstKey("SubStrategyID"); if (currentKey != null) { _subStrategyID = currentKey.valueInt; } } } // Chargement des données liées aux actions actionsGroup = inputFile.GetAllGroup(); if (actionsGroup != null) { Ret = 0; // Lecture de tous les groupes pour extraire les données liées aux actions foreach (StructuredFileGroup currentGroup in actionsGroup) { // Nous ne devons extraire que les données des groupes > à 0 if (currentGroup.ID > 0) { currentAction = new RobotAction(); currentAction.Import(currentGroup); // On ajoute l'objet que l'on vient d'importer s'il est valide if (currentAction.cmd != EnumCmd.NotSet) { if (_actions == null) { _actions = new List <RobotAction>(); } // Verification des ID currentAction.ID = CheckID(currentAction.ID); currentAction.nextID = CheckLinkID(currentAction.nextID); currentAction.timeoutID = CheckLinkID(currentAction.timeoutID); _actions.Add(currentAction); Ret = Ret + 1; } } } } } SortRobotActions(); return(Ret); }