iFun Engine API  1.0.0-b2601
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 std::vector<Ptr<Object> > ObjectVector;
111  typedef std::vector<Ptr<Session> > SessionVector;
112  typedef std::vector<float> DistanceVector;
113 
114  static const int64_t kInvalidId;
115  static const FindOption kDefaultOption;
116  static const FindFilter kNullFilter;
117 
118  static FindFilter MakeFilter(int64_t type_mask);
119  static FindFilter MakeRectangleFilter(float front_gap,
120  const Vector2 &direction,
121  float width,
122  float depth,
123  float height,
124  bool is_sphere = false,
125  int64_t type_mask = 0);
126  static FindFilter MakeCircularSectorFilter(float front_gap,
127  const Vector2 &direction,
128  float radius,
129  float left_angle_in_degree,
130  float right_angle_in_degree,
131  float height,
132  bool is_sphere = false,
133  int64_t type_mask = 0);
134 
135  ~World();
136 
137  const string &GetName() const;
138 
139  void MonitorTypeExistence(int64_t type,
140  const TypeExistenceCallback &callback);
141 
142  int64_t InsertObject(const Point &coordinates,
143  const Ptr<Object> &object) ASSERT_NO_ROLLBACK;
144  Ptr<Object> EraseObject(int64_t id) ASSERT_NO_ROLLBACK;
145  Ptr<Object> GetObject(int64_t id) const;
146  void GetAllObject(ObjectVector *objects) const;
147  size_t GetObjectCount() const;
148 
149  bool MoveObjectTo(int64_t id, const Point &coordinate) ASSERT_NO_ROLLBACK;
150  bool MoveObject(int64_t id, const Point &coordinates_delta,
151  Point *new_coordinates = NULL) ASSERT_NO_ROLLBACK;
152 
153  void FindObject(const Point &coordinates, float distance,
154  ObjectVector *objects);
155  void FindObject(const Point &coordinates, float distance,
156  const FindOption &option, ObjectVector *objects,
157  DistanceVector *distances = NULL);
158  void FindObject(const Point &coordinates, float distance,
159  const FindOption &option, const FindFilter &filter,
160  ObjectVector *objects, DistanceVector *distances = NULL);
161 
162  void FindObject(int64_t id, float distance, ObjectVector *objects);
163  void FindObject(int64_t id, float distance, const FindOption &option,
164  ObjectVector *objects, DistanceVector *distances = NULL);
165  void FindObject(int64_t id, float distance, const FindOption &option,
166  const FindFilter &filter, ObjectVector *objects,
167  DistanceVector *distances = NULL);
168 
169  void FindSession(const Point &coordinates, float distance,
170  SessionVector *out);
171  void FindSession(const Point &coordinates, float distance,
172  const FindOption &option, SessionVector *out);
173 
174  void FindSession(int64_t id, float distance, SessionVector *out);
175  void FindSession(int64_t id, float distance, const FindOption &option,
176  SessionVector *out);
177 
178  void Broadcast(const Point &coordinates, float distance,
179  const string &message_type, const Json &message,
180  Encryption encryption,
181  TransportProtocol protocol,
182  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
183  void Broadcast(const Point &coordinates, float distance,
184  const string &message_type, const Ptr<FunMessage> &message,
185  Encryption encryption,
186  TransportProtocol protocol,
187  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
188  void Broadcast(const Point &coordinates, float distance,
189  int32_t message_type, const Ptr<FunMessage> &message,
190  Encryption encryption,
191  TransportProtocol protocol,
192  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
193  void Broadcast(const Point &coordinates, float distance,
194  const ProtobufExtensionIdentifier &message_type,
195  const Ptr<FunMessage> &message, Encryption encryption,
196  TransportProtocol protocol,
197  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
198 
199  void Broadcast(int64_t id, float distance, const string &message_type,
200  const Json &message, Encryption encryption,
201  TransportProtocol protocol,
202  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
203  void Broadcast(int64_t id, float distance, const string &message_type,
204  const Ptr<FunMessage> &message, Encryption encryption,
205  TransportProtocol protocol,
206  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
207  void Broadcast(int64_t id, float distance, int32_t message_type,
208  const Ptr<FunMessage> &message, Encryption encryption,
209  TransportProtocol protocol,
210  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
211  void Broadcast(int64_t id, float distance,
212  const ProtobufExtensionIdentifier &message_type,
213  const Ptr<FunMessage> &message, Encryption encryption,
214  TransportProtocol protocol,
215  const FindOption &option = kDefaultOption) ASSERT_NO_ROLLBACK;
216 
217  void InsertStaticObject(const Point &coordinates,
218  const Ptr<Object> &object);
219  void FindStaticObject(int64_t id, const FindOption &option,
220  const FindFilter &filter, ObjectVector *objects,
221  DistanceVector *distances = NULL);
222  void FindStaticObject(const Point &coordinates, const FindOption &option,
223  const FindFilter &filter, ObjectVector *objects,
224  DistanceVector *distances = NULL);
225 
226  bool IsDeleted() const;
227 
228  private:
229  friend class PrivateAccess;
230  explicit World(const string &name, float index_block_length);
231  Impl *impl_;
232 };
233 
234 
236  public:
237  struct Zone {
238  string name;
239  string type;
240  World::Sphere sphere;
241  Json argument;
242  };
243  typedef std::vector<Zone> ZoneVector;
244 
245  typedef function<void(const Ptr<World> &/*world*/)> CreateCallback;
246  typedef function<void(const Rpc::PeerId &/*peer_id*/)> FindServerCallback;
247  typedef function<
248  void(const string &/*world name*/, const string &/*zone name*/,
249  const string &/*type*/, const Json &/*arguments*/, bool /*enter*/,
250  const Ptr<World::Object> &/*object*/)> ZoneHandler;
251 
252  static void Create(const string &name, const ZoneVector &zones,
253  float index_block_length, const CreateCallback &callback,
254  int64_t channel = 0) ASSERT_NO_ROLLBACK;
255  static Ptr<World> Get(const string &name, int64_t channel = 0);
256  static bool Delete(const string &name,
257  int64_t channel = 0) ASSERT_NO_ROLLBACK;
258  static void FindServer(const string &name,
259  const FindServerCallback &callback,
260  int64_t channel = 0) ASSERT_NO_ROLLBACK;
261 
262  static Ptr<World> CreateLocal(const string &name, const ZoneVector &zones,
263  float index_block_length,
264  int64_t channel = 0) ASSERT_NO_ROLLBACK;
265  static Ptr<World> GetLocal(const string &name, int64_t channel = 0);
266  static bool DeleteLocal(const string &name,
267  int64_t channel = 0) ASSERT_NO_ROLLBACK;
268 
269  static void RegisterZoneHandler(const ZoneHandler &handler);
270 };
271 
272 
273 std::ostream &operator<<(std::ostream &out, const World::Point &point);
274 std::ostream &operator<<(std::ostream &out, const World::Sphere &sphere);
275 
276 } // namespace fun
277 
278 #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:561
Definition: world.h:32
Definition: world.h:235
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:237
Definition: world.h:47