aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/PotionBrewing.cpp
blob: bc5fd8c714309fc18fcaa1803fad9962a16f465b (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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
#include "stdafx.h"
#include "..\Minecraft.Client\Minecraft.h"
#include "net.minecraft.world.effect.h"
#include "JavaMath.h"
#include "SharedConstants.h"
#include "PotionBrewing.h"

const int PotionBrewing::DEFAULT_APPEARANCES[] =
{
	IDS_POTION_PREFIX_MUNDANE,
	IDS_POTION_PREFIX_UNINTERESTING,
	IDS_POTION_PREFIX_BLAND,
	IDS_POTION_PREFIX_CLEAR,
	IDS_POTION_PREFIX_MILKY,
	IDS_POTION_PREFIX_DIFFUSE,
	IDS_POTION_PREFIX_ARTLESS,
	IDS_POTION_PREFIX_THIN,
	IDS_POTION_PREFIX_AWKWARD,
	IDS_POTION_PREFIX_FLAT,
	IDS_POTION_PREFIX_BULKY,
	IDS_POTION_PREFIX_BUNGLING,
	IDS_POTION_PREFIX_BUTTERED,
	IDS_POTION_PREFIX_SMOOTH,
	IDS_POTION_PREFIX_SUAVE,
	IDS_POTION_PREFIX_DEBONAIR,
	IDS_POTION_PREFIX_THICK,
	IDS_POTION_PREFIX_ELEGANT,
	IDS_POTION_PREFIX_FANCY,
	IDS_POTION_PREFIX_CHARMING,
	IDS_POTION_PREFIX_DASHING,
	IDS_POTION_PREFIX_REFINED,
	IDS_POTION_PREFIX_CORDIAL,
	IDS_POTION_PREFIX_SPARKLING,
	IDS_POTION_PREFIX_POTENT,
	IDS_POTION_PREFIX_FOUL,
	IDS_POTION_PREFIX_ODORLESS,
	IDS_POTION_PREFIX_RANK,
	IDS_POTION_PREFIX_HARSH,
	IDS_POTION_PREFIX_ACRID,
	IDS_POTION_PREFIX_GROSS,
	IDS_POTION_PREFIX_STINKY,
};

// bit 4 is the "enabler," lit by nether seeds

	// bits 0-3 are effect identifiers
	// 0001 - regeneration
	// 0010 - move speed
	// 0011 - fire resist
	// 0100 - poison
	// 0101 - heal
	// 0110 - night vision
	// 0111 - invisibility
	// 1000 - weakness
	// 1001 - damage boost
	// 1010 - move slow
	// 1011 -
	// 1100 - harm
	// 1101 -
	// 1110 -
	// 1111 -

/* 4J-JEV: Fix for #81196,
 * Bit 13 is always set in functional potions.
 * Therefore if bit 13 is on, don't use netherwart!
 * Added "&!13" which requires that bit 13 be turned off.
 */
const wstring PotionBrewing::MOD_NETHERWART = L"+4&!13"; // L"+4"

#if _SIMPLIFIED_BREWING
const wstring PotionBrewing::MOD_WATER = L"";
const wstring PotionBrewing::MOD_SUGAR = L"-0+1-2-3&4-4+13";
const wstring PotionBrewing::MOD_GHASTTEARS = L"+0-1-2-3&4-4+13";
const wstring PotionBrewing::MOD_SPIDEREYE = L"-0-1+2-3&4-4+13";
const wstring PotionBrewing::MOD_FERMENTEDEYE = L"-0+3-4+13";
const wstring PotionBrewing::MOD_SPECKLEDMELON = L"+0-1+2-3&4-4+13";
const wstring PotionBrewing::MOD_BLAZEPOWDER = L"+0-1-2+3&4-4+13";
const wstring PotionBrewing::MOD_GOLDENCARROT = L"-0+1+2-3+13&4-4";
const wstring PotionBrewing::MOD_MAGMACREAM = L"+0+1-2-3&4-4+13";
const wstring PotionBrewing::MOD_REDSTONE = L"-5+6-7"; // redstone increases duration
const wstring PotionBrewing::MOD_GLOWSTONE = L"+5-6-7"; // glowstone increases amplification
// 4J Stu - Don't require bit 13 to be set. We don't use it in the creative menu. Side effect is you can make a (virtually useless) Splash Mundane potion with water bottle and gunpowder
const wstring PotionBrewing::MOD_GUNPOWDER = L"+14";//&13-13"; // gunpowder makes them throwable! // gunpowder requires 13 and sets 14
#else
const wstring PotionBrewing::MOD_WATER = L"-1-3-5-7-9-11-13";
const wstring PotionBrewing::MOD_SUGAR = L"+0";
const wstring PotionBrewing::MOD_GHASTTEARS = L"+11";
const wstring PotionBrewing::MOD_SPIDEREYE = L"+10+7+5";
const wstring PotionBrewing::MOD_FERMENTEDEYE = L"+14+9";
const wstring PotionBrewing::MOD_SPECKLEDMELON = L"";
const wstring PotionBrewing::MOD_BLAZEPOWDER = L"+14";
const wstring PotionBrewing::MOD_MAGMACREAM = L"+14+6+1";
const wstring PotionBrewing::MOD_REDSTONE = L""; // redstone increases duration
const wstring PotionBrewing::MOD_GLOWSTONE = L""; // glowstone increases amplification
const wstring PotionBrewing::MOD_GUNPOWDER = L""; // gunpowder makes them throwable! // gunpowder requires 13 and sets 14
#endif

PotionBrewing::intStringMap PotionBrewing::potionEffectDuration;
PotionBrewing::intStringMap PotionBrewing::potionEffectAmplifier;

unordered_map<int, int> PotionBrewing::cachedColors;

void PotionBrewing::staticCtor()
{
#if _SIMPLIFIED_BREWING
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::regeneration->getId(), L"0 & !1 & !2 & !3 & 0+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::movementSpeed->getId(), L"!0 & 1 & !2 & !3 & 1+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::fireResistance->getId(), L"0 & 1 & !2 & !3 & 0+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::heal->getId(), L"0 & !1 & 2 & !3" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::poison->getId(), L"!0 & !1 & 2 & !3 & 2+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::weakness->getId(), L"!0 & !1 & !2 & 3 & 3+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::harm->getId(), L"!0 & !1 & 2 & 3" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::movementSlowdown->getId(), L"!0 & 1 & !2 & 3 & 3+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::damageBoost->getId(), L"0 & !1 & !2 & 3 & 3+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::nightVision->getId(), L"!0 & 1 & 2 & !3 & 2+6" ));
		potionEffectDuration.insert(intStringMap::value_type( MobEffect::invisibility->getId(), L"!0 & 1 & 2 & 3 & 2+6" ));

		// glowstone increases amplification
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::movementSpeed->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::digSpeed->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::damageBoost->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::regeneration->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::harm->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::heal->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::damageResistance->getId(), L"5" ));
		potionEffectAmplifier.insert(intStringMap::value_type( MobEffect::poison->getId(), L"5" ));
