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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 "treegen.h" // For treegen::make_tree
31 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
33 class GrowGrassABM : public ActiveBlockModifier
37 virtual std::set<std::string> getTriggerContents()
39 std::set<std::string> s;
40 s.insert("mapgen_dirt");
43 virtual float getTriggerInterval()
45 virtual u32 getTriggerChance()
47 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
49 INodeDefManager *ndef = env->getGameDef()->ndef();
50 ServerMap *map = &env->getServerMap();
52 MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
53 if(ndef->get(n_top).light_propagates &&
54 !ndef->get(n_top).isLiquid() &&
55 n_top.getLightBlend(env->getDayNightRatio(), ndef) >= 13)
57 n.setContent(ndef->getId("mapgen_dirt_with_grass"));
58 map->addNodeWithEvent(p, n);
63 class RemoveGrassABM : public ActiveBlockModifier
67 virtual std::set<std::string> getTriggerContents()
69 std::set<std::string> s;
70 s.insert("mapgen_dirt_with_grass");
73 virtual float getTriggerInterval()
75 virtual u32 getTriggerChance()
77 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
79 INodeDefManager *ndef = env->getGameDef()->ndef();
80 ServerMap *map = &env->getServerMap();
82 MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
83 if(!ndef->get(n_top).light_propagates ||
84 ndef->get(n_top).isLiquid())
86 n.setContent(ndef->getId("mapgen_dirt"));
87 map->addNodeWithEvent(p, n);
92 class MakeTreesFromSaplingsABM : public ActiveBlockModifier
96 virtual std::set<std::string> getTriggerContents()
98 std::set<std::string> s;
102 virtual float getTriggerInterval()
104 virtual u32 getTriggerChance()
106 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
107 u32 active_object_count, u32 active_object_count_wider)
109 INodeDefManager *ndef = env->getGameDef()->ndef();
110 ServerMap *map = &env->getServerMap();
112 actionstream<<"A sapling grows into a tree at "
115 core::map<v3s16, MapBlock*> modified_blocks;
117 ManualMapVoxelManipulator vmanip(map);
118 v3s16 tree_blockp = getNodeBlockPos(tree_p);
119 vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
120 bool is_apple_tree = myrand()%4 == 0;
121 treegen::make_tree(vmanip, tree_p, is_apple_tree, ndef);
122 vmanip.blitBackAll(&modified_blocks);
125 core::map<v3s16, MapBlock*> lighting_modified_blocks;
126 for(core::map<v3s16, MapBlock*>::Iterator
127 i = modified_blocks.getIterator();
128 i.atEnd() == false; i++)
130 lighting_modified_blocks.insert(i.getNode()->getKey(), i.getNode()->getValue());
132 map->updateLighting(lighting_modified_blocks, modified_blocks);
134 // Send a MEET_OTHER event
136 event.type = MEET_OTHER;
137 for(core::map<v3s16, MapBlock*>::Iterator
138 i = modified_blocks.getIterator();
139 i.atEnd() == false; i++)
141 v3s16 p = i.getNode()->getKey();
142 event.modified_blocks.insert(p, true);
144 map->dispatchEvent(&event);
148 void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef)
150 env->addActiveBlockModifier(new GrowGrassABM());
151 env->addActiveBlockModifier(new RemoveGrassABM());
152 env->addActiveBlockModifier(new MakeTreesFromSaplingsABM());