-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUtils.cpp
More file actions
294 lines (214 loc) · 7.77 KB
/
ClientUtils.cpp
File metadata and controls
294 lines (214 loc) · 7.77 KB
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
#include "ClientUtils.h"
#include <math.h>
#include <Windows.h>
std::vector<EntityPlayer> GetPlayerList()
{
std::vector<EntityPlayer> playerList;
Minecraft mc = Minecraft::getMinecraft();
if (mc.isInvalid()) {
return playerList;
}
WorldClient world = mc.getWorld();
if (world.isInvalid()) {
return playerList;
}
std::vector<Entity> entityList = world.getLoadedEntityList();
for (Entity entity : entityList)
{
if (g_env->IsInstanceOf(entity.obj(), forgeCache.FindClassCache("net/minecraft/entity/player/EntityPlayer"))) {
EntityPlayer player(entity.obj());
playerList.emplace_back(player);
}
}
return playerList;
}
bool NullCheck()
{
Minecraft mc = Minecraft::getMinecraft();
if (mc.isInvalid()) {
return true;
}
EntityPlayerSP localPlayer = mc.getPlayer();
if (localPlayer.isInvalid()) {
return true;
}
return false;
}
float wrapDegrees(float p_76142_0_) {
p_76142_0_ = fmod(p_76142_0_, 360.0);
if (p_76142_0_ >= 180.0F) {
p_76142_0_ -= 360.0F;
}
if (p_76142_0_ < -180.0F) {
p_76142_0_ += 360.0F;
}
return p_76142_0_;
}
float degToRadiants(float x)
{
return x * 3.141592653589793 / 180.f;
}
float radiantsToDeg(float x)
{
return x * 180.f / 3.141592653589793;
}
Vector2 getAngles(Vector3 pos, Vector3 pos1)
{
double d_x = pos1.X - pos.X;
double d_y = pos1.Y - pos.Y;
double d_z = pos1.Z - pos.Z;
double hypothenuse = sqrt(d_x * d_x + d_z * d_z);
float yaw = radiantsToDeg(atan2(d_z, d_x)) - 90.f;
float pitch = radiantsToDeg(-atan2(d_y, hypothenuse));
return Vector2(yaw, pitch);
}
std::vector<float> GetRotationsNeeded(EntityPlayer entity)
{
std::vector<float> result;
if (entity.isInvalid()) {
return result;
}
Minecraft mc = Minecraft::getMinecraft();
EntityPlayerSP player = mc.getPlayer();
double diffX = entity.getPosX() - player.getPosX();
double diffZ = entity.getPosZ() - player.getPosZ();
double diffY = entity.getPosY() + entity.getEyeHeight() - (player.getPosY() + player.getEyeHeight());
double dist = sqrt(diffX * diffX + diffZ * diffZ);
float yaw = (float)(atan2(diffZ, diffX) * 180.0 / 3.141592653589793) - 90.0F;
float pitch = (float)-(atan2(diffY, dist) * 180.0 / 3.141592653589793);
result.push_back(player.getRotationYaw() + wrapDegrees(yaw - player.getRotationYaw()));
result.push_back(player.getRotationPitch() + wrapDegrees(pitch - player.getRotationPitch()));
return result;
}
int GetDistanceFromMouse(EntityPlayer entity)
{
std::vector<float> neededRotations = GetRotationsNeeded(entity);
if (neededRotations.size() == 2) {
Minecraft mc = Minecraft::getMinecraft();
EntityPlayer player = mc.getPlayer();
float neededYaw = player.getRotationYaw() - neededRotations[0];
float neededPitch = player.getRotationPitch() - neededRotations[1];
float distanceFromMouse = sqrt(neededYaw * neededYaw + neededPitch * neededPitch * 2.0f);
return (int)distanceFromMouse;
}
return -1;
}
bool IsInAttackFOV(EntityPlayer entity, int fov)
{
return GetDistanceFromMouse(entity) <= fov;
}
bool IsInAttackRange(EntityLivingBase entity, float range)
{
Minecraft mc = Minecraft::getMinecraft();
EntityPlayerSP player = mc.getPlayer();
return entity.getDistanceToEntity(player) <= range;
}
bool IsClosest(EntityLivingBase entity, EntityLivingBase entityPriority)
{
Minecraft mc = Minecraft::getMinecraft();
EntityPlayerSP player = mc.getPlayer();
return entityPriority.isInvalid() || player.getDistanceToEntity(entity) < player.getDistanceToEntity(entityPriority);
}
bool IsLowHealth(EntityLivingBase entity, EntityLivingBase entityPriority)
{
return entityPriority.isInvalid() || entity.getHealth() < entityPriority.getHealth();
}
float updateRotation(float current, float target, float speed)
{
float var4 = wrapDegrees(target - current);
if (var4 > speed) {
var4 = speed;
}
if (var4 < -speed) {
var4 = -speed;
}
return current + var4;
}
void AssistFaceEntity(EntityPlayer entity, float yaw, float pitch)
{
if (entity.isInvalid()) {
return;
}
Minecraft mc = Minecraft::getMinecraft();
EntityPlayerSP player = mc.getPlayer();
double diffX = entity.getPosX() - player.getPosX();
double diffZ = entity.getPosZ() - player.getPosZ();
double yDifference;
yDifference = entity.getPosY() + entity.getEyeHeight() - (
player.getPosY() + player.getEyeHeight());
double dist = sqrt(diffX * diffX + diffZ * diffZ);
float rotationYaw = (float)(atan2(diffZ, diffX) * 180.0 / 3.14159265358979323846) - 90.0F;
float rotationPitch = (float)-(atan2(yDifference, dist) * 180.0 / 3.14159265358979323846);
if (yaw > 0) {
player.setRotationYaw(updateRotation(player.getRotationYaw(), rotationYaw, yaw / 4));
}
if (pitch > 0) {
player.setRotationPitch(updateRotation(player.getRotationPitch(), rotationPitch, pitch / 4));
}
}
void MoveMouseToEntity(EntityPlayer entity, float yawSpeed, float pitchSpeed) {
if (entity.isInvalid()) {
return;
}
Minecraft mc = Minecraft::getMinecraft();
EntityPlayerSP player = mc.getPlayer();
double diffX = entity.getPosX() - player.getPosX();
double diffZ = entity.getPosZ() - player.getPosZ();
double yDifference;
yDifference = entity.getPosY() + entity.getEyeHeight() - (
player.getPosY() + player.getEyeHeight());
double dist = sqrt(diffX * diffX + diffZ * diffZ);
float rotationYaw = (float)(atan2(diffZ, diffX) * 180.0 / 3.14159265358979323846) - 90.0F;
float rotationPitch = (float)-(atan2(yDifference, dist) * 180.0 / 3.14159265358979323846);
POINT cursor;
GetCursorPos(&cursor);
float yawDiff = wrapDegrees(player.getRotationYaw()) - wrapDegrees(rotationYaw);
float pitchDiff = wrapDegrees(player.getRotationPitch()) - wrapDegrees(rotationPitch);
//mouse_event(MOUSEEVENTF_MOVE, -wrapDegrees(yawDiff), -wrapDegrees(pitchDiff), 0, 0);
mouse_event(MOUSEEVENTF_MOVE, updateRotation(0, -wrapDegrees(yawDiff), yawSpeed / 4), updateRotation (0, -wrapDegrees(pitchDiff), pitchSpeed / 4), 0, 0);
}
double getRandomDouble()
{
std::random_device rd; // 使用随机设备获取种子
std::mt19937 gen(rd()); // 使用Mersenne Twister引擎
std::uniform_real_distribution<double> dis(0.0, 1.0); // 使用均匀分布在0.0到1.0之间
return dis(gen);
}
Vector4 Multiply(Vector4 vec, Matrix matrix)
{
float* mat = (float*)matrix.Data;
return Vector4(
vec.X * mat[0] + vec.Y * mat[4] + vec.Z * mat[8] + vec.W * mat[12],
vec.X * mat[1] + vec.Y * mat[5] + vec.Z * mat[9] + vec.W * mat[13],
vec.X * mat[2] + vec.Y * mat[6] + vec.Z * mat[10] + vec.W * mat[14],
vec.X * mat[3] + vec.Y * mat[7] + vec.Z * mat[11] + vec.W * mat[15]
);
}
bool World2Screen(const Vector3& position, const Matrix& modelview_matrix, const Matrix& projection_matrix, int screen_width, int screen_height, Vector2& screenPos)
{
Vector4 clipSpacePos = Multiply(Multiply(Vector4(position.X, position.Y, position.Z, 1.0f), modelview_matrix), projection_matrix);
Vector3 ndcSpacePos = Vector3(clipSpacePos.X / clipSpacePos.W, clipSpacePos.Y / clipSpacePos.W, clipSpacePos.Z / clipSpacePos.W);
// nPlane = -1, fPlane = 1
if (ndcSpacePos.Z <= 1.0 || ndcSpacePos.Z >= 1.15)
{
return false;
}
Minecraft mc = Minecraft::getMinecraft();
EntityPlayer player = mc.getPlayer();
screenPos.X = ((ndcSpacePos.X + 1.0f) / 2.0f) * screen_width;
screenPos.Y = ((1.0f - ndcSpacePos.Y) / 2.0f) * screen_height;
return true;
}
bool WorldToScreen_NoMatrix(Vector2& screenPos, float 水平角度差, float 高低角度差, int screen_width, int screen_height)
{
float 高低可视角度 = (float)((double)atan2(screen_height, screen_width) * 180 / 3.14159265358979323846);
if (fabs(水平角度差) > 45 || fabs(高低角度差) > 高低可视角度)
{
return false;// 不在屏幕范围内
}
int 水平差 = (int)(tan(水平角度差 * 3.14159265358979323846 / 180) * ((screen_width) / 2));
screenPos.X = (float)(screen_width / 2 + 水平差);
int 高度差 = (int)(tan(高低角度差 * 3.14159265358979323846 / 180) * ((screen_width) / 2));
screenPos.Y = (float)(screen_height / 2 + 高度差);
return true;
}