Showing the changes to libraries/entities/src/EntityScriptingInterface.cpp (1 of 104 files). Show all
Unified · Split
| @@ -20,7 +20,6 @@ · 19 unmodified lines | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | EntityScriptingInterface::EntityScriptingInterface() : | 22 | EntityScriptingInterface::EntityScriptingInterface() : |
| 23 | _nextCreatorTokenID(0), | ||
| 24 | _entityTree(NULL) | 23 | _entityTree(NULL) |
| 25 | { | 24 | { |
| 26 | auto nodeList = DependencyManager::get<NodeList>(); | 25 | auto nodeList = DependencyManager::get<NodeList>(); |
| @@ -29,7 +28,7 @@ EntityScriptingInterface::EntityScriptingInterface() : · 2 unmodified lines | |||
| 29 | } | 28 | } |
| 30 | 29 | ||
| 31 | void EntityScriptingInterface::queueEntityMessage(PacketType packetType, | 30 | void EntityScriptingInterface::queueEntityMessage(PacketType packetType, |
| 32 | EntityItemID entityID, const EntityItemProperties& properties) { | 31 | QUuid entityID, const EntityItemProperties& properties) { |
| 33 | getEntityPacketSender()->queueEditEntityMessage(packetType, entityID, properties); | 32 | getEntityPacketSender()->queueEditEntityMessage(packetType, entityID, properties); |
| 34 | } | 33 | } |
| 35 | 34 | ||
| @@ -71,14 +70,10 @@ void bidForSimulationOwnership(EntityItemProperties& properties) { · 35 unmodified lines | |||
| 71 | 70 | ||
| 72 | 71 | ||
| 73 | 72 | ||
| 74 | EntityItemID EntityScriptingInterface::addEntity(const EntityItemProperties& properties) { | 73 | QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties) { |
| 75 | |||
| 76 | // The application will keep track of creatorTokenID | ||
| 77 | uint32_t creatorTokenID = EntityItemID::getNextCreatorTokenID(); | ||
| 78 | 74 | ||
| 79 | EntityItemProperties propertiesWithSimID = properties; | 75 | EntityItemProperties propertiesWithSimID = properties; |
| 80 | 76 | QUuid id = QUuid::createUuid(); | |
| 81 | EntityItemID id(NEW_ENTITY, creatorTokenID, false); | ||
| 82 | 77 | ||
| 83 | // If we have a local entity tree set, then also update it. | 78 | // If we have a local entity tree set, then also update it. |
| 84 | bool success = true; | 79 | bool success = true; |
| @@ -104,25 +99,8 @@ EntityItemID EntityScriptingInterface::addEntity(const EntityItemProperties& pro · 19 unmodified lines | |||
| 104 | return id; | 99 | return id; |
| 105 | } | 100 | } |
| 106 | 101 | ||
| 107 | EntityItemID EntityScriptingInterface::identifyEntity(EntityItemID entityID) { | 102 | EntityItemProperties EntityScriptingInterface::getEntityProperties(QUuid identity) { |
| 108 | EntityItemID actualID = entityID; | ||
| 109 | |||
| 110 | if (!entityID.isKnownID) { | ||
| 111 | actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID); | ||
| 112 | if (actualID == UNKNOWN_ENTITY_ID) { | ||
| 113 | return entityID; // bailing early | ||
| 114 | } | ||
| 115 | |||
| 116 | // found it! | ||
| 117 | entityID.id = actualID.id; | ||
| 118 | entityID.isKnownID = true; | ||
| 119 | } | ||
| 120 | return entityID; | ||
| 121 | } | ||
| 122 | |||
| 123 | EntityItemProperties EntityScriptingInterface::getEntityProperties(EntityItemID entityID) { | ||
| 124 | EntityItemProperties results; | 103 | EntityItemProperties results; |
| 125 | EntityItemID identity = identifyEntity(entityID); | ||
| 126 | if (_entityTree) { | 104 | if (_entityTree) { |
| 127 | _entityTree->lockForRead(); | 105 | _entityTree->lockForRead(); |
| 128 | EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(identity)); | 106 | EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(identity)); |
| @@ -142,8 +120,6 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(EntityItemID · 13 unmodified lines | |||
| 142 | } | 120 | } |
| 143 | } | 121 | } |
| 144 | 122 | ||
| 145 | } else { | ||
| 146 | results.setIsUnknownID(); | ||
| 147 | } | 123 | } |
| 148 | _entityTree->unlock(); | 124 | _entityTree->unlock(); |
| 149 | } | 125 | } |
| @@ -151,59 +127,33 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(EntityItemID · 1 unmodified line | |||
| 151 | return results; | 127 | return results; |
| 152 | } | 128 | } |
| 153 | 129 | ||
| 154 | EntityItemID EntityScriptingInterface::editEntity(EntityItemID entityID, const EntityItemProperties& properties) { | 130 | QUuid EntityScriptingInterface::editEntity(QUuid entityID, const EntityItemProperties& properties) { |
| 155 | EntityItemID actualID = entityID; | 131 | // If we have a local entity tree set, then also update it. |
| 156 | // if the entity is unknown, attempt to look it up | ||
| 157 | if (!entityID.isKnownID) { | ||
| 158 | actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID); | ||
| 159 | if (actualID.id != UNKNOWN_ENTITY_ID) { | ||
| 160 | entityID.id = actualID.id; | ||
| 161 | entityID.isKnownID = true; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | // If we have a local entity tree set, then also update it. We can do this even if we don't know | ||
| 166 | // the actual id, because we can edit out local entities just with creatorTokenID | ||
| 167 | if (_entityTree) { | 132 | if (_entityTree) { |
| 168 | _entityTree->lockForWrite(); | 133 | _entityTree->lockForWrite(); |
| 169 | _entityTree->updateEntity(entityID, properties); | 134 | _entityTree->updateEntity(entityID, properties); |
| 170 | _entityTree->unlock(); | 135 | _entityTree->unlock(); |
| 171 | } | 136 | } |
| 172 | 137 | ||
| 173 | // if at this point, we know the id, send the update to the entity server | 138 | // make sure the properties has a type, so that the encode can know which properties to include |
| 174 | if (entityID.isKnownID) { | 139 | if (properties.getType() == EntityTypes::Unknown) { |
| 175 | // make sure the properties has a type, so that the encode can know which properties to include | 140 | EntityItem* entity = _entityTree->findEntityByEntityItemID(entityID); |
| 176 | if (properties.getType() == EntityTypes::Unknown) { | 141 | if (entity) { |
| 177 | EntityItem* entity = _entityTree->findEntityByEntityItemID(entityID); | 142 | // we need to change the outgoing properties, so we make a copy, modify, and send. |
| 178 | if (entity) { | 143 | EntityItemProperties modifiedProperties = properties; |
| 179 | // we need to change the outgoing properties, so we make a copy, modify, and send. | 144 | entity->setLastBroadcast(usecTimestampNow()); |
| 180 | EntityItemProperties modifiedProperties = properties; | 145 | modifiedProperties.setType(entity->getType()); |
| 181 | entity->setLastBroadcast(usecTimestampNow()); | 146 | bidForSimulationOwnership(modifiedProperties); |
| 182 | modifiedProperties.setType(entity->getType()); | 147 | queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, modifiedProperties); |
| 183 | bidForSimulationOwnership(modifiedProperties); | 148 | return entityID; |
| 184 | queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, modifiedProperties); | ||
| 185 | return entityID; | ||
| 186 | } | ||
| 187 | } | 149 | } |
| 188 | |||
| 189 | queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, properties); | ||
| 190 | } | 150 | } |
| 191 | 151 | ||
| 152 | queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, properties); | ||
| 192 | return entityID; | 153 | return entityID; |
| 193 | } | 154 | } |
| 194 | 155 | ||
| 195 | void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { | 156 | void EntityScriptingInterface::deleteEntity(QUuid entityID) { |
| 196 | |||
| 197 | EntityItemID actualID = entityID; | ||
| 198 | |||
| 199 | // if the entity is unknown, attempt to look it up | ||
| 200 | if (!entityID.isKnownID) { | ||
| 201 | actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID); | ||
| 202 | if (actualID.id != UNKNOWN_ENTITY_ID) { | ||
| 203 | entityID.id = actualID.id; | ||
| 204 | entityID.isKnownID = true; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | 157 | ||
| 208 | bool shouldDelete = true; | 158 | bool shouldDelete = true; |
| 209 | 159 | ||
| @@ -211,7 +161,7 @@ void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { · 1 unmodified line | |||
| 211 | if (_entityTree) { | 161 | if (_entityTree) { |
| 212 | _entityTree->lockForWrite(); | 162 | _entityTree->lockForWrite(); |
| 213 | 163 | ||
| 214 | EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(actualID)); | 164 | EntityItem* entity = const_cast<EntityItem*>(_entityTree->findEntityByEntityItemID(entityID)); |
| 215 | if (entity) { | 165 | if (entity) { |
| 216 | if (entity->getLocked()) { | 166 | if (entity->getLocked()) { |
| 217 | shouldDelete = false; | 167 | shouldDelete = false; |
| @@ -224,19 +174,19 @@ void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { · 6 unmodified lines | |||
| 224 | } | 174 | } |
| 225 | 175 | ||
| 226 | // if at this point, we know the id, and we should still delete the entity, send the update to the entity server | 176 | // if at this point, we know the id, and we should still delete the entity, send the update to the entity server |
| 227 | if (shouldDelete && entityID.isKnownID) { | 177 | if (shouldDelete) { |
| 228 | getEntityPacketSender()->queueEraseEntityMessage(entityID); | 178 | getEntityPacketSender()->queueEraseEntityMessage(entityID); |
| 229 | } | 179 | } |
| 230 | } | 180 | } |
| 231 | 181 | ||
| 232 | EntityItemID EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const { | 182 | QUuid EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const { |
| 233 | EntityItemID result(UNKNOWN_ENTITY_ID, UNKNOWN_ENTITY_TOKEN, false); | 183 | QUuid result; |
| 234 | if (_entityTree) { | 184 | if (_entityTree) { |
| 235 | _entityTree->lockForRead(); | 185 | _entityTree->lockForRead(); |
| 236 | const EntityItem* closestEntity = _entityTree->findClosestEntity(center, radius); | 186 | const EntityItem* closestEntity = _entityTree->findClosestEntity(center, radius); |
| 237 | _entityTree->unlock(); | 187 | _entityTree->unlock(); |
| 238 | if (closestEntity) { | 188 | if (closestEntity) { |
| 239 | result = closestEntity->getEntityItemID(); | 189 | result = closestEntity->getID(); |
| 240 | } | 190 | } |
| 241 | } | 191 | } |
| 242 | return result; | 192 | return result; |
| @@ -251,8 +201,8 @@ void EntityScriptingInterface::dumpTree() const { · 8 unmodified lines | |||
| 251 | } | 201 | } |
| 252 | } | 202 | } |
| 253 | 203 | ||
| 254 | QVector<EntityItemID> EntityScriptingInterface::findEntities(const glm::vec3& center, float radius) const { | 204 | QVector<QUuid> EntityScriptingInterface::findEntities(const glm::vec3& center, float radius) const { |
| 255 | QVector<EntityItemID> result; | 205 | QVector<QUuid> result; |
| 256 | if (_entityTree) { | 206 | if (_entityTree) { |
| 257 | _entityTree->lockForRead(); | 207 | _entityTree->lockForRead(); |
| 258 | QVector<const EntityItem*> entities; | 208 | QVector<const EntityItem*> entities; |
| @@ -260,14 +210,14 @@ QVector<EntityItemID> EntityScriptingInterface::findEntities(const glm::vec3& ce · 1 unmodified line | |||
| 260 | _entityTree->unlock(); | 210 | _entityTree->unlock(); |
| 261 | 211 | ||
| 262 | foreach (const EntityItem* entity, entities) { | 212 | foreach (const EntityItem* entity, entities) { |
| 263 | result << entity->getEntityItemID(); | 213 | result << entity->getID(); |
| 264 | } | 214 | } |
| 265 | } | 215 | } |
| 266 | return result; | 216 | return result; |
| 267 | } | 217 | } |
| 268 | 218 | ||
| 269 | QVector<EntityItemID> EntityScriptingInterface::findEntitiesInBox(const glm::vec3& corner, const glm::vec3& dimensions) const { | 219 | QVector<QUuid> EntityScriptingInterface::findEntitiesInBox(const glm::vec3& corner, const glm::vec3& dimensions) const { |
| 270 | QVector<EntityItemID> result; | 220 | QVector<QUuid> result; |
| 271 | if (_entityTree) { | 221 | if (_entityTree) { |
| 272 | _entityTree->lockForRead(); | 222 | _entityTree->lockForRead(); |
| 273 | AABox box(corner, dimensions); | 223 | AABox box(corner, dimensions); |
| @@ -276,7 +226,7 @@ QVector<EntityItemID> EntityScriptingInterface::findEntitiesInBox(const glm::vec · 2 unmodified lines | |||
| 276 | _entityTree->unlock(); | 226 | _entityTree->unlock(); |
| 277 | 227 | ||
| 278 | foreach (const EntityItem* entity, entities) { | 228 | foreach (const EntityItem* entity, entities) { |
| 279 | result << entity->getEntityItemID(); | 229 | result << entity->getID(); |
| 280 | } | 230 | } |
| 281 | } | 231 | } |
| 282 | return result; | 232 | return result; |
| @@ -303,7 +253,7 @@ RayToEntityIntersectionResult EntityScriptingInterface::findRayIntersectionWorke · 20 unmodified lines | |||
| 303 | (void**)&intersectedEntity, lockType, &result.accurate, | 253 | (void**)&intersectedEntity, lockType, &result.accurate, |
| 304 | precisionPicking); | 254 | precisionPicking); |
| 305 | if (result.intersects && intersectedEntity) { | 255 | if (result.intersects && intersectedEntity) { |
| 306 | result.entityID = intersectedEntity->getEntityItemID(); | 256 | result.entityID = intersectedEntity->getID(); |
| 307 | result.properties = intersectedEntity->getProperties(); | 257 | result.properties = intersectedEntity->getProperties(); |
| 308 | result.intersection = ray.origin + (ray.direction * result.distance); | 258 | result.intersection = ray.origin + (ray.direction * result.distance); |
| 309 | } | 259 | } |
| @@ -359,7 +309,7 @@ QScriptValue RayToEntityIntersectionResultToScriptValue(QScriptEngine* engine, c · 49 unmodified lines | |||
| 359 | QScriptValue obj = engine->newObject(); | 309 | QScriptValue obj = engine->newObject(); |
| 360 | obj.setProperty("intersects", value.intersects); | 310 | obj.setProperty("intersects", value.intersects); |
| 361 | obj.setProperty("accurate", value.accurate); | 311 | obj.setProperty("accurate", value.accurate); |
| 362 | QScriptValue entityItemValue = EntityItemIDtoScriptValue(engine, value.entityID); | 312 | QScriptValue entityItemValue = quuidToScriptValue(engine, value.entityID); |
| 363 | obj.setProperty("entityID", entityItemValue); | 313 | obj.setProperty("entityID", entityItemValue); |
| 364 | 314 | ||
| 365 | QScriptValue propertiesValue = EntityItemPropertiesToScriptValue(engine, value.properties); | 315 | QScriptValue propertiesValue = EntityItemPropertiesToScriptValue(engine, value.properties); |
| @@ -403,9 +353,7 @@ void RayToEntityIntersectionResultFromScriptValue(const QScriptValue& object, Ra · 37 unmodified lines | |||
| 403 | value.intersects = object.property("intersects").toVariant().toBool(); | 353 | value.intersects = object.property("intersects").toVariant().toBool(); |
| 404 | value.accurate = object.property("accurate").toVariant().toBool(); | 354 | value.accurate = object.property("accurate").toVariant().toBool(); |
| 405 | QScriptValue entityIDValue = object.property("entityID"); | 355 | QScriptValue entityIDValue = object.property("entityID"); |
| 406 | if (entityIDValue.isValid()) { | 356 | quuidFromScriptValue(entityIDValue, value.entityID); |
| 407 | EntityItemIDfromScriptValue(entityIDValue, value.entityID); | ||
| 408 | } | ||
| 409 | QScriptValue entityPropertiesValue = object.property("properties"); | 357 | QScriptValue entityPropertiesValue = object.property("properties"); |
| 410 | if (entityPropertiesValue.isValid()) { | 358 | if (entityPropertiesValue.isValid()) { |
| 411 | EntityItemPropertiesFromScriptValue(entityPropertiesValue, value.properties); | 359 | EntityItemPropertiesFromScriptValue(entityPropertiesValue, value.properties); |