aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/ArmorDyeRecipe.cpp
blob: d45811822aa8d31d7e63fa3b81fb14114a8a8da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "stdafx.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.item.crafting.h"
#include "ArmorDyeRecipe.h"

bool ArmorDyeRecipe::matches(shared_ptr<CraftingContainer> craftSlots, Level *level)
{
	shared_ptr<ItemInstance> target = nullptr;
	vector<shared_ptr<ItemInstance> > dyes;

	for (int slot = 0; slot < craftSlots->getContainerSize(); slot++)
	{
		shared_ptr<ItemInstance> item = craftSlots->getItem(slot);
		if (item == nullptr) continue;

		ArmorItem *armor = dynamic_cast<ArmorItem *>(item->getItem());
		if (armor)
		{
			if (armor->getMaterial() == ArmorItem::ArmorMaterial::CLOTH && target == nullptr)
			{
				target = item;
			}
			else
			{
				return false;
			}
		}
		else if (item->id == Item::dye_powder_Id)
		{
			dyes.push_back(item);
		}
		else
		{
			return false;
		}
	}

	return target != nullptr && !dyes.empty();
}

shared_ptr<ItemInstance> ArmorDyeRecipe::assembleDyedArmor(shared_ptr<CraftingContainer> craftSlots)
{
	shared_ptr<ItemInstance> target = nullptr;
	int colorTotals[3] = {0,0,0};
	int intensityTotal = 0;
	int colourCounts = 0;
	ArmorItem *armor = nullptr;

	if(craftSlots != nullptr)
	{
		for (int slot = 0; slot < craftSlots->getContainerSize(); slot++)
		{
			shared_ptr<ItemInstance> item = craftSlots->getItem(slot);
			if (item == nullptr) continue;

			armor = dynamic_cast<ArmorItem *>(item->getItem());
			if (armor)
			{
				if (armor->getMaterial() == ArmorItem::ArmorMaterial::CLOTH && target == nullptr)
				{
					target = item->copy();
					target->count = 1;

					if (armor->hasCustomColor(item))
					{
						int color = armor->getColor(target);
						float red = static_cast<float>((color >> 16) & 0xFF) / 0xFF;
						float green = static_cast<float>((color >> 8) & 0xFF) / 0xFF;
						float blue = static_cast<float>(color & 0xFF) / 0xFF;

						intensityTotal += max(red, max(green, blue)) * 0xFF;

						colorTotals[0] += red * 0xFF;
						colorTotals[1] += green * 0xFF;
						colorTotals[2] += blue * 0xFF;
						colourCounts++;
					}
				}
				else
				{
					return nullptr;
				}
			}
			else if (item->id == Item::dye_powder_Id)
			{
				int tileData = ColoredTile::getTileDataForItemAuxValue(item->getAuxValue());
				int red = static_cast<int>(Sheep::COLOR[tileData][0] * 0xFF);
				int green = static_cast<int>(Sheep::COLOR[tileData][1] * 0xFF);
				int blue = static_cast<int>(Sheep::COLOR[tileData][2] * 0xFF);

				intensityTotal += max(red, max(green, blue));

				colorTotals[0] += red;
				colorTotals[1] += green;
				colorTotals[2] += blue;
				colourCounts++;
			}
			else
			{
				return nullptr;
			}
		}
	}

	if (armor == nullptr) return nullptr;

	int red = (colorTotals[0] / colourCounts);
	int green = (colorTotals[1] / colourCounts);
	int blue = (colorTotals[2] / colourCounts);

	float averageIntensity = static_cast<float>(intensityTotal) / colourCounts;
	float resultIntensity = static_cast<float>(max(red, max(green, blue)));
	//        System.out.println(averageIntensity + ", " + resultIntensity);

	red = static_cast<int>((float)red * averageIntensity / resultIntensity);
	green = static_cast<int>((float)green * averageIntensity / resultIntensity);
	blue = static_cast<int>((float)blue * averageIntensity / resultIntensity);

	int rgb = red;
	rgb = (rgb << 8) + green;
	rgb = (rgb << 8) + blue;

	armor->setColor(target, rgb);
	return target;
}