#else
		potionEffectDuration.put(movementSpeed.getId(), "!10 & !4 & 5*2+0 & >1 | !7 & !4 & 5*2+0 & >1");
		potionEffectDuration.put(movementSlowdown.getId(), "10 & 7 & !4 & 7+5+1-0");
		potionEffectDuration.put(digSpeed.getId(), "2 & 12+2+6-1-7 & <8");
		potionEffectDuration.put(digSlowdown.getId(), "!2 & !1*2-9 & 14-5");
		potionEffectDuration.put(damageBoost.getId(), "9 & 3 & 9+4+5 & <11");
		potionEffectDuration.put(weakness.getId(), "=1>5>7>9+3-7-2-11 & !9 & !0");
		potionEffectDuration.put(heal.getId(), "11 & <6");
		potionEffectDuration.put(harm.getId(), "!11 & 1 & 10 & !7");
		potionEffectDuration.put(jump.getId(), "8 & 2+0 & <5");
		potionEffectDuration.put(confusion.getId(), "8*2-!7+4-11 & !2 | 13 & 11 & 2*3-1-5");
		potionEffectDuration.put(regeneration.getId(), "!14 & 13*3-!0-!5-8");
		potionEffectDuration.put(damageResistance.getId(), "10 & 4 & 10+5+6 & <9");
		potionEffectDuration.put(fireResistance.getId(), "14 & !5 & 6-!1 & 14+13+12");
		potionEffectDuration.put(waterBreathing.getId(), "0+1+12 & !6 & 10 & !11 & !13");
		potionEffectDuration.put(invisibility.getId(), "2+5+13-0-4 & !7 & !1 & >5");
		potionEffectDuration.put(blindness.getId(), "9 & !1 & !5 & !3 & =3");
		potionEffectDuration.put(nightVision.getId(), "8*2-!7 & 5 & !0 & >3");
		potionEffectDuration.put(hunger.getId(), ">4>6>8-3-8+2");
		potionEffectDuration.put(poison.getId(), "12+9 & !13 & !0");

		potionEffectAmplifier.put(movementSpeed.getId(), "7+!3-!1");
		potionEffectAmplifier.put(digSpeed.getId(), "1+0-!11");
		potionEffectAmplifier.put(damageBoost.getId(), "2+7-!12");
		potionEffectAmplifier.put(heal.getId(), "11+!0-!1-!14");
		potionEffectAmplifier.put(harm.getId(), "!11-!14+!0-!1");
		potionEffectAmplifier.put(damageResistance.getId(), "12-!2");
		potionEffectAmplifier.put(poison.getId(), "14>5");
