/traderous

To get this branch, use:
bzr branch http://9ix.org/bzr/traderous
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Good = {
   goods = {},
   maxMult = 6, -- max price multiplier if source & dest are at opposite ends of universe
   potentialGoods = {
      {name = 'Bobble-Headed Dolls', base = 20,  chance = 0.5},
      {name = 'Food',                base = 50,  chance = 0.8},
      {name = 'Medical Supplies',    base = 150, chance = 0.4},
      {name = 'Building Materials',  base = 100, chance = 0.4},
      {name = 'Scrap',               base = 25,  chance = 0.4}
   },
}

function Good:stockPlanets()
   for _, planet in ipairs(the.planets.sprites) do
      while #planet.goods == 0 do
         for _, good in ipairs(self.potentialGoods) do
            if math.random() < good.chance then
               if self.goods[good.name] then
                  -- calculate price from distance to source
                  local source = self.goods[good.name].source
                  local base = self.goods[good.name].base
                  local gvec = vector.new(planet.x - source.x,
                                          planet.y - source.y)

                  -- multiplier should be betw 1-maxMult and increase
                  -- exponentially with distance from source
                  local mult = gvec:len2() / the.bg.width^2 * (self.maxMult - 1) + 1

                  table.insert(planet.goods, {good.name, math.floor(base * mult)})
               else
                  -- set this planet as the source
                  self.goods[good.name] = {
                     source = planet,
                     base = good.base
                  }
               end

            end
         end -- for good in potentialGoods
      end -- while #planet.goods == 0
   end -- for planet in the.planets
end