]> git.lizzy.rs Git - minetest.git/blob - src/content_abm.cpp
Install minetest_game without .git and other unnecessary things
[minetest.git] / src / content_abm.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "content_abm.h"
21
22 #include "environment.h"
23 #include "gamedef.h"
24 #include "nodedef.h"
25 #include "content_sao.h"
26 #include "settings.h"
27 #include "mapblock.h" // For getNodeBlockPos
28 #include "mapgen.h" // For mapgen::make_tree
29
30 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
31
32 class GrowGrassABM : public ActiveBlockModifier
33 {
34 private:
35 public:
36         virtual std::set<std::string> getTriggerContents()
37         {
38                 std::set<std::string> s;
39                 s.insert("dirt");
40                 return s;
41         }
42         virtual float getTriggerInterval()
43         { return 2.0; }
44         virtual u32 getTriggerChance()
45         { return 200; }
46         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
47         {
48                 INodeDefManager *ndef = env->getGameDef()->ndef();
49                 ServerMap *map = &env->getServerMap();
50                 
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)
55                 {
56                         n.setContent(ndef->getId("dirt_with_grass"));
57                         map->addNodeWithEvent(p, n);
58                 }
59         }
60 };
61
62 class RemoveGrassABM : public ActiveBlockModifier
63 {
64 private:
65 public:
66         virtual std::set<std::string> getTriggerContents()
67         {
68                 std::set<std::string> s;
69                 s.insert("dirt_with_grass");
70                 return s;
71         }
72         virtual float getTriggerInterval()
73         { return 2.0; }
74         virtual u32 getTriggerChance()
75         { return 20; }
76         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n)
77         {
78                 INodeDefManager *ndef = env->getGameDef()->ndef();
79                 ServerMap *map = &env->getServerMap();
80                 
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())
84                 {
85                         n.setContent(ndef->getId("dirt"));
86                         map->addNodeWithEvent(p, n);
87                 }
88         }
89 };
90
91 class MakeTreesFromSaplingsABM : public ActiveBlockModifier
92 {
93 private:
94 public:
95         virtual std::set<std::string> getTriggerContents()
96         {
97                 std::set<std::string> s;
98                 s.insert("sapling");
99                 return s;
100         }
101         virtual float getTriggerInterval()
102         { return 10.0; }
103         virtual u32 getTriggerChance()
104         { return 50; }
105         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
106                         u32 active_object_count, u32 active_object_count_wider)
107         {
108                 INodeDefManager *ndef = env->getGameDef()->ndef();
109                 ServerMap *map = &env->getServerMap();
110                 
111                 actionstream<<"A sapling grows into a tree at "
112                                 <<PP(p)<<std::endl;
113
114                 core::map<v3s16, MapBlock*> modified_blocks;
115                 v3s16 tree_p = p;
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);
122
123                 // update lighting
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++)
128                 {
129                         lighting_modified_blocks.insert(i.getNode()->getKey(), i.getNode()->getValue());
130                 }
131                 map->updateLighting(lighting_modified_blocks, modified_blocks);
132
133                 // Send a MEET_OTHER event
134                 MapEditEvent event;
135                 event.type = MEET_OTHER;
136                 for(core::map<v3s16, MapBlock*>::Iterator
137                         i = modified_blocks.getIterator();
138                         i.atEnd() == false; i++)
139                 {
140                         v3s16 p = i.getNode()->getKey();
141                         event.modified_blocks.insert(p, true);
142                 }
143                 map->dispatchEvent(&event);
144         }
145 };
146
147 void add_legacy_abms(ServerEnvironment *env, INodeDefManager *nodedef)
148 {
149         env->addActiveBlockModifier(new GrowGrassABM());
150         env->addActiveBlockModifier(new RemoveGrassABM());
151         env->addActiveBlockModifier(new MakeTreesFromSaplingsABM());
152 }
153
154