Initial revision of animqueue

This commit is contained in:
2021-01-12 14:14:38 -05:00
parent 78f8610eb8
commit 25b9b4cb5d
18 changed files with 785 additions and 308 deletions

View File

@@ -68,7 +68,7 @@ void PlaneMap::remove(const std::string &plane, int64_t cellid, PlaneItem *clien
if (cellid == CELL_INVALID) {
return;
}
if (plane == "") {
if ((plane == "") || (plane == "nowhere")) {
return;
}
auto piter = planes_.find(plane);
@@ -94,7 +94,7 @@ void PlaneMap::insert(const std::string &plane, int64_t cellid, PlaneItem *clien
if (cellid == CELL_INVALID) {
return;
}
if (plane == "") {
if ((plane == "") || (plane == "nowhere")) {
return;
}
Plane &p = planes_[plane];
@@ -107,10 +107,10 @@ void PlaneItem::set_pos(const std::string &plane, double x, double y, double z)
int64_t new_cell = point_cell_id(x, y);
// Update the grid.
if (grid_ != 0) {
if (pmap_ != 0) {
if ((plane_ != plane) || (old_cell != new_cell)) {
grid_->remove(plane_, old_cell, this);
grid_->insert(plane, new_cell, this);
pmap_->remove(plane_, old_cell, this);
pmap_->insert(plane, new_cell, this);
}
}
@@ -121,32 +121,36 @@ void PlaneItem::set_pos(const std::string &plane, double x, double y, double z)
z_ = z;
}
void PlaneItem::use_map(PlaneMap *grid) {
int64_t cellid = point_cell_id(x_, y_);
if (grid_ != 0) {
grid_->remove(plane_, cellid, this);
}
grid_ = grid;
if (grid_ != 0) {
grid_->insert(plane_, cellid, this);
void PlaneItem::untrack() {
if (pmap_ != 0) {
pmap_->remove(plane_, point_cell_id(x_, y_), this);
pmap_ = 0;
}
}
PlaneItem::PlaneItem() : grid_(NULL), x_(0.0), y_(0.0), z_(0.0) {
void PlaneMap::track(PlaneItem *item) {
if (item->pmap_ != this) {
item->untrack();
insert(item->plane(), point_cell_id(item->x(), item->y()), item);
item->pmap_ = this;
}
}
PlaneItem::PlaneItem() : pmap_(NULL), x_(0.0), y_(0.0), z_(0.0) {
}
PlaneMap::PlaneMap() {
}
PlaneItem::~PlaneItem() {
use_map(NULL);
untrack();
}
PlaneMap::~PlaneMap() {
for (const auto &p : planes_) {
for (const auto &l : p.second) {
for (PlaneItem *i : l.second) {
i->grid_ = NULL;
i->pmap_ = NULL;
}
}
}
@@ -262,10 +266,10 @@ LuaDefine(cunittests_planemap, "c") {
pm.remove("foo", 12345, &pib);
assert(pm.total_cells() == 0);
// Test the insert function on an invalid plane.
// Test the insert function on the nowhere plane.
pm.clear();
pm.insert("", 12345, &pia);
pm.insert("", 12345, &pib);
pm.insert("nowhere", 12345, &pia);
pm.insert("nowhere", 12345, &pib);
assert(pm.total_cells() == 0);
// Test the insert function on an invalid cell.
@@ -283,17 +287,17 @@ LuaDefine(cunittests_planemap, "c") {
// Attach pia to the grid. This should record it.
pm.clear();
pia.use_map(&pm);
pm.track(&pia);
elts = pm.get_cell("foo", point_cell_id(3.0, 4.0));
assert(elts.size() == 1);
assert(elts[0] == &pia);
// Unattach pia from the grid. This should unrecord it.
pia.use_map(NULL);
pia.untrack();
assert(pm.total_cells() == 0);
// Reattach pia to the grid, then move it.
pia.use_map(&pm);
pm.track(&pia);
assert(pm.total_cells() == 1);
pia.set_pos("bar", 1000.0, 1000.0, 0.0);
assert(pm.total_cells() == 1);
@@ -302,7 +306,7 @@ LuaDefine(cunittests_planemap, "c") {
assert(elts[0] == &pia);
// Insert the four elements, then test the scan function.
pib.use_map(&pm);
pm.track(&pib);
pib.set_pos("bar", 1100.0, 1000.0, 0.0);
elts = pm.scan_radius("bar", 1000.0, 1000.0, 1.0);
assert(elts.size() == 1);