Skip to content

Commit

Permalink
Separate point, tripoint into new point.h
Browse files Browse the repository at this point in the history
It never made sense that point and tripoint were in enums.h.  Give them
their own header.
  • Loading branch information
jbytheway committed Jun 27, 2019
1 parent 0fe66aa commit eeb25e8
Show file tree
Hide file tree
Showing 29 changed files with 303 additions and 271 deletions.
1 change: 1 addition & 0 deletions src/advanced_inv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "cursesdef.h"
#include "enums.h"
#include "point.h"
#include "units.h"

class uilist;
Expand Down
1 change: 1 addition & 0 deletions src/avatar_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "enums.h"
#include "optional.h"
#include "point.h"

class avatar;
class item;
Expand Down
1 change: 1 addition & 0 deletions src/clzones.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "enums.h"
#include "optional.h"
#include "point.h"
#include "string_id.h"
#include "type_id.h"

Expand Down
1 change: 1 addition & 0 deletions src/coordinate_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define COORDINATE_CONVERSIONS_H

#include "enums.h"
#include "point.h"

/**
* Coordinate systems used here are:
Expand Down
1 change: 1 addition & 0 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "enums.h"
#include "game_constants.h"
#include "point.h"

/* find appropriate subdivided coordinates for absolute tile coordinate.
* This is less obvious than one might think, for negative coordinates, so this
Expand Down
1 change: 1 addition & 0 deletions src/craft_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>

#include "enums.h"
#include "point.h"
#include "requirements.h"

class inventory;
Expand Down
1 change: 1 addition & 0 deletions src/creature_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>

#include "enums.h"
#include "point.h"

class monster;
class JsonIn;
Expand Down
1 change: 1 addition & 0 deletions src/editmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "color.h"
#include "cursesdef.h"
#include "enums.h"
#include "point.h"
#include "type_id.h"

struct real_coords;
Expand Down
269 changes: 0 additions & 269 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
#ifndef ENUMS_H
#define ENUMS_H

#include <array>
#include <cassert>
#include <climits>
#include <ostream>
#include <cstdint>
#include <system_error>

class JsonOut;
class JsonIn;

template<typename T>
constexpr inline int sgn( const T x )
{
Expand Down Expand Up @@ -175,268 +168,6 @@ inline layer_level &operator++( layer_level &l )
return l;
}

struct point {
int x = 0;
int y = 0;
constexpr point() = default;
constexpr point( int X, int Y ) : x( X ), y( Y ) {}

constexpr point operator+( const point &rhs ) const {
return point( x + rhs.x, y + rhs.y );
}
point &operator+=( const point &rhs ) {
x += rhs.x;
y += rhs.y;
return *this;
}
constexpr point operator-( const point &rhs ) const {
return point( x - rhs.x, y - rhs.y );
}
point &operator-=( const point &rhs ) {
x -= rhs.x;
y -= rhs.y;
return *this;
}

/**
* Rotate point clockwise @param turns times, 90 degrees per turn,
* around the center of a rectangle with the dimensions specified
* by @param dim. By default rotates around the origin (0, 0).
*/
point rotate( int turns, const point &dim = { 1, 1 } ) const {
assert( turns >= 0 );
assert( turns <= 4 );

switch( turns ) {
case 1:
return { dim.y - y - 1, x };
case 2:
return { dim.x - x - 1, dim.y - y - 1 };
case 3:
return { y, dim.x - x - 1 };
}

return *this;
}
};

void serialize( const point &p, JsonOut &jsout );
void deserialize( point &p, JsonIn &jsin );

// Make point hashable so it can be used as an unordered_set or unordered_map key,
// or a component of one.
namespace std
{
template <>
struct hash<point> {
std::size_t operator()( const point &k ) const {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.y;
result *= a;
result += k.x;
return result;
}
};
} // namespace std

inline constexpr bool operator<( const point &a, const point &b )
{
return a.x < b.x || ( a.x == b.x && a.y < b.y );
}
inline constexpr bool operator==( const point &a, const point &b )
{
return a.x == b.x && a.y == b.y;
}
inline constexpr bool operator!=( const point &a, const point &b )
{
return !( a == b );
}

struct tripoint {
int x = 0;
int y = 0;
int z = 0;
constexpr tripoint() = default;
constexpr tripoint( int X, int Y, int Z ) : x( X ), y( Y ), z( Z ) {}
explicit constexpr tripoint( const point &p, int Z ) : x( p.x ), y( p.y ), z( Z ) {}

constexpr tripoint operator+( const tripoint &rhs ) const {
return tripoint( x + rhs.x, y + rhs.y, z + rhs.z );
}
constexpr tripoint operator-( const tripoint &rhs ) const {
return tripoint( x - rhs.x, y - rhs.y, z - rhs.z );
}
tripoint &operator+=( const tripoint &rhs ) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
constexpr tripoint operator-() const {
return tripoint( -x, -y, -z );
}
constexpr tripoint operator*( const int rhs ) const {
return tripoint( x * rhs, y * rhs, z * rhs );
}
tripoint &operator*=( const int rhs ) {
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
/*** some point operators and functions ***/
constexpr tripoint operator+( const point &rhs ) const {
return tripoint( x + rhs.x, y + rhs.y, z );
}
constexpr tripoint operator-( const point &rhs ) const {
return tripoint( x - rhs.x, y - rhs.y, z );
}
tripoint &operator+=( const point &rhs ) {
x += rhs.x;
y += rhs.y;
return *this;
}
tripoint &operator-=( const point &rhs ) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
tripoint &operator-=( const tripoint &rhs ) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}

void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
};

