summaryrefslogtreecommitdiffhomepage
path: root/src/object.cpp
blob: 89988747d3610e889e11f29d0f068f8b2ea87611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "object.hpp"
#include "tile-constants.hpp"
#include "world.hpp"
#include "rotation.inl"
#include "anim-atlas.hpp"
#include "src/RTree-search.hpp"
#include "rect-intersects.hpp"
#include "src/timer.hpp"
#include "compat/debug.hpp"
#include "compat/exception.hpp"
#include "compat/limits.hpp"
#include "compat/non-const.hpp"
#include "nanosecond.inl"
#include <cmath>
#include <algorithm>
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Containers/StructuredBindings.h>
#include <Corrade/Containers/Pair.h>
#include <Magnum/Math/Functions.h>

namespace floormat {

namespace {

constexpr auto object_id_lessp = [](const auto& a, const auto& b) { return a->id < b->id; };

// todo rewrite using bitwise ops. try this instead: x = 31; int((x+64+32)/64), (x + 64 + 32)%64 - 1
template<int tile_size>
constexpr inline Pair<int, int8_t> normalize_coord(const int8_t cur, const int new_off)
{
    constexpr int8_t half_tile = tile_size/2;
    const int tmp = cur + new_off;
    auto x = (int8_t)(tmp % tile_size);
    auto t = tmp / tile_size;
    auto a = Math::abs(x);
    auto s = Math::sign(x);
    bool b = x >= half_tile | x < -half_tile;
    auto tmask = -(int)b;
    auto xmask = (int8_t)-(int8_t)b;
    t += s & tmask;
    x = (int8_t)((tile_size - a)*-s) & xmask | (int8_t)(x & ~xmask);
    return { t, x };
}

} // namespace

bool object_proto::operator==(const object_proto&) const = default;
object_proto::object_proto(const object_proto&) noexcept = default;
object_proto& object_proto::operator=(const object_proto&) noexcept = default;
object_proto::object_proto(object_proto&&) noexcept = default;
object_proto& object_proto::operator=(object_proto&&) noexcept = default;
object_proto::~object_proto() noexcept = default;
object_proto::object_proto() noexcept = default;
object_type object_proto::type_of() const noexcept { return type; }

object::object(object_id id, class chunk& c, const object_proto& proto) :
    id{id}, c{&c}, atlas{proto.atlas},
    offset{proto.offset}, bbox_offset{proto.bbox_offset},
    bbox_size{proto.bbox_size}, delta{proto.delta},
    frame{proto.frame}, r{proto.r}, pass{proto.pass}
{
    if (id != 0)
    {
        fm_soft_assert(atlas);
        fm_soft_assert(atlas->check_rotation(r));
        fm_soft_assert(frame < atlas->info().nframes);
    }
}

object::~object() noexcept
{
    fm_debug_assert(id);
    if (c->is_teardown()) [[unlikely]]
        return;
#if 0
    if (chunk::bbox bb; c->_bbox_for_scenery(*this, bb))
        c->_remove_bbox(bb);
#endif
    c->_world->erase_object(id);
    const_cast<object_id&>(id) = 0;
}

float object::ordinal() const
{
    return ordinal(coord.local(), offset, atlas->group(r).z_offset);
}

float object::ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const
{
    constexpr auto inv_tile_size = 1.f/TILE_SIZE2;
    auto offset_ = ordinal_offset(offset);
    auto vec = Vector2(xy) + offset_*inv_tile_size;
    return vec[0] + vec[1] + Vector2(z_offset).sum();
}

size_t object::index() const
{
    auto& c = chunk();
    fm_assert(c._objects_sorted);
    const auto fn = [id = id](const auto& a, const auto&) { return a->id < id; };
    auto& es = c._objects;
    auto it = std::lower_bound(es.cbegin(), es.cend(), nullptr, fn);
    fm_assert(it != es.cend());
    fm_assert((*it)->id == id);
    return (size_t)std::distance(es.cbegin(), it);
}

bool object::can_rotate(global_coords coord, rotation new_r, rotation old_r,
                        Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size)
{
    if (bbox_offset.isZero() && bbox_size[0] == bbox_size[1])
        return true;
    const auto offset_ = rotate_point(offset, old_r, new_r);
    const auto bbox_offset_ = rotate_point(bbox_offset, old_r, new_r);
    const auto bbox_size_ = rotate_size(bbox_size, old_r, new_r);
    return can_move_to({}, coord, offset_, bbox_offset_, bbox_size_);
}

bool object::can_rotate(rotation new_r)
{
    if (new_r == r)
        return true;

    return can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size);
}

void object::rotate(size_t, rotation new_r)
{
    fm_assert(atlas->check_rotation(new_r));
    auto offset_ = !is_dynamic() ? rotate_point(offset, r, new_r) : offset;
    auto bbox_offset_ = rotate_point(bbox_offset, r, new_r);
    auto bbox_size_ = rotate_size(bbox_size, r, new_r);
    set_bbox(offset_, bbox_offset_, bbox_size_, pass);
    if (r != new_r && !is_dynamic())
        c->mark_scenery_modified();

    const_cast<rotation&>(r) = new_r;
}

