Merge pull request #1817 from ZappoMan/more_scripting_work

More scripting work
This commit is contained in:
AndrewMeadows 2014-01-31 17:54:44 -08:00
commit 867b66e73e
7 changed files with 55 additions and 42 deletions

View file

@ -32,10 +32,11 @@ void ParticleTreeRenderer::init() {
void ParticleTreeRenderer::update() {
if (_tree) {
ParticleTree* tree = (ParticleTree*)_tree;
_tree->lockForWrite();
tree->update();
_tree->unlock();
ParticleTree* tree = static_cast<ParticleTree*>(_tree);
if (tree->tryLockForWrite()) {
tree->update();
tree->unlock();
}
}
}

View file

@ -240,9 +240,9 @@ public:
// Octree does not currently handle its own locking, caller must use these to lock/unlock
void lockForRead() { lock.lockForRead(); }
void tryLockForRead() { lock.tryLockForRead(); }
bool tryLockForRead() { return lock.tryLockForRead(); }
void lockForWrite() { lock.lockForWrite(); }
void tryLockForWrite() { lock.tryLockForWrite(); }
bool tryLockForWrite() { return lock.tryLockForWrite(); }
void unlock() { lock.unlock(); }
unsigned long getOctreeElementsCount();

View file

@ -60,8 +60,6 @@ void Particle::handleAddParticleResponse(const QByteArray& packet) {
memcpy(&particleID, dataAt, sizeof(particleID));
dataAt += sizeof(particleID);
//qDebug() << "handleAddParticleResponse()... particleID=" << particleID << " creatorTokenID=" << creatorTokenID;
// add our token to id mapping
_tokenIDsToIDs[creatorTokenID] = particleID;
}
@ -361,8 +359,6 @@ Particle Particle::fromEditPacket(const unsigned char* data, int length, int& pr
dataAt += sizeof(editID);
processedBytes += sizeof(editID);
//qDebug() << "editID:" << editID;
bool isNewParticle = (editID == NEW_PARTICLE);
// special case for handling "new" particles
@ -411,7 +407,6 @@ Particle Particle::fromEditPacket(const unsigned char* data, int length, int& pr
memcpy(&packetContainsBits, dataAt, sizeof(packetContainsBits));
dataAt += sizeof(packetContainsBits);
processedBytes += sizeof(packetContainsBits);
//qDebug() << "packetContainsBits:" << packetContainsBits;
}
@ -528,7 +523,6 @@ Particle Particle::fromEditPacket(const unsigned char* data, int length, int& pr
if (wantDebugging) {
qDebug("Particle::fromEditPacket()...");
qDebug() << " Particle id in packet:" << editID;
//qDebug() << " position: " << newParticle._position;
newParticle.debugDump();
}
@ -735,8 +729,6 @@ bool Particle::encodeParticleEditMessageDetails(PacketType command, ParticleID i
// cleanup
delete[] octcode;
//qDebug() << "encoding... sizeOut:" << sizeOut;
return success;
}
@ -824,12 +816,8 @@ void Particle::update(const quint64& now) {
_lastUpdated = now;
// calculate our default shouldDie state... then allow script to change it if it wants...
float speed = glm::length(_velocity);
bool isStopped = (speed < MIN_VALID_SPEED);
const quint64 REALLY_OLD = 30 * USECS_PER_SECOND; // 30 seconds
bool isReallyOld = ((now - _created) > REALLY_OLD);
bool isInHand = getInHand();
bool shouldDie = (getAge() > getLifetime()) || getShouldDie() || (!isInHand && isStopped && isReallyOld);
bool shouldDie = (getAge() > getLifetime()) || getShouldDie();
setShouldDie(shouldDie);
executeUpdateScripts(); // allow the javascript to alter our state
@ -1068,7 +1056,7 @@ QScriptValue ParticleProperties::copyToScriptValue(QScriptEngine* engine) const
if (_idSet) {
properties.setProperty("id", _id);
properties.setProperty("isKnownID", (_id == UNKNOWN_PARTICLE_ID));
properties.setProperty("isKnownID", (_id != UNKNOWN_PARTICLE_ID));
}
return properties;

View file

@ -56,9 +56,10 @@ bool ParticleCollisionSystem::updateOperation(OctreeElement* element, void* extr
void ParticleCollisionSystem::update() {
// update all particles
_particles->lockForWrite();
_particles->recurseTreeWithOperation(updateOperation, this);
_particles->unlock();
if (_particles->tryLockForWrite()) {
_particles->recurseTreeWithOperation(updateOperation, this);
_particles->unlock();
}
}
@ -68,12 +69,12 @@ void ParticleCollisionSystem::checkParticle(Particle* particle) {
updateCollisionWithAvatars(particle);
}
void ParticleCollisionSystem::emitGlobalParicleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails) {
void ParticleCollisionSystem::emitGlobalParticleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails) {
ParticleID particleID = particle->getParticleID();
emit particleCollisionWithVoxel(particleID, *voxelDetails);
}
void ParticleCollisionSystem::emitGlobalParicleCollisionWithParticle(Particle* particleA, Particle* particleB) {
void ParticleCollisionSystem::emitGlobalParticleCollisionWithParticle(Particle* particleA, Particle* particleB) {
ParticleID idA = particleA->getParticleID();
ParticleID idB = particleB->getParticleID();
emit particleCollisionWithParticle(idA, idB);
@ -95,7 +96,7 @@ void ParticleCollisionSystem::updateCollisionWithVoxels(Particle* particle) {
particle->collisionWithVoxel(voxelDetails);
// let the global script run their collision scripts for particles if they have them
emitGlobalParicleCollisionWithVoxel(particle, voxelDetails);
emitGlobalParticleCollisionWithVoxel(particle, voxelDetails);
updateCollisionSound(particle, collisionInfo._penetration, COLLISION_FREQUENCY);
collisionInfo._penetration /= (float)(TREE_SCALE);
@ -124,7 +125,7 @@ void ParticleCollisionSystem::updateCollisionWithParticles(Particle* particleA)
if (glm::dot(relativeVelocity, penetration) > 0.0f) {
particleA->collisionWithParticle(particleB);
particleB->collisionWithParticle(particleA);
emitGlobalParicleCollisionWithParticle(particleA, particleB);
emitGlobalParticleCollisionWithParticle(particleA, particleB);
glm::vec3 axis = glm::normalize(penetration);
glm::vec3 axialVelocity = glm::dot(relativeVelocity, axis) * axis;

View file

@ -58,8 +58,8 @@ signals:
private:
static bool updateOperation(OctreeElement* element, void* extraData);
void emitGlobalParicleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails);
void emitGlobalParicleCollisionWithParticle(Particle* particleA, Particle* particleB);
void emitGlobalParticleCollisionWithVoxel(Particle* particle, VoxelDetail* voxelDetails);
void emitGlobalParticleCollisionWithParticle(Particle* particleA, Particle* particleB);
ParticleEditPacketSender* _packetSender;
ParticleTree* _particles;

View file

@ -43,6 +43,7 @@ ParticleID ParticlesScriptingInterface::addParticle(const ParticleProperties& pr
ParticleID ParticlesScriptingInterface::identifyParticle(ParticleID particleID) {
uint32_t actualID = particleID.id;
if (!particleID.isKnownID) {
actualID = Particle::getIDfromCreatorTokenID(particleID.creatorTokenID);
if (actualID == UNKNOWN_PARTICLE_ID) {
@ -65,8 +66,12 @@ ParticleProperties ParticlesScriptingInterface::getParticleProperties(ParticleID
}
if (_particleTree) {
_particleTree->lockForRead();
const Particle* particle = _particleTree->findParticleByID(identity.id);
results.copyFromParticle(*particle);
const Particle* particle = _particleTree->findParticleByID(identity.id, true);
if (particle) {
results.copyFromParticle(*particle);
} else {
results.setIsUnknownID();
}
_particleTree->unlock();
}
@ -113,22 +118,19 @@ void ParticlesScriptingInterface::deleteParticle(ParticleID particleID) {
properties.setShouldDie(true);
uint32_t actualID = particleID.id;
// if the particle is unknown, attempt to look it up
if (!particleID.isKnownID) {
actualID = Particle::getIDfromCreatorTokenID(particleID.creatorTokenID);
// hmmm... we kind of want to bail if someone attempts to edit an unknown
if (actualID == UNKNOWN_PARTICLE_ID) {
//qDebug() << "ParticlesScriptingInterface::deleteParticle(), bailing - unknown particle...";
return; // bailing early
}
}
particleID.id = actualID;
particleID.isKnownID = true;
// if at this point, we know the id, send the update to the particle server
if (actualID != UNKNOWN_PARTICLE_ID) {
particleID.id = actualID;
particleID.isKnownID = true;
queueParticleMessage(PacketTypeParticleAddOrEdit, particleID, properties);
}
//qDebug() << "ParticlesScriptingInterface::deleteParticle(), queueParticleMessage......";
queueParticleMessage(PacketTypeParticleAddOrEdit, particleID, properties);
// If we have a local particle tree set, then also update it.
if (_particleTree) {
_particleTree->lockForWrite();

View file

@ -260,6 +260,27 @@ void ScriptEngine::run() {
}
}
emit scriptEnding();
if (_voxelsScriptingInterface.getVoxelPacketSender()->serversExist()) {
// release the queue of edit voxel messages.
_voxelsScriptingInterface.getVoxelPacketSender()->releaseQueuedMessages();
// since we're in non-threaded mode, call process so that the packets are sent
if (!_voxelsScriptingInterface.getVoxelPacketSender()->isThreaded()) {
_voxelsScriptingInterface.getVoxelPacketSender()->process();
}
}
if (_particlesScriptingInterface.getParticlePacketSender()->serversExist()) {
// release the queue of edit voxel messages.
_particlesScriptingInterface.getParticlePacketSender()->releaseQueuedMessages();
// since we're in non-threaded mode, call process so that the packets are sent
if (!_particlesScriptingInterface.getParticlePacketSender()->isThreaded()) {
_particlesScriptingInterface.getParticlePacketSender()->process();
}
}
cleanMenuItems();
// If we were on a thread, then wait till it's done