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