Compare commits

...

5 Commits

Author SHA1 Message Date
SmallJoker 432d9ca6a0 Fix item override for 5.3.0-dev 2020-05-07 20:09:46 +02:00
SmallJoker be87054c49 Add number of iterations, proper array indexing 2018-04-27 11:20:23 +02:00
SmallJoker b250c01351 peoplecantplant: Change license to CC0, improve READMEs 2018-04-21 14:58:43 +02:00
SmallJoker dced2d6488 Generalize the auto-planting mechanism 2017-11-05 11:00:24 +01:00
SmallJoker 9556808b82 Stretch node above when terrain is lowered 2016-10-30 12:45:59 +01:00
4 changed files with 90 additions and 96 deletions

View File

@ -7,6 +7,9 @@ Warning: The repository name and all parts of it might contain some sarcasm.
Module descriptions
---------------------
blur:
Flattens the terrain using some kind of blur function.
mapgen:
Place a schematic when a new chunk is generated.

View File

@ -1,6 +1,7 @@
-- peoplecantblur - Generate flat areas
local HEIGHT_CHECK = 20
local HEIGHT_CHECK = 30
local BLUR_ITERATIONS = 8
local c_air = minetest.get_content_id("air")
@ -36,7 +37,7 @@ local function get_ground(data, area, pos, max_height, cache_c, get_contents)
local rel_surface -- Relative height of surface
-- Find the ground height (check downwards)
for y = 0, -max_height, -1 do
for y = 0, -max_height + 4, -1 do
local c_id = data[area:index(pos.x, pos.y + y, pos.z)]
local is_surface_content = check_surface_content(c_id, cache_c)
id_cache[y] = { c_id, is_surface_content }
@ -51,13 +52,15 @@ local function get_ground(data, area, pos, max_height, cache_c, get_contents)
if not rel_surface then
-- Check upper area
for y = 1, max_height do
for y = max_height - 1, 0, -1 do
local c_id = data[area:index(pos.x, pos.y + y, pos.z)]
local is_surface_content = check_surface_content(c_id, cache_c)
id_cache[y] = { c_id, is_surface_content }
if not is_surface_content and c_id ~= c_air then
rel_surface = y - 1
if is_surface_content then
if y ~= max_height - 1 then
rel_surface = y
end
break
end
end
@ -92,9 +95,21 @@ local function get_ground(data, area, pos, max_height, cache_c, get_contents)
end
end
-- Stretch the node above if it's air, a liquid, tree etc.
local c_above
local c_id = id_cache[rel_surface + 1][1]
local name = minetest.get_name_from_content_id(c_id)
local def = minetest.registered_nodes[name]
if def and (def.drawtype == "normal"
or def.drawtype == "airlike"
or def.drawtype == "liquid") then
c_above = c_id
end
return {
rel_surface = rel_surface,
c_contents = c_contents
c_contents = c_contents,
c_above = (c_above or c_air)
}
end
@ -113,6 +128,7 @@ local function flatten(ppos, radius)
local emin, emax = vm:read_from_map(minp, maxp)
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local sidelen = maxp.x - minp.x + 1
-- Lookup table for content ID groups
local cache_c = {}
@ -120,20 +136,20 @@ local function flatten(ppos, radius)
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local ground = get_ground(
data,
area,
vector.new(x, ppos.y, z),
max_height,
cache_c,
math.abs(x - ppos.x) <= radius and math.abs(z - ppos.z) <= radius
data,
area,
vector.new(x, ppos.y, z),
max_height,
cache_c,
math.abs(x - ppos.x) <= radius and math.abs(z - ppos.z) <= radius
)
heightmap[z * 0x10000 + x] = ground
heightmap[(z - minp.z) * sidelen + (x - minp.x)] = ground
end
end
-- Get the relative height from the heightmap with relative coordinates
local get_height = function(map, x, z, fallback)
local info = map[z * 0x10000 + x]
local info = map[(z - minp.z) * sidelen + (x - minp.x)]
if info and info.rel_surface then
return info.rel_surface
end
@ -141,14 +157,15 @@ local function flatten(ppos, radius)
end
local _dirty_ = false
local E = 1 -- effect width
-- Apply blur filter on each position and update the nodes
for z = minp.z + E, maxp.z - E do
for x = minp.x + E, maxp.x - E do
local p_info = heightmap[z * 0x10000 + x]
for n = 0, BLUR_ITERATIONS do
for z = minp.z + 1, maxp.z - 1 do
for x = minp.x + 1, maxp.x - 1 do
local p_info = heightmap[(z - minp.z) * sidelen + (x - minp.x)]
local nodes = p_info.c_contents
local old_h = p_info.rel_surface
local above = p_info.c_above
if nodes and #nodes > 0 and old_h then
--[[
@ -159,15 +176,15 @@ local function flatten(ppos, radius)
+----+----+----+ -> 13
]]
local h = old_h + (
get_height(heightmap, x , z - E, old_h)
+ get_height(heightmap, x - E, z , old_h)
+ get_height(heightmap, x + E, z , old_h)
+ get_height(heightmap, x , z + E, old_h)
get_height(heightmap, x , z - 1, old_h)
+ get_height(heightmap, x - 1, z , old_h)
+ get_height(heightmap, x + 1, z , old_h)
+ get_height(heightmap, x , z + 1, old_h)
) * 2 + (
get_height(heightmap, x - E, z - E, old_h)
+ get_height(heightmap, x + E, z - E, old_h)
+ get_height(heightmap, x - E, z + E, old_h)
+ get_height(heightmap, x + E, z + E, old_h)
get_height(heightmap, x - 1, z - 1, old_h)
+ get_height(heightmap, x + 1, z - 1, old_h)
+ get_height(heightmap, x - 1, z + 1, old_h)
+ get_height(heightmap, x + 1, z + 1, old_h)
)
h = math.floor(h / 13 + 0.5)
@ -179,7 +196,11 @@ local function flatten(ppos, radius)
for y = max_y, min_y, -1 do
local vi = area:index(x, ppos.y + y, z)
if y > h then
data[vi] = c_air
if h > old_h then
data[vi] = c_air
else
data[vi] = above
end
else
data[vi] = nodes[math.min(#nodes, i)]
i = i + 1
@ -190,6 +211,7 @@ local function flatten(ppos, radius)
end
end
end
end
if not _dirty_ then
return
@ -207,8 +229,8 @@ minetest.register_chatcommand("flat", {
func = function(name, param)
local player = minetest.get_player_by_name(name)
local player_pos = vector.round(player:getpos())
-- Flatten an area of (2 * 6 + 1) ^ 2 m
flatten(player_pos, 6)
-- Flatten an area of (2 * 7 + 1) ^ 2 square meters
flatten(player_pos, 7)
return true, "OK."
end
})

View File

@ -1,5 +1,7 @@
People can't plant
====================
This mod automatically plants dropped sapling items.
Depends: none!
@ -7,4 +9,4 @@ Depends: none!
----------
Source code:
LGPL 2.1
CC0

View File

@ -1,13 +1,8 @@
-- peoplecantplant - Plant saplings when touching the ground
-- Contains code fragments from builtin/game/item_entity.lua
-- peoplecantplant - Plant saplings when they touch the ground
-- Function that gets called when the builtin item stops
function builtin_item_stopped(self, pos_below, node)
if minetest.get_item_group(node.name, "soil") == 0 then
return -- Not soil
end
function builtin_item_stopped(self)
local stack = ItemStack(self.itemstring)
if stack:get_count() > 1 then
return -- Obviously dropped by someone (more than 1 item on the stack)
@ -18,82 +13,54 @@ function builtin_item_stopped(self, pos_below, node)
return -- It is not a sapling
end
if minetest.is_protected(pos_below, ":nobody") then
local pos = vector.round(self.object:get_pos())
local pos_below = vector.new(pos)
pos_below.y = pos_below.y - 1
if minetest.is_protected(pos, ":nobody") then
return -- The area is protected by someone
end
local node_below = minetest.get_node(pos_below)
if minetest.get_item_group(node_below.name, "soil") == 0 then
return -- Not soil
end
if minetest.find_node_near(pos_below, 3, {"group:sapling", "group:tree"}) then
return -- There's another tree around, prevent jungle
end
-- Using 'pos = pos_below' would not allow us to modify the tables seperately,
-- thus make a copy of it with vector.new
local pos = vector.new(pos_below)
pos.y = pos.y + 1
if minetest.get_node(pos).name ~= "air" then
local node = minetest.get_node(pos).name
-- Get node definition to decide whether to replace or not
-- fallback to empty table when it's an unknown node
local nodedef = minetest.registered_nodes[node] or {}
if not nodedef.buildable_to then
return
end
minetest.set_node(pos, {name = item})
self.itemstring = ""
self.object:remove()
return true -- Success!
end
-- Lifespan of an entity, default it to 900 on fail
local time_to_live = tonumber(minetest.setting_get("item_entity_ttl")) or 900
-- Overwrite "on_step" in the entity that's used for dropped items
minetest.registered_entities["__builtin:item"].on_step = function(self, dtime)
self.age = self.age + dtime
if time_to_live > 0 and self.age > time_to_live then
-- Item expired, remove it
self.itemstring = ""
self.object:remove()
return
local entity_def = minetest.registered_entities["__builtin:item"]
local old_step = entity_def.on_step
entity_def.on_step = function(self, dtime, ...)
local old_acc = self.object:get_acceleration()
old_step(self, dtime, ...)
if self.itemstring == "" or not self.object:get_pos() then
return -- Item is removed
end
if vector.equals(old_acc, {x=0, y=0, z=0}) then
return -- No motion change
end
local p = self.object:getpos()
p.y = p.y - 0.5
local node = minetest.get_node(p)
local def = minetest.registered_nodes[node.name]
-- Ignore is walkable, so let it stop until the stuff below loaded
local entity_fall = (def and not def.walkable)
if self.physical_state == entity_fall then
return -- State didn't change, don't do anything
-- Acceleration defines in this case whether it's moving or not
local new_acc = self.object:get_acceleration()
if vector.equals(new_acc, {x=0, y=0, z=0}) then
-- Not moving: Try to place on node below
builtin_item_stopped(self)
end
-- Different to previous state - resetting the velocity doesn't hurt anything
self.object:setvelocity({x=0, y=0, z=0})
if entity_fall then
-- Entity is falling: downwards acceleration of earth
self.object:setacceleration({x=0, y=-9.81, z=0})
else
-- Entity stopped, call our magic planting function
local success = builtin_item_stopped(self, vector.round(p), node)
if success then
return -- The entity doesn't exist anymore when our function was successful
end
-- Code from the original __builtin:item, get surrounding objects
local own_stack = ItemStack(self.itemstring)
for _, object in ipairs(minetest.get_objects_inside_radius(p, 0.8)) do
local obj = object:get_luaentity()
if obj and obj.name == "__builtin:item"
and obj.physical_state == false then
-- Try to merge the items around with this one
if self:try_merge_with(own_stack, object, obj) then
return -- Item was removed/replaced
end
end
end
self.object:setacceleration({x=0, y=0, z=0})
end
self.physical_state = entity_fall
self.object:set_properties({
physical = entity_fall
})
end