map.cpp

1
2
3
#include <planet/log.hpp>
#include <planet/map/square.hpp>
#include <planet/serialise.hpp>

planet::map::coordinates

 9
10
11
12
13
14
15
16
17
18
19
20
21
void planet::map::save(serialise::save_buffer &ab, coordinates const c) {
    ab.save_box("_p:m:coord", c.x, c.y);
}
void planet::map::load(serialise::load_buffer &lb, coordinates &c) {
    lb.load_box("_p:m:coord", c.x, c.y);
}
namespace {
    auto const sc = planet::log::format("_p:m:coord", [](auto &os, auto &box) {
        std::int32_t c, r;
        box.named("_p:m:coord", c, r);
        os << "square@(" << c << ", " << r << ')';
    });
}

planet::hexmap

27
bool planet::hexmap::is_within(affine::point2d const c, float const ir) {

kx is -cos(pi/6) and ky is sin(pi/6). kr describes the outer radius

29
    constexpr float kx{-0.8660254037844387f}, ky{0.5f};

Our hex is point up, so swap x & y

31
32
33
34
35
36
    float px{std::abs(c.y())}, py{std::abs(c.x())};
    auto const min_dot = std::min(0.0f, kx * px + ky * py);
    py -= 2.0f * min_dot * ky;
    py -= ir;
    return py < 0.0f;
}

planet::hexmap::coordinates

42
namespace {

https://www.redblobgames.com/grids/hexagons/#rounding

 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
    template<typename F>
    struct cube {
        F q = {}, r = {}, s = {-q - r};
    };
    cube<float> xy_to_qrs(planet::affine::point2d const p, float const ir) {
        auto const x = p.x(), y = p.y();
        auto const oR = 2.0f * ir / planet::sqrt3;
        return {(x * planet::sqrt3 / 3.0f - y / 3.0f) / oR,
                2.0f * y / 3.0f / oR};
    }
    cube<planet::hexmap::coordinates::value_type>
            round_qrs(cube<float> const c) {
        auto q = std::round(c.q);
        auto r = std::round(c.r);
        auto s = std::round(c.s);
        auto const qd = std::abs(q - c.q);
        auto const rd = std::abs(r - c.r);
        auto const sd = std::abs(s - c.s);
        if (qd > rd and qd > sd) {
            q = -r - s;
        } else if (rd > sd) {
            r = -q - s;
        } else {
            s = -q - r;
        }
        return {planet::hexmap::coordinates::value_type(q),
                planet::hexmap::coordinates::value_type(r),
                planet::hexmap::coordinates::value_type(s)};
    }
}


planet::hexmap::coordinates planet::hexmap::coordinates::from_position(
        affine::point2d const p, float const r) {
    auto const qrs = xy_to_qrs(p, r);
    auto const cube = round_qrs(qrs);
    return {2 * cube.q + cube.r, cube.r};
}


auto planet::hexmap::best_direction(
        coordinates const from, coordinates const to) -> coordinates {
    auto const angle = (to - from).centre().theta();
    for (std::size_t index{}; auto const ref : angles) {
        if (std::abs(ref - angle) <= 1.0f / 12.0f) {
            return directions[index];
        } else {
            ++index;
        }
    }
    return south_east;
}


void planet::hexmap::save(serialise::save_buffer &ab, coordinates const c) {
    ab.save_box("_p:h:coord", c.column(), c.row());
}
void planet::hexmap::load(serialise::load_buffer &lb, coordinates &c) {
    coordinates::value_type col{}, row{};
    lb.load_box("_p:h:coord", col, row);
    c = {col, row};
}
namespace {
    auto const hc = planet::log::format("_p:h:coord", [](auto &os, auto &box) {
        std::int32_t c, r;
        box.named("_p:h:coord", c, r);
        os << "hex@(" << c << ", " << r << ')';
    });
}