Skip to content

Commit

Permalink
progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
9551-Dev committed Feb 22, 2023
1 parent 0ac4e7e commit 076bea9
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 84 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Lua.diagnostics.globals": [
"this"
]
}
267 changes: 202 additions & 65 deletions acidity.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
local acidity = {}
local map_methods = {}
local raw_map_methods = {}

local function normalize(a)
local lenght = math.sqrt(a.x^2 + a.y^2)
-- objects
local I_map_interfacable = {}
acidity.noise_map_simple = {}
acidity.noise_map_complex = {}

a.x = a.x / lenght
a.y = a.y / lenght
local RANDOM,RANDOMSEED,LSHIFT,RSHIFT,CEIL = math.random,math.randomseed,bit32 and bit32.lshift or bit.lshift,bit32 and bit32.rshift or bit.rshift,math.ceil

return a
end

local edge_1 = {x=0,y=0}
local edge_2 = {x=64,y=0}
local edge_3 = {x=0,y=64}
local edge_4 = {x=64,y=64}
local default_edge_1 = {x=0,y=0}
local default_edge_2 = {x=1,y=0}
local default_edge_3 = {x=0,y=1}
local default_edge_4 = {x=1,y=1}

local function sub(a,b)
return {
x = a.x - b.x,
y = a.y - b.y
}
local function default_easing_curve(t)
return 6*t^5-15*t^4+10*t^3
end

local function dot(a,b)
return a.x * b.x + a.y * b.y
local function default_output_processor(n)
return (n+1)/2
end

local function fade_easing_curve(t)
return 6*t^5-15*t^4+10*t^3
local function offset_direction(x,y,b,chunk_size)
return (x - b.x*chunk_size)/chunk_size,
(y - b.y*chunk_size)/chunk_size
end

local vector_directions = {0,45,90,135,180,225,270}
local vector_directions_cnt = #vector_directions
local generated_vectors = {}
for i=1,vector_directions_cnt do
local angle = vector_directions[i]
generated_vectors[i] = normalize{
x=math.cos(angle),
y=math.sin(angle)
}
local function dot(a,x,y)
return a.x * x + a.y * y
end

local function createNDarray(n, tbl)
Expand All @@ -55,40 +44,44 @@ end
local function make_chunk_seed(map_seed,x,y)
local seed = x

seed = seed + bit.lshift(y,16)
seed = seed + bit.rshift(y,16)
seed = seed + LSHIFT(y,16)
seed = seed + RSHIFT(y,16)

return seed^map_seed
end

local function init_map_vector(map,x,y)
local function generate_map_vector(map,x,y)

local map_vectors = map.vector

local seed = make_chunk_seed(map.seed,x,y)

