public async Task ConnectAsync(CancellationToken cancellationToken = default) { await _bot.ConnectAsync(); _tokenSource = new CancellationTokenSource(); Task = Task.Run(async() => { // Spin around await _bot.StartMovementAsync(Movement.RotatingRight); while (true) { if (_tokenSource.Token.IsCancellationRequested) { break; } // And shoot await _bot.FireAsync(); await Task.Delay(100); } }, _tokenSource.Token); }
public async Task WanderAsync(UpdateContext data) { var movement = data.YourShip.Movement; if (!movement.Moving.Forward) { await _bot.StartMovementAsync(Movement.Forward); } // Used to convert the calculated ship rotation to a usable 0-360 rotation CurrentRotation = ResolveCurrentRotation(data.YourShip); // Too close to left side if (movement.Position.X - DISTANCE_AWAY_FROM_BOUNDARY < 0 && ((CurrentRotation <= 269 && CurrentRotation >= 91) || TargetRotation != -1)) { await DetermineRotationAsync(data, 180, 270, 359, 0, 90); } else if (movement.Position.Y - DISTANCE_AWAY_FROM_BOUNDARY < 0 && (CurrentRotation <= 179 || TargetRotation != -1)) // To Close to top { await DetermineRotationAsync(data, 90, 180, 270, 270, 360); } else if (movement.Position.X + DISTANCE_AWAY_FROM_BOUNDARY > data.MapWidth && (CurrentRotation <= 91 || CurrentRotation >= 271 || TargetRotation != -1)) // To close to right { // Crosses origin so we need to calculate in line // If we're not already moving away from the boundary if (!movement.Moving.RotatingRight && !movement.Moving.RotatingLeft) { if (CurrentRotation >= 270) // Rotate Right { await _bot.StopMovementAsync(Movement.RotatingLeft); await _bot.StartMovementAsync(Movement.RotatingRight); TargetRotation = _gen.Next(180, 270); } else if (CurrentRotation <= 90) { await _bot.StopMovementAsync(Movement.RotatingRight); await _bot.StartMovementAsync(Movement.RotatingLeft); TargetRotation = _gen.Next(90, 180); } } else { if (Math.Abs(CurrentRotation - TargetRotation) <= ROTATION_BUFFER) { await _bot.StopMovementAsync(Movement.RotatingLeft); await _bot.StopMovementAsync(Movement.RotatingRight); TargetRotation = -1; } } } else if (movement.Position.Y + DISTANCE_AWAY_FROM_BOUNDARY > data.MapHeight && (CurrentRotation >= 181 || TargetRotation != -1)) // To close to bottom { await DetermineRotationAsync(data, 270, 90, 180, 0, 90); } else { await _bot.StopMovementAsync(Movement.RotatingLeft); await _bot.StopMovementAsync(Movement.RotatingRight); TargetRotation = -1; } }