bzr branch
http://9ix.org/bzr/traderous
91
by Josh C
set prices based on distance |
1 |
Good = { |
2 |
goods = {}, |
|
3 |
maxMult = 5, -- max price multiplier if source & dest are at opposite ends of universe |
|
4 |
potentialGoods = { |
|
5 |
{name = 'Bobble-Headed Dolls', base = 20, chance = 0.5}, |
|
6 |
{name = 'Alliance Foodstuffs', base = 50, chance = 0.8}, |
|
7 |
{name = 'Medical Supplies', base = 150, chance = 0.4}, |
|
8 |
{name = 'Building Materials', base = 100, chance = 0.4}, |
|
9 |
{name = 'Scrap', base = 25, chance = 0.4} |
|
10 |
}, |
|
11 |
} |
|
12 |
||
13 |
function Good:stockPlanets() |
|
14 |
for _, planet in ipairs(the.planets.sprites) do |
|
15 |
while #planet.goods == 0 do |
|
16 |
for _, good in ipairs(self.potentialGoods) do |
|
17 |
if math.random() < good.chance then |
|
18 |
if self.goods[good.name] then |
|
19 |
-- calculate price from distance to source |
|
20 |
local source = self.goods[good.name].source |
|
21 |
local base = self.goods[good.name].base |
|
22 |
local gvec = vector.new(planet.x - source.x, |
|
23 |
planet.y - source.y) |
|
24 |
||
25 |
-- multiplier should be betw 1-maxMult and increase |
|
26 |
-- exponentially with distance from source |
|
27 |
local mult = gvec:len2() / the.bg.width^2 * (self.maxMult - 1) + 1 |
|
28 |
||
29 |
table.insert(planet.goods, {good.name, math.floor(base * mult)}) |
|
30 |
else |
|
31 |
-- set this planet as the source |
|
32 |
self.goods[good.name] = { |
|
33 |
source = planet, |
|
34 |
base = good.base |
|
35 |
} |
|
36 |
end |
|
37 |
||
38 |
end |
|
39 |
end -- for good in potentialGoods |
|
40 |
end -- while #planet.goods == 0 |
|
41 |
end -- for planet in the.planets |
|
42 |
end |