MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / updateAllEntities

Method updateAllEntities

source/game/StarEntityMap.cpp:92–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90}
91
92void EntityMap::updateAllEntities(EntityCallback const& callback, function<bool(EntityPtr const&, EntityPtr const&)> sortOrder) {
93 auto updateEntityInfo = [&](SpatialMap::Entry const& entry) {
94 auto const& entity = entry.value;
95
96 auto position = entity->position();
97 auto boundBox = entity->metaBoundBox();
98
99 if (boundBox.isNegative() || boundBox.width() > MaximumEntityBoundBox || boundBox.height() > MaximumEntityBoundBox) {
100 throw EntityMapException::format("Entity id: {} type: {} bound box is negative or beyond the maximum entity bound box size in EntityMap::addEntity",
101 entity->entityId(), (int)entity->entityType());
102 }
103
104 auto entityId = entity->entityId();
105 if (entityId == NullEntityId)
106 throw EntityMapException::format("Null entity id in EntityMap::setEntityInfo");
107
108 auto rects = m_geometry.splitRect(boundBox, position);
109 if (!containersEqual(rects, entry.rects))
110 m_spatialMap.set(entityId, rects);
111
112 auto uniqueId = entity->uniqueId();
113 if (uniqueId) {
114 if (auto existingEntityId = m_uniqueMap.maybeRight(*uniqueId)) {
115 if (entityId != *existingEntityId)
116 throw EntityMapException::format("Duplicate entity unique id on entity ids ({}) and ({})", *existingEntityId, entityId);
117 } else {
118 m_uniqueMap.removeRight(entityId);
119 m_uniqueMap.add(*uniqueId, entityId);
120 }
121 } else {
122 m_uniqueMap.removeRight(entityId);
123 }
124 };
125
126 // Even if there is no sort order, we still copy pointers to a temporary
127 // list, so that it is safe to call addEntity from the callback.
128 m_entrySortBuffer.clear();
129 for (auto const& entry : m_spatialMap.entries())
130 m_entrySortBuffer.append(&entry.second);
131
132 if (sortOrder) {
133 m_entrySortBuffer.sort([&sortOrder](auto a, auto b) {
134 return sortOrder(a->value, b->value);
135 });
136 }
137
138 for (auto entry : m_entrySortBuffer) {
139 if (callback)
140 callback(entry->value);
141 updateEntityInfo(*entry);
142 }
143}
144
145EntityId EntityMap::uniqueEntityId(String const& uniqueId) const {
146 return m_uniqueMap.maybeRight(uniqueId).value(NullEntityId);

Callers 2

updateMethod · 0.80
updateMethod · 0.80

Calls 15

containersEqualFunction · 0.85
isNegativeMethod · 0.80
entityIdMethod · 0.80
uniqueIdMethod · 0.80
removeRightMethod · 0.80
formatFunction · 0.50
positionMethod · 0.45
metaBoundBoxMethod · 0.45
widthMethod · 0.45
heightMethod · 0.45
entityTypeMethod · 0.45
splitRectMethod · 0.45

Tested by

no test coverage detected