Skip to content

Commit

Permalink
use Pos classes in Map and Door (#422)
Browse files Browse the repository at this point in the history
* use Pos classes in Map and Door
removed most of the functions in ufacing.cpp
removed Plib::UFACING
added executor getPos2d

* added forward methods to baseobject, lets see where it ends :)

* fix typo

* moved methods to uobject
  • Loading branch information
turleypol committed Sep 11, 2021
1 parent 7ee1b64 commit 9fe3e36
Show file tree
Hide file tree
Showing 27 changed files with 346 additions and 397 deletions.
19 changes: 0 additions & 19 deletions pol-core/plib/uconst.h
Expand Up @@ -30,25 +30,6 @@ enum URACE : u8
RACE_GARGOYLE = 2
};

/* Character Facings:
N^
7 0 1
6 * 2
5 4 3
*/

enum UFACING : u8
{
FACING_N = 0,
FACING_NE = 1,
FACING_E = 2,
FACING_SE = 3,
FACING_S = 4,
FACING_SW = 5,
FACING_W = 6,
FACING_NW = 7
};


enum UTEXTTYPE : u8
{
Expand Down
10 changes: 10 additions & 0 deletions pol-core/pol/baseobject.h
Expand Up @@ -59,6 +59,8 @@ class ULWObject
u8 look_height() const; // where you're looking from, or to

const Pos4d& pos() const;
const Pos3d& pos3d() const;
const Pos2d& pos2d() const;
u16 x() const;
u16 y() const;
s8 z() const;
Expand Down Expand Up @@ -133,6 +135,14 @@ inline void ULWObject::pos( Pos4d newpos )
{
position = std::move( newpos );
}
inline const Pos3d& ULWObject::pos3d() const
{
return pos().xyz();
}
inline const Pos2d& ULWObject::pos2d() const
{
return pos().xy();
}

// TODO POS remove as final step
inline u16 ULWObject::x() const
Expand Down
6 changes: 3 additions & 3 deletions pol-core/pol/create.cpp
Expand Up @@ -321,7 +321,7 @@ void ClientCreateChar( Network::Client* client, PKTIN_00* msg )
Realms::Realm* realm = gamestate.startlocations[msg->StartIndex]->realm;
// TODO POS
chr->setposition( Pos4d( coord.x, coord.y, coord.z, realm ) );
chr->facing = Plib::FACING_W;
chr->facing = Core::FACING_W;
chr->position_changed();

bool valid_stats = false;
Expand Down Expand Up @@ -676,7 +676,7 @@ void ClientCreateCharKR( Network::Client* client, PKTIN_8D* msg )
// TODO POS
chr->setposition( Pos4d( coord.x, coord.y, coord.z, realm ) );
chr->position_changed();
chr->facing = Plib::FACING_W;
chr->facing = Core::FACING_W;

bool valid_stats = false;
unsigned int stat_total = msg->strength + msg->intelligence + msg->dexterity;
Expand Down Expand Up @@ -1062,7 +1062,7 @@ void ClientCreateChar70160( Network::Client* client, PKTIN_F8* msg )

chr->setposition( Pos4d( coord.x, coord.y, coord.z, realm ) );
chr->position_changed();
chr->facing = Plib::FACING_W;
chr->facing = Core::FACING_W;

bool valid_stats = false;
unsigned int stat_total = msg->Strength + msg->Intelligence + msg->Dexterity;
Expand Down
8 changes: 4 additions & 4 deletions pol-core/pol/door.cpp
Expand Up @@ -43,7 +43,7 @@ void UDoor::toggle()
{
const Items::DoorDesc* dd = static_cast<const Items::DoorDesc*>( &itemdesc() );

unsigned short oldx = x(), oldy = y();
Pos4d oldpos = pos();

set_dirty();
if ( is_open() )
Expand All @@ -52,15 +52,15 @@ void UDoor::toggle()
graphic = dd->graphic;
else
graphic = static_cast<u16>( objtype_ );
setposition( pos() - Vec2d( dd->xmod, dd->ymod ) );
setposition( pos() - dd->mod );
}
else
{
graphic = dd->open_graphic;
setposition( pos() + Vec2d( dd->xmod, dd->ymod ) );
setposition( pos() + dd->mod );
}

MoveItemWorldPosition( oldx, oldy, this, nullptr );
MoveItemWorldPosition( oldpos.x(), oldpos.y(), this, nullptr );

send_item_to_inrange( this );
}
Expand Down
18 changes: 9 additions & 9 deletions pol-core/pol/item/itemdesc.cpp
Expand Up @@ -44,10 +44,10 @@
#include "../multi/multidef.h"
#include "../network/pktdef.h"
#include "../proplist.h"
#include "regions/resource.h"
#include "../syshookscript.h"
#include "../uobject.h"
#include "armrtmpl.h"
#include "regions/resource.h"
#include "wepntmpl.h"
#include <format/format.h>

