summaryrefslogtreecommitdiffstats
path: root/source/Entities/Minecart.cpp
blob: a148886a63eb5efb2ff97baf74e95aaac7142092 (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

// Minecart.cpp

// Implements the cMinecart class representing a minecart in the world
// Indiana Jones!

#include "Globals.h"
#include "Minecart.h"
#include "../World.h"
#include "../ClientHandle.h"
#include "Player.h"





cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) :
	super(etMinecart, a_X, a_Y, a_Z, 0.98, 0.7),
	m_Payload(a_Payload)
{
	SetMass(20.f);
	SetMaxHealth(6);
}




bool cMinecart::Initialize(cWorld * a_World)
{
	if (super::Initialize(a_World))
	{
		a_World->BroadcastSpawnEntity(*this);
		return true;
	}
	return false;
}





void cMinecart::SpawnOn(cClientHandle & a_ClientHandle)
{
	char SubType = 0;
	switch (m_Payload)
	{
		case mpNone:    SubType = 0; break;
		case mpChest:   SubType = 1; break;
		case mpFurnace: SubType = 2; break;
		case mpTNT:     SubType = 3; break;
		case mpHopper:  SubType = 5; break;
		default:
		{
			ASSERT(!"Unknown payload, cannot spawn on client");
			return;
		}
	}
	a_ClientHandle.SendSpawnVehicle(*this, 10, SubType); // 10 = Minecarts, SubType = What type of Minecart
}





void cMinecart::Tick(float a_Dt, cChunk & a_Chunk)
{
	/*
	NOTE: Please bear in mind that taking away from negatives make them even more negative,
	adding to negatives make them positive, etc.
	*/
	
	super::Tick(a_Dt, a_Chunk);
	
	BLOCKTYPE BelowType;
	NIBBLETYPE BelowMeta;
	double SpeedX = GetSpeedX(), SpeedY = GetSpeedY(), SpeedZ = GetSpeedZ(); // Get current speed

	// Get block type & meta below the cart
	GetWorld()->GetBlockTypeMeta(floor(GetPosX()), floor(GetPosY() -1 ), floor(GetPosZ()), BelowType, BelowMeta);

	if ((BelowType == E_BLOCK_RAIL) || (BelowType == E_BLOCK_DETECTOR_RAIL) || (BelowType == E_BLOCK_ACTIVATOR_RAIL))
	{
		switch (BelowMeta)
		{

			case E_RAIL_ZM_ZP:
			{
				SpeedY = 0; // Don't move vertically as on ground
				
				// Set Y as current Y rounded up to bypass friction
				SetPosY(floor(GetPosY()));

				if (SpeedZ != 0) // Don't do anything if cart is stationary
				{
					if (SpeedZ > 0)
					{
						// Going SOUTH, slow down
						SpeedZ = SpeedZ - 0.1;
					}
					else
					{
						// Going NORTH, slow down
						SpeedZ = SpeedZ + 0.1;
					}
				}
				break;
			}

			case E_RAIL_XM_XP:
			{
				SpeedY = 0;
				SetPosY(floor(GetPosY()));

				if (SpeedX != 0)
				{
					if (SpeedX > 0)
					{
						SpeedX = SpeedX - 0.1;
					}
					else
					{
						SpeedX = SpeedX + 0.1;
					}
				}
				break;
			}

			case E_RAIL_ASCEND_ZM:
			{
				if (SpeedZ >= 0)
				{
					// SpeedZ POSITIVE, going SOUTH
					if (SpeedZ <= 8) // Speed limit of 8 SOUTH (m/s??)
					{
						SpeedZ = SpeedZ + 0.5; // Speed up
						SpeedY = (0 - SpeedZ); // Downward movement is negative (0 minus positive numbers is negative)
					}
					else
					{
						SpeedZ = 8; // Enforce speed limit
						SpeedY = (0 - SpeedZ);
					}
				}
				else
				{
					// SpeedZ NEGATIVE, going NORTH
					SpeedZ = SpeedZ + 0.6; // Slow down
					SpeedY = (0 - SpeedZ); // Upward movement is positive (0 minus negative number is positive number)
				}
				break;
			}

			case E_RAIL_ASCEND_ZP:
			{
				if (SpeedX > 0)
				{
					// SpeedZ POSITIVE, going SOUTH
					SpeedZ = SpeedZ - 0.6; // Slow down
					SpeedY = SpeedZ; // Upward movement positive
				}
				else
				{
					if (SpeedZ >= -8) // Speed limit of 8 WEST (m/s??)
					{
						// SpeedZ NEGATIVE, going NORTH
						SpeedZ = SpeedZ - 0.5; // Speed up
						SpeedY = SpeedZ; // Downward movement negative
					}
					else
					{
						SpeedZ = 8; // Enforce speed limit
						SpeedY = SpeedZ;
					}
				}
				break;
			}

			case E_RAIL_ASCEND_XM:
			{
				if (SpeedX >= 0)
				{
					if (SpeedX <= 8)
					{
						SpeedX = SpeedX + 0.5;
						SpeedY = (0 - SpeedX);
					}
					else
					{
						SpeedX = 8;
						SpeedY = (0 - SpeedX);
					}
				}
				else
				{
					SpeedX = SpeedX + 0.6;
					SpeedY = (0 - SpeedX);
				}
				break;
			}

			case E_RAIL_ASCEND_XP:
			{
				if (SpeedX > 0)
				{
					SpeedX = SpeedX - 0.6;
					SpeedY = SpeedX;
				}
				else
				{
					if (SpeedX >= -8)
					{
						SpeedX = SpeedX - 0.5;
						SpeedY = SpeedX;
					}
					else
					{
						SpeedX = -8;
						SpeedY = SpeedX;
					}
				}
				break;
			}

			default:
			{
				ASSERT(!"Unhandled rail meta!");
				break;
			}

		}
	}
	
	// Set speed to speed variables
	SetSpeedX(SpeedX);
	SetSpeedY(SpeedY);
	SetSpeedZ(SpeedZ);

	// Pass to physics handlers in Entity.cpp and broadcast position to client
	HandlePhysics(a_Dt, a_Chunk);
	BroadcastMovementUpdate();

}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cEmptyMinecart:

