1+ import cv2
2+ import numpy as np
3+
4+ from module .base .button import ButtonGrid
5+ from module .base .decorator import cached_property
6+ from module .base .utils import color_similarity_2d , rgb2luma
7+ from module .combat .assets import GET_ITEMS_1
8+ from module .island .assets import *
9+ from module .ui .navbar import Navbar
10+ from module .ui .ui import UI
11+
12+
13+ class IslandUI (UI ):
14+ def ui_additional (self , get_ship = True ):
15+ return super ().ui_additional (get_ship = False )
16+
17+ def has_white_band (self , threshold = 1000 ):
18+ min_y = 405
19+ max_y = 560
20+ image = self .image_crop ((0 , min_y , 1280 , max_y ), copy = False )
21+ luma = rgb2luma (image )
22+ white_mask = cv2 .inRange (luma , 200 , 255 )
23+ kernel = cv2 .getStructuringElement (cv2 .MORPH_RECT , (31 , 3 ))
24+ white_mask = cv2 .morphologyEx (white_mask , cv2 .MORPH_CLOSE , kernel )
25+ row_white_width = (white_mask > 0 ).sum (axis = 1 )
26+ candidate_rows = row_white_width > threshold
27+ min_continuous_height = 50
28+ count = 0
29+ for ok in candidate_rows :
30+ if ok :
31+ count += 1
32+ if count >= min_continuous_height :
33+ return True
34+ else :
35+ count = 0
36+ return False
37+
38+ def handle_island_get_items (self ):
39+ if self .appear (GET_ITEMS_1 , offset = (20 , 20 ), interval = 3 ):
40+ self .device .click (ISLAND_CLICK_SAFE_AREA )
41+ return True
42+ if self .has_white_band ():
43+ self .device .click (ISLAND_CLICK_SAFE_AREA )
44+ return True
45+ return False
46+
47+ def handle_island_level_up (self ):
48+ if self .appear (ISLAND_LEVEL_UP , offset = (20 , 20 ), interval = 3 ):
49+ self .device .click (ISLAND_CLICK_SAFE_AREA )
50+ return True
51+ return False
52+
53+ def handle_island_additional (self ):
54+ if self .handle_island_get_items ():
55+ return True
56+ if self .handle_island_level_up ():
57+ return True
58+ return False
59+
60+ def is_button_selected (self , button , color = (57 , 189 , 255 ), threshold = 221 , count = 100 ):
61+ """
62+ Detects if the button is surrounded by a blue border,
63+ which indicates that the button is chosen.
64+
65+ Args:
66+ button (Button, tuple): Button instance or area.
67+
68+ Returns:
69+ bool: True if the button is chosen, False otherwise.
70+ """
71+ if isinstance (button , np .ndarray ):
72+ image = button
73+ else :
74+ image = self .image_crop (button , copy = False )
75+ mask = color_similarity_2d (image , color )
76+ cv2 .inRange (mask , threshold , 255 , dst = mask )
77+ mask [2 :- 2 , 2 :- 2 ] = 0
78+ sum_ = cv2 .countNonZero (mask )
79+ return sum_ > count
80+
81+ @cached_property
82+ def _island_manage_side_navbar (self ):
83+ island_manage_side_navbar = ButtonGrid (
84+ origin = (13 , 107 ), delta = (0 , 196 / 3 ),
85+ button_shape = (128 , 43 ), grid_shape = (1 , 3 )
86+ )
87+ return Navbar (grids = island_manage_side_navbar ,
88+ active_color = (57 , 189 , 255 ),
89+ inactive_color = (50 , 52 , 55 ),
90+ active_count = 500 ,
91+ inactive_count = 500 )
92+
93+ def island_manage_side_navbar_ensure (self , upper = 1 , skip_first_screenshot = True ):
94+ """
95+ Args:
96+ upper (int):
97+ 1 for production,
98+ 2 for restaurant,
99+ 3 for collect
100+ bottom (int):
101+ 1 for collect,
102+ 2 for restaurant,
103+ 3 for production
104+ """
105+ return self ._island_manage_side_navbar .set (self , upper = upper , skip_first_screenshot = skip_first_screenshot )
0 commit comments