Expand Down Expand Up @@ -418,7 +418,8 @@ ItemDesc::ItemDesc( u32 objtype, Clib::ConfigElem& elem, Type type, const Plib::
}
}
auto diceValue = [this]( const std::string& value,
const std::string& error_msg ) -> unsigned short {
const std::string& error_msg ) -> unsigned short
{
Core::Dice dice;
std::string errmsg;
if ( !dice.load( value.c_str(), &errmsg ) )
Expand Down Expand Up @@ -981,24 +982,23 @@ size_t ContainerDesc::estimatedSize() const

DoorDesc::DoorDesc( u32 objtype, Clib::ConfigElem& elem, const Plib::Package* pkg )
: ItemDesc( objtype, elem, DOORDESC, pkg ),
xmod( static_cast<s16>( elem.remove_int( "XMOD" ) ) ),
ymod( static_cast<s16>( elem.remove_int( "YMOD" ) ) ),
mod( static_cast<s16>( elem.remove_int( "XMOD" ) ),
static_cast<s16>( elem.remove_int( "YMOD" ) ) ),
open_graphic( elem.remove_ushort( "OPENGRAPHIC", 0 ) )
{
}

void DoorDesc::PopulateStruct( Bscript::BStruct* descriptor ) const
{
base::PopulateStruct( descriptor );
descriptor->addMember( "XMod", new Bscript::BLong( xmod ) );
descriptor->addMember( "YMod", new Bscript::BLong( ymod ) );
descriptor->addMember( "XMod", new Bscript::BLong( mod.x() ) );
descriptor->addMember( "YMod", new Bscript::BLong( mod.y() ) );
descriptor->addMember( "OpenGraphic", new Bscript::BLong( open_graphic ) );
}
size_t DoorDesc::estimatedSize() const
{
return base::estimatedSize() + sizeof( s16 ) /*xmod*/
+ sizeof( s16 ) /*ymod*/
+ sizeof( u16 ) /*open_graphic*/
return base::estimatedSize() + sizeof( Core::Vec2d ) /*mod*/
+ sizeof( u16 ) /*open_graphic*/
;
}

Expand Down
4 changes: 2 additions & 2 deletions pol-core/pol/item/itemdesc.h
Expand Up @@ -17,6 +17,7 @@
#include "../proplist.h"
#include "../scrdef.h"
#include "../uobject.h"
#include "base/vector.h"

namespace Pol
{
Expand Down Expand Up @@ -213,8 +214,7 @@ class DoorDesc final : public ItemDesc
virtual void PopulateStruct( Bscript::BStruct* descriptor ) const override;
virtual ~DoorDesc(){};
virtual size_t estimatedSize() const override;
s16 xmod;
s16 ymod;
Core::Vec2d mod;
u16 open_graphic;
};

Expand Down
10 changes: 5 additions & 5 deletions pol-core/pol/mobile/charactr.cpp
Expand Up @@ -2811,7 +2811,7 @@ void PropagateMove( /*Client *client,*/ Character* chr )
} );
}