#endif
}

bool PotionBrewing::isWrappedLit(int brew, int position)
{
	return (brew & (1 << (position % NUM_BITS))) != 0;
}

bool PotionBrewing::isLit(int brew, int position)
{
	return (brew & (1 << position)) != 0;
}

int PotionBrewing::isBit(int brew, int position)
{
	return isLit(brew, position) ? 1 : 0;
}

int PotionBrewing::isNotBit(int brew, int position)
{
	return isLit(brew, position) ? 0 : 1;
}

int PotionBrewing::getAppearanceValue(int brew)
{
	return valueOf(brew, 5, 4, 3, 2, 1);
}

int PotionBrewing::getColorValue(vector<MobEffectInstance *> *effects)
{
	ColourTable *colourTable = Minecraft::GetInstance()->getColourTable();

	int baseColor = colourTable->getColor( eMinecraftColour_Potion_BaseColour );

	if (effects == nullptr || effects->empty())
	{
		return baseColor;
	}

	float red = 0;
	float green = 0;
	float blue = 0;
	float count = 0;

	//for (MobEffectInstance effect : effects){
	for(auto& effect : *effects)
	{
		int potionColor = colourTable->getColor( MobEffect::effects[effect->getId()]->getColor() );

		for (int potency = 0; potency <= effect->getAmplifier(); potency++)
		{
			red += static_cast<float>((potionColor >> 16) & 0xff) / 255.0f;
			green += static_cast<float>((potionColor >> 8) & 0xff) / 255.0f;
			blue += static_cast<float>((potionColor >> 0) & 0xff) / 255.0f;
			count++;
		}
	}

	red = (red / count) * 255.0f;
	green = (green / count) * 255.0f;
	blue = (blue / count) * 255.0f;

	return static_cast<int>(red) << 16 | static_cast<int>(green) << 8 | static_cast<int>(blue);
}

bool PotionBrewing::areAllEffectsAmbient(vector<MobEffectInstance *> *effects)
{
	for(auto& effect : *effects)
	{
		if (!effect->isAmbient()) return false;
	}

	return true;
}

int PotionBrewing::getColorValue(int brew, bool includeDisabledEffects)
{
	if (!includeDisabledEffects)
	{
        auto colIt = cachedColors.find(brew);
        if (colIt != cachedColors.end())
		{
			return colIt->second;//cachedColors.get(brew);
		}
		vector<MobEffectInstance *> *effects = getEffects(brew, false);
		int color = getColorValue(effects);
		if(effects != nullptr)
		{
			for(auto& effect : *effects)
			{
				delete effect;
			}
			delete effects;
		}
		cachedColors.insert( std::pair<int,int>(brew, color) );
		return color;
	}

	return getColorValue(getEffects(brew, includeDisabledEffects));
}

int PotionBrewing::getSmellValue(int brew)
{
	return valueOf(brew, 12, 11, 6, 4, 0);
}

