Further whipping of dynamic clusters into shape.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2323 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-03-25 19:28:59 +00:00
parent b66e0d2584
commit be1f2eb1fc
3 changed files with 48 additions and 22 deletions
@@ -1,5 +1,5 @@
// //
// $Id: SpotSceneDirector.java,v 1.21 2003/02/12 07:23:31 mdb Exp $ // $Id: SpotSceneDirector.java,v 1.22 2003/03/25 19:28:58 mdb Exp $
package com.threerings.whirled.spot.client; package com.threerings.whirled.spot.client;
@@ -177,7 +177,7 @@ public class SpotSceneDirector extends BasicDirector
{ {
// refuse if there's a pending location change or if we're already // refuse if there's a pending location change or if we're already
// at the specified location // at the specified location
if (loc.equals(_location) || (_pendingLoc != null)) { if (loc.equivalent(_location) || (_pendingLoc != null)) {
Log.info("Not going to " + loc + "; we're at " + _location + Log.info("Not going to " + loc + "; we're at " + _location +
" and we're headed to " + _pendingLoc + "."); " and we're headed to " + _pendingLoc + ".");
return; return;
@@ -1,5 +1,5 @@
// //
// $Id: Location.java,v 1.6 2003/02/12 07:23:31 mdb Exp $ // $Id: Location.java,v 1.7 2003/03/25 19:28:58 mdb Exp $
package com.threerings.whirled.spot.data; package com.threerings.whirled.spot.data;
@@ -75,6 +75,15 @@ public class Location extends SimpleStreamableObject
} }
} }
/**
* Location equivalence means that the coordinates and orientation are
* the same.
*/
public boolean equivalent (Location oloc)
{
return equals(oloc) && (orient == oloc.orient);
}
/** /**
* Computes a reasonable hashcode for location instances. * Computes a reasonable hashcode for location instances.
*/ */
@@ -1,5 +1,5 @@
// //
// $Id: SpotSceneManager.java,v 1.28 2003/03/17 19:34:26 mdb Exp $ // $Id: SpotSceneManager.java,v 1.29 2003/03/25 19:28:59 mdb Exp $
package com.threerings.whirled.spot.server; package com.threerings.whirled.spot.server;
@@ -337,14 +337,21 @@ public class SpotSceneManager extends SceneManager
public boolean addBody (BodyObject body) public boolean addBody (BodyObject body)
{ {
if (body instanceof ClusteredBodyObject) { if (body instanceof ClusteredBodyObject) {
// if they're already in the cluster, do nothing
if (containsKey(body.getOid())) {
return false;
}
put(body.getOid(), body); put(body.getOid(), body);
_cluster.occupants++; _cluster.occupants++;
recomputeCenter(); recomputeCenter();
if (_clobj != null) { if (_clobj != null) {
_ssobj.updateClusters(_cluster);
_clobj.addToOccupants(body.getOid());
((ClusteredBodyObject)body).setClusterOid(_clobj.getOid()); ((ClusteredBodyObject)body).setClusterOid(_clobj.getOid());
_clobj.addToOccupants(body.getOid());
_ssobj.updateClusters(_cluster);
} }
Log.info("Added " + body.who() + " to "+ this + ".");
return true; return true;
} else { } else {
@@ -362,16 +369,18 @@ public class SpotSceneManager extends SceneManager
Log.warning("Requested to remove unknown body from cluster " + Log.warning("Requested to remove unknown body from cluster " +
"[cloid=" + _clobj.getOid() + "[cloid=" + _clobj.getOid() +
", size=" + size() + ", who=" + bodyOid + "]."); ", size=" + size() + ", who=" + bodyOid + "].");
} else { return;
body.setClusterOid(-1);
_cluster.occupants--;
recomputeCenter();
if (_clobj != null) {
_clobj.removeFromOccupants(bodyOid);
_ssobj.updateClusters(_cluster);
}
} }
body.setClusterOid(-1);
_cluster.occupants--;
recomputeCenter();
if (_clobj != null) {
_clobj.removeFromOccupants(bodyOid);
_ssobj.updateClusters(_cluster);
}
Log.info("Removed " + bodyOid + " from "+ this + ".");
// if we've removed our last body; stick a fork in ourselves // if we've removed our last body; stick a fork in ourselves
if (size() == 0) { if (size() == 0) {
destroy(); destroy();
@@ -388,10 +397,6 @@ public class SpotSceneManager extends SceneManager
// keep this feller around // keep this feller around
_clobj = (ClusterObject)object; _clobj = (ClusterObject)object;
// configure our cluster record and publish it
_cluster.clusterOid = _clobj.getOid();
_ssobj.addToClusters(_cluster);
// let any mapped users know about our cluster // let any mapped users know about our cluster
Iterator iter = values().iterator(); Iterator iter = values().iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
@@ -400,6 +405,10 @@ public class SpotSceneManager extends SceneManager
_clobj.addToOccupants(((BodyObject)body).getOid()); _clobj.addToOccupants(((BodyObject)body).getOid());
} }
// configure our cluster record and publish it
_cluster.clusterOid = _clobj.getOid();
_ssobj.addToClusters(_cluster);
// if we didn't manage to add our creating user when we first // if we didn't manage to add our creating user when we first
// started up, there's no point in our sticking around // started up, there's no point in our sticking around
if (size() == 0) { if (size() == 0) {
@@ -420,6 +429,11 @@ public class SpotSceneManager extends SceneManager
} }
} }
public String toString ()
{
return "[cluster=" + _cluster + ", size=" + size() + "]";
}
protected void recomputeCenter () protected void recomputeCenter ()
{ {
int tx = 0, ty = 0, count = 0; int tx = 0, ty = 0, count = 0;
@@ -437,16 +451,19 @@ public class SpotSceneManager extends SceneManager
} }
} }
// convert the center back to "location" coordinates if (count > 1) {
coordsToLocation(tx/count, ty/count, _scratch); // convert the center back to "location" coordinates
_cluster.x = _scratch.x; coordsToLocation(tx/count, ty/count, _scratch);
_cluster.y = _scratch.y; _cluster.x = _scratch.x;
_cluster.y = _scratch.y;
}
} }
protected void destroy () protected void destroy ()
{ {
Log.info("Cluster empty, going away " + Log.info("Cluster empty, going away " +
"[cloid=" + _clobj.getOid() + "]."); "[cloid=" + _clobj.getOid() + "].");
_ssobj.removeFromClusters(_cluster.getKey());
CrowdServer.omgr.destroyObject(_clobj.getOid()); CrowdServer.omgr.destroyObject(_clobj.getOid());
} }