10 #include "../bscript/berror.h" 11 #include "../clib/cfgelem.h" 12 #include "../clib/cfgfile.h" 13 #include "../clib/fileutil.h" 14 #include "../clib/logfacility.h" 15 #include "../clib/passert.h" 16 #include "../clib/random.h" 17 #include "../clib/streamsaver.h" 18 #include "../plib/maptile.h" 19 #include "../plib/systemstate.h" 37 units_per_area_( elem.remove_ulong(
"UnitsPerArea" ) ),
38 seconds_per_regrow_( elem.remove_ulong(
"SecondsPerRegrow" ) ),
41 capacity_( elem.remove_ulong(
"Capacity" ) ),
60 unsigned int xy = ( x << 16 ) | y;
65 units -= ( *itr ).second;
75 unsigned int this_difficulty =
Clib::random_int( ( base_difficulty * 2 ) - 1 );
81 unsigned int xy = ( x << 16 ) | y;
86 avail_units -= ( *itr ).second;
89 int a = avail_units / n;
92 short na =
static_cast<short>( n * a );
95 ( *itr ).second += na;
106 unsigned int diff =
static_cast<unsigned int>( now -
last_regen_ );
107 unsigned int n_regrow;
130 Depletions::iterator cur = itr;
132 if ( n_regrow >= ( *cur ).second )
135 ( *cur ).second -= static_cast<unsigned short>( n_regrow );
175 if ( realm->
findstatic( static_cast<unsigned short>( x ), static_cast<unsigned short>( y ),
176 static_cast<unsigned short>( objtype ) ) )
183 realm->
getmaptile( static_cast<unsigned short>( x ), static_cast<unsigned short>( y ) );
184 return ( cell.
landtile == objtype );
190 unsigned int tilecount = 0;
192 std::vector<Realms::Realm*>::iterator itr;
195 for (
unsigned short x = 0; x < ( *itr )->width(); ++x )
197 for (
unsigned short y = 0; y < ( *itr )->height(); ++y )
212 for (
unsigned i = 0; i <
regions_.size(); ++i )
221 for (
unsigned i = 0; i <
regions_.size(); ++i )
246 ( *itr ).second->regenerate( now );
256 ( *itr ).second->counttiles();
272 if ( rgn ==
nullptr )
292 if ( rgn ==
nullptr )
308 if ( rgn ==
nullptr )
324 return ( *itr ).second;
331 std::string filename = std::string(
"regions/" ) + resource + std::string(
".cfg" );
335 std::unique_ptr<ResourceDef> rd(
new ResourceDef( resource ) );
337 while ( cf.read( elem ) )
339 if ( elem.type_is(
"global" ) )
341 rd->read_config( elem );
345 rd->read_region( elem );
357 INFO_PRINT <<
"File regions/resource.cfg not found, skipping.\n";
363 while ( cf.
read( elem ) )
365 std::string resourcename;
366 while ( elem.
remove_prop(
"ResourceType", &resourcename ) )
381 ERROR_PRINT <<
"Error reading RESOURCE.DAT: Unable to find resource type " << elem.
rest()
383 throw std::runtime_error(
"Data file error" );
393 ERROR_PRINT <<
"Error reading RESOURCE.DAT: Unable to find resource type " << elem.
rest()
395 throw std::runtime_error(
"Data file error" );
399 if ( rgn ==
nullptr )
401 ERROR_PRINT <<
"Error reading RESOURCE.DAT: Unable to find region " << regionname
402 <<
" in resource " << elem.
rest() <<
"\n";
403 throw std::runtime_error(
"Data file error" );
414 Clib::ConfigFile cf( resourcefile,
"GLOBALRESOURCEPOOL REGIONALRESOURCEPOOL" );
416 while ( cf.
read( elem ) )
418 if ( elem.
type_is(
"GlobalResourcePool" ) )
432 sw() <<
"GlobalResourcePool " <<
name() <<
'\n' 438 for (
unsigned i = 0; i <
regions_.size(); ++i )
450 + 3 *
sizeof(
void* ) +
451 landtiles_.size() * (
sizeof(
unsigned short ) + 3 *
sizeof(
void* ) ) + 3 *
sizeof(
void* ) +
452 tiles_.size() * (
sizeof(
unsigned short ) + 3 *
sizeof(
void* ) );
459 sw() <<
"RegionalResourcePool " << resource_name <<
'\n' 461 <<
"\tName\t" <<
name_ <<
'\n' 462 <<
"\tUnits\t" << units_ <<
'\n' 463 <<
"#\t(regions/" << resource_name <<
".cfg: Capacity is " << capacity_ <<
")" <<
'\n' 473 5 *
sizeof(
unsigned int )
475 + (
sizeof(
unsigned int ) +
sizeof(
unsigned short ) + (
sizeof(
void* ) * 3 + 1 ) / 2 ) *
484 resdef.second->write( sw_resource );
492 delete resdef.second;
void read_global_data(Clib::ConfigElem &elem)
void read_region_data(RegionGroupBase &grp, const char *preferred_filename, const char *other_filename, const char *tags_expected)
void write(Clib::StreamWriter &sw, const std::string &resource_name) const
void produce(unsigned amount)
std::string remove_string(const char *propname)
Bscript::BObjectImp * get_region_string(const std::string &propname)
void read_resource_cfg(const char *resource)
Bscript::BObjectImp * harvest_resource(const char *resource, xcoord x, ycoord y, Realms::Realm *realm, int b, int n)
std::string world_data_path
std::vector< Region * > regions_
void regenerate(time_t now)
unsigned int remove_ulong(const char *propname)
ResourceDefs resourcedefs
#define THREAD_CHECKPOINT(thread, check)
void consume(unsigned amount)
Bscript::BObjectImp * get_harvest_difficulty(xcoord x, ycoord y, Realms::Realm *realm)
[1] Harvest Difficulty
bool findstatic(unsigned short x, unsigned short y, unsigned short objtype) const
virtual ResourceRegion * getregion(xcoord x, ycoord y, Realms::Realm *realm)
ResourceDef(const char *name)
const unsigned int units_per_area_
unsigned int initial_units_
void read_data(Clib::ConfigElem &elem)
void read_data(Clib::ConfigElem &elem)
unsigned short getgraphic(u32 objtype)
void write_resources_dat(Clib::StreamWriter &sw_resource)
const char * rest() const
virtual size_t estimateSize() const
void read_config(Clib::ConfigElem &elem)
void read_resources_dat()
void write(Clib::StreamWriter &sw) const
virtual size_t estimateSize() const POL_OVERRIDE
Bscript::BObjectImp * harvest_resource(xcoord x, ycoord y, int b, int n)
std::vector< Realms::Realm * > Realms
virtual size_t estimateSize() const
void regenerate(time_t now)
const std::string & name() const
ResourceDef * find_resource_def(const std::string &rname)
std::set< unsigned short > landtiles_
std::set< unsigned short > tiles_
const unsigned int seconds_per_regrow_
bool remove_prop(const char *propname, std::string *value)
void count_resource_tiles()
bool type_is(const char *name) const
Bscript::BObjectImp * get_region_string(const char *resource, xcoord x, ycoord y, Realms::Realm *realm, const std::string &propname)
ResourceRegion(Clib::ConfigElem &elem, RegionId id)
Resource Management.
const std::string & name() const
virtual size_t estimateSize() const POL_OVERRIDE
bool FileExists(const char *filename)
bool read(ConfigElem &elem)
Bscript::BObjectImp * get_harvest_difficulty(const char *resource, xcoord x, ycoord y, Realms::Realm *realm, unsigned short marker)
Plib::MAPTILE_CELL getmaptile(unsigned short x, unsigned short y) const
bool findmarker(xcoord x, ycoord y, Realms::Realm *realm, unsigned int objtype)