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/>.
25 var datafile = flag.String("planet_data_file", "planet-data",
26 "The file to read planet data from")
28 type Commodity struct {
37 /* Use relative prices rather than absolute prices because you
38 can get relative prices without traveling to each planet. */
39 RelativePrices map [string] int
41 type planet_data struct {
42 Commodities []Commodity
46 func ReadData() (data planet_data) {
47 f, err := os.Open(*datafile)
52 err = json.NewDecoder(f).Decode(&data)
59 func TradeValue(from, to *Planet,
62 if !commodity.CanSell {
65 from_relative_price, from_available := from.RelativePrices[commodity.Name]
69 to_relative_price, to_available := to.RelativePrices[commodity.Name]
74 from_absolute_price := from_relative_price * commodity.BasePrice
75 to_absolute_price := to_relative_price * commodity.BasePrice
76 buy_price := from_absolute_price
77 sell_price := int(float64(to_absolute_price) * 0.9)
78 return (sell_price - buy_price) * quantity
82 func FindBestTrades(data planet_data) [][]*Commodity {
83 best := make([][]*Commodity, len(data.Planets))
84 for from_index := range data.Planets {
85 best[from_index] = make([]*Commodity, len(data.Planets))
86 for to_index := range data.Planets {
88 for commodity_index := range data.Commodities {
89 gain := TradeValue(&data.Planets[from_index],
90 &data.Planets[to_index],
91 &data.Commodities[commodity_index],
94 best[from_index][to_index] = &data.Commodities[commodity_index]
106 best_trades := FindBestTrades(data)
107 for from_index, from_planet := range data.Planets {
108 for to_index, to_planet := range data.Planets {
109 best_trade := "(nothing)"
110 if best_trades[from_index][to_index] != nil {
111 best_trade = best_trades[from_index][to_index].Name
113 fmt.Printf("%s to %s: %s\n", from_planet.Name, to_planet.Name, best_trade)