int PotionBrewing::getAppearanceName(int brew)
{
	int value = getAppearanceValue(brew);
	return DEFAULT_APPEARANCES[value];
}

int PotionBrewing::constructParsedValue(bool isNot, bool hasMultiplier, bool isNeg, int countCompare, int valuePart, int multiplierPart, int brew)
{
	int value = 0;
	if (isNot)
	{
		value = isNotBit(brew, valuePart);
	}
#if !(_SIMPLIFIED_BREWING)
	else if (countCompare != NO_COUNT) // Never true for simplified brewing
	{
		if (countCompare == EQUAL_COUNT && countOnes(brew) == valuePart)
		{
			value = 1;
		}
		else if (countCompare == GREATER_COUNT && countOnes(brew) > valuePart)
		{
			value = 1;
		}
		else if (countCompare == LESS_COUNT && countOnes(brew) < valuePart)
		{
			value = 1;
		}
	}
#endif
	else
	{
		value = isBit(brew, valuePart);
	}
#if !(_SIMPLIFIED_BREWING)
	if (hasMultiplier) // Always false for simplified brewing
	{
		value *= multiplierPart;
	}
#endif
	if (isNeg)
	{
		value *= -1;
	}
	return value;
}

int PotionBrewing::countOnes(int brew)
{
	int c = 0;
	for (; brew > 0; c++)
	{
		brew &= brew - 1;
	}
	return c;
}

#if _SIMPLIFIED_BREWING
// 4J Stu - Trimmed this function to remove all the unused features for simplified brewing
int PotionBrewing::parseEffectFormulaValue(const wstring &definition, int start, int end, int brew)
{
	if (start >= definition.length() || end < 0 || start >= end)
	{
		return 0;
	}

	// split by and
	size_t andIndex = definition.find_first_of(L'&', start);
	if (andIndex != wstring::npos && andIndex < static_cast<size_t>(end))
	{
		int leftSide = parseEffectFormulaValue(definition, start, static_cast<int>(andIndex) - 1, brew);
		if (leftSide <= 0)
		{
			return 0;
		}

		int rightSide = parseEffectFormulaValue(definition, static_cast<int>(andIndex) + 1, end, brew);
		if (rightSide <= 0)
		{
			return 0;
		}

		if (leftSide > rightSide)
		{
			return leftSide;
		}
		return rightSide;
	}

	bool hasMultiplier = false;
	bool hasValue = false;
	bool isNot = false;
	bool isNeg = false;
	int bitCount = NO_COUNT;
	int valuePart = 0;
	int multiplierPart = 0;
	int result = 0;
	for (int i = start; i < end; i++)
	{

		char current = definition.at(i);
		if (current >= L'0' && current <= L'9')
		{
			valuePart *= 10;
			valuePart += (int) (current - L'0');
			hasValue = true;
		}
		else if (current == L'!')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = isNeg = isNot = false;
				valuePart = 0;
			}

			isNot = true;
		}
		else if (current == L'-')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = isNeg = isNot = false;
				valuePart = 0;
			}

			isNeg = true;
		}
		else if (current == L'+')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = isNeg = isNot = false;
				valuePart = 0;
			}
		}
	}
	if (hasValue)
	{
		result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
	}

	return result;
}
#else
int PotionBrewing::parseEffectFormulaValue(const wstring &definition, int start, int end, int brew)
{
	if (start >= definition.length() || end < 0 || start >= end)
	{
		return 0;
	}

	// split by or
	size_t orIndex = definition.find_first_of(L'|', start);
	if (orIndex != wstring::npos && orIndex < static_cast<size_t>(end))
	{
		int leftSide = parseEffectFormulaValue(definition, start, static_cast<int>(orIndex) - 1, brew);
		if (leftSide > 0)
		{
			return leftSide;
		}

		int rightSide = parseEffectFormulaValue(definition, static_cast<int>(orIndex) + 1, end, brew);
		if (rightSide > 0)
		{
			return rightSide;
		}
		return 0;
	}
	// split by and
	size_t andIndex = definition.find_first_of(L'&', start);
	if (andIndex != wstring::npos && andIndex < static_cast<size_t>(end))
	{
		int leftSide = parseEffectFormulaValue(definition, start, static_cast<int>(andIndex) - 1, brew);
		if (leftSide <= 0)
		{
			return 0;
		}

		int rightSide = parseEffectFormulaValue(definition, andIndex + 1, end, brew);
		if (rightSide <= 0)
		{
			return 0;
		}

		if (leftSide > rightSide)
		{
			return leftSide;
		}
		return rightSide;
	}

	bool isMultiplier = false;
	bool hasMultiplier = false;
	bool hasValue = false;
	bool isNot = false;
	bool isNeg = false;
	int bitCount = NO_COUNT;
	int valuePart = 0;
	int multiplierPart = 0;
	int result = 0;
	for (int i = start; i < end; i++)
	{

		char current = definition.at(i);
		if (current >= L'0' && current <= L'9')
		{
			if (isMultiplier)
			{
				multiplierPart = (int) (current - L'0');
				hasMultiplier = true;
			}
			else
			{
				valuePart *= 10;
				valuePart += (int) (current - L'0');
				hasValue = true;
			}
		}
		else if (current == L'*')
		{
			isMultiplier = true;
		}
		else if (current == L'!')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = hasMultiplier = isMultiplier = isNeg = isNot = false;
				valuePart = multiplierPart = 0;
				bitCount = NO_COUNT;
			}

			isNot = true;
		}
		else if (current == L'-')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = hasMultiplier = isMultiplier = isNeg = isNot = false;
				valuePart = multiplierPart = 0;
				bitCount = NO_COUNT;
			}

			isNeg = true;
		}
		else if (current == L'=' || current == L'<' || current == L'>')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = hasMultiplier = isMultiplier = isNeg = isNot = false;
				valuePart = multiplierPart = 0;
				bitCount = NO_COUNT;
			}

			if (current == L'=')
			{
				bitCount = EQUAL_COUNT;
			}
			else if (current == L'<')
			{
				bitCount = LESS_COUNT;
			}
			else if (current == L'>')
			{
				bitCount = GREATER_COUNT;
			}
		}
		else if (current == L'+')
		{
			if (hasValue)
			{
				result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
				hasValue = hasMultiplier = isMultiplier = isNeg = isNot = false;
				valuePart = multiplierPart = 0;
				bitCount = NO_COUNT;
			}
		}
	}
	if (hasValue)
	{
		result += constructParsedValue(isNot, hasMultiplier, isNeg, bitCount, valuePart, multiplierPart, brew);
	}

	return result;
}
#endif

