Move the sapling growing and grass adding/removing ABMs to Lua

This commit is contained in:
Novatux 2013-11-02 10:48:13 +01:00
parent b1c82f332c
commit 69a59f1e41
4 changed files with 134 additions and 139 deletions

@ -6,6 +6,7 @@ minetest.features = {
chat_send_player_param3 = true,
get_all_craft_recipes_works = true,
use_texture_alpha = true,
no_legacy_abms = true,
}
function minetest.has_feature(arg)

@ -1519,6 +1519,129 @@ minetest.register_node("default:apple", {
sounds = default.node_sound_defaults(),
})
local c_air = minetest.get_content_id("air")
local c_ignore = minetest.get_content_id("ignore")
local c_tree = minetest.get_content_id("default:tree")
local c_leaves = minetest.get_content_id("default:leaves")
local c_apple = minetest.get_content_id("default:apple")
function default.grow_tree(data, a, pos, is_apple_tree, seed)
--[[
NOTE: Tree-placing code is currently duplicated in the engine
and in games that have saplings; both are deprecated but not
replaced yet
]]--
local pr = PseudoRandom(seed)
local th = pr:next(4, 5)
local x, y, z = pos.x, pos.y, pos.z
for yy = y, y+th-1 do
local vi = a:index(x, yy, z)
if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
data[vi] = c_tree
end
end
y = y+th-1 -- (x, y, z) is now last piece of trunk
local leaves_a = VoxelArea:new{MinEdge={x=-2, y=-1, z=-2}, MaxEdge={x=2, y=2, z=2}}
local leaves_buffer = {}
-- Force leaves near the trunk
local d = 1
for xi = -d, d do
for yi = -d, d do
for zi = -d, d do
leaves_buffer[leaves_a:index(xi, yi, zi)] = true
end
end
end
-- Add leaves randomly
for iii = 1, 8 do
local d = 1
local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
for xi = 0, d do
for yi = 0, d do
for zi = 0, d do
leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
end
end
end
end
-- Add the leaves
for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
if a:contains(x+xi, y+yi, z+zi) then
local vi = a:index(x+xi, y+yi, z+zi)
if data[vi] == c_air or data[vi] == c_ignore then
if leaves_buffer[leaves_a:index(xi, yi, zi)] then
if is_apple_tree and pr:next(1, 100) <= 10 then
data[vi] = c_apple
else
data[vi] = c_leaves
end
end
end
end
end
end
end
end
minetest.register_abm({
nodenames = {"default:sapling"},
interval = 10,
chance = 50,
action = function(pos, node)
local is_soil = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name].groups.soil
if is_soil == nil or is_soil == 0 then return end
print("A sapling grows into a tree at "..minetest.pos_to_string(pos))
local vm = minetest.get_voxel_manip()
local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
local data = vm:get_data()
default.grow_tree(data, a, pos, math.random(1, 4) == 1, math.random(1,100000))
vm:set_data(data)
vm:write_to_map(data)
vm:update_map()
end
})
minetest.register_abm({
nodenames = {"default:dirt"},
interval = 2,
chance = 200,
action = function(pos, node)
local above = {x=pos.x, y=pos.y+1, z=pos.z}
local name = minetest.get_node(above).name
local nodedef = minetest.registered_nodes[name]
if nodedef and nodedef.sunlight_propagates and nodedef.liquidtype == "none" and minetest.get_node_light(above) >= 13 then
if name == "default:snow" or name == "default:snowblock" then
minetest.set_node(pos, {name = "default:dirt_with_snow"})
else
minetest.set_node(pos, {name = "default:dirt_with_grass"})
end
end
end
})
minetest.register_abm({
nodenames = {"default:dirt_with_grass"},
interval = 2,
chance = 20,
action = function(pos, node)
local above = {x=pos.x, y=pos.y+1, z=pos.z}
local name = minetest.get_node(above).name
local nodedef = minetest.registered_nodes[name]
if name ~= "ignore" and nodedef and not (nodedef.sunlight_propagates and nodedef.liquidtype == "none") then
minetest.set_node(pos, {name = "default:dirt"})
end
end
})
--
-- Crafting items
--

