diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-04 10:38:55 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-04 10:38:55 +0200 |
| commit | 525dd3c3990ee8365e08be59a48df1f51a42b638 (patch) | |
| tree | 3e3170a24e9c0399596b649398ca90be4efb3a26 | |
| parent | 0a64a4bdb028449bc3852887b1f54f397e40cf90 (diff) | |
Find a ne wpath to resource when blocked
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | gather_order.go | 84 | ||||
| -rw-r--r-- | move.go | 46 |
3 files changed, 81 insertions, 53 deletions
@@ -24,3 +24,7 @@ func (g \*Game) updatePlacingWall(cursor Point) { } } ``` + +## Dependencies + +- `sudo dnf install libX11-devel libXcursor-devel libXrandr-devel libXinerama-devel mesa-libGL-devel libXi-devel libXxf86vm-devel` diff --git a/gather_order.go b/gather_order.go index 8cb3a7b..f1e77a7 100644 --- a/gather_order.go +++ b/gather_order.go @@ -12,39 +12,41 @@ type GatherOrder struct { } func (o *GatherOrder) Update(e *Entity, g *Game) { - if e.Position.IsEnabled && e.Move.IsEnabled && e.ResourceGatherer.IsEnabled && e.ResourceGatherer.Value.CurrentTarget != nil { - gatherer := &e.ResourceGatherer.Value - source := gatherer.CurrentTarget.ResourceSource.Value - if gatherer.CurrentVolume == gatherer.MaxCapacity && !e.Move.Value.IsActive { - pointsNextToStorage := []Point{} - for _, entity := range g.Entities { - if entity.Position.IsEnabled && entity.ResourceStorage.IsEnabled { - pointsNextToStorage = append(pointsNextToStorage, AdjacentPoints(entity.Position.Value)...) - } - } - nextToStorage, distance := g.Closest(e.Position.Value, pointsNextToStorage) + if !e.Position.IsEnabled || !e.Move.IsEnabled || !e.ResourceGatherer.IsEnabled || e.ResourceGatherer.Value.CurrentTarget == nil { + return + } + + gatherer := &e.ResourceGatherer.Value + source := gatherer.CurrentTarget.ResourceSource.Value + if gatherer.CurrentVolume == gatherer.MaxCapacity { + if e.Move.Value.IsActive { + return + } + storageDockings := getAllStorageDockings(g) + destination, distance := g.Closest(e.Position.Value, storageDockings) + if distance == math.MaxInt { + slog.Info("no accessible storage, canceling gather order") + e.Order.Value = nil + return + } + slog.Info("storage target", slog.String("storage", destination.String()), slog.Int("distance", distance)) + if distance > 1 { + slog.Info("moving to storage", slog.String("storage", destination.String())) + e.StartMove(destination, g.getMoveMap()) + } else { + gatherer.CurrentVolume = 0 + // TODO: increment storage + sourceDockings := AdjacentPoints(gatherer.CurrentTarget.Position.Value) + destination, distance := g.Closest(e.Position.Value, sourceDockings) if distance == math.MaxInt { slog.Info("no accessible storage, canceling gather order") e.Order.Value = nil return } - slog.Info("storage target", slog.String("storage", nextToStorage.String()), slog.Int("distance", distance)) - if distance > 1 { - slog.Info("moving to storage", slog.String("storage", nextToStorage.String())) - e.StartMove(nextToStorage, g.getMoveMap()) - } else { - gatherer.CurrentVolume = 0 - // TODO: increment storage - pointsNextToSource := AdjacentPoints(gatherer.CurrentTarget.Position.Value) - nextToSource, distance := g.Closest(e.Position.Value, pointsNextToSource) - if distance == math.MaxInt { - slog.Info("no accessible storage, canceling gather order") - e.Order.Value = nil - return - } - e.StartMove(nextToSource, g.getMoveMap()) - } - } else if Distance(e.Position.Value, gatherer.CurrentTarget.Position.Value) <= 100 { + e.StartMove(destination, g.getMoveMap()) + } + } else { + if Distance(e.Position.Value, gatherer.CurrentTarget.Position.Value) <= 100 { now := time.Now() if gatherer.CurrentVolume < gatherer.MaxCapacity && gatherer.LastPickupTime.Add(200*time.Millisecond).Before(now) { gatherer.LastPickupTime = now @@ -52,8 +54,28 @@ func (o *GatherOrder) Update(e *Entity, g *Game) { source.Remaining -= 1 slog.Info("gathered", slog.Int("current_volume", gatherer.CurrentVolume)) } + } else if !e.Move.Value.IsActive { + // Lost path to source (todo mutualise) + sourceDockings := AdjacentPoints(gatherer.CurrentTarget.Position.Value) + destination, distance := g.Closest(e.Position.Value, sourceDockings) + if distance == math.MaxInt { + slog.Info("no accessible storage, canceling gather order") + e.Order.Value = nil + return + } + e.StartMove(destination, g.getMoveMap()) + } + } +} + +func getAllStorageDockings(g *Game) []Point { + storageDockings := []Point{} + for _, entity := range g.Entities { + if entity.Position.IsEnabled && entity.ResourceStorage.IsEnabled { + storageDockings = append(storageDockings, AdjacentPoints(entity.Position.Value)...) } } + return storageDockings } func Gather(e *Entity, source *Entity, g *Game) Order { @@ -62,14 +84,14 @@ func Gather(e *Entity, source *Entity, g *Game) Order { slog.Info("gathering from", slog.String("source", source.Position.Value.String())) e.Order.Value = &GatherOrder{source: source} e.ResourceGatherer.Value.CurrentTarget = source - pointsNextToSource := AdjacentPoints(source.Position.Value) - nextToSource, distance := g.Closest(e.Position.Value, pointsNextToSource) + sourceDockings := AdjacentPoints(source.Position.Value) + destination, distance := g.Closest(e.Position.Value, sourceDockings) if distance == math.MaxInt { slog.Info("no accessible storage, canceling gather order") e.Order.Value = nil return nil } - e.StartMove(nextToSource, g.getMoveMap()) + e.StartMove(destination, g.getMoveMap()) } } return &GatherOrder{source: source} @@ -173,32 +173,34 @@ func (e *Entity) moveToward(destination Point) { } func (e *Entity) UpdateMove(moveMap MoveMap) { - if e.Move.IsEnabled && e.Position.IsEnabled { - position := e.Position.Value - if !e.Move.Value.IsActive { - return - } - next := e.Move.Value.Path[0] - if Distance(position, next) < MOVE_SPEED { - e.Position.Value = next - remainingPath := e.Move.Value.Path[1:] - if next == e.Move.Value.Destination { + if !e.Move.IsEnabled || !e.Position.IsEnabled { + return + } + position := e.Position.Value + if !e.Move.Value.IsActive { + return + } + next := e.Move.Value.Path[0] + if Distance(position, next) < MOVE_SPEED { + e.Position.Value = next + remainingPath := e.Move.Value.Path[1:] + if next == e.Move.Value.Destination { + e.Move.Value.IsActive = false + slog.Info("move finished", slog.String("destination", e.Move.Value.Destination.String())) + } else if remainingPath.isValid(moveMap) { + e.Move.Value.Path = remainingPath + } else { + // Here maybe the destination has to change + path, ok := SearchPath(next, e.Move.Value.Destination, moveMap) + if !ok { + slog.Info("no path found", slog.String("destination", e.Move.Value.Destination.String())) e.Move.Value.IsActive = false - slog.Info("move finished", slog.String("destination", e.Move.Value.Destination.String())) - } else if remainingPath.isValid(moveMap) { - e.Move.Value.Path = remainingPath } else { - path, ok := SearchPath(next, e.Move.Value.Destination, moveMap) - if !ok { - slog.Info("no path found", slog.String("destination", e.Move.Value.Destination.String())) - e.Move.Value.IsActive = false - } else { - e.Move.Value.Path = path[1:] - } + e.Move.Value.Path = path[1:] } - } else { - e.moveToward(next) } + } else { + e.moveToward(next) } } |
