[Tree] New attributes. [biomes.lua] Desert now spawns cacti and dead bushes.

This commit is contained in:
Quentin Bazin 2020-03-08 17:28:35 +01:00
parent 4079d17dcc
commit edec9ff87f
15 changed files with 123 additions and 29 deletions

View File

@ -49,10 +49,10 @@ Item &Registry::registerSerializedItem(sf::Packet &packet) {
return m_items.back();
}
Tree &Registry::registerTree(const std::string &stringID, const std::string &label) {
Tree &Registry::registerTree(const std::string &stringID) {
size_t id = m_trees.size();
m_treesID.emplace(stringID, id);
m_trees.emplace_back(id, stringID, label);
m_trees.emplace_back(id, stringID);
return m_trees.back();
}

View File

@ -69,7 +69,7 @@ class Registry : public ISerializable {
return m_recipes.back().get();
}
Tree &registerTree(const std::string &stringID, const std::string &label);
Tree &registerTree(const std::string &stringID);
Tree &registerSerializedTree(sf::Packet &packet);
Biome &registerBiome(const std::string &stringID, const std::string &label);

View File

@ -27,17 +27,18 @@
#include "NetworkUtils.hpp"
#include "Tree.hpp"
Tree::Tree(u16 id, const std::string &stringID, const std::string &label) {
Tree::Tree(u16 id, const std::string &stringID) {
m_id = id;
m_stringID = stringID;
m_label = label;
}
void Tree::serialize(sf::Packet &packet) const {
packet << m_id << m_stringID << m_label << m_logBlockID << m_leavesBlockID;
packet << m_id << m_stringID << m_logBlockID << m_leavesBlockID
<< m_trunkMinHeight << m_trunkMaxHeight << m_hasLeaves;
}
void Tree::deserialize(sf::Packet &packet) {
packet >> m_id >> m_stringID >> m_label >> m_logBlockID >> m_leavesBlockID;
packet >> m_id >> m_stringID >> m_logBlockID >> m_leavesBlockID
>> m_trunkMinHeight >> m_trunkMaxHeight >> m_hasLeaves;
}

View File

@ -36,29 +36,40 @@
class Tree : public ISerializable {
public:
Tree() = default;
Tree(u16 id, const std::string &stringID, const std::string &label);
Tree(u16 id, const std::string &stringID);
void serialize(sf::Packet &packet) const override;
void deserialize(sf::Packet &packet) override;
u16 id() const { return m_id; }
const std::string &stringID() const { return m_stringID; }
const std::string &label() const { return m_label; }
void setLabel(const std::string &label) { m_label = label; }
u16 getLogBlockID() const { return m_logBlockID; }
u16 getLeavesBlockID() const { return m_leavesBlockID; }
u8 trunkMinHeight() const { return m_trunkMinHeight; }
u8 trunkMaxHeight() const { return m_trunkMaxHeight; }
bool hasLeaves() const { return m_hasLeaves; }
void setLogBlockID(u16 value) { m_logBlockID = value; }
void setLeavesBlockID(u16 value) { m_leavesBlockID = value; }
private:
u16 m_id;
std::string m_stringID;
std::string m_label;
void setTrunkHeight(u8 min, u8 max) { m_trunkMinHeight = min; m_trunkMaxHeight = max; }
u16 m_logBlockID;
u16 m_leavesBlockID;
void setHasLeaves(bool hasLeaves) { m_hasLeaves = hasLeaves; }
private:
u16 m_id = 0;
std::string m_stringID;
u16 m_logBlockID = 0;
u16 m_leavesBlockID = 0;
u8 m_trunkMinHeight = 0;
u8 m_trunkMaxHeight = 0;
bool m_hasLeaves = true;
};
#endif // TREE_HPP_

View File

@ -99,6 +99,21 @@ mod:biome {
precipitation = -0.7
},
trees = {
{
type = "default:tree_cactus",
probability = 0.001,
}
},
flora = {
{
block = "default:deadbush",
spawns_on = "default:sand",
probability = 0.005,
}
},
top_block = "default:sand",
ground_block = "default:sand",
deep_block = "default:stone",

View File

@ -234,3 +234,19 @@ mod:block {
is_opaque = false,
}
mod:block {
id = "cactus",
name = "Cactus",
tiles = {"cactus_top.png", "cactus_side.png"},
draw_type = "boundingbox",
bounding_box = {2/16, 2/16, 0, 12/16, 12/16, 1}
}
mod:block {
id = "deadbush",
name = "Dead Bush",
tiles = "deadbush.png",
draw_type = "xshape",
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 718 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 430 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

View File

@ -27,8 +27,26 @@
mod:tree {
id = "oak",
name = "Oak",
log_block = "default:oak_wood",
leaves_block = "default:oak_leaves"
leaves_block = "default:oak_leaves",
trunk_height = {
min = 3,
max = 6
},
}
mod:tree {
id = "tree_cactus",
log_block = "default:cactus",
trunk_height = {
min = 3,
max = 4
},
has_leaves = false,
}

View File

@ -24,17 +24,48 @@
*
* =====================================================================================
*/
#include <gk/core/Debug.hpp>
#include "LuaBiomeLoader.hpp"
#include "LuaMod.hpp"
#include "Registry.hpp"
void LuaBiomeLoader::loadTree(const sol::table &table) const {
std::string stringID = m_mod.id() + ":" + table["id"].get<std::string>();
std::string label = table["name"].get<std::string>();
Tree &tree = Registry::getInstance().registerTree(stringID, label);
Tree &tree = Registry::getInstance().registerTree(stringID);
tree.setLogBlockID(Registry::getInstance().getBlockFromStringID(table["log_block"]).id());
tree.setLeavesBlockID(Registry::getInstance().getBlockFromStringID(table["leaves_block"]).id());
sol::object trunkHeightObject = table["trunk_height"];
if (trunkHeightObject.valid()) {
if (trunkHeightObject.get_type() == sol::type::table) {
sol::table trunkHeight = trunkHeightObject.as<sol::table>();
tree.setTrunkHeight(trunkHeight["min"], trunkHeight["max"]);
}
else
DEBUG("ERROR: For tree '" + stringID + "': trunk_height must be a table");
}
sol::object hasLeavesObject = table["has_leaves"];
if (hasLeavesObject.valid()) {
if (hasLeavesObject.get_type() == sol::type::boolean) {
tree.setHasLeaves(hasLeavesObject.as<bool>());
}
else
DEBUG("ERROR: For tree '" + stringID + "': has_leaves must be a boolean");
}
sol::object leavesBlockObject = table["leaves_block"];
if (leavesBlockObject.valid()) {
if (leavesBlockObject.get_type() == sol::type::string) {
std::string leavesBlock = leavesBlockObject.as<std::string>();
tree.setLeavesBlockID(Registry::getInstance().getBlockFromStringID(leavesBlock).id());
}
else
DEBUG("ERROR: For tree '" + stringID + "': leaves_block must be a string");
}
else if (tree.hasLeaves())
DEBUG("ERROR: For tree '" + stringID + "': leaves_block must be defined if has_leaves == true");
}
void LuaBiomeLoader::loadBiome(const sol::table &table) const {

View File

@ -77,20 +77,22 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const {
const Tree &tree = Registry::getInstance().getTree(treePlacement.treeID);
// Trunk
int h = (rand() & 3) + 3;
int h = (rand() & (tree.trunkMaxHeight() - tree.trunkMinHeight())) + tree.trunkMinHeight();
for (int i = 0; i < h; i++) {
chunk.setBlockRaw(x, y, z + i, tree.getLogBlockID());
}
// Leaves
for (int iz = -3; iz <= 3; iz++) {
for (int iy = -3; iy <= 3; iy++) {
for (int ix = -3; ix <= 3; ix++) {
if (ix * ix + iy * iy + iz * iz < 8 + (rand() & 1) && !chunk.getBlock(x + ix, y + iy, z + h + iz)) {
chunk.setBlockRaw(x + ix, y + iy, z + h + iz, tree.getLeavesBlockID());
if (tree.hasLeaves()) {
for (int iz = -3; iz <= 3; iz++) {
for (int iy = -3; iy <= 3; iy++) {
for (int ix = -3; ix <= 3; ix++) {
if (ix * ix + iy * iy + iz * iz < 8 + (rand() & 1) && !chunk.getBlock(x + ix, y + iy, z + h + iz)) {
chunk.setBlockRaw(x + ix, y + iy, z + h + iz, tree.getLeavesBlockID());
// FIXME: This is a temporary fix for the second part of #41
chunk.lightmap().setSunlight(x + ix, y + iy, z + h + iz, 0);
// FIXME: This is a temporary fix for the second part of #41
chunk.lightmap().setSunlight(x + ix, y + iy, z + h + iz, 0);
}
}
}
}