]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | package main | |
19 | ||
20 | import "flag" | |
21 | import "json" | |
22 | import "os" | |
23 | import "fmt" | |
24 | ||
25 | var datafile = flag.String("planet_data_file", "planet-data", | |
26 | "The file to read planet data from") | |
27 | ||
28 | type Commodity struct { | |
29 | BasePrice int | |
30 | CanSell bool | |
31 | Limit int | |
32 | } | |
33 | type Planet struct { | |
34 | Name string | |
35 | BeaconOn bool | |
36 | /* Use relative prices rather than absolute prices because you | |
37 | can get relative prices without traveling to each planet. */ | |
38 | RelativePrices map [string] int | |
39 | } | |
40 | type planet_data struct { | |
41 | Commodities map [string] Commodity | |
42 | Planets []Planet | |
43 | } | |
44 | ||
45 | func ReadData() (data planet_data) { | |
46 | f, err := os.Open(*datafile) | |
47 | if err != nil { | |
48 | panic(err) | |
49 | } | |
50 | defer f.Close() | |
51 | err = json.NewDecoder(f).Decode(&data) | |
52 | if err != nil { | |
53 | panic(err) | |
54 | } | |
55 | return | |
56 | } | |
57 | ||
58 | /* What is the value of hauling 'commodity' from 'from' to 'to'? | |
59 | * Take into account the available funds and the available cargo space. */ | |
60 | func TradeValue(data planet_data, | |
61 | from, to *Planet, | |
62 | commodity string, | |
63 | initial_funds, max_quantity int) int { | |
64 | if !data.Commodities[commodity].CanSell { | |
65 | return 0 | |
66 | } | |
67 | from_relative_price, from_available := from.RelativePrices[commodity] | |
68 | if !from_available { | |
69 | return 0 | |
70 | } | |
71 | to_relative_price, to_available := to.RelativePrices[commodity] | |
72 | if !to_available { | |
73 | return 0 | |
74 | } | |
75 | ||
76 | base_price := data.Commodities[commodity].BasePrice | |
77 | from_absolute_price := from_relative_price * base_price | |
78 | to_absolute_price := to_relative_price * base_price | |
79 | buy_price := from_absolute_price | |
80 | sell_price := int(float64(to_absolute_price) * 0.9) | |
81 | var can_afford int = initial_funds / buy_price | |
82 | quantity := can_afford | |
83 | if quantity > max_quantity { | |
84 | quantity = max_quantity | |
85 | } | |
86 | return (sell_price - buy_price) * max_quantity | |
87 | } | |
88 | ||
89 | func FindBestTrades(data planet_data) [][]string { | |
90 | best := make([][]string, len(data.Planets)) | |
91 | for from_index, from_planet := range data.Planets { | |
92 | best[from_index] = make([]string, len(data.Planets)) | |
93 | for to_index, to_planet := range data.Planets { | |
94 | best_gain := 0 | |
95 | price_list := from_planet.RelativePrices | |
96 | if len(to_planet.RelativePrices) < len(from_planet.RelativePrices) { | |
97 | price_list = to_planet.RelativePrices | |
98 | } | |
99 | for commodity := range price_list { | |
100 | gain := TradeValue(data, | |
101 | &from_planet, | |
102 | &to_planet, | |
103 | commodity, | |
104 | 10000000, | |
105 | 1) | |
106 | if gain > best_gain { | |
107 | best[from_index][to_index] = commodity | |
108 | gain = best_gain | |
109 | } | |
110 | } | |
111 | } | |
112 | } | |
113 | return best | |
114 | } | |
115 | ||
116 | func main() { | |
117 | flag.Parse() | |
118 | data := ReadData() | |
119 | best_trades := FindBestTrades(data) | |
120 | for from_index, from_planet := range data.Planets { | |
121 | for to_index, to_planet := range data.Planets { | |
122 | best_trade := "(nothing)" | |
123 | if best_trades[from_index][to_index] != "" { | |
124 | best_trade = best_trades[from_index][to_index] | |
125 | } | |
126 | fmt.Printf("%s to %s: %s\n", from_planet.Name, to_planet.Name, best_trade) | |
127 | } | |
128 | } | |
129 | } |