World model is now operational

This commit is contained in:
2021-01-16 01:24:33 -05:00
parent d7d633bd96
commit 313e78067a
11 changed files with 313 additions and 188 deletions

View File

@@ -13,7 +13,7 @@
#define CELL_INVALID 0
// Round a float and return as integer. Clamp result to the specified range.
static int round_and_clamp(double x, int lo, int hi) {
static int round_and_clamp(float x, int lo, int hi) {
x = round(x);
if (x < lo) return lo;
if (x > hi) return hi;
@@ -38,7 +38,7 @@ struct CellRange {
// the map: in that case, returns exactly the valid cells and not the
// invalid ones.
//
static CellRange rect_cell_range(double x1, double y1, double x2, double y2) {
static CellRange rect_cell_range(float x1, float y1, float x2, float y2) {
CellRange result;
result.xlo = round_and_clamp(x1 / CELL_SCALE, -CELL_LIMIT, CELL_LIMIT + 1);
result.ylo = round_and_clamp(y1 / CELL_SCALE, -CELL_LIMIT, CELL_LIMIT + 1);
@@ -54,9 +54,9 @@ static int64_t cell_id(int64_t cellx, int64_t celly) {
}
// Get the cell ID of the specified point, or CELL_INVALID if the point is off the map.
static int64_t point_cell_id(double x, double y) {
double cellx = round(x / CELL_SCALE);
double celly = round(y / CELL_SCALE);
static int64_t point_cell_id(float x, float y) {
float cellx = round(x / CELL_SCALE);
float celly = round(y / CELL_SCALE);
if ((cellx < -CELL_LIMIT) || (celly < -CELL_LIMIT) || (cellx > CELL_LIMIT) || (celly > CELL_LIMIT)) {
return CELL_INVALID;
}
@@ -101,10 +101,10 @@ void PlaneMap::insert(const std::string &plane, int64_t cellid, PlaneItem *clien
l.push_back(client);
}
void PlaneItem::set_pos(const std::string &plane, double x, double y, double z) {
int64_t old_cell = point_cell_id(x_, y_);
int64_t new_cell = point_cell_id(x, y);
void PlaneItem::set_pos(const std::string &plane, const util::XYZ &xyz) {
int64_t old_cell = point_cell_id(xyz_.x, xyz_.y);
int64_t new_cell = point_cell_id(xyz.x, xyz.y);
// Update the grid.
if (pmap_ != 0) {
if ((plane_ != plane) || (old_cell != new_cell)) {
@@ -115,14 +115,12 @@ void PlaneItem::set_pos(const std::string &plane, double x, double y, double z)
// Update the client position.
plane_ = plane;
x_ = x;
y_ = y;
z_ = z;
xyz_ = xyz;
}
void PlaneItem::untrack() {
if (pmap_ != 0) {
pmap_->remove(plane_, point_cell_id(x_, y_), this);
pmap_->remove(plane_, point_cell_id(xyz_.x, xyz_.y), this);
pmap_ = 0;
}
}
@@ -135,7 +133,7 @@ void PlaneMap::track(PlaneItem *item) {
}
}
PlaneItem::PlaneItem() : pmap_(NULL), x_(0.0), y_(0.0), z_(0.0) {
PlaneItem::PlaneItem() : pmap_(NULL), xyz_(0,0,0) {
}
PlaneMap::PlaneMap() {
@@ -176,13 +174,13 @@ int PlaneMap::total_cells() const {
return total;
}
PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, double x, double y, double radius) const {
PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, float x, float y, float radius) const {
PlaneMap::EltVec result;
auto piter = planes_.find(plane);
if (piter != planes_.end()) {
const Plane &p = piter->second;
CellRange range = rect_cell_range(x - radius, y - radius, x + radius, y + radius);
double radsq = radius*radius;
float radsq = radius*radius;
for (int cy = range.ylo; cy <= range.yhi; cy++) {
for (int cx = range.xlo; cx <= range.xhi; cx++) {
auto liter = p.find(cell_id(cx, cy));
@@ -200,8 +198,8 @@ PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, double x, doubl
}
LuaDefine(unittests_planemap, "c") {
double SC = CELL_SCALE;
double E = CELL_SCALE * 0.4;
float SC = CELL_SCALE;
float E = CELL_SCALE * 0.4;
int LO = -CELL_LIMIT;
int HI = CELL_LIMIT;
PlaneMap pm;
@@ -314,5 +312,15 @@ LuaDefine(unittests_planemap, "c") {
elts = pm.scan_radius("bar", 1000.0, 1000.0, 100.0);
LuaAssert(L, elts.size() == 2);
// Test static_field_cast.
struct MyTangible {
int x, y, z;
PlaneItem plane_item_;
};
MyTangible tan;
PlaneItem *pip = &tan.plane_item_;
MyTangible *tanp = pip->static_field_cast<MyTangible>();
LuaAssert(L, tanp == &tan);
return 0;
}