cEmptyMinecart::cEmptyMinecart(double a_X, double a_Y, double a_Z) :
	super(mpNone, a_X, a_Y, a_Z)
{
}





void cEmptyMinecart::OnRightClicked(cPlayer & a_Player)
{
	if (m_Attachee != NULL)
	{
		if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
		{
			// This player is already sitting in, they want out.
			a_Player.Detach();
			return;
		}
		
		if (m_Attachee->IsPlayer())
		{
			// Another player is already sitting in here, cannot attach
			return;
		}
		
		// Detach whatever is sitting in this minecart now:
		m_Attachee->Detach();
	}
	
	// Attach the player to this minecart
	a_Player.AttachTo(this);
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithChest:

cMinecartWithChest::cMinecartWithChest(double a_X, double a_Y, double a_Z) :
	super(mpChest, a_X, a_Y, a_Z)
{
}





void cMinecartWithChest::SetSlot(int a_Idx, const cItem & a_Item)
{
	ASSERT((a_Idx >= 0) && (a_Idx < ARRAYCOUNT(m_Items)));
	
	m_Items[a_Idx] = a_Item;
}





void cMinecartWithChest::OnRightClicked(cPlayer & a_Player)
{
	// Show the chest UI window to the player
	// TODO
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithFurnace:

cMinecartWithFurnace::cMinecartWithFurnace(double a_X, double a_Y, double a_Z) :
	super(mpFurnace, a_X, a_Y, a_Z)
{
}





void cMinecartWithFurnace::OnRightClicked(cPlayer & a_Player)
{
	// Try to power the furnace with whatever the player is holding
	// TODO
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithTNT:

cMinecartWithTNT::cMinecartWithTNT(double a_X, double a_Y, double a_Z) :
	super(mpTNT, a_X, a_Y, a_Z)
{
}

// TODO: Make it activate when passing over activator rail





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cMinecartWithHopper:

cMinecartWithHopper::cMinecartWithHopper(double a_X, double a_Y, double a_Z) :
	super(mpHopper, a_X, a_Y, a_Z)
{
}

// TODO: Make it suck up blocks and travel further than any other cart and physics and put and take blocks
// AND AVARYTHING!!