small performance improvement for flatten village area

This commit is contained in:
Sokomine 2017-06-27 21:11:09 +02:00
parent 512c03bf8b
commit f44e97072b

View File

@ -189,21 +189,22 @@ mg_villages.lower_or_raise_terrain_at_point = function( x, z, target_height, min
-- search for a surface and set everything above target_height to air
while( y > minp.y) do
local ci = data[a:index(x, y, z)];
local ci_below = data[a:index( x, y-1, z)];
if( look_for_snow and (ci == cid.c_snow or ci == cid.c_ice or ci == cid.c_snowblock)) then
has_snow = true;
elseif( ci == cid.c_tree ) then
tree = true;
-- no jungletrees for branches
elseif( ci == cid.c_jtree and data[a:index( x, y-1, z)]==cid.c_jtree) then
elseif( ci == cid.c_jtree and ci_below==cid.c_jtree) then
jtree = true;
-- pinetrees
elseif( ci == cid.c_ptree and data[a:index( x, y-1, z)]==cid.c_ptree) then
elseif( ci == cid.c_ptree and ci_below==cid.c_ptree) then
ptree = true;
-- acacia
elseif( ci == cid.c_atree and data[a:index( x, y-1, z)]==cid.c_atree) then
elseif( ci == cid.c_atree and ci_below==cid.c_atree) then
atree = true;
-- aspen
elseif( ci == cid.c_asptree and data[a:index( x, y-1, z)]==cid.c_asptree) then
elseif( ci == cid.c_asptree and ci_below==cid.c_asptree) then
asptree = true;
elseif( not( surface_node) and ci ~= cid.c_air and ci ~= cid.c_ignore and mg_villages.check_if_ground( ci ) == true) then
-- we have found a surface of some kind
@ -271,13 +272,14 @@ mg_villages.lower_or_raise_terrain_at_point = function( x, z, target_height, min
target_height = old_height;
end
for y = math.max( minp.y, yblend), maxp.y do
local a_index = a:index( x, y, z );
if( y<=MG_VILLAGES_WATER_LEVEL ) then
-- keep ice
if( data[a:index( x, y, z )] ~= cid.c_ice ) then
data[a:index( x, y, z)] = cid.c_water;
if( data[a_index] ~= cid.c_ice ) then
data[a_index] = cid.c_water;
end
else
data[a:index( x, y, z)] = cid.c_air;
data[a_index] = cid.c_air;
end
end
end
@ -334,47 +336,70 @@ end
-- adjust the terrain level to the respective height of the village
mg_villages.flatten_village_area = function( villages, minp, maxp, vm, data, param2_data, a, village_area, cid )
-- prepare information about all villages that might occour here
local village_tmp = {};
for village_nr, village in ipairs(villages) do
village_tmp[ village_nr ] = {};
local force_ground = nil;
local force_underground = nil;
local has_artificial_snow = false;
if( village.village_type
and mg_villages.village_type_data[ village.village_type ]
and mg_villages.village_type_data[ village.village_type ].force_ground
and mg_villages.village_type_data[ village.village_type ].force_underground ) then
force_ground = minetest.get_content_id(mg_villages.village_type_data[ village.village_type ].force_ground);
force_underground = minetest.get_content_id(mg_villages.village_type_data[ village.village_type ].force_underground);
if( not( force_ground ) or force_ground < 0 or force_ground == cid.c_ignore
or not( force_underground ) or force_underground < 0 or force_underground == cid.c_ignore ) then
force_ground = nil;
force_underground = nil;
end
end
if( village.artificial_snow and village.artificial_snow==1) then
has_artificial_snow = true;
end
village_tmp[ village_nr ].force_ground = force_ground;
village_tmp[ village_nr ].force_underground = force_underground;
village_tmp[ village_nr ].has_artificial_snow = has_artificial_snow;
village_tmp[ village_nr ].vh = village.vh; -- height of village
end
local treepos = {};
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
for village_nr, village in ipairs(villages) do
local force_ground = nil;
local force_underground = nil;
local has_artificial_snow = false;
if( village.village_type
and mg_villages.village_type_data[ village.village_type ]
and mg_villages.village_type_data[ village.village_type ].force_ground
and mg_villages.village_type_data[ village.village_type ].force_underground ) then
force_ground = minetest.get_content_id(mg_villages.village_type_data[ village.village_type ].force_ground);
force_underground = minetest.get_content_id(mg_villages.village_type_data[ village.village_type ].force_underground);
if( not( force_ground ) or force_ground < 0 or force_ground == cid.c_ignore
or not( force_underground ) or force_underground < 0 or force_underground == cid.c_ignore ) then
force_ground = nil;
force_underground = nil;
local village_nr = village_area[ x ][ z ][ 1 ];
local terrain_blending_value = village_area[ x ][ z ][ 2 ];
-- is there a village at this spot?
if( village_nr > 0
and terrain_blending_value ~= 0
-- some data is stored in a temp table
and village_tmp[ village_nr]
and data[a:index(x, village_tmp[ village_nr ].vh ,z)] ~= cid.c_ignore) then
if( terrain_blending_value > 0 ) then -- inside a village
mg_villages.lower_or_raise_terrain_at_point( x, z,
village_tmp[ village_nr ].vh,
minp, maxp, vm, data, param2_data, a, cid,
village_tmp[ village_nr ].vh,
nil,
village_tmp[ village_nr ].has_artificial_snow,
0,
village_tmp[ village_nr ].force_ground,
village_tmp[ village_nr ].force_underground );
elseif( mg_villages.ENABLE_TERRAIN_BLEND and terrain_blending_value < 0) then
mg_villages.lower_or_raise_terrain_at_point( x, z,
maxp.y,
minp, maxp, vm, data, param2_data, a, cid,
village_tmp[ village_nr ].vh,
treepos,
village_tmp[ village_nr ].has_artificial_snow,
-1* terrain_blending_value,
village_tmp[ village_nr ].force_ground,
village_tmp[ village_nr ].force_underground);
end
end
-- is village_nr the village that is the one that is relevant for this spot?
if( village_area[ x ][ z ][ 1 ] > 0
and village_area[ x ][ z ][ 1 ]==village_nr
and village_area[ x ][ z ][ 2 ]~= 0
and data[a:index(x,village.vh,z)] ~= cid.c_ignore) then
if( village.artificial_snow and village.artificial_snow==1) then
has_artificial_snow = true;
end
if( village_area[ x ][ z ][ 2 ] > 0 ) then -- inside a village
mg_villages.lower_or_raise_terrain_at_point( x, z, village.vh, minp, maxp, vm, data, param2_data, a, cid, village.vh,
nil, has_artificial_snow, 0, force_ground, force_underground );
elseif( mg_villages.ENABLE_TERRAIN_BLEND and village_area[ x ][ z ][ 2 ] < 0) then
mg_villages.lower_or_raise_terrain_at_point( x, z, maxp.y, minp, maxp, vm, data, param2_data, a, cid, village.vh,
treepos, has_artificial_snow, -1* village_area[ x ][ z ][ 2 ],
force_ground, force_underground);
end
end -- PM ^
end
end
end
-- grow normal trees and jungletrees in those parts of the terrain where height blending occours
for _, tree in ipairs(treepos) do