Colobot
object_manager.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program 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  * This program 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.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include "common/singleton.h"
28 
29 #include "math/const.h"
30 #include "math/vector.h"
31 
32 #include "object/object_create_params.h"
34 #include "object/object_type.h"
35 
36 #include <map>
37 #include <vector>
38 #include <memory>
39 
40 namespace Gfx
41 {
42 class CEngine;
43 class CModelManager;
44 class COldModelManager;
45 class CParticle;
46 class CTerrain;
47 } // namespace Gfx
48 
49 class CObject;
50 class CObjectFactory;
51 
52 enum RadarFilter
53 {
54  FILTER_NONE = 0,
55 
56  FILTER_ONLYLANDING = 1 << (8+0),
57  FILTER_ONLYFLYING = 1 << (8+1),
58 
59  FILTER_FRIENDLY = 1 << (8+2),
60  FILTER_ENEMY = 1 << (8+3),
61  FILTER_NEUTRAL = 1 << (8+4),
62 };
63 
64 using CObjectMap = std::map<int, std::unique_ptr<CObject>>;
65 using CObjectMapCIt = std::map<int, std::unique_ptr<CObject>>::const_iterator;
66 
68 {
69 private:
70  friend class CObjectContainerProxy;
71 
72  CObjectIteratorProxy(CObjectMapCIt currentIt, CObjectMapCIt endIt)
73  : m_currentIt(currentIt)
74  , m_endIt(endIt)
75  {
76  while (m_currentIt != m_endIt && m_currentIt->second == nullptr)
77  {
78  ++m_currentIt;
79  }
80  }
81 
82 public:
83  CObject* operator*()
84  {
85  return m_currentIt->second.get();
86  }
87 
88  void operator++()
89  {
90  do
91  {
92  ++m_currentIt;
93  }
94  while (m_currentIt != m_endIt && m_currentIt->second == nullptr);
95  }
96 
97  bool operator==(const CObjectIteratorProxy& other)
98  {
99  return m_currentIt == other.m_currentIt;
100  }
101 
102  bool operator!=(const CObjectIteratorProxy& other)
103  {
104  return m_currentIt != other.m_currentIt;
105  }
106 
107 private:
108  CObjectMapCIt m_currentIt;
109  CObjectMapCIt m_endIt;
110 };
111 
113 {
114 private:
115  friend class CObjectManager;
116 
117  CObjectContainerProxy(const CObjectMap& map, int& activeIteratorsCounter)
118  : m_map(map),
119  m_activeIteratorsCounter(activeIteratorsCounter)
120  {
121  ++m_activeIteratorsCounter;
122  }
123 
124 public:
126  {
127  --m_activeIteratorsCounter;
128  }
129 
130  CObjectIteratorProxy begin() const
131  {
132  return CObjectIteratorProxy(m_map.begin(), m_map.end());
133  }
134  CObjectIteratorProxy end() const
135  {
136  return CObjectIteratorProxy(m_map.end(), m_map.end());
137  }
138 
139 private:
140  const CObjectMap& m_map;
141  int& m_activeIteratorsCounter;
142 };
143 
148 class CObjectManager : public CSingleton<CObjectManager>
149 {
150 public:
152  Gfx::CTerrain* terrain,
153  Gfx::COldModelManager* oldModelManager,
154  Gfx::CModelManager* modelManager,
155  Gfx::CParticle* particle);
156  virtual ~CObjectManager();
157 
159 
160  CObject* CreateObject(ObjectCreateParams params);
161  CObject* CreateObject(Math::Vector pos, float angle, ObjectType type, float power = -1.0f);
163 
165  bool DeleteObject(CObject* instance);
167  void DeleteAllObjects();
168 
170  CObject* GetObjectById(unsigned int id);
171 
173  CObject* GetObjectByRank(unsigned int id);
174 
176  std::vector<CObject*> GetObjectsOfTeam(int team);
177 
179  bool TeamExists(int team);
180 
182  // TODO: This should be probably moved to separate class
183  void DestroyTeam(int team);
184 
186  int CountObjectsImplementing(ObjectInterfaceType interface);
187 
190  {
191  CleanRemovedObjectsIfNeeded();
192  return CObjectContainerProxy(m_objects, m_activeObjectIterators);
193  }
194 
196 
197  std::vector<CObject*> RadarAll(CObject* pThis,
198  ObjectType type = OBJECT_NULL,
199  float angle = 0.0f,
200  float focus = Math::PI*2.0f,
201  float minDist = 0.0f,
202  float maxDist = 1000.0f,
203  bool furthest = false,
204  RadarFilter filter = FILTER_NONE,
205  bool cbotTypes = false);
206  std::vector<CObject*> RadarAll(CObject* pThis,
207  std::vector<ObjectType> type = std::vector<ObjectType>(),
208  float angle = 0.0f,
209  float focus = Math::PI*2.0f,
210  float minDist = 0.0f,
211  float maxDist = 1000.0f,
212  bool furthest = false,
213  RadarFilter filter = FILTER_NONE,
214  bool cbotTypes = false);
215  std::vector<CObject*> RadarAll(CObject* pThis,
216  Math::Vector thisPosition,
217  float thisAngle,
218  ObjectType type = OBJECT_NULL,
219  float angle = 0.0f,
220  float focus = Math::PI*2.0f,
221  float minDist = 0.0f,
222  float maxDist = 1000.0f,
223  bool furthest = false,
224  RadarFilter filter = FILTER_NONE,
225  bool cbotTypes = false);
226  std::vector<CObject*> RadarAll(CObject* pThis,
227  Math::Vector thisPosition,
228  float thisAngle,
229  std::vector<ObjectType> type = std::vector<ObjectType>(),
230  float angle = 0.0f,
231  float focus = Math::PI*2.0f,
232  float minDist = 0.0f,
233  float maxDist = 1000.0f,
234  bool furthest = false,
235  RadarFilter filter = FILTER_NONE,
236  bool cbotTypes = false);
238 
240  CObject* Radar(CObject* pThis,
241  ObjectType type = OBJECT_NULL,
242  float angle = 0.0f,
243  float focus = Math::PI*2.0f,
244  float minDist = 0.0f,
245  float maxDist = 1000.0f,
246  bool furthest = false,
247  RadarFilter filter = FILTER_NONE,
248  bool cbotTypes = false);
249  CObject* Radar(CObject* pThis,
250  std::vector<ObjectType> type = std::vector<ObjectType>(),
251  float angle = 0.0f,
252  float focus = Math::PI*2.0f,
253  float minDist = 0.0f,
254  float maxDist = 1000.0f,
255  bool furthest = false,
256  RadarFilter filter = FILTER_NONE,
257  bool cbotTypes = false);
258  CObject* Radar(CObject* pThis,
259  Math::Vector thisPosition,
260  float thisAngle,
261  ObjectType type = OBJECT_NULL,
262  float angle = 0.0f,
263  float focus = Math::PI*2.0f,
264  float minDist = 0.0f,
265  float maxDist = 1000.0f,
266  bool furthest = false,
267  RadarFilter filter = FILTER_NONE,
268  bool cbotTypes = false);
269  CObject* Radar(CObject* pThis,
270  Math::Vector thisPosition,
271  float thisAngle,
272  std::vector<ObjectType> type = std::vector<ObjectType>(),
273  float angle = 0.0f,
274  float focus = Math::PI*2.0f,
275  float minDist = 0.0f,
276  float maxDist = 1000.0f,
277  bool furthest = false,
278  RadarFilter filter = FILTER_NONE,
279  bool cbotTypes = false);
281 
283  CObject* FindNearest(CObject* pThis,
284  ObjectType type = OBJECT_NULL,
285  float maxDist = 1000.0f,
286  bool cbotTypes = false);
287  CObject* FindNearest(CObject* pThis,
288  std::vector<ObjectType> type = std::vector<ObjectType>(),
289  float maxDist = 1000.0f,
290  bool cbotTypes = false);
291  CObject* FindNearest(CObject* pThis,
292  Math::Vector thisPosition,
293  ObjectType type = OBJECT_NULL,
294  float maxDist = 1000.0f,
295  bool cbotTypes = false);
296  CObject* FindNearest(CObject* pThis,
297  Math::Vector thisPosition,
298  std::vector<ObjectType> type = std::vector<ObjectType>(),
299  float maxDist = 1000.0f,
300  bool cbotTypes = false);
302 
303 private:
304  void CleanRemovedObjectsIfNeeded();
305 
306 private:
307  CObjectMap m_objects;
308  std::unique_ptr<CObjectFactory> m_objectFactory;
309  int m_nextId;
310  int m_activeObjectIterators;
311  bool m_shouldCleanRemovedObjects;
312 };
Definition: object_manager.h:112
CSingleton base class for singletons.
Definition: singleton.h:30
Manager for static models.
Definition: oldmodelmanager.h:54
ObjectType enum.
const float PI
PI.
Definition: const.h:48
Particle engine.
Definition: particle.h:223
ObjectInterfaceType
Type of interface that an object implements.
Definition: object_interface_type.h:34
CObjectContainerProxy GetAllObjects()
Returns all objects.
Definition: object_manager.h:189
ObjectInterfaceType enum.
Terrain loader/generator and manager.
Definition: terrain.h:147
Definition: object_manager.h:67
Constants used in math functions.
Namespace for (new) graphics code.
Definition: app.h:49
Vector struct and related functions.
Manager for models read from model files.
Definition: model_manager.h:34
The graphics engine.
Definition: engine.h:585
Definition: object_create_params.h:26
ObjectType
Type of game object.
Definition: object_type.h:33
3D (3x1) vector
Definition: vector.h:53
Definition: object_factory.h:48
Manages CObject instances.
Definition: object_manager.h:148
Base class for all 3D in-game objects.
Definition: object.h:63