2021-01-12 14:14:38 -05:00
|
|
|
//////////////////////////////////////////////////////////////
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
|
|
|
|
// PLANEMAP
|
|
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// This module defines two classes: PlaneMap, and PlaneItem. A
|
|
|
|
|
// PlaneItem is an object that has a plane (a string) and an
|
|
|
|
|
// XYZ position. A PlaneMap keeps track of potentially thousands
|
|
|
|
|
// of PlaneItem objects, allowing you to search for PlaneItems
|
|
|
|
|
// by scanning a geographic region.
|
|
|
|
|
//
|
|
|
|
|
// CLASS SPRITE DERIVES FROM PLANEITEM
|
|
|
|
|
//
|
|
|
|
|
// A PlaneMap records the positions of PlaneItems. The intent is
|
|
|
|
|
// that class Sprite should derive from PlaneItem. That way, the
|
|
|
|
|
// PlaneMap can record the positions of sprites.
|
|
|
|
|
//
|
|
|
|
|
// When you put derived types like Sprite into a PlaneMap, the
|
|
|
|
|
// PlaneMap will work fine. However, when you scan the PlaneMap,
|
|
|
|
|
// it will give return PlaneItem pointers, not Sprite pointers.
|
|
|
|
|
//
|
|
|
|
|
// If you're sure that the PlaneMap contains only sprites, then
|
|
|
|
|
// you can use static_cast to convert those PlaneItem pointers to
|
|
|
|
|
// Sprite pointers. Sadly, there's no simple way to avoid the casting.
|
|
|
|
|
//
|
|
|
|
|
// THE SCANNABLE REGION IS FINITE
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
|
|
|
|
// A plane is a rectangle, with a finite size: if you stray more
|
2021-01-12 14:14:38 -05:00
|
|
|
// than 80,000,000 meters from the origin, then that PlaneItem
|
|
|
|
|
// is beyond the scannable region.
|
|
|
|
|
//
|
|
|
|
|
// There's nothing stopping you from moving a PlaneItem outside
|
|
|
|
|
// the scannable region. It is not an error to do so.
|
|
|
|
|
// However, if you do move a PlaneItem outside the scannable
|
|
|
|
|
// region, then that PlaneItem will not show up for scan_radius.
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// PLANES CANNOT BE DESTROYED BUT THEY CAN BE UNINHABITED
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// You can't "create" or "destroy" planes. Instead, we say
|
|
|
|
|
// that a plane is "uninhabited" until you put a PlaneItem
|
|
|
|
|
// there. Initially, all possible planes exist, but they're
|
|
|
|
|
// all uninhabited until you put a PlaneItem there. Planes
|
|
|
|
|
// that are uninhabited take no memory.
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// THE NOWHERE PLANE
|
|
|
|
|
//
|
|
|
|
|
// If you use the literal "nowhere" as a plane name, a special case is
|
2021-01-06 15:10:21 -05:00
|
|
|
// triggered: you're "nowhere." Radius scans can never find items
|
2021-01-12 14:14:38 -05:00
|
|
|
// whose plane is "nowhere." The same also applies when you use the
|
|
|
|
|
// empty string as a plane name.
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// THE Z COORDINATE IS IGNORED
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// Class PlaneItem stores X, Y, and Z. The Z coordinate is
|
|
|
|
|
// ignored for all plane-scanning operations. In other words,
|
|
|
|
|
// planes are 2D. The only reason we store a Z coordinate
|
|
|
|
|
// is for the consistency of storing the model's X, Y, and Z
|
|
|
|
|
// all in the same place.
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
|
|
|
|
// MEMORY MANAGEMENT
|
|
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
// PlaneMaps do not own PlaneItems. This is a deliberate choice.
|
|
|
|
|
// We assume that sprites will be owned by the sprite ID table.
|
|
|
|
|
// So therefore, the PlaneMaps shouldn't own the sprites.
|
|
|
|
|
//
|
|
|
|
|
// So instead, we use this rule: a PlaneMap has a function 'track'
|
|
|
|
|
// that causes it to start tracking the location of a PlaneItem.
|
|
|
|
|
// However, the PlaneMap still doesn't own the PlaneItem.
|
|
|
|
|
// If you destroy a PlaneMap, all the PlaneItems
|
|
|
|
|
// will automatically be untracked, but they won't be deleted.
|
2021-01-06 15:10:21 -05:00
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
//////////////////////////////////////////////////////////////
|
2021-01-06 15:10:21 -05:00
|
|
|
|
|
|
|
|
#ifndef PLANEMAP_HPP
|
|
|
|
|
#define PLANEMAP_HPP
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <map>
|
2021-07-25 19:22:39 -04:00
|
|
|
#include "util.hpp"
|
2021-01-06 15:10:21 -05:00
|
|
|
|
|
|
|
|
class PlaneMap;
|
|
|
|
|
|
|
|
|
|
class PlaneItem {
|
|
|
|
|
friend class PlaneMap;
|
2021-01-12 14:14:38 -05:00
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
private:
|
2021-01-12 14:14:38 -05:00
|
|
|
PlaneMap *pmap_;
|
2021-01-06 15:10:21 -05:00
|
|
|
std::string plane_;
|
2021-08-03 11:25:12 -04:00
|
|
|
float x_, y_, z_;
|
2021-01-22 17:35:23 -05:00
|
|
|
int64_t id_;
|
2021-01-12 14:14:38 -05:00
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
public:
|
|
|
|
|
PlaneItem();
|
|
|
|
|
~PlaneItem();
|
|
|
|
|
|
2021-01-22 17:35:23 -05:00
|
|
|
// You may modify the ID at any time.
|
|
|
|
|
void set_id(int64_t id) { id_ = id; }
|
|
|
|
|
|
|
|
|
|
int64_t id() const { return id_; }
|
2021-01-06 15:10:21 -05:00
|
|
|
const std::string &plane() const { return plane_; }
|
2021-08-03 11:25:12 -04:00
|
|
|
const float x() const { return x_; }
|
|
|
|
|
const float y() const { return y_; }
|
|
|
|
|
const float z() const { return z_; }
|
2021-01-06 15:10:21 -05:00
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
void untrack();
|
2021-03-25 15:34:43 -04:00
|
|
|
void track(PlaneMap *pmap);
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
void set_pos(const std::string &plane, float x, float y, float z);
|
|
|
|
|
void set_xyz(float x, float y, float z) { set_pos(plane_, x, y, z); }
|
2021-01-06 15:10:21 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PlaneMap {
|
|
|
|
|
friend class PlaneItem;
|
|
|
|
|
private:
|
2021-01-12 14:14:38 -05:00
|
|
|
using EltVec = std::vector<PlaneItem *>;
|
2021-01-06 15:10:21 -05:00
|
|
|
using Plane = std::map<int64_t, EltVec>;
|
|
|
|
|
std::map<std::string, Plane> planes_;
|
|
|
|
|
void remove(const std::string &plane, int64_t cell, PlaneItem *client);
|
|
|
|
|
void insert(const std::string &plane, int64_t cell, PlaneItem *client);
|
2021-01-12 14:14:38 -05:00
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
public:
|
2021-07-25 19:22:39 -04:00
|
|
|
using IdVector = util::IdVector;
|
2021-01-06 15:10:21 -05:00
|
|
|
PlaneMap();
|
|
|
|
|
~PlaneMap();
|
2021-07-25 19:22:39 -04:00
|
|
|
|
|
|
|
|
// Caution: scan_radius is not deterministically ordered.
|
2021-08-03 11:25:12 -04:00
|
|
|
IdVector scan_radius(const std::string &plane, float x, float y, float radius, int64_t prepend) const;
|
2021-01-12 14:14:38 -05:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// unit testing stuff.
|
2021-09-07 17:37:23 -04:00
|
|
|
friend int lfn_unittests_planemap(lua_State *L);
|
2021-01-12 14:14:38 -05:00
|
|
|
EltVec get_cell(const std::string &plane, int64_t cell) const;
|
|
|
|
|
int total_cells() const;
|
|
|
|
|
void clear() { planes_.clear(); }
|
2021-01-06 15:10:21 -05:00
|
|
|
};
|
|
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
#endif // PLANEMAP_HPP
|
|
|
|
|
|
|
|
|
|
|