-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.go
126 lines (108 loc) · 2.73 KB
/
assemble.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package minifac
import (
"fmt"
"sort"
"github.com/mazzegi/minifac/grid"
"golang.org/x/exp/maps"
)
var _ ProducerConsumer = &Assembler{}
func NewAssembler(name string, receipt Receipt, inCapa int, outCapa int) *Assembler {
a := &Assembler{
name: name,
receipt: receipt,
inStocks: make(map[Resource]*Stock),
outStock: NewStock(outCapa),
}
for inRes := range receipt.Input {
a.inStocks[inRes] = NewStock(inCapa)
}
return a
}
type Assembler struct {
name string
receipt Receipt
inStocks map[Resource]*Stock
outStock *Stock
lastProdTick int
currTick int
producing bool
}
func (c *Assembler) Size() grid.Size {
return grid.S(1, 1)
}
func (c *Assembler) Name() string {
return c.name
}
func (c *Assembler) Info() []string {
stockRess := maps.Keys(c.inStocks)
sort.Slice(stockRess, func(i, j int) bool { return stockRess[i] < stockRess[j] })
var stock []string
for _, res := range stockRess {
stock = append(stock, fmt.Sprintf("Stock: %s: %d", res, c.inStocks[res].TotalAmount()))
}
info := []string{
fmt.Sprintf("Assembler: %s", c.name),
fmt.Sprintf("Receipt: %s", c.receipt.String()),
}
info = append(info, stock...)
return info
}
func (c *Assembler) ProduceAtPositions(base grid.Position) []grid.Position {
return base.Neighbours()
}
func (c *Assembler) ConsumeAtPositions(base grid.Position) []grid.Position {
return []grid.Position{base}
}
func (c *Assembler) Tick() {
c.currTick++
if c.producing {
if c.currTick-c.lastProdTick >= c.receipt.ProductionTime {
c.outStock.Add(c.receipt.Output, 1)
c.producing = false
}
} else if c.outStock.CanAdd(c.receipt.Output, 1) {
// see if we can produce something
for res, cnt := range c.receipt.Input {
if c.inStocks[res].Amount(res) < cnt {
return
}
}
// we have enough - take it from stocks
for res, cnt := range c.receipt.Input {
c.inStocks[res].Take(res, cnt)
}
//start production
c.producing = true
c.lastProdTick = c.currTick
}
}
func (c *Assembler) ConsumeFrom(res Resource, dir grid.Direction) {
if !c.CanConsumeFrom(res, dir) {
return
}
c.inStocks[res].Add(res, 1)
}
func (c *Assembler) CanConsumeFrom(res Resource, dir grid.Direction) bool {
_, ok := c.receipt.Input[res]
if !ok {
return false
}
return c.inStocks[res].CanAdd(res, 1)
}
func (c *Assembler) CanConsumeAny() bool {
return true
}
func (c *Assembler) Produce() (Resource, bool) {
if c.outStock.Amount(c.receipt.Output) > 0 {
//Log("%s: produce: %s", c.name, c.receipt.output)
c.outStock.Take(c.receipt.Output, 1)
return c.receipt.Output, true
}
return None, false
}
func (c *Assembler) CanProduce() bool {
return c.outStock.Amount(c.receipt.Output) > 0
}
func (c *Assembler) Resource() Resource {
return c.receipt.Output
}