OpenXcom  1.0
Open-source clone of the original X-Com
Position.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <yaml-cpp/yaml.h>
21 
22 namespace OpenXcom
23 {
24 
28 class Position
29 {
30 public:
31  int x, y, z;
32 
34  Position() : x(0), y(0), z(0) {};
36  Position(int x_, int y_, int z_) : x(x_), y(y_), z(z_) {};
38  Position(const Position& pos) : x(pos.x), y(pos.y), z(pos.z) {};
39 
40  Position& operator=(const Position& pos) { x = pos.x; y = pos.y; z = pos.z; return *this; }
41 
42  Position operator+(const Position& pos) const { return Position(x + pos.x, y + pos.y, z + pos.z); }
43  Position& operator+=(const Position& pos) { x+=pos.x; y+=pos.y; z+=pos.z; return *this; }
44 
45  Position operator-(const Position& pos) const { return Position(x - pos.x, y - pos.y, z - pos.z); }
46  Position& operator-=(const Position& pos) { x-=pos.x; y-=pos.y; z-=pos.z; return *this; }
47 
48  Position operator*(const Position& pos) const { return Position(x * pos.x, y * pos.y, z * pos.z); }
49  Position& operator*=(const Position& pos) { x*=pos.x; y*=pos.y; z*=pos.z; return *this; }
50  Position operator*(const int v) const { return Position(x * v, y * v, z * v); }
51  Position& operator*=(const int v) { x*=v; y*=v; z*=v; return *this; }
52 
53  Position operator/(const Position& pos) const { return Position(x / pos.x, y / pos.y, z / pos.z); }
54  Position& operator/=(const Position& pos) { x/=pos.x; y/=pos.y; z/=pos.z; return *this; }
55 
56  Position operator/(const int v) const { return Position(x / v, y / v, z / v); }
57 
59  bool operator== (const Position& pos) const
60  {
61  return x == pos.x && y == pos.y && z == pos.z;
62  }
64  bool operator!= (const Position& pos) const
65  {
66  return x != pos.x || y != pos.y || z != pos.z;
67  }
68 
69 };
70 
71 inline std::ostream& operator<<(std::ostream& out, const Position& pos)
72 {
73  out << "(" << pos.x << "," << pos.y << ","<< pos.z << ")";
74  return out;
75 }
76 
77 typedef Position Vector3i;
78 
79 }
80 
81 namespace YAML
82 {
83  template<>
84  struct convert<OpenXcom::Position>
85  {
86  static Node encode(const OpenXcom::Position& rhs)
87  {
88  Node node;
89  node.push_back(rhs.x);
90  node.push_back(rhs.y);
91  node.push_back(rhs.z);
92  return node;
93  }
94 
95  static bool decode(const Node& node, OpenXcom::Position& rhs)
96  {
97  if (!node.IsSequence() || node.size() != 3)
98  return false;
99 
100  rhs.x = node[0].as<int>();
101  rhs.y = node[1].as<int>();
102  rhs.z = node[2].as<int>();
103  return true;
104  }
105  };
106 }
bool operator==(const Position &pos) const
== operator
Definition: Position.h:59
bool operator!=(const Position &pos) const
!= operator
Definition: Position.h:64
Position(const Position &pos)
Copy constructor.
Definition: Position.h:38
Definition: Position.h:81
Position()
Null position constructor.
Definition: Position.h:34
Position(int x_, int y_, int z_)
X Y Z position constructor.
Definition: Position.h:36
Easy handling of X-Y-Z coordinates.
Definition: Position.h:28
Definition: BaseInfoState.cpp:40