Skip to content

Commit e6c539e

Browse files
committed
Fix looking TODOs in interact plugin, add eye_pos property to clientinfo
1 parent 6da771f commit e6c539e

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

spockbot/mcdata/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
###########
99

1010
CLIENT_TICK_RATE = 0.05
11+
PLAYER_EYE_HEIGHT = 1.62
1112
PLAYER_HEIGHT = 1.80
1213
PLAYER_WIDTH = 0.6
1314

spockbot/plugins/helpers/clientinfo.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ class ClientInfo(object):
7878
game_info (GameInfo): Information about the current world/server
7979
spawn_position (Position): Players initial position
8080
health (PlayerHealth): Player's health, food and saturation
81-
position (PlayerPosition): Player's Current position
81+
position (PlayerPosition): Player's current position
8282
player_list (dict): List of all players in the server
83+
eye_pos (PlayerPosition): Player's eye position
8384
8485
"""
8586
def __init__(self):
@@ -94,6 +95,10 @@ def __init__(self):
9495
self.position = PlayerPosition()
9596
self.player_list = {}
9697

98+
@property
99+
def eye_pos(self):
100+
return self.position + (0, const.PLAYER_EYE_HEIGHT, 0)
101+
97102
def reset(self):
98103
"""Resets the information in ClientInfo"""
99104
self.__init__()

spockbot/plugins/helpers/interact.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ def look_at_rel(self, delta):
8484
self.look(*delta.yaw_pitch)
8585

8686
def look_at(self, pos):
87-
delta = pos - self.clientinfo.position
88-
delta.y -= constants.PLAYER_HEIGHT
87+
delta = pos - self.clientinfo.eye_pos
8988
if delta.x or delta.z:
9089
self.look_at_rel(delta)
91-
else:
90+
else: # looking up or down, do not turn head
9291
self.look(self.clientinfo.position.yaw, delta.yaw_pitch.pitch)
9392

9493
def _send_dig_block(self, status, pos=None, face=constants.FACE_Y_POS):
@@ -102,7 +101,7 @@ def _send_dig_block(self, status, pos=None, face=constants.FACE_Y_POS):
102101

103102
def start_digging(self, pos):
104103
if self.auto_look:
105-
self.look_at(pos) # TODO look at block center
104+
self.look_at(pos.floor().iadd(0.5, 0.5, 0.5))
106105
self._send_dig_block(constants.DIG_START, pos)
107106
if self.auto_swing:
108107
self.swing_arm()
@@ -144,7 +143,7 @@ def click_block(self, pos, face=1, cursor_pos=Vector3(8, 8, 8),
144143
"""
145144
if look_at_block and self.auto_look:
146145
# TODO look at cursor_pos
147-
self.look_at(pos)
146+
self.look_at(pos.floor().iadd(0.5, 0.5, 0.5))
148147
self._send_click_block(pos, face, cursor_pos)
149148
if swing and self.auto_swing:
150149
self.swing_arm()

tests/plugins/helpers/test_interact.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from unittest import TestCase
22

3-
from spockbot.mcdata.constants import \
4-
ATTACK_ENTITY, INTERACT_ENTITY, PLAYER_HEIGHT
3+
from spockbot.mcdata import constants
54
from spockbot.plugins.helpers.clientinfo import PlayerPosition
65
from spockbot.plugins.helpers.interact import InteractPlugin
76
from spockbot.vector import Vector3
@@ -53,6 +52,7 @@ class InventoryMock(object):
5352
class ClientInfoMock(object):
5453
eid = 123
5554
position = PlayerPosition(1., 2., 3.)
55+
eye_pos = position + (0, constants.PLAYER_EYE_HEIGHT, 0)
5656

5757

5858
class InteractPluginTest(TestCase):
@@ -97,7 +97,7 @@ def test_look(self):
9797
self.assertAlmostEqual(ClientInfoMock.position.yaw, -90)
9898
self.assertAlmostEqual(ClientInfoMock.position.pitch, 0)
9999

100-
self.plug.look_at(Vector3(0, 2 + PLAYER_HEIGHT, 3))
100+
self.plug.look_at(Vector3(0, 2 + constants.PLAYER_EYE_HEIGHT, 3))
101101
self.assertAlmostEqual(ClientInfoMock.position.yaw, 90)
102102
self.assertAlmostEqual(ClientInfoMock.position.pitch, 0)
103103

@@ -115,32 +115,32 @@ def test_activate_item(self):
115115
# TODO deactivate_item
116116

117117
def test_entity(self):
118-
entity = DataDict(eid=234, x=2, y=2 + PLAYER_HEIGHT, z=4)
118+
entity = DataDict(eid=234, x=2, y=2 + constants.PLAYER_EYE_HEIGHT, z=4)
119119

120120
self.plug.use_entity(entity)
121121
self.assertAlmostEqual(ClientInfoMock.position.yaw, -45)
122122
self.assertAlmostEqual(ClientInfoMock.position.pitch, 0)
123-
self.assertEqual(NetMock.datas[-2].action, INTERACT_ENTITY)
123+
self.assertEqual(NetMock.datas[-2].action, constants.INTERACT_ENTITY)
124124
self.assertEqual(NetMock.datas[-2].target, 234)
125125
self.assertEqual(NetMock.idents[-1], 'PLAY>Animation')
126126

127127
self.plug.auto_look = False
128-
entity = DataDict(eid=345, x=0, y=3 + PLAYER_HEIGHT, z=3)
128+
entity = DataDict(eid=345, x=0, y=3 + constants.PLAYER_EYE_HEIGHT, z=3)
129129
self.plug.attack_entity(entity)
130130
# different pos, but look shouldn't have changed
131131
self.assertAlmostEqual(ClientInfoMock.position.yaw, -45)
132132
self.assertAlmostEqual(ClientInfoMock.position.pitch, 0)
133-
self.assertEqual(NetMock.datas[-2].action, ATTACK_ENTITY)
133+
self.assertEqual(NetMock.datas[-2].action, constants.ATTACK_ENTITY)
134134
self.assertEqual(NetMock.datas[-2].target, 345)
135135
self.assertEqual(NetMock.idents[-1], 'PLAY>Animation')
136136
self.plug.auto_look = True
137137

138138
self.plug.auto_swing = False
139-
entity = DataDict(eid=456, x=2, y=3 + PLAYER_HEIGHT, z=3)
139+
entity = DataDict(eid=456, x=2, y=3 + constants.PLAYER_EYE_HEIGHT, z=3)
140140
self.plug.mount_vehicle(entity)
141141
self.assertAlmostEqual(ClientInfoMock.position.yaw, -90)
142142
self.assertAlmostEqual(ClientInfoMock.position.pitch, -45)
143-
self.assertEqual(NetMock.datas[-1].action, INTERACT_ENTITY)
143+
self.assertEqual(NetMock.datas[-1].action, constants.INTERACT_ENTITY)
144144
self.assertEqual(NetMock.datas[-1].target, 456)
145145
self.plug.auto_swing = True
146146

0 commit comments

Comments
 (0)