]> git.lizzy.rs Git - Crafter.git/blob - mods/aether/biomes.lua
switch os.clock to minetest.get_us_time
[Crafter.git] / mods / aether / biomes.lua
1 local minetest,math = minetest,math
2
3
4 minetest.register_biome({
5         name = "aether",
6         node_top = "air",
7         depth_top = 1,
8         node_filler = "air",
9         depth_filler = 3,
10         node_riverbed = "air",
11         depth_riverbed= 0,
12         node_stone = "air",
13         node_water = "air",
14         node_dungeon = "air",
15         node_dungeon_alt = "air",
16         node_dungeon_stair = "air",
17         node_cave_liquid = "air",
18         vertical_blend = 0,
19         y_max = 31000,
20         y_min = 21000,
21         heat_point = -100,
22         humidity_point = -100,
23 })
24
25 --this is from https://github.com/paramat/lvm_example/blob/master/init.lua
26 --hi paramat :D
27
28 -- Set the 3D noise parameters for the terrain.
29 local np_terrain = {
30         offset = 0,
31         scale = 1,
32         spread = {x = 200, y = 100, z = 200},
33         seed = tonumber(minetest.get_mapgen_setting("seed")) or math.random(0,999999999),
34         octaves = 5,
35         persist = 0.63,
36         lacunarity = 2.0,
37         --flags = ""
38 }
39
40
41 -- Set singlenode mapgen (air nodes only).
42 -- Disable the engine lighting calculation since that will be done for a
43 -- mapchunk of air nodes and will be incorrect after we place nodes.
44 --minetest.set_mapgen_params({mgname = "singlenode", flags = "nolight"})
45 -- Get the content IDs for the nodes used.
46
47 local c_dirt = minetest.get_content_id("aether:dirt")
48 local c_stone = minetest.get_content_id("aether:stone")
49 local c_air = minetest.get_content_id("air")
50 local c_grass = minetest.get_content_id("aether:grass")
51 -- Initialize noise object to nil. It will be created once only during the
52 -- generation of the first mapchunk, to minimise memory use.
53 local nobj_terrain = nil
54 -- Localise noise buffer table outside the loop, to be re-used for all
55 -- mapchunks, therefore minimising memory use.
56 local nvals_terrain = {}
57 -- Localise data buffer table outside the loop, to be re-used for all
58 -- mapchunks, therefore minimising memory use.
59
60 local data = {}
61 local n_pos = {}
62 local node2 = ""
63 local vi = {}
64 local content_id = minetest.get_name_from_content_id
65 local sidelen = {}
66 local permapdims3d = {}
67 local nobj_terrain = {}
68 local vm, emin, emax = {},{},{}
69 local area = {}
70 local ni = 1
71 local density_noise = {}
72 -- On generated function.
73
74 -- 'minp' and 'maxp' are the minimum and maximum positions of the mapchunk that
75 -- define the 3D volume.
76 minetest.register_on_generated(function(minp, maxp, seed)
77         --nether starts at -10033 y
78         if minp.y < 21000 then
79                 return
80         end
81         -- Start time of mapchunk generation.
82         --local t0 = minetest.get_us_time()/1000000
83         
84         -- Noise stuff.
85
86         -- Side length of mapchunk.
87         sidelen = maxp.x - minp.x + 1
88         -- Required dimensions of the 3D noise perlin map.
89         permapdims3d = {x = sidelen, y = sidelen, z = sidelen}
90         -- Create the perlin map noise object once only, during the generation of
91         -- the first mapchunk when 'nobj_terrain' is 'nil'.
92         nobj_terrain = minetest.get_perlin_map(np_terrain, permapdims3d) --nobj_terrain or 
93         -- Create a flat array of noise values from the perlin map, with the
94         -- minimum point being 'minp'.
95         -- Set the buffer parameter to use and reuse 'nvals_terrain' for this.
96         nobj_terrain:get_3d_map_flat(minp, nvals_terrain)
97         ni = 1
98         -- Voxelmanip stuff.
99
100         -- Load the voxelmanip with the result of engine mapgen. Since 'singlenode'
101         -- mapgen is used this will be a mapchunk of air nodes.
102         vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
103         -- 'area' is used later to get the voxelmanip indexes for positions.
104         area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
105         -- Get the content ID data from the voxelmanip in the form of a flat array.
106         -- Set the buffer parameter to use and reuse 'data' for this.
107         vm:get_data(data)
108
109         -- Generation loop.
110
111         -- Noise index for the flat array of noise values.
112         -- Process the content IDs in 'data'.
113         -- The most useful order is a ZYX loop because:
114         -- 1. This matches the order of the 3D noise flat array.
115         -- 2. This allows a simple +1 incrementing of the voxelmanip index along x
116         -- rows.
117         -- rows.
118         for z = minp.z, maxp.z do
119         for y = minp.y, maxp.y do
120                 -- Voxelmanip index for the flat array of content IDs.
121                 -- Initialise to first node in this x row.
122                 vi = area:index(minp.x, y, z)
123                 for x = minp.x, maxp.x do
124                         -- Consider a 'solidness' value for each node,
125                         -- let's call it 'density', where
126                         -- density = density noise + density gradient.
127                         density_noise = nvals_terrain[ni]
128                         -- Density gradient is a value that is 0 at water level (y = 1)
129                         -- and falls in value with increasing y. This is necessary to
130                         -- create a 'world surface' with only solid nodes deep underground
131                         -- and only air high above water level.
132                         -- Here '128' determines the typical maximum height of the terrain.
133                         
134                         
135                         --print(density_noise, density_gradient)
136                         -- Place solid nodes when 'density' > 0.
137                         --if density_noise + density_gradient > 0 then
138                         
139                         --print(density_noise + density_gradient)
140                         if density_noise > 0.1 then
141                                 data[vi] = c_dirt
142                         else
143                                 --force create grass
144                                 n_pos = area:index(x,y-1,z)
145                                 node2 = content_id(data[n_pos])
146                                 if node2 == "aether:dirt" then
147                                         data[n_pos] = c_grass
148                                 end
149                         end
150                         -- Otherwise if at or below water level place water.
151                         --elseif y == -10033 then
152                                 --data[vi] = c_bedrock
153                         --elseif y <= 1 then
154                         --      data[vi] = c_water
155                         --elseif y > -15000 then
156                         --      data[vi] = c_air
157                         --else
158                                 --data[vi] = c_lava
159                         --end
160                         
161                         -- Increment noise index.
162                         ni = ni + 1
163                         -- Increment voxelmanip index along x row.
164                         -- The voxelmanip index increases by 1 when
165                         -- moving by 1 node in the +x direction.
166                         vi = vi + 1
167                 end
168         end
169         end
170
171         -- After processing, write content ID data back to the voxelmanip.
172         
173         vm:set_data(data)
174         -- Calculate lighting for what has been created.
175         --vm:calc_lighting()
176         vm:set_lighting({day=15,night=0}, minp, maxp)
177         --minetest.generate_ores(vm)
178         
179         --minetest.generate_decorations(vm)
180         -- Write what has been created to the world.
181         vm:write_to_map()
182                 
183                 
184         -- Liquid nodes were placed so set them flowing.
185         --vm:update_liquids()
186
187         -- Print generation time of this mapchunk.
188         --local chugent = math.ceil((minetest.get_us_time()/1000000- t0) * 1000)
189         --print ("[lvm_example] Mapchunk generation time " .. chugent .. " ms")
190 end)