X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2Fblock.cpp;h=a3db57d91242dfadece48003c0f656fc46ff1a64;hb=ca74cd8cdaf25b5ae778bef1dbabad21cea13b2d;hp=61e2ab4e9801fc920b4885353a7bc03858620808;hpb=ed3bdc028edc0ecb5835d1c0bf18dbc59b342daf;p=blank.git diff --git a/src/world/block.cpp b/src/world/block.cpp index 61e2ab4..a3db57d 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -8,6 +8,7 @@ #include "../shared/ResourceIndex.hpp" #include +#include #include #include #include @@ -73,10 +74,11 @@ std::ostream &operator <<(std::ostream &out, const Block::Turn &turn) { 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, 255) , gravity() +, name("anonymous") , label("some block") , place_sound(-1) , remove_sound(-1) @@ -87,22 +89,35 @@ BlockType::BlockType() noexcept , collision(true) , collide_block(true) , generate(false) -, min_solidity(0.5f) -, mid_solidity(0.75f) -, max_solidity(1.0f) -, min_humidity(-1.0f) -, mid_humidity(0.0f) -, max_humidity(1.0f) -, min_temperature(-1.0f) -, mid_temperature(0.0f) -, max_temperature(1.0f) -, min_richness(-1.0f) -, mid_richness(0.0f) -, max_richness(1.0f) +, solidity(0.5f, 0.75f, 1.0f) +, humidity(-1.0f, 0.0f, 1.0f) +, temperature(-1.0f, 0.0f, 1.0f) +, richness(-1.0f, 0.0f, 1.0f) , commonness(1.0f) { } +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; + solidity = other.solidity; + humidity = other.humidity; + temperature = other.temperature; + richness = other.richness; + commonness = other.commonness; +} + void BlockType::Read( TokenStreamReader &in, ResourceIndex &snd_index, @@ -111,15 +126,18 @@ void BlockType::Read( ) { 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); @@ -130,11 +148,14 @@ void BlockType::Read( } in.Skip(Token::BRACKET_CLOSE); } else if (name == "rgb_mod") { - in.ReadVec(rgb_mod); + in.ReadVec(color_conv); + rgb_mod = BlockMesh::ColorMod(color_conv * 255.0f); } else if (name == "hsl_mod") { - in.ReadVec(hsl_mod); + in.ReadVec(color_conv); + hsl_mod = BlockMesh::ColorMod(color_conv * 255.0f); } else if (name == "outline") { - in.ReadVec(outline_color); + in.ReadVec(color_conv); + outline_color = PrimitiveMesh::Color(color_conv * 255.0f, 255); } else if (name == "gravity") { gravity = BlockGravity::Read(in); } else if (name == "label") { @@ -156,29 +177,29 @@ void BlockType::Read( } else if (name == "generate") { generate = in.GetBool(); } else if (name == "min_solidity") { - min_solidity = in.GetFloat(); + solidity.Min(in.GetFloat()); } else if (name == "mid_solidity") { - mid_solidity = in.GetFloat(); + solidity.Mid(in.GetFloat()); } else if (name == "max_solidity") { - max_solidity = in.GetFloat(); + solidity.Max(in.GetFloat()); } else if (name == "min_humidity") { - min_humidity = in.GetFloat(); + humidity.Min(in.GetFloat()); } else if (name == "mid_humidity") { - mid_humidity = in.GetFloat(); + humidity.Mid(in.GetFloat()); } else if (name == "max_humidity") { - max_humidity = in.GetFloat(); + humidity.Max(in.GetFloat()); } else if (name == "min_temperature") { - min_temperature = in.GetFloat(); + temperature.Min(in.GetFloat()); } else if (name == "mid_temperature") { - mid_temperature = in.GetFloat(); + temperature.Mid(in.GetFloat()); } else if (name == "max_temperature") { - max_temperature = in.GetFloat(); + temperature.Max(in.GetFloat()); } else if (name == "min_richness") { - min_richness = in.GetFloat(); + richness.Min(in.GetFloat()); } else if (name == "mid_richness") { - mid_richness = in.GetFloat(); + richness.Mid(in.GetFloat()); } else if (name == "max_richness") { - max_richness = in.GetFloat(); + richness.Max(in.GetFloat()); } else if (name == "commonness") { commonness = in.GetFloat(); } else if (name == "shape") { @@ -219,12 +240,14 @@ void BlockType::FillBlockMesh( 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(), outline_color); } BlockTypeRegistry::BlockTypeRegistry() { BlockType air; + air.name = "air"; + air.label = "Air"; air.visible = false; air.block_light = false; air.collision = false; @@ -234,11 +257,32 @@ BlockTypeRegistry::BlockTypeRegistry() { 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 { @@ -253,8 +297,8 @@ struct RadialGravity : strength(strength) { } glm::vec3 GetGravity(const glm::vec3 &diff, const glm::mat4 &) const noexcept override { - float dist2 = length2(diff); - glm::vec3 dir = -normalize(diff); + float dist2 = glm::length2(diff); + glm::vec3 dir = -glm::normalize(diff); return dir * (strength / dist2); }