]> git.scottworley.com Git - planeteer/blame - planeteer.go
Some safety against future modifications in DimensionSizes().
[planeteer] / planeteer.go
CommitLineData
d07f3caa
SW
1/* Planeteer: Give trade route advice for Planets: The Exploration of Space
2 * Copyright (C) 2011 Scott Worley <sworley@chkno.net>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18package main
19
20import "flag"
c45c1bca 21import "fmt"
d07f3caa
SW
22import "json"
23import "os"
c45c1bca
SW
24import "strings"
25
26var start = flag.String("start", "",
27 "The planet to start at")
d07f3caa 28
c45c1bca 29var end = flag.String("end", "",
e9ff66cf 30 "A comma-separated list of acceptable ending planets.")
c45c1bca
SW
31
32var planet_data_file = flag.String("planet_data_file", "planet-data",
d07f3caa
SW
33 "The file to read planet data from")
34
e9ff66cf
SW
35var fuel = flag.Int("fuel", 16, "Reactor units")
36
37var hold = flag.Int("hold", 300, "Size of your cargo hold")
c45c1bca
SW
38
39var start_edens = flag.Int("start_edens", 0,
40 "How many Eden Warp Units are you starting with?")
41
42var end_edens = flag.Int("end_edens", 0,
43 "How many Eden Warp Units would you like to keep (not use)?")
44
45var cloak = flag.Bool("cloak", false,
46 "Make sure to end with a Device of Cloaking")
47
e9ff66cf 48var drones = flag.Int("drones", 0, "Buy this many Fighter Drones")
c45c1bca 49
e9ff66cf 50var batteries = flag.Int("batteries", 0, "Buy this many Shield Batterys")
c45c1bca
SW
51
52var visit_string = flag.String("visit", "",
53 "A comma-separated list of planets to make sure to visit")
54
55func visit() []string {
56 return strings.Split(*visit_string, ",")
57}
58
9b3b3d9a 59type Commodity struct {
9b3b3d9a
SW
60 BasePrice int
61 CanSell bool
62 Limit int
63}
12bc2cd7 64type Planet struct {
12bc2cd7
SW
65 BeaconOn bool
66 /* Use relative prices rather than absolute prices because you
67 can get relative prices without traveling to each planet. */
0e94bdac 68 RelativePrices map[string]int
12bc2cd7 69}
d07f3caa 70type planet_data struct {
0e94bdac
SW
71 Commodities map[string]Commodity
72 Planets map[string]Planet
73 pi, ci map[string]int // Generated; not read from file
d07f3caa
SW
74}
75
76func ReadData() (data planet_data) {
c45c1bca 77 f, err := os.Open(*planet_data_file)
d07f3caa
SW
78 if err != nil {
79 panic(err)
80 }
81 defer f.Close()
82 err = json.NewDecoder(f).Decode(&data)
83 if err != nil {
84 panic(err)
85 }
86 return
87}
88
c45c1bca
SW
89/* This program operates by filling in a state table representing the best
90 * possible trips you could make; the ones that makes you the most money.
91 * This is feasible because we don't look at all the possible trips.
92 * We define a list of things that are germane to this game and then only
93 * consider the best outcome in each possible game state.
94 *
95 * Each cell in the table represents a state in the game. In each cell,
96 * we track two things: 1. the most money you could possibly have while in
97 * that state and 2. one possible way to get into that state with that
98 * amount of money.
99 *
100 * A basic analysis can be done with a two-dimensional table: location and
101 * fuel. planeteer-1.0 used this two-dimensional table. This version
102 * adds features mostly by adding dimensions to this table.
103 *
104 * Note that the sizes of each dimension are data driven. Many dimensions
105 * collapse to one possible value (ie, disappear) if the corresponding
106 * feature is not enabled.
107 */
108
109// The official list of dimensions:
110const (
e9ff66cf 111 // Name Num Size Description
0e94bdac
SW
112 Edens = iota // 1 3 # of Eden warp units (0 - 2 typically)
113 Cloaks // 2 2 # of Devices of Cloaking (0 or 1)
114 UnusedCargo // 3 4 # of unused cargo spaces (0 - 3 typically)
115 Fuel // 4 17 Reactor power left (0 - 16)
116 Location // 5 26 Location (which planet)
117 Hold // 6 15 Cargo bay contents (a *Commodity or nil)
118 NeedFighters // 7 2 Errand: Buy fighter drones (needed or not)
119 NeedShields // 8 2 Errand: Buy shield batteries (needed or not)
120 Visit // 9 2**N Visit: Stop by these N planets in the route
c45c1bca
SW
121
122 NumDimensions
123)
124
125func bint(b bool) int {
0e94bdac
SW
126 if b {
127 return 1
128 }
c45c1bca
SW
129 return 0
130}
131
132func DimensionSizes(data planet_data) []int {
133 eden_capacity := data.Commodities["Eden Warp Units"].Limit
134 cloak_capacity := bint(*cloak)
64d87250
SW
135 dims := make([]int, NumDimensions)
136 dims[Edens] = eden_capacity + 1
137 dims[Cloaks] = cloak_capacity + 1
138 dims[UnusedCargo] = eden_capacity + cloak_capacity + 1
139 dims[Fuel] = *fuel + 1
140 dims[Location] = len(data.Planets)
141 dims[Hold] = len(data.Commodities)
142 dims[NeedFighters] = bint(*drones > 0) + 1
143 dims[NeedShields] = bint(*batteries > 0) + 1
144 dims[Visit] = 1 << uint(len(visit()))
2f4ed5ca
SW
145
146 // Remind myself to add a line above when adding new dimensions
147 for i, dim := range dims {
148 if dim < 1 {
149 panic(i)
150 }
151 }
c45c1bca
SW
152 return dims
153}
154
155func StateTableSize(dims []int) int {
156 sum := 0
157 for _, size := range dims {
158 sum += size
159 }
160 return sum
161}
162
163type State struct {
164 funds, from int
165}
166
167func NewStateTable(dims []int) []State {
168 return make([]State, StateTableSize(dims))
169}
170
171func EncodeIndex(dims, addr []int) int {
172 index := addr[0]
173 for i := 1; i < len(dims); i++ {
0e94bdac 174 index = index*dims[i] + addr[i]
c45c1bca
SW
175 }
176 return index
177}
178
179func DecodeIndex(dims []int, index int) []int {
180 addr := make([]int, len(dims))
181 for i := len(dims) - 1; i > 0; i-- {
182 addr[i] = index % dims[i]
183 index /= dims[i]
184 }
185 addr[0] = index
186 return addr
187}
188
5f1a50e1
SW
189/* What is the value of hauling 'commodity' from 'from' to 'to'?
190 * Take into account the available funds and the available cargo space. */
191func TradeValue(data planet_data,
0e94bdac
SW
192from, to Planet,
193commodity string,
194initial_funds, max_quantity int) int {
5f1a50e1 195 if !data.Commodities[commodity].CanSell {
5a1593ab
SW
196 return 0
197 }
5f1a50e1 198 from_relative_price, from_available := from.RelativePrices[commodity]
5a1593ab
SW
199 if !from_available {
200 return 0
201 }
5f1a50e1 202 to_relative_price, to_available := to.RelativePrices[commodity]
5a1593ab
SW
203 if !to_available {
204 return 0
205 }
206
5f1a50e1
SW
207 base_price := data.Commodities[commodity].BasePrice
208 from_absolute_price := from_relative_price * base_price
209 to_absolute_price := to_relative_price * base_price
5a1593ab
SW
210 buy_price := from_absolute_price
211 sell_price := int(float64(to_absolute_price) * 0.9)
5f1a50e1
SW
212 var can_afford int = initial_funds / buy_price
213 quantity := can_afford
214 if quantity > max_quantity {
215 quantity = max_quantity
216 }
217 return (sell_price - buy_price) * max_quantity
5a1593ab
SW
218}
219
5f1a50e1 220func FindBestTrades(data planet_data) [][]string {
c45c1bca 221 // TODO: We can't cache this because this can change based on available funds.
5f1a50e1 222 best := make([][]string, len(data.Planets))
c45c1bca
SW
223 for from := range data.Planets {
224 best[data.pi[from]] = make([]string, len(data.Planets))
225 for to := range data.Planets {
5a1593ab 226 best_gain := 0
c45c1bca
SW
227 price_list := data.Planets[from].RelativePrices
228 if len(data.Planets[to].RelativePrices) < len(data.Planets[from].RelativePrices) {
229 price_list = data.Planets[to].RelativePrices
5f1a50e1
SW
230 }
231 for commodity := range price_list {
232 gain := TradeValue(data,
0e94bdac
SW
233 data.Planets[from],
234 data.Planets[to],
235 commodity,
236 10000000,
237 1)
5a1593ab 238 if gain > best_gain {
c45c1bca 239 best[data.pi[from]][data.pi[to]] = commodity
5a1593ab
SW
240 gain = best_gain
241 }
242 }
243 }
244 }
245 return best
246}
247
c45c1bca 248// (Example of a use case for generics in Go)
0e94bdac
SW
249func IndexPlanets(m *map[string]Planet) map[string]int {
250 index := make(map[string]int, len(*m))
c45c1bca
SW
251 i := 0
252 for e := range *m {
253 index[e] = i
254 i++
255 }
256 return index
257}
0e94bdac
SW
258func IndexCommodities(m *map[string]Commodity) map[string]int {
259 index := make(map[string]int, len(*m))
c45c1bca
SW
260 i := 0
261 for e := range *m {
262 index[e] = i
263 i++
264 }
265 return index
266}
267
d07f3caa
SW
268func main() {
269 flag.Parse()
270 data := ReadData()
c45c1bca
SW
271 data.pi = IndexPlanets(&data.Planets)
272 data.ci = IndexCommodities(&data.Commodities)
273 dims := DimensionSizes(data)
274 table := NewStateTable(dims)
0e94bdac 275 table[0] = State{1, 1}
5a1593ab 276 best_trades := FindBestTrades(data)
c45c1bca
SW
277
278 for from := range data.Planets {
279 for to := range data.Planets {
5a1593ab 280 best_trade := "(nothing)"
c45c1bca
SW
281 if best_trades[data.pi[from]][data.pi[to]] != "" {
282 best_trade = best_trades[data.pi[from]][data.pi[to]]
5a1593ab 283 }
c45c1bca 284 fmt.Printf("%s to %s: %s\n", from, to, best_trade)
5a1593ab
SW
285 }
286 }
d07f3caa 287}