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;
103 content_t c_dirt_with_grass;
106 MakeTreesFromSaplingsABM(ServerEnvironment *env, INodeDefManager *nodemgr) {
107 c_junglesapling = nodemgr->getId("junglesapling");
108 c_dirt = nodemgr->getId("mapgen_dirt");
109 c_dirt_with_grass = nodemgr->getId("mapgen_dirt_with_grass");
112 virtual std::set<std::string> getTriggerContents()
114 std::set<std::string> s;
116 s.insert("junglesapling");
119 virtual float getTriggerInterval()
121 virtual u32 getTriggerChance()
123 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
124 u32 active_object_count, u32 active_object_count_wider)
126 INodeDefManager *ndef = env->getGameDef()->ndef();
127 ServerMap *map = &env->getServerMap();
129 MapNode n_below = map->getNodeNoEx(p - v3s16(0, 1, 0));
130 if (n_below.getContent() != c_dirt &&
131 n_below.getContent() != c_dirt_with_grass)
134 bool is_jungle_tree = n.getContent() == c_junglesapling;
136 actionstream <<"A " << (is_jungle_tree ? "jungle " : "")
137 << "sapling grows into a tree at "
138 << PP(p) << std::endl;
140 std::map<v3s16, MapBlock*> modified_blocks;
142 ManualMapVoxelManipulator vmanip(map);
143 v3s16 tree_blockp = getNodeBlockPos(tree_p);
144 vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
146 if (is_jungle_tree) {
147 treegen::make_jungletree(vmanip, tree_p, ndef, myrand());
149 bool is_apple_tree = myrand() % 4 == 0;
150 treegen::make_tree(vmanip, tree_p, is_apple_tree, ndef, myrand());
153 vmanip.blitBackAll(&modified_blocks);
156 std::map<v3s16, MapBlock*> lighting_modified_blocks;
157 lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
158 map->updateLighting(lighting_modified_blocks, modified_blocks);
160 // Send a MEET_OTHER event
162 event.type = MEET_OTHER;
163 // event.modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
164 for(std::map<v3s16, MapBlock*>::iterator
165 i = modified_blocks.begin();
166 i != modified_blocks.end(); ++i)
168 event.modified_blocks.insert(i->first);
170 map->dispatchEvent(&event);
174 class LiquidFlowABM : public ActiveBlockModifier
177 std::set<std::string> contents;
180 LiquidFlowABM(ServerEnvironment *env, INodeDefManager *nodemgr)
182 std::set<content_t> liquids;
183 nodemgr->getIds("group:liquid", liquids);
184 for(std::set<content_t>::const_iterator k = liquids.begin(); k != liquids.end(); k++)
185 contents.insert(nodemgr->get(*k).liquid_alternative_flowing);
188 virtual std::set<std::string> getTriggerContents()
192 virtual float getTriggerInterval()
194 virtual u32 getTriggerChance()
196 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
198 ServerMap *map = &env->getServerMap();
199 if (map->transforming_liquid_size() < 500)
200 map->transforming_liquid_add(p);
201 //if ((*map).m_transforming_liquid.size() < 500) (*map).m_transforming_liquid.push_back(p);
205 void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef)
207 env->addActiveBlockModifier(new GrowGrassABM());
208 env->addActiveBlockModifier(new RemoveGrassABM());
209 env->addActiveBlockModifier(new MakeTreesFromSaplingsABM(env, nodedef));
210 if (g_settings->getBool("liquid_finite"))
211 env->addActiveBlockModifier(new LiquidFlowABM(env, nodedef));