]> git.scottworley.com Git - planeteer/commitdiff
Index commodities by their name.
authorScott Worley <sworley@chkno.net>
Mon, 24 Oct 2011 14:57:12 +0000 (07:57 -0700)
committerScott Worley <sworley@chkno.net>
Mon, 24 Oct 2011 14:57:12 +0000 (07:57 -0700)
Also, use initial_funds and max_quantity in TradeValue()

planet-data
planeteer.go

index 3ead4ffacd5fe0fbe1a7bbb882457b0bdf5eb0b7..f9d119f5d77bb7ef390d6a7a80985baee8705be3 100644 (file)
 {
-       "Commodities": [
-               {
-                       "Name": "AntiCloak Scanners",
+       "Commodities": {
+               "AntiCloak Scanners": {
                        "BasePrice": 100000,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Big Cargo Spaces",
+               "Big Cargo Spaces": {
                        "BasePrice": 0,
                        "CanSell": false,
                        "Limit": 0
                },
-               {
-                       "Name": "Clothes Bundles",
+               "Clothes Bundles": {
                        "BasePrice": 450,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Device Of Cloakings",
+               "Device Of Cloakings": {
                        "BasePrice": 5000,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Eden Warp Units",
+               "Eden Warp Units": {
                        "BasePrice": 25000,
                        "CanSell": true,
                        "Limit": 2
                },
-               {
-                       "Name": "Fighter Drones",
+               "Fighter Drones": {
                        "BasePrice": 200,
                        "CanSell": false,
                        "Limit": 0
                },
-               {
-                       "Name": "Ground Weapons",
+               "Ground Weapons": {
                        "BasePrice": 350,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Heating Units",
+               "Heating Units": {
                        "BasePrice": 1500,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Ice Units",
+               "Ice Units": {
                        "BasePrice": 200,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Jewels",
+               "Jewels": {
                        "BasePrice": 2000,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Medical Units",
+               "Medical Units": {
                        "BasePrice": 500,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Novelty Packs",
+               "Novelty Packs": {
                        "BasePrice": 100,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Onboard Printing Kits",
+               "Onboard Printing Kits": {
                        "BasePrice": 100000,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Plastic Trinkets",
+               "Plastic Trinkets": {
                        "BasePrice": 150,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Reactor Units",
+               "Reactor Units": {
                        "BasePrice": 100000,
                        "CanSell": false,
                        "Limit": 0
                },
-               {
-                       "Name": "Shield Batterys",
+               "Shield Batterys": {
                        "BasePrice": 100,
                        "CanSell": false,
                        "Limit": 0
                },
-               {
-                       "Name": "Tree Growth Kits",
+               "Tree Growth Kits": {
                        "BasePrice": 800,
                        "CanSell": true,
                        "Limit": 0
                },
-               {
-                       "Name": "Utopia Planetary Defenses",
+               "Utopia Planetary Defenses": {
                        "BasePrice": 5000000,
                        "CanSell": true,
                        "Limit": 0
                }
-       ],
+       },
        "Planets": [
                {
                        "Name": "Earth",
index 57f00ecc0780668ce6bf00f08ff8f121542c9da7..bc47c3d0320aeecde0d612f3f331ef5fa46353b6 100644 (file)
@@ -26,7 +26,6 @@ var datafile = flag.String("planet_data_file", "planet-data",
        "The file to read planet data from")
 
 type Commodity struct {
-       Name      string
        BasePrice int
        CanSell   bool
        Limit     int
@@ -39,7 +38,7 @@ type Planet struct {
        RelativePrices map [string] int
 }
 type planet_data struct {
-       Commodities []Commodity
+       Commodities map [string] Commodity
        Planets []Planet
 }
 
@@ -56,42 +55,56 @@ func ReadData() (data planet_data) {
        return
 }
 
-func TradeValue(from, to *Planet,
-                commodity *Commodity,
-                quantity int) int {
-       if !commodity.CanSell {
+/* What is the value of hauling 'commodity' from 'from' to 'to'?
+ * Take into account the available funds and the available cargo space. */
+func TradeValue(data planet_data,
+                from, to *Planet,
+                commodity string,
+                initial_funds, max_quantity int) int {
+       if !data.Commodities[commodity].CanSell {
                return 0
        }
-       from_relative_price, from_available := from.RelativePrices[commodity.Name]
+       from_relative_price, from_available := from.RelativePrices[commodity]
        if !from_available {
                return 0
        }
-       to_relative_price, to_available := to.RelativePrices[commodity.Name]
+       to_relative_price, to_available := to.RelativePrices[commodity]
        if !to_available {
                return 0
        }
 
-       from_absolute_price := from_relative_price * commodity.BasePrice
-       to_absolute_price := to_relative_price * commodity.BasePrice
+       base_price := data.Commodities[commodity].BasePrice
+       from_absolute_price := from_relative_price * base_price
+       to_absolute_price := to_relative_price * base_price
        buy_price := from_absolute_price
        sell_price := int(float64(to_absolute_price) * 0.9)
-       return (sell_price - buy_price) * quantity
-
+       var can_afford int = initial_funds / buy_price
+       quantity := can_afford
+       if quantity > max_quantity {
+               quantity = max_quantity
+       }
+       return (sell_price - buy_price) * max_quantity
 }
 
-func FindBestTrades(data planet_data) [][]*Commodity {
-       best := make([][]*Commodity, len(data.Planets))
-       for from_index := range data.Planets {
-               best[from_index] = make([]*Commodity, len(data.Planets))
-               for to_index := range data.Planets {
+func FindBestTrades(data planet_data) [][]string {
+       best := make([][]string, len(data.Planets))
+       for from_index, from_planet := range data.Planets {
+               best[from_index] = make([]string, len(data.Planets))
+               for to_index, to_planet := range data.Planets {
                        best_gain := 0
-                       for commodity_index := range data.Commodities {
-                               gain := TradeValue(&data.Planets[from_index],
-                                                  &data.Planets[to_index],
-                                                  &data.Commodities[commodity_index],
+                       price_list := from_planet.RelativePrices
+                       if len(to_planet.RelativePrices) < len(from_planet.RelativePrices) {
+                               price_list = to_planet.RelativePrices
+                       }
+                       for commodity := range price_list {
+                               gain := TradeValue(data,
+                                                  &from_planet,
+                                                  &to_planet,
+                                                  commodity,
+                                                  10000000,
                                                   1)
                                if gain > best_gain {
-                                       best[from_index][to_index] = &data.Commodities[commodity_index]
+                                       best[from_index][to_index] = commodity
                                        gain = best_gain
                                }
                        }
@@ -107,8 +120,8 @@ func main() {
        for from_index, from_planet := range data.Planets {
                for to_index, to_planet := range data.Planets {
                        best_trade := "(nothing)"
-                       if best_trades[from_index][to_index] != nil {
-                               best_trade = best_trades[from_index][to_index].Name
+                       if best_trades[from_index][to_index] != "" {
+                               best_trade = best_trades[from_index][to_index]
                        }
                        fmt.Printf("%s to %s: %s\n", from_planet.Name, to_planet.Name, best_trade)
                }