-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (159 loc) · 4.55 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
_ "embed"
"fmt"
"os"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/PotatoesFall/satisfactory/game"
"github.com/erikgeiser/promptkit/selection"
"github.com/erikgeiser/promptkit/textinput"
"github.com/fbiville/markdown-table-formatter/pkg/markdown"
"golang.org/x/exp/slices"
)
/*
TODO
- allow for multiple inputs
- account for byproducts
- combine with other recipes
- in ratios if possible, that would be dank
- idea, build macro recipes during the getRecipeWeights phase?
- a different kind of recipe
- contains a list of sub recipes
- no byproducts
- consumed in tree
- partially consumed? (then sink the rest)
- burned for energy
- generate energy if possible
- process and sink if liquid ?
- add energy costs for resource extraction
- add building cost
- also for resoure extraction
*/
type Config struct {
Weights Weights `toml:"Weights"`
Recipes RecipeConfig `toml:"Recipes"`
}
type RecipeConfig struct {
Disallowed []string `toml:"Disallowed"`
}
type Weights struct {
Resources map[game.Item]float64 `toml:"Resources"`
Power float64 `toml:"Power"`
// Machines map[string]float64
}
var (
allRecipes []*game.Recipe
allItems []game.Item
recipesByItem map[game.Item][]*game.Recipe
recipesByName map[string]*game.Recipe
)
func main() {
config := readConfig()
loadRecipes(config.Recipes)
item, amount := getItem()
_, _ = item, amount
recipeCounts := optimize(allRecipes, config.Weights, map[game.Item]float64{item: amount})
// recipeCounts := optimize([]*game.Recipe{
// recipesByName["Alternate: Pure Iron Ingot"],
// recipesByName["Iron Ingot"],
// }, config.Weights, map[game.Item]float64{"Iron Ingot": 60})
total := 0.0
for recipe, count := range recipeCounts {
power := recipe.Power * count
total += power
fmt.Println(count, recipe.Name, power, "MW")
}
fmt.Println("Total", total, "MW")
// recipeTrees := getAllItemWeights(weights)
// tree := recipeTrees[item]
// // fmt.Println(tree.Print(amount))
// recipeOrder := tree.RecipeOrder()
// fmt.Println(markdownTable(tree.RecipeCounts(amount), recipeOrder))
// fmt.Printf("Total Power: %.2f MW", tree.Power(amount))
}
//go:embed config.example.toml
var defaultConfig []byte
func readConfig() Config {
var config Config
configFile, err := os.ReadFile("config.toml")
if err != nil {
fmt.Println("No config.toml found, using default config.")
configFile = defaultConfig
}
if err := toml.Unmarshal(configFile, &config); err != nil {
panic(err)
}
return config
}
func markdownTable(recipeCounts map[string]float64, recipeOrder []string) string {
rows := make([][]string, 0, len(recipeCounts))
for _, recipeName := range recipeOrder {
count := recipeCounts[recipeName]
recipe, ok := recipesByName[recipeName]
if !ok {
rows = append(rows, []string{
"", "", "", recipeName + " (Resource)", fmt.Sprintf("%8.2f %25s", count, recipeName), "",
})
continue
}
var ingredients, products strings.Builder
for ingredient, inCount := range recipe.Ingredients {
ingredients.WriteString(fmt.Sprintf("%8.2f %25s", float64(inCount)*count*60/recipe.Duration, ingredient))
}
for product, prodCount := range recipe.Products {
products.WriteString(fmt.Sprintf("%8.2f %25s", float64(prodCount)*count*60/recipe.Duration, product))
}
rows = append(rows, []string{
fmt.Sprintf("%7.2f", count),
recipe.Machine,
fmt.Sprintf("%7.2f MW", recipe.Power*count),
recipeName,
products.String(),
ingredients.String(),
})
}
builder := markdown.NewTableFormatterBuilder().WithPrettyPrint()
f := builder.Build("", "Machine", "Power", "Recipe", "Products", "Ingredients")
table, err := f.Format(rows)
if err != nil {
panic(err)
}
return table
}
func getItem() (game.Item, float64) {
if len(os.Args) < 3 {
return pickItem()
}
f, err := strconv.ParseFloat(os.Args[len(os.Args)-1], 64)
if err != nil {
panic(err)
}
item := game.Item(strings.Join(os.Args[1:len(os.Args)-1], " "))
if _, ok := recipesByItem[item]; !ok {
panic("item not found: " + item)
}
return item, f
}
func pickItem() (game.Item, float64) {
slices.Sort(allItems)
itemPicker := selection.New("Produce What?", allItems)
itemPicker.PageSize = 10
item, err := itemPicker.RunPrompt()
if err != nil {
panic(err)
}
amountPicker := textinput.New("How much?")
amountPicker.Placeholder = "2.5"
amountPicker.Validate = func(s string) error {
_, err := strconv.ParseFloat(s, 64)
return err
}
s, err := amountPicker.RunPrompt()
if err != nil {
panic(err)
}
amount, _ := strconv.ParseFloat(s, 64)
return item, amount
}