@ -25,7 +25,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "content_sao.h"
#include "settings.h"
#include "mapblock.h" // For getNodeBlockPos
#include "treegen.h" // For treegen::make_tree
#include "main.h" // for g_settings
#include "map.h"
#include "scripting_game.h"
@ -33,141 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
class GrowGrassABM : public ActiveBlockModifier
{
private:
public:
virtual std::set<std::string> getTriggerContents()
{
std::set<std::string> s;
s.insert("mapgen_dirt");
return s;
}
virtual float getTriggerInterval()
{ return 2.0; }
virtual u32 getTriggerChance()
{ return 200; }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
{
INodeDefManager *ndef = env->getGameDef()->ndef();
ServerMap *map = &env->getServerMap();
MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
content_t c_snow = ndef->getId("snow");
if(ndef->get(n_top).light_propagates &&
!ndef->get(n_top).isLiquid() &&
n_top.getLightBlend(env->getDayNightRatio(), ndef) >= 13)
{
if(c_snow != CONTENT_IGNORE && n_top.getContent() == c_snow)
n.setContent(ndef->getId("dirt_with_snow"));
else
n.setContent(ndef->getId("mapgen_dirt_with_grass"));
map->addNodeWithEvent(p, n);
}
}
};
class RemoveGrassABM : public ActiveBlockModifier
{
private:
public:
virtual std::set<std::string> getTriggerContents()
{
std::set<std::string> s;
s.insert("mapgen_dirt_with_grass");
return s;
}
virtual float getTriggerInterval()
{ return 2.0; }
virtual u32 getTriggerChance()
{ return 20; }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
{
INodeDefManager *ndef = env->getGameDef()->ndef();
ServerMap *map = &env->getServerMap();
MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
if((!ndef->get(n_top).light_propagates &&
n_top.getContent() != CONTENT_IGNORE) ||
ndef->get(n_top).isLiquid())
{
n.setContent(ndef->getId("mapgen_dirt"));
map->addNodeWithEvent(p, n);
}
}
};
class MakeTreesFromSaplingsABM : public ActiveBlockModifier
{
private:
content_t c_junglesapling;
public:
MakeTreesFromSaplingsABM(ServerEnvironment *env, INodeDefManager *nodemgr) {
c_junglesapling = nodemgr->getId("junglesapling");
}
virtual std::set<std::string> getTriggerContents()
{
std::set<std::string> s;
s.insert("sapling");
s.insert("junglesapling");
return s;
}
virtual float getTriggerInterval()
{ return 10.0; }
virtual u32 getTriggerChance()
{ return 50; }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
u32 active_object_count, u32 active_object_count_wider)
{
INodeDefManager *ndef = env->getGameDef()->ndef();
ServerMap *map = &env->getServerMap();
MapNode n_below = map->getNodeNoEx(p - v3s16(0, 1, 0));
if (!((ItemGroupList) ndef->get(n_below).groups)["soil"])
return;
bool is_jungle_tree = n.getContent() == c_junglesapling;
actionstream <<"A " << (is_jungle_tree ? "jungle " : "")
<< "sapling grows into a tree at "
<< PP(p) << std::endl;
std::map<v3s16, MapBlock*> modified_blocks;
v3s16 tree_p = p;
ManualMapVoxelManipulator vmanip(map);
v3s16 tree_blockp = getNodeBlockPos(tree_p);
vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
if (is_jungle_tree) {
treegen::make_jungletree(vmanip, tree_p, ndef, myrand());
} else {
bool is_apple_tree = myrand() % 4 == 0;
treegen::make_tree(vmanip, tree_p, is_apple_tree, ndef, myrand());
}
vmanip.blitBackAll(&modified_blocks);
// update lighting
std::map<v3s16, MapBlock*> lighting_modified_blocks;
lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
map->updateLighting(lighting_modified_blocks, modified_blocks);
// Send a MEET_OTHER event
MapEditEvent event;
event.type = MEET_OTHER;
// event.modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
for(std::map<v3s16, MapBlock*>::iterator
i = modified_blocks.begin();
i != modified_blocks.end(); ++i)
{
event.modified_blocks.insert(i->first);
}
map->dispatchEvent(&event);
}
};
class LiquidFlowABM : public ActiveBlockModifier {
private:
std::set<std::string> contents;
@ -370,9 +234,6 @@ class LiquidMeltAround : public LiquidMeltHot {
*/
void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef) {
env->addActiveBlockModifier(new GrowGrassABM());
env->addActiveBlockModifier(new RemoveGrassABM());
env->addActiveBlockModifier(new MakeTreesFromSaplingsABM(env, nodedef));
if (g_settings->getBool("liquid_finite")) {
env->addActiveBlockModifier(new LiquidFlowABM(env, nodedef));
env->addActiveBlockModifier(new LiquidDropABM(env, nodedef));

@ -33,6 +33,11 @@ namespace treegen
void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
bool is_apple_tree, INodeDefManager *ndef, int seed)
{
/*
NOTE: Tree-placing code is currently duplicated in the engine
and in games that have saplings; both are deprecated but not
replaced yet
*/
MapNode treenode(ndef->getId("mapgen_tree"));
MapNode leavesnode(ndef->getId("mapgen_leaves"));
MapNode applenode(ndef->getId("mapgen_apple"));
@ -511,6 +516,11 @@ v3f transposeMatrix(irr::core::matrix4 M, v3f v)
void make_jungletree(VoxelManipulator &vmanip, v3s16 p0,
INodeDefManager *ndef, int seed)
{
/*
NOTE: Tree-placing code is currently duplicated in the engine
and in games that have saplings; both are deprecated but not
replaced yet
*/
content_t c_tree = ndef->getId("mapgen_jungletree");
content_t c_leaves = ndef->getId("mapgen_jungleleaves");
if (c_tree == CONTENT_IGNORE)