Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/lure #69

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ def handleGameplayTasks(context):
context['gameWindow']['previousMonsters'] = context['gameWindow']['monsters']
return context
hasCreaturesToAttackAfterCheck = hasCreaturesToAttack(context)
if hasCreaturesToAttackAfterCheck:
if context['cavebot']['closestCreature'] is not None:
context['way'] = 'cavebot'
if context['targeting']['enabled'] == False:
context['way'] = 'waypoint'
else:
if hasCreaturesToAttackAfterCheck:
if context['cavebot']['closestCreature'] is not None:
context['way'] = 'cavebot'
else:
context['way'] = 'waypoint'
else:
context['way'] = 'waypoint'
else:
context['way'] = 'waypoint'
if hasCreaturesToAttackAfterCheck and shouldAskForCavebotTasks(context):
currentRootTask = currentTask.rootTask if currentTask is not None else None
isTryingToAttackClosestCreature = currentRootTask is not None and (currentRootTask.name == 'attackClosestCreature')
Expand Down
29 changes: 20 additions & 9 deletions src/gameplay/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
'waypoints': {
'currentIndex': None,
'points': np.array([
('', 'lure', (33158, 32317, 12), {}),
# maior que X mobs ligar o target
# habilitar o target ao tomar block
# ('', 'lure', (33165, 32287, 12), {}),
# ('', 'lure', (33139, 32287, 12), {}),
# ('', 'lure', (33156, 32268, 12), {}),
# ('', 'lure', (33171, 32237, 12), {}),
# ('', 'lure', (33142, 32240, 12), {}),



# werehyaena cave 1
('', 'walk', (33214, 32458, 8), {}),
('', 'moveUp', (33214, 32456, 8), {'direction': 'north'}),
Expand All @@ -35,15 +46,15 @@
('', 'moveDown', (33227, 32358, 8), {'direction': 'east'}),
('', 'walk', (33222, 32355, 9), {}), # 10
('', 'moveDown', (33222, 32355, 9), {'direction': 'north'}),
('caveStart', 'walk', (33207, 32353, 10), {}),
('', 'walk', (33189, 32348, 10), {}),
('', 'walk', (33193, 32366, 10), {}),
('', 'walk', (33208, 32367, 10), {}),
('', 'walk', (33223, 32387, 10), {}),
('', 'walk', (33200, 32389, 10), {}),
('', 'walk', (33196, 32398, 10), {}),
('', 'walk', (33210, 32373, 10), {}),
('', 'walk', (33221, 32351, 10), {}), # 20
('caveStart', 'lure', (33207, 32353, 10), {}),
('', 'lure', (33189, 32348, 10), {}),
('', 'lure', (33193, 32366, 10), {}),
('', 'lure', (33208, 32367, 10), {}),
('', 'lure', (33223, 32387, 10), {}),
('', 'lure', (33200, 32389, 10), {}),
('', 'lure', (33196, 32398, 10), {}),
('', 'lure', (33210, 32373, 10), {}),
('', 'lure', (33221, 32351, 10), {}), # 20
('', 'dropFlasks', (33306, 32289, 7), {}),
('', 'refillChecker', (33306, 32289, 7), { # 22
'minimumOfManaPotions': 200,
Expand Down
4 changes: 1 addition & 3 deletions src/gameplay/core/tasks/dropFlasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from src.repositories.inventory.core import images
from ...typings import Context
from .common.vector import VectorTask
from .dropEachFlask import DropEachFlaskTask
Expand All @@ -15,11 +14,10 @@ def __init__(self):
self.delayAfterComplete = 1
self.isRootTask = True

# TODO: add unit tests
def onBeforeStart(self, context: Context) -> Context:
self.tasks = [
OpenBackpackTask(context['backpacks']['main']).setParentTask(self).setRootTask(self),
ExpandBackpackTask(images['containersBars'][context['backpacks']['main']]).setParentTask(self).setRootTask(self),
ExpandBackpackTask(context['backpacks']['main']).setParentTask(self).setRootTask(self),
DropEachFlaskTask(context['backpacks']['main']).setParentTask(self).setRootTask(self),
SetNextWaypointTask().setParentTask(self).setRootTask(self),
]
Expand Down
21 changes: 7 additions & 14 deletions src/gameplay/core/tasks/expandBackpack.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
from src.gameplay.typings import Context
from src.repositories.inventory.core import images
from src.shared.typings import GrayImage
from src.utils.core import locate
from src.utils.mouse import drag
from ...typings import Context
from .common.base import BaseTask


# TODO: ignore when backpack already expanded
# TODO: check if backpack is expanded on did
class ExpandBackpackTask(BaseTask):
def __init__(self, backpackBarImage: GrayImage):
def __init__(self, backpack: str):
super().__init__()
self.name = 'expandBackpack'
self.delayBeforeStart = 1
self.delayAfterComplete = 1
self.terminable = False
self.backpackBarImage = backpackBarImage

# TODO: ignore if backpack already expanded
# TODO: add unit tests
def shouldIgnore(self, context: Context) -> bool:
return super().shouldIgnore(context)
self.backpack = backpack

# TODO: add unit tests
def do(self, context: Context) -> Context:
backpackBarImage = images['containersBars'][context['backpacks']['main']]
# TODO: locate should be done in right content position to avoid calculation in whole screen
backpackBarPosition = locate(context['screenshot'], self.backpackBarImage, confidence=0.8)
backpackBarPosition = locate(context['screenshot'], backpackBarImage, confidence=0.8)
croppedImage = context['screenshot'][backpackBarPosition[1]:, backpackBarPosition[0]:]
# TODO: locate should be done in right content position to avoid calculation in whole screen
backpackBottomBarPosition = locate(croppedImage, images['containersBars']['backpack bottom'], confidence=0.8)
backpackBottomBarPositionX = backpackBottomBarPosition[0] + backpackBarPosition[0]
backpackBottomBarPositionY = backpackBottomBarPosition[1] + backpackBarPosition[1]
Expand All @@ -36,8 +34,3 @@ def do(self, context: Context) -> Context:
return context
drag((backpackBottomBarPositionX, backpackBottomBarPositionY), (backpackBottomBarPositionX, backpackBottomBarPositionY + scrollY))
return context

# TODO: check if backpack is expanded
# TODO: add unit tests
def did(self, _: Context) -> bool:
return True
1 change: 0 additions & 1 deletion src/gameplay/core/tasks/goToFreeDepot.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def getVisibleDepotCoordinates(self, coordinate: Coordinate, depotCoordinates: C

# TODO: add unit tests
def ping(self, context: Context) -> Context:
print('GoToFreeDepotTask.ping')
if self.closestFreeDepotCoordinate is None:
return context
if self.state == 'walkingIntoFreeDepot' and context['radar']['coordinate'][0] == self.closestFreeDepotCoordinate[0] and context['radar']['coordinate'][1] == self.closestFreeDepotCoordinate[1]:
Expand Down
9 changes: 4 additions & 5 deletions src/gameplay/core/tasks/lootCorpse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@


class LootCorpseTask(VectorTask):
def __init__(self, corpse: Creature):
def __init__(self, creature: Creature):
super().__init__()
self.name = 'lootCorpse'
self.isRootTask = True
self.corpse = corpse
self.creature = creature

# TODO: add unit tests
def onBeforeStart(self, context: Context) -> Context:
self.tasks = [
# TODO: add walkToCoordinate to reach dead corpse
CollectDeadCorpseTask(self.corpse).setParentTask(self).setRootTask(self)
# TODO: add walkToCoordinate to reach dead creature
CollectDeadCorpseTask(self.creature).setParentTask(self).setRootTask(self)
]
return context
43 changes: 43 additions & 0 deletions src/gameplay/core/tasks/lure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from src.gameplay.core.waypoint import resolveGoalCoordinate
from src.gameplay.typings import Context
import src.repositories.gameWindow.creatures as gameWindowCreatures
from ...typings import Context
from .common.vector import VectorTask
from .walkToCoordinate import WalkToCoordinateTask


# Tarefas:
# - Se algum bicho parar perto da coordenada, ligar o target
# - Se levar trap, ligar o target
class LureWaypointTask(VectorTask):
# TODO: add types
def __init__(self, waypoint):
super().__init__()
self.name = 'lureWaypoint'
self.delayAfterComplete = 1
self.isRootTask = True
self.manuallyTerminable = True
self.waypoint = waypoint

def shouldManuallyComplete(self, context: Context) -> bool:
return len(context['battleList']['creatures']) == 0

# def ping(self, context: Context) -> Context:
# if gameWindowCreatures.isTrappedByCreatures(context['gameWindow']['monsters'], context['radar']['coordinate']):
# context['targeting']['enabled'] = True
# return context

def onBeforeStart(self, context: Context) -> Context:
context['targeting']['enabled'] = False
self.tasks = [
WalkToCoordinateTask(self.waypoint['coordinate']).setParentTask(self).setRootTask(self),
]
return context

def onComplete(self, context: Context) -> Context:
# nextWaypointIndex = getNextArrayIndex(
# context['cavebot']['waypoints']['points'], context['cavebot']['waypoints']['currentIndex'])
# context['cavebot']['waypoints']['currentIndex'] = nextWaypointIndex
# currentWaypoint = context['cavebot']['waypoints']['points'][context['cavebot']['waypoints']['currentIndex']]
# context['cavebot']['waypoints']['state'] = resolveGoalCoordinate(context['radar']['coordinate'], currentWaypoint)
return context
3 changes: 3 additions & 0 deletions src/gameplay/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .core.tasks.depositItems import DepositItemsTask
from .core.tasks.dropFlasks import DropFlasksTask
from .core.tasks.logout import LogoutTask
from .core.tasks.lure import LureWaypointTask
from .core.tasks.refill import RefillTask
from .core.tasks.refillChecker import RefillCheckerTask
from .core.tasks.singleWalk import SingleWalkTask
Expand All @@ -24,6 +25,8 @@ def resolveTasksByWaypoint(waypoint: Waypoint) -> Union[BaseTask, VectorTask]:
return DropFlasksTask()
elif waypoint['type'] == 'logout':
return LogoutTask()
elif waypoint['type'] == 'lure':
return LureWaypointTask(waypoint)
elif waypoint['type'] == 'moveDown':
return SingleWalkTask(waypoint['type'], waypoint['options']['direction'])
elif waypoint['type'] == 'moveUp':
Expand Down
21 changes: 21 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import src.repositories.gameWindow.creatures as gameWindowCreatures
import src.utils.core
import src.repositories.battleList.core
import src.repositories.battleList.extractors
import src.repositories.gameWindow.config
import src.repositories.gameWindow.core



grayScreenshot = src.utils.core.getScreenshot()
radarCoordinate = src.repositories.radar.core.getCoordinate(grayScreenshot)
content = src.repositories.battleList.extractors.getContent(grayScreenshot)
battleListCreatures = src.repositories.battleList.core.getCreatures(content)
beingAttackedCreatureCategory = src.repositories.battleList.core.getBeingAttackedCreatureCategory(battleListCreatures)
gameWindowSize = src.repositories.gameWindow.config.gameWindowSizes[1080]
gameWindowCoordinate = src.repositories.gameWindow.core.getCoordinate(grayScreenshot, gameWindowSize)
gameWindowImg = src.repositories.gameWindow.core.getImageByCoordinate(grayScreenshot, gameWindowCoordinate, gameWindowSize)
gameWindowCreaturess = src.repositories.gameWindow.creatures.getCreatures(battleListCreatures, 'left', gameWindowCoordinate, gameWindowImg, radarCoordinate,beingAttackedCreatureCategory=beingAttackedCreatureCategory)
isTrapped = gameWindowCreatures.isTrappedByCreatures(gameWindowCreaturess, radarCoordinate)

print('isTrapped', isTrapped)
35 changes: 35 additions & 0 deletions tests/unit/gameplay/core/tasks/test_dropFlasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from src.gameplay.core.tasks.dropEachFlask import DropEachFlaskTask
from src.gameplay.core.tasks.dropFlasks import DropFlasksTask
from src.gameplay.core.tasks.expandBackpack import ExpandBackpackTask
from src.gameplay.core.tasks.openBackpack import OpenBackpackTask
from src.gameplay.core.tasks.setNextWaypoint import SetNextWaypointTask


context = {'backpacks': {'main': 'beach backpack'}}

def test_should_test_default_params():
task = DropFlasksTask()
assert task.name == 'dropFlasks'
assert task.delayBeforeStart == 1
assert task.delayAfterComplete == True
assert task.isRootTask == True

def test_onBeforeStart():
task = DropFlasksTask()
assert task.onBeforeStart(context) == context
assert len(task.tasks) == 4
assert isinstance(task.tasks[0], OpenBackpackTask)
assert task.tasks[0].backpack == context['backpacks']['main']
assert task.tasks[0].parentTask == task
assert task.tasks[0].rootTask == task
assert isinstance(task.tasks[1], ExpandBackpackTask)
assert task.tasks[1].backpack == context['backpacks']['main']
assert task.tasks[1].parentTask == task
assert task.tasks[1].rootTask == task
assert isinstance(task.tasks[2], DropEachFlaskTask)
assert task.tasks[2].backpack == context['backpacks']['main']
assert task.tasks[2].parentTask == task
assert task.tasks[2].rootTask == task
assert isinstance(task.tasks[3], SetNextWaypointTask)
assert task.tasks[3].parentTask == task
assert task.tasks[3].rootTask == task
21 changes: 21 additions & 0 deletions tests/unit/gameplay/core/tasks/test_lootCorpse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from src.gameplay.core.tasks.collectDeadCorpse import CollectDeadCorpseTask
from src.gameplay.core.tasks.lootCorpse import LootCorpseTask


context = {}
creature = {'coordinate': (0, 0, 0)}

def test_should_test_default_params():
task = LootCorpseTask(creature)
assert task.name == 'lootCorpse'
assert task.isRootTask == True
assert task.creature == creature

def test_onBeforeStart():
task = LootCorpseTask(creature)
assert task.onBeforeStart(context) == context
assert len(task.tasks) == 1
assert isinstance(task.tasks[0], CollectDeadCorpseTask)
assert task.tasks[0].creature == creature
assert task.tasks[0].parentTask == task
assert task.tasks[0].rootTask == task
30 changes: 30 additions & 0 deletions tests/unit/gameplay/core/tasks/test_useShovelWaypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from src.gameplay.core.tasks.clickInCoordinate import ClickInCoordinateTask
from src.gameplay.core.tasks.setNextWaypoint import SetNextWaypointTask
from src.gameplay.core.tasks.useShovel import UseShovelTask
from src.gameplay.core.tasks.useShovelWaypoint import UseShovelWaypointTask


context = {}
waypoint = {'coordinate': (1, 2, 3)}

def test_should_test_default_params():
task = UseShovelWaypointTask(waypoint)
assert task.name == 'useShovelWaypoint'
assert task.isRootTask == True
assert task.waypoint == waypoint

def test_onBeforeStart():
task = UseShovelWaypointTask(waypoint)
assert task.onBeforeStart(context) == context
assert len(task.tasks) == 3
assert isinstance(task.tasks[0], UseShovelTask)
assert task.tasks[0].waypoint == waypoint
assert task.tasks[0].parentTask == task
assert task.tasks[0].rootTask == task
assert isinstance(task.tasks[1], ClickInCoordinateTask)
assert task.tasks[1].waypoint == waypoint
assert task.tasks[1].parentTask == task
assert task.tasks[1].rootTask == task
assert isinstance(task.tasks[2], SetNextWaypointTask)
assert task.tasks[2].parentTask == task
assert task.tasks[2].rootTask == task