Skip to content

Commit 4dab9ef

Browse files
committed
Lib : Just a whole bunch of change
Stop be lazy and procrastinating. Go do your job!
1 parent 9672565 commit 4dab9ef

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

PearlCalculatorLib/General/Calculation.cs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using PearlCalculatorLib.PearlCalculationLib.MathLib;
2-
using PearlCalculatorLib.Result;
1+
using PearlCalculatorLib.Result;
32
using PearlCalculatorLib.PearlCalculationLib;
43
using PearlCalculatorLib.PearlCalculationLib.World;
54
using System;
65
using System.Collections.Generic;
76
using System.Collections;
87
using PearlCalculatorLib.PearlCalculationLib.Entity;
9-
using System.Security.Cryptography;
10-
using System.Transactions;
118

129
namespace PearlCalculatorLib.General
1310
{
@@ -41,10 +38,12 @@ private static PearlEntity PearlSimulation(int redTNT , int blueTNT , int ticks
4138
/// <exception cref="ArgumentException"></exception>
4239
public static bool CalculateTNTAmount(int maxTicks , double maxDistance)
4340
{
44-
int redTNT , blueTNT;
45-
double divider = 0;
41+
int redTNT, blueTNT;
42+
double trueRed, trueBlue;
4643
Direction direction;
47-
Space3D distance , trueDistance , truePosition;
44+
Space3D trueDistance, truePosition;
45+
46+
double divider = 0;
4847

4948
truePosition = Data.Pearl.Position + Data.PearlOffset.ToSpace3D();
5049
trueDistance = Data.Destination - truePosition;
@@ -56,17 +55,18 @@ public static bool CalculateTNTAmount(int maxTicks , double maxDistance)
5655
Data.TNTResult.Clear();
5756
direction = DirectionUtils.GetDirection(truePosition.WorldAngle(Data.Destination));
5857
CalculateTNTVector(direction , out Space3D redTNTVector , out Space3D blueTNTVector);
59-
58+
//Calculate redTNT and blueTNT through simultaneous equations
59+
trueRed = (trueDistance.Z * blueTNTVector.X - trueDistance.X * blueTNTVector.Z) / (redTNTVector.Z * blueTNTVector.X - blueTNTVector.Z * redTNTVector.X);
60+
trueBlue = (trueDistance.X - trueRed * redTNTVector.X) / blueTNTVector.X;
61+
6062
for(int tick = 1; tick <= maxTicks; tick++)
6163
{
62-
//Factorization trueDistance to get a easier calculation
64+
//Factorization trueDistance to make a easier calculation
6365
divider += Math.Pow(0.99 , tick - 1);
64-
distance = trueDistance / divider;
6566

66-
//Calculate redTNT and blueTNT through simultaneous equations
67-
redTNT = Convert.ToInt32(Math.Round((distance.Z * blueTNTVector.X - distance.X * blueTNTVector.Z) / (redTNTVector.Z * blueTNTVector.X - blueTNTVector.Z * redTNTVector.X)));
68-
blueTNT = Convert.ToInt32(Math.Round((distance.X - redTNT * redTNTVector.X) / blueTNTVector.X));
69-
67+
redTNT = Convert.ToInt32(trueRed / divider);
68+
blueTNT = Convert.ToInt32(trueBlue / divider);
69+
7070
//Run through the loops to get a better result around the redTNT and blueTNT
7171
//It is not perfect dude to the round up
7272
for(int r = -5; r <= 5; r++)
@@ -78,7 +78,7 @@ public static bool CalculateTNTAmount(int maxTicks , double maxDistance)
7878
PearlEntity pearlEntity = PearlSimulation(redTNT + r , blueTNT + b , tick , direction);
7979

8080
//Only the pearl with a difference smaller than 5 will pass the check
81-
if((pearlEntity.Position - Data.Destination).ToSurface2D().Absolute() <= 5)
81+
if((pearlEntity.Position - Data.Destination).ToSurface2D().Absolute() <= maxDistance)
8282
{
8383

8484
TNTCalculationResult result = new TNTCalculationResult
@@ -93,7 +93,7 @@ public static bool CalculateTNTAmount(int maxTicks , double maxDistance)
9393

9494
//Bypass MaxTNT check if it is set to 0
9595
//If MaxTNT is greater than 0 , only add those result which is lesser than MaxTNT
96-
//If both redTNT and blueTNT are not greater or equal than zero , ignore them
96+
//If both redTNT and blueTNT are not greater or equal to zero , ignore them
9797
if((Data.MaxTNT <= 0 || (result.Blue <= Data.MaxTNT && result.Red <= Data.MaxTNT)) && result.Blue >= 0 && result.Red >= 0)
9898
Data.TNTResult.Add(result);
9999

@@ -123,7 +123,7 @@ public static bool CalculateTNTAmount(int maxTicks , double maxDistance)
123123
/// <exception cref="ArgumentException"></exception>
124124
public static List<Entity> CalculatePearlTrace(int redTNT , int blueTNT , int ticks , Direction direction)
125125
{
126-
List<Entity> result = new List<Entity>();
126+
List<Entity> result = new List<Entity>(ticks + 1);
127127
PearlEntity pearl = new PearlEntity((PearlEntity)Data.Pearl.AddPosition(Data.PearlOffset));
128128

129129
CalculateTNTVector(direction , out Space3D redTNTVector , out Space3D blueTNTVector);
@@ -166,7 +166,6 @@ public static bool CalculateTNTConfiguration(int redTNT , int blueTNT , out BitA
166166

167167
for(int i = 0; i < sortedRedConfig.Count; i++)
168168
{
169-
170169
if(redTNT >= sortedRedConfig[i])
171170
{
172171
redTNT -= sortedRedConfig[i];
@@ -179,7 +178,6 @@ public static bool CalculateTNTConfiguration(int redTNT , int blueTNT , out BitA
179178

180179
for(int i = 0; i < sortedBlueConfig.Count; i++)
181180
{
182-
183181
if(blueTNT >= sortedBlueConfig[i])
184182
{
185183
blueTNT -= sortedBlueConfig[i];
@@ -199,10 +197,10 @@ public static bool CalculateTNTConfiguration(int redTNT , int blueTNT , out BitA
199197
}
200198

201199
/// <summary>
202-
/// Calculate the accelerating vector of each Blue and Red TNT in given Direction
200+
/// Calculate the accelerating vector created by Blue and Red TNT in a given Direction
203201
/// </summary>
204202
/// <param name="direction">The Acceleration Direction of the Ender Pearl</param>
205-
/// <param name="redTNTVector">Return am accelerating vector of Red TNT</param>
203+
/// <param name="redTNTVector">Return an accelerating vector of Red TNT</param>
206204
/// <param name="blueTNTVector">Return an accelerating vector of Blue TNT</param>
207205
/// <exception cref="ArgumentException"></exception>
208206
public static void CalculateTNTVector(Direction direction , out Space3D redTNTVector , out Space3D blueTNTVector)
@@ -246,18 +244,14 @@ public static void CalculateTNTVector(Direction direction , out Space3D redTNTVe
246244

247245
private static Space3D TNTDirectionToCoordinate(Direction coner)
248246
{
249-
Space3D tntCoordinate = new Space3D(0 , 0 , 0);
250-
251-
if(coner == Direction.NorthEast)
252-
tntCoordinate = Data.NorthEastTNT;
253-
else if(coner == Direction.NorthWest)
254-
tntCoordinate = Data.NorthWestTNT;
255-
else if(coner == Direction.SouthEast)
256-
tntCoordinate = Data.SouthEastTNT;
257-
else if(coner == Direction.SouthWest)
258-
tntCoordinate = Data.SouthWestTNT;
259-
260-
return tntCoordinate;
247+
return coner switch
248+
{
249+
Direction.NorthEast => Data.NorthEastTNT,
250+
Direction.NorthWest => Data.NorthWestTNT,
251+
Direction.SouthEast => Data.SouthEastTNT,
252+
Direction.SouthWest => Data.SouthWestTNT,
253+
_ => Space3D.zero
254+
};
261255
}
262256
}
263257
}

PearlCalculatorLib/Manually/Calculation.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,48 @@ public static class Calculation
1212
public static bool CalculateTNTAmount(Surface2D destination , int ticks , out List<TNTCalculationResult> result)
1313
{
1414
int aTNT , bTNT;
15-
Space3D aTNTVector , bTNTVector;
15+
double denominator, trueA, trueB;
16+
Space3D aTNTVector , bTNTVector , distance;
1617

1718
double divider = 0;
18-
Space3D distance = destination.ToSpace3D() - Data.Pearl.Position;
19+
20+
distance = destination.ToSpace3D() - Data.Pearl.Position;
1921
result = new List<TNTCalculationResult>();
2022

21-
if(Math.Abs(distance.X) == 0 && Math.Abs(distance.Z) == 0)
23+
if(distance.Absolute().ToSurface2D() == 0)
2224
return false;
2325

2426
aTNTVector = VectorCalculation.CalculateMotion(Data.Pearl.Position , Data.ATNT);
2527
bTNTVector = VectorCalculation.CalculateMotion(Data.Pearl.Position , Data.BTNT);
26-
27-
if(aTNTVector.Z * bTNTVector.X - bTNTVector.Z * aTNTVector.X == 0)
28+
denominator = aTNTVector.Z * bTNTVector.X - bTNTVector.Z * aTNTVector.X;
29+
30+
trueA = (distance.Z * bTNTVector.X - distance.X * bTNTVector.Z) / (aTNTVector.Z * bTNTVector.X - bTNTVector.Z * aTNTVector.X);
31+
trueB = (distance.X - trueA * aTNTVector.X) / bTNTVector.X;
32+
33+
34+
if(denominator == 0)
2835
return false;
2936

3037
for(int i = 1; i <= ticks; i++)
3138
{
3239
divider += Math.Pow(0.99 , i - 1);
33-
distance = (distance - Data.Pearl.Position) / divider;
34-
35-
aTNT = Convert.ToInt32(Math.Round((distance.Z * bTNTVector.X - distance.X * bTNTVector.Z) / (aTNTVector.Z * bTNTVector.X - bTNTVector.Z * aTNTVector.X)));
36-
bTNT = Convert.ToInt32(Math.Round((distance.X - aTNT * aTNTVector.X) / bTNTVector.X));
40+
aTNT = Convert.ToInt32(trueA / divider);
41+
bTNT = Convert.ToInt32(trueB / divider);
3742

3843
for(int a = -5; a <= 5; a++)
3944
{
4045

4146
for(int b = -5; b <= 5; b++)
4247
{
4348
PearlEntity aPearl = PearlSimulation(aTNT + a , bTNT + b , i , aTNTVector , bTNTVector);
44-
Surface2D difference = aPearl.Position.ToSurface2D() - destination;
49+
Surface2D displacement = aPearl.Position.ToSurface2D() - destination;
4550

46-
if(difference.AxialDistanceLessOrEqualTo(10) && bTNT + b > 0 && aTNT + a > 0)
51+
if(displacement.AxialDistanceLessOrEqualTo(10) && bTNT + b > 0 && aTNT + a > 0)
4752
{
4853

4954
TNTCalculationResult tResult = new TNTCalculationResult
5055
{
51-
Distance = aPearl.Position.Distance2D(destination.ToSpace3D()) ,
56+
Distance = displacement.Distance(Surface2D.zero) ,
5257
Tick = i ,
5358
Blue = bTNT + b ,
5459
Red = aTNT + a ,

PearlCalculatorLib/PearlCalculationLib/World/Direction.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ public static bool TryParse(string s , out Direction result)
9393

9494
public static Direction GetDirection(double angle)
9595
{
96-
Direction direction = World.Direction.None;
96+
Direction direction = Direction.None;
9797

9898
if(angle > -135 && angle <= -45)
99-
direction = World.Direction.East;
99+
direction = Direction.East;
100100
else if(angle > -45 && angle <= 45)
101-
direction = World.Direction.South;
101+
direction = Direction.South;
102102
else if(angle > 45 && angle <= 135)
103-
direction = World.Direction.West;
103+
direction = Direction.West;
104104
else if((angle > 135 && angle <= 180) || (angle > -180 && angle <= -135))
105-
direction = World.Direction.North;
105+
direction = Direction.North;
106106
return direction;
107107
}
108108
}

PearlCalculatorLib/PearlCalculationLib/World/Surface2D.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static Space3D FromPolarCoordinate(double lenght , double Radinat)
5454

5555
public Surface2D Absolute() => new Surface2D(Math.Abs(X) , Math.Abs(Z));
5656

57+
public double Distance(Surface2D position2) => Math.Sqrt(Math.Pow(position2.X - X , 2) + Math.Pow(position2.Z - Z , 2));
58+
5759
public override bool Equals(object obj) => obj is Surface2D s && Equals(s);
5860

5961
public bool AxialDistanceLessThan(double distance) => Math.Abs(X) < distance && Math.Abs(Z) < distance;

0 commit comments

Comments
 (0)