aboutsummaryrefslogtreecommitdiff
path: root/gather_order.go
blob: eec9b99a6cf7511d7c7750d2edd03b9cc69e3c6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

package main

import (
	"math"
	"time"

	"age_of_empires/physics"

	"golang.org/x/exp/slog"
)

type GatherOrder struct {
	source *Entity
}

func (o *GatherOrder) Update(e *Entity, g *Game) {
	if !e.Position.IsEnabled || !e.Move.IsEnabled || !e.ResourceGatherer.IsEnabled || e.ResourceGatherer.Value.CurrentTarget == nil {
		return
	}

	gatherer := &e.ResourceGatherer.Value
	if gatherer.CurrentVolume == gatherer.MaxCapacity {
		o.updateGathererFull(e, g, gatherer)
		return
	}
	if physics.Distance(e.Position.Value, gatherer.CurrentTarget.Position.Value) <= 100 {
		o.updateGathering(g.Now(), gatherer, &gatherer.CurrentTarget.ResourceSource.Value)
		return
	}
	if !e.Move.Value.IsActive {
		o.startMoveToSource(e, gatherer, g)
	}
}

func (*GatherOrder) updateGathering(now time.Time, gatherer *ResourceGatherer, source *ResourceSource) {
	if gatherer.CurrentVolume >= gatherer.MaxCapacity || gatherer.LastPickupTime.Add(200*time.Millisecond).After(now) {
		return
	}
	gatherer.LastPickupTime = now
	gatherer.CurrentVolume += 1
	source.Remaining -= 1
}

func (o *GatherOrder) updateGathererFull(e *Entity, g *Game, gatherer *ResourceGatherer) {
	if e.Move.Value.IsActive {
		return
	}
	storageDockings := getAllStorageDockings(g)
	destination, distance := physics.Closest(e.Position.Value, storageDockings, g.getMoveMap())
	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()))
		physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap())
		return
	}
	g.ResourceAmount += gatherer.CurrentVolume
	gatherer.CurrentVolume = 0
	o.startMoveToSource(e, gatherer, g)
}

func (o *GatherOrder) startMoveToSource(e *Entity, gatherer *ResourceGatherer, g *Game) {
	sourceDockings := physics.AdjacentPoints(gatherer.CurrentTarget.Position.Value)
	destination, distance := physics.Closest(e.Position.Value, sourceDockings, g.getMoveMap())
	if distance == math.MaxInt {
		slog.Info("no accessible storage, canceling gather order")
		e.Order.Value = nil
		return
	}
	physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap())
}

func getAllStorageDockings(g *Game) []physics.Point {
	storageDockings := []physics.Point{}
	for _, entity := range g.Entities {
		if entity.Position.IsEnabled && entity.ResourceStorage.IsEnabled {
			storageDockings = append(storageDockings, physics.AdjacentPoints(entity.Position.Value)...)
		}
	}
	return storageDockings
}

func Gather(e *Entity, source *Entity, g *Game) Order {
	if !e.Position.IsEnabled || !e.Move.IsEnabled || !e.Order.IsEnabled {
		return nil
	}
	slog.Info("gathering from", slog.String("source", source.Position.Value.String()))
	e.Order.Value = &GatherOrder{source: source}
	e.ResourceGatherer.Value.CurrentTarget = source
	sourceDockings := physics.AdjacentPoints(source.Position.Value)
	destination, distance := physics.Closest(e.Position.Value, sourceDockings, g.getMoveMap())
	if distance == math.MaxInt {
		slog.Info("no accessible storage, canceling gather order")
		e.Order.Value = nil
		return nil
	}
	physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap())
	return &GatherOrder{source: source}
}