1
0
Fork 0

Add anvil window and slot area.

This commit is contained in:
Howaner 2014-05-01 00:47:57 +02:00
parent 3c40544336
commit 3c102870f6
13 changed files with 356 additions and 0 deletions

View File

@ -23,6 +23,13 @@ public:
{
a_Pickups.push_back(cItem(E_BLOCK_ANVIL, 1, a_BlockMeta >> 2));
}
virtual void OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
{
cWindow * Window = new cAnvilWindow();
a_Player->OpenWindow(Window);
}
virtual bool GetPlacementBlockTypeMeta(

View File

@ -83,6 +83,15 @@ void cEnchantments::AddFromString(const AString & a_StringSpec)
int cEnchantments::Size(void)
{
return (int)m_Enchantments.size();
}
AString cEnchantments::ToString(void) const
{
// Serialize all the enchantments into a string

View File

@ -84,6 +84,9 @@ public:
/** Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it */
void AddFromString(const AString & a_StringSpec);
/** Get the count of enchantments */
int Size(void);
/** Serializes all the enchantments into a string */
AString ToString(void) const;

View File

@ -60,6 +60,49 @@ public:
return true;
}
virtual bool CanRepairWithItem(const cItem & a_Item) override
{
switch (m_ItemType)
{
case E_ITEM_CHAIN_BOOTS:
case E_ITEM_CHAIN_CHESTPLATE:
case E_ITEM_CHAIN_HELMET:
case E_ITEM_CHAIN_LEGGINGS:
{
return (a_Item.m_ItemType == E_ITEM_IRON);
}
case E_ITEM_DIAMOND_BOOTS:
case E_ITEM_DIAMOND_CHESTPLATE:
case E_ITEM_DIAMOND_HELMET:
case E_ITEM_DIAMOND_LEGGINGS:
{
return (a_Item.m_ItemType == E_ITEM_DIAMOND);
}
case E_ITEM_IRON_BOOTS:
case E_ITEM_IRON_CHESTPLATE:
case E_ITEM_IRON_HELMET:
case E_ITEM_IRON_LEGGINGS:
{
return (a_Item.m_ItemType == E_ITEM_IRON);
}
case E_ITEM_GOLD_BOOTS:
case E_ITEM_GOLD_CHESTPLATE:
case E_ITEM_GOLD_HELMET:
case E_ITEM_GOLD_LEGGINGS:
{
return (a_Item.m_ItemType == E_ITEM_GOLD);
}
case E_ITEM_LEATHER_BOOTS:
case E_ITEM_LEATHER_CAP:
case E_ITEM_LEATHER_PANTS:
case E_ITEM_LEATHER_TUNIC:
{
return (a_Item.m_ItemType == E_ITEM_LEATHER);
}
}
return false;
}
} ;

View File