math.randomseed(seed)
local direction_vector = generated_vectors[
math.random(1,vector_directions_cnt)
RANDOMSEED(seed)

local direction_vector = map_vectors.directions[
RANDOM(1,map_vectors.ndirections)
]

return direction_vector
end

local function init_map_chunk(map,x,y)
local chunk_size = map.chunk_size
local chunk_x = math.ceil(x/chunk_size)
local chunk_y = math.ceil(y/chunk_size)
local chunk_x = CEIL(x/chunk_size)
local chunk_y = CEIL(y/chunk_size)

local vector_grid = map.vector_grid

vector_grid[chunk_x] [chunk_y] = init_map_vector(map,chunk_x, chunk_y)
vector_grid[chunk_x+1][chunk_y] = init_map_vector(map,chunk_x+1,chunk_y)
vector_grid[chunk_x] [chunk_y+1] = init_map_vector(map,chunk_x, chunk_y+1)
vector_grid[chunk_x+1][chunk_y+1] = init_map_vector(map,chunk_x+1,chunk_y+1)
vector_grid[chunk_x] [chunk_y] = generate_map_vector(map,chunk_x, chunk_y)
vector_grid[chunk_x+1][chunk_y] = generate_map_vector(map,chunk_x+1,chunk_y)
vector_grid[chunk_x] [chunk_y+1] = generate_map_vector(map,chunk_x, chunk_y+1)
vector_grid[chunk_x+1][chunk_y+1] = generate_map_vector(map,chunk_x+1,chunk_y+1)
end

local function get_chunk_vectors(map,x,y)
local chunk_size = map.chunk_size
local chunk_x = math.ceil(x/chunk_size)
local chunk_y = math.ceil(y/chunk_size)
local chunk_x = CEIL(x/chunk_size)
local chunk_y = CEIL(y/chunk_size)

local vector_grid = map.vector_grid

Expand All @@ -105,37 +98,181 @@ local function bilinear_lerp(a,b,c,d,t1,t2)
return (1-t2)*ab + t2*cd
end

function map_methods:get_block(x,y)
function raw_map_methods:get_point(x,y)
init_map_chunk(self,x,y)

local a,b,c,d = get_chunk_vectors(self,x,y)

local chunk_point = {
x = (x-1)%self.chunk_size+1,
y = (y-1)%self.chunk_size+1
}
local chunk_size = self.chunk_size

local chunk_relative_x = (x-1)%chunk_size+1
local chunk_relative_y = (y-1)%chunk_size+1

local easing_curve = self.fading_function
local edges = self.edges

local t1 = fade_easing_curve(chunk_point.x/self.chunk_size)
local t2 = fade_easing_curve(chunk_point.y/self.chunk_size)
local t1 = easing_curve(chunk_relative_x/chunk_size)
local t2 = easing_curve(chunk_relative_y/chunk_size)

local a_dir = sub(chunk_point,edge_1)
local b_dir = sub(chunk_point,edge_2)
local c_dir = sub(chunk_point,edge_3)
local d_dir = sub(chunk_point,edge_4)
local direction_a_x,direction_a_y = offset_direction(chunk_relative_x,chunk_relative_y,edges[1],chunk_size)
local direction_b_x,direction_b_y = offset_direction(chunk_relative_x,chunk_relative_y,edges[2],chunk_size)
local direction_c_x,direction_c_y = offset_direction(chunk_relative_x,chunk_relative_y,edges[3],chunk_size)
local direction_d_x,direction_d_y = offset_direction(chunk_relative_x,chunk_relative_y,edges[4],chunk_size)

local dot1 = dot(a,a_dir)
local dot2 = dot(b,b_dir)
local dot3 = dot(c,c_dir)
local dot4 = dot(d,d_dir)
local dot1 = dot(a,direction_a_x,direction_a_y)
local dot2 = dot(b,direction_b_x,direction_b_y)
local dot3 = dot(c,direction_c_x,direction_c_y)
local dot4 = dot(d,direction_d_x,direction_d_y)

return bilinear_lerp(dot1,dot2,dot3,dot4,t1,t2)/16

return self.output_processor(bilinear_lerp(dot1,dot2,dot3,dot4,t1,t2))
end

function acidity.new_map(seed,chunk_size)
--[[
local generated_vectors = {}
local vector_directions_cnt = 18
local n = 0
for dir=0,math.pi*2,(math.pi*2)/vector_directions_cnt do
n = n + 1
generated_vectors[n] = {
x=math.cos(dir),
y=math.sin(dir)
}
end
vector={
ndirections = vector_directions_cnt,
directions = generated_vectors
},
]]

function acidity.create_map_raw(seed,chunk_size,vector_grid,edges,direction_types,fading_function,output_processor)
return setmetatable({
chunk_size=chunk_size,
seed=seed,
vector_grid=createNDarray(1)
},{__index=map_methods})
chunk_size = chunk_size,
seed = seed,
vector_grid = vector_grid,
edges = edges,
vector = direction_types,
fading_function = fading_function,
output_processor = output_processor

},{__index=raw_map_methods})
end

-- constructors
setmetatable(I_map_interfacable,{__call=function(methods,interface_parent)
setmetatable(getmetatable(interface_parent).__index,{__index=methods})
return interface_parent
end,__tostring=function() return "CLASS-IMapInterfacable" end})