vector<MobEffectInstance *> *PotionBrewing::getEffects(int brew, bool includeDisabledEffects)
{
	vector<MobEffectInstance *> *list = nullptr;

	//for (MobEffect effect : MobEffect.effects)
	for(unsigned int i = 0; i < MobEffect::NUM_EFFECTS; ++i)
	{
		MobEffect *effect = MobEffect::effects[i];
		if (effect == nullptr || (effect->isDisabled() && !includeDisabledEffects))
		{
			continue;
		}
		//wstring durationString = potionEffectDuration.get(effect->getId());
        auto effIt = potionEffectDuration.find(effect->getId());
        if ( effIt == potionEffectDuration.end() )
		{
			continue;
		}
		wstring durationString = effIt->second;

		int duration = parseEffectFormulaValue(durationString, 0, static_cast<int>(durationString.length()), brew);
		if (duration > 0)
		{
			int amplifier = 0;
            auto ampIt = potionEffectAmplifier.find(effect->getId());
            if (ampIt != potionEffectAmplifier.end())
			{
				wstring amplifierString = ampIt->second;
				amplifier = parseEffectFormulaValue(amplifierString, 0, static_cast<int>(amplifierString.length()), brew);
				if (amplifier < 0)
				{
					amplifier = 0;
				}
			}

			if (effect->isInstantenous())
			{
				duration = 1;
			}
			else
			{
				// 3, 8, 13, 18.. minutes
				duration = (SharedConstants::TICKS_PER_SECOND * 60) * (duration * 3 + (duration - 1) * 2);
				duration >>= amplifier;
				duration = static_cast<int>(Math::round((double)duration * effect->getDurationModifier()));

				if ((brew & THROWABLE_MASK) != 0)
				{
					duration = static_cast<int>(Math::round((double)duration * .75 + .5));
				}
			}

			if (list == nullptr)
			{
				list = new vector<MobEffectInstance *>();
			}
			MobEffectInstance *instance = new MobEffectInstance(effect->getId(), duration, amplifier);
			if ((brew & THROWABLE_MASK) != 0) instance->setSplash(true);
			list->push_back(instance);
		}
	}

	return list;
}

