1 /* Planeteer: Give trade route advice for Planets: The Exploration of Space
2 * Copyright (C) 2011 Scott Worley <sworley@chkno.net>
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.
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.
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/>.
26 var start = flag.String("start", "",
27 "The planet to start at")
29 var end = flag.String("end", "",
30 "A comma-separated list of planets to end at")
32 var planet_data_file = flag.String("planet_data_file", "planet-data",
33 "The file to read planet data from")
35 var fuel = flag.Int("fuel", 16,
36 "Reactor units; How many non-Eden jumps we can make "+
37 "(but remember that deviating from the flight plan "+
38 "costs two units of fuel per jump)")
40 var start_edens = flag.Int("start_edens", 0,
41 "How many Eden Warp Units are you starting with?")
43 var end_edens = flag.Int("end_edens", 0,
44 "How many Eden Warp Units would you like to keep (not use)?")
46 var cloak = flag.Bool("cloak", false,
47 "Make sure to end with a Device of Cloaking")
49 var drones = flag.Int("drones", 0,
50 "Buy this many Fighter Drones")
52 var batteries = flag.Int("batteries", 0,
53 "Buy this many Shield Batterys")
55 var visit_string = flag.String("visit", "",
56 "A comma-separated list of planets to make sure to visit")
58 func visit() []string {
59 return strings.Split(*visit_string, ",")
62 type Commodity struct {
69 /* Use relative prices rather than absolute prices because you
70 can get relative prices without traveling to each planet. */
71 RelativePrices map[string]int
73 type planet_data struct {
74 Commodities map[string]Commodity
75 Planets map[string]Planet
76 pi, ci map[string]int // Generated; not read from file
79 func ReadData() (data planet_data) {
80 f, err := os.Open(*planet_data_file)
85 err = json.NewDecoder(f).Decode(&data)
92 /* This program operates by filling in a state table representing the best
93 * possible trips you could make; the ones that makes you the most money.
94 * This is feasible because we don't look at all the possible trips.
95 * We define a list of things that are germane to this game and then only
96 * consider the best outcome in each possible game state.
98 * Each cell in the table represents a state in the game. In each cell,
99 * we track two things: 1. the most money you could possibly have while in
100 * that state and 2. one possible way to get into that state with that
103 * A basic analysis can be done with a two-dimensional table: location and
104 * fuel. planeteer-1.0 used this two-dimensional table. This version
105 * adds features mostly by adding dimensions to this table.
107 * Note that the sizes of each dimension are data driven. Many dimensions
108 * collapse to one possible value (ie, disappear) if the corresponding
109 * feature is not enabled.
112 // The official list of dimensions:
114 // Name Num Size Description
115 Edens = iota // 1 3 # of Eden warp units (0 - 2 typically)
116 Cloaks // 2 2 # of Devices of Cloaking (0 or 1)
117 UnusedCargo // 3 4 # of unused cargo spaces (0 - 3 typically)
118 Fuel // 4 17 Reactor power left (0 - 16)
119 Location // 5 26 Location (which planet)
120 Hold // 6 15 Cargo bay contents (a *Commodity or nil)
121 NeedFighters // 7 2 Errand: Buy fighter drones (needed or not)
122 NeedShields // 8 2 Errand: Buy shield batteries (needed or not)
123 Visit // 9 2**N Visit: Stop by these N planets in the route
128 func bint(b bool) int {
135 func DimensionSizes(data planet_data) []int {
136 eden_capacity := data.Commodities["Eden Warp Units"].Limit
137 cloak_capacity := bint(*cloak)
141 eden_capacity + cloak_capacity + 1,
144 len(data.Commodities),
145 bint(*drones > 0) + 1,
146 bint(*batteries > 0) + 1,
147 1 << uint(len(visit())),
149 if len(dims) != NumDimensions {
150 panic("Dimensionality mismatch")
155 func StateTableSize(dims []int) int {
157 for _, size := range dims {
167 func NewStateTable(dims []int) []State {
168 return make([]State, StateTableSize(dims))
171 func EncodeIndex(dims, addr []int) int {
173 for i := 1; i < len(dims); i++ {
174 index = index*dims[i] + addr[i]
179 func 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]
189 /* What is the value of hauling 'commodity' from 'from' to 'to'?
190 * Take into account the available funds and the available cargo space. */
191 func TradeValue(data planet_data,
194 initial_funds, max_quantity int) int {
195 if !data.Commodities[commodity].CanSell {
198 from_relative_price, from_available := from.RelativePrices[commodity]
202 to_relative_price, to_available := to.RelativePrices[commodity]
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
210 buy_price := from_absolute_price
211 sell_price := int(float64(to_absolute_price) * 0.9)
212 var can_afford int = initial_funds / buy_price
213 quantity := can_afford
214 if quantity > max_quantity {
215 quantity = max_quantity
217 return (sell_price - buy_price) * max_quantity
220 func FindBestTrades(data planet_data) [][]string {
221 // TODO: We can't cache this because this can change based on available funds.
222 best := make([][]string, len(data.Planets))
223 for from := range data.Planets {
224 best[data.pi[from]] = make([]string, len(data.Planets))
225 for to := range data.Planets {
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
231 for commodity := range price_list {
232 gain := TradeValue(data,
238 if gain > best_gain {
239 best[data.pi[from]][data.pi[to]] = commodity
248 // (Example of a use case for generics in Go)
249 func IndexPlanets(m *map[string]Planet) map[string]int {
250 index := make(map[string]int, len(*m))
258 func IndexCommodities(m *map[string]Commodity) map[string]int {
259 index := make(map[string]int, len(*m))
271 data.pi = IndexPlanets(&data.Planets)
272 data.ci = IndexCommodities(&data.Commodities)
273 dims := DimensionSizes(data)
274 table := NewStateTable(dims)
275 table[0] = State{1, 1}
276 best_trades := FindBestTrades(data)
278 for from := range data.Planets {
279 for to := range data.Planets {
280 best_trade := "(nothing)"
281 if best_trades[data.pi[from]][data.pi[to]] != "" {
282 best_trade = best_trades[data.pi[from]][data.pi[to]]
284 fmt.Printf("%s to %s: %s\n", from, to, best_trade)