summaryrefslogtreecommitdiffstats
path: root/src/BlockEntities/FurnaceEntity.cpp
blob: 7d6d1f89ebedcc1d821d9fc01c4f4df25ac58a2b (plain) (blame)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "FurnaceEntity.h"
#include "../UI/Window.h"
#include "../Entities/Player.h"
#include "../Root.h"
#include "../Chunk.h"
#include "json/json.h"






enum
{
	PROGRESSBAR_SMELTING = 0,
	PROGRESSBAR_FUEL = 1,
} ;





cFurnaceEntity::cFurnaceEntity(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, cWorld * a_World) :
	super(E_BLOCK_FURNACE, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World),
	m_BlockType(a_BlockType),
	m_BlockMeta(a_BlockMeta),
	m_CurrentRecipe(NULL),
	m_IsCooking((a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ) == E_BLOCK_LIT_FURNACE)),
	m_NeedCookTime(0),
	m_TimeCooked(0),
	m_FuelBurnTime(0),
	m_TimeBurned(0),
	m_LastProgressFuel(0),
	m_LastProgressCook(0)
{
	cBlockEntityWindowOwner::SetBlockEntity(this);
	m_Contents.AddListener(*this);
}





cFurnaceEntity::~cFurnaceEntity()
{
	// Tell window its owner is destroyed
	cWindow * Window = GetWindow();
	if (Window != NULL)
	{
		Window->OwnerDestroyed();
	}
}





void cFurnaceEntity::UsedBy(cPlayer * a_Player)
{
	if (GetWindow() == NULL)
	{
		OpenWindow(new cFurnaceWindow(m_PosX, m_PosY, m_PosZ, this));
	}
	cWindow * Window = GetWindow();
	if (Window != NULL)
	{
		if (a_Player->GetWindow() != Window)
		{
			a_Player->OpenWindow(Window);
			BroadcastProgress(PROGRESSBAR_FUEL,     (short)m_LastProgressFuel);
			BroadcastProgress(PROGRESSBAR_SMELTING, (short)m_LastProgressCook);
		}
	}
}





/// Restarts cooking. Used after the furnace is loaded from storage to set up the internal variables so that cooking continues, if it was active. Returns true if cooking.
bool cFurnaceEntity::ContinueCooking(void)
{
	UpdateInput();
	UpdateFuel();
	return m_IsCooking;
}





bool cFurnaceEntity::Tick(float a_Dt, cChunk & a_Chunk)
{
	UNUSED(a_Dt);
	UNUSED(a_Chunk);
	if (m_FuelBurnTime <= 0)
	{
		// No fuel is burning, reset progressbars and bail out
		if ((m_LastProgressCook > 0) || (m_LastProgressFuel > 0))
		{
			UpdateProgressBars();
		}
		return false;
	}

	if (m_IsCooking)
	{
		m_TimeCooked++;
		if (m_TimeCooked >= m_NeedCookTime)
		{
			// Finished smelting one item
			FinishOne();
		}
	}
	
	m_TimeBurned++;
	if (m_TimeBurned >= m_FuelBurnTime)
	{
		// The current fuel has been exhausted, use another one, if possible
		BurnNewFuel();
	}
	
	UpdateProgressBars();
	
	return true;
}





bool cFurnaceEntity::LoadFromJson(const Json::Value & a_Value)
{
	m_PosX = a_Value.get("x", 0).asInt();
	m_PosY = a_Value.get("y", 0).asInt();
	m_PosZ = a_Value.get("z", 0).asInt();

	Json::Value AllSlots = a_Value.get("Slots", 0);
	int SlotIdx = 0;
	for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr)
	{
		cItem Item;
		Item.FromJson(*itr);
		SetSlot(SlotIdx, Item);
		SlotIdx++;
	}

	m_NeedCookTime = (int)(a_Value.get("CookTime",   0).asDouble() / 50);
	m_TimeCooked   = (int)(a_Value.get("TimeCooked", 0).asDouble() / 50);
	m_FuelBurnTime = (int)(a_Value.get("BurnTime",   0).asDouble() / 50);
	m_TimeBurned   = (int)(a_Value.get("TimeBurned", 0).asDouble() / 50);

	return true;
}