#if !(_SIMPLIFIED_BREWING)
int PotionBrewing::boil(int brew)
{
	if ((brew & 1) == 0)
	{
		return brew;
	}

	// save highest bit
	int savedBit = NUM_BITS - 1;
	while ((brew & (1 << savedBit)) == 0 && savedBit >= 0)
	{
		savedBit--;
	}
	// it's not possible to boil if there are no "empty slots" in front of
	// the last bit
	if (savedBit < 2 || (brew & (1 << (savedBit - 1))) != 0)
	{
		return brew;
	}
	if (savedBit >= 0)
	{
		brew &= ~(1 << savedBit);
	}

	brew <<= 1;

	if (savedBit >= 0)
	{
		brew |= (1 << savedBit);
		brew |= (1 << (savedBit - 1));
	}

	return brew & BREW_MASK;
}

int PotionBrewing::shake(int brew)
{

	// save highest bit
	int savedBit = NUM_BITS - 1;
	while ((brew & (1 << savedBit)) == 0 && savedBit >= 0)
	{
		savedBit--;
	}
	if (savedBit >= 0)
	{
		brew &= ~(1 << savedBit);
	}

	int currentResult = 0;
	int nextResult = brew;

	while (nextResult != currentResult)
	{
		nextResult = brew;
		currentResult = 0;
		// evaluate each bit
		for (int bit = 0; bit < NUM_BITS; bit++)
		{

			bool on = isWrappedLit(brew, bit);
			if (on)
			{
				if (!isWrappedLit(brew, bit + 1) && isWrappedLit(brew, bit + 2))
				{
					on = false;
				}
				else if (!isWrappedLit(brew, bit - 1) && isWrappedLit(brew, bit - 2))
				{
					on = false;
				}
			}
			else
			{
				// turn on if both neighbors are on
				on = isWrappedLit(brew, bit - 1) && isWrappedLit(brew, bit + 1);
			}
			if (on)
			{
				currentResult |= (1 << bit);
			}
		}
		brew = currentResult;
	}

	if (savedBit >= 0)
	{
		currentResult |= (1 << savedBit);
	}

	return currentResult & BREW_MASK;
}

int PotionBrewing::stirr(int brew)
{
	if ((brew & 1) != 0)
	{
		brew = boil(brew);
	}
	return shake(brew);
}
#endif

int PotionBrewing::applyBrewBit(int currentBrew, int bit, bool isNeg, bool isNot, bool isRequired)
{
	if (isRequired)
	{
		// 4J-JEV: I wanted to be able to specify that a
		// bit is required to be false.
		if (isLit(currentBrew, bit) == isNot)
		{
			return 0;
		}
	}
	else if (isNeg)
	{
		currentBrew &= ~(1 << bit);
	}
	else if (isNot)
	{
		if ((currentBrew & (1 << bit)) == 0)
		{
			currentBrew |= (1 << bit);
		}
		else
		{
			currentBrew &= ~(1 << bit);
		}
	}
	else
	{
		currentBrew |= (1 << bit);
	}
	return currentBrew;
}

