-func FillCellByBuying(data planet_data, dims []int, table []State, addr []int) {
- if addr[Hold] == 0 {
- // Can't buy and then have nothing
- return
- }
- my_index := EncodeIndex(dims, addr)
- other := make([]int, NumDimensions)
- copy(other, addr)
- planet := data.i2p[addr[Location]]
- commodity := data.i2c[addr[Hold]]
- if !data.Commodities[commodity].CanSell {
- return
- }
- relative_price, available := data.Planets[planet].RelativePrices[commodity]
- if !available {
- return
+ if !data.Planets[data.i2p[addr[Location]]].Private {
+
+ /* Sell */
+ if addr[Hold] == 0 && addr[UnusedCargo] == 0 {
+ for other[Hold] = 0; other[Hold] < dims[Hold]; other[Hold]++ {
+ commodity := data.i2c[other[Hold]]
+ if !data.Commodities[commodity].CanSell {
+ continue
+ }
+ relative_price, available := data.Planets[planet].RelativePrices[commodity]
+ if !available {
+ // TODO: Dump cargo
+ continue
+ }
+ base_price := data.Commodities[commodity].BasePrice
+ absolute_price := float64(base_price) * float64(relative_price) / 100.0
+ sell_price := int(absolute_price * 0.9)
+
+ for other[UnusedCargo] = 0; other[UnusedCargo] < dims[UnusedCargo]; other[UnusedCargo]++ {
+ quantity := *hold - (other[UnusedCargo] + other[Cloaks] + other[Edens])
+ sale_value := quantity * sell_price
+ Consider(data, dims, table, other, sale_value, &best_value, best_source)
+ }
+ }
+ other[UnusedCargo] = addr[UnusedCargo]
+ other[Hold] = addr[Hold]
+ }
+
+ /* Buy */
+ other[Traded] = addr[Traded] /* Buy after selling */
+ if addr[Hold] != 0 {
+ commodity := data.i2c[addr[Hold]]
+ if data.Commodities[commodity].CanSell {
+ relative_price, available := data.Planets[planet].RelativePrices[commodity]
+ if available {
+ base_price := data.Commodities[commodity].BasePrice
+ absolute_price := int(float64(base_price) * float64(relative_price) / 100.0)
+ quantity := *hold - (addr[UnusedCargo] + addr[Cloaks] + addr[Edens])
+ total_price := quantity * absolute_price
+ other[Hold] = 0
+ other[UnusedCargo] = 0
+ Consider(data, dims, table, other, -total_price, &best_value, best_source)
+ other[UnusedCargo] = addr[UnusedCargo]
+ other[Hold] = addr[Hold]
+ }
+ }
+ }
+ }
+ other[Traded] = addr[Traded]