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