Pol  Revision:cb584c9
region.cpp
Go to the documentation of this file.
1 
8 #include "region.h"
9 
10 #include <stddef.h>
11 #include <string>
12 #include <tuple>
13 
14 #include "../bscript/berror.h"
15 #include "../bscript/impstr.h"
16 #include "../clib/cfgelem.h"
17 #include "../clib/cfgfile.h"
18 #include "../clib/fileutil.h"
19 #include "../clib/stlutil.h"
20 #include "globals/uvars.h"
21 #include "poltype.h"
22 #include "proplist.h"
23 #include "realms.h"
24 #include "realms/realm.h"
25 #include "zone.h"
26 
27 namespace Pol
28 {
29 namespace Core
30 {
32  : name_( elem.rest() ), regionid_( id ), proplist_( Core::CPropProfiler::Type::REGION )
33 {
34 }
35 
37 
38 size_t Region::estimateSize() const
39 {
40  return name_.capacity() + sizeof( RegionId ) + proplist_.estimatedSize();
41 }
42 
44 {
46 }
47 
48 Bscript::BObjectImp* Region::get_region_string( const std::string& propname )
49 {
50  std::string propvalue;
51  if ( proplist_.getprop( propname, propvalue ) )
52  {
53  return new Bscript::String( propvalue );
54  }
55  else
56  {
57  return new Bscript::BError( "Property not found" );
58  }
59 }
60 
61 
63 {
64  // memset( &regionidx_, 0, sizeof regionidx_ );
65  for ( const auto& realm : gamestate.Realms )
66  {
67  unsigned int gridwidth = realm->width() / ZONE_SIZE;
68  unsigned int gridheight = realm->height() / ZONE_SIZE;
69 
70  RegionId** zone = new RegionId*[gridwidth];
71 
72  for ( unsigned int i = 0; i < gridwidth; i++ )
73  {
74  zone[i] = new RegionId[gridheight];
75  for ( unsigned int j = 0; j < gridheight; j++ )
76  {
77  zone[i][j] = 0;
78  }
79  }
80  regionrealms.insert( std::make_pair( realm, zone ) );
81  }
82 }
84 {
85  for ( auto& realm : regionrealms )
86  {
87  unsigned int gridwidth = realm.first->width() / ZONE_SIZE;
88 
89  // Tokuno-Fix removed Turley, 2009/09/08 (for ZONE_SIZE 4 not needed)
90  /*if (gridwidth * ZONE_SIZE < itr->first->width())
91  gridwidth++;*/
92 
93  for ( unsigned int i = 0; i < gridwidth; i++ )
94  delete[] realm.second[i];
95  delete[] realm.second;
96  }
97 
98  // cleans the regions_ vector...
99  for ( auto& region : regions_ )
100  {
101  delete region;
102  }
103  regions_.clear();
104 }
105 
107 {
108  std::string zonestr, strrealm;
109 
110  strrealm = elem.remove_string( "Realm", "britannia" );
111  Realms::Realm* realm = find_realm( strrealm );
112  if ( !realm )
113  elem.throw_error( "Realm not found" );
114  while ( elem.remove_prop( "Range", &zonestr ) )
115  {
116  unsigned short xwest, ynorth, xeast, ysouth;
117  ISTRINGSTREAM is( zonestr );
118  if ( is >> xwest >> ynorth >> xeast >> ysouth )
119  {
120  if ( xeast >= realm->width() || ysouth >= realm->height() )
121  {
122  elem.throw_error( "Zone range is out of bounds for realm" );
123  }
124  unsigned zone_xwest, zone_ynorth, zone_xeast, zone_ysouth;
125  std::tie( zone_xwest, zone_ynorth ) = XyToZone( xwest, ynorth );
126  std::tie( zone_xeast, zone_ysouth ) = XyToZone( xeast, ysouth );
127  unsigned zx, zy;
128  for ( zx = zone_xwest; zx <= zone_xeast; ++zx )
129  {
130  for ( zy = zone_ynorth; zy <= zone_ysouth; ++zy )
131  {
132  /*
133  if (zones[zx][zy].regions[regiontype])
134  {
135  elem.throw_error( "Zone is already allocated" );
136  }
137  */
138  regionrealms[realm][zx][zy] = ridx;
139  }
140  }
141  }
142  else
143  {
144  elem.throw_error( "Poorly formed zone range: " + zonestr );
145  }
146  }
147 }
148 
150 {
151  unsigned zx, zy;
152  std::tie( zx, zy ) = XyToZone( x, y );
153  RegionId** regiongrp;
154  if ( realm->is_shadowrealm )
155  regiongrp = regionrealms[realm->baserealm];
156  else
157  regiongrp = regionrealms[realm];
158  return regiongrp[zx][zy];
159 }
160 
161 Region* RegionGroupBase::getregion_byname( const std::string& regionname )
162 {
163  RegionsByName::iterator itr = regions_byname_.find( regionname );
164  if ( itr == regions_byname_.end() )
165  return nullptr;
166  else
167  return ( *itr ).second;
168 }
169 
171 {
172  RegionId ridx = getregionid( x, y, realm );
173 
174  // dave 12-22 return null if no regions, don't throw
175  std::vector<Region*>::iterator itr = regions_.begin();
176  if ( ( itr += ridx ) >= regions_.end() )
177  return nullptr;
178  else
179  return regions_[ridx];
180 }
181 
183 {
184  Region* rgn = create_region( elem, static_cast<RegionId>( regions_.size() ) );
185  regions_.push_back( rgn );
186  regions_byname_.insert( RegionsByName::value_type( elem.rest(), rgn ) );
187 
188  paint_zones( elem, static_cast<RegionId>( regions_.size() - 1 ) );
189  rgn->read_custom_config( elem );
190 }
191 
193 {
194  Region* rgn = create_region( elem, static_cast<RegionId>( regions_.size() ) );
195 
196  regions_.push_back( rgn );
197  regions_byname_.insert( RegionsByName::value_type( "_background_", rgn ) );
198  rgn->read_custom_config( elem );
199 }
200 
202 {
203  size_t size = 0;
204  for ( const auto& region : regions_ )
205  {
206  size += region->estimateSize();
207  }
208  for ( const auto& realm : regionrealms )
209  {
210  if ( realm.first != nullptr )
211  {
212  unsigned int gridwidth = realm.first->width() / ZONE_SIZE;
213  size += gridwidth * sizeof( RegionId ) + sizeof( Realms::Realm* ) +
214  ( sizeof( void* ) * 3 + 1 ) / 2;
215  }
216  }
217  size += name_.capacity();
218  for ( const auto& realm : regions_byname_ )
219  {
220  size += realm.first.capacity() + sizeof( Region* ) + ( sizeof( void* ) * 3 + 1 ) / 2;
221  }
222  return size;
223 }
224 
225 void read_region_data( RegionGroupBase& grp, const char* preferred_filename,
226  const char* other_filename, const char* tags_expected )
227 {
228  const char* filename;
229  if ( Clib::FileExists( preferred_filename ) )
230  filename = preferred_filename;
231  else
232  filename = other_filename;
233 
234  if ( Clib::FileExists( filename ) )
235  {
236  Clib::ConfigFile cf( filename, tags_expected );
237  Clib::ConfigElem elem;
238 
239  grp.create_bgnd_region( elem );
240 
241  while ( cf.read( elem ) )
242  {
243  grp.read_region( elem );
244  }
245  }
246 }
247 } // namespace Core
248 } // namespace Pol
void read_region_data(RegionGroupBase &grp, const char *preferred_filename, const char *other_filename, const char *tags_expected)
Definition: region.cpp:225
Region * getregion_byloc(xcoord x, ycoord y, Realms::Realm *realm)
Definition: region.cpp:170
std::string remove_string(const char *propname)
Definition: cfgfile.cpp:381
virtual ~Region()
Definition: region.cpp:36
RegionsByName regions_byname_
Definition: region.h:98
Bscript::BObjectImp * get_region_string(const std::string &propname)
Definition: region.cpp:48
unsigned short ycoord
Definition: poltype.h:20
std::vector< Region * > regions_
Definition: region.h:86
void read_custom_config(Clib::ConfigElem &elem)
Definition: region.cpp:43
virtual Region * create_region(Clib::ConfigElem &elem, RegionId id) const =0
size_t estimatedSize() const
Definition: proplist.cpp:362
bool is_shadowrealm
Definition: realm.h:64
std::pair< unsigned, unsigned > XyToZone(xcoord x, ycoord y)
Definition: zone.cpp:13
RegionId getregionid(xcoord x, ycoord y, Realms::Realm *realm)
Definition: region.cpp:149
const char * rest() const
Definition: cfgfile.cpp:71
void readRemainingPropertiesAsStrings(Clib::ConfigElem &elem)
Definition: proplist.cpp:487
const unsigned ZONE_SIZE
Definition: zone.h:18
virtual size_t estimateSize() const
Definition: region.cpp:201
POL_NORETURN void throw_error(const std::string &errmsg) const
Definition: cfgfile.cpp:285
Realm * baserealm
Definition: realm.h:66
std::vector< Realms::Realm * > Realms
Definition: uvars.h:163
virtual size_t estimateSize() const
Definition: region.cpp:38
unsigned short height() const
Definition: realm.h:245
RegionRealms regionrealms
Definition: region.h:89
Region * getregion_byname(const std::string &regionname)
Definition: region.cpp:161
GameState gamestate
Definition: uvars.cpp:74
bool remove_prop(const char *propname, std::string *value)
Definition: cfgfile.cpp:128
#define ISTRINGSTREAM
Definition: stlutil.h:73
unsigned short RegionId
Definition: zone.h:23
unsigned short width() const
Definition: realm.h:241
bool getprop(const std::string &propname, std::string &propvalue) const
Definition: proplist.cpp:371
Realms::Realm * find_realm(const std::string &name)
Definition: realms.cpp:64
PropertyList proplist_
Definition: region.h:57
virtual ~RegionGroupBase()
Definition: region.cpp:83
std::string name_
Definition: region.h:50
const std::string & name() const
Definition: region.h:60
RegionGroupBase(const char *name)
Definition: region.cpp:62
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
void paint_zones(Clib::ConfigElem &elem, RegionId ridx)
Definition: region.cpp:106
bool read(ConfigElem &elem)
Definition: cfgfile.cpp:1015
Definition: berror.cpp:12
void create_bgnd_region(Clib::ConfigElem &elem)
Definition: region.cpp:192
void read_region(Clib::ConfigElem &elem)
Definition: region.cpp:182
unsigned short xcoord
Definition: poltype.h:19
Region(Clib::ConfigElem &elem, RegionId id)
Definition: region.cpp:31