Pol  Revision:cb584c9
miscrgn.cpp
Go to the documentation of this file.
1 
9 #include "miscrgn.h"
10 
11 #include <stddef.h>
12 #include <tuple>
13 
14 #include "../clib/cfgelem.h"
15 #include "../clib/rawtypes.h"
16 #include "globals/uvars.h"
17 #include "realms/realm.h"
18 #include "ufunc.h"
19 
20 namespace Pol
21 {
22 namespace Core
23 {
25  : Region( elem, id ), nocast_( elem.remove_bool( "nocast", false ) )
26 {
27 }
28 
30 {
31  return base::estimateSize() + sizeof( bool ); /*nocast_*/
32 }
33 
35 {
36  gamestate.nocastdef = new NoCastDef( "nocast" );
37  read_region_data( *gamestate.nocastdef, "regions/nocast.cfg", "regions/regions.cfg",
38  "NoCastRegion Region" );
39 }
40 
42  : Region( elem, id ), lightlevel( elem.remove_ushort( "LightLevel", 0 ) )
43 {
44 }
45 
47 {
48  return base::estimateSize() + sizeof( unsigned ); /*lightlevel*/
49 }
50 
52 {
53  gamestate.lightdef = new LightDef( "light" );
54  read_region_data( *gamestate.lightdef, "regions/light.cfg", "regions/regions.cfg",
55  "LightRegion Region" );
56 }
57 
58 
60  : Region( elem, id ),
61  weathertype( static_cast<u8>( elem.remove_ushort(
62  "WeatherType", 255 ) ) ), // dave changed 6/30/03, 255 is no weather, not 0
63  severity( static_cast<u8>( elem.remove_ushort( "WeatherSeverity", 0 ) ) ),
64  aux( static_cast<u8>( elem.remove_ushort( "WeatherAux", 0 ) ) ),
65  lightoverride( elem.remove_int( "LightOverride", -1 ) )
66 {
67 }
68 
70 {
71  return base::estimateSize() + 3 * sizeof( unsigned char ) /*weathertype severity aux*/
72  + sizeof( int ); /*lightoverride*/
73 }
74 
76 {
77  for ( auto const& realm : gamestate.Realms )
78  {
79  unsigned int gridwidth = realm->width() / ZONE_SIZE;
80  unsigned int gridheight = realm->height() / ZONE_SIZE;
81 
82  RegionId** zone = new RegionId*[gridwidth];
83 
84  for ( unsigned int i = 0; i < gridwidth; i++ )
85  {
86  zone[i] = new RegionId[gridheight];
87  for ( unsigned int j = 0; j < gridheight; j++ )
88  {
89  zone[i][j] = 0;
90  }
91  }
92  default_regionrealms.insert( std::make_pair( realm, zone ) );
93  }
94 }
95 
97 {
98  for ( auto& realmregion : default_regionrealms )
99  {
100  unsigned int gridwidth = realmregion.first->width() / ZONE_SIZE;
101 
102  for ( unsigned int i = 0; i < gridwidth; i++ )
103  delete[] realmregion.second[i];
104  delete[] realmregion.second;
105  }
106 }
107 
109 {
111 
112  for ( const auto& realm : default_regionrealms )
113  {
114  unsigned int gridwidth = realm.first->width() / ZONE_SIZE;
115  size +=
116  gridwidth * sizeof( RegionId ) + sizeof( Realms::Realm* ) + ( sizeof( void* ) * 3 + 1 ) / 2;
117  }
118  return size;
119 }
120 
122 {
123  for ( auto& realmregion : regionrealms )
124  {
125  Realms::Realm* realm = realmregion.first;
126  unsigned int gridwidth = realm->width() / ZONE_SIZE;
127  unsigned int gridheight = realm->height() / ZONE_SIZE;
128 
129  for ( unsigned int i = 0; i < gridwidth; i++ )
130  {
131  for ( unsigned int j = 0; j < gridheight; j++ )
132  {
133  default_regionrealms[realm][i][j] = regionrealms[realm][i][j];
134  }
135  }
136  }
137 }
138 
139 bool WeatherDef::assign_zones_to_region( const char* regionname, unsigned short xwest,
140  unsigned short ynorth, unsigned short xeast,
141  unsigned short ysouth, Realms::Realm* realm )
142 {
143  if ( xwest >= realm->width() )
144  xwest = static_cast<unsigned short>( realm->width() ) - 1;
145  if ( xeast >= realm->width() )
146  xeast = static_cast<unsigned short>( realm->width() ) - 1;
147  if ( ynorth >= realm->height() )
148  ynorth = static_cast<unsigned short>( realm->height() ) - 1;
149  if ( ysouth >= realm->height() )
150  ysouth = static_cast<unsigned short>( realm->height() ) - 1;
151 
152 
153  if ( regionname && regionname[0] )
154  {
155  Region* rgn = getregion_byname( regionname );
156  if ( rgn == nullptr )
157  return false;
158 
159  unsigned zone_xwest, zone_ynorth, zone_xeast, zone_ysouth;
160 
161  std::tie( zone_xwest, zone_ynorth ) = XyToZone( xwest, ynorth );
162  std::tie( zone_xeast, zone_ysouth ) = XyToZone( xeast, ysouth );
163 
164  for ( auto zx = zone_xwest; zx <= zone_xeast; ++zx )
165  {
166  for ( auto zy = zone_ynorth; zy <= zone_ysouth; ++zy )
167  {
168  regionrealms[realm][zx][zy] = rgn->regionid();
169  }
170  }
171  }
172  else // move 'em back to the default
173  {
174  unsigned zone_xwest, zone_ynorth, zone_xeast, zone_ysouth;
175  std::tie( zone_xwest, zone_ynorth ) = XyToZone( xwest, ynorth );
176  std::tie( zone_xeast, zone_ysouth ) = XyToZone( xeast, ysouth );
177 
178  for ( auto zx = zone_xwest; zx <= zone_xeast; ++zx )
179  {
180  for ( auto zy = zone_ynorth; zy <= zone_ysouth; ++zy )
181  {
182  regionrealms[realm][zx][zy] = default_regionrealms[realm][zx][zy];
183  }
184  }
185  }
187  return true;
188 }
189 
191 {
192  gamestate.weatherdef = new WeatherDef( "weather" );
193  read_region_data( *gamestate.weatherdef, "regions/weather.cfg", "regions/regions.cfg",
194  "WeatherRegion Region" );
195 
197 }
198 } // namespace Core
199 } // namespace Pol
virtual size_t estimateSize() const POL_OVERRIDE
Definition: miscrgn.cpp:69
void read_region_data(RegionGroupBase &grp, const char *preferred_filename, const char *other_filename, const char *tags_expected)
Definition: region.cpp:225
virtual size_t estimateSize() const POL_OVERRIDE
Definition: miscrgn.cpp:108
unsigned char u8
Definition: rawtypes.h:25
bool assign_zones_to_region(const char *regionname, unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, Realms::Realm *realm)
Definition: miscrgn.cpp:139
RegionId regionid() const
Definition: region.h:65
LightRegion(Clib::ConfigElem &elem, RegionId id)
Definition: miscrgn.cpp:41
void update_all_weatherregions()
Definition: ufunc.cpp:1761
void read_light_zones()
Definition: miscrgn.cpp:51
virtual ~WeatherDef()
Definition: miscrgn.cpp:96
int lightlevel(unsigned short x, unsigned short y)
void read_nocast_zones()
Definition: miscrgn.cpp:34
std::pair< unsigned, unsigned > XyToZone(xcoord x, ycoord y)
Definition: zone.cpp:13
LightDef * lightdef
Definition: uvars.h:150
void read_weather_zones()
Definition: miscrgn.cpp:190
virtual size_t estimateSize() const POL_OVERRIDE
Definition: miscrgn.cpp:29
const unsigned ZONE_SIZE
Definition: zone.h:18
virtual size_t estimateSize() const
Definition: region.cpp:201
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
WeatherDef(const char *name)
Definition: miscrgn.cpp:75
NoCastDef * nocastdef
Definition: uvars.h:149
Region * getregion_byname(const std::string &regionname)
Definition: region.cpp:161
GameState gamestate
Definition: uvars.cpp:74
NoCastRegion(Clib::ConfigElem &elem, RegionId id)
Definition: miscrgn.cpp:24
unsigned short RegionId
Definition: zone.h:23
WeatherRegion(Clib::ConfigElem &elem, RegionId id)
Definition: miscrgn.cpp:59
RegionGroup< LightRegion > LightDef
Definition: uvars.h:95
unsigned short width() const
Definition: realm.h:241
const std::string & name() const
Definition: region.h:60
RegionGroup< NoCastRegion > NoCastDef
Definition: uvars.h:94
WeatherDef * weatherdef
Definition: uvars.h:151
virtual size_t estimateSize() const POL_OVERRIDE
Definition: miscrgn.cpp:46
Definition: berror.cpp:12
RegionRealms default_regionrealms
Definition: miscrgn.h:87
void copy_default_regions()
Definition: miscrgn.cpp:121