added edited times to particles and don't change local tree if particle has not been edited

This commit is contained in:
ZappoMan 2013-12-17 12:39:35 -08:00
parent a2f1b3e325
commit 11a5ff2eb8
6 changed files with 61 additions and 11 deletions

View file

@ -43,6 +43,7 @@ void Particle::init(glm::vec3 position, float radius, rgbColor color, glm::vec3
_id = id;
}
_lastUpdated = usecTimestampNow();
_lastEdited = _lastUpdated;
_position = position;
_radius = radius;
@ -67,6 +68,9 @@ bool Particle::appendParticleData(OctreePacketData* packetData) const {
if (success) {
success = packetData->appendValue(getLastUpdated());
}
if (success) {
success = packetData->appendValue(getLastEdited());
}
if (success) {
success = packetData->appendValue(getRadius());
}
@ -99,7 +103,7 @@ bool Particle::appendParticleData(OctreePacketData* packetData) const {
}
int Particle::expectedBytes() {
int expectedBytes = sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint64_t) + sizeof(float) +
int expectedBytes = sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint64_t) + sizeof(uint64_t) + sizeof(float) +
sizeof(glm::vec3) + sizeof(rgbColor) + sizeof(glm::vec3) +
sizeof(glm::vec3) + sizeof(float) + sizeof(bool);
return expectedBytes;
@ -125,6 +129,11 @@ int Particle::readParticleDataFromBuffer(const unsigned char* data, int bytesLef
dataAt += sizeof(_lastUpdated);
bytesRead += sizeof(_lastUpdated);
// _lastEdited
memcpy(&_lastEdited, dataAt, sizeof(_lastEdited));
dataAt += sizeof(_lastEdited);
bytesRead += sizeof(_lastEdited);
// radius
memcpy(&_radius, dataAt, sizeof(_radius));
dataAt += sizeof(_radius);
@ -219,6 +228,11 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe
memcpy(&newParticle._lastUpdated, dataAt, sizeof(newParticle._lastUpdated));
dataAt += sizeof(newParticle._lastUpdated);
processedBytes += sizeof(newParticle._lastUpdated);
// lastEdited
memcpy(&newParticle._lastEdited, dataAt, sizeof(newParticle._lastEdited));
dataAt += sizeof(newParticle._lastEdited);
processedBytes += sizeof(newParticle._lastEdited);
// radius
memcpy(&newParticle._radius, dataAt, sizeof(newParticle._radius));
@ -279,6 +293,7 @@ void Particle::debugDump() const {
printf("Particle id :%u\n", _id);
printf(" created:%llu\n", _created);
printf(" last updated:%llu\n", _lastUpdated);
printf(" last edited:%llu\n", _lastEdited);
printf(" position:%f,%f,%f\n", _position.x, _position.y, _position.z);
printf(" velocity:%f,%f,%f\n", _velocity.x, _velocity.y, _velocity.z);
printf(" gravity:%f,%f,%f\n", _gravity.x, _gravity.y, _gravity.z);
@ -337,6 +352,11 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, int count,
copyAt += sizeof(details[i].lastUpdated);
sizeOut += sizeof(details[i].lastUpdated);
// lastEdited
memcpy(copyAt, &details[i].lastEdited, sizeof(details[i].lastEdited));
copyAt += sizeof(details[i].lastEdited);
sizeOut += sizeof(details[i].lastEdited);
// radius
memcpy(copyAt, &details[i].radius, sizeof(details[i].radius));
copyAt += sizeof(details[i].radius);

View file

@ -26,6 +26,7 @@ class ParticleDetail {
public:
uint32_t id;
uint64_t lastUpdated;
uint64_t lastEdited;
glm::vec3 position;
float radius;
rgbColor color;
@ -70,6 +71,7 @@ public:
uint64_t getCreated() const { return _created; }
uint64_t getLifetime() const { return usecTimestampNow() - _created; }
uint64_t getLastUpdated() const { return _lastUpdated; }
uint64_t getLastEdited() const { return _lastEdited; }
uint32_t getID() const { return _id; }
bool getShouldDie() const { return _shouldDie; }
QString getUpdateScript() const { return _updateScript; }
@ -92,7 +94,7 @@ public:
void setUpdateScript(QString updateScript) { _updateScript = updateScript; }
void setCreatorTokenID(uint32_t creatorTokenID) { _creatorTokenID = creatorTokenID; }
void setCreated(uint64_t created) { _created = created; }
bool appendParticleData(OctreePacketData* packetData) const;
int readParticleDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args);
static int expectedBytes();
@ -116,6 +118,7 @@ protected:
glm::vec3 _velocity;
uint64_t _lastUpdated;
uint64_t _created;
uint64_t _lastEdited;
uint32_t _id;
static uint32_t _nextID;
bool _shouldDie;

View file

@ -44,7 +44,8 @@ void ParticleEditHandle::createParticle(glm::vec3 position, float radius, xColor
glm::vec3 gravity, float damping, bool inHand, QString updateScript) {
// setup a ParticleDetail struct with the data
ParticleDetail addParticleDetail = { NEW_PARTICLE, usecTimestampNow(),
uint64_t now = usecTimestampNow();
ParticleDetail addParticleDetail = { NEW_PARTICLE, now, now,
position, radius, {color.red, color.green, color.blue },
velocity, gravity, damping, inHand, updateScript, _creatorTokenID };
@ -69,7 +70,8 @@ bool ParticleEditHandle::updateParticle(glm::vec3 position, float radius, xColor
}
// setup a ParticleDetail struct with the data
ParticleDetail newParticleDetail = { _id, usecTimestampNow(),
uint64_t now = usecTimestampNow();
ParticleDetail newParticleDetail = { _id, now, now,
position, radius, {color.red, color.green, color.blue },
velocity, gravity, damping, inHand, updateScript, _creatorTokenID };

View file

@ -22,7 +22,8 @@ unsigned int ParticleScriptingInterface::queueParticleAdd(glm::vec3 position, fl
_nextCreatorTokenID++;
// setup a ParticleDetail struct with the data
ParticleDetail addParticleDetail = { NEW_PARTICLE, usecTimestampNow(),
uint64_t now = usecTimestampNow();
ParticleDetail addParticleDetail = { NEW_PARTICLE, now, now,
position, radius, {color.red, color.green, color.blue }, velocity,
gravity, damping, inHand, updateScript, creatorTokenID };

View file

@ -115,15 +115,39 @@ bool ParticleTreeElement::containsParticle(const Particle& particle) const {
}
bool ParticleTreeElement::updateParticle(const Particle& particle) {
bool wantDebug = false;
uint16_t numberOfParticles = _particles.size();
for (uint16_t i = 0; i < numberOfParticles; i++) {
if (_particles[i].getID() == particle.getID()) {
uint64_t actuallyCreated = particle.getCreated();
if (!particle.isNewlyCreated()) {
actuallyCreated = _particles[i].getCreated();
int difference = _particles[i].getLastUpdated() - particle.getLastUpdated();
bool changedOnServer = _particles[i].getLastEdited() < particle.getLastEdited();
bool localOlder = _particles[i].getLastUpdated() < particle.getLastUpdated();
if (changedOnServer || localOlder) {
if (wantDebug) {
printf("local particle [id:%d] %s and %s than server particle by %d, particle.isNewlyCreated()=%s\n",
particle.getID(), (changedOnServer ? "CHANGED" : "same"),
(localOlder ? "OLDER" : "NEWER"),
difference, debug::valueOf(particle.isNewlyCreated()) );
}
uint64_t actuallyCreated = particle.getCreated();
if (!particle.isNewlyCreated()) {
actuallyCreated = _particles[i].getCreated();
}
_particles[i] = particle;
_particles[i].setCreated(actuallyCreated);
} else {
if (wantDebug) {
printf(">>> NO CHANGE <<< -- local particle [id:%d] %s and %s than server particle by %d, "
"particle.isNewlyCreated()=%s\n",
particle.getID(), (changedOnServer ? "CHANGED" : "same"),
(localOlder ? "OLDER" : "NEWER"),
difference, debug::valueOf(particle.isNewlyCreated()) );
}
}
_particles[i] = particle;
_particles[i].setCreated(actuallyCreated);
return true;
}
}

View file

@ -54,7 +54,7 @@ PACKET_VERSION versionForPacketType(PACKET_TYPE type) {
return 2;
case PACKET_TYPE_PARTICLE_DATA:
return 2;
return 3;
default:
return 0;