Skip to content

Commit

Permalink
Added functionality for multiple item usage
Browse files Browse the repository at this point in the history
You can now have items with multiple uses, with proper display using
progress bars and all of the bells and whistles.
  • Loading branch information
AxelDominatoR authored and Xottab-DUTY committed Dec 16, 2017
1 parent 4da9bb6 commit afc85df
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/xrGame/ActorHelmet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void CHelmet::Load(LPCSTR section)

m_BonesProtectionSect = READ_IF_EXISTS(pSettings, r_string, section, "bones_koeff_protection", "");
m_fShowNearestEnemiesDistance = READ_IF_EXISTS(pSettings, r_float, section, "nearest_enemies_show_dist", 0.0f);

// Added by Axel, to enable optional condition use on any item
m_flags.set(FUsingCondition, READ_IF_EXISTS(pSettings, r_bool, section, "use_condition", true));
}

void CHelmet::ReloadBonesProtection()
Expand Down
3 changes: 3 additions & 0 deletions src/xrGame/CustomOutfit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ void CCustomOutfit::Load(LPCSTR section)

m_BonesProtectionSect = READ_IF_EXISTS(pSettings, r_string, section, "bones_koeff_protection", "");
bIsHelmetAvaliable = !!READ_IF_EXISTS(pSettings, r_bool, section, "helmet_avaliable", true);

// Added by Axel, to enable optional condition use on any item
m_flags.set(FUsingCondition, READ_IF_EXISTS(pSettings, r_bool, section, "use_condition", true));
}

void CCustomOutfit::ReloadBonesProtection()
Expand Down
3 changes: 2 additions & 1 deletion src/xrGame/ExplosiveItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ void CExplosiveItem::Load(LPCSTR section)
{
inherited::Load(section);
CExplosive::Load(section);
m_flags.set(FUsingCondition, TRUE);
// Added by Axel, to enable optional condition use on any item
m_flags.set(FUsingCondition, READ_IF_EXISTS(pSettings, r_bool, section, "use_condition", true));
CDelayedActionFuse::Initialize(
pSettings->r_float(section, "time_to_explode"), pSettings->r_float(section, "condition_to_explode"));
VERIFY(pSettings->line_exist(section, "set_timer_particles"));
Expand Down
2 changes: 1 addition & 1 deletion src/xrGame/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ bool CInventory::Eat(PIItem pIItem)
if (IsGameTypeSingle() && Actor()->m_inventory == this)
Actor()->callback(GameObject::eUseObject)((smart_cast<CGameObject*>(pIItem))->lua_game_object());

if (pItemToEat->Empty())
if (pItemToEat->CanDelete())
{
pIItem->SetDropManual(TRUE);
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/xrGame/Weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ void CWeapon::Load(LPCSTR section)
m_zoom_params.m_bUseDynamicZoom = READ_IF_EXISTS(pSettings, r_bool, section, "scope_dynamic_zoom", false);
m_zoom_params.m_sUseZoomPostprocess = nullptr;
m_zoom_params.m_sUseBinocularVision = nullptr;

// Added by Axel, to enable optional condition use on any item
m_flags.set(FUsingCondition, READ_IF_EXISTS(pSettings, r_bool, section, "use_condition", true));
}

void CWeapon::LoadFireParams(LPCSTR section)
Expand Down
41 changes: 34 additions & 7 deletions src/xrGame/eatable_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
#include "entity_alive.h"
#include "EntityCondition.h"
#include "InventoryOwner.h"
#include "UIGameCustom.h"

CEatableItem::CEatableItem()
{
m_iPortionsNum = -1;
m_physic_item = 0;
m_iMaxUses = 1;
m_iRemainingUses = 1;
m_bRemoveAfterUse = true;
}

CEatableItem::~CEatableItem() {}
Expand All @@ -32,15 +35,35 @@ void CEatableItem::Load(LPCSTR section)
{
inherited::Load(section);

m_iPortionsNum = pSettings->r_s32(section, "eat_portions_num");
VERIFY(m_iPortionsNum < 10000);
m_iRemainingUses = m_iMaxUses = READ_IF_EXISTS(pSettings, r_u16, section, "max_uses", 1);
m_bRemoveAfterUse = READ_IF_EXISTS(pSettings, r_bool, section, "remove_after_use", true);

if (IsUsingCondition())
SetCondition((float)m_iRemainingUses / (float)m_iMaxUses );
}

void CEatableItem::load(IReader& packet)
{
inherited::load(packet);

m_iRemainingUses = packet.r_u16();
}

void CEatableItem::save(NET_Packet& packet)
{
inherited::save(packet);

packet.w_u16(m_iRemainingUses);
}

BOOL CEatableItem::net_Spawn(CSE_Abstract* DC)
{
if (!inherited::net_Spawn(DC))
return FALSE;

if (IsUsingCondition())
SetCondition((float)m_iRemainingUses / (float)m_iMaxUses);

return TRUE;
};

Expand All @@ -50,7 +73,7 @@ bool CEatableItem::Useful() const
return false;

//проверить не все ли еще съедено
if (m_iPortionsNum == 0)
if (m_iRemainingUses == 0)
return false;

return true;
Expand Down Expand Up @@ -108,10 +131,14 @@ bool CEatableItem::UseBy(CEntityAlive* entity_alive)
Level().Send(tmp_packet);
}