int PotionBrewing::applyBrew(int currentBrew, const wstring &formula)
{

	int start = 0;
	int end = static_cast<int>(formula.length());

	bool hasValue = false;
	bool isNot = false;
	bool isNeg = false;
	bool isRequired = false;
	int valuePart = 0;
	for (int i = start; i < end; i++)
	{
		char current = formula.at(i);
		if (current >= L'0' && current <= L'9')
		{
			valuePart *= 10;
			valuePart += (int) (current - L'0');
			hasValue = true;
		}
		else if (current == L'!')
		{
			if (hasValue)
			{
				currentBrew = applyBrewBit(currentBrew, valuePart, isNeg, isNot, isRequired);
				hasValue = isNeg = isNot = isRequired = false;
				valuePart = 0;
			}

			isNot = true;
		}
		else if (current == L'-')
		{
			if (hasValue)
			{
				currentBrew = applyBrewBit(currentBrew, valuePart, isNeg, isNot, isRequired);
				hasValue = isNeg = isNot = isRequired = false;
				valuePart = 0;
			}

			isNeg = true;
		}
		else if (current == L'+')
		{
			if (hasValue)
			{
				currentBrew = applyBrewBit(currentBrew, valuePart, isNeg, isNot, isRequired);
				hasValue = isNeg = isNot = isRequired = false;
				valuePart = 0;
			}
		}
		else if (current == L'&')
		{
			if (hasValue)
			{
				currentBrew = applyBrewBit(currentBrew, valuePart, isNeg, isNot, isRequired);
				hasValue = isNeg = isNot = isRequired = false;
				valuePart = 0;
			}
			isRequired = true;
		}
	}
	if (hasValue)
	{
		currentBrew = applyBrewBit(currentBrew, valuePart, isNeg, isNot, isRequired);
	}

	return currentBrew & BREW_MASK;
}

int PotionBrewing::setBit(int brew, int position, bool onOff)
{
	if (onOff)
	{
		return brew | (1 << position);
	}
	return brew & ~(1 << position);
}

int PotionBrewing::valueOf(int brew, int p1, int p2, int p3, int p4)
{
	return ((isLit(brew, p1) ? 0x08 : 0) | (isLit(brew, p2) ? 0x04 : 0) | (isLit(brew, p3) ? 0x02 : 0) | (isLit(brew, p4) ? 0x01 : 0));
}

int PotionBrewing::valueOf(int brew, int p1, int p2, int p3, int p4, int p5)
{
	return (isLit(brew, p1) ? 0x10 : 0) | (isLit(brew, p2) ? 0x08 : 0) | (isLit(brew, p3) ? 0x04 : 0) | (isLit(brew, p4) ? 0x02 : 0) | (isLit(brew, p5) ? 0x01 : 0);
}

wstring PotionBrewing::toString(int brew)
{
	wstring string;

	int bit = NUM_BITS - 1;
	while (bit >= 0)
	{
		if ((brew & (1 << bit)) != 0)
		{
			string.append(L"O");
		}
		else
		{
			string.append(L"x");
		}
		bit--;
	}

	return string;
}

//void main(String[] args)
//{

//	HashMap<String, Integer> existingCombinations = new HashMap<String, Integer>();
//	HashMap<String, Integer> distinctCombinations = new HashMap<String, Integer>();
//	int noEffects = 0;
//	for (int brew = 0; brew <= BREW_MASK; brew++) {
//		List<MobEffectInstance> effects = PotionBrewing.getEffects(brew, true);
//		if (effects != null) {

//			{
//				StringBuilder builder = new StringBuilder();
//				for (MobEffectInstance effect : effects) {
//					builder.append(effect.toString());
//					builder.append(" ");
//				}
//				String string = builder.toString();
//				Integer count = existingCombinations.get(string);
//				if (count != null) {
//					count++;
//				} else {
//					count = 1;
//				}
//				existingCombinations.put(string, count);
//			}
//			{
//				StringBuilder builder = new StringBuilder();
//				for (MobEffectInstance effect : effects) {
//					builder.append(effect.getDescriptionId());
//					builder.append(" ");
//				}
//				String string = builder.toString();
//				Integer count = distinctCombinations.get(string);
//				if (count != null) {
//					count++;
//				} else {
//					count = 1;
//				}
//				distinctCombinations.put(string, count);
//			}
//		} else {
//			noEffects++;
//		}
//	}

//	for (String combination : existingCombinations.keySet()) {
//		Integer count = existingCombinations.get(combination);
//		if (count > 20) {
//			System.out.println(combination + ": " + count);
//		}
//	}

//	System.out.println("Combination with no effects: " + noEffects + " (" + ((double) noEffects / BREW_MASK * 100.0) + " %)");
//	System.out.println("Unique combinations: " + existingCombinations.size());
//	System.out.println("Distinct combinations: " + distinctCombinations.size());
//}