the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.level.h"
3#include "net.minecraft.world.level.tile.h"
4#include "TreeFeature.h"
5
6TreeFeature::TreeFeature(bool doUpdate) : Feature(doUpdate), baseHeight(4), trunkType(0), leafType(0), addJungleFeatures(false)
7{
8}
9
10TreeFeature::TreeFeature(bool doUpdate, int baseHeight, int trunkType, int leafType, bool addJungleFeatures) : Feature(doUpdate), baseHeight(baseHeight), trunkType(trunkType), leafType(leafType), addJungleFeatures(addJungleFeatures)
11{
12}
13
14bool TreeFeature::place(Level *level, Random *random, int x, int y, int z)
15{
16 int treeHeight = random->nextInt(3) + baseHeight;
17
18 bool free = true;
19 if (y < 1 || y + treeHeight + 1 > Level::maxBuildHeight) return false;
20
21 // 4J Stu Added to stop tree features generating areas previously place by game rule generation
22 if(app.getLevelGenerationOptions() != NULL)
23 {
24 PIXBeginNamedEvent(0,"TreeFeature checking intersects");
25 LevelGenerationOptions *levelGenOptions = app.getLevelGenerationOptions();
26 bool intersects = levelGenOptions->checkIntersects(x - 2, y - 1, z - 2, x + 2, y + treeHeight, z + 2);
27 PIXEndNamedEvent();
28 if(intersects)
29 {
30 //app.DebugPrintf("Skipping reeds feature generation as it overlaps a game rule structure\n");
31 return false;
32 }
33 }
34
35 for (int yy = y; yy <= y + 1 + treeHeight; yy++)
36 {
37 int r = 1;
38 if (yy == y) r = 0;
39 if (yy >= y + 1 + treeHeight - 2) r = 2;
40 for (int xx = x - r; xx <= x + r && free; xx++)
41 {
42 for (int zz = z - r; zz <= z + r && free; zz++)
43 {
44 if (yy >= 0 && yy < Level::maxBuildHeight)
45 {
46 int tt = level->getTile(xx, yy, zz);
47 if (tt != 0 && tt != Tile::leaves_Id && tt != Tile::grass_Id && tt != Tile::dirt_Id && tt != Tile::treeTrunk_Id) free = false;
48 }
49 else
50 {
51 free = false;
52 }
53 }
54 }
55 }
56
57 if (!free) return false;
58
59 int belowTile = level->getTile(x, y - 1, z);
60 if ((belowTile != Tile::grass_Id && belowTile != Tile::dirt_Id) || y >= Level::maxBuildHeight - treeHeight - 1) return false;
61
62 placeBlock(level, x, y - 1, z, Tile::dirt_Id, 0);
63
64 PIXBeginNamedEvent(0,"Placing TreeFeature leaves");
65 int grassHeight = 3;
66 int extraWidth = 0;
67 // 4J Stu - Generate leaves from the top down to stop having to recalc heightmaps
68 for (int yy = y + treeHeight; yy >= y - grassHeight + treeHeight; yy--)
69 {
70 int yo = yy - (y + treeHeight);
71 int offs = extraWidth + 1 - yo / 2;
72 for (int xx = x - offs; xx <= x + offs; xx++)
73 {
74 int xo = xx - (x);
75 for (int zz = z - offs; zz <= z + offs; zz++)
76 {
77 int zo = zz - (z);
78 if (abs(xo) == offs && abs(zo) == offs && (random->nextInt(2) == 0 || yo == 0)) continue;
79 int t = level->getTile(xx, yy, zz);
80 if (t == 0 || t == Tile::leaves_Id) placeBlock(level, xx, yy, zz, Tile::leaves_Id, leafType);
81 }
82 }
83 }
84 PIXEndNamedEvent();
85 PIXBeginNamedEvent(0,"Placing TreeFeature trunks");
86 for (int hh = 0; hh < treeHeight; hh++)
87 {
88 int t = level->getTile(x, y + hh, z);
89 if (t == 0 || t == Tile::leaves_Id)
90 {
91 placeBlock(level, x, y + hh, z, Tile::treeTrunk_Id, trunkType);
92 if (addJungleFeatures && hh > 0)
93 {
94 if (random->nextInt(3) > 0 && level->isEmptyTile(x - 1, y + hh, z))
95 {
96 placeBlock(level, x - 1, y + hh, z, Tile::vine_Id, VineTile::VINE_EAST);
97 }
98 if (random->nextInt(3) > 0 && level->isEmptyTile(x + 1, y + hh, z))
99 {
100 placeBlock(level, x + 1, y + hh, z, Tile::vine_Id, VineTile::VINE_WEST);
101 }
102 if (random->nextInt(3) > 0 && level->isEmptyTile(x, y + hh, z - 1))
103 {
104 placeBlock(level, x, y + hh, z - 1, Tile::vine_Id, VineTile::VINE_SOUTH);
105 }
106 if (random->nextInt(3) > 0 && level->isEmptyTile(x, y + hh, z + 1))
107 {
108 placeBlock(level, x, y + hh, z + 1, Tile::vine_Id, VineTile::VINE_NORTH);
109 }
110 }
111 }
112 }
113 PIXEndNamedEvent();
114 if (addJungleFeatures)
115 {
116 PIXBeginNamedEvent(0,"TreeFeature adding vines");
117 for (int yy = y - 3 + treeHeight; yy <= y + treeHeight; yy++)
118 {
119 int yo = yy - (y + treeHeight);
120 int offs = 2 - yo / 2;
121 for (int xx = x - offs; xx <= x + offs; xx++)
122 {
123 for (int zz = z - offs; zz <= z + offs; zz++)
124 {
125 if (level->getTile(xx, yy, zz) == Tile::leaves_Id)
126 {
127 if (random->nextInt(4) == 0 && level->getTile(xx - 1, yy, zz) == 0)
128 {
129 addVine(level, xx - 1, yy, zz, VineTile::VINE_EAST);
130 }
131 if (random->nextInt(4) == 0 && level->getTile(xx + 1, yy, zz) == 0)
132 {
133 addVine(level, xx + 1, yy, zz, VineTile::VINE_WEST);
134 }
135 if (random->nextInt(4) == 0 && level->getTile(xx, yy, zz - 1) == 0)
136 {
137 addVine(level, xx, yy, zz - 1, VineTile::VINE_SOUTH);
138 }
139 if (random->nextInt(4) == 0 && level->getTile(xx, yy, zz + 1) == 0)
140 {
141 addVine(level, xx, yy, zz + 1, VineTile::VINE_NORTH);
142 }
143 }
144 }
145 }
146 }
147 PIXEndNamedEvent();
148 // also chance for cocoa plants around stem
149 if (random->nextInt(5) == 0 && treeHeight > 5)
150 {
151 PIXBeginNamedEvent(0,"TreeFeature adding cocoa");
152 for (int rows = 0; rows < 2; rows++)
153 {
154 for (int dir = 0; dir < 4; dir++)
155 {
156 if (random->nextInt(4 - rows) == 0)
157 {
158 int age = random->nextInt(3);
159 placeBlock(level, x + Direction::STEP_X[Direction::DIRECTION_OPPOSITE[dir]], y + treeHeight - 5 + rows, z + Direction::STEP_Z[Direction::DIRECTION_OPPOSITE[dir]],
160 Tile::cocoa_Id, (age << 2) | dir);
161 }
162 }
163 }
164 PIXEndNamedEvent();
165 }
166 }
167
168 return true;
169}
170
171void TreeFeature::addVine(Level *level, int xx, int yy, int zz, int dir)
172{
173 placeBlock(level, xx, yy, zz, Tile::vine_Id, dir);
174 int maxDir = 4;
175 while (level->getTile(xx, --yy, zz) == 0 && maxDir > 0)
176 {
177 placeBlock(level, xx, yy, zz, Tile::vine_Id, dir);
178 maxDir--;
179 }
180}