aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-04 10:53:32 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-04 10:53:32 +0200
commite3f14fc0b375b4b7c2204a669d058ac124b78e92 (patch)
treead3bd4e6274f597a26a1fbb2cd3e3f5328913994
parent525dd3c3990ee8365e08be59a48df1f51a42b638 (diff)
Some pretty refactoring
-rw-r--r--gather_order.go97
-rw-r--r--move.go5
2 files changed, 55 insertions, 47 deletions
diff --git a/gather_order.go b/gather_order.go
index f1e77a7..b4f3c9e 100644
--- a/gather_order.go
+++ b/gather_order.go
@@ -19,53 +19,60 @@ func (o *GatherOrder) Update(e *Entity, g *Game) {
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
+ o.updateGathererFull(e, g, gatherer)
+ return
+ }
+ if Distance(e.Position.Value, gatherer.CurrentTarget.Position.Value) <= 100 {
+ o.updateGathering(gatherer, source)
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
- }
- 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
- gatherer.CurrentVolume += 1
- 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())
- }
}
+ if !e.Move.Value.IsActive {
+ o.startMoveToSource(e, gatherer, g)
+ }
+}
+
+func (*GatherOrder) updateGathering(gatherer *ResourceGatherer, source ResourceSource) {
+ now := time.Now()
+ if gatherer.CurrentVolume >= gatherer.MaxCapacity || gatherer.LastPickupTime.Add(200*time.Millisecond).After(now) {
+ return
+ }
+ gatherer.LastPickupTime = now
+ gatherer.CurrentVolume += 1
+ source.Remaining -= 1
+ slog.Info("gathered", slog.Int("current_volume", gatherer.CurrentVolume))
+}
+
+func (o *GatherOrder) updateGathererFull(e *Entity, g *Game, gatherer *ResourceGatherer) {
+ 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())
+ return
+ }
+ gatherer.CurrentVolume = 0
+ // TODO: increment storage
+ o.startMoveToSource(e, gatherer, g)
+}
+
+func (o *GatherOrder) startMoveToSource(e *Entity, gatherer *ResourceGatherer, g *Game) {
+ 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 {
diff --git a/move.go b/move.go
index da73c27..1322a51 100644
--- a/move.go
+++ b/move.go
@@ -173,11 +173,12 @@ func (e *Entity) moveToward(destination Point) {
}
func (e *Entity) UpdateMove(moveMap MoveMap) {
- if !e.Move.IsEnabled || !e.Position.IsEnabled {
+ if !e.Move.IsEnabled || !e.Position.IsEnabled || !e.Move.Value.IsActive {
return
}
position := e.Position.Value
- if !e.Move.Value.IsActive {
+ if len(e.Move.Value.Path) == 0 {
+ e.Move.Value.IsActive = false
return
}
next := e.Move.Value.Path[0]