iFun Engine API  1.0.0-b6053
Great Technology for Great Games
world.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_SERVICE_WORLD_H_
10 #define INCLUDE_FUNAPI_SERVICE_WORLD_H_
11 
12 #include <boost/noncopyable.hpp>
13 #include <funapi/network/session.h>
14 #include <funapi/types.h>
15 
16 
17 namespace fun {
18 
19 class FUNAPI_DLL_VISIBILITY World : private boost::noncopyable {
20  public:
22 
23  class Impl;
24 
25  struct FUNAPI_DLL_VISIBILITY Point {
26  Point();
27  Point(float x, float y, float z);
28  float x;
29  float y;
30  float z;
31  };
32 
33  struct FUNAPI_DLL_VISIBILITY Sphere {
34  Sphere();
35  Sphere(const Point &coordinates, float radius);
36  Sphere(float x, float y, float z, float radius);
37  Point coordinates;
38  float radius;
39  };
40 
41  struct FUNAPI_DLL_VISIBILITY Vector2 {
42  Vector2();
43  Vector2(float x, float y);
44  float x;
45  float y;
46  };
47 
48  struct FUNAPI_DLL_VISIBILITY FindOption {
49  FindOption();
50  FindOption(bool use_distance_square,
51  bool use_sphere_center_for_distance = false,
52  bool ignore_z_axis_in_block = false,
53  bool use_ascending_order = false,
54  size_t boundary_begin = 0,
55  size_t boundary_count = -1);
56  bool use_distance_square;
57  bool use_sphere_center_for_distance;
58  bool ignore_z_axis_in_block;
59  bool use_ascending_order;
60  size_t boundary_begin;
61  size_t boundary_count;
62  };
63 
64  class FUNAPI_DLL_VISIBILITY Object : private boost::noncopyable {
65  public:
66  class Impl;
67 
69 
70  Object();
71  explicit Object(float radius);
72 
73  explicit Object(int64_t id);
74  Object(int64_t id, float radius);
75 
76  virtual ~Object();
77 
78  int64_t GetId() const;
79 
80  const Point &GetCoordinates() const;
81  const Sphere &GetSphere() const;
82 
83  const string &GetName() const;
84  void SetName(const string &name);
85 
86  const float &GetRadius() const;
87  void SetRadius(float radius);
88 
89  int64_t GetType() const;
90  void SetType(int64_t type);
91 
92  bool GetValueForKey(const int64_t &key, int64_t *result) const;
93  void SetKeyValue(const int64_t &key, const int64_t &value);
94 
95  Ptr<Session> GetSession() const;
96  void SetSession(const Ptr<Session> &session);
97 
98  bool IsSphere();
99 
100  private:
101  friend class PrivateAccess;
102  Impl *impl_;
103  };
104 
105  typedef function<void(int64_t type, bool exist)> TypeExistenceCallback;
106 
107  typedef function<bool(const Point &/*coordinates arg*/,
108  float /*distance arg*/,
109  const Ptr<Object> &/*object*/)> FindFilter;
110 
111  typedef function<bool(const Ptr<World::Object> &object)> ObjectFilter;
112 
113  typedef std::vector<Ptr<Object> > ObjectVector;
114  typedef std::vector<Ptr<Session> > SessionVector;
115  typedef std::vector<float> DistanceVector;
116 
117  static const int64_t kInvalidId;
118  static const FindOption kDefaultOption;
119  static const FindFilter kNullFilter;
120 
121  static FindFilter MakeFilter(int64_t type_mask);
122  static FindFilter MakeMovingSightFilter(const Sphere &previous_coordinates,
123  int64_t type_mask = 0);
124  static FindFilter MakeRectangleFilter(float front_gap,
125  const Vector2 &direction,
126  float width,
127  float depth,
128  float height,
129  bool is_sphere = false,
130  int64_t type_mask = 0);
131  static FindFilter MakeCircularSectorFilter(float front_gap,
132  const Vector2 &direction,
133  float radius,
134  float left_angle_in_degree,
135  float right_angle_in_degree,
136  float height,
137  bool is_sphere = false,
138  int64_t type_mask = 0);
139 
140  ~World();
141 
142  const string &GetName() const;
143 
144  void MonitorTypeExistence(int64_t type,
145  const TypeExistenceCallback &callback);
146 
147  int64_t InsertObject(const Point &coordinates,
148  const Ptr<Object> &object) ASSERT_NO_ROLLBACK;
149  Ptr<Object> EraseObject(int64_t id) ASSERT_NO_ROLLBACK;
150  Ptr<Object> GetObject(int64_t id) const;
151 
152  size_t EraseAllObjects(ObjectVector *objects = NULL) ASSERT_NO_ROLLBACK;
153 
154  void GetAllObjects(const ObjectFilter &filter,
155  ObjectVector *objects) const;
156  void GetAllObjects(ObjectVector *objects) const;
157 
158  size_t GetObjectCount() const;
159 
160  bool MoveObjectTo(int64_t id, const Point &coordinate) ASSERT_NO_ROLLBACK;
161  bool MoveObject(int64_t id, const Point &coordinates_delta,
162  Point *new_coordinates = NULL) ASSERT_NO_ROLLBACK;
163 
164  void FindObject(const Point &coordinates, float distance,
165  ObjectVector *objects);
166  void FindObject(const Point &coordinates, float distance,
167  const FindOption &option, ObjectVector *objects,
168  DistanceVector *distances = NULL);
169  void FindObject(const Point &coordinates, float distance,
170  const FindOption &option, const FindFilter &filter,
171  ObjectVector *objects, DistanceVector *distances = NULL);
172 
173  void FindObject(int64_t id, float distance, ObjectVector *objects);
174  void FindObject(int64_t id, float distance, const FindOption &option,
175  ObjectVector *objects, DistanceVector *distances = NULL);
176  void FindObject(int64_t id, float distance, const FindOption &option,
177  const FindFilter &filter, ObjectVector *objects,
178  DistanceVector *distances = NULL);
179 
180  void FindSession(const Point &coordinates, float distance,
181  SessionVector *out);
182  void FindSession(const Point &coordinates, float distance,
183  const FindOption &option, SessionVector *out);
184  void FindSession(const Point &coordinates, float distance,
185  const FindOption &option, const FindFilter &filter,
186  SessionVector *out);
187 
188  void FindSession(int64_t id, float distance, SessionVector *out);
189  void FindSession(int64_t id, float distance, const FindOption &option,
190  SessionVector *out);
191  void FindSession(int64_t id, float distance, const FindOption &option,
192  const FindFilter &filter, SessionVector *out);
193 
194  void Broadcast(const Point &coordinates, float distance,
195  const string &message_type, const Json &message,
196  Encryption encryption, TransportProtocol protocol,
197  const FindOption &option = kDefaultOption,
198  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
199  void Broadcast(const Point &coordinates, float distance,
200  const string &message_type, const Ptr<FunMessage> &message,
201  Encryption encryption, TransportProtocol protocol,
202  const FindOption &option = kDefaultOption,
203  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
204  void Broadcast(const Point &coordinates, float distance,
205  int32_t message_type, const Ptr<FunMessage> &message,
206  Encryption encryption, TransportProtocol protocol,
207  const FindOption &option = kDefaultOption,
208  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
209  void Broadcast(const Point &coordinates, float distance,
210  const ProtobufExtensionIdentifier &message_type,
211  const Ptr<FunMessage> &message, Encryption encryption,
212  TransportProtocol protocol,
213  const FindOption &option = kDefaultOption,
214  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
215 
216  void Broadcast(int64_t id, float distance, const string &message_type,
217  const Json &message, Encryption encryption,
218  TransportProtocol protocol,
219  const FindOption &option = kDefaultOption,
220  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
221  void Broadcast(int64_t id, float distance, const string &message_type,
222  const Ptr<FunMessage> &message, Encryption encryption,
223  TransportProtocol protocol,
224  const FindOption &option = kDefaultOption,
225  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
226  void Broadcast(int64_t id, float distance, int32_t message_type,
227  const Ptr<FunMessage> &message, Encryption encryption,
228  TransportProtocol protocol,
229  const FindOption &option = kDefaultOption,
230  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
231  void Broadcast(int64_t id, float distance,
232  const ProtobufExtensionIdentifier &message_type,
233  const Ptr<FunMessage> &message, Encryption encryption,
234  TransportProtocol protocol,
235  const FindOption &option = kDefaultOption,
236  const FindFilter &filter = kNullFilter) ASSERT_NO_ROLLBACK;
237 
238  void Broadcast(const string &message_type, const Json &message,
239  Encryption encryption,
240  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
241  void Broadcast(const string &message_type, const Ptr<FunMessage> &message,
242  Encryption encryption,
243  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
244  void Broadcast(int32_t message_type, const Ptr<FunMessage> &message,
245  Encryption encryption,
246  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
247  void Broadcast(const ProtobufExtensionIdentifier &message_type,
248  const Ptr<FunMessage> &message, Encryption encryption,
249  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
250 
251  void InsertStaticObject(const Point &coordinates,
252  const Ptr<Object> &object);
253  void FindStaticObject(int64_t id, const FindOption &option,
254  const FindFilter &filter, ObjectVector *objects,
255  DistanceVector *distances = NULL);
256  void FindStaticObject(const Point &coordinates, const FindOption &option,
257  const FindFilter &filter, ObjectVector *objects,
258  DistanceVector *distances = NULL);
259 
260  bool IsDeleted() const;
261 
262  void SetSingleThreadMode();
263 
264  private:
265  friend class PrivateAccess;
266  explicit World(const string &name, float index_block_length);
267  Impl *impl_;
268 };
269 
270 
271 class FUNAPI_DLL_VISIBILITY WorldManager {
272  public:
273  struct FUNAPI_DLL_VISIBILITY Zone {
274  string name;
275  string type;
276  World::Sphere sphere;
277  Json argument;
278  };
279  typedef std::vector<Zone> ZoneVector;
280 
281  typedef function<void(const Ptr<World> &/*world*/)> CreateCallback;
282  typedef function<void(const Rpc::PeerId &/*peer_id*/)> FindServerCallback;
283  typedef function<
284  void(const string &/*world name*/, const string &/*zone name*/,
285  const string &/*type*/, const Json &/*arguments*/, bool /*enter*/,
286  const Ptr<World::Object> &/*object*/)> ZoneHandler;
287 
288  static void Create(const string &name, const ZoneVector &zones,
289  float index_block_length, const CreateCallback &callback,
290  int64_t channel = 0) ASSERT_NO_ROLLBACK;
291  static Ptr<World> Get(const string &name, int64_t channel = 0);
292  static bool Delete(const string &name,
293  int64_t channel = 0) ASSERT_NO_ROLLBACK;
294  static void FindServer(const string &name,
295  const FindServerCallback &callback,
296  int64_t channel = 0) ASSERT_NO_ROLLBACK;
297 
298  static Ptr<World> CreateLocal(const string &name, const ZoneVector &zones,
299  float index_block_length,
300  int64_t channel = 0) ASSERT_NO_ROLLBACK;
301  static Ptr<World> GetLocal(const string &name, int64_t channel = 0);
302  static bool DeleteLocal(const string &name,
303  int64_t channel = 0) ASSERT_NO_ROLLBACK;
304 
305  static void RegisterZoneHandler(const ZoneHandler &handler);
306 };
307 
308 
309 FUNAPI_DLL_VISIBILITY
310 std::ostream &operator<<(std::ostream &out, const World::Point &point);
311 
312 FUNAPI_DLL_VISIBILITY
313 std::ostream &operator<<(std::ostream &out, const World::Sphere &sphere);
314 
315 } // namespace fun
316 
317 #endif // INCLUDE_FUNAPI_SERVICE_WORLD_H_
Definition: world.h:64
Definition: world.h:19
Definition: world.h:41
Definition: world.h:25
Definition: session.h:624
Definition: world.h:33
Definition: json.h:18
Definition: world.h:271
Definition: json.h:27
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:89
Definition: world.h:273
Definition: world.h:48