summaryrefslogtreecommitdiffstats
path: root/src/Defines.cpp
blob: 02b9f28d538b8b7e79bf2b20346e713521418867 (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include "Globals.h"

#include "Defines.h"
#include "BlockType.h"





const char * ClickActionToString(int a_ClickAction)
{
	switch (a_ClickAction)
	{
		case caLeftClick:                    return "caLeftClick";
		case caRightClick:                   return "caRightClick";
		case caShiftLeftClick:               return "caShiftLeftClick";
		case caShiftRightClick:              return "caShiftRightClick";
		case caNumber1:                      return "caNumber1";
		case caNumber2:                      return "caNumber2";
		case caNumber3:                      return "caNumber3";
		case caNumber4:                      return "caNumber4";
		case caNumber5:                      return "caNumber5";
		case caNumber6:                      return "caNumber6";
		case caNumber7:                      return "caNumber7";
		case caNumber8:                      return "caNumber8";
		case caNumber9:                      return "caNumber9";
		case caMiddleClick:                  return "caMiddleClick";
		case caDropKey:                      return "caDropKey";
		case caCtrlDropKey:                  return "caCtrlDropKey";
		case caLeftClickOutside:             return "caLeftClickOutside";
		case caRightClickOutside:            return "caRightClickOutside";
		case caLeftClickOutsideHoldNothing:  return "caLeftClickOutsideHoldNothing";
		case caRightClickOutsideHoldNothing: return "caRightClickOutsideHoldNothing";
		case caLeftPaintBegin:               return "caLeftPaintBegin";
		case caRightPaintBegin:              return "caRightPaintBegin";
		case caMiddlePaintBegin:             return "caMiddlePaintBegin";
		case caLeftPaintProgress:            return "caLeftPaintProgress";
		case caRightPaintProgress:           return "caRightPaintProgress";
		case caMiddlePaintProgress:          return "caMiddlePaintProgress";
		case caLeftPaintEnd:                 return "caLeftPaintEnd";
		case caRightPaintEnd:                return "caRightPaintEnd";
		case caMiddlePaintEnd:               return "caMiddlePaintEnd";
		case caDblClick:                     return "caDblClick";
		case caUnknown:                      return "caUnknown";
	}
	UNREACHABLE("Unknown click action");
}





/** Returns a blockface mirrored around the Y axis (doesn't change up / down). */
eBlockFace MirrorBlockFaceY(eBlockFace a_BlockFace)
{
	switch (a_BlockFace)
	{
		case BLOCK_FACE_XM: return BLOCK_FACE_XP;
		case BLOCK_FACE_XP: return BLOCK_FACE_XM;
		case BLOCK_FACE_ZM: return BLOCK_FACE_ZP;
		case BLOCK_FACE_ZP: return BLOCK_FACE_ZM;
		case BLOCK_FACE_NONE:
		case BLOCK_FACE_YM:
		case BLOCK_FACE_YP:
		{
			return a_BlockFace;
		}
	}
	UNREACHABLE("Unsupported block face");
}





/** Returns a blockface rotated around the Y axis counter-clockwise. */
eBlockFace RotateBlockFaceCCW(eBlockFace a_BlockFace)
{
	switch (a_BlockFace)
	{
		case BLOCK_FACE_XM: return BLOCK_FACE_ZP;
		case BLOCK_FACE_XP: return BLOCK_FACE_ZM;
		case BLOCK_FACE_ZM: return BLOCK_FACE_XM;
		case BLOCK_FACE_ZP: return BLOCK_FACE_XP;
		case BLOCK_FACE_NONE:
		case BLOCK_FACE_YM:
		case BLOCK_FACE_YP:
		{
			return a_BlockFace;
		}
	}
	UNREACHABLE("Unsupported block face");
}





eBlockFace RotateBlockFaceCW(eBlockFace a_BlockFace)
{
	switch (a_BlockFace)
	{
		case BLOCK_FACE_XM: return BLOCK_FACE_ZM;
		case BLOCK_FACE_XP: return BLOCK_FACE_ZP;
		case BLOCK_FACE_ZM: return BLOCK_FACE_XP;
		case BLOCK_FACE_ZP: return BLOCK_FACE_XM;
		case BLOCK_FACE_NONE:
		case BLOCK_FACE_YM:
		case BLOCK_FACE_YP:
		{
			return a_BlockFace;
		}
	}
	UNREACHABLE("Unsupported block face");
}





eBlockFace ReverseBlockFace(eBlockFace  a_BlockFace)
{
	switch (a_BlockFace)
	{
		case BLOCK_FACE_YP:   return BLOCK_FACE_YM;
		case BLOCK_FACE_XP:   return BLOCK_FACE_XM;
		case BLOCK_FACE_ZP:   return BLOCK_FACE_ZM;
		case BLOCK_FACE_YM:   return BLOCK_FACE_YP;
		case BLOCK_FACE_XM:   return BLOCK_FACE_XP;
		case BLOCK_FACE_ZM:   return BLOCK_FACE_ZP;
		case BLOCK_FACE_NONE: return a_BlockFace;
	}
	UNREACHABLE("Unsupported block face");
}





/** Returns the textual representation of the BlockFace constant. */
AString BlockFaceToString(eBlockFace a_BlockFace)
{
	switch (a_BlockFace)
	{
		case BLOCK_FACE_XM: return "BLOCK_FACE_XM";
		case BLOCK_FACE_XP: return "BLOCK_FACE_XP";
		case BLOCK_FACE_YM: return "BLOCK_FACE_YM";
		case BLOCK_FACE_YP: return "BLOCK_FACE_YP";
		case BLOCK_FACE_ZM: return "BLOCK_FACE_ZM";
		case BLOCK_FACE_ZP: return "BLOCK_FACE_ZP";
		case BLOCK_FACE_NONE: return "BLOCK_FACE_NONE";
	}
	UNREACHABLE("Unsupported block face");
}





bool IsValidBlock(int a_BlockType)
{
	return (
		((a_BlockType > -1) && (a_BlockType <= E_BLOCK_MAX_TYPE_ID)) ||
		(a_BlockType == 255)  // the blocks 253-254 don't exist yet -> https://minecraft.gamepedia.com/Data_values#Block_IDs
	);
}





bool IsValidItem(int a_ItemType)
{
	if (
		((a_ItemType >= E_ITEM_FIRST) && (a_ItemType <= E_ITEM_MAX_CONSECUTIVE_TYPE_ID)) ||  // Basic items range
		((a_ItemType >= E_ITEM_FIRST_DISC) && (a_ItemType <= E_ITEM_LAST_DISC))   // Music discs' special range
	)
	{
		return true;
	}

	if (a_ItemType == 0)
	{
		return false;
	}

	return IsValidBlock(a_ItemType);
}





eDimension StringToDimension(const AString & a_DimensionString)
{
	// First try decoding as a number
	int res;
	if (StringToInteger(a_DimensionString, res))
	{
		// It was a valid number
		return static_cast<eDimension>(res);
	}

	// Decode using a built-in map:
	static struct
	{
		eDimension m_Dimension;
		const char * m_String;
	} DimensionMap [] =
	{
		{ dimOverworld, "Overworld"},
		{ dimOverworld, "Normal"},
		{ dimOverworld, "World"},
		{ dimNether,    "Nether"},
		{ dimNether,    "Hell"},  // Alternate name for Nether
		{ dimEnd,       "End"},
		{ dimEnd,       "Sky"},  // Old name for End
	} ;
	for (size_t i = 0; i < ARRAYCOUNT(DimensionMap); i++)
	{
		if (NoCaseCompare(DimensionMap[i].m_String, a_DimensionString) == 0)
		{
			return DimensionMap[i].m_Dimension;
		}
	}  // for i - DimensionMap[]

	// Not found
	LOGWARNING("Unknown dimension: \"%s\". Setting to Overworld", a_DimensionString.c_str());
	return dimOverworld;
}





AString DimensionToString(eDimension a_Dimension)
{
	// Decode using a built-in map:
	static struct
	{
		eDimension m_Dimension;
		const char * m_String;
	} DimensionMap[] =
	{
		{ dimOverworld, "Overworld" },
		{ dimNether, "Nether" },
		{ dimEnd, "End" },
	};

	for (size_t i = 0; i < ARRAYCOUNT(DimensionMap); i++)
	{
		if (DimensionMap[i].m_Dimension == a_Dimension)
		{
			return DimensionMap[i].m_String;
		}
	}  // for i - DimensionMap[]

	// Not found
	LOGWARNING("Unknown dimension: \"%i\". Setting to Overworld", static_cast<int>(a_Dimension));
	return "Overworld";
}





AString DamageTypeToString(eDamageType a_DamageType)
{
	// Make sure to keep this alpha-sorted.
	switch (a_DamageType)
	{
		case dtAdmin:           return "dtAdmin";
		case dtAttack:          return "dtAttack";
		case dtCactusContact:   return "dtCactusContact";
		case dtMagmaContact:    return "dtMagmaContact";
		case dtDrowning:        return "dtDrowning";
		case dtEnderPearl:      return "dtEnderPearl";
		case dtEnvironment:     return "dtEnvironment";
		case dtFalling:         return "dtFalling";
		case dtFireContact:     return "dtFireContact";
		case dtInVoid:          return "dtInVoid";
		case dtLavaContact:     return "dtLavaContact";
		case dtLightning:       return "dtLightning";
		case dtOnFire:          return "dtOnFire";
		case dtPoisoning:       return "dtPoisoning";
		case dtWithering:       return "dtWithering";
		case dtPotionOfHarming: return "dtPotionOfHarming";
		case dtRangedAttack:    return "dtRangedAttack";
		case dtStarving:        return "dtStarving";
		case dtSuffocating:     return "dtSuffocation";
		case dtExplosion:       return "dtExplosion";
	}
	UNREACHABLE("Unsupported damage type");
}





eDamageType StringToDamageType(const AString & a_DamageTypeString)
{
	// First try decoding as a number:
	int res;
	if (!StringToInteger(a_DamageTypeString, res))
	{
		// It was a valid number
		return static_cast<eDamageType>(res);
	}

	// Decode using a built-in map:
	static struct
	{
		eDamageType  m_DamageType;
		const char * m_String;
	} DamageTypeMap [] =
	{
		// Cannonical names:
		{ dtAttack,          "dtAttack"},
		{ dtRangedAttack,    "dtRangedAttack"},
		{ dtLightning,       "dtLightning"},
		{ dtFalling,         "dtFalling"},
		{ dtDrowning,        "dtDrowning"},
		{ dtSuffocating,     "dtSuffocation"},
		{ dtStarving,        "dtStarving"},
		{ dtCactusContact,   "dtCactusContact"},
		{ dtMagmaContact,    "dtMagmaContact"},
		{ dtLavaContact,     "dtLavaContact"},
		{ dtPoisoning,       "dtPoisoning"},
		{ dtWithering,       "dtWithering"},
		{ dtOnFire,          "dtOnFire"},
		{ dtFireContact,     "dtFireContact"},
		{ dtInVoid,          "dtInVoid"},
		{ dtPotionOfHarming, "dtPotionOfHarming"},
		{ dtAdmin,           "dtAdmin"},
		{ dtExplosion,       "dtExplosion"},
		{ dtEnvironment,     "dtEnvironment"},

		// Common synonyms:
		{ dtAttack,        "dtPawnAttack"},
		{ dtAttack,        "dtEntityAttack"},
		{ dtAttack,        "dtMob"},
		{ dtAttack,        "dtMobAttack"},
		{ dtRangedAttack,  "dtArrowAttack"},
		{ dtRangedAttack,  "dtArrow"},
		{ dtRangedAttack,  "dtProjectile"},
		{ dtFalling,       "dtFall"},
		{ dtDrowning,      "dtDrown"},
		{ dtSuffocating,   "dtSuffocation"},
		{ dtStarving,      "dtStarvation"},
		{ dtStarving,      "dtHunger"},
		{ dtCactusContact, "dtCactus"},
		{ dtCactusContact, "dtCactuses"},
		{ dtCactusContact, "dtCacti"},
		{ dtMagmaContact,  "dtMagma"},
		{ dtLavaContact,   "dtLava"},
		{ dtPoisoning,     "dtPoison"},
		{ dtWithering,     "dtWither"},
		{ dtOnFire,        "dtBurning"},
		{ dtFireContact,   "dtInFire"},
		{ dtAdmin,         "dtPlugin"},
	} ;
	for (size_t i = 0; i < ARRAYCOUNT(DamageTypeMap); i++)
	{
		if (NoCaseCompare(DamageTypeMap[i].m_String, a_DamageTypeString) == 0)
		{
			return DamageTypeMap[i].m_DamageType;
		}
	}  // for i - DamageTypeMap[]

	// Not found:
	return static_cast<eDamageType>(-1);
}





void AddFaceDirection(int & a_BlockX, int & a_BlockY, int & a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse)
{
	if (!a_bInverse)
	{
		switch (a_BlockFace)
		{
			case BLOCK_FACE_YP: a_BlockY++; break;
			case BLOCK_FACE_YM: a_BlockY--; break;
			case BLOCK_FACE_ZM: a_BlockZ--; break;
			case BLOCK_FACE_ZP: a_BlockZ++; break;
			case BLOCK_FACE_XP: a_BlockX++; break;
			case BLOCK_FACE_XM: a_BlockX--; break;
			case BLOCK_FACE_NONE:
			{
				LOGWARNING("%s: Unknown face: %d", __FUNCTION__, a_BlockFace);
				ASSERT(!"AddFaceDirection(): Unknown face");
				break;
			}
		}
	}
	else
	{
		switch (a_BlockFace)
		{
			case BLOCK_FACE_YP: a_BlockY--; break;
			case BLOCK_FACE_YM: a_BlockY++; break;
			case BLOCK_FACE_ZM: a_BlockZ++; break;
			case BLOCK_FACE_ZP: a_BlockZ--; break;
			case BLOCK_FACE_XP: a_BlockX--; break;
			case BLOCK_FACE_XM: a_BlockX++; break;
			case BLOCK_FACE_NONE:
			{
				LOGWARNING("%s: Unknown inv face: %d", __FUNCTION__, a_BlockFace);
				ASSERT(!"AddFaceDirection(): Unknown face");
				break;
			}
		}
	}
}





bool ItemCategory::IsPickaxe(short a_ItemType)
{
	switch (a_ItemType)
	{
		case E_ITEM_WOODEN_PICKAXE:
		case E_ITEM_STONE_PICKAXE:
		case E_ITEM_IRON_PICKAXE:
		case E_ITEM_GOLD_PICKAXE:
		case E_ITEM_DIAMOND_PICKAXE:
		{
			return true;
		}
		default:
		{
			return false;
		}
	}
}





bool ItemCategory::IsAxe(short a_ItemType)
{
	switch (a_ItemType)
	{
		case E_ITEM_WOODEN_AXE:
		case E_ITEM_STONE_AXE:
		case E_ITEM_IRON_AXE:
		case E_ITEM_GOLD_AXE:
		case E_ITEM_DIAMOND_AXE:
		{
			return true;
		}
		default:
		{
			return false;
		}
	}
}





bool ItemCategory::IsSword(short a_ItemID)
{
	return (a_ItemID == E_ITEM_WOODEN_SWORD)
		|| (a_ItemID == E_ITEM_STONE_SWORD)
		|| (a_ItemID == E_ITEM_IRON_SWORD)
		|| (a_ItemID == E_ITEM_GOLD_SWORD)
		|| (a_ItemID == E_ITEM_DIAMOND_SWORD);
}





bool ItemCategory::IsHoe(short a_ItemID)
{
	return (a_ItemID == E_ITEM_WOODEN_HOE)
		|| (a_ItemID == E_ITEM_STONE_HOE)
		|| (a_ItemID == E_ITEM_IRON_HOE)
		|| (a_ItemID == E_ITEM_GOLD_HOE)
		|| (a_ItemID == E_ITEM_DIAMOND_HOE);
}





bool ItemCategory::IsShovel(short a_ItemID)
{
	return (a_ItemID == E_ITEM_WOODEN_SHOVEL)
		|| (a_ItemID == E_ITEM_STONE_SHOVEL)
		|| (a_ItemID == E_ITEM_IRON_SHOVEL)
		|| (a_ItemID == E_ITEM_GOLD_SHOVEL)
		|| (a_ItemID == E_ITEM_DIAMOND_SHOVEL);
}





bool ItemCategory::IsTool(short a_ItemID)
{
	return IsPickaxe( a_ItemID)
		|| IsAxe    ( a_ItemID)
		|| IsSword  ( a_ItemID)
		|| IsHoe    ( a_ItemID)
		|| IsShovel ( a_ItemID);
}





bool ItemCategory::IsHelmet(short a_ItemType)
{
	return (
		(a_ItemType == E_ITEM_LEATHER_CAP) ||
		(a_ItemType == E_ITEM_GOLD_HELMET) ||
		(a_ItemType == E_ITEM_CHAIN_HELMET) ||
		(a_ItemType == E_ITEM_IRON_HELMET) ||
		(a_ItemType == E_ITEM_DIAMOND_HELMET)
	);
}





bool ItemCategory::IsChestPlate(short a_ItemType)
{
	return (
		(a_ItemType == E_ITEM_ELYTRA) ||
		(a_ItemType == E_ITEM_LEATHER_TUNIC) ||
		(a_ItemType == E_ITEM_GOLD_CHESTPLATE) ||
		(a_ItemType == E_ITEM_CHAIN_CHESTPLATE) ||
		(a_ItemType == E_ITEM_IRON_CHESTPLATE) ||
		(a_ItemType == E_ITEM_DIAMOND_CHESTPLATE)
	);
}





bool ItemCategory::IsLeggings(short a_ItemType)
{
	return (
		(a_ItemType == E_ITEM_LEATHER_PANTS) ||
		(a_ItemType == E_ITEM_GOLD_LEGGINGS) ||
		(a_ItemType == E_ITEM_CHAIN_LEGGINGS) ||
		(a_ItemType == E_ITEM_IRON_LEGGINGS) ||
		(a_ItemType == E_ITEM_DIAMOND_LEGGINGS)
	);
}





bool ItemCategory::IsBoots(short a_ItemType)
{
	return (
		(a_ItemType == E_ITEM_LEATHER_BOOTS) ||
		(a_ItemType == E_ITEM_GOLD_BOOTS) ||
		(a_ItemType == E_ITEM_CHAIN_BOOTS) ||
		(a_ItemType == E_ITEM_IRON_BOOTS) ||
		(a_ItemType == E_ITEM_DIAMOND_BOOTS)
	);
}





bool ItemCategory::IsMinecart(short a_ItemType)
{
	return (
		(a_ItemType == E_ITEM_MINECART) ||
		(a_ItemType == E_ITEM_CHEST_MINECART) ||
		(a_ItemType == E_ITEM_FURNACE_MINECART) ||
		(a_ItemType == E_ITEM_MINECART_WITH_TNT) ||
		(a_ItemType == E_ITEM_MINECART_WITH_HOPPER)
	);
}





bool ItemCategory::IsArmor(short a_ItemType)
{
	return (
		IsHelmet(a_ItemType) ||
		IsChestPlate(a_ItemType) ||
		IsLeggings(a_ItemType) ||
		IsBoots(a_ItemType)
	);
}





bool ItemCategory::IsHorseArmor(short a_ItemType)
{
	switch (a_ItemType)
	{
		case E_ITEM_IRON_HORSE_ARMOR:
		case E_ITEM_GOLD_HORSE_ARMOR:
		case E_ITEM_DIAMOND_HORSE_ARMOR:
		{
			return true;
		}
		default:
		{
			return false;
		}
	}
}