point object::normalize_coords(global_coords coord, Vector2b cur, Vector2i new_off)
{
    auto [cx, ox] = normalize_coord<iTILE_SIZE2.x()>(cur.x(), new_off.x());
    auto [cy, oy] = normalize_coord<iTILE_SIZE2.y()>(cur.y(), new_off.y());
    coord += Vector2i(cx, cy);
    return { coord, { ox, oy }, };
}

point object::normalize_coords(const point& pt, Vector2i delta)
{
    return object::normalize_coords(pt.coord(), pt.offset(), delta);
}

template<bool neighbor = true>
static bool do_search(class chunk* c, chunk_coords_ coord,
                      object_id id, Vector2 min, Vector2 max, Vector2b off = {})
{
    if constexpr(neighbor)
    {
        const auto ch = chunk_coords{(int16_t)(coord.x + off[0]), (int16_t)(coord.y + off[1])};
        constexpr auto size = TILE_SIZE2 * TILE_MAX_DIM, grace = TILE_SIZE2 * 4;
        const auto off_ = Vector2(off) * size;
        min -= off_;
        max -= off_;
        if (!(min + grace >= Vector2{} && max - grace <= size)) [[likely]]
            return true;
        auto& w = c->world();
        c = w.at({ch, coord.z});
        if (!c) [[unlikely]]
            return true;
    }
    bool ret = true;
    c->rtree()->Search(min.data(), max.data(), [&](object_id data, const auto& r) {
        auto x = std::bit_cast<collision_data>(data);
        if (x.data != id && x.pass != (uint64_t)pass_mode::pass &&
            rect_intersects(min, max, {r.m_min[0], r.m_min[1]}, {r.m_max[0], r.m_max[1]}))
            return ret = false;
        else
            return true;
    });
    return ret;
}

bool object::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset,
                         Vector2b bbox_offset, Vector2ub bbox_size)
{
    auto [coord_, offset_] = normalize_coords(coord2, offset, delta);

    if (coord_.z() != coord.z()) [[unlikely]]
        return false;

    auto& w = *c->_world;
    auto&= coord_.chunk() == coord.chunk() ? *c : w[coord_.chunk3()];

    const auto center = Vector2(coord_.local())*TILE_SIZE2 + Vector2(offset_) + Vector2(bbox_offset),
               half_bbox = Vector2(bbox_size)*.5f,
               min = center - half_bbox, max = min + Vector2(bbox_size);
    if (!do_search<false>(&, coord_, id, min, max))
        return false;
    for (const auto& off : world::neighbor_offsets)
        if (!do_search(&, coord_, id, min, max, off))
            return false;
    return true;
}

bool object::can_move_to(Vector2i delta)
{
    return can_move_to(delta, coord, offset, bbox_offset, bbox_size);
}

void object::teleport_to(size_t& i, point pt, rotation new_r)
{
    return teleport_to(i, pt.coord(), pt.offset(), new_r);
}

void object::teleport_to(size_t& i, global_coords coord_, Vector2b offset_, rotation new_r)
{
    if (new_r == rotation_COUNT)
        new_r = r;
    else if (!atlas->check_rotation(new_r))
    {
        const auto& info = atlas->info();
        const auto *obj = info.object_name.data(), *anim = info.anim_name.data();
        fm_abort("wrong rotation %d for %s/%s!", (int)new_r, obj, anim);
    }

    fm_assert(i < c->_objects.size());
    const auto= c->_objects[i];
    fm_assert(&*== this);

    if (coord_ == coord && offset_ == offset)
        return;

    if (!is_dynamic())
        c->mark_scenery_modified();

    chunk::bbox bb0, bb1;
    const auto bb_offset = rotate_point(bbox_offset, r, new_r);
    const auto bb_size   = rotate_size(bbox_size, r, new_r);
    bool b0 = c->_bbox_for_scenery(*this, bb0),
         b1 = c->_bbox_for_scenery(*this, coord_.local(), offset_, bb_offset, bb_size, bb1);

    if (coord_.chunk() == coord.chunk())
    {
        c->_replace_bbox(bb0, bb1, b0, b1);
        const_cast<global_coords&>(coord) = coord_;
        set_bbox_(offset_, bb_offset, bb_size, pass);
        const_cast<rotation&>(r) = new_r;
    }
    else
    {
        auto& w = *c->_world;

        auto& c2 = w[coord_.chunk3()];
        if (!is_dynamic())
            c2.mark_scenery_modified();
        c2._add_bbox(bb1);
        c->remove_object(i);
        auto& es = c2._objects;
        const_cast<global_coords&>(coord) = coord_;
        set_bbox_(offset_, bb_offset, bb_size, pass);
        const_cast<rotation&>(r) = new_r;
        const_cast<class chunk*&>(c) = &c2;
        i = (size_t)std::distance(es.cbegin(), std::lower_bound(es.cbegin(), es.cend(),, object_id_lessp));
        arrayInsert(es, i, move());
    }
}

