3 Copyright (C) 2013 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
29 #include "main.h" // for g_settings
32 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
34 class GrowGrassABM : public ActiveBlockModifier
38 virtual std::set<std::string> getTriggerContents()
40 std::set<std::string> s;
41 s.insert("mapgen_dirt");
44 virtual float getTriggerInterval()
46 virtual u32 getTriggerChance()
48 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
50 INodeDefManager *ndef = env->getGameDef()->ndef();
51 ServerMap *map = &env->getServerMap();
53 MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
54 content_t c_snow = ndef->getId("snow");
55 if(ndef->get(n_top).light_propagates &&
56 !ndef->get(n_top).isLiquid() &&
57 n_top.getLightBlend(env->getDayNightRatio(), ndef) >= 13)
59 if(c_snow != CONTENT_IGNORE && n_top.getContent() == c_snow)
60 n.setContent(ndef->getId("dirt_with_snow"));
62 n.setContent(ndef->getId("mapgen_dirt_with_grass"));
63 map->addNodeWithEvent(p, n);
68 class RemoveGrassABM : public ActiveBlockModifier
72 virtual std::set<std::string> getTriggerContents()
74 std::set<std::string> s;
75 s.insert("mapgen_dirt_with_grass");
78 virtual float getTriggerInterval()
80 virtual u32 getTriggerChance()
82 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
84 INodeDefManager *ndef = env->getGameDef()->ndef();
85 ServerMap *map = &env->getServerMap();
87 MapNode n_top = map->getNodeNoEx(p+v3s16(0,1,0));
88 if((!ndef->get(n_top).light_propagates &&
89 n_top.getContent() != CONTENT_IGNORE) ||
90 ndef->get(n_top).isLiquid())
92 n.setContent(ndef->getId("mapgen_dirt"));
93 map->addNodeWithEvent(p, n);
98 class MakeTreesFromSaplingsABM : public ActiveBlockModifier
101 content_t c_junglesapling;
104 MakeTreesFromSaplingsABM(ServerEnvironment *env, INodeDefManager *nodemgr) {
105 c_junglesapling = nodemgr->getId("junglesapling");
108 virtual std::set<std::string> getTriggerContents()
110 std::set<std::string> s;
112 s.insert("junglesapling");
115 virtual float getTriggerInterval()
117 virtual u32 getTriggerChance()
119 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
120 u32 active_object_count, u32 active_object_count_wider)
122 INodeDefManager *ndef = env->getGameDef()->ndef();
123 ServerMap *map = &env->getServerMap();
125 MapNode n_below = map->getNodeNoEx(p - v3s16(0, 1, 0));
126 if (!((ItemGroupList) ndef->get(n_below).groups)["soil"])
129 bool is_jungle_tree = n.getContent() == c_junglesapling;
131 actionstream <<"A " << (is_jungle_tree ? "jungle " : "")
132 << "sapling grows into a tree at "
133 << PP(p) << std::endl;
135 std::map<v3s16, MapBlock*> modified_blocks;
137 ManualMapVoxelManipulator vmanip(map);
138 v3s16 tree_blockp = getNodeBlockPos(tree_p);
139 vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
141 if (is_jungle_tree) {
142 treegen::make_jungletree(vmanip, tree_p, ndef, myrand());
144 bool is_apple_tree = myrand() % 4 == 0;
145 treegen::make_tree(vmanip, tree_p, is_apple_tree, ndef, myrand());
148 vmanip.blitBackAll(&modified_blocks);
151 std::map<v3s16, MapBlock*> lighting_modified_blocks;
152 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
153 map->updateLighting(lighting_modified_blocks, modified_blocks);
155 // Send a MEET_OTHER event
157 event.type = MEET_OTHER;
158 // event.modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
159 for(std::map<v3s16, MapBlock*>::iterator
160 i = modified_blocks.begin();
161 i != modified_blocks.end(); ++i)
163 event.modified_blocks.insert(i->first);
165 map->dispatchEvent(&event);
169 class LiquidFlowABM : public ActiveBlockModifier
172 std::set<std::string> contents;
175 LiquidFlowABM(ServerEnvironment *env, INodeDefManager *nodemgr)
177 std::set<content_t> liquids;
178 nodemgr->getIds("group:liquid", liquids);
179 for(std::set<content_t>::const_iterator k = liquids.begin(); k != liquids.end(); k++)
180 contents.insert(nodemgr->get(*k).liquid_alternative_flowing);
183 virtual std::set<std::string> getTriggerContents()
187 virtual float getTriggerInterval()
189 virtual u32 getTriggerChance()
191 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
193 ServerMap *map = &env->getServerMap();
194 if (map->transforming_liquid_size() > 500)
196 map->transforming_liquid_add(p);
197 //if ((*map).m_transforming_liquid.size() < 500) (*map).m_transforming_liquid.push_back(p);
201 class LiquidDropABM : public ActiveBlockModifier
204 std::set<std::string> contents;
207 LiquidDropABM(ServerEnvironment *env, INodeDefManager *nodemgr)
209 std::set<content_t> liquids;
210 nodemgr->getIds("group:liquid", liquids);
211 for(std::set<content_t>::const_iterator k = liquids.begin(); k != liquids.end(); k++)
212 contents.insert(nodemgr->get(*k).liquid_alternative_source);
214 virtual std::set<std::string> getTriggerContents()
216 virtual std::set<std::string> getRequiredNeighbors()
218 std::set<std::string> neighbors;
219 neighbors.insert("mapgen_air");
222 virtual float getTriggerInterval()
224 virtual u32 getTriggerChance()
226 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
228 ServerMap *map = &env->getServerMap();
229 if (map->transforming_liquid_size() > 500)
231 //todo: look around except top
232 MapNode n_below = map->getNodeNoEx(p - v3s16(0, 1, 0));
233 if (n_below.getContent() != CONTENT_AIR)
235 map->transforming_liquid_add(p);
239 void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef)
241 env->addActiveBlockModifier(new GrowGrassABM());
242 env->addActiveBlockModifier(new RemoveGrassABM());
243 env->addActiveBlockModifier(new MakeTreesFromSaplingsABM(env, nodedef));
244 if (g_settings->getBool("liquid_finite")) {
245 env->addActiveBlockModifier(new LiquidFlowABM(env, nodedef));
246 env->addActiveBlockModifier(new LiquidDropABM(env, nodedef));