void Character::getpos_ifmove( Plib::UFACING i_facing, unsigned short* px, unsigned short* py )
void Character::getpos_ifmove( Core::UFACING i_facing, unsigned short* px, unsigned short* py )
{
*px = x() + Core::move_delta[i_facing].xmove;
*py = y() + Core::move_delta[i_facing].ymove;
Expand Down Expand Up @@ -3708,7 +3708,7 @@ bool Character::doors_block() const
We're sending the "78 create" _before_ the move-approve.
*/

bool Character::can_face( Plib::UFACING /*i_facing*/ )
bool Character::can_face( Core::UFACING /*i_facing*/ )
{
if ( can_freemove() )
return true;
Expand Down Expand Up @@ -3736,7 +3736,7 @@ bool Character::can_face( Plib::UFACING /*i_facing*/ )
}


bool Character::face( Plib::UFACING i_facing, int flags )
bool Character::face( Core::UFACING i_facing, int flags )
{
if ( ( flags & 1 ) == 0 )
{
Expand Down Expand Up @@ -3774,7 +3774,7 @@ bool Character::CustomHousingMove( unsigned char i_dir )
Multi::UHouse* house = multi->as_house();
if ( house != nullptr )
{
Plib::UFACING i_facing = static_cast<Plib::UFACING>( i_dir & PKTIN_02_FACING_MASK );
Core::UFACING i_facing = static_cast<Core::UFACING>( i_dir & PKTIN_02_FACING_MASK );
if ( i_facing != facing )
{
setfacing( static_cast<u8>( i_facing ) );
Expand Down Expand Up @@ -3826,7 +3826,7 @@ bool Character::move( unsigned char i_dir )

u8 oldFacing = facing;

Plib::UFACING i_facing = static_cast<Plib::UFACING>( i_dir & PKTIN_02_FACING_MASK );
Core::UFACING i_facing = static_cast<Core::UFACING>( i_dir & PKTIN_02_FACING_MASK );
if ( !face( i_facing ) )
return false;

Expand Down
6 changes: 3 additions & 3 deletions pol-core/pol/mobile/charactr.h
Expand Up @@ -401,9 +401,9 @@ class Character : public Core::UObject
static Plib::MOVEMODE decode_movemode( const std::string& str );
static std::string encode_movemode( Plib::MOVEMODE movemode );
// if a move were made, what would the new position be?
void getpos_ifmove( Plib::UFACING i_facing, unsigned short* px, unsigned short* py );
bool can_face( Plib::UFACING i_facing );
bool face( Plib::UFACING i_facing, int flags = 0 );
void getpos_ifmove( Core::UFACING i_facing, unsigned short* px, unsigned short* py );
bool can_face( Core::UFACING i_facing );
bool face( Core::UFACING i_facing, int flags = 0 );
bool move( unsigned char dir );
bool CustomHousingMove( unsigned char i_dir );
void tellmove( void );
Expand Down
6 changes: 3 additions & 3 deletions pol-core/pol/mobile/npc.cpp
Expand Up @@ -148,7 +148,7 @@ const char* NPC::classname() const
// unsigned short x2, unsigned short y2 )
// to ufunc.cpp

bool NPC::anchor_allows_move( Plib::UFACING fdir ) const
bool NPC::anchor_allows_move( Core::UFACING fdir ) const
{
unsigned short newx = x() + Core::move_delta[fdir].xmove;
unsigned short newy = y() + Core::move_delta[fdir].ymove;
Expand All @@ -172,7 +172,7 @@ bool NPC::anchor_allows_move( Plib::UFACING fdir ) const
return true;
}

bool NPC::could_move( Plib::UFACING fdir ) const
bool NPC::could_move( Core::UFACING fdir ) const
{
short newz;
Multi::UMulti* supporting_multi;
Expand Down Expand Up @@ -205,7 +205,7 @@ bool NPC::could_move( Plib::UFACING fdir ) const
!npc_path_blocked( fdir ) && anchor_allows_move( fdir );
}

bool NPC::npc_path_blocked( Plib::UFACING fdir ) const
bool NPC::npc_path_blocked( Core::UFACING fdir ) const
{
if ( can_freemove() ||
( !this->master() && !Core::settingsManager.ssopt.mobiles_block_npc_movement ) )
Expand Down
6 changes: 3 additions & 3 deletions pol-core/pol/mobile/npc.h
Expand Up @@ -163,9 +163,9 @@ class NPC final : public Character
public:
bool use_adjustments() const;
void use_adjustments( bool newvalue );
bool could_move( Plib::UFACING dir ) const;
bool anchor_allows_move( Plib::UFACING dir ) const;
bool npc_path_blocked( Plib::UFACING dir ) const;
bool could_move( Core::UFACING dir ) const;
bool anchor_allows_move( Core::UFACING dir ) const;
bool npc_path_blocked( Core::UFACING dir ) const;

// EVENTS
public:
Expand Down
80 changes: 21 additions & 59 deletions pol-core/pol/mobile/ufacing.cpp
Expand Up @@ -13,103 +13,65 @@ namespace Pol
namespace Core
{
// FIXME shouldn't be using PKTIN_02_FACING_MASK here
// TODO Pos: kill me
MoveDelta move_delta[8] = { { 0, -1 }, // 0 is N
{ +1, -1 }, // 1 is NE
{ +1, 0 }, // ...
{ +1, +1 }, { 0, +1 }, { -1, +1 }, { -1, 0 }, { -1, -1 } };

Plib::UFACING away_cvt[8] = { Plib::FACING_S, Plib::FACING_SW, Plib::FACING_W, Plib::FACING_NW,
Plib::FACING_N, Plib::FACING_NE, Plib::FACING_E, Plib::FACING_SE };

std::array<int, 7> adjustments = { { 0, +1, -1, +2, -2, +3, -3 } };
} // namespace Core
namespace Mobile
{
Plib::UFACING GetRandomFacing()
Core::UFACING GetRandomFacing()
{
int rval = Clib::random_int( 7 );
return static_cast<Plib::UFACING>( rval );
return static_cast<Core::UFACING>( rval );
}

const char* FacingStr( Plib::UFACING facing )
const char* FacingStr( Core::UFACING facing )
{
switch ( facing )
{
case Plib::FACING_N:
case Core::FACING_N:
return "N";
case Plib::FACING_S:
case Core::FACING_S:
return "S";
case Plib::FACING_E:
case Core::FACING_E:
return "E";
case Plib::FACING_W:
case Core::FACING_W:
return "W";
case Plib::FACING_NE:
case Core::FACING_NE:
return "NE";
case Plib::FACING_NW:
case Core::FACING_NW:
return "NW";
case Plib::FACING_SE:
case Core::FACING_SE:
return "SE";
case Plib::FACING_SW:
case Core::FACING_SW:
return "SW";
}
return "";
}

Plib::UFACING direction_toward( const Character* src, const Core::UObject* idst )
{
using namespace Core;
const UObject* dst = idst->toplevel_owner();
auto f = src->pos().xy().direction_toward( dst->pos().xy() );
return static_cast<Plib::UFACING>( f );
}

Plib::UFACING direction_toward( const Character* src, Core::xcoord to_x, Core::ycoord to_y )
{
using namespace Core;
auto f = src->pos().xy().direction_toward( Pos2d( to_x, to_y ) );
return static_cast<Plib::UFACING>( f );
}

Plib::UFACING direction_toward( Core::xcoord from_x, Core::ycoord from_y, Core::xcoord to_x,
Core::ycoord to_y )
{
using namespace Core;
auto f = Pos2d( from_x, from_y ).direction_toward( Pos2d( to_x, to_y ) );
return static_cast<Plib::UFACING>( f );
}

Plib::UFACING direction_away( const Character* src, const Core::UObject* idst )
{
Plib::UFACING toward = direction_toward( src, idst );
Plib::UFACING away = Core::away_cvt[static_cast<int>( toward )];
return away;
}

Plib::UFACING direction_away( const Character* src, Core::xcoord from_x, Core::ycoord from_y )
{
Plib::UFACING toward = direction_toward( src, from_x, from_y );
Plib::UFACING away = Core::away_cvt[static_cast<int>( toward )];
return away;
}

bool DecodeFacing( const char* dir, Plib::UFACING& facing )
bool DecodeFacing( const char* dir, Core::UFACING& facing )
{
if ( stricmp( dir, "N" ) == 0 )
facing = Plib::FACING_N;
facing = Core::FACING_N;
else if ( stricmp( dir, "S" ) == 0 )
facing = Plib::FACING_S;
facing = Core::FACING_S;
else if ( stricmp( dir, "E" ) == 0 )
facing = Plib::FACING_E;
facing = Core::FACING_E;
else if ( stricmp( dir, "W" ) == 0 )
facing = Plib::FACING_W;
facing = Core::FACING_W;
else if ( stricmp( dir, "NE" ) == 0 )
facing = Plib::FACING_NE;
facing = Core::FACING_NE;
else if ( stricmp( dir, "SE" ) == 0 )
facing = Plib::FACING_SE;
facing = Core::FACING_SE;
else if ( stricmp( dir, "SW" ) == 0 )
facing = Plib::FACING_SW;
facing = Core::FACING_SW;
else if ( stricmp( dir, "NW" ) == 0 )
facing = Plib::FACING_NW;
facing = Core::FACING_NW;
else
return false;
return true;
Expand Down

0 comments on commit 9fe3e36

Please sign in to comment.