Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarEntityMap.cpp
blob: c82c7bdf9eb58b6aa0f0c0a8f02c9c4c3b5e107b (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
#include "StarEntityMap.hpp"
#include "StarTileEntity.hpp"
#include "StarInteractiveEntity.hpp"
#include "StarProjectile.hpp"

namespace Star {

float const EntityMapSpatialHashSectorSize = 16.0f;
int const EntityMap::MaximumEntityBoundBox = 10000;

EntityMap::EntityMap(Vec2U const& worldSize, EntityId beginIdSpace, EntityId endIdSpace)
  : m_geometry(worldSize),
    m_spatialMap(EntityMapSpatialHashSectorSize),
    m_nextId(beginIdSpace),
    m_beginIdSpace(beginIdSpace),
    m_endIdSpace(endIdSpace) {}

EntityId EntityMap::reserveEntityId() {
  if (m_spatialMap.size() >= (size_t)(m_endIdSpace - m_beginIdSpace))
    throw EntityMapException("No more entity id space in EntityMap::reserveEntityId");

  EntityId id = m_nextId;
  while (m_spatialMap.contains(id))
    id = cycleIncrement(id, m_beginIdSpace, m_endIdSpace);
  m_nextId = cycleIncrement(id, m_beginIdSpace, m_endIdSpace);

  return id;
}

Maybe<EntityId> EntityMap::maybeReserveEntityId(EntityId entityId) {
  if (m_spatialMap.size() >= (size_t)(m_endIdSpace - m_beginIdSpace))
    throw EntityMapException("No more entity id space in EntityMap::reserveEntityId");

  if (entityId == NullEntityId || m_spatialMap.contains(entityId))
    return {};
  else
    return entityId;
}

EntityId EntityMap::reserveEntityId(EntityId entityId) {
  if (entityId == NullEntityId)
    return reserveEntityId();
  if (auto reserved = maybeReserveEntityId(entityId))
    return *reserved;

  m_nextId = entityId;
  return reserveEntityId();
}


void EntityMap::addEntity(EntityPtr entity) {
  auto position = entity->position();
  auto boundBox = entity->metaBoundBox();
  auto entityId = entity->entityId();
  auto uniqueId = entity->uniqueId();

  if (m_spatialMap.contains(entityId))
    throw EntityMapException::format("Duplicate entity id '{}' in EntityMap::addEntity", entityId);

  if (boundBox.isNegative() || boundBox.width() > MaximumEntityBoundBox || boundBox.height() > MaximumEntityBoundBox) {
    throw EntityMapException::format("Entity id: {} type: {} bound box is negative or beyond the maximum entity bound box size in EntityMap::addEntity",
        entity->entityId(), (int)entity->entityType());
  }

  if (entityId == NullEntityId)
    throw EntityMapException::format("Null entity id in EntityMap::addEntity");

  if (uniqueId && m_uniqueMap.hasLeftValue(*uniqueId))
    throw EntityMapException::format("Duplicate entity unique id ({}) on entity id ({}) in EntityMap::addEntity", *uniqueId, entityId);

  m_spatialMap.set(entityId, m_geometry.splitRect(boundBox, position), std::move(entity));
  if (uniqueId)
    m_uniqueMap.add(*uniqueId, entityId);
}

EntityPtr EntityMap::removeEntity(EntityId entityId) {
  if (auto entity = m_spatialMap.remove(entityId)) {
    m_uniqueMap.removeRight(entityId);
    return entity.take();
  }
  return {};
}

size_t EntityMap::size() const {
  return m_spatialMap.size();
}

List<EntityId> EntityMap::entityIds() const {
  return m_spatialMap.keys();
}

void EntityMap::updateAllEntities(EntityCallback const& callback, function<bool(EntityPtr const&, EntityPtr const&)> sortOrder) {
  auto updateEntityInfo = [&](SpatialMap::Entry const& entry) {
    auto const& entity = entry.value;

    auto position = entity->position();
    auto boundBox = entity->metaBoundBox();

    if (boundBox.isNegative() || boundBox.width() > MaximumEntityBoundBox || boundBox.height() > MaximumEntityBoundBox) {
      throw EntityMapException::format("Entity id: {} type: {} bound box is negative or beyond the maximum entity bound box size in EntityMap::addEntity",
          entity->entityId(), (int)entity->entityType());
    }

    auto entityId = entity->entityId();
    if (entityId == NullEntityId)
      throw EntityMapException::format("Null entity id in EntityMap::setEntityInfo");

    auto rects = m_geometry.splitRect(boundBox, position);
    if (!containersEqual(rects, entry.rects))
      m_spatialMap.set(entityId, rects);

    auto uniqueId = entity->uniqueId();
    if (uniqueId) {
      if (auto existingEntityId = m_uniqueMap.maybeRight(*uniqueId)) {
        if (entityId != *existingEntityId)
          throw EntityMapException::format("Duplicate entity unique id on entity ids ({}) and ({})", *existingEntityId, entityId);
      } else {
        m_uniqueMap.removeRight(entityId);
        m_uniqueMap.add(*uniqueId, entityId);
      }
    } else {
      m_uniqueMap.removeRight(entityId);
    }
  };

  // Even if there is no sort order, we still copy pointers to a temporary
  // list, so that it is safe to call addEntity from the callback.
  m_entrySortBuffer.clear();
  for (auto const& entry : m_spatialMap.entries())
    m_entrySortBuffer.append(&entry.second);

  if (sortOrder) {
    m_entrySortBuffer.sort([&sortOrder](auto a, auto b) {
        return sortOrder(a->value, b->value);
      });
  }

  for (auto entry : m_entrySortBuffer) {
    if (callback)
      callback(entry->value);
    updateEntityInfo(*entry);
  }
}

EntityId EntityMap::uniqueEntityId(String const& uniqueId) const {
  return m_uniqueMap.maybeRight(uniqueId).value(NullEntityId);
}

EntityPtr EntityMap::entity(EntityId entityId) const {
  auto entity = m_spatialMap.value(entityId);
  starAssert(!entity || entity->entityId() == entityId);
  return entity;
}

EntityPtr EntityMap::uniqueEntity(String const& uniqueId) const {
  return entity(uniqueEntityId(uniqueId));
}

List<EntityPtr> EntityMap::entityQuery(RectF const& boundBox, EntityFilter const& filter) const {
  List<EntityPtr> values;
  forEachEntity(boundBox, [&](EntityPtr const& entity) {
      if (!filter || filter(entity))
        values.append(entity);
    });
  return values;
}

List<EntityPtr> EntityMap::entitiesAt(Vec2F const& pos, EntityFilter const& filter) const {
  auto entityList = entityQuery(RectF::withCenter(pos, {0, 0}), filter);

  sortByComputedValue(entityList, [&](EntityPtr const& entity) -> float {
      return vmagSquared(entity->position() - pos);
    });
  return entityList;
}

List<TileEntityPtr> EntityMap::entitiesAtTile(Vec2I const& pos, EntityFilterOf<TileEntity> const& filter) const {
  List<TileEntityPtr> values;
  forEachEntityAtTile(pos, [&](TileEntityPtr const& entity) {
      if (!filter || filter(entity))
        values.append(entity);
    });
  return values;
}

void EntityMap::forEachEntity(RectF const& boundBox, EntityCallback const& callback) const {
  m_spatialMap.forEach(m_geometry.splitRect(boundBox), callback);
}

void EntityMap::forEachEntityLine(Vec2F const& begin, Vec2F const& end, EntityCallback const& callback) const {
  return m_spatialMap.forEach(m_geometry.splitRect(RectF::boundBoxOf(begin, end)), [&](EntityPtr const& entity) {
      if (m_geometry.lineIntersectsRect({begin, end}, entity->metaBoundBox().translated(entity->position())))
        callback(entity);
    });
}

void EntityMap::forEachEntityAtTile(Vec2I const& pos, EntityCallbackOf<TileEntity> const& callback) const {
  RectF rect(Vec2F(pos[0], pos[1]), Vec2F(pos[0] + 1, pos[1] + 1));
  forEachEntity(rect, [&](EntityPtr const& entity) {
      if (auto tileEntity = as<TileEntity>(entity)) {
        for (Vec2I space : tileEntity->spaces()) {
          if (m_geometry.equal(pos, space + tileEntity->tilePosition()))
            callback(tileEntity);
        }
      }
    });
}

void EntityMap::forAllEntities(EntityCallback const& callback, function<bool(EntityPtr const&, EntityPtr const&)> sortOrder) const {
  // Even if there is no sort order, we still copy pointers to a temporary
  // list, so that it is safe to call addEntity from the callback.
  List<EntityPtr const*> allEntities;
  allEntities.reserve(m_spatialMap.size());
  for (auto const& entry : m_spatialMap.entries())
    allEntities.append(&entry.second.value);

  if (sortOrder) {
    allEntities.sort([&sortOrder](EntityPtr const* a, EntityPtr const* b) {
        return sortOrder(*a, *b);
      });
  }

  for (auto ptr : allEntities)
    callback(*ptr);
}

EntityPtr EntityMap::findEntity(RectF const& boundBox, EntityFilter const& filter) const {
  EntityPtr res;
  forEachEntity(boundBox, [&filter, &res](EntityPtr const& entity) {
      if (res)
        return;
      if (filter(entity))
        res = entity;
    });
  return res;
}

EntityPtr EntityMap::findEntityLine(Vec2F const& begin, Vec2F const& end, EntityFilter const& filter) const {
  return findEntity(RectF::boundBoxOf(begin, end), [&](EntityPtr const& entity) {
      if (m_geometry.lineIntersectsRect({begin, end}, entity->metaBoundBox().translated(entity->position()))) {
        if (filter(entity))
          return true;
      }
      return false;
    });
}

EntityPtr EntityMap::findEntityAtTile(Vec2I const& pos, EntityFilterOf<TileEntity> const& filter) const {
  RectF rect(Vec2F(pos[0], pos[1]), Vec2F(pos[0] + 1, pos[1] + 1));
  return findEntity(rect, [&](EntityPtr const& entity) {
      if (auto tileEntity = as<TileEntity>(entity)) {
        for (Vec2I space : tileEntity->spaces()) {
          if (m_geometry.equal(pos, space + tileEntity->tilePosition())) {
            if (filter(tileEntity))
              return true;
          }
        }
      }
      return false;
    });
}

List<EntityPtr> EntityMap::entityLineQuery(Vec2F const& begin, Vec2F const& end, EntityFilter const& filter) const {
  List<EntityPtr> values;
  forEachEntityLine(begin, end, [&](EntityPtr const& entity) {
      if (!filter || filter(entity))
        values.append(entity);
    });
  return values;
}

EntityPtr EntityMap::closestEntity(Vec2F const& center, float radius, EntityFilter const& filter) const {
  EntityPtr closest;
  float distSquared = square(radius);
  RectF boundBox(center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius);

  m_spatialMap.forEach(m_geometry.splitRect(boundBox), [&](EntityPtr const& entity) {
      Vec2F pos = entity->position();
      float thisDistSquared = m_geometry.diff(center, pos).magnitudeSquared();
      if (distSquared > thisDistSquared) {
        if (!filter || filter(entity)) {
          distSquared = thisDistSquared;
          closest = entity;
        }
      }
    });

  return closest;
}

InteractiveEntityPtr EntityMap::interactiveEntityNear(Vec2F const& pos, float maxRadius) const {
  auto rect = RectF::withCenter(pos, Vec2F::filled(maxRadius));
  InteractiveEntityPtr interactiveEntity;
  double bestDistance = maxRadius + 100;
  double bestCenterDistance = maxRadius + 100;
  m_spatialMap.forEach(m_geometry.splitRect(rect), [&](EntityPtr const& entity) {
      if (auto ie = as<InteractiveEntity>(entity)) {
        if (ie->isInteractive()) {
          if (auto tileEntity = as<TileEntity>(entity)) {
            for (Vec2I space : tileEntity->interactiveSpaces()) {
              auto dist = m_geometry.diff(pos, centerOfTile(space + tileEntity->tilePosition())).magnitude();
              auto centerDist = m_geometry.diff(tileEntity->metaBoundBox().center() + tileEntity->position(), pos).magnitude();
              if ((dist < bestDistance) || ((dist == bestDistance) && (centerDist < bestCenterDistance))) {
                interactiveEntity = ie;
                bestDistance = dist;
                bestCenterDistance = centerDist;
              }
            }
          } else {
            auto box = ie->interactiveBoundBox().translated(entity->position());
            auto dist = m_geometry.diffToNearestCoordInBox(box, pos).magnitude();
            auto centerDist = m_geometry.diff(box.center(), pos).magnitude();
            if ((dist < bestDistance) || ((dist == bestDistance) && (centerDist < bestCenterDistance))) {
              interactiveEntity = ie;
              bestDistance = dist;
              bestCenterDistance = centerDist;
            }
          }
        }
      }
    });
  if (bestDistance <= maxRadius)
    return interactiveEntity;
  return {};
}

bool EntityMap::tileIsOccupied(Vec2I const& pos, bool includeEphemeral) const {
  RectF rect(Vec2F(pos[0], pos[1]), Vec2F(pos[0] + 1, pos[1] + 1));
  return (bool)findEntity(rect, [&](EntityPtr const& entity) {
      if (auto tileEntity = as<TileEntity>(entity)) {
        if (includeEphemeral || !tileEntity->ephemeral()) {
          for (Vec2I space : tileEntity->spaces()) {
            if (m_geometry.equal(pos, space + tileEntity->tilePosition())) {
              return true;
            }
          }
        }
      }
      return false;
    });
}

bool EntityMap::spaceIsOccupied(RectF const& rect, bool includesEphemeral) const {
  for (auto const& entity : entityQuery(rect)) {
    if (!includesEphemeral && entity->ephemeral())
      continue;

    for (RectF const& c : m_geometry.splitRect(entity->collisionArea(), entity->position())) {
      if (!c.isNull() && rect.intersects(c))
        return true;
    }
  }
  return false;
}

}