void cFurnaceEntity::SaveToJson( Json::Value& a_Value )
{
	a_Value["x"] = m_PosX;
	a_Value["y"] = m_PosY;
	a_Value["z"] = m_PosZ;

	Json::Value AllSlots;
	int NumSlots = m_Contents.GetNumSlots();
	for (int i = 0; i < NumSlots; i++)
	{
		Json::Value Slot;
		m_Contents.GetSlot(i).GetJson(Slot);
		AllSlots.append(Slot);
	}
	a_Value["Slots"] = AllSlots;

	a_Value["CookTime"]   = m_NeedCookTime * 50;
	a_Value["TimeCooked"] = m_TimeCooked   * 50;
	a_Value["BurnTime"]   = m_FuelBurnTime * 50;
	a_Value["TimeBurned"] = m_TimeBurned   * 50;
}





void cFurnaceEntity::SendTo(cClientHandle & a_Client)
{
	// Nothing needs to be sent
	UNUSED(a_Client);
}





void cFurnaceEntity::BroadcastProgress(int a_ProgressbarID, short a_Value)
{
	cWindow * Window = GetWindow();
	if (Window != NULL)
	{
		Window->BroadcastProgress(a_ProgressbarID, a_Value);
	}
}





/// One item finished cooking
void cFurnaceEntity::FinishOne()
{
	m_TimeCooked = 0;

	if (m_Contents.GetSlot(fsOutput).IsEmpty())
	{
		m_Contents.SetSlot(fsOutput, *m_CurrentRecipe->Out);
	}
	else
	{
		m_Contents.ChangeSlotCount(fsOutput, m_CurrentRecipe->Out->m_ItemCount);
	}
	m_Contents.ChangeSlotCount(fsInput, -m_CurrentRecipe->In->m_ItemCount);
	
	UpdateIsCooking();
}





void cFurnaceEntity::BurnNewFuel(void)
{
	cFurnaceRecipe * FR = cRoot::Get()->GetFurnaceRecipe();
	int NewTime = FR->GetBurnTime(m_Contents.GetSlot(fsFuel));
	if (NewTime == 0)
	{
		// The item in the fuel slot is not suitable
		m_FuelBurnTime = 0;
		m_TimeBurned = 0;
		SetIsCooking(false);
		return;
	}
	
	// Is the input and output ready for cooking?
	if (!CanCookInputToOutput())
	{
		return;
	}

	// Burn one new fuel:
	m_FuelBurnTime = NewTime;
	m_TimeBurned = 0;
	SetIsCooking(true);
	if (m_Contents.GetSlot(fsFuel).m_ItemType == E_ITEM_LAVA_BUCKET)
	{
		m_Contents.SetSlot(fsFuel, cItem(E_ITEM_BUCKET));
	}
	else
	{
		m_Contents.ChangeSlotCount(fsFuel, -1);
	}
}





void cFurnaceEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum)
{
	super::OnSlotChanged(a_ItemGrid, a_SlotNum);
	
	if (m_World == NULL)
	{
		// The furnace isn't initialized yet, do no processing
		return;
	}
	
	ASSERT(a_ItemGrid == &m_Contents);
	switch (a_SlotNum)
	{
		case fsInput:
		{
			UpdateInput();
			break;
		}
		
		case fsFuel:
		{
			UpdateFuel();
			break;
		}
		
		case fsOutput:
		{
			UpdateOutput();
			break;
		}
	}
}






/// Updates the current recipe, based on the current input
void cFurnaceEntity::UpdateInput(void)
{
	if (!m_Contents.GetSlot(fsInput).IsEqual(m_LastInput))
	{
		// The input is different from what we had before, reset the cooking time
		m_TimeCooked = 0;
	}
	m_LastInput = m_Contents.GetSlot(fsInput);
	
	cFurnaceRecipe * FR = cRoot::Get()->GetFurnaceRecipe();
	m_CurrentRecipe = FR->GetRecipeFrom(m_Contents.GetSlot(fsInput));
	if (!CanCookInputToOutput())
	{
		// This input cannot be cooked
		m_NeedCookTime = 0;
		SetIsCooking(false);
	}
	else
	{
		m_NeedCookTime = m_CurrentRecipe->CookTime;
		SetIsCooking(true);
		
		// Start burning new fuel if there's no flame now:
		if (GetFuelBurnTimeLeft() <= 0)
		{
			BurnNewFuel();
		}
	}
}





