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