bool object::move_to(size_t& i, Vector2i delta, rotation new_r)
{
    if (!can_rotate(new_r))
        return false;
    const auto [coord_, offset_] = normalize_coords(coord, offset, delta);
    teleport_to(i, coord_, offset_, new_r);
    return true;
}

bool object::move_to(Magnum::Vector2i delta)
{
    auto i = index();
    return move_to(i, delta, r);
}

template<typename T> requires std::is_unsigned_v<T>
uint32_t object::alloc_frame_time(const Ns& dt, T& accum, uint32_t hz, float speed)
{
    constexpr auto ns_in_sec = Ns((int)1e9);
    constexpr auto accum_max = uint64_t{limits<T>::max};
    static_assert(Ns{accum_max} * ns_in_sec.stamp != Ns{}); // check for overflow on T

    const auto from_accum = uint64_t{accum} * ns_in_sec / accum_max;
    const auto from_dt = Ns(uint64_t(double(dt.stamp) * double(speed)));
    fm_assert(from_dt <= Ns{uint64_t{1} << 53});
    const auto ticks = from_dt + from_accum;
    const auto frame_duration = ns_in_sec / hz;
    const auto nframes = (uint32_t)(ticks / frame_duration);
    const auto rem = ticks % frame_duration;
    const auto new_accum_ = rem * accum_max / uint64_t{ns_in_sec};
    const auto new_accum = (T)Math::clamp(new_accum_, uint64_t{0}, accum_max);
    [[maybe_unused]] const auto old_accum = accum;
    accum = new_accum;

#if 0
    DBG_nospace << "alloc-frame-time: "
                << "dt:" << fraction(Time::to_milliseconds(dt)) << "ms"
                << " ticks:" << ticks
                << " frames:" << nframes
                << " acc:" << new_accum_
                << " rem:" << rem;
#endif
    fm_assert(nframes < 1 << 12);
    return nframes;
}

template uint32_t object::alloc_frame_time(const Ns& dt, uint16_t& accum, uint32_t hz, float speed);
template uint32_t object::alloc_frame_time(const Ns& dt, uint32_t& accum, uint32_t hz, float speed);

void object::set_bbox_(Vector2b offset_, Vector2b bb_offset_, Vector2ub bb_size_, pass_mode pass_)
{
    const_cast<Vector2b&>(offset) = offset_;
    const_cast<Vector2b&>(bbox_offset) = bb_offset_;
    const_cast<Vector2ub&>(bbox_size) = bb_size_;
    const_cast<pass_mode&>(pass) = pass_;
}

object::operator object_proto() const
{
    object_proto ret;
    ret.atlas = atlas;
    ret.offset = offset;
    ret.bbox_offset = bbox_offset;
    ret.bbox_size = bbox_size;
    ret.delta = delta;
    ret.frame = frame;
    ret.type = type();
    ret.r = r;
    ret.pass = pass;
    return ret;
}

void object::set_bbox(Vector2b offset_, Vector2b bb_offset_, Vector2ub bb_size_, pass_mode pass)
{
    fm_assert(Vector2ui(bb_size_).product() != 0);

    if (offset != offset_)
        if (!is_dynamic())
            c->mark_scenery_modified();

    chunk::bbox bb0, bb;
    const bool b0 = c->_bbox_for_scenery(*this, bb0);
    set_bbox_(offset_, bb_offset_, bb_size_, pass);
    const bool b = c->_bbox_for_scenery(*this, bb);
    c->_replace_bbox(bb0, bb, b0, b);
}

bool object::can_activate(size_t) const { return false; }
bool object::activate(size_t) { return false; }
class chunk& object::chunk() const { return *c; }
class world& object::world() const { return c->world(); }
bool object::is_virtual() const { return false; }
point object::position() const { return {coord, offset}; }
object_type object::type_of() const noexcept { return type(); }

bool object::is_dynamic() const { return atlas->info().fps > 0; }
bool object::updates_passability() const { return false; }

void object::init_script(const std::shared_ptr<object>&) {}
void object::destroy_script_pre(const std::shared_ptr<object>&, script_destroy_reason) {}
void object::destroy_script_post() {}

void object::check_script_update_1(script_lifecycle state)
{
    switch (state)
    {
    case script_lifecycle::no_init:
        //return; // for tests?
    case script_lifecycle::COUNT:
    case script_lifecycle::destroying:
    case script_lifecycle::initializing:
    case script_lifecycle::torn_down:
        break;
    case script_lifecycle::created:
        return;
    }
    fm_abort("bad script state %d in update() #1", (int)state);
}

bool object::check_script_update_2(script_lifecycle state)
{
    switch (state)
    {
    case script_lifecycle::no_init:
        // bad = false; break; // for tests?
    case script_lifecycle::COUNT:
    case script_lifecycle::destroying:
    case script_lifecycle::initializing:
        break;
    case script_lifecycle::torn_down:
        return true;
    case script_lifecycle::created:
        return false;
    }
    fm_abort("bad script state %d in update() #2", (int)state);
}

} // namespace floormat