/// Called when the fuel slot changes or when the fuel is spent, burns another piece of fuel if appropriate
void cFurnaceEntity::UpdateFuel(void)
{
	if (m_FuelBurnTime > m_TimeBurned)
	{
		// The current fuel is still burning, don't modify anything:
		return;
	}
	
	// The current fuel is spent, try to burn some more:
	BurnNewFuel();
}





/// Called when the output slot changes; starts burning if space became available
void cFurnaceEntity::UpdateOutput(void)
{
	if (!CanCookInputToOutput())
	{
		// Cannot cook anymore:
		m_TimeCooked = 0;
		m_NeedCookTime = 0;
		SetIsCooking(false);
		return;
	}
	
	// No need to burn new fuel, the Tick() function will take care of that

	// Can cook, start cooking if not already underway:
	m_NeedCookTime = m_CurrentRecipe->CookTime;
	SetIsCooking(m_FuelBurnTime > 0);
}





/// Updates the m_IsCooking, based on the input slot, output slot and m_FuelBurnTime / m_TimeBurned
void cFurnaceEntity::UpdateIsCooking(void)
{
	if (
		!CanCookInputToOutput() ||        // Cannot cook this
		(m_FuelBurnTime <= 0) ||          // No fuel
		(m_TimeBurned >= m_FuelBurnTime)  // Fuel burnt out
	)
	{
		// Reset everything
		SetIsCooking(false);
		m_TimeCooked = 0;
		m_NeedCookTime = 0;
		return;
	}
	
	SetIsCooking(true);
}





/// Returns true if the input can be cooked into output and the item counts allow for another cooking operation
bool cFurnaceEntity::CanCookInputToOutput(void) const
{
	if (m_CurrentRecipe == NULL)
	{
		// This input cannot be cooked
		return false;
	}
	
	if (m_Contents.GetSlot(fsOutput).IsEmpty())
	{
		// The output is empty, can cook
		return true;
	}

	if (!m_Contents.GetSlot(fsOutput).IsEqual(*m_CurrentRecipe->Out))
	{
		// The output slot is blocked with something that cannot be stacked with the recipe's output
		return false;
	}
	
	if (m_Contents.GetSlot(fsOutput).IsFullStack())
	{
		// Cannot add any more items to the output slot
		return false;
	}
	
	return true;
}





/// Broadcasts progressbar updates, if needed
void cFurnaceEntity::UpdateProgressBars(void)
{
	// In order to preserve bandwidth, an update is sent only every 10th tick
	// That's why the comparisons use the division by eight
	
	int CurFuel = (m_FuelBurnTime > 0) ? (200 - 200 * m_TimeBurned / m_FuelBurnTime) : 0;
	if ((CurFuel / 8) != (m_LastProgressFuel / 8))
	{
		BroadcastProgress(PROGRESSBAR_FUEL, (short)CurFuel);
		m_LastProgressFuel = CurFuel;
	}
	
	int CurCook = (m_NeedCookTime > 0) ? (200 * m_TimeCooked / m_NeedCookTime) : 0;
	if ((CurCook / 8) != (m_LastProgressCook / 8))
	{
		BroadcastProgress(PROGRESSBAR_SMELTING, (short)CurCook);
		m_LastProgressCook = CurCook;
	}
}





void cFurnaceEntity::SetIsCooking(bool a_IsCooking)
{
	if (a_IsCooking == m_IsCooking)
	{
		return;
	}

	m_IsCooking = a_IsCooking;
		
	// Light or extinguish the furnace:
	m_World->FastSetBlock(m_PosX, m_PosY, m_PosZ, m_IsCooking ? E_BLOCK_LIT_FURNACE : E_BLOCK_FURNACE, m_BlockMeta);
}