1
0
mirror of https://github.com/osmarks/random-stuff synced 2025-09-07 04:47:56 +00:00

declassify some projects

This commit is contained in:
2023-06-19 14:09:54 +01:00
parent 307c513a9b
commit 30a1e0e358
40 changed files with 2828 additions and 8 deletions

93
heavbiome/main.lua Normal file
View File

@@ -0,0 +1,93 @@
local map = {}
local w = 300
local h = 300
local seed = 349259
require("perlin") -- not mine.
-- it uses globals. spiteful, but i do not care enough about code quality for this.
local biomecols = {
empty = {0.5, 0.5, 0.5},
forest = { 0, 0.5, 0},
rainforest = { 0, 0.5, 0.5},
ocean = { 0, 0, 1},
plains = { 0, 1, 0},
desert = { 1, 1, 0},
}
local PERSISTENCE = 0.3
local ITERS = 4
local function oct_noise(x, y, seed, gen)
local total = 0
local frequency = 1
local amplitude = 1
local norm = 0
for i = 1, ITERS do
total = total + gen:noise(x * frequency, y * frequency, seed + i) * amplitude
norm = norm + amplitude
frequency = frequency * 2
amplitude = amplitude * PERSISTENCE
end
return total
end
local sf = 1/20 -- scaling factor.
local function pick_biome(x, y, gen)
local islandicity = gen:noise(x*sf/10, y*sf/10, 302382359)/2+1/2
islandicity = math.min(math.max(0,islandicity*3),1)
local mainland_modifier = gen:noise(x*sf/20, y*sf/20, 302382359)/2
local is_ocean = gen:noise(x*sf/4, y*sf/4, 555575)/2+1/2 + gen:noise(x*sf, y*sf, 555575)/(25-islandicity*10) + mainland_modifier
local humidity = oct_noise(x*sf/2, y*sf/2, 10000, gen)/2+1/2
local temp = oct_noise(x*sf/6, y*sf/6, 20000, gen)/2+1/2
--return { is_ocean, humidity, temp }
--
if is_ocean > 0.45 then return "ocean" end
if is_ocean > 0.43 and humidity < 0.6 then return "desert" end -- coast?
if temp > 0.75 then return "desert" end
if humidity < 0.5 then
if temp > 0.5 then
return "desert"
else
return "plains"
end
else
if humidity > 0.75 then
return "rainforest"
else
if temp < 0.6 then
return "forest"
else
return "plains"
end
end
end
return "empty"
--]]
end
function love.load()
local noise = perlin(seed)
local gen = noise
for x=1, w do
map[x] = {}
for y=1, h do
map[x][y] = 0
end
end
for x=1, w do
for y=1, h do
local biome = pick_biome(x, y, noise)
--map[x][y] = biome
map[x][y] = biomecols[biome]
end
end
end
function love.draw()
for x=1, w do
for y=1, h do
love.graphics.setColor(unpack(map[x][y]))
love.graphics.rectangle("fill",x*2, y*2, 2, 2)
end
end
end

134
heavbiome/perlin.lua Normal file
View File

@@ -0,0 +1,134 @@
local defaultSeed = 1337
local dot_product = {
[0x0]=function(x,y,z) return x + y end,
[0x1]=function(x,y,z) return -x + y end,
[0x2]=function(x,y,z) return x - y end,
[0x3]=function(x,y,z) return -x - y end,
[0x4]=function(x,y,z) return x + z end,
[0x5]=function(x,y,z) return -x + z end,
[0x6]=function(x,y,z) return x - z end,
[0x7]=function(x,y,z) return -x - z end,
[0x8]=function(x,y,z) return y + z end,
[0x9]=function(x,y,z) return -y + z end,
[0xA]=function(x,y,z) return y - z end,
[0xB]=function(x,y,z) return -y - z end,
[0xC]=function(x,y,z) return y + x end,
[0xD]=function(x,y,z) return -y + z end,
[0xE]=function(x,y,z) return y - x end,
[0xF]=function(x,y,z) return -y - z end
}
local grad = function(hash, x,y,z)
return dot_product[hash % 0x10](x,y,z)
end
local fade = function(t)
return t * t * t * (t * (t * 6 - 15) + 10)
end
local lerp = function(t,a,b)
return a + t * (b - a)
end
local generatePermutation = function(seed)
math.randomseed(seed)
local permutation = {0}
for i=1,255 do
table.insert(permutation,math.random(1,#permutation+1),i)
end
local p = {}
for i=0,255 do
p[i] = permutation[i+1]
p[i+256] = permutation[i+1]
end
return p
end
perlin = {}
perlin.__index = perlin
perlin.noise = function(self,x,y,z)
y = y or 0
z = z or 0
local xi = math.floor(x) % 0x100
local yi = math.floor(y) % 0x100
local zi = math.floor(z) % 0x100
x = x - math.floor(x)
y = y - math.floor(y)
z = z - math.floor(z)
local u = fade(x)
local v = fade(y)
local w = fade(z)
local A, AA, AB, AAA, ABA, AAB, ABB, B, BA, BB, BAA, BBA, BAB, BBB
A = self.p[xi ] + yi
AA = self.p[A ] + zi
AB = self.p[A+1 ] + zi
AAA = self.p[ AA ]
ABA = self.p[ AB ]
AAB = self.p[ AA+1 ]
ABB = self.p[ AB+1 ]
B = self.p[xi+1] + yi
BA = self.p[B ] + zi
BB = self.p[B+1 ] + zi
BAA = self.p[ BA ]
BBA = self.p[ BB ]
BAB = self.p[ BA+1 ]
BBB = self.p[ BB+1 ]
return lerp(w,
lerp(v,
lerp(u,
grad(AAA,x,y,z),
grad(BAA,x-1,y,z)
),
lerp(u,
grad(ABA,x,y-1,z),
grad(BBA,x-1,y-1,z)
)
),
lerp(v,
lerp(u,
grad(AAB,x,y,z-1), grad(BAB,x-1,y,z-1)
),
lerp(u,
grad(ABB,x,y-1,z-1), grad(BBB,x-1,y-1,z-1)
)
)
)
end
setmetatable(perlin,{
__call = function(self,seed)
seed = seed or defaultSeed
return setmetatable({
seed = seed,
p = generatePermutation(seed),
},self)
end
})
--[[
EXAMPLE
local p1 = perlin(1338)
local p2 = perlin(1337)
local x,y,z = 1.0,2.0,3.0
print(p1:noise(x,y,z))
print(p2:noise(x,y,z))
>> 0.23456
>> 0.47598
--]]