From e3f14fc0b375b4b7c2204a669d058ac124b78e92 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Fri, 4 Jul 2025 10:53:32 +0200 Subject: Some pretty refactoring --- gather_order.go | 97 +++++++++++++++++++++++++++++++-------------------------- move.go | 5 +-- 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] -- cgit v1.3