Skip to content

Commit

Permalink
Regions Pos class refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
turleypol committed Sep 12, 2021
1 parent 95783a3 commit 8503e72
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 229 deletions.
10 changes: 4 additions & 6 deletions pol-core/pol/mobile/charactr.cpp
Expand Up @@ -3484,7 +3484,7 @@ void Character::check_light_region_change()
else
{
// dave 12-22 check for no regions
Core::LightRegion* light_region = Core::gamestate.lightdef->getregion( x(), y(), realm() );
Core::LightRegion* light_region = Core::gamestate.lightdef->getregion( pos() );
if ( light_region != nullptr )
newlightlevel = light_region->lightlevel;
else
Expand All @@ -3501,8 +3501,7 @@ void Character::check_light_region_change()
void Character::check_justice_region_change()
{
Core::JusticeRegion* cur_justice_region = client->gd->justice_region;
Core::JusticeRegion* new_justice_region =
Core::gamestate.justicedef->getregion( x(), y(), client->chr->realm() );
Core::JusticeRegion* new_justice_region = Core::gamestate.justicedef->getregion( pos() );

if ( cur_justice_region != new_justice_region )
{
Expand Down Expand Up @@ -3568,7 +3567,7 @@ void Character::check_justice_region_change()
void Character::check_music_region_change()
{
Core::MusicRegion* cur_music_region = client->gd->music_region;
Core::MusicRegion* new_music_region = Core::gamestate.musicdef->getregion( x(), y(), realm() );
Core::MusicRegion* new_music_region = Core::gamestate.musicdef->getregion( pos() );

// may want to consider changing every n minutes, too, even if region didn't change
if ( cur_music_region != new_music_region )
Expand All @@ -3590,8 +3589,7 @@ void Character::check_weather_region_change( bool force ) // dave changed 5/26/
// changed type/intensity
{
Core::WeatherRegion* cur_weather_region = client->gd->weather_region;
Core::WeatherRegion* new_weather_region =
Core::gamestate.weatherdef->getregion( x(), y(), realm() );
Core::WeatherRegion* new_weather_region = Core::gamestate.weatherdef->getregion( pos() );

// eric 5/31/03: I don't think this is right. it's possible to go from somewhere that has no
// weather region,
Expand Down
103 changes: 31 additions & 72 deletions pol-core/pol/module/uomod.cpp
Expand Up @@ -3368,24 +3368,17 @@ BObjectImp* UOExecutorModule::mf_SetRegionWeatherLevel()
BObjectImp* UOExecutorModule::mf_AssignRectToWeatherRegion()
{
const String* region_name_str;
unsigned short xwest, ynorth, xeast, ysouth;
const String* strrealm;
Pos2d nw, se;
Realms::Realm* realm;

if ( !( getStringParam( 0, region_name_str ) && getParam( 1, xwest ) && getParam( 2, ynorth ) &&
getParam( 3, xeast ) && getParam( 4, ysouth ) && getStringParam( 5, strrealm ) ) )
if ( !( getStringParam( 0, region_name_str ) && getRealmParam( 5, &realm ) &&
getPos2dParam( 1, 2, &nw, realm ) && getPos2dParam( 3, 4, &se, realm ) ) )
{
return new BError( "Invalid Parameter type" );
}
Realms::Realm* realm = find_realm( strrealm->value() );
if ( !realm )
return new BError( "Realm not found" );
if ( !realm->valid( xwest, ynorth, 0 ) )
return new BError( "Invalid Coordinates for realm" );
if ( !realm->valid( xeast, ysouth, 0 ) )
return new BError( "Invalid Coordinates for realm" );

bool res = gamestate.weatherdef->assign_zones_to_region( region_name_str->data(), xwest, ynorth,
xeast, ysouth, realm );
bool res = gamestate.weatherdef->assign_zones_to_region( region_name_str->data(),
Range2d( nw, se, nullptr ), realm );
if ( res )
return new BLong( 1 );
else
Expand Down Expand Up @@ -3865,21 +3858,13 @@ BObjectImp* UOExecutorModule::mf_RestartScript()
BObjectImp* UOExecutorModule::mf_GetHarvestDifficulty()
{
const String* resource;
xcoord x;
ycoord y;
unsigned short tiletype;
const String* strrealm;

if ( getStringParam( 0, resource ) && getParam( 1, x ) && getParam( 2, y ) &&
getParam( 3, tiletype ) && getStringParam( 4, strrealm ) )
Pos2d pos;
Realms::Realm* realm;
if ( getStringParam( 0, resource ) && getRealmParam( 4, &realm ) && getPos2dParam( 1, 2, &pos ) &&
getParam( 3, tiletype ) )
{
Realms::Realm* realm = find_realm( strrealm->value() );
if ( !realm )
return new BError( "Realm not found" );
if ( !realm->valid( x, y, 0 ) )
return new BError( "Invalid Coordinates for realm" );

return get_harvest_difficulty( resource->data(), x, y, realm, tiletype );
return get_harvest_difficulty( resource->data(), Pos4d( pos, 0, realm ), tiletype );
}
else
{
Expand All @@ -3889,25 +3874,18 @@ BObjectImp* UOExecutorModule::mf_GetHarvestDifficulty()

BObjectImp* UOExecutorModule::mf_HarvestResource()
{
xcoord x;
ycoord y;
Pos2d pos;
Realms::Realm* realm;
const String* resource;
int b;
int n;
const String* strrealm;

if ( getStringParam( 0, resource ) && getParam( 1, x ) && getParam( 2, y ) && getParam( 3, b ) &&
getParam( 4, n ) && getStringParam( 5, strrealm ) )
if ( getStringParam( 0, resource ) && getRealmParam( 5, &realm ) &&
getPos2dParam( 1, 2, &pos, realm ) && getParam( 3, b ) && getParam( 4, n ) )
{
Realms::Realm* realm = find_realm( strrealm->value() );
if ( !realm )
return new BError( "Realm not found" );
if ( !realm->valid( x, y, 0 ) )
return new BError( "Invalid Coordinates for realm" );

if ( b <= 0 )
return new BError( "b must be >= 0" );
return harvest_resource( resource->data(), x, y, realm, b, n );
return harvest_resource( resource->data(), Pos4d( pos, 0, realm ), b, n );
}
else
{
Expand All @@ -3932,10 +3910,10 @@ BObjectImp* UOExecutorModule::mf_GetRegionName( /* objref */ )
if ( chr->logged_in() )
justice_region = chr->client->gd->justice_region;
else
justice_region = gamestate.justicedef->getregion( chr->x(), chr->y(), chr->realm() );
justice_region = gamestate.justicedef->getregion( chr->pos() );
}
else
justice_region = gamestate.justicedef->getregion( obj->x(), obj->y(), obj->realm() );
justice_region = gamestate.justicedef->getregion( obj->pos() );

if ( justice_region == nullptr )
return new BError( "No Region defined at this Location" );
Expand All @@ -3948,18 +3926,11 @@ BObjectImp* UOExecutorModule::mf_GetRegionName( /* objref */ )

BObjectImp* UOExecutorModule::mf_GetRegionNameAtLocation( /* x, y, realm */ )
{
unsigned short x, y;
const String* strrealm;

if ( getParam( 0, x ) && getParam( 1, y ) && getStringParam( 2, strrealm ) )
Pos2d pos;
Realms::Realm* realm;
if ( getRealmParam( 2, &realm ) && getPos2dParam( 0, 1, &pos ) )
{
Realms::Realm* realm = find_realm( strrealm->value() );
if ( !realm )
return new BError( "Realm not found" );
if ( !realm->valid( x, y, 0 ) )
return new BError( "Invalid Coordinates for realm" );

JusticeRegion* justice_region = gamestate.justicedef->getregion( x, y, realm );
JusticeRegion* justice_region = gamestate.justicedef->getregion( Pos4d( pos, 0, realm ) );
if ( justice_region == nullptr )
return new BError( "No Region defined at this Location" );
else
Expand All @@ -3972,20 +3943,13 @@ BObjectImp* UOExecutorModule::mf_GetRegionNameAtLocation( /* x, y, realm */ )
BObjectImp* UOExecutorModule::mf_GetRegionString()
{
const String* resource;
unsigned short x, y;
const String* propname;
const String* strrealm;

if ( getStringParam( 0, resource ) && getParam( 1, x ) && getParam( 2, y ) &&
getStringParam( 3, propname ) && getStringParam( 4, strrealm ) )
Pos2d pos;
Realms::Realm* realm;
if ( getStringParam( 0, resource ) && getRealmParam( 4, &realm ) && getPos2dParam( 1, 2, &pos ) &&
getStringParam( 3, propname ) )
{
Realms::Realm* realm = find_realm( strrealm->value() );
if ( !realm )
return new BError( "Realm not found" );
if ( !realm->valid( x, y, 0 ) )
return new BError( "Invalid Coordinates for realm" );

return get_region_string( resource->data(), x, y, realm, propname->value() );
return get_region_string( resource->data(), Pos4d( pos, 0, realm ), propname->value() );
}
else
{
Expand All @@ -3995,17 +3959,12 @@ BObjectImp* UOExecutorModule::mf_GetRegionString()

BObjectImp* UOExecutorModule::mf_GetRegionLightLevelAtLocation( /* x, y, realm */ )
{
unsigned short x, y;
const String* strrealm;
Pos2d pos;
Realms::Realm* realm;

if ( getParam( 0, x ) && getParam( 1, y ) && getStringParam( 2, strrealm ) )
if ( getRealmParam( 2, &realm ) && getPos2dParam( 0, 1, &pos ) )
{
Realms::Realm* realm = find_realm( strrealm->value() );
if ( !realm )
return new BError( "Realm not found" );
if ( !realm->valid( x, y, 0 ) )
return new BError( "Invalid Coordinates for realm" );
LightRegion* light_region = gamestate.lightdef->getregion( x, y, realm );
LightRegion* light_region = gamestate.lightdef->getregion( Pos4d( pos, 0, realm ) );
int lightlevel;
if ( light_region != nullptr )
lightlevel = light_region->lightlevel;
Expand Down
10 changes: 7 additions & 3 deletions pol-core/pol/pol.cpp
Expand Up @@ -83,6 +83,7 @@
#include "../plib/pkg.h"
#include "../plib/systemstate.h"
#include "accounts/account.h"
#include "base/position.h"
#include "checkpnt.h"
#include "console.h"
#include "core.h"
Expand Down Expand Up @@ -287,9 +288,12 @@ void start_client_char( Network::Client* client )

client->chr->lastx = client->chr->lasty = client->chr->lastz = 0;

client->gd->music_region = gamestate.musicdef->getregion( 0, 0, client->chr->realm() );
client->gd->justice_region = gamestate.justicedef->getregion( 0, 0, client->chr->realm() );
client->gd->weather_region = gamestate.weatherdef->getregion( 0, 0, client->chr->realm() );
client->gd->music_region =
gamestate.musicdef->getregion( Pos4d( 0, 0, 0, client->chr->realm() ) );
client->gd->justice_region =
gamestate.justicedef->getregion( Pos4d( 0, 0, 0, client->chr->realm() ) );
client->gd->weather_region =
gamestate.weatherdef->getregion( Pos4d( 0, 0, 0, client->chr->realm() ) );

send_goxyz( client, client->chr );
client->chr->check_region_changes();
Expand Down
13 changes: 9 additions & 4 deletions pol-core/pol/realms/realm.h
Expand Up @@ -21,17 +21,17 @@
#include "plib/uconst.h"
#include "plib/udatfile.h"

#include "base/range.h"
#include "realms/WorldChangeReasons.h"


namespace Pol
{
namespace Core
{
class ItemsVector;
class ULWObject;
struct Zone;
}
} // namespace Core
namespace Mobile
{
class Character;
Expand All @@ -51,7 +51,7 @@ class MapTileServer;
class StaticEntryList;
class StaticServer;
struct MAPTILE_CELL;
}
} // namespace Plib
namespace Realms
{
typedef std::vector<Multi::UMulti*> MultiList;
Expand All @@ -71,6 +71,7 @@ class Realm
unsigned short height() const;
unsigned short grid_width() const;
unsigned short grid_height() const;
Core::Range2d area() const;

unsigned season() const;

Expand Down Expand Up @@ -247,6 +248,10 @@ inline unsigned short Realm::height() const
{
return _descriptor.height;
}
inline Core::Range2d Realm::area() const
{
return Core::Range2d( Core::Pos2d( 0, 0 ), Core::Pos2d( width() - 1, height() - 1 ), nullptr );
}
}
} // namespace Realms
} // namespace Pol
#endif
57 changes: 10 additions & 47 deletions pol-core/pol/regions/miscrgn.cpp
Expand Up @@ -9,7 +9,6 @@
#include "miscrgn.h"

#include <stddef.h>
#include <tuple>

#include "clib/cfgelem.h"
#include "clib/rawtypes.h"
Expand Down Expand Up @@ -124,65 +123,29 @@ void WeatherDef::copy_default_regions()
for ( auto& realmregion : regionrealms )
{
Realms::Realm* realm = realmregion.first;
unsigned int gridwidth = realm->width() / ZONE_SIZE;
unsigned int gridheight = realm->height() / ZONE_SIZE;

for ( unsigned int i = 0; i < gridwidth; i++ )
{
for ( unsigned int j = 0; j < gridheight; j++ )
{
default_regionrealms[realm][i][j] = regionrealms[realm][i][j];
}
}
Range2d area = Range2d( Pos2d( 0, 0 ), XyToZone( realm->area().se() ), nullptr );
for ( const auto& p : area )
default_regionrealms[realm][p.x()][p.y()] = regionrealms[realm][p.x()][p.y()];
}
}

bool WeatherDef::assign_zones_to_region( const char* regionname, unsigned short xwest,
unsigned short ynorth, unsigned short xeast,
unsigned short ysouth, Realms::Realm* realm )
bool WeatherDef::assign_zones_to_region( const char* regionname, const Range2d& area,
Realms::Realm* realm )
{
if ( xwest >= realm->width() )
xwest = static_cast<unsigned short>( realm->width() ) - 1;
if ( xeast >= realm->width() )
xeast = static_cast<unsigned short>( realm->width() ) - 1;
if ( ynorth >= realm->height() )
ynorth = static_cast<unsigned short>( realm->height() ) - 1;
if ( ysouth >= realm->height() )
ysouth = static_cast<unsigned short>( realm->height() ) - 1;


Range2d zone_area( XyToZone( area.nw() ), XyToZone( area.se() ), nullptr );
if ( regionname && regionname[0] )
{
Region* rgn = getregion_byname( regionname );
if ( rgn == nullptr )
return false;

unsigned zone_xwest, zone_ynorth, zone_xeast, zone_ysouth;

std::tie( zone_xwest, zone_ynorth ) = XyToZone( xwest, ynorth );
std::tie( zone_xeast, zone_ysouth ) = XyToZone( xeast, ysouth );

for ( auto zx = zone_xwest; zx <= zone_xeast; ++zx )
{
for ( auto zy = zone_ynorth; zy <= zone_ysouth; ++zy )
{
regionrealms[realm][zx][zy] = rgn->regionid();
}
}
for ( const auto& p : zone_area )
regionrealms[realm][p.x()][p.y()] = rgn->regionid();
}
else // move 'em back to the default
{
unsigned zone_xwest, zone_ynorth, zone_xeast, zone_ysouth;
std::tie( zone_xwest, zone_ynorth ) = XyToZone( xwest, ynorth );
std::tie( zone_xeast, zone_ysouth ) = XyToZone( xeast, ysouth );

for ( auto zx = zone_xwest; zx <= zone_xeast; ++zx )
{
for ( auto zy = zone_ynorth; zy <= zone_ysouth; ++zy )
{
regionrealms[realm][zx][zy] = default_regionrealms[realm][zx][zy];
}
}
for ( const auto& p : zone_area )
regionrealms[realm][p.x()][p.y()] = default_regionrealms[realm][p.x()][p.y()];
}
update_all_weatherregions();
return true;
Expand Down
8 changes: 4 additions & 4 deletions pol-core/pol/regions/miscrgn.h
Expand Up @@ -8,6 +8,7 @@
#define MISCRGN_H


#include "base/range.h"
#include "regions/region.h"
#include "zone.h"

Expand Down Expand Up @@ -79,13 +80,12 @@ class WeatherDef final : public RegionGroup<WeatherRegion>
void copy_default_regions();
virtual size_t estimateSize() const override;

bool assign_zones_to_region( const char* regionname, unsigned short x1, unsigned short y1,
unsigned short x2, unsigned short y2, Realms::Realm* realm );
bool assign_zones_to_region( const char* regionname, const Range2d& area, Realms::Realm* realm );

private:
RegionRealms default_regionrealms;
};
}
}
} // namespace Core
} // namespace Pol

#endif

0 comments on commit 8503e72

Please sign in to comment.