Pol  Revision:cb584c9
realm.cpp
Go to the documentation of this file.
1 
11 #include "realm.h"
12 
13 #include "../../plib/mapserver.h"
14 #include "../../plib/maptileserver.h"
15 #include "../../plib/realmdescriptor.h"
16 #include "../../plib/staticserver.h"
17 #include "../mobile/charactr.h"
18 #include "../poltype.h"
19 #include "../ufunc.h"
20 #include "../uworld.h"
21 #include "WorldChangeReasons.h"
22 
23 namespace Pol
24 {
25 namespace Realms
26 {
27 Realm::Realm( const std::string& realm_name, const std::string& realm_path )
28  : is_shadowrealm( false ),
29  shadowid( 0 ),
30  baserealm( nullptr ),
31  _descriptor( Plib::RealmDescriptor::Load( realm_name, realm_path ) ),
32  _mobile_count( 0 ),
33  _offline_count( 0 ),
34  _toplevel_item_count( 0 ),
35  _multi_count( 0 ),
36  _mapserver( Plib::MapServer::Create( _descriptor ) ),
37  _staticserver( new Plib::StaticServer( _descriptor ) ),
38  _maptileserver( new Plib::MapTileServer( _descriptor ) )
39 {
40  size_t gridwidth = grid_width();
41  size_t gridheight = grid_height();
42 
43  zone = new Core::Zone*[gridwidth];
44 
45  for ( size_t i = 0; i < gridwidth; i++ )
46  zone[i] = new Core::Zone[gridheight];
47 }
48 
49 Realm::Realm( const std::string& realm_name, Realm* realm )
50  : is_shadowrealm( true ),
51  shadowid( 0 ),
52  baserealm( realm ),
53  shadowname( realm_name ),
54  _descriptor( realm->_descriptor ),
55  _mobile_count( 0 ),
56  _offline_count( 0 ),
58  _multi_count( 0 )
59 {
60  size_t gridwidth = grid_width();
61  size_t gridheight = grid_height();
62 
63  zone = new Core::Zone*[gridwidth];
64 
65  for ( size_t i = 0; i < gridwidth; i++ )
66  zone[i] = new Core::Zone[gridheight];
67 }
68 
70 {
71  size_t gridwidth = grid_width();
72  for ( size_t i = 0; i < gridwidth; i++ )
73  delete[] zone[i];
74  delete[] zone;
75 }
76 
77 size_t Realm::sizeEstimate() const
78 {
79  size_t size = sizeof( *this );
80  size += shadowname.capacity();
81  // zone **
82  unsigned gridwidth = grid_width();
83  unsigned gridheight = grid_height();
84 
85  for ( unsigned x = 0; x < gridwidth; ++x )
86  {
87  for ( unsigned y = 0; y < gridheight; ++y )
88  {
89  size += 3 * sizeof( void** ) + zone[x][y].characters.capacity() * sizeof( void* );
90  size += 3 * sizeof( void** ) + zone[x][y].npcs.capacity() * sizeof( void* );
91  size += 3 * sizeof( void** ) + zone[x][y].items.capacity() * sizeof( void* );
92  size += 3 * sizeof( void** ) + zone[x][y].multis.capacity() * sizeof( void* );
93  }
94  }
95 
96  // estimated set footprint
97  size +=
98  3 * sizeof( void* ) + global_hulls.size() * ( sizeof( unsigned int ) + 3 * sizeof( void* ) );
99  size += _descriptor.sizeEstimate() + ( ( !_mapserver ) ? 0 : _mapserver->sizeEstimate() ) +
100  ( ( !_staticserver ) ? 0 : _staticserver->sizeEstimate() ) +
101  ( ( !_maptileserver ) ? 0 : _maptileserver->sizeEstimate() );
102  return size;
103 }
104 
105 unsigned short Realm::grid_width() const
106 {
107  return _descriptor.grid_width;
108 }
109 unsigned short Realm::grid_height() const
110 {
111  return _descriptor.grid_height;
112 }
113 
114 unsigned Realm::season() const
115 {
116  return _descriptor.season;
117 }
118 
119 bool Realm::valid( unsigned short x, unsigned short y, short z ) const
120 {
121  return ( x < width() && y < height() && z >= Core::ZCOORD_MIN && z <= Core::ZCOORD_MAX );
122 }
123 
124 const std::string Realm::name() const
125 {
126  if ( is_shadowrealm )
127  return shadowname;
128  return _descriptor.name;
129 }
130 
132 {
133  // When the movement is larger than 32 tiles, notify mobiles in the old location
134  if ( Core::pol_distance( whomoved.lastx, whomoved.lasty, whomoved.x, whomoved.y ) > 32 )
135  {
137  whomoved.lastx, whomoved.lasty, this, 32,
138  [&]( Mobile::Character* chr ) { Mobile::NpcPropagateMove( chr, &whomoved ); } );
139  }
140 
141  // Inform nearby mobiles that a movement has been made.
143  whomoved.x, whomoved.y, this, 33,
144  [&]( Mobile::Character* chr ) { Mobile::NpcPropagateMove( chr, &whomoved ); } );
145 }
146 
147 // The unhid character was already in the area and must have seen the other mobiles. So only notify
148 // the other mobiles in the region that a new one appeared.
150 {
152  whounhid.x, whounhid.y, this, 32,
153  [&]( Mobile::Character* chr ) { Mobile::NpcPropagateEnteredArea( chr, &whounhid ); } );
154 }
155 
156 // Resurrecting is just like unhiding
158 {
159  notify_unhid( whoressed );
160 }
161 
163 {
165  whoentered.x, whoentered.y, this, 32, [&]( Mobile::Character* chr ) {
166  Mobile::NpcPropagateEnteredArea( chr, &whoentered );
167  Mobile::NpcPropagateEnteredArea( &whoentered,
168  chr ); // Notify the one who entered this area about
169  // the mobiles that were already there
170  } );
171 }
172 
173 // Must be used right before a mobile leaves (before updating x and y)
175 {
177  wholeft.x, wholeft.y, this, 32,
178  [&]( Mobile::Character* chr ) { Mobile::NpcPropagateLeftArea( chr, &wholeft ); } );
179 }
180 
181 // This function will be called whenever:
182 //
183 // - a npc is created,
184 // - a npc is loaded from npcs.txt,
185 // - a player character is created,
186 // - a player character is loaded from pcs.txt,
187 // - a player character connects,
188 // - a player or npc gets moved from one realm to another
189 //
191 {
192  switch ( reason )
193  {
195  if ( !chr.logged_in() )
196  ++_offline_count;
197  break;
198 
200  ++_offline_count;
201  break;
202 
204  --_offline_count;
205  break;
206 
207  default:
208  break;
209  }
210 
211  // For some reason, characters are loaded with logged_in = true by default.
212  // This only changes AFTER they are added here in read_data()...
213  //
214  // Of course, to make life harder, when PlayerEnter calls here the logged_in is still false. Yay!
215  //
216  if ( reason != WorldChangeReason::PlayerLoad &&
217  ( reason == WorldChangeReason::PlayerEnter || chr.logged_in() ) )
218  ++_mobile_count;
219 }
220 
221 // This function will be called whenever:
222 //
223 // - a npc is killed,
224 // - a player character disconnects,
225 // - a player character is deleted
226 // - a player or npc gets moved from one realm to another
227 //
229 {
230  switch ( reason )
231  {
233  ++_offline_count;
234  break;
235 
238  if ( !chr.logged_in() )
239  {
240  --_offline_count;
241  }
242  break;
243 
244  default:
245  break;
246  }
247 
248  if ( chr.logged_in() )
249  --_mobile_count;
250 }
251 } // namespace Realms
252 } // namespace Pol
Realm(const std::string &realm_name, const std::string &realm_path="")
Definition: realm.cpp:27
void notify_resurrected(Mobile::Character &whoressed)
Definition: realm.cpp:157
bool is_shadowrealm
Definition: realm.h:64
bool logged_in() const
Definition: charactr.cpp:428
static void InRange(u16 x, u16 y, const Realms::Realm *realm, unsigned range, F &&f)
Definition: uworld.h:235
unsigned int _toplevel_item_count
Definition: realm.h:173
const std::string name() const
Definition: realm.cpp:124
unsigned int _multi_count
Definition: realm.h:174
Realm * baserealm
Definition: realm.h:66
std::unique_ptr< Plib::MapServer > _mapserver
Definition: realm.h:175
unsigned season() const
Definition: realm.cpp:114
const std::string shadowname
Definition: realm.h:67
const short ZCOORD_MIN
Definition: poltype.h:15
unsigned short height() const
Definition: realm.h:245
unsigned short grid_width() const
Definition: realm.cpp:105
void notify_entered(Mobile::Character &whoentered)
Definition: realm.cpp:162
std::unique_ptr< Plib::StaticServer > _staticserver
Definition: realm.h:176
unsigned short grid_height() const
Definition: realm.cpp:109
void notify_left(Mobile::Character &wholeft)
Definition: realm.cpp:174
void remove_mobile(const Mobile::Character &chr, WorldChangeReason reason)
Definition: realm.cpp:228
void add_mobile(const Mobile::Character &chr, WorldChangeReason reason)
Definition: realm.cpp:190
unsigned int _mobile_count
Definition: realm.h:171
size_t sizeEstimate() const
Definition: realm.cpp:77
const short ZCOORD_MAX
Definition: poltype.h:16
unsigned short pol_distance(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2)
Definition: ufunc.cpp:481
void notify_unhid(Mobile::Character &whounhid)
Definition: realm.cpp:149
std::set< unsigned int > global_hulls
Definition: realm.h:134
unsigned short width() const
Definition: realm.h:241
std::unique_ptr< Plib::MapTileServer > _maptileserver
Definition: realm.h:177
Core::Zone ** zone
Definition: realm.h:133
bool valid(unsigned short x, unsigned short y, short z) const
Definition: realm.cpp:119
unsigned int _offline_count
Definition: realm.h:172
void notify_moved(Mobile::Character &whomoved)
Definition: realm.cpp:131
Definition: berror.cpp:12
const Plib::RealmDescriptor _descriptor
Definition: realm.h:170
unsigned int shadowid
Definition: realm.h:65