forked from ReikaKalseki/DragonIndustries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biters.lua
57 lines (55 loc) · 1.51 KB
/
biters.lua
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function getPossibleBiters(curve, evo)
local ret = {}
local totalWeight = 0
for _,entry in pairs(curve) do
local biter = entry.unit
local vals = entry.spawn_points -- eg "{0.5, 0.0}, {1.0, 0.4}"
for idx = 1,#vals do
local point = vals[idx]
local ref = point.evolution_factor
local chance = point.weight
if evo >= ref then
local interp = 0
if idx == #vals then
interp = chance
else
interp = chance+(vals[idx+1].weight-chance)*(vals[idx+1].evolution_factor-ref)
end
if interp > 0 then
table.insert(ret, {biter, interp+totalWeight})
totalWeight = totalWeight+interp
--game.print("Adding " .. biter .. " with weight " .. interp)
end
break
end
end
end
--game.print("Fake Evo " .. evo)
--for i=1,#ret do game.print(ret[i][1] .. ": " .. ((i == 1 and 0 or ret[i-1][2]) .. " -> " .. ret[i][2])) end
return ret, totalWeight
end
function selectWeightedBiter(biters, total)
local f = math.random()*total
local ret = "nil"
local smallest = 99999999
for i = 1,#biters do
if f <= biters[i][2] and smallest > biters[i][2] then
smallest = biters[i][2]
ret = biters[i][1]
end
end
--game.print("Selected " .. ret .. " with " .. f .. " / " .. total)
return ret
end
function getSpawnedBiter(curve, evo)
--game.print("Real Evo " .. evo)
if math.random() < 0.5 then
evo = evo-0.1
end
if math.random() < 0.25 then
evo = evo-0.1
end
evo = math.max(evo, 0)
local biters, total = getPossibleBiters(curve, evo)
return selectWeightedBiter(biters, total)
end