]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | package main | |
19 | ||
20 | import "flag" | |
c45c1bca | 21 | import "fmt" |
69d36952 | 22 | import "encoding/json" |
d07f3caa | 23 | import "os" |
42f6427c | 24 | import "runtime/pprof" |
c45c1bca SW |
25 | import "strings" |
26 | ||
330093c1 SW |
27 | var funds = flag.Int("funds", 0, |
28 | "Starting funds") | |
29 | ||
c45c1bca SW |
30 | var start = flag.String("start", "", |
31 | "The planet to start at") | |
d07f3caa | 32 | |
e346cb37 | 33 | var flight_plan_string = flag.String("flight_plan", "", |
63b4dbbc | 34 | "Your hyper-holes for the day, comma-separated.") |
544108c4 | 35 | |
1c1ede68 | 36 | var end_string = flag.String("end", "", |
e9ff66cf | 37 | "A comma-separated list of acceptable ending planets.") |
c45c1bca SW |
38 | |
39 | var planet_data_file = flag.String("planet_data_file", "planet-data", | |
d07f3caa SW |
40 | "The file to read planet data from") |
41 | ||
63b4dbbc | 42 | var fuel = flag.Int("fuel", 16, "Hyper Jump power left") |
e9ff66cf SW |
43 | |
44 | var hold = flag.Int("hold", 300, "Size of your cargo hold") | |
c45c1bca | 45 | |
a06dc4cb SW |
46 | var start_hold = flag.String("start_hold", "", "Start with a hold full of cargo") |
47 | ||
c45c1bca SW |
48 | var start_edens = flag.Int("start_edens", 0, |
49 | "How many Eden Warp Units are you starting with?") | |
50 | ||
51 | var end_edens = flag.Int("end_edens", 0, | |
52 | "How many Eden Warp Units would you like to keep (not use)?") | |
53 | ||
54 | var cloak = flag.Bool("cloak", false, | |
55 | "Make sure to end with a Device of Cloaking") | |
56 | ||
e9ff66cf | 57 | var drones = flag.Int("drones", 0, "Buy this many Fighter Drones") |
c45c1bca | 58 | |
e9ff66cf | 59 | var batteries = flag.Int("batteries", 0, "Buy this many Shield Batterys") |
c45c1bca | 60 | |
76db1b3c SW |
61 | var drone_price = flag.Int("drone_price", 0, "Today's Fighter Drone price") |
62 | ||
63 | var battery_price = flag.Int("battery_price", 0, "Today's Shield Battery price") | |
64 | ||
c45c1bca SW |
65 | var visit_string = flag.String("visit", "", |
66 | "A comma-separated list of planets to make sure to visit") | |
67 | ||
60025e5d SW |
68 | var tomorrow_weight = flag.Float64("tomorrow_weight", 1.0, |
69 | "Weight for the expected value of tomorrow's trading. 0.0 - 1.0") | |
70 | ||
bc4a3744 SW |
71 | var extra_stats = flag.Bool("extra_stats", true, |
72 | "Show additional information of possible interest") | |
73 | ||
42f6427c SW |
74 | var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") |
75 | ||
42f6427c | 76 | var visit_cache []string |
1539cc25 | 77 | |
c45c1bca | 78 | func visit() []string { |
42f6427c SW |
79 | if visit_cache == nil { |
80 | if *visit_string == "" { | |
81 | return nil | |
82 | } | |
83 | visit_cache = strings.Split(*visit_string, ",") | |
e346cb37 | 84 | } |
42f6427c | 85 | return visit_cache |
c45c1bca SW |
86 | } |
87 | ||
42f6427c | 88 | var flight_plan_cache []string |
1539cc25 | 89 | |
e346cb37 | 90 | func flight_plan() []string { |
42f6427c SW |
91 | if flight_plan_cache == nil { |
92 | if *flight_plan_string == "" { | |
93 | return nil | |
94 | } | |
95 | flight_plan_cache = strings.Split(*flight_plan_string, ",") | |
e346cb37 | 96 | } |
42f6427c | 97 | return flight_plan_cache |
e346cb37 SW |
98 | } |
99 | ||
42f6427c | 100 | var end_cache map[string]bool |
1539cc25 | 101 | |
1c1ede68 | 102 | func end() map[string]bool { |
42f6427c SW |
103 | if end_cache == nil { |
104 | if *end_string == "" { | |
105 | return nil | |
106 | } | |
107 | m := make(map[string]bool) | |
108 | for _, p := range strings.Split(*end_string, ",") { | |
109 | m[p] = true | |
110 | } | |
111 | end_cache = m | |
1c1ede68 | 112 | } |
42f6427c | 113 | return end_cache |
1c1ede68 SW |
114 | } |
115 | ||
9b3b3d9a | 116 | type Commodity struct { |
9b3b3d9a SW |
117 | BasePrice int |
118 | CanSell bool | |
119 | Limit int | |
120 | } | |
12bc2cd7 | 121 | type Planet struct { |
60025e5d SW |
122 | BeaconOn bool |
123 | Private bool | |
124 | TomorrowValue int | |
12bc2cd7 SW |
125 | /* Use relative prices rather than absolute prices because you |
126 | can get relative prices without traveling to each planet. */ | |
0e94bdac | 127 | RelativePrices map[string]int |
12bc2cd7 | 128 | } |
d07f3caa | 129 | type planet_data struct { |
0e94bdac SW |
130 | Commodities map[string]Commodity |
131 | Planets map[string]Planet | |
e7e4bc13 SW |
132 | p2i, c2i map[string]int // Generated; not read from file |
133 | i2p, i2c []string // Generated; not read from file | |
d07f3caa SW |
134 | } |
135 | ||
091c6280 SW |
136 | func json_slurp(filename string, receptacle interface{}) error { |
137 | f, err := os.Open(filename) | |
d07f3caa | 138 | if err != nil { |
091c6280 | 139 | return err |
d07f3caa SW |
140 | } |
141 | defer f.Close() | |
091c6280 SW |
142 | err = json.NewDecoder(f).Decode(receptacle) |
143 | if err != nil { | |
144 | return err | |
145 | } | |
146 | return nil | |
147 | } | |
148 | ||
a18532c2 SW |
149 | func ReadData() planet_data { |
150 | var data planet_data | |
091c6280 | 151 | err := json_slurp(*planet_data_file, &data) |
d07f3caa SW |
152 | if err != nil { |
153 | panic(err) | |
154 | } | |
a18532c2 | 155 | return data |
d07f3caa SW |
156 | } |
157 | ||
c45c1bca SW |
158 | /* This program operates by filling in a state table representing the best |
159 | * possible trips you could make; the ones that makes you the most money. | |
160 | * This is feasible because we don't look at all the possible trips. | |
161 | * We define a list of things that are germane to this game and then only | |
162 | * consider the best outcome in each possible game state. | |
163 | * | |
164 | * Each cell in the table represents a state in the game. In each cell, | |
165 | * we track two things: 1. the most money you could possibly have while in | |
166 | * that state and 2. one possible way to get into that state with that | |
167 | * amount of money. | |
168 | * | |
169 | * A basic analysis can be done with a two-dimensional table: location and | |
170 | * fuel. planeteer-1.0 used this two-dimensional table. This version | |
171 | * adds features mostly by adding dimensions to this table. | |
172 | * | |
173 | * Note that the sizes of each dimension are data driven. Many dimensions | |
174 | * collapse to one possible value (ie, disappear) if the corresponding | |
175 | * feature is not enabled. | |
e7e4bc13 SW |
176 | * |
177 | * The order of the dimensions in the list of constants below determines | |
178 | * their layout in RAM. The cargo-based 'dimensions' are not completely | |
179 | * independent -- some combinations are illegal and not used. They are | |
180 | * handled as three dimensions rather than one for simplicity. Placing | |
181 | * these dimensions first causes the unused cells in the table to be | |
a16bf050 SW |
182 | * grouped together in large blocks. This keeps the unused cells from |
183 | * polluting cache lines, and if the spans of unused cells are large | |
184 | * enough, allows the memory manager to swap out entire pages. | |
e346cb37 SW |
185 | * |
186 | * If the table gets too big to fit in RAM: | |
187 | * * Combine the Edens, Cloaks, and UnusedCargo dimensions. Of the | |
188 | * 24 combinations, only 15 are legal: a 38% savings. | |
a16bf050 SW |
189 | * * Reduce the size of the Fuel dimension to 3. Explicit iteration |
190 | * only ever needs to look backwards 2 units, so the logical values | |
191 | * can rotate through the same 3 physical addresses. This would be | |
192 | * good for an 82% savings. Note that explicit iteration went away | |
193 | * in 0372f045. | |
e346cb37 SW |
194 | * * Reduce the size of the Edens dimension from 3 to 2, for the |
195 | * same reasons as Fuel above. 33% savings. | |
196 | * * Buy more ram. (Just sayin'. It's cheaper than you think.) | |
a49e368f | 197 | * |
c45c1bca SW |
198 | */ |
199 | ||
200 | // The official list of dimensions: | |
201 | const ( | |
0372f045 | 202 | // Name Num Size Description |
1539cc25 SW |
203 | Edens = iota // 1 3 # of Eden warp units (0 - 2 typically) |
204 | Cloaks // 2 1-2 # of Devices of Cloaking (0 or 1) | |
205 | UnusedCargo // 3 4 # of unused cargo spaces (0 - 3 typically) | |
206 | Fuel // 4 17 Hyper jump power left (0 - 16) | |
207 | Location // 5 26 Location (which planet) | |
208 | Hold // 6 15 Cargo bay contents (a *Commodity or nil) | |
209 | Traded // 7 2 Traded yet? | |
210 | BuyFighters // 8 1-2 Errand: Buy fighter drones | |
211 | BuyShields // 9 1-2 Errand: Buy shield batteries | |
212 | Visit // 10 1-2**N Visit: Stop by these N planets in the route | |
c45c1bca SW |
213 | |
214 | NumDimensions | |
215 | ) | |
216 | ||
217 | func bint(b bool) int { | |
0e94bdac SW |
218 | if b { |
219 | return 1 | |
220 | } | |
c45c1bca SW |
221 | return 0 |
222 | } | |
223 | ||
a29aebec | 224 | func DimensionSizes(data planet_data) LogicalIndex { |
c45c1bca | 225 | eden_capacity := data.Commodities["Eden Warp Units"].Limit |
330093c1 SW |
226 | if *start_edens > eden_capacity { |
227 | eden_capacity = *start_edens | |
228 | } | |
c45c1bca | 229 | cloak_capacity := bint(*cloak) |
a29aebec | 230 | dims := make(LogicalIndex, NumDimensions) |
64d87250 SW |
231 | dims[Edens] = eden_capacity + 1 |
232 | dims[Cloaks] = cloak_capacity + 1 | |
233 | dims[UnusedCargo] = eden_capacity + cloak_capacity + 1 | |
234 | dims[Fuel] = *fuel + 1 | |
235 | dims[Location] = len(data.Planets) | |
c67c206a | 236 | dims[Hold] = len(data.Commodities) + 1 |
0372f045 | 237 | dims[Traded] = 2 |
76db1b3c SW |
238 | dims[BuyFighters] = bint(*drones > 0) + 1 |
239 | dims[BuyShields] = bint(*batteries > 0) + 1 | |
64d87250 | 240 | dims[Visit] = 1 << uint(len(visit())) |
2f4ed5ca SW |
241 | |
242 | // Remind myself to add a line above when adding new dimensions | |
243 | for i, dim := range dims { | |
244 | if dim < 1 { | |
245 | panic(i) | |
246 | } | |
247 | } | |
c45c1bca SW |
248 | return dims |
249 | } | |
250 | ||
a29aebec SW |
251 | type Value int32 |
252 | type PhysicalIndex int32 | |
253 | type LogicalIndex []int | |
254 | ||
255 | func StateTableSize(dims LogicalIndex) int { | |
e346cb37 | 256 | product := 1 |
c45c1bca | 257 | for _, size := range dims { |
e346cb37 | 258 | product *= size |
c45c1bca | 259 | } |
e346cb37 | 260 | return product |
c45c1bca SW |
261 | } |
262 | ||
263 | type State struct { | |
fc93fd36 | 264 | value Value |
db81510d | 265 | from PhysicalIndex |
c45c1bca SW |
266 | } |
267 | ||
1539cc25 | 268 | const ( |
fb8eccbf SW |
269 | FROM_ROOT = -2147483647 + iota |
270 | FROM_UNINITIALIZED | |
271 | VALUE_UNINITIALIZED | |
272 | VALUE_BEING_EVALUATED | |
273 | VALUE_RUBISH | |
1539cc25 | 274 | ) |
0372f045 | 275 | |
a29aebec | 276 | func EncodeIndex(dims, addr LogicalIndex) PhysicalIndex { |
688733e1 | 277 | index := addr[0] |
e346cb37 SW |
278 | if addr[0] > dims[0] { |
279 | panic(0) | |
280 | } | |
330093c1 | 281 | for i := 1; i < NumDimensions; i++ { |
688733e1 | 282 | if addr[i] < 0 || addr[i] >= dims[i] { |
e346cb37 SW |
283 | panic(i) |
284 | } | |
688733e1 | 285 | index = index*dims[i] + addr[i] |
c45c1bca | 286 | } |
db81510d | 287 | return PhysicalIndex(index) |
c45c1bca SW |
288 | } |
289 | ||
a29aebec | 290 | func DecodeIndex(dims LogicalIndex, index PhysicalIndex) LogicalIndex { |
db81510d | 291 | scratch := int(index) |
a29aebec | 292 | addr := make(LogicalIndex, NumDimensions) |
330093c1 | 293 | for i := NumDimensions - 1; i > 0; i-- { |
db81510d SW |
294 | addr[i] = scratch % dims[i] |
295 | scratch /= dims[i] | |
c45c1bca | 296 | } |
db81510d | 297 | addr[0] = scratch |
c45c1bca SW |
298 | return addr |
299 | } | |
300 | ||
dd6c757d SW |
301 | func PlanetIndex(data planet_data, name string) int { |
302 | index, ok := data.p2i[name] | |
303 | if !ok { | |
304 | panic("Unknown planet " + name) | |
305 | } | |
306 | return index | |
307 | } | |
308 | ||
309 | func CommodityIndex(data planet_data, name string) int { | |
310 | index, ok := data.c2i[name] | |
311 | if !ok { | |
312 | panic("Unknown commodity " + name) | |
313 | } | |
314 | return index | |
315 | } | |
316 | ||
a29aebec | 317 | func CreateStateTable(data planet_data, dims LogicalIndex) []State { |
330093c1 | 318 | table := make([]State, StateTableSize(dims)) |
0372f045 | 319 | for i := range table { |
fb8eccbf SW |
320 | table[i].value = VALUE_UNINITIALIZED |
321 | table[i].from = FROM_UNINITIALIZED | |
0372f045 | 322 | } |
330093c1 | 323 | |
a29aebec | 324 | addr := make(LogicalIndex, NumDimensions) |
330093c1 SW |
325 | addr[Fuel] = *fuel |
326 | addr[Edens] = *start_edens | |
dd6c757d | 327 | addr[Location] = PlanetIndex(data, *start) |
a06dc4cb | 328 | if *start_hold != "" { |
a49e368f | 329 | addr[Hold] = CommodityIndex(data, *start_hold) |
a06dc4cb | 330 | } |
fb8eccbf | 331 | start_index := EncodeIndex(dims, addr) |
fc93fd36 | 332 | table[start_index].value = Value(*funds) |
fb8eccbf | 333 | table[start_index].from = FROM_ROOT |
330093c1 SW |
334 | |
335 | return table | |
e346cb37 SW |
336 | } |
337 | ||
0372f045 SW |
338 | /* CellValue fills in the one cell at address addr by looking at all |
339 | * the possible ways to reach this cell and selecting the best one. */ | |
330093c1 | 340 | |
a29aebec | 341 | func Consider(data planet_data, dims LogicalIndex, table []State, there LogicalIndex, value_difference int, best_value *Value, best_source LogicalIndex) { |
0372f045 | 342 | there_value := CellValue(data, dims, table, there) |
fc93fd36 | 343 | if value_difference < 0 && Value(-value_difference) > there_value { |
0372f045 SW |
344 | /* Can't afford this transition */ |
345 | return | |
346 | } | |
fc93fd36 | 347 | possible_value := there_value + Value(value_difference) |
0372f045 SW |
348 | if possible_value > *best_value { |
349 | *best_value = possible_value | |
350 | copy(best_source, there) | |
330093c1 SW |
351 | } |
352 | } | |
353 | ||
0372f045 | 354 | var cell_filled_count int |
1539cc25 | 355 | |
a29aebec | 356 | func CellValue(data planet_data, dims LogicalIndex, table []State, addr LogicalIndex) Value { |
e346cb37 | 357 | my_index := EncodeIndex(dims, addr) |
fb8eccbf | 358 | if table[my_index].value == VALUE_BEING_EVALUATED { |
0372f045 SW |
359 | panic("Circular dependency") |
360 | } | |
fb8eccbf | 361 | if table[my_index].value != VALUE_UNINITIALIZED { |
0372f045 SW |
362 | return table[my_index].value |
363 | } | |
fb8eccbf | 364 | table[my_index].value = VALUE_BEING_EVALUATED |
0372f045 | 365 | |
fc93fd36 | 366 | best_value := Value(VALUE_RUBISH) |
a29aebec SW |
367 | best_source := make(LogicalIndex, NumDimensions) |
368 | other := make(LogicalIndex, NumDimensions) | |
e346cb37 | 369 | copy(other, addr) |
0372f045 | 370 | planet := data.i2p[addr[Location]] |
e346cb37 | 371 | |
0372f045 SW |
372 | /* Travel here */ |
373 | if addr[Traded] == 0 { /* Can't have traded immediately after traveling. */ | |
374 | other[Traded] = 1 /* Travel from states that have done trading. */ | |
375 | ||
376 | /* Travel here via a 2-fuel unit jump */ | |
58c3880d | 377 | if data.Planets[data.i2p[addr[Location]]].BeaconOn && addr[Fuel]+2 < dims[Fuel] { |
0372f045 SW |
378 | other[Fuel] = addr[Fuel] + 2 |
379 | hole_index := (dims[Fuel] - 1) - (addr[Fuel] + 2) | |
dd6c757d | 380 | if hole_index >= len(flight_plan()) || addr[Location] != PlanetIndex(data, flight_plan()[hole_index]) { |
0372f045 | 381 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { |
58c3880d | 382 | Consider(data, dims, table, other, 0, &best_value, best_source) |
0372f045 | 383 | } |
beb45aca | 384 | } |
0372f045 SW |
385 | other[Location] = addr[Location] |
386 | other[Fuel] = addr[Fuel] | |
e346cb37 | 387 | } |
e346cb37 | 388 | |
0372f045 SW |
389 | /* Travel here via a 1-fuel unit jump (a hyper hole) */ |
390 | if addr[Fuel]+1 < dims[Fuel] { | |
391 | hole_index := (dims[Fuel] - 1) - (addr[Fuel] + 1) | |
dd6c757d | 392 | if hole_index < len(flight_plan()) && addr[Location] == PlanetIndex(data, flight_plan()[hole_index]) { |
0372f045 SW |
393 | other[Fuel] = addr[Fuel] + 1 |
394 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
395 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
396 | } | |
397 | other[Location] = addr[Location] | |
398 | other[Fuel] = addr[Fuel] | |
7b5d9d13 | 399 | } |
e346cb37 | 400 | } |
e346cb37 | 401 | |
0372f045 | 402 | /* Travel here via Eden Warp Unit */ |
35e3c424 | 403 | if addr[Edens]+1 < dims[Edens] && (addr[Hold] == 0 || addr[UnusedCargo] > 0) { |
0372f045 SW |
404 | _, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Eden Warp Units"] |
405 | if !available { | |
406 | other[Edens] = addr[Edens] + 1 | |
407 | if other[Hold] != 0 { | |
408 | other[UnusedCargo] = addr[UnusedCargo] - 1 | |
409 | } | |
410 | for other[Location] = 0; other[Location] < dims[Location]; other[Location]++ { | |
411 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
412 | } | |
413 | other[Location] = addr[Location] | |
414 | other[UnusedCargo] = addr[UnusedCargo] | |
415 | other[Edens] = addr[Edens] | |
0c27c344 | 416 | } |
330093c1 | 417 | } |
0372f045 | 418 | other[Traded] = addr[Traded] |
330093c1 | 419 | } |
330093c1 | 420 | |
0372f045 SW |
421 | /* Trade */ |
422 | if addr[Traded] == 1 { | |
423 | other[Traded] = 0 | |
330093c1 | 424 | |
0372f045 SW |
425 | /* Consider not trading */ |
426 | Consider(data, dims, table, other, 0, &best_value, best_source) | |
330093c1 | 427 | |
0372f045 | 428 | if !data.Planets[data.i2p[addr[Location]]].Private { |
330093c1 | 429 | |
0372f045 SW |
430 | /* Sell */ |
431 | if addr[Hold] == 0 && addr[UnusedCargo] == 0 { | |
432 | for other[Hold] = 0; other[Hold] < dims[Hold]; other[Hold]++ { | |
433 | commodity := data.i2c[other[Hold]] | |
434 | if !data.Commodities[commodity].CanSell { | |
435 | continue | |
436 | } | |
437 | relative_price, available := data.Planets[planet].RelativePrices[commodity] | |
438 | if !available { | |
439 | // TODO: Dump cargo | |
440 | continue | |
441 | } | |
442 | base_price := data.Commodities[commodity].BasePrice | |
443 | absolute_price := float64(base_price) * float64(relative_price) / 100.0 | |
444 | sell_price := int(absolute_price * 0.9) | |
445 | ||
446 | for other[UnusedCargo] = 0; other[UnusedCargo] < dims[UnusedCargo]; other[UnusedCargo]++ { | |
447 | quantity := *hold - (other[UnusedCargo] + other[Cloaks] + other[Edens]) | |
448 | sale_value := quantity * sell_price | |
449 | Consider(data, dims, table, other, sale_value, &best_value, best_source) | |
450 | } | |
451 | } | |
452 | other[UnusedCargo] = addr[UnusedCargo] | |
453 | other[Hold] = addr[Hold] | |
454 | } | |
330093c1 | 455 | |
0372f045 SW |
456 | /* Buy */ |
457 | other[Traded] = addr[Traded] /* Buy after selling */ | |
458 | if addr[Hold] != 0 { | |
459 | commodity := data.i2c[addr[Hold]] | |
460 | if data.Commodities[commodity].CanSell { | |
461 | relative_price, available := data.Planets[planet].RelativePrices[commodity] | |
462 | if available { | |
463 | base_price := data.Commodities[commodity].BasePrice | |
464 | absolute_price := int(float64(base_price) * float64(relative_price) / 100.0) | |
465 | quantity := *hold - (addr[UnusedCargo] + addr[Cloaks] + addr[Edens]) | |
466 | total_price := quantity * absolute_price | |
467 | other[Hold] = 0 | |
468 | other[UnusedCargo] = 0 | |
469 | Consider(data, dims, table, other, -total_price, &best_value, best_source) | |
470 | other[UnusedCargo] = addr[UnusedCargo] | |
471 | other[Hold] = addr[Hold] | |
472 | } | |
473 | } | |
474 | } | |
475 | } | |
6918cad2 | 476 | other[Traded] = addr[Traded] |
0372f045 | 477 | } |
d16f3322 | 478 | |
544108c4 | 479 | /* Buy a Device of Cloaking */ |
ada59973 SW |
480 | if addr[Cloaks] == 1 && addr[UnusedCargo] < dims[UnusedCargo]-1 { |
481 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Device Of Cloakings"] | |
482 | if available { | |
483 | absolute_price := int(float64(data.Commodities["Device Of Cloakings"].BasePrice) * float64(relative_price) / 100.0) | |
484 | other[Cloaks] = 0 | |
f800f732 SW |
485 | if other[Hold] != 0 { |
486 | other[UnusedCargo] = addr[UnusedCargo] + 1 | |
487 | } | |
0372f045 | 488 | Consider(data, dims, table, other, -absolute_price, &best_value, best_source) |
ada59973 SW |
489 | other[UnusedCargo] = addr[UnusedCargo] |
490 | other[Cloaks] = addr[Cloaks] | |
491 | } | |
492 | } | |
76db1b3c | 493 | |
544108c4 | 494 | /* Buy Fighter Drones */ |
76db1b3c SW |
495 | if addr[BuyFighters] == 1 { |
496 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Fighter Drones"] | |
497 | if available { | |
498 | absolute_price := int(float64(data.Commodities["Fighter Drones"].BasePrice) * float64(relative_price) / 100.0) | |
499 | other[BuyFighters] = 0 | |
1539cc25 | 500 | Consider(data, dims, table, other, -absolute_price**drones, &best_value, best_source) |
76db1b3c SW |
501 | other[BuyFighters] = addr[BuyFighters] |
502 | } | |
503 | } | |
504 | ||
544108c4 | 505 | /* Buy Shield Batteries */ |
76db1b3c SW |
506 | if addr[BuyShields] == 1 { |
507 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Shield Batterys"] | |
508 | if available { | |
509 | absolute_price := int(float64(data.Commodities["Shield Batterys"].BasePrice) * float64(relative_price) / 100.0) | |
510 | other[BuyShields] = 0 | |
1539cc25 | 511 | Consider(data, dims, table, other, -absolute_price**batteries, &best_value, best_source) |
76db1b3c SW |
512 | other[BuyShields] = addr[BuyShields] |
513 | } | |
514 | } | |
515 | ||
544108c4 | 516 | /* Visit this planet */ |
b7a6e28b | 517 | for i := uint(0); i < uint(len(visit())); i++ { |
1539cc25 | 518 | if addr[Visit]&(1<<i) != 0 && visit()[i] == data.i2p[addr[Location]] { |
4d7362df | 519 | other[Visit] = addr[Visit] & ^(1 << i) |
0372f045 | 520 | Consider(data, dims, table, other, 0, &best_value, best_source) |
4d7362df SW |
521 | } |
522 | } | |
523 | other[Visit] = addr[Visit] | |
76db1b3c | 524 | |
d16f3322 SW |
525 | /* Buy Eden warp units */ |
526 | eden_limit := data.Commodities["Eden Warp Units"].Limit | |
527 | if addr[Edens] > 0 && addr[Edens] <= eden_limit { | |
528 | relative_price, available := data.Planets[data.i2p[addr[Location]]].RelativePrices["Eden Warp Units"] | |
529 | if available { | |
530 | absolute_price := int(float64(data.Commodities["Eden Warp Units"].BasePrice) * float64(relative_price) / 100.0) | |
531 | for quantity := addr[Edens]; quantity > 0; quantity-- { | |
532 | other[Edens] = addr[Edens] - quantity | |
533 | if addr[Hold] != 0 { | |
534 | other[UnusedCargo] = addr[UnusedCargo] + quantity | |
535 | } | |
536 | if other[UnusedCargo] < dims[UnusedCargo] { | |
1539cc25 | 537 | Consider(data, dims, table, other, -absolute_price*quantity, &best_value, best_source) |
d16f3322 SW |
538 | } |
539 | } | |
540 | other[Edens] = addr[Edens] | |
541 | other[UnusedCargo] = addr[UnusedCargo] | |
542 | } | |
543 | } | |
d16f3322 | 544 | |
688733e1 SW |
545 | // Check that we didn't lose track of any temporary modifications to other. |
546 | for i := 0; i < NumDimensions; i++ { | |
547 | if addr[i] != other[i] { | |
548 | panic(i) | |
549 | } | |
550 | } | |
551 | ||
552 | // Sanity check: This cell was in state BEING_EVALUATED | |
553 | // the whole time that it was being evaluated. | |
fb8eccbf | 554 | if table[my_index].value != VALUE_BEING_EVALUATED { |
0372f045 | 555 | panic(my_index) |
e7e4bc13 | 556 | } |
688733e1 SW |
557 | |
558 | // Record our findings | |
0372f045 SW |
559 | table[my_index].value = best_value |
560 | table[my_index].from = EncodeIndex(dims, best_source) | |
330093c1 | 561 | |
688733e1 | 562 | // UI: Progress bar |
1539cc25 | 563 | cell_filled_count++ |
c5ac83ce | 564 | if cell_filled_count&0xfff == 0 { |
0372f045 | 565 | print(fmt.Sprintf("\r%3.1f%%", 100*float64(cell_filled_count)/float64(StateTableSize(dims)))) |
fcdc120b | 566 | } |
e7e4bc13 | 567 | |
0372f045 | 568 | return table[my_index].value |
e7e4bc13 SW |
569 | } |
570 | ||
a29aebec SW |
571 | func FinalState(dims LogicalIndex) LogicalIndex { |
572 | addr := make(LogicalIndex, NumDimensions) | |
ad4de13f SW |
573 | addr[Edens] = *end_edens |
574 | addr[Cloaks] = dims[Cloaks] - 1 | |
76db1b3c SW |
575 | addr[BuyFighters] = dims[BuyFighters] - 1 |
576 | addr[BuyShields] = dims[BuyShields] - 1 | |
ad4de13f | 577 | addr[Visit] = dims[Visit] - 1 |
0372f045 | 578 | addr[Traded] = 1 |
688733e1 SW |
579 | addr[Hold] = 0 |
580 | addr[UnusedCargo] = 0 | |
7881bdfb SW |
581 | // Fuel and Location are determined by FindBestState |
582 | return addr | |
583 | } | |
584 | ||
a29aebec | 585 | func FindBestState(data planet_data, dims LogicalIndex, table []State, addr LogicalIndex) PhysicalIndex { |
db81510d | 586 | max_index := PhysicalIndex(-1) |
60025e5d | 587 | max_value := 0.0 |
688733e1 SW |
588 | max_fuel := 1 |
589 | if *fuel == 0 { | |
590 | max_fuel = 0 | |
591 | } | |
592 | for addr[Fuel] = 0; addr[Fuel] <= max_fuel; addr[Fuel]++ { | |
ddef04ab | 593 | for addr[Location] = 0; addr[Location] < dims[Location]; addr[Location]++ { |
60025e5d SW |
594 | planet := data.i2p[addr[Location]] |
595 | if len(end()) == 0 || end()[planet] { | |
ddef04ab | 596 | index := EncodeIndex(dims, addr) |
60025e5d SW |
597 | today_value := CellValue(data, dims, table, addr) |
598 | tomorrow_value := *tomorrow_weight * float64(*hold+data.Planets[planet].TomorrowValue) | |
599 | value := float64(today_value) + tomorrow_value | |
0372f045 SW |
600 | if value > max_value { |
601 | max_value = value | |
ddef04ab SW |
602 | max_index = index |
603 | } | |
809e65f4 | 604 | } |
ad4de13f SW |
605 | } |
606 | } | |
607 | return max_index | |
608 | } | |
609 | ||
a18532c2 SW |
610 | func Commas(n Value) string { |
611 | var s string | |
b7a6e28b SW |
612 | if n < 0 { |
613 | panic(n) | |
614 | } | |
9eafb7a4 SW |
615 | r := n % 1000 |
616 | n /= 1000 | |
617 | for n > 0 { | |
618 | s = fmt.Sprintf(",%03d", r) + s | |
619 | r = n % 1000 | |
620 | n /= 1000 | |
621 | } | |
622 | s = fmt.Sprint(r) + s | |
a18532c2 | 623 | return s |
9eafb7a4 SW |
624 | } |
625 | ||
a29aebec | 626 | func FighterAndShieldCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) { |
a5a4d824 SW |
627 | if *drones == 0 && *batteries == 0 { |
628 | return | |
629 | } | |
630 | fmt.Println() | |
631 | if *drones > 0 { | |
632 | final_state := FinalState(dims) | |
633 | final_state[BuyFighters] = 0 | |
634 | alt_best := FindBestState(data, dims, table, final_state) | |
635 | cost := table[alt_best].value - table[best].value | |
785c799c | 636 | fmt.Printf("\rDrones were %.2f each\n", float64(cost)/float64(*drones)) |
a5a4d824 SW |
637 | } |
638 | if *batteries > 0 { | |
639 | final_state := FinalState(dims) | |
640 | final_state[BuyShields] = 0 | |
641 | alt_best := FindBestState(data, dims, table, final_state) | |
642 | cost := table[alt_best].value - table[best].value | |
785c799c | 643 | fmt.Printf("\rBatteries were %.2f each\n", float64(cost)/float64(*batteries)) |
a5a4d824 SW |
644 | } |
645 | } | |
646 | ||
a29aebec | 647 | func EndEdensCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) { |
a5a4d824 SW |
648 | if *end_edens == 0 { |
649 | return | |
650 | } | |
651 | fmt.Println() | |
652 | final_state := FinalState(dims) | |
653 | for extra_edens := 1; extra_edens <= *end_edens; extra_edens++ { | |
654 | final_state[Edens] = *end_edens - extra_edens | |
655 | alt_best := FindBestState(data, dims, table, final_state) | |
656 | extra_funds := table[alt_best].value - table[best].value | |
657 | fmt.Println("\rUse", extra_edens, "extra edens, make an extra", | |
658 | Commas(extra_funds), "(", | |
fc93fd36 | 659 | Commas(extra_funds/Value(extra_edens)), "per eden)") |
a5a4d824 SW |
660 | } |
661 | } | |
662 | ||
a29aebec | 663 | func VisitCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) { |
a5a4d824 SW |
664 | if dims[Visit] == 1 { |
665 | return | |
666 | } | |
667 | fmt.Println() | |
668 | final_state := FinalState(dims) | |
669 | for i := uint(0); i < uint(len(visit())); i++ { | |
670 | all_bits := dims[Visit] - 1 | |
671 | final_state[Visit] = all_bits & ^(1 << i) | |
672 | alt_best := FindBestState(data, dims, table, final_state) | |
673 | cost := table[alt_best].value - table[best].value | |
674 | fmt.Printf("\r%11v Cost to visit %v\n", Commas(cost), visit()[i]) | |
675 | } | |
676 | } | |
677 | ||
a29aebec | 678 | func EndLocationCost(data planet_data, dims LogicalIndex, table []State, best PhysicalIndex) { |
a5a4d824 SW |
679 | if len(end()) == 0 { |
680 | return | |
681 | } | |
682 | fmt.Println() | |
683 | final_state := FinalState(dims) | |
684 | save_end_string := *end_string | |
685 | *end_string = "" | |
686 | end_cache = nil | |
687 | alt_best := FindBestState(data, dims, table, final_state) | |
688 | cost := table[alt_best].value - table[best].value | |
689 | fmt.Printf("\r%11v Cost of --end %v\n", Commas(cost), save_end_string) | |
690 | *end_string = save_end_string | |
691 | } | |
692 | ||
a18532c2 SW |
693 | func DescribePath(data planet_data, dims LogicalIndex, table []State, start PhysicalIndex) []string { |
694 | var description []string | |
fb8eccbf SW |
695 | for index := start; table[index].from > FROM_ROOT; index = table[index].from { |
696 | if table[index].from == FROM_UNINITIALIZED { | |
697 | panic(index) | |
698 | } | |
e4a1b48f | 699 | var line string |
2f4a9ae8 SW |
700 | addr := DecodeIndex(dims, index) |
701 | prev := DecodeIndex(dims, table[index].from) | |
e4a1b48f | 702 | if addr[Fuel] != prev[Fuel] { |
2f4a9ae8 SW |
703 | from := data.i2p[prev[Location]] |
704 | to := data.i2p[addr[Location]] | |
63b4dbbc | 705 | line += fmt.Sprintf("Jump from %v to %v (%v hyper jump units)", from, to, prev[Fuel]-addr[Fuel]) |
e4a1b48f | 706 | } |
1539cc25 | 707 | if addr[Edens] == prev[Edens]-1 { |
e4a1b48f SW |
708 | from := data.i2p[prev[Location]] |
709 | to := data.i2p[addr[Location]] | |
710 | line += fmt.Sprintf("Eden warp from %v to %v", from, to) | |
2f4a9ae8 SW |
711 | } |
712 | if addr[Hold] != prev[Hold] { | |
713 | if addr[Hold] == 0 { | |
714 | quantity := *hold - (prev[UnusedCargo] + prev[Edens] + prev[Cloaks]) | |
e4a1b48f | 715 | line += fmt.Sprintf("Sell %v %v", quantity, data.i2c[prev[Hold]]) |
2f4a9ae8 SW |
716 | } else if prev[Hold] == 0 { |
717 | quantity := *hold - (addr[UnusedCargo] + addr[Edens] + addr[Cloaks]) | |
e4a1b48f | 718 | line += fmt.Sprintf("Buy %v %v", quantity, data.i2c[addr[Hold]]) |
2f4a9ae8 SW |
719 | } else { |
720 | panic("Switched cargo?") | |
721 | } | |
722 | ||
723 | } | |
f800f732 SW |
724 | if addr[Cloaks] == 1 && prev[Cloaks] == 0 { |
725 | // TODO: Dump cloaks, convert from cargo? | |
e4a1b48f SW |
726 | line += "Buy a Cloak" |
727 | } | |
d16f3322 | 728 | if addr[Edens] > prev[Edens] { |
1539cc25 | 729 | line += fmt.Sprint("Buy ", addr[Edens]-prev[Edens], " Eden Warp Units") |
e4a1b48f | 730 | } |
76db1b3c SW |
731 | if addr[BuyShields] == 1 && prev[BuyShields] == 0 { |
732 | line += fmt.Sprint("Buy ", *batteries, " Shield Batterys") | |
733 | } | |
734 | if addr[BuyFighters] == 1 && prev[BuyFighters] == 0 { | |
735 | line += fmt.Sprint("Buy ", *drones, " Fighter Drones") | |
736 | } | |
4d7362df SW |
737 | if addr[Visit] != prev[Visit] { |
738 | // TODO: verify that the bit chat changed is addr[Location] | |
739 | line += fmt.Sprint("Visit ", data.i2p[addr[Location]]) | |
740 | } | |
0372f045 SW |
741 | if line == "" && addr[Hold] == prev[Hold] && addr[Traded] != prev[Traded] { |
742 | // The Traded dimension is for housekeeping. It doesn't directly | |
743 | // correspond to in-game actions, so don't report transitions. | |
744 | continue | |
745 | } | |
e4a1b48f SW |
746 | if line == "" { |
747 | line = fmt.Sprint(prev, " -> ", addr) | |
f800f732 | 748 | } |
1539cc25 | 749 | description = append(description, fmt.Sprintf("%13v ", Commas(table[index].value))+line) |
2f4a9ae8 | 750 | } |
a18532c2 | 751 | return description |
2f4a9ae8 SW |
752 | } |
753 | ||
c45c1bca | 754 | // (Example of a use case for generics in Go) |
e7e4bc13 | 755 | func IndexPlanets(m *map[string]Planet, start_at int) (map[string]int, []string) { |
a1f10151 SW |
756 | e2i := make(map[string]int, len(*m)+start_at) |
757 | i2e := make([]string, len(*m)+start_at) | |
e7e4bc13 | 758 | i := start_at |
c45c1bca | 759 | for e := range *m { |
e7e4bc13 SW |
760 | e2i[e] = i |
761 | i2e[i] = e | |
c45c1bca SW |
762 | i++ |
763 | } | |
e7e4bc13 | 764 | return e2i, i2e |
c45c1bca | 765 | } |
e7e4bc13 | 766 | func IndexCommodities(m *map[string]Commodity, start_at int) (map[string]int, []string) { |
a1f10151 SW |
767 | e2i := make(map[string]int, len(*m)+start_at) |
768 | i2e := make([]string, len(*m)+start_at) | |
e7e4bc13 | 769 | i := start_at |
c45c1bca | 770 | for e := range *m { |
e7e4bc13 SW |
771 | e2i[e] = i |
772 | i2e[i] = e | |
c45c1bca SW |
773 | i++ |
774 | } | |
e7e4bc13 | 775 | return e2i, i2e |
c45c1bca SW |
776 | } |
777 | ||
d07f3caa SW |
778 | func main() { |
779 | flag.Parse() | |
311b26d4 SW |
780 | if *start == "" || *funds == 0 { |
781 | print("--start and --funds are required. --help for more\n") | |
782 | return | |
783 | } | |
42f6427c SW |
784 | if *cpuprofile != "" { |
785 | f, err := os.Create(*cpuprofile) | |
786 | if err != nil { | |
787 | panic(err) | |
788 | } | |
789 | pprof.StartCPUProfile(f) | |
790 | defer pprof.StopCPUProfile() | |
791 | } | |
d07f3caa | 792 | data := ReadData() |
76db1b3c SW |
793 | if *drone_price > 0 { |
794 | temp := data.Commodities["Fighter Drones"] | |
795 | temp.BasePrice = *drone_price | |
796 | data.Commodities["Fighter Drones"] = temp | |
797 | } | |
798 | if *battery_price > 0 { | |
799 | temp := data.Commodities["Shield Batterys"] | |
800 | temp.BasePrice = *battery_price | |
801 | data.Commodities["Shield Batterys"] = temp | |
802 | } | |
e7e4bc13 SW |
803 | data.p2i, data.i2p = IndexPlanets(&data.Planets, 0) |
804 | data.c2i, data.i2c = IndexCommodities(&data.Commodities, 1) | |
c45c1bca | 805 | dims := DimensionSizes(data) |
0372f045 | 806 | table := CreateStateTable(data, dims) |
7881bdfb SW |
807 | final_state := FinalState(dims) |
808 | best := FindBestState(data, dims, table, final_state) | |
0372f045 | 809 | print("\n") |
ada59973 | 810 | if best == -1 { |
7393f03a | 811 | print("Cannot achieve success criteria\n") |
7881bdfb SW |
812 | return |
813 | } | |
814 | description := DescribePath(data, dims, table, best) | |
815 | for i := len(description) - 1; i >= 0; i-- { | |
816 | fmt.Println(description[i]) | |
817 | } | |
818 | ||
bc4a3744 SW |
819 | if *extra_stats { |
820 | FighterAndShieldCost(data, dims, table, best) | |
821 | EndEdensCost(data, dims, table, best) | |
822 | VisitCost(data, dims, table, best) | |
823 | EndLocationCost(data, dims, table, best) | |
824 | } | |
d07f3caa | 825 | } |