if (m_iPortionsNum > 0)
--m_iPortionsNum;
if (m_iRemainingUses > 0)
--m_iRemainingUses;
else
m_iPortionsNum = 0;
m_iRemainingUses = 0;

SetCondition((float)m_iRemainingUses / (float)m_iMaxUses);
CurrentGameUI()->HideActorMenu();
CurrentGameUI()->ShowActorMenu();

return true;
}
14 changes: 10 additions & 4 deletions src/xrGame/eatable_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ class CEatableItem : public CInventoryItem
protected:
CPhysicItem* m_physic_item;

u16 m_iMaxUses;
u16 m_iRemainingUses;
bool m_bRemoveAfterUse;

public:
CEatableItem();
virtual ~CEatableItem();
virtual IFactoryObject* _construct();
virtual CEatableItem* cast_eatable_item() { return this; }
virtual void Load(LPCSTR section);
void load(IReader& packet) override;
void save(NET_Packet& packet) override;
virtual bool Useful() const;

virtual BOOL net_Spawn(CSE_Abstract* DC);

virtual void OnH_B_Independent(bool just_before_destroy);
virtual void OnH_A_Independent();
virtual bool UseBy(CEntityAlive* npc);
virtual bool Empty() { return PortionsNum() == 0; };
int PortionsNum() const { return m_iPortionsNum; }
protected:
int m_iPortionsNum;
virtual bool Empty() const { return m_iRemainingUses == 0; }
bool CanDelete() const { return Empty() && m_bRemoveAfterUse == true; }
virtual u16 GetMaxUses() const { return m_iMaxUses; }
virtual u16 GetRemainingUses() const { return m_iRemainingUses; }
};
5 changes: 4 additions & 1 deletion src/xrGame/inventory_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ void CInventoryItem::Load(LPCSTR section)
m_flags.set(FCanTrade, m_can_trade);
m_flags.set(FIsQuestItem, READ_IF_EXISTS(pSettings, r_bool, section, "quest_item", FALSE));

// Added by Axel, to enable optional condition use on any item
m_flags.set(FUsingCondition, READ_IF_EXISTS(pSettings, r_bool, section, "use_condition", false));

if (BaseSlot() != NO_ACTIVE_SLOT || Belt())
{
m_flags.set(FRuckDefault, pSettings->r_bool(section, "default_to_ruck"));
Expand All @@ -127,7 +130,7 @@ void CInventoryItem::ChangeCondition(float fDeltaCondition)

void CInventoryItem::Hit(SHit* pHDS)
{
if (!m_flags.test(FUsingCondition))
if (IsUsingCondition() == false)
return;

float hit_power = pHDS->damage();
Expand Down
1 change: 1 addition & 0 deletions src/xrGame/inventory_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class CInventoryItem : public CAttachableItem,
virtual void OnEvent(NET_Packet& P, u16 type);

virtual bool Useful() const; // !!! Переопределить. (см. в Inventory.cpp)
virtual bool IsUsingCondition() const { return (m_flags.test(FUsingCondition) > 0); }
virtual bool Attach(PIItem pIItem, bool b_send_event) { return false; }
virtual bool Detach(PIItem pIItem) { return false; }
//при детаче спаунится новая вещь при заданно названии секции
Expand Down
32 changes: 27 additions & 5 deletions src/xrGame/ui/UICellItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "uicursor.h"
#include "inventory_item.h"
#include "UIDragDropListEx.h"
#include "eatable_item.h"
#include "xr_level_controller.h"
#include "xrEngine/xr_input.h"
#include "Level.h"
Expand Down Expand Up @@ -204,11 +205,32 @@ void CUICellItem::UpdateConditionProgressBar()
if (m_pParentList && m_pParentList->GetConditionProgBarVisibility())
{
PIItem itm = (PIItem)m_pData;
CWeapon* pWeapon = smart_cast<CWeapon*>(itm);
CCustomOutfit* pOutfit = smart_cast<CCustomOutfit*>(itm);
CHelmet* pHelmet = smart_cast<CHelmet*>(itm);
if (pWeapon || pOutfit || pHelmet)

if (itm->IsUsingCondition())
{
float cond = itm->GetCondition();

CEatableItem* eitm = smart_cast<CEatableItem*>(itm);
if (eitm)
{
const u16 max_uses = eitm->GetMaxUses();
if (max_uses > 1)
{
const u16 remaining_uses = eitm->GetRemainingUses();
if (remaining_uses < 1)
cond = 0.0f;
else if (max_uses > 8)
cond = (float)remaining_uses / (float)max_uses;
else
{
cond = (float)remaining_uses * 0.125f - 0.0625f;
m_pConditionState->ShowBackground(false);
}

m_pConditionState->m_bUseColor = false;
}
}

Ivector2 itm_grid_size = GetGridSize();
if (m_pParentList->GetVerticalPlacement())
std::swap(itm_grid_size.x, itm_grid_size.y);
Expand All @@ -219,7 +241,7 @@ void CUICellItem::UpdateConditionProgressBar()
float y = itm_grid_size.y * (cell_size.y + cell_space.y) - m_pConditionState->GetHeight() - 2.f;

m_pConditionState->SetWndPos(Fvector2().set(x, y));
m_pConditionState->SetProgressPos(iCeil(itm->GetCondition() * 13.0f) / 13.0f);
m_pConditionState->SetProgressPos(iCeil(cond * 13.0f) / 13.0f);
m_pConditionState->Show(true);
return;
}
Expand Down

0 comments on commit afc85df

Please sign in to comment.