inline std::ostream &operator<<( std::ostream &os, const tripoint &pos )
{
return os << pos.x << "," << pos.y << "," << pos.z;
}

// Make tripoint hashable so it can be used as an unordered_set or unordered_map key,
// or a component of one.
namespace std
{
template <>
struct hash<tripoint> {
std::size_t operator()( const tripoint &k ) const {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.z;
result *= a;
result += k.y;
result *= a;
result += k.x;
return result;
}
};
} // namespace std

inline constexpr bool operator==( const tripoint &a, const tripoint &b )
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
inline constexpr bool operator!=( const tripoint &a, const tripoint &b )
{
return !( a == b );
}
inline bool operator<( const tripoint &a, const tripoint &b )
{
if( a.x != b.x ) {
return a.x < b.x;
}
if( a.y != b.y ) {
return a.y < b.y;
}
if( a.z != b.z ) {
return a.z < b.z;
}
return false;
}

static const std::array<tripoint, 8> eight_horizontal_neighbors = { {
{ -1, -1, 0 },
{ 0, -1, 0 },
{ +1, -1, 0 },
{ -1, 0, 0 },
{ +1, 0, 0 },
{ -1, +1, 0 },
{ 0, +1, 0 },
{ +1, +1, 0 },
}
};

struct rectangle {
point p_min;
point p_max;
constexpr rectangle() = default;
constexpr rectangle( const point &P_MIN, const point &P_MAX ) : p_min( P_MIN ), p_max( P_MAX ) {}
};

struct box {
tripoint p_min;
tripoint p_max;
constexpr box() = default;
constexpr box( const tripoint &P_MIN, const tripoint &P_MAX ) : p_min( P_MIN ), p_max( P_MAX ) {}
explicit constexpr box( const rectangle &R, int Z1, int Z2 ) :
p_min( tripoint( R.p_min, Z1 ) ), p_max( tripoint( R.p_max, Z2 ) ) {}
};

static constexpr tripoint tripoint_min { INT_MIN, INT_MIN, INT_MIN };
static constexpr tripoint tripoint_zero { 0, 0, 0 };
static constexpr tripoint tripoint_max{ INT_MAX, INT_MAX, INT_MAX };

static constexpr point point_min{ tripoint_min.x, tripoint_min.y };
static constexpr point point_zero{ tripoint_zero.x, tripoint_zero.y };
static constexpr point point_max{ tripoint_max.x, tripoint_max.y };

static constexpr point point_north{ 0, -1 };
static constexpr point point_north_east{ 1, -1 };
static constexpr point point_east{ 1, 0 };
static constexpr point point_south_east{ 1, 1 };
static constexpr point point_south{ 0, 1 };
static constexpr point point_south_west{ -1, 1 };
static constexpr point point_west{ -1, 0 };
static constexpr point point_north_west{ -1, -1 };

static constexpr box box_zero( tripoint_zero, tripoint_zero );
static constexpr rectangle rectangle_zero( point_zero, point_zero );

/** Checks if given tripoint is inbounds of given min and max tripoints using given clearance **/
inline bool generic_inbounds( const tripoint &p,
const box &boundaries,
const box &clearance = box_zero )
{
return p.x >= boundaries.p_min.x + clearance.p_min.x &&
p.x <= boundaries.p_max.x - clearance.p_max.x &&
p.y >= boundaries.p_min.y + clearance.p_min.y &&
p.y <= boundaries.p_max.y - clearance.p_max.y &&
p.z >= boundaries.p_min.z + clearance.p_min.z &&
p.z <= boundaries.p_max.z - clearance.p_max.z;
}

/** Checks if given point is inbounds of given min and max point using given clearance **/
inline bool generic_inbounds( const point &p,
const rectangle &boundaries,
const rectangle &clearance = rectangle_zero )
{
return generic_inbounds( tripoint( p, 0 ),
box( boundaries, 0, 0 ),
box( clearance, 0, 0 ) );
}

struct sphere {
int radius = 0;
tripoint center = tripoint_zero;

sphere() = default;
explicit sphere( const tripoint &center ) : radius( 1 ), center( center ) {}
explicit sphere( const tripoint &center, int radius ) : radius( radius ), center( center ) {}
};

/** Possible reasons to interrupt an activity. */
enum class distraction_type {
noise,
Expand Down
1 change: 1 addition & 0 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "calendar.h"
#include "enums.h"
#include "point.h"

enum event_type : int {
EVENT_NULL,
Expand Down
1 change: 1 addition & 0 deletions src/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "enums.h"
#include "math_defines.h"
#include "point.h"

/** Converts degrees to radians */
constexpr double DEGREES( double v )
Expand Down
2 changes: 1 addition & 1 deletion src/live_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef LIVE_VIEW_H
#define LIVE_VIEW_H

#include "enums.h" // tripoint
#include "point.h"

namespace catacurses
{
Expand Down
1 change: 1 addition & 0 deletions src/magic_teleporter_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "enums.h"
#include "optional.h"
#include "point.h"

class avatar;
class JsonIn;
Expand Down
1 change: 1 addition & 0 deletions src/map_item_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "enums.h"
#include "point.h"

class item;

Expand Down
1 change: 1 addition & 0 deletions src/map_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstddef>

#include "enums.h"
#include "point.h"

class tripoint_range
{
Expand Down
Loading

0 comments on commit eeb25e8

Please sign in to comment.