summaryrefslogtreecommitdiff
path: root/mods/ma_pan/init.lua
blob: de0d586ab1396c74a6ce4510bd6e1028ab81249c (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local chunkgen = {}
chunkgen.__index = chunkgen

function chunkgen.new(minp, maxp, blockseed)
    local new = {}
    setmetatable(new, chunkgen)
    new.minp, new.maxp, new.blockseed = minp, maxp, blockseed
    return new
end

local perlin_noise_params = {
    offset = 0,
    scale = 1,
    spread = {x=10, y=10, z=10},
    seed = 0xBEE,
    octaves = 2,
    persistence = 0.5,
    lacunarity = 2,
    flags = {"noeased", "absvalue"}
}

local perlin_noise_params_2 = {
    offset = 0,
    scale = 1,
    spread = {x=32, y=32, z=32},
    seed = 0xBEE5,
    octaves = 2,
    persistence = 0.5,
    lacunarity = 2,
    flags = {"noeased", "absvalue"}
}

local ingredients = {"alasa_pan:telo", "alasa_pan:ko_pan", "alasa_pan:ko_suli"}
local blocks = {"alasa_pan:leko_loje", "alasa_pan:leko_loje2", "alasa_pan:leko_jelo", "alasa_pan:leko_laso", "alasa_pan:leko_laso2", "alasa_pan:leko_laso3", "alasa_pan:leko_unu", "alasa_pan:leko_pimeja"}
local min_height = -1.3
local max_height = 1.3
local function get_color(height)
    height = (height - min_height) / (max_height - min_height)
    height = height * #blocks
    height = math.floor(height)+1
    if height < 1 then height = 1 end
    if height > #blocks then height = #blocks end
    return blocks[height]
end

function chunkgen:block_at_pos(x, y, z)
    if y < 0 then
        return "alasa_pan:anpa"
    end
    if y > 2 then return "air" end
    local height = self.heightmap and self.heightmap[x-self.minp.x+1][z-self.minp.z+1] or 0
    local height2 = self.heightmap_2 and self.heightmap_2[x-self.minp.x+1][z-self.minp.z+1] or 0
    if height > 0 and x*x + z*z > 70 then return get_color(height2) end

    if y == 0 and math.random(1, 100) == 1 then
        return ingredients[math.random(1, 3)]
    end

    return "air"
end

local oven_structure = {"alasa_pan:ilo_seli", nil, nil, nil, "alasa_pan:ilo_pona"}
local oven_w = 5
local oven_h = 1
function chunkgen:oven_override(block, x, y, z, ox, oz)
    if y < 0 or y > 1 then return block end
    local in_oven = x >= ox and x <= ox + oven_w - 1 and z >= oz and z <= oz + oven_h - 1
    if not in_oven then return block end
    local oven_block = oven_structure[x-ox + (z-oz)*oven_w + 1]
    if not oven_block then return block end
    if y == 0 then return "alasa_pan:leko" end
    return oven_block
end


function chunkgen:generate()
    local minp, maxp = self.minp, self.maxp
    if minp.y > 1 then return end
    math.randomseed(self.blockseed, 34802398420983428)
    
    local has_oven = math.random(1, 25) == 1 -- ni li suli tan ni: leko li lon la jan li ken ala lukin e ilo seli.
    local oven_x = math.random(minp.x, maxp.x - oven_w + 1)
    local oven_z = math.random(minp.z, maxp.z - oven_h + 1)
    if minp.x == -32 and minp.z == -32 then
        has_oven = true
        oven_x = -2
        oven_z = 0
    end

    local nontrivial_chunk = (minp.y <= 0 and maxp.y >= 2)
    if nontrivial_chunk then
        local noise_map = minetest.get_perlin_map(perlin_noise_params, {x=80, y=80})
        local noise_map_2 = minetest.get_perlin_map(perlin_noise_params_2, {x=80, y=80})
        self.heightmap = noise_map:get_2d_map({x=minp.x, y=minp.z})
        self.heightmap_2 = noise_map_2:get_2d_map({x=minp.x, y=minp.z})
    end

    local voxel_manip = minetest.get_voxel_manip(minp, maxp)
    local data = {}
    for x=minp.x, maxp.x do
        for y=minp.y, maxp.y do
            for z=minp.z, maxp.z do
                local block = self:block_at_pos(x, y, z)
                if has_oven then block = self:oven_override(block, x, y, z, oven_x, oven_z) end
                table.insert(data, minetest.get_content_id(block))
            end
        end
    end
    voxel_manip:set_data(data)
    voxel_manip:write_to_map(true)
end

minetest.register_on_generated(function(minp, maxp, blockseed)
    local cg = chunkgen.new(minp, maxp, blockseed)
    cg:generate()
end)