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