#include "../shared/ResourceIndex.hpp"
#include <iostream>
+#include <stdexcept>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/transform.hpp>
BlockType::BlockType() noexcept
: shape(nullptr)
, textures()
-, hsl_mod(0.0f, 1.0f, 1.0f)
-, rgb_mod(1.0f, 1.0f, 1.0f)
-, outline_color(-1, -1, -1)
+, hsl_mod(0, 255, 255)
+, rgb_mod(255, 255, 255)
+, outline_color(0, 0, 0)
, gravity()
+, name("anonymous")
, label("some block")
, place_sound(-1)
, remove_sound(-1)
}
+void BlockType::Copy(const BlockType &other) noexcept {
+ shape = other.shape;
+ textures = other.textures;
+ hsl_mod = other.hsl_mod;
+ rgb_mod = other.rgb_mod;
+ outline_color = other.outline_color;
+ place_sound = other.place_sound;
+ remove_sound = other.remove_sound;
+ luminosity = other.luminosity;
+ visible = other.visible;
+ block_light = other.block_light;
+ collision = other.collision;
+ collide_block = collide_block;
+ generate = other.generate;
+ min_solidity = other.min_solidity;
+ mid_solidity = other.mid_solidity;
+ max_solidity = other.max_solidity;
+ min_humidity = other.min_humidity;
+ mid_humidity = other.mid_humidity;
+ max_humidity = other.max_humidity;
+ min_temperature = other.min_temperature;
+ mid_temperature = other.mid_temperature;
+ max_temperature = other.max_temperature;
+ min_richness = other.min_richness;
+ mid_richness = other.mid_richness;
+ max_richness = other.max_richness;
+ commonness = other.commonness;
+}
+
void BlockType::Read(
TokenStreamReader &in,
ResourceIndex &snd_index,
) {
std::string name;
in.Skip(Token::ANGLE_BRACKET_OPEN);
+ glm::vec3 color_conv;
while (in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
in.ReadIdentifier(name);
in.Skip(Token::EQUALS);
if (name == "visible") {
visible = in.GetBool();
} else if (name == "texture") {
+ textures.clear();
in.ReadString(name);
textures.push_back(tex_index.GetID(name));
} else if (name == "textures") {
+ textures.clear();
in.Skip(Token::BRACKET_OPEN);
while (in.Peek().type != Token::BRACKET_CLOSE) {
in.ReadString(name);
}
in.Skip(Token::BRACKET_CLOSE);
} else if (name == "rgb_mod") {
- in.ReadVec(rgb_mod);
+ in.ReadVec(color_conv);
+ rgb_mod = glm::tvec3<unsigned char>(color_conv * 255.0f);
} else if (name == "hsl_mod") {
- in.ReadVec(hsl_mod);
+ in.ReadVec(color_conv);
+ hsl_mod = glm::tvec3<unsigned char>(color_conv * 255.0f);
} else if (name == "outline") {
- in.ReadVec(outline_color);
+ in.ReadVec(color_conv);
+ outline_color = glm::tvec3<unsigned char>(color_conv * 255.0f);
} else if (name == "gravity") {
gravity = BlockGravity::Read(in);
} else if (name == "label") {
void BlockType::OutlinePrimitiveMesh(PrimitiveMesh::Buffer &buf) const noexcept {
if (!shape) return;
shape->Outline(buf);
- buf.colors.insert(buf.colors.end(), shape->OutlineCount(), glm::vec4(outline_color, 1.0f));
+ buf.colors.insert(buf.colors.end(), shape->OutlineCount(), glm::tvec4<unsigned char>(outline_color, 255));
}
BlockTypeRegistry::BlockTypeRegistry() {
BlockType air;
+ air.name = "air";
+ air.label = "Air";
air.visible = false;
air.block_light = false;
air.collision = false;
Block::Type BlockTypeRegistry::Add(BlockType &&t) {
int id = types.size();
+ if (!names.emplace(t.name, id).second) {
+ throw std::runtime_error("duplicate block type name " + t.name);
+ }
types.push_back(std::move(t));
types.back().id = id;
return id;
}
+BlockType &BlockTypeRegistry::Get(const std::string &name) {
+ auto entry = names.find(name);
+ if (entry != names.end()) {
+ return Get(entry->second);
+ } else {
+ throw std::runtime_error("unknown block type " + name);
+ }
+}
+
+const BlockType &BlockTypeRegistry::Get(const std::string &name) const {
+ auto entry = names.find(name);
+ if (entry != names.end()) {
+ return Get(entry->second);
+ } else {
+ throw std::runtime_error("unknown block type " + name);
+ }
+}
+
namespace {