Skip to content

Commit

Permalink
Item/ItemView refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cvet committed Oct 4, 2019
1 parent c59100b commit 8d3a08c
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 197 deletions.
2 changes: 1 addition & 1 deletion Source/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6226,7 +6226,7 @@ string FOClient::SScriptFunc::Global_CustomCall( string command, string separato
Self->Chosen->Action( ACTION_DROP_ITEM, from_slot, item );
if( item->GetStackable() && item_count < item->GetCount() )
{
item->ChangeCount( -(int) item->GetCount() );
item->SetCount( item->GetCount() - item_count );
}
else
{
Expand Down
6 changes: 5 additions & 1 deletion Source/Client/CritterView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ void CritterView::AddItem( ItemView* item )
item->SetCritId( Id );

InvItems.push_back( item );
ItemView::SortItems( InvItems );

std::sort( InvItems.begin(), InvItems.end(), [] ( ItemView * l, ItemView * r )
{
return l->GetSortValue() < r->GetSortValue();
} );

if( item->GetCritSlot() && !IsAnim() )
AnimateStay();
Expand Down
13 changes: 6 additions & 7 deletions Source/Client/HexManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,25 +1856,24 @@ void HexManager::CollectLightSources()
#else
for( auto& item : hexItems )
if( item->IsStatic() && item->GetIsLight() )
lightSources.push_back( LightSource( item->GetHexX(), item->GetHexY(), item->LightGetColor(), item->LightGetDistance(), item->LightGetIntensity(), item->LightGetFlags() ) );
lightSources.push_back( LightSource( item->GetHexX(), item->GetHexY(), item->GetLightColor(), item->GetLightDistance(), item->GetLightIntensity(), item->GetLightFlags() ) );
#endif

// Items on ground
for( auto& item : hexItems )
if( !item->IsStatic() && item->GetIsLight() )
lightSources.push_back( LightSource( item->GetHexX(), item->GetHexY(), item->LightGetColor(), item->LightGetDistance(), item->LightGetIntensity(), item->LightGetFlags() ) );
lightSources.push_back( LightSource( item->GetHexX(), item->GetHexY(), item->GetLightColor(), item->GetLightDistance(), item->GetLightIntensity(), item->GetLightFlags() ) );

// Items in critters slots
for( auto& kv : allCritters )
{
CritterView* cr = kv.second;
bool added = false;
for( auto it_ = cr->InvItems.begin(), end_ = cr->InvItems.end(); it_ != end_; ++it_ )
for( auto item : cr->InvItems )
{
ItemView* item = *it_;
if( item->GetIsLight() && item->GetCritSlot() )
{
lightSources.push_back( LightSource( cr->GetHexX(), cr->GetHexY(), item->LightGetColor(), item->LightGetDistance(), item->LightGetIntensity(), item->LightGetFlags(), &cr->SprOx, &cr->SprOy ) );
lightSources.push_back( LightSource( cr->GetHexX(), cr->GetHexY(), item->GetLightColor(), item->GetLightDistance(), item->GetLightIntensity(), item->GetLightFlags(), &cr->SprOx, &cr->SprOy ) );
added = true;
}
}
Expand Down Expand Up @@ -4211,8 +4210,8 @@ void HexManager::GenerateItem( uint id, hash proto_id, Properties& props )

if( scenery->GetIsLight() )
{
lightSourcesScen.push_back( LightSource( scenery->GetHexX(), scenery->GetHexY(), scenery->LightGetColor(),
scenery->LightGetDistance(), scenery->LightGetIntensity(), scenery->LightGetFlags() ) );
lightSourcesScen.push_back( LightSource( scenery->GetHexX(), scenery->GetHexY(), scenery->GetLightColor(),
scenery->GetLightDistance(), scenery->GetLightIntensity(), scenery->GetLightFlags() ) );
}

PushItem( scenery );
Expand Down
132 changes: 44 additions & 88 deletions Source/Client/ItemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ CLASS_PROPERTY_IMPL( ItemView, FlyEffectSpeed );
ItemView::ItemView( uint id, ProtoItem* proto ): Entity( id, EntityType::ItemView, PropertiesRegistrator, proto )
{
RUNTIME_ASSERT( proto );

MEMORY_PROCESS( MEMORY_ITEM, sizeof( ItemView ) + PropertiesRegistrator->GetWholeDataSize() );

ChildItems = nullptr;

RUNTIME_ASSERT( GetCount() > 0 );
}

Expand All @@ -94,97 +90,13 @@ ItemView::~ItemView()
MEMORY_PROCESS( MEMORY_ITEM, -(int) ( sizeof( ItemView ) + PropertiesRegistrator->GetWholeDataSize() ) );
}

void ItemView::SetProto( ProtoItem* proto )
{
RUNTIME_ASSERT( proto );
proto->AddRef();
Proto->Release();
Proto = proto;
Props = proto->Props;
}

ItemView* ItemView::Clone()
{
RUNTIME_ASSERT( Type == EntityType::ItemView );
ItemView* clone = new ItemView( Id, (ProtoItem*) Proto );
clone->Props = Props;
return clone;
}

void ItemView::SetSortValue( ItemViewVec& items )
{
short sort_value = 0;
for( auto it = items.begin(), end = items.end(); it != end; ++it )
{
ItemView* item = *it;
if( item == this )
continue;
if( sort_value >= item->GetSortValue() )
sort_value = item->GetSortValue() - 1;
}
SetSortValue( sort_value );
}

static bool SortItemsFunc( ItemView* l, ItemView* r ) { return l->GetSortValue() < r->GetSortValue(); }
void ItemView::SortItems( ItemViewVec& items )
{
std::sort( items.begin(), items.end(), SortItemsFunc );
}

void ItemView::ClearItems( ItemViewVec& items )
{
for( auto it = items.begin(), end = items.end(); it != end; ++it )
( *it )->Release();
items.clear();
}

void ItemView::ChangeCount( int val )
{
SetCount( GetCount() + val );
}

void ItemView::ContSetItem( ItemView* item )
{
if( !ChildItems )
ChildItems = new ItemViewVec();

RUNTIME_ASSERT( std::find( ChildItems->begin(), ChildItems->end(), item ) == ChildItems->end() );

ChildItems->push_back( item );
item->SetAccessory( ITEM_ACCESSORY_CONTAINER );
item->SetContainerId( Id );
}

void ItemView::ContEraseItem( ItemView* item )
{
RUNTIME_ASSERT( ChildItems );
RUNTIME_ASSERT( item );

auto it = std::find( ChildItems->begin(), ChildItems->end(), item );
RUNTIME_ASSERT( it != ChildItems->end() );
ChildItems->erase( it );

item->SetAccessory( ITEM_ACCESSORY_NONE );
item->SetContainerId( 0 );
item->SetContainerStack( 0 );

if( ChildItems->empty() )
SAFEDEL( ChildItems );
}

void ItemView::ContGetItems( ItemViewVec& items, uint stack_id )
{
if( !ChildItems )
return;

for( auto it = ChildItems->begin(), end = ChildItems->end(); it != end; ++it )
{
ItemView* item = *it;
if( stack_id == uint( -1 ) || item->GetContainerStack() == stack_id )
items.push_back( item );
}
}

uint ItemView::GetCurSprId()
{
AnyFrames* anim = ResMngr.GetItemAnim( GetPicMap() );
Expand Down Expand Up @@ -242,3 +154,47 @@ uint ProtoItem::GetCurSprId()
uint ticks = anim->Ticks / anim->CntFrm * count;
return anim->Ind[ beg + ( ( Timer::GameTick() % ticks ) * 100 / ticks ) * count / 100 ];
}

#ifdef FONLINE_EDITOR
void ItemView::ContSetItem( ItemView* item )
{
if( !ChildItems )
ChildItems = new ItemViewVec();

RUNTIME_ASSERT( std::find( ChildItems->begin(), ChildItems->end(), item ) == ChildItems->end() );

ChildItems->push_back( item );
item->SetAccessory( ITEM_ACCESSORY_CONTAINER );
item->SetContainerId( Id );
}

void ItemView::ContEraseItem( ItemView* item )
{
RUNTIME_ASSERT( ChildItems );
RUNTIME_ASSERT( item );

auto it = std::find( ChildItems->begin(), ChildItems->end(), item );
RUNTIME_ASSERT( it != ChildItems->end() );
ChildItems->erase( it );

item->SetAccessory( ITEM_ACCESSORY_NONE );
item->SetContainerId( 0 );
item->SetContainerStack( 0 );

if( ChildItems->empty() )
SAFEDEL( ChildItems );
}

void ItemView::ContGetItems( ItemViewVec& items, uint stack_id )
{
if( !ChildItems )
return;

for( auto it = ChildItems->begin(), end = ChildItems->end(); it != end; ++it )
{
ItemView* item = *it;
if( stack_id == uint( -1 ) || item->GetContainerStack() == stack_id )
items.push_back( item );
}
}
#endif
50 changes: 15 additions & 35 deletions Source/Client/ItemView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class ItemView: public Entity
{
public:
// Properties
PROPERTIES_HEADER();
CLASS_PROPERTY( bool, Stackable );
CLASS_PROPERTY( bool, Opened );
Expand Down Expand Up @@ -82,48 +81,29 @@ class ItemView: public Entity
CLASS_PROPERTY( short, OffsetY );
CLASS_PROPERTY( float, FlyEffectSpeed );

public:
ItemView( uint id, ProtoItem* proto );
~ItemView();

ItemViewVec* ChildItems;

ProtoItem* GetProtoItem() { return (ProtoItem*) Proto; }
ItemView* Clone();
bool operator==( const uint& id ) { return Id == id; }
void SetSortValue( ItemViewVec& items );
static void SortItems( ItemViewVec& items );
static void ClearItems( ItemViewVec& items );
ProtoItem* GetProtoItem() { return (ProtoItem*) Proto; }
ItemView* Clone();

bool IsStatic() { return GetIsStatic(); }
bool IsAnyScenery() { return IsScenery() || IsWall(); }
bool IsScenery() { return GetIsScenery(); }
bool IsWall() { return GetIsWall(); }

void ChangeCount( int val );
bool IsStatic() { return GetIsStatic(); }
bool IsAnyScenery() { return IsScenery() || IsWall(); }
bool IsScenery() { return GetIsScenery(); }
bool IsWall() { return GetIsWall(); }
bool IsColorize() { return GetIsColorize(); }
uint GetColor() { return GetLightColor() & 0xFFFFFF; }
uchar GetAlpha() { return GetLightColor() >> 24; }
uint GetInvColor() { return GetIsColorizeInv() ? GetLightColor() : 0; }
uint LightGetHash() { return GetIsLight() ? GetLightIntensity() + GetLightDistance() + GetLightFlags() + GetLightColor() : 0; }

uint GetCurSprId();

#ifdef FONLINE_EDITOR
ItemViewVec* ChildItems = nullptr;

void ContSetItem( ItemView* item );
void ContEraseItem( ItemView* item );
void ContGetItems( ItemViewVec& items, uint stack_id );

// Colorize
bool IsColorize() { return GetIsColorize(); }
uint GetColor() { return GetLightColor() & 0xFFFFFF; }
uchar GetAlpha() { return GetLightColor() >> 24; }
uint GetInvColor() { return GetIsColorizeInv() ? GetLightColor() : 0; }

// Light
uint LightGetHash() { return GetIsLight() ? GetLightIntensity() + GetLightDistance() + GetLightFlags() + GetLightColor() : 0; }
int LightGetIntensity() { return GetLightIntensity(); }
int LightGetDistance() { return GetLightDistance(); }
int LightGetFlags() { return GetLightFlags(); }
uint LightGetColor() { return GetLightColor() & 0xFFFFFF; }

// Radio
bool RadioIsSendActive() { return !FLAG( GetRadioFlags(), RADIO_DISABLE_SEND ); }
bool RadioIsRecvActive() { return !FLAG( GetRadioFlags(), RADIO_DISABLE_RECV ); }

void SetProto( ProtoItem* proto );
#endif
};
1 change: 0 additions & 1 deletion Source/Common/ScriptFunctions_Include.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ static int SystemCall( string command, std::function< void(const string&) > log_

return pclose( in );
#else

return 1;
#endif
}
Expand Down
23 changes: 1 addition & 22 deletions Source/Server/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,8 @@ CLASS_PROPERTY_IMPL( Item, FlyEffectSpeed );
Item::Item( uint id, ProtoItem* proto ): Entity( id, EntityType::Item, PropertiesRegistrator, proto )
{
RUNTIME_ASSERT( proto );

MEMORY_PROCESS( MEMORY_ITEM, sizeof( Item ) + PropertiesRegistrator->GetWholeDataSize() );

ChildItems = nullptr;
ViewPlaceOnMap = false;
SceneryScriptBindId = 0;
ViewByCritter = nullptr;

if( GetCount() == 0 )
SetCount( 1 );
RUNTIME_ASSERT( GetCount() > 0 );
}

Item::~Item()
Expand Down Expand Up @@ -146,19 +138,6 @@ void Item::SetSortValue( ItemVec& items )
SetSortValue( sort_value );
}

static bool SortItemsFunc( Item* l, Item* r ) { return l->GetSortValue() < r->GetSortValue(); }
void Item::SortItems( ItemVec& items )
{
std::sort( items.begin(), items.end(), SortItemsFunc );
}

void Item::ClearItems( ItemVec& items )
{
for( auto it = items.begin(), end = items.end(); it != end; ++it )
( *it )->Release();
items.clear();
}

void Item::ChangeCount( int val )
{
SetCount( GetCount() + val );
Expand Down
Loading

0 comments on commit 8d3a08c

Please sign in to comment.