3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_abm.h"
22 #include "environment.h"
25 #include "content_sao.h"
27 #include "mapblock.h" // For getNodeBlockPos
28 #include "mapgen.h" // For mapgen::make_tree
30 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
32 class GrowGrassABM : public ActiveBlockModifier
36 virtual std::set<std::string> getTriggerContents()
38 std::set<std::string> s;
42 virtual float getTriggerInterval()
44 virtual u32 getTriggerChance()
46 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
48 INodeDefManager *ndef = env->getGameDef()->ndef();
49 ServerMap *map = &env->getServerMap();
51 MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
52 if(ndef->get(n_top).light_propagates &&
53 !ndef->get(n_top).isLiquid() &&
54 n_top.getLightBlend(env->getDayNightRatio(), ndef) >= 13)
56 n.setContent(ndef->getId("dirt_with_grass"));
57 map->addNodeWithEvent(p, n);
62 class RemoveGrassABM : public ActiveBlockModifier
66 virtual std::set<std::string> getTriggerContents()
68 std::set<std::string> s;
69 s.insert("dirt_with_grass");
72 virtual float getTriggerInterval()
74 virtual u32 getTriggerChance()
76 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
78 INodeDefManager *ndef = env->getGameDef()->ndef();
79 ServerMap *map = &env->getServerMap();
81 MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
82 if(!ndef->get(n_top).light_propagates ||
83 ndef->get(n_top).isLiquid())
85 n.setContent(ndef->getId("dirt"));
86 map->addNodeWithEvent(p, n);
91 class MakeTreesFromSaplingsABM : public ActiveBlockModifier
95 virtual std::set<std::string> getTriggerContents()
97 std::set<std::string> s;
101 virtual float getTriggerInterval()
103 virtual u32 getTriggerChance()
105 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
106 u32 active_object_count, u32 active_object_count_wider)
108 INodeDefManager *ndef = env->getGameDef()->ndef();
109 ServerMap *map = &env->getServerMap();
111 actionstream<<"A sapling grows into a tree at "
114 core::map<v3s16, MapBlock*> modified_blocks;
116 ManualMapVoxelManipulator vmanip(map);
117 v3s16 tree_blockp = getNodeBlockPos(tree_p);
118 vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
119 bool is_apple_tree = myrand()%4 == 0;
120 mapgen::make_tree(vmanip, tree_p, is_apple_tree, ndef);
121 vmanip.blitBackAll(&modified_blocks);
124 core::map<v3s16, MapBlock*> lighting_modified_blocks;
125 for(core::map<v3s16, MapBlock*>::Iterator
126 i = modified_blocks.getIterator();
127 i.atEnd() == false; i++)
129 lighting_modified_blocks.insert(i.getNode()->getKey(), i.getNode()->getValue());
131 map->updateLighting(lighting_modified_blocks, modified_blocks);
133 // Send a MEET_OTHER event
135 event.type = MEET_OTHER;
136 for(core::map<v3s16, MapBlock*>::Iterator
137 i = modified_blocks.getIterator();
138 i.atEnd() == false; i++)
140 v3s16 p = i.getNode()->getKey();
141 event.modified_blocks.insert(p, true);
143 map->dispatchEvent(&event);
147 void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef)
149 env->addActiveBlockModifier(new GrowGrassABM());
150 env->addActiveBlockModifier(new RemoveGrassABM());
151 env->addActiveBlockModifier(new MakeTreesFromSaplingsABM());