-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
186 lines (137 loc) · 4.18 KB
/
client.py
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
from typing import List, Any, Optional, Tuple
import typing
from shader import ShaderProgram
import world
from world import World, nearestBlockCoord
from entity import Entity, EntityModel, Animation, EntityRenderData, AnimController
from player import Player
from util import BlockPos, rayAABBIntersect
from PIL import Image
import math
from math import sin, cos
class ClientData:
chunkProgram: ShaderProgram
blockProgram: ShaderProgram
skyProgram: ShaderProgram
entityProgram: ShaderProgram
guiProgram: ShaderProgram
transProgram: ShaderProgram
fullscreenVao: int
transColorTex: int
transDepthTex: int
textureAtlas: Any
atlasWidth: int
breakTextures: List[Any]
sunTex: Any
skyboxVao: Any
guiTextures: dict[str, Image.Image]
entityRenderData: dict[str, EntityRenderData]
entityModels: dict[str, EntityModel]
entityTextures: dict[str, int]
entityAnimations: dict[str, Animation]
entityAnimControllers: dict[str, AnimController]
glTextures: dict[Any, Any]
itemTextures: Any
translucentFb: int
CLIENT_DATA = ClientData()
class ClientState:
world: World
entities: List[Entity]
time: int
player: Player
breakingBlock: float
breakingBlockPos: BlockPos
lastDigSound: float
cameraPos: List[float]
cameraPitch: float
cameraYaw: float
height: int
width: int
horizFov: float
vertFov: float
vpDist: float
vpWidth: float
vpHeight: float
wireframe: bool
local: bool
renderDistanceSq: int
lastTickTime: float
lastFrameTime: float
chat: List[Tuple[float, Any]]
tickTimes: List[float]
tickTimeIdx: int
serverTickTimes: List[float]
serverTickTimeIdx: int
csToCanvasMat: Any
gravity: float
cinematic: bool
showDebugInfo: bool
w: bool
a: bool
s: bool
d: bool
shift: bool
space: bool
def getPlayer(self) -> Optional[Player]:
return self.player
def lookedAtBlock(self, useFluids: bool = False) -> Optional[Tuple[BlockPos, str]]:
player = self.getPlayer()
assert(player is not None)
cameraPos = typing.cast(Tuple[float, float, float], tuple(self.cameraPos))
return self.world.lookedAtBlock(player.reach, cameraPos, self.cameraPitch, self.cameraYaw, useFluids)
def lookedAtEntity(client: ClientState) -> Optional[int]:
lookX = cos(client.cameraPitch)*sin(-client.cameraYaw)
lookY = sin(client.cameraPitch)
lookZ = cos(client.cameraPitch)*cos(-client.cameraYaw)
if lookX == 0.0:
lookX = 1e-6
if lookY == 0.0:
lookY = 1e-6
if lookZ == 0.0:
lookZ = 1e-6
mag = math.sqrt(lookX**2 + lookY**2 + lookZ**2)
lookX /= mag
lookY /= mag
lookZ /= mag
rayOrigin = typing.cast(Tuple[float, float, float], tuple(client.cameraPos))
rayDir = (lookX, lookY, lookZ)
inters = []
reach = client.getPlayer().reach
for idx, entity in enumerate(client.entities):
if abs(entity.pos[0] - client.cameraPos[0]) + abs(entity.pos[2] - client.cameraPos[2]) > 2 * reach:
continue
(aabb0, aabb1) = entity.getAABB()
inter = rayAABBIntersect(rayOrigin, rayDir, aabb0, aabb1)
if inter is not None:
inters.append((idx, inter))
def dist(inter):
(_, i) = inter
dx = i[0] - rayOrigin[0]
dy = i[1] - rayOrigin[1]
dz = i[2] - rayOrigin[2]
return math.sqrt(dx**2 + dy**2 + dz**2)
inters.sort(key=dist)
if inters == []:
return None
else:
inter = inters[0]
if dist(inter) > reach:
print(f"dist: {dist(inter)}")
return None
else:
return inter[0]
def getLookVector(client: ClientState) -> Tuple[float, float, float]:
lookX = cos(client.cameraPitch)*sin(-client.cameraYaw)
lookY = sin(client.cameraPitch)
lookZ = cos(client.cameraPitch)*cos(-client.cameraYaw)
if lookX == 0.0:
lookX = 1e-6
if lookY == 0.0:
lookY = 1e-6
if lookZ == 0.0:
lookZ = 1e-6
mag = math.sqrt(lookX**2 + lookY**2 + lookZ**2)
lookX /= mag
lookY /= mag
lookZ /= mag
return (lookX, lookY, lookZ)