shared_ptr<ItemInstance> ArmorDyeRecipe::assemble(shared_ptr<CraftingContainer> craftSlots)
{
	return ArmorDyeRecipe::assembleDyedArmor(craftSlots);
}

int ArmorDyeRecipe::size()
{
	return 10;
}

const ItemInstance *ArmorDyeRecipe::getResultItem()
{
	return nullptr;
}

const int ArmorDyeRecipe::getGroup()
{
	return ShapedRecipy::eGroupType_Armour;
}

// 4J-PB
bool ArmorDyeRecipe::reqs(int iRecipe)
{
	return false;
}

void ArmorDyeRecipe::reqs(INGREDIENTS_REQUIRED *pIngReq)
{
	//int iCount=0;
	//bool bFound;
	//int j;
	INGREDIENTS_REQUIRED TempIngReq;

	// shapeless doesn't have the 3x3 shape, but we'll just use this to store the ingredients anyway
	TempIngReq.iIngC=0;
	TempIngReq.iType = RECIPE_TYPE_2x2; // all the dyes can be made in a 2x2
	TempIngReq.uiGridA = new unsigned int [9];
	TempIngReq.iIngIDA= new int [3*3];
	TempIngReq.iIngValA = new int [3*3];
	TempIngReq.iIngAuxValA = new int [3*3];

	ZeroMemory(TempIngReq.iIngIDA,sizeof(int)*9);
	ZeroMemory(TempIngReq.iIngValA,sizeof(int)*9);
	memset(TempIngReq.iIngAuxValA,Recipes::ANY_AUX_VALUE,sizeof(int)*9);
	ZeroMemory(TempIngReq.uiGridA,sizeof(unsigned int)*9);

#if 0
	for ( ItemInstance *expected : *ingredients )
	{
		if (expected!=nullptr) 
		{			
			int iAuxVal = expected->getAuxValue();
			TempIngReq.uiGridA[iCount++]=expected->id | iAuxVal<<24;
			// 4J-PB - put the ingredients in boxes 1,2,4,5 so we can see them in a 2x2 crafting screen
			if(iCount==2) iCount=3;
			bFound=false;
			for(j=0;j<TempIngReq.iIngC;j++)
			{
				if((TempIngReq.iIngIDA[j]==expected->id) && (iAuxVal == Recipes::ANY_AUX_VALUE || TempIngReq.iIngAuxValA[j] == iAuxVal))
				{
					bFound= true;
					break;
				}
			}
			if(bFound)
			{
				TempIngReq.iIngValA[j]++;
			}
			else
			{
				TempIngReq.iIngIDA[TempIngReq.iIngC]=expected->id;
				TempIngReq.iIngAuxValA[TempIngReq.iIngC]=iAuxVal;
				TempIngReq.iIngValA[TempIngReq.iIngC++]++;
			}
		}
	}
#endif

	pIngReq->iIngIDA = new int [TempIngReq.iIngC];
	pIngReq->iIngValA = new int [TempIngReq.iIngC];
	pIngReq->iIngAuxValA = new int [TempIngReq.iIngC];
	pIngReq->uiGridA = new unsigned int [9];

	pIngReq->pRecipy=this;

	for(unsigned int i = 0; i < XUSER_MAX_COUNT; ++i)
	{
		pIngReq->bCanMake[i]=false;
	}

	pIngReq->iIngC=TempIngReq.iIngC;
	pIngReq->iType=TempIngReq.iType;

	if(pIngReq->iIngC!=0)
	{
		memcpy(pIngReq->iIngIDA,TempIngReq.iIngIDA,sizeof(int)*TempIngReq.iIngC);
		memcpy(pIngReq->iIngValA,TempIngReq.iIngValA,sizeof(int)*TempIngReq.iIngC);
		memcpy(pIngReq->iIngAuxValA,TempIngReq.iIngAuxValA,sizeof(int)*TempIngReq.iIngC);
	}
	memcpy(pIngReq->uiGridA,TempIngReq.uiGridA,sizeof(unsigned int) *9);

	delete [] TempIngReq.iIngIDA;
	delete [] TempIngReq.iIngValA;
	delete [] TempIngReq.iIngAuxValA;
	delete [] TempIngReq.uiGridA;
}