From e18506c77f11a3f8e8d4a0f49d14d5d74b769db9 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 26 Jun 2015 15:30:18 -0700 Subject: [PATCH] promote volunteer priority also remove some cruft change uint8_t to be quint8 --- libraries/entities/src/EntityItem.cpp | 4 ++++ libraries/entities/src/EntityItem.h | 3 ++- .../entities/src/EntityScriptingInterface.cpp | 2 +- libraries/entities/src/EntityTree.cpp | 6 +++++- libraries/entities/src/SimulationOwner.cpp | 12 ++++++++---- libraries/entities/src/SimulationOwner.h | 15 ++++++++++++--- libraries/physics/src/EntityMotionState.cpp | 19 +++++++++---------- libraries/physics/src/EntityMotionState.h | 8 ++++---- libraries/physics/src/ObjectMotionState.h | 4 ++-- libraries/physics/src/PhysicsEngine.cpp | 4 ++-- 10 files changed, 49 insertions(+), 28 deletions(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 1cfa992526..09cdb0a776 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1392,6 +1392,10 @@ void EntityItem::setSimulationOwner(const SimulationOwner& owner) { _simulationOwner.set(owner); } +void EntityItem::promoteSimulationPriority(quint8 priority) { + _simulationOwner.promotePriority(priority); +} + void EntityItem::updateSimulatorID(const QUuid& value) { if (_simulationOwner.setID(value)) { _dirtyFlags |= EntityItem::DIRTY_SIMULATOR_ID; diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index df369b43f5..d8371e545d 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -321,8 +321,9 @@ public: const SimulationOwner& getSimulationOwner() const { return _simulationOwner; } void setSimulationOwner(const QUuid& id, quint8 priority); void setSimulationOwner(const SimulationOwner& owner); + void promoteSimulationPriority(quint8 priority); - quint8 getSimulatorPriority() const { return _simulationOwner.getPriority(); } + quint8 getSimulationPriority() const { return _simulationOwner.getPriority(); } QUuid getSimulatorID() const { return _simulationOwner.getID(); } void updateSimulatorID(const QUuid& value); void clearSimulationOwnership(); diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index de9bf09d37..8fdbe479e6 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -163,7 +163,7 @@ QUuid EntityScriptingInterface::editEntity(QUuid id, EntityItemProperties proper // we re-assert our simulation ownership properties.setSimulationOwner(myNodeID, - glm::max(entity->getSimulatorPriority(), SCRIPT_EDIT_SIMULATION_PRIORITY)); + glm::max(entity->getSimulationPriority(), SCRIPT_EDIT_SIMULATION_PRIORITY)); } else { // we make a bid for simulation ownership properties.setSimulationOwner(myNodeID, SCRIPT_EDIT_SIMULATION_PRIORITY); diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 45b5a95271..3f38e18a9e 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -169,7 +169,7 @@ bool EntityTree::updateEntityWithElement(EntityItemPointer entity, const EntityI // so we apply the rules for ownership change: // (1) higher priority wins // (2) equal priority wins if ownership filter has expired except... - uint8_t oldPriority = entity->getSimulatorPriority(); + uint8_t oldPriority = entity->getSimulationPriority(); uint8_t newPriority = properties.getSimulationOwner().getPriority(); if (newPriority > oldPriority || (newPriority == oldPriority && properties.getSimulationOwner().hasExpired())) { @@ -208,6 +208,10 @@ bool EntityTree::updateEntityWithElement(EntityItemPointer entity, const EntityI _simulation->lock(); _simulation->changeEntity(entity); _simulation->unlock(); + // always promote volunteer priority + if (entity->getSimulationPriority() == VOLUNTEER_SIMULATION_PRIORITY) { + entity->promoteSimulationPriority(RECRUIT_SIMULATION_PRIORITY); + } } } else { // normally the _simulation clears ALL updateFlags, but since there is none we do it explicitly diff --git a/libraries/entities/src/SimulationOwner.cpp b/libraries/entities/src/SimulationOwner.cpp index 88bb3fcab3..eb5f8b6ecd 100644 --- a/libraries/entities/src/SimulationOwner.cpp +++ b/libraries/entities/src/SimulationOwner.cpp @@ -48,16 +48,20 @@ void SimulationOwner::clear() { void SimulationOwner::setPriority(quint8 priority) { _priority = priority; - if (_priority == MAX_SIMULATION_PRIORITY) { - // we extend the the expiry whenever we set MAX_SIMULATION_PRIORITY - updateExpiry(); - } else if (_priority == 0) { + if (_priority == 0) { // when priority is zero we clear everything _expiry = 0; _id = QUuid(); } } +void SimulationOwner::promotePriority(quint8 priority) { + if (priority > _priority) { + _priority = priority; + updateExpiry(); + } +} + bool SimulationOwner::setID(const QUuid& id) { if (_id != id) { _id = id; diff --git a/libraries/entities/src/SimulationOwner.h b/libraries/entities/src/SimulationOwner.h index 46d621b566..325be54e62 100644 --- a/libraries/entities/src/SimulationOwner.h +++ b/libraries/entities/src/SimulationOwner.h @@ -18,11 +18,19 @@ #include #include +// Simulation observers will bid to simulate unowned active objects at the lowest possible priority +// which is VOLUNTEER. If the server accepts a VOLUNTEER bid it will automatically bump it +// to RECRUIT priority so that other volunteers don't accidentally take over. const quint8 VOLUNTEER_SIMULATION_PRIORITY = 0x01; -const quint8 PERSONAL_SIMULATION_PRIORITY = 0x7f; +const quint8 RECRUIT_SIMULATION_PRIORITY = VOLUNTEER_SIMULATION_PRIORITY + 1; + +// When poking objects with scripts an observer will bid at SCRIPT_EDIT priority. const quint8 SCRIPT_EDIT_SIMULATION_PRIORITY = 0x80; -const quint8 MAX_SIMULATION_PRIORITY = 0xff; -const quint8 ATTACHMENT_SIMULATION_PRIORITY = MAX_SIMULATION_PRIORITY; + +// PERSONAL priority (needs a better name) is the level at which a simulation observer will bid for +// objects that collide its MyAvatar. +const quint8 PERSONAL_SIMULATION_PRIORITY = SCRIPT_EDIT_SIMULATION_PRIORITY - 1; + class SimulationOwner { public: @@ -42,6 +50,7 @@ public: void clear(); void setPriority(quint8 priority); + void promotePriority(quint8 priority); // return true if id is changed bool setID(const QUuid& id); diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index f7f6c629a9..884eccb609 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -111,7 +111,7 @@ void EntityMotionState::handleEasyChanges(uint32_t flags, PhysicsEngine* engine) _outgoingPriority = 0; } else { _nextOwnershipBid = usecTimestampNow() + USECS_BETWEEN_OWNERSHIP_BIDS; - if (engine->getSessionID() == _entity->getSimulatorID() || _entity->getSimulatorPriority() > _outgoingPriority) { + if (engine->getSessionID() == _entity->getSimulatorID() || _entity->getSimulationPriority() > _outgoingPriority) { // we own the simulation or our priority looses to remote _outgoingPriority = 0; } @@ -351,7 +351,7 @@ bool EntityMotionState::shouldSendUpdate(uint32_t simulationStep, const QUuid& s if (_entity->getSimulatorID() != sessionID) { // we don't own the simulation, but maybe we should... if (_outgoingPriority > 0) { - if (_outgoingPriority < _entity->getSimulatorPriority()) { + if (_outgoingPriority < _entity->getSimulationPriority()) { // our priority looses to remote, so we don't bother to bid _outgoingPriority = 0; return false; @@ -453,7 +453,7 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, const Q // else the ownership is not changing so we don't bother to pack it } else { // we don't own the simulation for this entity yet, but we're sending a bid for it - properties.setSimulationOwner(sessionID, glm::max(_outgoingPriority, VOLUNTEER_SIMULATION_PRIORITY)); + properties.setSimulationOwner(sessionID, glm::max(_outgoingPriority, VOLUNTEER_SIMULATION_PRIORITY)); _nextOwnershipBid = now + USECS_BETWEEN_OWNERSHIP_BIDS; } @@ -493,9 +493,9 @@ uint32_t EntityMotionState::getAndClearIncomingDirtyFlags() { } // virtual -uint8_t EntityMotionState::getSimulatorPriority() const { +quint8 EntityMotionState::getSimulationPriority() const { if (_entity) { - return _entity->getSimulatorPriority(); + return _entity->getSimulationPriority(); } return 0; } @@ -510,10 +510,9 @@ QUuid EntityMotionState::getSimulatorID() const { } // virtual -void EntityMotionState::bump(uint8_t priority) { +void EntityMotionState::bump(quint8 priority) { if (_entity) { - //uint8_t inheritedPriority = priority < 2 ? 1 : priority - 1; - uint8_t inheritedPriority = VOLUNTEER_SIMULATION_PRIORITY; + quint8 inheritedPriority = VOLUNTEER_SIMULATION_PRIORITY; setOutgoingPriority(inheritedPriority); } } @@ -586,6 +585,6 @@ int16_t EntityMotionState::computeCollisionGroup() { return COLLISION_GROUP_DEFAULT; } -void EntityMotionState::setOutgoingPriority(uint8_t priority) { - _outgoingPriority = glm::max(_outgoingPriority, priority); +void EntityMotionState::setOutgoingPriority(quint8 priority) { + _outgoingPriority = glm::max(_outgoingPriority, priority); } diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index 6d08343286..9f7bafd416 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -68,9 +68,9 @@ public: virtual const QUuid& getObjectID() const { return _entity->getID(); } - virtual uint8_t getSimulatorPriority() const; + virtual quint8 getSimulationPriority() const; virtual QUuid getSimulatorID() const; - virtual void bump(uint8_t priority); + virtual void bump(quint8 priority); EntityItemPointer getEntity() const { return _entity; } @@ -82,7 +82,7 @@ public: virtual int16_t computeCollisionGroup(); // eternal logic can suggest a simuator priority bid for the next outgoing update - void setOutgoingPriority(uint8_t priority); + void setOutgoingPriority(quint8 priority); friend class PhysicalEntitySimulation; @@ -116,7 +116,7 @@ protected: quint8 _accelerationNearlyGravityCount; quint64 _nextOwnershipBid = 0; uint32_t _loopsWithoutOwner; - uint8_t _outgoingPriority = 0; + quint8 _outgoingPriority = 0; }; #endif // hifi_EntityMotionState_h diff --git a/libraries/physics/src/ObjectMotionState.h b/libraries/physics/src/ObjectMotionState.h index 1ef3ac0dcd..30394ef5fc 100644 --- a/libraries/physics/src/ObjectMotionState.h +++ b/libraries/physics/src/ObjectMotionState.h @@ -119,9 +119,9 @@ public: virtual const QUuid& getObjectID() const = 0; - virtual uint8_t getSimulatorPriority() const { return 0; } + virtual quint8 getSimulationPriority() const { return 0; } virtual QUuid getSimulatorID() const = 0; - virtual void bump(uint8_t priority) {} + virtual void bump(quint8 priority) {} virtual QString getName() { return ""; } diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 2c7c39f222..5c586eb09d 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -270,14 +270,14 @@ void PhysicsEngine::doOwnershipInfection(const btCollisionObject* objectA, const // NOTE: we might own the simulation of a kinematic object (A) // but we don't claim ownership of kinematic objects (B) based on collisions here. if (!objectB->isStaticOrKinematicObject() && b->getSimulatorID() != _sessionID) { - quint8 priority = a ? a->getSimulatorPriority() : PERSONAL_SIMULATION_PRIORITY; + quint8 priority = a ? a->getSimulationPriority() : PERSONAL_SIMULATION_PRIORITY; b->bump(priority); } } else if (a && ((b && b->getSimulatorID() == _sessionID && !objectB->isStaticObject()) || (objectB == characterObject))) { // SIMILARLY: we might own the simulation of a kinematic object (B) // but we don't claim ownership of kinematic objects (A) based on collisions here. if (!objectA->isStaticOrKinematicObject() && a->getSimulatorID() != _sessionID) { - quint8 priority = b ? b->getSimulatorPriority() : PERSONAL_SIMULATION_PRIORITY; + quint8 priority = b ? b->getSimulationPriority() : PERSONAL_SIMULATION_PRIORITY; a->bump(priority); } }