1
0

Enchanting table improvements.

This commit is contained in:
Hownaer 2014-08-28 01:21:54 +02:00
parent e555cb4ab3
commit 0f1fd33123
5 changed files with 113 additions and 154 deletions

View File

@ -190,31 +190,23 @@ void cItem::FromJson(const Json::Value & a_Value)
bool cItem::IsEnchantable(short item) bool cItem::IsEnchantable(short a_ItemType)
{ {
if ((item >= 256) && (item <= 259)) if (ItemCategory::IsTool(a_ItemType) || ItemCategory::IsArmor(a_ItemType))
{ {
return true; return true;
} }
if ((item >= 267) && (item <= 279))
switch (a_ItemType)
{
case E_ITEM_BOOK:
case E_ITEM_BOW:
case E_ITEM_CARROT_ON_STICK:
case E_ITEM_FISHING_ROD:
case E_ITEM_SHEARS:
{ {
return true; return true;
} }
if ((item >= 283) && (item <= 286))
{
return true;
}
if ((item >= 290) && (item <= 294))
{
return true;
}
if ((item >= 298) && (item <= 317))
{
return true;
}
if ((item == 346) || (item == 359) || (item == 261))
{
return true;
} }
return false; return false;

View File

@ -207,7 +207,7 @@ void cSlotArea::ShiftClicked(cPlayer & a_Player, int a_SlotNum, const cItem & a_
m_ParentWindow.DistributeStack(Slot, a_Player, this, true); m_ParentWindow.DistributeStack(Slot, a_Player, this, true);
if (Slot.IsEmpty()) if (Slot.IsEmpty())
{ {
// Empty the slot completely, the cilent doesn't like left-over ItemType with zero count // Empty the slot completely, the client doesn't like left-over ItemType with zero count
Slot.Empty(); Slot.Empty();
} }
SetSlot(a_SlotNum, a_Player, Slot); SetSlot(a_SlotNum, a_Player, Slot);
@ -1389,8 +1389,11 @@ void cSlotAreaBeacon::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// cSlotAreaEnchanting: // cSlotAreaEnchanting:
cSlotAreaEnchanting::cSlotAreaEnchanting(cEnchantingWindow & a_ParentWindow) : cSlotAreaEnchanting::cSlotAreaEnchanting(cEnchantingWindow & a_ParentWindow, int a_BlockX, int a_BlockY, int a_BlockZ) :
cSlotAreaTemporary(1, a_ParentWindow) cSlotAreaTemporary(1, a_ParentWindow),
m_BlockX(a_BlockX),
m_BlockY(a_BlockY),
m_BlockZ(a_BlockZ)
{ {
a_ParentWindow.m_SlotArea = this; a_ParentWindow.m_SlotArea = this;
} }
@ -1420,7 +1423,7 @@ void cSlotAreaEnchanting::Clicked(cPlayer & a_Player, int a_SlotNum, eClickActio
} }
case caDblClick: case caDblClick:
{ {
DblClicked(a_Player, a_SlotNum); // DblClicked(a_Player, a_SlotNum);
return; return;
} }
case caMiddleClick: case caMiddleClick:
@ -1428,6 +1431,25 @@ void cSlotAreaEnchanting::Clicked(cPlayer & a_Player, int a_SlotNum, eClickActio
MiddleClicked(a_Player, a_SlotNum); MiddleClicked(a_Player, a_SlotNum);
return; return;
} }
case caDropKey:
case caCtrlDropKey:
{
DropClicked(a_Player, a_SlotNum, false);
return;
}
case caNumber1:
case caNumber2:
case caNumber3:
case caNumber4:
case caNumber5:
case caNumber6:
case caNumber7:
case caNumber8:
case caNumber9:
{
NumberClicked(a_Player, a_SlotNum, a_ClickAction);
return;
}
default: default:
{ {
break; break;
@ -1443,107 +1465,38 @@ void cSlotAreaEnchanting::Clicked(cPlayer & a_Player, int a_SlotNum, eClickActio
bAsync = true; bAsync = true;
} }
cItem & DraggingItem = a_Player.GetDraggingItem(); cItem & DraggingItem = a_Player.GetDraggingItem();
switch (a_ClickAction)
{
case caRightClick:
{
// Right-clicked
if (DraggingItem.IsEmpty()) if (DraggingItem.IsEmpty())
{ {
DraggingItem = Slot.CopyOne();
Slot.Empty();
break;
}
if (Slot.IsEmpty())
{
Slot = DraggingItem.CopyOne();
DraggingItem.m_ItemCount -= 1;
if (DraggingItem.m_ItemCount <= 0)
{
DraggingItem.Empty();
}
}
else if ((!DraggingItem.IsEqual(Slot)) && (DraggingItem.m_ItemCount == 1))
{
// Swap contents
cItem tmp(DraggingItem);
DraggingItem = Slot;
Slot = tmp;
}
break;
}
case caLeftClick:
{
// Left-clicked
if (DraggingItem.IsEmpty())
{
DraggingItem = Slot.CopyOne();
Slot.Empty();
break;
}
if (DraggingItem.IsEqual(Slot))
{
// Do nothing
break;
}
if (!Slot.IsEmpty()) if (!Slot.IsEmpty())
{ {
if (DraggingItem.m_ItemCount == 1)
{
// Swap contents
cItem tmp(DraggingItem);
DraggingItem = Slot; DraggingItem = Slot;
Slot = tmp; Slot.Empty();
} }
} }
else else if (Slot.IsEmpty())
{ {
Slot = DraggingItem.CopyOne(); Slot = DraggingItem.CopyOne();
DraggingItem.m_ItemCount -= 1; DraggingItem.m_ItemCount -= 1;
if (DraggingItem.m_ItemCount <= 0) if (DraggingItem.m_ItemCount <= 0)
{ {
DraggingItem.Empty(); DraggingItem.Empty();
} }
} }
break; else if ((DraggingItem.m_ItemCount == 1) && !DraggingItem.IsEqual(Slot))
}
default:
{ {
LOGWARNING("SlotArea: Unhandled click action: %d (%s)", a_ClickAction, ClickActionToString(a_ClickAction)); // Switch contents
m_ParentWindow.BroadcastWholeWindow(); cItem tmp(DraggingItem);
return; DraggingItem = Slot;
Slot = tmp;
} }
} // switch (a_ClickAction
SetSlot(a_SlotNum, a_Player, Slot); SetSlot(a_SlotNum, a_Player, Slot);
if (bAsync) if (bAsync)
{ {
m_ParentWindow.BroadcastWholeWindow(); m_ParentWindow.BroadcastWholeWindow();
} }
UpdateResult(a_Player);
}
void cSlotAreaEnchanting::DblClicked(cPlayer & a_Player, int a_SlotNum)
{
cItem & Dragging = a_Player.GetDraggingItem();
if ((!Dragging.IsEmpty()) || (a_SlotNum != 0))
{
return;
}
cItem Item = *GetSlot(0, a_Player);
if (!m_ParentWindow.CollectItemsToHand(Item, *this, a_Player, false))
{
m_ParentWindow.CollectItemsToHand(Item, *this, a_Player, true);
}
} }
@ -1567,7 +1520,15 @@ void cSlotAreaEnchanting::DistributeStack(cItem & a_ItemStack, cPlayer & a_Playe
{ {
a_ItemStack.Empty(); a_ItemStack.Empty();
} }
}
void cSlotAreaEnchanting::OnPlayerAdded(cPlayer & a_Player)
{
super::OnPlayerAdded(a_Player);
UpdateResult(a_Player); UpdateResult(a_Player);
} }
@ -1587,29 +1548,33 @@ void cSlotAreaEnchanting::OnPlayerRemoved(cPlayer & a_Player)
void cSlotAreaEnchanting::SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item)
{
super::SetSlot(a_SlotNum, a_Player, a_Item);
UpdateResult(a_Player);
}
void cSlotAreaEnchanting::UpdateResult(cPlayer & a_Player) void cSlotAreaEnchanting::UpdateResult(cPlayer & a_Player)
{ {
cItem Item = *GetSlot(0, a_Player); cItem Item = *GetSlot(0, a_Player);
if (Item.IsEmpty() || !Item.m_Enchantments.IsEmpty()) if (cItem::IsEnchantable(Item.m_ItemType) && Item.m_Enchantments.IsEmpty())
{
m_ParentWindow.SetProperty(0, 0, a_Player);
m_ParentWindow.SetProperty(1, 0, a_Player);
m_ParentWindow.SetProperty(2, 0, a_Player);
}
else if (cItem::IsEnchantable(Item.m_ItemType) || Item.m_ItemType == E_ITEM_BOOK)
{ {
int Bookshelves = std::min(GetBookshelvesCount(a_Player.GetWorld()), 15); int Bookshelves = std::min(GetBookshelvesCount(a_Player.GetWorld()), 15);
cFastRandom Random; cFastRandom Random;
int base = (Random.GenerateRandomInteger(1, 8) + (int)floor((float)Bookshelves / 2) + Random.GenerateRandomInteger(0, Bookshelves)); int Base = (Random.GenerateRandomInteger(1, 8) + (int)floor((float)Bookshelves / 2) + Random.GenerateRandomInteger(0, Bookshelves));
int topSlot = std::max(base / 3, 1); int TopSlot = std::max(Base / 3, 1);
int middleSlot = (base * 2) / 3 + 1; int MiddleSlot = (Base * 2) / 3 + 1;
int bottomSlot = std::max(base, Bookshelves * 2); int BottomSlot = std::max(Base, Bookshelves * 2);
m_ParentWindow.SetProperty(0, topSlot, a_Player); m_ParentWindow.SetProperty(0, TopSlot, a_Player);
m_ParentWindow.SetProperty(1, middleSlot, a_Player); m_ParentWindow.SetProperty(1, MiddleSlot, a_Player);
m_ParentWindow.SetProperty(2, bottomSlot, a_Player); m_ParentWindow.SetProperty(2, BottomSlot, a_Player);
} }
else else
{ {
@ -1625,12 +1590,9 @@ void cSlotAreaEnchanting::UpdateResult(cPlayer & a_Player)
int cSlotAreaEnchanting::GetBookshelvesCount(cWorld * a_World) int cSlotAreaEnchanting::GetBookshelvesCount(cWorld * a_World)
{ {
int PosX, PosY, PosZ;
((cEnchantingWindow*)&m_ParentWindow)->GetBlockPos(PosX, PosY, PosZ);
int Bookshelves = 0; int Bookshelves = 0;
cBlockArea Area; cBlockArea Area;
Area.Read(a_World, PosX - 2, PosX + 2, PosY, PosY + 1, PosZ - 2, PosZ + 2); Area.Read(a_World, m_BlockX - 2, m_BlockX + 2, m_BlockY, m_BlockY + 1, m_BlockZ - 2, m_BlockZ + 2);
static const struct static const struct
{ {

View File

@ -349,14 +349,15 @@ class cSlotAreaEnchanting :
typedef cSlotAreaTemporary super; typedef cSlotAreaTemporary super;
public: public:
cSlotAreaEnchanting(cEnchantingWindow & a_ParentWindow); cSlotAreaEnchanting(cEnchantingWindow & a_ParentWindow, int a_BlockX, int a_BlockY, int a_BlockZ);
// cSlotArea overrides: // cSlotArea overrides:
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override; virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
virtual void DblClicked(cPlayer & a_Player, int a_SlotNum) override;
virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) override; virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots) override;
virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
// cSlotAreaTemporary overrides: // cSlotAreaTemporary overrides:
virtual void OnPlayerAdded (cPlayer & a_Player) override;
virtual void OnPlayerRemoved(cPlayer & a_Player) override; virtual void OnPlayerRemoved(cPlayer & a_Player) override;
/* Get the count of bookshelves who stand in the near of the enchanting table */ /* Get the count of bookshelves who stand in the near of the enchanting table */
@ -365,6 +366,8 @@ public:
protected: protected:
/** Handles a click in the item slot. */ /** Handles a click in the item slot. */
void UpdateResult(cPlayer & a_Player); void UpdateResult(cPlayer & a_Player);
int m_BlockX, m_BlockY, m_BlockZ;
}; };

View File

@ -881,7 +881,7 @@ cEnchantingWindow::cEnchantingWindow(int a_BlockX, int a_BlockY, int a_BlockZ) :
m_BlockY(a_BlockY), m_BlockY(a_BlockY),
m_BlockZ(a_BlockZ) m_BlockZ(a_BlockZ)
{ {
m_SlotAreas.push_back(new cSlotAreaEnchanting(*this)); m_SlotAreas.push_back(new cSlotAreaEnchanting(*this, m_BlockX, m_BlockY, m_BlockZ));
m_SlotAreas.push_back(new cSlotAreaInventory(*this)); m_SlotAreas.push_back(new cSlotAreaInventory(*this));
m_SlotAreas.push_back(new cSlotAreaHotBar(*this)); m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
} }
@ -892,8 +892,13 @@ cEnchantingWindow::cEnchantingWindow(int a_BlockX, int a_BlockY, int a_BlockZ) :
void cEnchantingWindow::SetProperty(int a_Property, int a_Value) void cEnchantingWindow::SetProperty(int a_Property, int a_Value)
{ {
m_PropertyValue[a_Property] = a_Value; if ((a_Property < 0) || ((size_t)a_Property >= ARRAYCOUNT(m_PropertyValue)))
{
ASSERT(!"a_Property is invalid");
return;
}
m_PropertyValue[a_Property] = a_Value;
super::SetProperty(a_Property, a_Value); super::SetProperty(a_Property, a_Value);
} }
@ -903,8 +908,13 @@ void cEnchantingWindow::SetProperty(int a_Property, int a_Value)
void cEnchantingWindow::SetProperty(int a_Property, int a_Value, cPlayer & a_Player) void cEnchantingWindow::SetProperty(int a_Property, int a_Value, cPlayer & a_Player)
{ {
m_PropertyValue[a_Property] = a_Value; if ((a_Property < 0) || ((size_t)a_Property >= ARRAYCOUNT(m_PropertyValue)))
{
ASSERT(!"a_Property is invalid");
return;
}
m_PropertyValue[a_Property] = a_Value;
super::SetProperty(a_Property, a_Value, a_Player); super::SetProperty(a_Property, a_Value, a_Player);
} }
@ -914,18 +924,13 @@ void cEnchantingWindow::SetProperty(int a_Property, int a_Value, cPlayer & a_Pla
int cEnchantingWindow::GetPropertyValue(int a_Property) int cEnchantingWindow::GetPropertyValue(int a_Property)
{ {
return m_PropertyValue[a_Property]; if ((a_Property < 0) || ((size_t)a_Property >= ARRAYCOUNT(m_PropertyValue)))
{
ASSERT(!"a_Property is invalid");
return 0;
} }
return m_PropertyValue[a_Property];
void cEnchantingWindow::GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ)
{
a_PosX = m_BlockX;
a_PosY = m_BlockY;
a_PosZ = m_BlockZ;
} }

View File

@ -291,9 +291,6 @@ public:
/** Return the Value of a Property */ /** Return the Value of a Property */
int GetPropertyValue(int a_Property); int GetPropertyValue(int a_Property);
/** Get the Position from the Enchantment Table */
void GetBlockPos(int & a_PosX, int & a_PosY, int & a_PosZ);
cSlotArea * m_SlotArea; cSlotArea * m_SlotArea;
protected: protected: