OpenXcom  1.0
Open-source clone of the original X-Com
RuleRegion.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 <string>
21 #include <vector>
22 #include <yaml-cpp/yaml.h>
23 #include "../fmath.h"
24 #include "../Savegame/WeightedOptions.h"
25 
26 namespace OpenXcom
27 {
28 
34 {
35  double lonMin, lonMax, latMin, latMax;
36  int texture;
37  std::string name;
38 
39  bool operator== (const MissionArea& ma) const
40  {
41  return AreSame(lonMax, ma.lonMax) && AreSame(lonMin, ma.lonMin) && AreSame(latMax, ma.latMax) && AreSame(latMin, ma.latMin);
42  }
43 
44  bool isPoint() const
45  {
46  return AreSame(lonMin, lonMax) && AreSame(latMin, latMax);
47  }
48 };
49 
54 {
55  std::vector<MissionArea> areas;
56 
57  void swap(MissionZone &other)
58  {
59  areas.swap(other.areas);
60  }
61 };
62 
63 class City;
64 class Target;
65 
72 {
73 private:
74  std::string _type;
75  int _cost;
76  std::vector<double> _lonMin, _lonMax, _latMin, _latMax;
77  std::vector<City*> _cities;
79  WeightedOptions _missionWeights;
81  size_t _regionWeight;
83  std::vector<MissionZone> _missionZones;
85  std::string _missionRegion;
86 public:
88  RuleRegion(const std::string &type);
90  ~RuleRegion();
92  void load(const YAML::Node& node);
94  std::string getType() const;
96  int getBaseCost() const;
98  bool insideRegion(double lon, double lat) const;
100  std::vector<City*> *getCities();
102  size_t getWeight() const;
104  const WeightedOptions &getAvailableMissions() const { return _missionWeights; }
106  const std::string &getMissionRegion() const { return _missionRegion; }
108  std::pair<double, double> getRandomPoint(size_t zone) const;
110  MissionArea getMissionPoint(size_t zone, Target *target) const;
112  MissionArea getRandomMissionPoint(size_t zone) const;
114  const std::vector<double> &getLonMax() const { return _lonMax; }
116  const std::vector<double> &getLonMin() const { return _lonMin; }
118  const std::vector<double> &getLatMax() const { return _latMax; }
120  const std::vector<double> &getLatMin() const { return _latMin; }
122  const std::vector<MissionZone> &getMissionZones() const;
123 };
124 
125 }
126 
127 namespace YAML
128 {
129  template<>
130  struct convert<OpenXcom::MissionArea>
131  {
132  static Node encode(const OpenXcom::MissionArea& rhs)
133  {
134  Node node;
135  node.push_back(Rad2Deg(rhs.lonMin));
136  node.push_back(Rad2Deg(rhs.lonMax));
137  node.push_back(Rad2Deg(rhs.latMin));
138  node.push_back(Rad2Deg(rhs.latMax));
139  return node;
140  }
141 
142  static bool decode(const Node& node, OpenXcom::MissionArea& rhs)
143  {
144  if (!node.IsSequence() || node.size() < 4)
145  return false;
146 
147  rhs.lonMin = Deg2Rad(node[0].as<double>());
148  rhs.lonMax = Deg2Rad(node[1].as<double>());
149  rhs.latMin = Deg2Rad(node[2].as<double>());
150  rhs.latMax = Deg2Rad(node[3].as<double>());
151  if (rhs.latMin > rhs.latMax)
152  std::swap(rhs.latMin, rhs.latMax);
153  if (node.size() >= 5) rhs.texture = node[4].as<int>();
154  if (node.size() >= 6) rhs.name = node[5].as<std::string>();
155  return true;
156  }
157  };
158 
159  template<>
160  struct convert<OpenXcom::MissionZone>
161  {
162  static Node encode(const OpenXcom::MissionZone& rhs)
163  {
164  Node node;
165  node = rhs.areas;
166  return node;
167  }
168 
169  static bool decode(const Node& node, OpenXcom::MissionZone& rhs)
170  {
171  if (!node.IsSequence())
172  return false;
173 
174  rhs.areas = node.as< std::vector<OpenXcom::MissionArea> >(rhs.areas);
175  return true;
176  }
177  };
178 }
const std::vector< double > & getLatMin() const
Gets the minimum latitude.
Definition: RuleRegion.h:120
std::vector< City * > * getCities()
Gets the cities in this region.
Definition: RuleRegion.cpp:124
int getBaseCost() const
Gets the region&#39;s base cost.
Definition: RuleRegion.cpp:90
RuleRegion(const std::string &type)
Creates a blank region ruleset.
Definition: RuleRegion.cpp:31
const std::vector< double > & getLonMin() const
Gets the minimum longitude.
Definition: RuleRegion.h:116
MissionArea getRandomMissionPoint(size_t zone) const
Gets a random mission area.
Definition: RuleRegion.cpp:221
Defines a rectangle in polar coordinates.
Definition: RuleRegion.h:33
A zone (set of areas) on the globe.
Definition: RuleRegion.h:53
const std::vector< double > & getLatMax() const
Gets the maximum latitude.
Definition: RuleRegion.h:118
const std::vector< double > & getLonMax() const
Gets the maximum longitude.
Definition: RuleRegion.h:114
size_t getWeight() const
Gets the weight of this region for mission selection.
Definition: RuleRegion.cpp:149
const std::vector< MissionZone > & getMissionZones() const
Gets a list of MissionZones.
Definition: RuleRegion.cpp:158
Definition: Position.h:81
Represents a city of the world.
Definition: City.h:32
const WeightedOptions & getAvailableMissions() const
Gets the weighted list of missions for this region.
Definition: RuleRegion.h:104
bool insideRegion(double lon, double lat) const
Checks if a point is inside the region.
Definition: RuleRegion.cpp:101
const std::string & getMissionRegion() const
Gets the substitute mission region.
Definition: RuleRegion.h:106
std::pair< double, double > getRandomPoint(size_t zone) const
Gets a random point inside a mission zone.
Definition: RuleRegion.cpp:168
Holds pairs of relative weights and IDs.
Definition: WeightedOptions.h:33
Base class for targets on the globe with a set of radian coordinates.
Definition: Target.h:35
void load(const YAML::Node &node)
Loads the region from YAML.
Definition: RuleRegion.cpp:50
Represents a specific region of the world.
Definition: RuleRegion.h:71
MissionArea getMissionPoint(size_t zone, Target *target) const
Gets the mission area for the corresponding target.
Definition: RuleRegion.cpp:201
std::string getType() const
Gets the region&#39;s type.
Definition: RuleRegion.cpp:81
~RuleRegion()
Cleans up the region ruleset.
Definition: RuleRegion.cpp:38
Definition: BaseInfoState.cpp:40