@ -511,6 +511,25 @@ bool cItemHandler::IsPlaceable(void)
bool cItemHandler::CanRepairWithItem(const cItem & a_Item)
{
return false;
}
int cItemHandler::GetRepairCost(void)
{
return 0;
}
bool cItemHandler::CanHarvestBlock(BLOCKTYPE a_BlockType)
{
UNUSED(a_BlockType);

View File

@ -85,6 +85,12 @@ public:
/** Blocks simply get placed */
virtual bool IsPlaceable(void);
/** Can the anvil repair this item, when a_Item is the second input? */
virtual bool CanRepairWithItem(const cItem & a_Item);
/** Get the repair cost from the item, or 0 if the item hasn't repair cost. */
virtual int GetRepairCost(void);
/** Called before a block is placed into a world.
The handler should return true to allow placement, false to refuse.
Also, the handler should set a_BlockType and a_BlockMeta to correct values for the newly placed block.

View File

@ -85,6 +85,19 @@ public:
}
return false;
}
virtual bool CanRepairWithItem(const cItem & a_Item) override
{
switch (m_ItemType)
{
case E_ITEM_WOODEN_PICKAXE: return (a_Item.m_ItemType == E_BLOCK_PLANKS);
case E_ITEM_STONE_PICKAXE: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE);
case E_ITEM_IRON_PICKAXE: return (a_Item.m_ItemType == E_ITEM_IRON);
case E_ITEM_GOLD_PICKAXE: return (a_Item.m_ItemType == E_ITEM_GOLD);
case E_ITEM_DIAMOND_PICKAXE: return (a_Item.m_ItemType == E_ITEM_DIAMOND);
}
return false;
}
} ;

View File

@ -41,4 +41,18 @@ public:
{
return (a_BlockType == E_BLOCK_SNOW);
}
virtual bool CanRepairWithItem(const cItem & a_Item) override
{
switch (m_ItemType)
{
case E_ITEM_WOODEN_SHOVEL: return (a_Item.m_ItemType == E_BLOCK_PLANKS);
case E_ITEM_STONE_SHOVEL: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE);
case E_ITEM_IRON_SHOVEL: return (a_Item.m_ItemType == E_ITEM_IRON);
case E_ITEM_GOLD_SHOVEL: return (a_Item.m_ItemType == E_ITEM_GOLD);
case E_ITEM_DIAMOND_SHOVEL: return (a_Item.m_ItemType == E_ITEM_DIAMOND);
}
return false;
}
};

View File

@ -23,6 +23,19 @@ public:
{
return (a_BlockType == E_BLOCK_COBWEB);
}
virtual bool CanRepairWithItem(const cItem & a_Item) override
{
switch (m_ItemType)
{
case E_ITEM_WOODEN_SWORD: return (a_Item.m_ItemType == E_BLOCK_PLANKS);
case E_ITEM_STONE_SWORD: return (a_Item.m_ItemType == E_BLOCK_COBBLESTONE);
case E_ITEM_IRON_SWORD: return (a_Item.m_ItemType == E_ITEM_IRON);
case E_ITEM_GOLD_SWORD: return (a_Item.m_ItemType == E_ITEM_GOLD);
case E_ITEM_DIAMOND_SWORD: return (a_Item.m_ItemType == E_ITEM_DIAMOND);
}
return false;
}
} ;

View File

@ -595,6 +595,179 @@ cCraftingRecipe & cSlotAreaCrafting::GetRecipeForPlayer(cPlayer & a_Player)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSlotAreaAnvil:
cSlotAreaAnvil::cSlotAreaAnvil(cAnvilWindow & a_ParentWindow) :
cSlotAreaTemporary(3, a_ParentWindow),
m_MaximumCost(0),
m_RepairedItemName("")
{
}
void cSlotAreaAnvil::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
{
super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem);
UpdateResult(a_Player);
}
void cSlotAreaAnvil::OnPlayerRemoved(cPlayer & a_Player)
{
TossItems(a_Player, 0, 3);
super::OnPlayerRemoved(a_Player);
}
void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player)
{
cItem Input(*GetSlot(0, a_Player));
cItem SecondInput(*GetSlot(1, a_Player));
cItem Output(*GetSlot(2, a_Player));
if (Input.IsEmpty() && !Output.IsEmpty())
{
Output.Empty();
SetSlot(2, a_Player, Output);
m_ParentWindow.SetProperty(0, 0, a_Player);
return;
}
int RepairCost = cItemHandler::GetItemHandler(Input)->GetRepairCost();
int NeedExp = 0;
if (!SecondInput.IsEmpty())
{
RepairCost += cItemHandler::GetItemHandler(SecondInput)->GetRepairCost();
if (Input.IsDamageable() && cItemHandler::GetItemHandler(Input)->CanRepairWithItem(SecondInput))
{
// Tool and armor repair with special item (iron / gold / diamond / ...)
int DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4);
if (DamageDiff < 0)
{
// No enchantment
Output.Empty();
SetSlot(2, a_Player, Output);
m_ParentWindow.SetProperty(0, 0, a_Player);
return;
}
int x = 0;
while ((DamageDiff > 0) && (x < SecondInput.m_ItemCount))
{
Input.m_ItemDamage -= DamageDiff;
NeedExp += std::max(1, DamageDiff / 100) + Input.m_Enchantments.Size();
DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4);
++x;
}
}
else
{
// Tool and armor repair with two tools / armors
if (!Input.IsSameType(SecondInput) || !Input.IsDamageable())
{
// No enchantment
Output.Empty();
SetSlot(2, a_Player, Output);
m_ParentWindow.SetProperty(0, 0, a_Player);
return;
}
int FirstDamageDiff = Input.GetMaxDamage() - Input.m_ItemDamage;
int SecondDamageDiff = SecondInput.GetMaxDamage() - SecondInput.m_ItemDamage;
int Damage = SecondDamageDiff + Input.GetMaxDamage() * 12 / 100;
int NewItemDamage = Input.GetMaxDamage() - (FirstDamageDiff + Damage);
if (NewItemDamage > 0)
{
NewItemDamage = 0;
}
if (NewItemDamage < Input.m_ItemDamage)
{
Input.m_ItemDamage = NewItemDamage;
NeedExp += std::max(1, Damage / 100);
}
// TODO: Add enchantments.
}
}
int NameChangeExp = 0;
if (m_RepairedItemName.empty())
{
// Remove custom name
if (!Input.m_CustomName.empty())
{
NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5);
NeedExp += NameChangeExp;
Input.m_CustomName = "";
}
}
else if (m_RepairedItemName != Input.m_CustomName)
{
// Change custom name
NameChangeExp = (Input.IsDamageable()) ? 4 : (Input.m_ItemCount * 5);
NeedExp += NameChangeExp;
if (!Input.m_CustomName.empty())
{
RepairCost += NameChangeExp / 2;
}
Input.m_CustomName = m_RepairedItemName;
}
// TODO: Add enchantment exp cost.
int MaximumCost = RepairCost + NeedExp;
if (NeedExp < 0)
{
Input.Empty();
}
if (NameChangeExp == NeedExp && NameChangeExp > 0 && MaximumCost >= 40)
{
MaximumCost = 39;
}
if (MaximumCost >= 40 && !a_Player.IsGameModeCreative())
{
Input.Empty();
}
/* TODO: Add repair cost to cItem and not ItemHandler. This is required for this function!
if (!Input.IsEmpty())
{
RepairCost = max(cItemHandler::GetItemHandler(Input)->GetRepairCost(), cItemHandler::GetItemHandler(SecondInput)->GetRepairCost());
if (!Input.m_CustomName.empty())
{
RepairCost -= 9;
}
RepairCost = max(RepairCost, 0);
RepairCost += 2;
}*/
SetSlot(2, a_Player, Input);
m_ParentWindow.SetProperty(0, MaximumCost, a_Player);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSlotAreaEnchanting:

View File

@ -9,6 +9,7 @@
#pragma once
#include "../Inventory.h"
#include "Window.h"
@ -259,6 +260,34 @@ protected:
class cSlotAreaAnvil :
public cSlotAreaTemporary
{
typedef cSlotAreaTemporary super;
public:
cSlotAreaAnvil(cAnvilWindow & a_ParentWindow);
// cSlotArea overrides:
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
// cSlotAreaTemporary overrides:
virtual void OnPlayerRemoved(cPlayer & a_Player) override;
protected:
/** Handles a click in the item slot. */
void UpdateResult(cPlayer & a_Player);
/** The maximum cost of repairing/renaming in the anvil. */
int m_MaximumCost;
AString m_RepairedItemName;
} ;
class cSlotAreaEnchanting :
public cSlotAreaTemporary
{

View File

@ -804,6 +804,21 @@ cCraftingWindow::cCraftingWindow(int a_BlockX, int a_BlockY, int a_BlockZ) :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cAnvilWindow:
cAnvilWindow::cAnvilWindow() :
cWindow(wtAnvil, "Repair")
{
m_SlotAreas.push_back(new cSlotAreaAnvil(*this));
m_SlotAreas.push_back(new cSlotAreaInventory(*this));
m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cEnchantingWindow:

View File

@ -231,6 +231,18 @@ public:
class cAnvilWindow :
public cWindow
{
typedef cWindow super;
public:
cAnvilWindow();
} ;
class cEnchantingWindow :
public cWindow
{