Updating movement
This commit is contained in:
@@ -462,7 +462,7 @@ public class ResourceService
|
||||
.ToListAsync();
|
||||
var persistedUnits = await _units.Find(x =>
|
||||
x.Username == username &&
|
||||
(x.UnitType == ArcherUnitType || x.UnitType == PeasantUnitType))
|
||||
(x.UnitType == ScoutUnitType || x.UnitType == ArcherUnitType || x.UnitType == PeasantUnitType))
|
||||
.ToListAsync();
|
||||
|
||||
var units = completedScouts
|
||||
@@ -511,23 +511,29 @@ public class ResourceService
|
||||
|
||||
var destinationQ = worker.DestinationQ.Value;
|
||||
var destinationR = worker.DestinationR.Value;
|
||||
var currentQ = worker.Q;
|
||||
var currentR = worker.R;
|
||||
var nextTick = worker.MovementCompletesAtUtc!.Value;
|
||||
while (nextTick <= now && HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
|
||||
if (HexDistance(worker.Q, worker.R, destinationQ, destinationR) > 1)
|
||||
{
|
||||
(currentQ, currentR) = NextStepToward(currentQ, currentR, destinationQ, destinationR);
|
||||
nextTick = nextTick.AddMinutes(1);
|
||||
}
|
||||
var nextStep = await NextPeasantStepTowardAsync(username, worker.Q, worker.R, destinationQ, destinationR);
|
||||
if (nextStep is null)
|
||||
{
|
||||
await _units.UpdateOneAsync(
|
||||
x => x.Id == worker.Id,
|
||||
Builders<UnitInstance>.Update
|
||||
.Set(x => x.Status, "Idle")
|
||||
.Unset(x => x.DestinationQ)
|
||||
.Unset(x => x.DestinationR)
|
||||
.Unset(x => x.MovementStartedAtUtc)
|
||||
.Unset(x => x.MovementCompletesAtUtc)
|
||||
.Set(x => x.UpdatedAtUtc, now));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (HexDistance(currentQ, currentR, destinationQ, destinationR) > 1)
|
||||
{
|
||||
await _units.UpdateOneAsync(
|
||||
x => x.Id == worker.Id,
|
||||
Builders<UnitInstance>.Update
|
||||
.Set(x => x.Q, currentQ)
|
||||
.Set(x => x.R, currentR)
|
||||
.Set(x => x.MovementCompletesAtUtc, nextTick)
|
||||
.Set(x => x.Q, nextStep.Value.q)
|
||||
.Set(x => x.R, nextStep.Value.r)
|
||||
.Set(x => x.MovementCompletesAtUtc, now.AddMinutes(1))
|
||||
.Set(x => x.UpdatedAtUtc, now));
|
||||
continue;
|
||||
}
|
||||
@@ -536,7 +542,7 @@ public class ResourceService
|
||||
x => x.OwnerUsername == username &&
|
||||
x.Q == destinationQ &&
|
||||
x.R == destinationR &&
|
||||
!string.IsNullOrWhiteSpace(x.BuildingType) &&
|
||||
(x.BuildingType == "LogCamp" || x.BuildingType == "Mine" || x.BuildingType == "Farm") &&
|
||||
x.BuildingStatus != "Building" &&
|
||||
(x.WorkerUnitId == null || x.WorkerUnitId == ""),
|
||||
Builders<Location>.Update
|
||||
@@ -561,13 +567,57 @@ public class ResourceService
|
||||
}
|
||||
}
|
||||
|
||||
private static (int q, int r) NextStepToward(int fromQ, int fromR, int toQ, int toR)
|
||||
private async Task<(int q, int r)?> NextPeasantStepTowardAsync(string username, int fromQ, int fromR, int toQ, int toR)
|
||||
{
|
||||
return NeighborCoordinates(fromQ, fromR)
|
||||
.OrderBy(x => HexDistance(x.q, x.r, toQ, toR))
|
||||
.ThenBy(x => x.q)
|
||||
.ThenBy(x => x.r)
|
||||
.First();
|
||||
var owned = await _locations.Find(x => x.OwnerUsername == username).ToListAsync();
|
||||
var ownedCoordinates = owned.Select(x => (x.Q, x.R)).ToHashSet();
|
||||
if (!ownedCoordinates.Contains((toQ, toR)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var start = (fromQ, fromR);
|
||||
var target = (toQ, toR);
|
||||
var queue = new Queue<(int q, int r)>();
|
||||
var previous = new Dictionary<(int q, int r), (int q, int r)>();
|
||||
var visited = new HashSet<(int q, int r)> { start };
|
||||
queue.Enqueue(start);
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var current = queue.Dequeue();
|
||||
if (current == target)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
foreach (var neighbor in NeighborCoordinates(current.q, current.r)
|
||||
.OrderBy(x => HexDistance(x.q, x.r, toQ, toR))
|
||||
.ThenBy(x => x.q)
|
||||
.ThenBy(x => x.r))
|
||||
{
|
||||
if (!ownedCoordinates.Contains(neighbor) || !visited.Add(neighbor))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
previous[neighbor] = current;
|
||||
queue.Enqueue(neighbor);
|
||||
}
|
||||
}
|
||||
|
||||
if (!visited.Contains(target))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var step = target;
|
||||
while (previous.TryGetValue(step, out var before) && before != start)
|
||||
{
|
||||
step = before;
|
||||
}
|
||||
|
||||
return step == start ? target : step;
|
||||
}
|
||||
|
||||
private static IEnumerable<(int q, int r)> NeighborCoordinates(int q, int r)
|
||||
|
||||
Reference in New Issue
Block a user