8000 cpu optimization. Partial updates on demand by elb91 · Pull Request #761 · hectorgimenez/koolo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cpu optimization. Partial updates on demand #761

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

Open
wants to merge 14 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
8000
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/bwmarrin/discordgo v0.28.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/gorilla/websocket v1.5.3
github.com/hectorgimenez/d2go v0.0.0-20250314185000-169a76515285
github.com/hectorgimenez/d2go v0.0.0-20250324070559-dba9a7b5a54f
github.com/inkeliz/gowebview v1.0.1
github.com/lxn/win v0.0.0-20210218163916-a377121e959e
github.com/otiai10/copy v1.14.0
Expand Down
20 changes: 4 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,12 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hectorgimenez/d2go v0.0.0-20241216152730-04f45d306978 h1:nfC7EZCCDEXNisTgE8LuCPRRxBbtnhP3hX1nxmZaS3s=
github.com/hectorgimenez/d2go v0.0.0-20241216152730-04f45d306978/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20241220085033-444f4b0e9b01 h1:2sXMzraTLYD5E6eY9sHA6OkDfYrqu3l2sbgAXENk7cg=
github.com/hectorgimenez/d2go v0.0.0-20241220085033-444f4b0e9b01/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20241230114702-591fbf16055e h1:gal4qtJv+5ue0NB+PxIN4E8bx3f2p3w7aSRUCkq/D+s=
github.com/hectorgimenez/d2go v0.0.0-20241230114702-591fbf16055e/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250106015039-8bce23b03973 h1:dRqPFWAf0yb1T2d/NQLOLTJkega8E8MZlVUuMKOrPkA=
github.com/hectorgimenez/d2go v0.0.0-20250106015039-8bce23b03973/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250119163231-415ac29ab1ab h1:vrZ5hlN1qnUXPHjVRCvSuWyjfauNopuMLU+/YmBge2c=
github.com/hectorgimenez/d2go v0.0.0-20250119163231-415ac29ab1ab/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250219111533-438f80b9113e h1:upmLVX6I3rYRJZXwVJ0ZDv0O0zcSpVLUKeCucbuHjyg=
github.com/hectorgimenez/d2go v0.0.0-20250219111533-438f80b9113e/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250221161127-5455fd977276 h1:A4LnLo5dsLmUzoDtndegivn5hx2TfiXL/18bepMxW70=
github.com/hectorgimenez/d2go v0.0.0-20250221161127-5455fd977276/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250227150416-4204d68f233f h1:Qp4gJANcIkJJdnImJ9G1LRLqZh0gXnONm9awBEMEjPk=
github.com/hectorgimenez/d2go v0.0.0-20250227150416-4204d68f233f/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250303230922-844439002c08 h1:ocaSoaHkM/hH6GBO49RlVezZVa9QXa8v15+mhlvIsBE=
github.com/hectorgimenez/d2go v0.0.0-20250303230922-844439002c08/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250314185000-169a76515285 h1:UOpB9azve0kdL5/E/qBT725Fft+fk33o0NUIP8GsFUo=
github.com/hectorgimenez/d2go v0.0.0-20250314185000-169a76515285/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250324070559-dba9a7b5a54f h1:OowTv4zUxdZN9Kal1ORV4MCGfkPoi16bMwtLXiP6oAI=
github.com/hectorgimenez/d2go v0.0.0-20250324070559-dba9a7b5a54f/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/inkeliz/gowebview v1.0.1 h1:4gpLE2qt4kV3DB+xHkHKUeLLiGPN5Xw3or9A3hVqYyA=
github.com/inkeliz/gowebview v1.0.1/go.mod h1:4SNjXp/fogE11MwvJD67kMBmSObY2BBqinEgH8+8eM8=
github.com/inkeliz/w32 v1.0.2 h1:Es8Bmw9ApOY0PVRpGs7wsqIKdK5C3xBkP5TOATfVmtU=
Expand Down
2 changes: 2 additions & 0 deletions internal/action/horadric_cube.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func ensureCubeIsOpen() error {
ctx := context.Get()
ctx.Logger.Debug("Opening Horadric Cube...")

ctx.RefreshOpenMenus()
if ctx.Data.OpenMenus.Cube {
ctx.Logger.Debug("Horadric Cube window already open")
return nil
Expand All @@ -173,6 +174,7 @@ func ensureCubeIsOpen() error {
ctx.HID.Click(game.RightButton, screenPos.X, screenPos.Y)
utils.Sleep(500)

ctx.RefreshOpenMenus()
if ctx.Data.OpenMenus.Cube {
ctx.Logger.Debug("Horadric Cube window detected")
return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/action/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func IdentifyAll(skipIdentify bool) error {
step.CloseAllMenus()
for !ctx.Data.OpenMenus.Inventory {
ctx.HID.PressKeyBinding(ctx.Data.KeyBindings.Inventory)
ctx.RefreshOpenMenus()
utils.Sleep(1000) // Add small delay to allow the game to open the inventory
}

Expand Down Expand Up @@ -90,7 +91,7 @@ func CainIdentify() error {
menuWait := time.Now().Add(2 * time.Second)
for time.Now().Before(menuWait) {
ctx.PauseIfNotPriority()
ctx.RefreshGameData()
ctx.RefreshOpenMenus()
if ctx.Data.OpenMenus.NPCInteract {
break
}
Expand Down
4 changes: 2 additions & 2 deletions internal/action/move.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ensureAreaSync(ctx *context.Status, expectedArea area.ID) error {

// Wait for area data to sync
for attempts := 0; attempts < maxAreaSyncAttempts; attempts++ {
ctx.RefreshGameData()
ctx.RefreshPlayerData()

if ctx.Data.PlayerUnit.Area == expectedArea {
return nil
Expand Down Expand Up @@ -213,7 +213,7 @@ func MoveTo(toFunc func() (data.Position, bool)) error {
}

for {
ctx.RefreshGameData()
ctx.RefreshPlayerData()
to, found := toFunc()
if !found {
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/action/step/close_all_menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CloseAllMenus() error {
// Pause the execution if the priority is not the same as the execution priority
ctx.PauseIfNotPriority()

ctx.RefreshGameData()
ctx.RefreshOpenMenus()
if attempts > 10 {
return errors.New("failed closing game menu")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/action/step/interact_npc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func InteractNPC(npcID npc.ID) error {
// Pause the execution if the priority is not the same as the execution priority
ctx.PauseIfNotPriority()

ctx.RefreshOpenMenus()
// Check if interaction succeeded and menu is open
if ctx.Data.OpenMenus.NPCInteract || ctx.Data.OpenMenus.NPCShop {
// Find current NPC position
Expand Down Expand Up @@ -74,6 +75,7 @@ func InteractNPC(npcID npc.ID) error {
hoverStart := time.Now()

for time.Since(hoverStart) < hoverWait {
ctx.RefreshHoverData()
if currentNPC, found := ctx.Data.Monsters.FindOne(npcID, data.MonsterTypeNone); found && currentNPC.IsHovered {
targetNPCID = currentNPC.UnitID
ctx.HID.Click(game.LeftButton, x, y)
Expand Down
5 changes: 3 additions & 2 deletions internal/action/step/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func MoveTo(dest data.Position, options ...MoveOption) error {
switch ctx.Data.PlayerUnit.Mode {
case mode.Walking, mode.WalkingInTown, mode.Running, mode.CastingSkill:
utils.Sleep(100)
ctx.RefreshGameData()
// We need updated mode so only PlayerUnit refresh is required
ctx.RefreshPlayerData()
continue
default:
return
Expand All @@ -71,7 +72,7 @@ func MoveTo(dest data.Position, options ...MoveOption) error {
// Pause the execution if the priority is not the same as the execution priority
ctx.PauseIfNotPriority()
// is needed to prevent bot teleporting in circle when it reached destination (lower end cpu) cost is minimal.
ctx.RefreshGameData()
ctx.RefreshPlayerData()

// Add some delay between clicks to let the character move to destination
walkDuration := utils.RandomDurationMs(600, 1200)
Expand Down
9 changes: 6 additions & 3 deletions internal/action/step/pickup_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func PickupItem(it data.Item, itemPickupAttempt int) error {
// Casting skill/moving return back
for ctx.Data.PlayerUnit.Mode == mode.CastingSkill || ctx.Data.PlayerUnit.Mode == mode.Running || ctx.Data.PlayerUnit.Mode == mode.Walking || ctx.Data.PlayerUnit.Mode == mode.WalkingInTown {
time.Sleep(25 * time.Millisecond)
ctx.RefreshPlayerData()
return ErrCastingMoving
}

Expand Down Expand Up @@ -86,7 +87,7 @@ func PickupItem(it data.Item, itemPickupAttempt int) error {

for {
ctx.PauseIfNotPriority()
ctx.RefreshGameData()
ctx.RefreshHoverData()

// Periodic monster check
if time.Since(lastMonsterCheck) > monsterCheckInterval {
Expand Down Expand Up @@ -121,9 +122,11 @@ func PickupItem(it data.Item, itemPickupAttempt int) error {
// Move cursor directly to target position
ctx.HID.MovePointer(cursorX, cursorY)
time.Sleep(spiralDelay)


ctx.RefreshHoverData()
// Click on item if mouse is hovering over
if currentItem.UnitID == ctx.GameReader.GameReader.GetData().HoverData.UnitID {

if currentItem.UnitID == ctx.Data.HoverData.UnitID {
ctx.HID.Click(game.LeftButton, cursorX, cursorY)
time.Sleep(clickDelay)

Expand Down
16 changes: 14 additions & 2 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,22 @@ func getGoroutineID() uint64 {

return id
}

// Full Data Update
func (ctx *Context) RefreshGameData() {
*ctx.Data = ctx.GameReader.GetData()
}
// Partial updates
func (ctx *Context) RefreshPlayerData() {
rawPlayerUnits := ctx.GameReader.GetRawPlayerUnits()
mainPlayerUnit := rawPlayerUnits.GetMainPlayer()
ctx.Data.PlayerUnit = ctx.GameReader.GetPlayerUnit(mainPlayerUnit)
}
func (ctx *Context) RefreshOpenMenus() {
ctx.Data.OpenMenus = ctx.GameReader.OpenMenus()
}
func (ctx *Context) RefreshHoverData() {
ctx.Data.HoverData = ctx.GameReader.HoveredData()
}

func (ctx *Context) Detach() {
mu.Lock()
Expand Down Expand Up @@ -166,7 +178,7 @@ func (s *Status) PauseIfNotPriority() {
func (ctx *Context) WaitForGameToLoad() {
for ctx.Data.OpenMenus.LoadingScreen {
time.Sleep(100 * time.Millisecond)
ctx.RefreshGameData()
ctx.RefreshOpenMenus()
}
// Add a small buffer to ensure everything is fully loaded
time.Sleep(300 * time.Millisecond)
Expand Down
0