Skip to content

Commit

Permalink
Fixed an alignment issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrixchung committed Jul 19, 2022
1 parent 4d5b9a8 commit 31c1c12
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
14 changes: 6 additions & 8 deletions gameui.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def run(self):
class ImageMsg:
offsetX: int
offsetY: int
isHollow: bool
isAligned: bool
isFocused: bool
fps: int
Expand All @@ -172,7 +171,7 @@ def __init__(self,screen:Screen,image:Image,logger:Logger=None):

def getNavPoint(self,compassImg) -> tuple : # return offsetX and offsetY
compassLeftTop = self.image.matchTemplate('compass',compassImg,confidence=0.45,center=False)
if compassLeftTop == (0,0): return (0,0,False) # can't get compassImage, left it standby
if compassLeftTop == (0,0): return (0,0) # can't get compassImage, left it standby
compassSize = self.image.getSize('compass')
trimX, trimY = int(compassSize[0]*0.2), int(compassSize[1]*0.2)
compassCropped = compassImg[compassLeftTop[1]-trimY:compassLeftTop[1]+compassSize[1]+trimY,compassLeftTop[0]-trimX:compassLeftTop[0]+compassSize[0]+trimX]
Expand All @@ -181,7 +180,7 @@ def getNavPoint(self,compassImg) -> tuple : # return offsetX and offsetY
compassImgSizeY, compassImgSizeX = compassCropped.shape[:2]
offsetX = navPointCenter[0]-compassImgSizeX/2
offsetY = navPointCenter[1]-compassImgSizeY/2
return (offsetX,offsetY,isHollow)
return (offsetX,offsetY)

def run(self):
isAligned = 0
Expand All @@ -207,11 +206,11 @@ def run(self):
compassImg = self.screen.getRegion('compass',img=originGrayImg)
destCircleImg = self.image.getImage('destCircle')
isAligned = 1 if checkAlignWithTemplate(centerImg,destCircleImg) else 0
(offsetX,offsetY,isHollow) = self.getNavPoint(compassImg)
(offsetX,offsetY) = self.getNavPoint(compassImg)
# print(offsetX,offsetY)
elapsedTime = time.time()-startTime
fps = int(1.0/elapsedTime)
message = ImageMsg(offsetX,offsetY,isHollow,isAligned,isFocused,fps,gameCoord[0],gameCoord[1])
message = ImageMsg(offsetX,offsetY,isAligned,isFocused,fps,gameCoord[0],gameCoord[1])
self._imageSignal.emit(message)
except:
self.logger.critical(traceback.format_exc())
Expand All @@ -223,7 +222,7 @@ class Main(QObject):
usingWatchdog = False
isDebug = True
offsetX = offsetY = 0
isHollow = isAligned = isFocused = False
isAligned = isFocused = False
fps = 0
windowLeftX = windowTopY = 0
stateList = []
Expand Down Expand Up @@ -317,7 +316,6 @@ def updateImage(self,data:ImageMsg):
#self.navCenter = data.navCenter
self.offsetX = data.offsetX
self.offsetY = data.offsetY
self.isHollow = data.isHollow
self.isAligned = data.isAligned
self.isFocused = data.isFocused
self.fps = data.fps
Expand All @@ -331,7 +329,7 @@ def updateImage(self,data:ImageMsg):
# try to send data to ScriptThread
if self.thread_script:
# pack & form a ScriptInputMsg
outputMsg = ScriptInputMsg(self.isAligned,self.isFocused,self.stateList,self.journal,self.guiFocus,self.offsetX,self.offsetY,self.isHollow,self.windowLeftX,self.windowTopY)
outputMsg = ScriptInputMsg(self.isAligned,self.isFocused,self.stateList,self.journal,self.guiFocus,self.offsetX,self.offsetY,self.windowLeftX,self.windowTopY)
# then emit it
self._outputSignalToScript.emit(outputMsg)

Expand Down
13 changes: 4 additions & 9 deletions utils/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class ScriptInputMsg:
guiFocus: str
offsetX: int
offsetY: int
isHollow: bool
windowLeftX: int
windowTopY: int
class routeWorker(QThread):
Expand Down Expand Up @@ -49,7 +48,6 @@ def _update(self,data:ScriptInputMsg) -> None:
self.guiFocus = data.guiFocus
self.offsetX = data.offsetX
self.offsetY = data.offsetY
self.isHollow = data.isHollow
self.windowCoord = (data.windowLeftX,data.windowTopY)
self.status = self.journal.status
self.shipLoc = self.journal.nav.location
Expand All @@ -72,19 +70,16 @@ def align(self) -> bool : # return False if already aligned
absX, absY = abs(offsetX), abs(offsetY)
if absX<3: trimX = ALIGN_TRIMM_DELAY
if absY<3: trimY = ALIGN_TRIMM_DELAY
if self.isHollow: # hollow dot, which means we are in the opposite side
if offsetY<0: self.sendKey('PitchUpButton',hold=ALIGN_KEY_DELAY)
else: self.sendKey('PitchDownButton',hold=ALIGN_KEY_DELAY)
elif absX>ALIGN_DEAD_ZONE: # align X-Axis first
if absY>ALIGN_DEAD_ZONE: # align Y-Axis first
if offsetY<0: self.sendKey('PitchUpButton',hold=ALIGN_KEY_DELAY-trimY)
else: self.sendKey('PitchDownButton',hold=ALIGN_KEY_DELAY-trimY)
elif absX>ALIGN_DEAD_ZONE:
if absX>ROLL_YAW_DEAD_ZONE:
if offsetX>0: self.sendKey('RollRightButton' if offsetY<0 else 'RollLeftButton',hold=0.1)
else: self.sendKey('RollLeftButton' if offsetY<0 else 'RollRightButton',hold=0.1)
else:
if offsetX>0: self.sendKey('YawRightButton',hold=ALIGN_KEY_DELAY-trimX)
else : self.sendKey('YawLeftButton',hold=ALIGN_KEY_DELAY-trimX)
elif absY>ALIGN_DEAD_ZONE:
if offsetY<0: self.sendKey('PitchUpButton',hold=ALIGN_KEY_DELAY-trimY)
else: self.sendKey('PitchDownButton',hold=ALIGN_KEY_DELAY-trimY)
return True

def sunAvoiding(self,fwdDelay=18,turnDelay=12):
Expand Down

0 comments on commit 31c1c12

Please sign in to comment.