Skip to content

Commit

Permalink
Leather Armor can now be dyed.
Browse files Browse the repository at this point in the history
* Created new color class to handle dye-related coloring
  • Loading branch information
SamJBarney committed Jul 15, 2015
1 parent 118aec1 commit 561296f
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 6 deletions.
38 changes: 38 additions & 0 deletions MCServer/crafting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,44 @@ GlisteringMelon = MelonSlice, 2:2 | GoldNugget, 1:1, 1:2, 1:3, 2:1, 2:3, 3:1
GoldNugget, 9 = GoldIngot, *
EnchantmentTable = Obsidian, 1:3, 2:3, 3:3, 2:2 | Diamond, 1:2, 3:2 | Book, 2:1

#******************************************************#
# Dyed Armor
# Do not modify
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherHelmet = LeatherHelmet^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *

LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherChestplate = LeatherChestplate^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *

LeatherPants = LeatherPants^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherPants = LeatherPants^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *

LeatherBoots = LeatherBoots^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *
LeatherBoots = LeatherBoots^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, * | Dye^-1, *

#******************************************************#
# Fireworks & Co.
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SET (SRCS
ChunkSender.cpp
ChunkStay.cpp
ClientHandle.cpp
Color.cpp
CommandOutput.cpp
CompositeChat.cpp
CraftingRecipes.cpp
Expand Down
76 changes: 76 additions & 0 deletions src/Color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Color.h"





#define COLOR_RED_BITS 0x00FF0000
#define COLOR_GREEN_BITS 0x0000FF00
#define COLOR_BLUE_BITS 0x000000FF
#define COLOR_RED_OFFSET 16
#define COLOR_GREEN_OFFSET 8





void cColor::SetColor(unsigned char a_Red, unsigned char a_Green, unsigned char a_Blue)
{
m_Color = (static_cast<unsigned int>(a_Red) << COLOR_RED_OFFSET) + (static_cast<unsigned int>(a_Green) << COLOR_GREEN_OFFSET) + (static_cast<unsigned int>(a_Blue));
}





void cColor::SetRed(unsigned char a_Red)
{
m_Color = (static_cast<unsigned int>(a_Red) << COLOR_RED_OFFSET) + ((COLOR_GREEN_BITS | COLOR_BLUE_BITS) & m_Color);
}





void cColor::SetGreen(unsigned char a_Green)
{
m_Color = (static_cast<unsigned int>(a_Green) << COLOR_GREEN_OFFSET) + ((COLOR_RED_BITS | COLOR_BLUE_BITS) & m_Color);
}





void cColor::SetBlue(unsigned char a_Blue)
{
m_Color = static_cast<unsigned int>(a_Blue) + ((COLOR_RED_BITS | COLOR_GREEN_BITS) & m_Color);
}





unsigned char cColor::GetRed() const
{
return (m_Color & COLOR_RED_BITS) >> COLOR_RED_OFFSET;
}





unsigned char cColor::GetGreen() const
{
return (m_Color & COLOR_GREEN_BITS) >> COLOR_GREEN_OFFSET;
}





unsigned char cColor::GetBlue() const
{
return m_Color & COLOR_BLUE_BITS;
}
58 changes: 58 additions & 0 deletions src/Color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

// Color.h

// Declares a class to handle item color related code





#pragma once

// tolua_begin

class cColor
{
public:

enum
{
COLOR_MIN = 0,
COLOR_MAX = 255,
COLOR_LIMIT = 256,
COLOR_NONE = 0xFFFFFFFF,
};
cColor() { m_Color = COLOR_NONE;}
cColor(unsigned char a_Red, unsigned char a_Green, unsigned char a_Blue) { SetColor(a_Red, a_Green, a_Blue); }

/// Returns whether the color is a valid color
bool IsValid() const { return m_Color != COLOR_NONE; }

/// Changes the color
void SetColor(unsigned char a_Red, unsigned char a_Green, unsigned char a_Blue);

/// Alters the red value of the color
void SetRed(unsigned char a_Red);

/// Alters the green value of the color
void SetGreen(unsigned char a_Red);

/// Alters the blue value of the color
void SetBlue(unsigned char a_Red);

/// Returns the red value of the color
unsigned char GetRed() const;

/// Returns the green value of the color
unsigned char GetGreen() const;

/// Returns the blue value of the color
unsigned char GetBlue() const;

/// Resets the color
void Clear() { m_Color = COLOR_NONE; }
// tolua_end

unsigned int m_Color;

};
192 changes: 192 additions & 0 deletions src/CraftingRecipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ cCraftingRecipes::cRecipe * cCraftingRecipes::MatchRecipe(const cItem * a_Crafti
// We use Recipe instead of a_Recipe because we want the wildcard ingredients' slot numbers as well, which was just added previously
HandleFireworks(a_CraftingGrid, Recipe.get(), a_GridStride, a_OffsetX, a_OffsetY);

// Handle Dyed Leather
HandleDyedLeather(a_CraftingGrid, Recipe.get(), a_GridStride, a_GridWidth, a_GridHeight);

return Recipe.release();
}

Expand Down Expand Up @@ -874,3 +877,192 @@ void cCraftingRecipes::HandleFireworks(const cItem * a_CraftingGrid, cCraftingRe




void cCraftingRecipes::HandleDyedLeather(const cItem * a_CraftingGrid, cCraftingRecipes::cRecipe * a_Recipe, int a_GridStride, int a_GridWidth, int a_GridHeight)
{
short result_type = a_Recipe->m_Result.m_ItemType;
if ((result_type == E_ITEM_LEATHER_CAP) || (result_type == E_ITEM_LEATHER_TUNIC) || (result_type == E_ITEM_LEATHER_PANTS) || (result_type == E_ITEM_LEATHER_BOOTS))
{
bool found = false;
cItem temp;

float red = 0;
float green = 0;
float blue = 0;
float dye_count = 0;

for (int x = 0; x < a_GridWidth; ++x)
{
for (int y = 0; y < a_GridHeight; ++y)
{
int GridIdx = x + a_GridStride * y;
if ((a_CraftingGrid[GridIdx].m_ItemType == result_type) && (found == false))
{
found = true;
temp = a_CraftingGrid[GridIdx].CopyOne();
// The original color of the item affects the result
if (temp.m_ItemColor.IsValid())
{
red += temp.m_ItemColor.GetRed();
green += temp.m_ItemColor.GetGreen();
blue += temp.m_ItemColor.GetBlue();
++dye_count;
}
}
else if (a_CraftingGrid[GridIdx].m_ItemType == E_ITEM_DYE)
{
switch (a_CraftingGrid[GridIdx].m_ItemDamage)
{
case E_META_DYE_BLACK:
{
red += 23;
green += 23;
blue += 23;
break;
}
case E_META_DYE_RED:
{
red += 142;
green += 47;
blue += 47;
break;
}
case E_META_DYE_GREEN:
{
red += 95;
green += 118;
blue += 47;
break;
}
case E_META_DYE_BROWN:
{
red += 95;
green += 71;
blue += 47;
break;
}
case E_META_DYE_BLUE:
{
red += 47;
green += 71;
blue += 165;
break;
}
case E_META_DYE_PURPLE:
{
red += 118;
green += 59;
blue += 165;
break;
}
case E_META_DYE_CYAN:
{
red += 71;
green += 118;
blue += 142;
break;
}
case E_META_DYE_LIGHTGRAY:
{
red += 142;
green += 142;
blue += 142;
break;
}
case E_META_DYE_GRAY:
{
red += 71;
green += 71;
blue += 71;
break;
}
case E_META_DYE_PINK:
{
red += 225;
green += 118;
blue += 153;
break;
}
case E_META_DYE_LIGHTGREEN:
{
red += 118;
green += 190;
blue += 23;
break;
}
case E_META_DYE_YELLOW:
{
red += 213;
green += 213;
blue += 47;
break;
}
case E_META_DYE_LIGHTBLUE:
{
red += 95;
green += 142;
blue += 201;
break;
}
case E_META_DYE_MAGENTA:
{
red += 165;
green += 71;
blue += 201;
break;
}
case E_META_DYE_ORANGE:
{
red += 201;
green += 118;
blue += 47;
break;
}
case E_META_DYE_WHITE:
{
red += 237;
green += 237;
blue += 237;
break;
}
}
++dye_count;
}
else if (a_CraftingGrid[GridIdx].m_ItemType != E_ITEM_EMPTY)
{
return;
}
}
}

if (!found)
{
return;
}

// Calculate the rgb values
double maximum = static_cast<double>(std::max({red, green, blue}));

double average_red = red / dye_count;
double average_green = green / dye_count;
double average_blue = blue / dye_count;
double average_max = maximum / dye_count;

double max_average = std::max({average_red, average_green, average_blue});

double gain_factor = average_max / max_average;


unsigned char result_red = static_cast<unsigned char>(average_red * gain_factor);
unsigned char result_green = static_cast<unsigned char>(average_green * gain_factor);
unsigned char result_blue = static_cast<unsigned char>(average_blue * gain_factor);

// Set the results values
a_Recipe->m_Result = temp;
a_Recipe->m_Result.m_ItemColor.SetColor(result_red, result_green, result_blue);
}
}




Loading

0 comments on commit 561296f

Please sign in to comment.