setmetatable(acidity.noise_map_simple,{__call=function(methods,seed,frequency,generate_vector_directions,custom_edges,fade,output)
local generated_vectors = {}

local directions = generate_vector_directions or 8

local n = 0
for dir=0,math.pi*2,(math.pi*2)/directions do
n = n + 1
generated_vectors[n] = {
x=math.cos(dir),
y=math.sin(dir)
}
end

local self = {raw=acidity.create_map_raw(
seed,frequency,createNDarray(1),
custom_edges or {
{x=default_edge_1.x,y=default_edge_1.y},
{x=default_edge_2.x,y=default_edge_2.y},
{x=default_edge_3.x,y=default_edge_3.y},
{x=default_edge_4.x,y=default_edge_4.y}
},
{
ndirections = directions,
directions = generated_vectors
},fade or default_easing_curve,output or default_output_processor
)}

return I_map_interfacable(setmetatable(self,{__index=methods,__tostring=function() return "Object-noise_map_simple" end}))
end,__tostring=function() return "CLASS-noise_map_simple" end})

setmetatable(acidity.noise_map_complex,{__call=function(methods,seed,frequency,octaves,lacunarity,persistance,generate_vector_directions,custom_edges,fade,output)
local self = {
octaves = octaves,
lacunarity = lacunarity,
persistance = persistance
}

local generated_vectors = {}

local directions = generate_vector_directions or 8

local n = 0
for dir=0,math.pi*2,(math.pi*2)/directions do
n = n + 1
generated_vectors[n] = {
x=math.cos(dir),
y=math.sin(dir)
}
end

local edges = custom_edges or {
{x=default_edge_1.x,y=default_edge_1.y},
{x=default_edge_2.x,y=default_edge_2.y},
{x=default_edge_3.x,y=default_edge_3.y},
{x=default_edge_4.x,y=default_edge_4.y}
}

local vector_directions = {
ndirections = directions,
directions = generated_vectors
}

local fade_processor = fade or default_easing_curve
local output_processor = output or default_output_processor

for i=1,octaves do
local octave_id = i-1
local octave_amplitude = persistance ^ octave_id
local octave_frequency = CEIL(frequency/(lacunarity ^ octave_id))

self[i] = {
raw = acidity.create_map_raw(
seed,octave_frequency,createNDarray(1),edges,vector_directions,fade_processor,output_processor
),
frequency = octave_frequency,
amplitude = octave_amplitude
}
end


return setmetatable(self,{__index=methods,__tostring=function() return "Object-noise_map_complex" end})
end,__tostring=function() return "CLASS-noise_map_complex" end})

function acidity.noise_map_complex:get_point(x,y)
local total = 0

local octaves = self.octaves

local total_amplitude = 0

for i=1,octaves do
local octave = self[i]

local amplitude = octave.amplitude

total = total + (octave.raw:get_point(x,y) * amplitude)

total_amplitude = total_amplitude + amplitude
end

return total/total_amplitude
end

function acidity.noise_map_simple:get_point(x,y)
return self.raw:get_point(x,y)
end

return acidity
38 changes: 19 additions & 19 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
local acidity = require("acidity")


local map = acidity.new_map(10,64)

local orig_line = love.graphics.line
function love.graphics.line(x,y,x_,y_)
orig_line(x+20,y+20,x_+20,y_+20)
end
local orig_points = love.graphics.points
function love.graphics.points(x,y)
orig_points(x+20,y+20)
end
local map = acidity.noise_map_complex(11,50,3,2,0.5)

function love.load()
love.graphics.setLineWidth(2)
end

function love.draw()
for x=1,450 do
for y=1,150 do
local c = map:get_block(x,y)
local w,h = love.graphics.getDimensions()

local min = math.huge
local max = -math.huge

for x=1,w do
for y=1,h do
local c = map:get_point(x,y)
max = math.max(max,c)
min = math.min(min,c)
love.graphics.setColor(c,c,c)
love.graphics.points(x,y)
end
end

love.graphics.setColor(1,0,0)
--error(("%f:%f"):format(max,min))

--[[love.graphics.setColor(1,0,0)
for x,ls in pairs(map.vector_grid) do
x = x - 1
for y,vec in pairs(ls) do
y = y - 1
local nx = x * 64
local ny = y * 64
local nx = x * 32
local ny = y * 32
local enx = x*64 + vec.x*15
local eny = y*64 + vec.y*15
local enx = x*32 + vec.x*15
local eny = y*32 + vec.y*15
love.graphics.line(nx,ny,enx,eny)
end
end
end]]
end

0 comments on commit 076bea9

Please sign in to comment.