forked from LegendsOfSky/PearlCalculatorCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculation.cs
216 lines (203 loc) · 10.8 KB
/
Calculation.cs
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
using PearlCalculatorLib.PearlCalculationLib.MathLib;
using PearlCalculatorLib.Result;
using PearlCalculatorLib.PearlCalculationLib;
using PearlCalculatorLib.PearlCalculationLib.World;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml.Serialization;
using PearlCalculatorLib.PearlCalculationLib.Entity;
namespace PearlCalculatorLib.General
{
public static class Calculation
{
private static PearlEntity PearlSimulation(int redTNT , int blueTNT , int ticks , Direction direction)
{
PearlEntity pearlEntity = new PearlEntity(Data.Pearl);
CalculateTNTVector(direction , out Space3D redTNTVector , out Space3D blueTNTVector);
pearlEntity.Motion = redTNT * redTNTVector + blueTNT * blueTNTVector;
for(int i = 0; i < ticks; i++)
{
pearlEntity.Tick();
}
return pearlEntity;
}
/// <summary>
/// Calculate all the suitable TNT combination
/// <para>There are some other parameters you had to input in <see cref="Data"/> for it to work correctly</para>
/// <para>See more detail in <see cref="Data"/></para>
/// </summary>
/// <param name="maxTicks">The maximum time the Ender Pearl allowed to travel</param>
/// <param name="maxDistance">The maximum difference between Ender Pearl drop off and Destination in each axis</param>
/// <returns>Returns a true or false value indicate whether the calculation is correctly executed
/// <para>TNT combination result will be stored into <see cref="Data.TNTResult"/></para>
/// </returns>
public static bool CalculateTNTAmount(int maxTicks , double maxDistance)
{
int redTNT;
int blueTNT;
Space3D distance;
double divider = 0;
Direction direction;
distance = Data.Destination - Data.Pearl.Position;
if(Math.Abs(distance.X) == 0 && Math.Abs(distance.Z) == 0)
return false;
Data.TNTResult.Clear();
direction = Data.Pearl.Position.Direction(Data.Pearl.Position.WorldAngle(Data.Destination));
CalculateTNTVector(direction , out Space3D redTNTVector , out Space3D blueTNTVector);
for(int tick = 1; tick <= maxTicks; tick++)
{
divider += Math.Pow(0.99 , tick - 1);
distance = (Data.Destination - Data.Pearl.Position) / divider;
redTNT = Convert.ToInt32(Math.Round((distance.Z * blueTNTVector.X - distance.X * blueTNTVector.Z) / (redTNTVector.Z * blueTNTVector.X - blueTNTVector.Z * redTNTVector.X)));
blueTNT = Convert.ToInt32(Math.Round((distance.X - redTNT * redTNTVector.X) / blueTNTVector.X));
for(int r = -5; r <= 5; r++)
{
for(int b = -5; b <= 5; b++)
{
PearlEntity pearlEntity = PearlSimulation(redTNT + r , blueTNT + b , tick , direction);
if(Math.Abs(pearlEntity.Position.X - Data.Destination.X) <= maxDistance && Math.Abs(pearlEntity.Position.Z - Data.Destination.Z) <= maxDistance)
{
TNTCalculationResult result = new TNTCalculationResult
{
Distance = pearlEntity.Position.Distance2D(Data.Destination) ,
Tick = tick ,
Blue = blueTNT + b ,
Red = redTNT + r ,
TotalTNT = blueTNT + b + redTNT + r ,
Pearl = pearlEntity
};
if(Data.MaxTNT <= 0)
Data.TNTResult.Add(result);
else if((blueTNT + b) <= Data.MaxTNT && (redTNT + r) <= Data.MaxTNT)
Data.TNTResult.Add(result);
if(Data.MaxCalculateTNT < result.TotalTNT)
Data.MaxCalculateTNT = result.TotalTNT;
if(Data.MaxCalculateDistance < result.Distance)
Data.MaxCalculateDistance = result.Distance;
}
}
}
}
Data.TNTResult = Data.TNTResult.Where(result => result.Blue >= 0 && result.Red >= 0).ToList();
return true;
}
/// <summary>
/// Calculate the trace of the Ender Pearl
/// <para>There are some other parameters you had to input in the <see cref="Data"/> class for it to work correctly</para>
/// <para>See more detail in <see cref="Data"/> class</para>
/// </summary>
/// <param name="redTNT">The Amount of Red TNT for Accelerating Ender Pearl</param>
/// <param name="blueTNT">The Amount of Blue TNT for Accelerating Ender Pearl</param>
/// <param name="ticks">The Maximum Tick the Ender Pearl Allowed to travel</param>
/// <param name="direction">The accelerating Direction of the Ender Pearl. Only Allow North, South, East, West</param>
/// <returns>Return A List of Entity contains the Motion and Position in each Ticks</returns>
public static List<Entity> CalculatePearlTrace(int redTNT , int blueTNT , int ticks , Direction direction)
{
List<Entity> result = new List<Entity>();
PearlEntity pearl = new PearlEntity(Data.Pearl);
CalculateTNTVector(direction , out Space3D redTNTVector , out Space3D blueTNTVector);
pearl.Motion = redTNT * redTNTVector + blueTNT * blueTNTVector + Data.Pearl.Motion;
result.Add(new PearlEntity(pearl));
for(int i = 0; i < ticks; i++)
{
pearl.Tick();
result.Add(new PearlEntity(pearl));
}
return result;
}
/// <summary>
/// Calculate the accelerating vector of each Blue and Red TNT in given Direction
/// </summary>
/// <param name="direction">The Acceleration Direction of the Ender Pearl</param>
/// <param name="redTNTVector">Return am accelerating vector of Red TNT</param>
/// <param name="blueTNTVector">Return an accelerating vector of Blue TNT</param>
public static void CalculateTNTVector(Direction direction , out Space3D redTNTVector , out Space3D blueTNTVector)
{
Space3D aVector = new Space3D(0 , 0 , 0);
Space3D bVector = new Space3D(0 , 0 , 0);
redTNTVector = new Space3D(0 , 0 , 0);
blueTNTVector = new Space3D(0 , 0 , 0);
Space3D pearlPosition = Data.PearlOffset.ToSpace3D();
pearlPosition.Y = Data.Pearl.Position.Y;
if(direction.IsSouth())
{
aVector = VectorCalculation.CalculateMotion(pearlPosition , Data.NorthEastTNT);
bVector = VectorCalculation.CalculateMotion(pearlPosition , Data.NorthWestTNT);
if(Data.DefaultBlueDuper.IsNorth())
{
blueTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultBlueDuper));
redTNTVector = aVector + bVector - blueTNTVector;
}
else if(Data.DefaultRedDuper.IsNorth())
{
redTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultBlueDuper));
blueTNTVector = aVector + bVector - redTNTVector;
}
}
else if(direction.IsNorth())
{
aVector = VectorCalculation.CalculateMotion(pearlPosition , Data.SouthEastTNT);
bVector = VectorCalculation.CalculateMotion(pearlPosition , Data.SouthWestTNT);
if(Data.DefaultBlueDuper.IsSouth())
{
blueTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultBlueDuper));
redTNTVector = aVector + bVector - blueTNTVector;
}
else if(Data.DefaultRedDuper.IsSouth())
{
redTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultRedDuper));
blueTNTVector = aVector + bVector - redTNTVector;
}
}
else if(direction.IsEast())
{
aVector = VectorCalculation.CalculateMotion(pearlPosition , Data.SouthWestTNT);
bVector = VectorCalculation.CalculateMotion(pearlPosition , Data.NorthWestTNT);
if(Data.DefaultBlueDuper.IsWest())
{
blueTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultBlueDuper));
redTNTVector = aVector + bVector - blueTNTVector;
}
else if(Data.DefaultRedDuper.IsWest())
{
redTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultRedDuper));
blueTNTVector = aVector + bVector - redTNTVector;
}
}
else if(direction.IsWest())
{
aVector = VectorCalculation.CalculateMotion(pearlPosition , Data.SouthEastTNT);
bVector = VectorCalculation.CalculateMotion(pearlPosition , Data.NorthEastTNT);
if(Data.DefaultBlueDuper.IsEast())
{
blueTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultBlueDuper));
redTNTVector = aVector + bVector - blueTNTVector;
}
else if(Data.DefaultRedDuper.IsEast())
{
redTNTVector = VectorCalculation.CalculateMotion(pearlPosition , TNTDirectionToCoordinate(Data.DefaultRedDuper));
blueTNTVector = aVector + bVector - redTNTVector;
}
}
else
throw new ArgumentException();
}
private static Space3D TNTDirectionToCoordinate(Direction coner)
{
Space3D tntCoordinate = new Space3D(0 , 0 , 0);
if(coner == Direction.NorthEast)
tntCoordinate = Data.NorthEastTNT;
else if(coner == Direction.NorthWest)
tntCoordinate = Data.NorthWestTNT;
else if(coner == Direction.SouthEast)
tntCoordinate = Data.SouthEastTNT;
else if(coner == Direction.SouthWest)
tntCoordinate = Data.SouthWestTNT;
return tntCoordinate;
}
}
}