Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586// sc2k/structure/model.hpp — terrain-independent structural support state.#pragma once
#include "sc2k/terrain/model.hpp"#include "sc2k/tile_codes.hpp"
#include <cstddef>#include <cstdint>#include <limits>#include <optional>#include <span>#include <utility>#include <vector>
namespace sc2k::structure {
enum class SupportKind : std::uint8_t { None = 0, Foundation = 1, Ramp = 2,};
struct Support { SupportKind kind = SupportKind::None; std::uint8_t legacy_presentation = 0; std::int8_t elevation_offset = 0; bool obscures_water = false;
explicit operator bool() const { return kind != SupportKind::None; } friend bool operator==(const Support&, const Support&) = default;};
class Model {public: static std::optional<Model> create(terrain::Extent extent, std::vector<Support> supports) { if (extent.width <= 0 || extent.height <= 0) return std::nullopt; const std::size_t width = static_cast<std::size_t>(extent.width); const std::size_t height = static_cast<std::size_t>(extent.height); if (height > std::numeric_limits<std::size_t>::max() / width || supports.size() != width * height) return std::nullopt; for (const Support& support : supports) { if (support.kind == SupportKind::None) { if (support.legacy_presentation != 0 || support.elevation_offset != 0 || support.obscures_water) return std::nullopt; } else if (support.kind == SupportKind::Foundation) { if (xter::base_shape(support.legacy_presentation) != xter::kCube || support.elevation_offset < -31 || support.elevation_offset > 31 || support.obscures_water) return std::nullopt; } else if (support.kind == SupportKind::Ramp) { if (support.legacy_presentation < xter::kSlopeCardFirst || support.legacy_presentation > xter::kSlopeCardLast || support.elevation_offset < -31 || support.elevation_offset > 31 || !support.obscures_water) return std::nullopt; } else { return std::nullopt; } } return Model(extent, std::move(supports)); }
terrain::Extent extent() const { return extent_; }
bool contains(terrain::TileCoord coord) const { return coord.row >= 0 && coord.row < extent_.height && coord.column >= 0 && coord.column < extent_.width; }
Support support(terrain::TileCoord coord) const { return contains(coord) ? supports_[index(coord)] : Support{}; }
std::span<const Support> supports() const { return supports_; }
private: Model(terrain::Extent extent, std::vector<Support> supports) : extent_(extent), supports_(std::move(supports)) {}
std::size_t index(terrain::TileCoord coord) const { return static_cast<std::size_t>(coord.row) * extent_.width + coord.column; }
terrain::Extent extent_{}; std::vector<Support> supports_;};
} // namespace sc2k::structure