Browse Source

fix: share cross-skin realm search regions and replace EXP bottle template

Ghostuel 2 weeks ago
parent
commit
9636b7240d

+ 2 - 3
assets/i18n/zh-CN.json

@@ -162,6 +162,5 @@
   "green_mark_type": "绿标匹配方式",
   "green_mark_type_help": "按坐标点击指定绿标,或按式神名OCR 匹配后点击",
   "green_mark_name": "绿标式神名称",
-  "green_mark_name_help": "仅当绿标按式神名匹配时生效,填写目标式神名",
-  "costume_realm_yuehai_youxing": "月海游星"
-}
+  "green_mark_name_help": "仅当绿标按式神名匹配时生效,填写目标式神名"
+}

+ 0 - 4
tasks/Component/Costume/config.py

@@ -28,7 +28,6 @@ class MainType(str, Enum):
 # 结界皮肤
 class RealmType(str, Enum):
     COSTUME_REALM_DEFAULT = 'costume_realm_default'  # 妖扇结界
-    COSTUME_REALM_YUEHAI_YOUXING = 'costume_realm_yuehai_youxing'  # 月海游星
 
 # 鲤鱼旗皮肤
 class CarpBannerType(str, Enum):
@@ -100,9 +99,6 @@ class CostumeConfig(BaseModel):
     @field_validator('costume_realm_type', mode='before')
     @classmethod
     def validate_realm(cls, v):
-        # Preserve selections saved by the earlier local numbered adaptation.
-        if v == 'costume_realm_2':
-            return RealmType.COSTUME_REALM_YUEHAI_YOUXING
         if v and v not in RealmType._value2member_map_:
             return RealmType.COSTUME_REALM_DEFAULT
         return v

+ 6 - 8
tasks/Component/Costume/costume_base.py

@@ -16,7 +16,6 @@ from tasks.Component.Costume.assets import CostumeAssets
 from tasks.Component.CostumeBattle.assets import CostumeBattleAssets
 from tasks.Component.CostumeShikigami.assets import CostumeShikigamiAssets
 from tasks.Component.CostumeCarpBanner.assets import CostumeCarpBannerAssets
-from tasks.Component.CostumeRealm.realm import apply_realm
 from tasks.Component.CostumeBattleScene.assets import CostumeBattleSceneAssets
 
 # 庭院皮肤
@@ -108,7 +107,6 @@ class CostumeBase:
         current_task = self.get_task_name()
         logger.info(f'Current task: {current_task}')
         self.check_costume_main(config.costume_main_type)
-        self.check_costume_realm(config.costume_realm_type)
         self.check_costume_carpbanner(config.costume_carpbanner_type)
         self.check_costume_battle(config.costume_battle_type)
         self.check_costume_battle_scene(config.costume_battle_scene_type)
@@ -122,11 +120,16 @@ class CostumeBase:
             return
         # setattr(self, asset_before, asset_after)
         asset_before_object: RuleImage = getattr(self, asset_before)
-        asset_before_object.roi_front = asset_after.roi_front
+        asset_before_object.roi_front = list(asset_after.roi_front)
         if rp_roi_back:
             asset_before_object.roi_back = asset_after.roi_back
         asset_before_object.threshold = asset_after.threshold
         asset_before_object.file = asset_after.file
+        asset_before_object.method = asset_after.method
+        # A cached dispatch flag must not retain the previous skin's method.
+        for name in ('is_template_match', 'is_masked_template_match',
+                     'is_multi_scale_template_match', 'is_sift_flann'):
+            asset_before_object.__dict__.pop(name, None)
         # https://github.com/runhey/OnmyojiAutoScript/issues/1972
         asset_before_object.refresh()
 
@@ -145,11 +148,6 @@ class CostumeBase:
                 assert_value: RuleImage = getattr(costume_assets, value)
                 self.replace_img(key, assert_value)
 
-    def check_costume_realm(self, realm_type: RealmType):
-        apply_realm(realm_type)
-        if realm_type != RealmType.COSTUME_REALM_DEFAULT:
-            logger.info(f'Switch realm skin {realm_type} ({I18n.trans_zh_cn(realm_type)})')
-
     def check_costume_carpbanner(self, carpbanner_type: CarpBannerType):
         if carpbanner_type == CarpBannerType.COSTUME_CARPBANNER_DEFAULT:
             return

+ 1 - 1
tasks/Component/Costume/costume_test_realm.py

@@ -62,7 +62,7 @@ if __name__ == '__main__':
     c = Config('oas1')
     d = Device(c)
     t = ScriptTask(c, d)
-    t.set_costume(RealmType.COSTUME_REALM_YUEHAI_YOUXING)
+    t.set_costume(RealmType.COSTUME_REALM_DEFAULT)
     t.run()
 
 

+ 9 - 9
tasks/Component/CostumeCarpBanner/assets.py

@@ -12,28 +12,28 @@ class CostumeCarpBannerAssets:
 
 	# Image Rule Assets
 	# 育成 
-	I_SHI_GROWN_1 = RuleImage(roi_front=(572,272,21,33), roi_back=(530,220,202,267), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb1/cb1_shi_grown_1.png")
+	I_SHI_GROWN_1 = RuleImage(roi_front=(572,272,21,33), roi_back=(560,250,130,220), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb1/cb1_shi_grown_1.png")
 	# 结界卡 
-	I_SHI_CARD_1 = RuleImage(roi_front=(911,262,20,33), roi_back=(823,210,252,297), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb1/cb1_shi_card_1.png")
+	I_SHI_CARD_1 = RuleImage(roi_front=(911,262,20,33), roi_back=(760,250,220,220), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb1/cb1_shi_card_1.png")
 	# 防守 
-	I_SHI_DEFENSE_1 = RuleImage(roi_front=(216,258,22,33), roi_back=(178,209,261,274), threshold=0.6, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb1/cb1_shi_defense_1.png")
+	I_SHI_DEFENSE_1 = RuleImage(roi_front=(216,258,22,33), roi_back=(260,250,210,210), threshold=0.6, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb1/cb1_shi_defense_1.png")
 
 
 	# Image Rule Assets
 	# 育成 
-	I_SHI_GROWN_2 = RuleImage(roi_front=(585,336,21,38), roi_back=(519,212,220,281), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb2/cb2_shi_grown_2.png")
+	I_SHI_GROWN_2 = RuleImage(roi_front=(585,336,21,38), roi_back=(560,250,130,220), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb2/cb2_shi_grown_2.png")
 	# 结界卡 
-	I_SHI_CARD_2 = RuleImage(roi_front=(871,341,20,36), roi_back=(823,210,252,297), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb2/cb2_shi_card_2.png")
+	I_SHI_CARD_2 = RuleImage(roi_front=(871,341,20,36), roi_back=(760,250,220,220), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb2/cb2_shi_card_2.png")
 	# 防守 
-	I_SHI_DEFENSE_2 = RuleImage(roi_front=(287,343,19,38), roi_back=(178,209,261,274), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb2/cb2_shi_defense_2.png")
+	I_SHI_DEFENSE_2 = RuleImage(roi_front=(287,343,19,38), roi_back=(260,250,210,210), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb2/cb2_shi_defense_2.png")
 
 
 	# Image Rule Assets
 	# 育成 
-	I_SHI_GROWN_3 = RuleImage(roi_front=(600,277,19,34), roi_back=(519,212,220,281), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb3/cb3_shi_grown_3.png")
+	I_SHI_GROWN_3 = RuleImage(roi_front=(600,277,19,34), roi_back=(560,250,130,220), threshold=0.5, method="Multi-scale template matching", file="./tasks/Component/CostumeCarpBanner/cb3/cb3_shi_grown_3.png")
 	# 结界卡 
-	I_SHI_CARD_3 = RuleImage(roi_front=(899,281,21,40), roi_back=(823,210,252,297), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb3/cb3_shi_card_3.png")
+	I_SHI_CARD_3 = RuleImage(roi_front=(899,281,21,40), roi_back=(760,250,220,220), threshold=0.5, method="Multi-scale template matching", file="./tasks/Component/CostumeCarpBanner/cb3/cb3_shi_card_3.png")
 	# 防守 
-	I_SHI_DEFENSE_3 = RuleImage(roi_front=(298,279,19,38), roi_back=(178,209,261,274), threshold=0.5, method="Template matching", file="./tasks/Component/CostumeCarpBanner/cb3/cb3_shi_defense_3.png")
+	I_SHI_DEFENSE_3 = RuleImage(roi_front=(298,279,19,38), roi_back=(260,250,210,210), threshold=0.5, method="Multi-scale template matching", file="./tasks/Component/CostumeCarpBanner/cb3/cb3_shi_defense_3.png")
 
 

+ 3 - 3
tasks/Component/CostumeCarpBanner/cb1/image.json

@@ -3,7 +3,7 @@
     "itemName": "shi_grown_1",
     "imageName": "cb1_shi_grown_1.png",
     "roiFront": "572,272,21,33",
-    "roiBack": "530,220,202,267",
+    "roiBack": "560,250,130,220",
     "method": "Template matching",
     "threshold": 0.5,
     "description": "育成"
@@ -12,7 +12,7 @@
     "itemName": "shi_card_1",
     "imageName": "cb1_shi_card_1.png",
     "roiFront": "911,262,20,33",
-    "roiBack": "823,210,252,297",
+    "roiBack": "760,250,220,220",
     "method": "Template matching",
     "threshold": 0.5,
     "description": "结界卡"
@@ -21,7 +21,7 @@
     "itemName": "shi_defense_1",
     "imageName": "cb1_shi_defense_1.png",
     "roiFront": "216,258,22,33",
-    "roiBack": "178,209,261,274",
+    "roiBack": "260,250,210,210",
     "method": "Template matching",
     "threshold": 0.6,
     "description": "防守"

+ 3 - 3
tasks/Component/CostumeCarpBanner/cb2/image.json

@@ -3,7 +3,7 @@
     "itemName": "shi_grown_2",
     "imageName": "cb2_shi_grown_2.png",
     "roiFront": "585,336,21,38",
-    "roiBack": "519,212,220,281",
+    "roiBack": "560,250,130,220",
     "method": "Template matching",
     "threshold": 0.5,
     "description": "育成"
@@ -12,7 +12,7 @@
     "itemName": "shi_card_2",
     "imageName": "cb2_shi_card_2.png",
     "roiFront": "871,341,20,36",
-    "roiBack": "823,210,252,297",
+    "roiBack": "760,250,220,220",
     "method": "Template matching",
     "threshold": 0.5,
     "description": "结界卡"
@@ -21,7 +21,7 @@
     "itemName": "shi_defense_2",
     "imageName": "cb2_shi_defense_2.png",
     "roiFront": "287,343,19,38",
-    "roiBack": "178,209,261,274",
+    "roiBack": "260,250,210,210",
     "method": "Template matching",
     "threshold": 0.5,
     "description": "防守"

+ 7 - 7
tasks/Component/CostumeCarpBanner/cb3/image.json

@@ -3,8 +3,8 @@
     "itemName": "shi_grown_3",
     "imageName": "cb3_shi_grown_3.png",
     "roiFront": "600,277,19,34",
-    "roiBack": "519,212,220,281",
-    "method": "Template matching",
+    "roiBack": "560,250,130,220",
+    "method": "Multi-scale template matching",
     "threshold": 0.5,
     "description": "育成"
   },
@@ -12,8 +12,8 @@
     "itemName": "shi_card_3",
     "imageName": "cb3_shi_card_3.png",
     "roiFront": "899,281,21,40",
-    "roiBack": "823,210,252,297",
-    "method": "Template matching",
+    "roiBack": "760,250,220,220",
+    "method": "Multi-scale template matching",
     "threshold": 0.5,
     "description": "结界卡"
   },
@@ -21,9 +21,9 @@
     "itemName": "shi_defense_3",
     "imageName": "cb3_shi_defense_3.png",
     "roiFront": "298,279,19,38",
-    "roiBack": "178,209,261,274",
-    "method": "Template matching",
+    "roiBack": "260,250,210,210",
+    "method": "Multi-scale template matching",
     "threshold": 0.5,
     "description": "防守"
   }
-]
+]

+ 12 - 0
tasks/Component/CostumeRealm/assets.py

@@ -0,0 +1,12 @@
+from module.atom.image import RuleImage
+from module.atom.click import RuleClick
+from module.atom.long_click import RuleLongClick
+from module.atom.swipe import RuleSwipe
+from module.atom.ocr import RuleOcr
+from module.atom.list import RuleList
+
+# This file was automatically generated by ./dev_tools/assets_extract.py.
+# Don't modify it manually.
+class CostumeRealmAssets: 
+	pass
+

+ 0 - 32
tasks/Component/CostumeRealm/realm.py

@@ -1,32 +0,0 @@
-"""结界皮肤只覆盖共用素材的搜索区域;在鲤鱼旗配置之前应用。"""
-from copy import deepcopy
-
-from tasks.Component.Costume.config import RealmType
-from tasks.KekkaiUtilize.assets import KekkaiUtilizeAssets
-
-
-_REALM_NAMES = ('I_REALM_SHIN', 'I_CARD_EXP', 'I_BOX_AP', 'I_BOX_EXP')
-# Capture pristine shared rules before a task applies any realm/carp-banner skin.
-_DEFAULT_RULES = {name: deepcopy(getattr(KekkaiUtilizeAssets, name)) for name in _REALM_NAMES}
-# Only the search region varies. Template, method, threshold and template size
-# remain the shared defaults; matching updates the actual click position.
-_REALM_REGIONS = {
-    RealmType.COSTUME_REALM_DEFAULT: {},
-    RealmType.COSTUME_REALM_YUEHAI_YOUXING: {
-        'I_REALM_SHIN': (175, 390, 115, 110),
-        'I_CARD_EXP': (865, 205, 135, 105),
-        'I_BOX_AP': (975, 415, 90, 105),
-        'I_BOX_EXP': (1035, 420, 85, 110),
-    },
-}
-
-
-def apply_realm(realm_type: RealmType) -> None:
-    regions = _REALM_REGIONS[realm_type]  # Validate before mutating shared rules.
-    for name, source in _DEFAULT_RULES.items():
-        # Preserve references and reset stale images, features and match state.
-        target = getattr(KekkaiUtilizeAssets, name)
-        target.__dict__.clear()
-        target.__dict__.update(deepcopy(source.__dict__))
-        if name in regions:
-            target.roi_back = regions[name]

+ 9 - 9
tasks/KekkaiUtilize/assets.py

@@ -38,27 +38,27 @@ class KekkaiUtilizeAssets:
 
 	# Image Rule Assets
 	# 育成 
-	I_SHI_GROWN = RuleImage(roi_front=(595,291,35,38), roi_back=(530,220,202,267), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_grown.png")
+	I_SHI_GROWN = RuleImage(roi_front=(595,291,35,38), roi_back=(560,250,130,220), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_grown.png")
 	# 结界卡 
-	I_SHI_CARD = RuleImage(roi_front=(891,300,31,55), roi_back=(823,210,252,297), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_card.png")
+	I_SHI_CARD = RuleImage(roi_front=(891,300,31,55), roi_back=(760,250,220,220), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_card.png")
 	# description 
-	I_SHI_DEFENSE = RuleImage(roi_front=(303,308,34,52), roi_back=(178,209,261,274), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_defense.png")
+	I_SHI_DEFENSE = RuleImage(roi_front=(303,308,34,52), roi_back=(260,250,210,210), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_defense.png")
 	# 收取经验(没有满的图) 
-	I_CARD_EXP = RuleImage(roi_front=(889,151,57,54), roi_back=(829,33,231,257), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_card_exp.png")
+	I_CARD_EXP = RuleImage(roi_front=(889,151,57,54), roi_back=(750,90,250,230), threshold=0.8, method="Multi-scale template matching", file="./tasks/KekkaiUtilize/realm/realm_card_exp.png")
 	# 收取体力(没有满) 
-	I_BOX_AP = RuleImage(roi_front=(815,435,57,51), roi_back=(744,336,395,216), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_ap.png")
+	I_BOX_AP = RuleImage(roi_front=(815,435,57,51), roi_back=(790,285,295,240), threshold=0.7, method="Multi-scale template matching", file="./tasks/KekkaiUtilize/realm/realm_box_ap.png")
 	# 收取盒子的经验(没有满) 
-	I_BOX_EXP = RuleImage(roi_front=(893,440,42,45), roi_back=(792,334,359,232), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp.png")
+	I_BOX_EXP = RuleImage(roi_front=(893,440,41,64), roi_back=(875,285,255,240), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp.png")
 	# 结界皮肤 
-	I_REALM_SHIN = RuleImage(roi_front=(175,460,54,58), roi_back=(22,359,393,328), threshold=0.9, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_realm_shin.png")
+	I_REALM_SHIN = RuleImage(roi_front=(175,460,54,58), roi_back=(140,270,270,285), threshold=0.9, method="Multi-scale template matching", file="./tasks/KekkaiUtilize/realm/realm_realm_shin.png")
 	# 寄养别人的经验 
-	I_UTILIZE_EXP = RuleImage(roi_front=(583,144,58,43), roi_back=(513,43,280,309), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_utilize_exp.png")
+	I_UTILIZE_EXP = RuleImage(roi_front=(583,144,58,43), roi_back=(500,65,220,280), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_utilize_exp.png")
 	# 取出 
 	I_AP_EXTRACT = RuleImage(roi_front=(592,501,100,75), roi_back=(592,501,100,75), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_ap_extract.png")
 	# 提取 
 	I_EXP_EXTRACT = RuleImage(roi_front=(592,448,100,73), roi_back=(592,448,100,73), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_exp_extract.png")
 	# 盒子经验满 
-	I_BOX_EXP_MAX = RuleImage(roi_front=(889,430,61,64), roi_back=(796,326,405,330), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp_max.png")
+	I_BOX_EXP_MAX = RuleImage(roi_front=(889,430,61,64), roi_back=(875,285,255,240), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp_max.png")
 	# 种树活动关闭标识 
 	I_PLANT_TREE_CLOSE = RuleImage(roi_front=(777,91,36,34), roi_back=(711,52,169,125), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_plant_tree_close.png")
 

+ 14 - 14
tasks/KekkaiUtilize/realm/image.json

@@ -3,7 +3,7 @@
     "itemName": "shi_grown",
     "imageName": "realm_shi_grown.png",
     "roiFront": "595,291,35,38",
-    "roiBack": "530,220,202,267",
+    "roiBack": "560,250,130,220",
     "method": "Template matching",
     "threshold": 0.6,
     "description": "育成"
@@ -12,7 +12,7 @@
     "itemName": "shi_card",
     "imageName": "realm_shi_card.png",
     "roiFront": "891,300,31,55",
-    "roiBack": "823,210,252,297",
+    "roiBack": "760,250,220,220",
     "method": "Template matching",
     "threshold": 0.6,
     "description": "结界卡"
@@ -21,7 +21,7 @@
     "itemName": "shi_defense",
     "imageName": "realm_shi_defense.png",
     "roiFront": "303,308,34,52",
-    "roiBack": "178,209,261,274",
+    "roiBack": "260,250,210,210",
     "method": "Template matching",
     "threshold": 0.7,
     "description": "description"
@@ -30,8 +30,8 @@
     "itemName": "card_exp",
     "imageName": "realm_card_exp.png",
     "roiFront": "889,151,57,54",
-    "roiBack": "829,33,231,257",
-    "method": "Template matching",
+    "roiBack": "750,90,250,230",
+    "method": "Multi-scale template matching",
     "threshold": 0.8,
     "description": "收取经验(没有满的图)"
   },
@@ -39,16 +39,16 @@
     "itemName": "box_ap",
     "imageName": "realm_box_ap.png",
     "roiFront": "815,435,57,51",
-    "roiBack": "744,336,395,216",
-    "method": "Template matching",
+    "roiBack": "790,285,295,240",
+    "method": "Multi-scale template matching",
     "threshold": 0.7,
     "description": "收取体力(没有满)"
   },
   {
     "itemName": "box_exp",
     "imageName": "realm_box_exp.png",
-    "roiFront": "893,440,42,45",
-    "roiBack": "792,334,359,232",
+    "roiFront": "893,440,41,64",
+    "roiBack": "875,285,255,240",
     "method": "Template matching",
     "threshold": 0.7,
     "description": "收取盒子的经验(没有满)"
@@ -57,8 +57,8 @@
     "itemName": "realm_shin",
     "imageName": "realm_realm_shin.png",
     "roiFront": "175,460,54,58",
-    "roiBack": "22,359,393,328",
-    "method": "Template matching",
+    "roiBack": "140,270,270,285",
+    "method": "Multi-scale template matching",
     "threshold": 0.9,
     "description": "结界皮肤"
   },
@@ -66,7 +66,7 @@
     "itemName": "utilize_exp",
     "imageName": "realm_utilize_exp.png",
     "roiFront": "583,144,58,43",
-    "roiBack": "513,43,280,309",
+    "roiBack": "500,65,220,280",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "寄养别人的经验"
@@ -93,7 +93,7 @@
     "itemName": "box_exp_max",
     "imageName": "realm_box_exp_max.png",
     "roiFront": "889,430,61,64",
-    "roiBack": "796,326,405,330",
+    "roiBack": "875,285,255,240",
     "method": "Template matching",
     "threshold": 0.7,
     "description": "盒子经验满"
@@ -107,4 +107,4 @@
     "threshold": 0.8,
     "description": "种树活动关闭标识"
   }
-]
+]

BIN
tasks/KekkaiUtilize/realm/realm_box_exp.png


+ 0 - 120
tests/tasks/Component/CostumeRealm/test_realm.py

@@ -1,120 +0,0 @@
-"""Offline realm-skin regression tests (no device connection)."""
-import unittest
-from copy import deepcopy
-
-from tasks.Component.Costume.config import CostumeConfig, RealmType, CarpBannerType
-from tasks.Component.Costume.costume_base import CostumeBase
-from tasks.Component.CostumeRealm.realm import apply_realm, _REALM_NAMES, _REALM_REGIONS
-from tasks.KekkaiUtilize.assets import KekkaiUtilizeAssets
-
-
-class RealmTests(unittest.TestCase):
-    def setUp(self):
-        apply_realm(RealmType.COSTUME_REALM_DEFAULT)
-
-    def tearDown(self):
-        apply_realm(RealmType.COSTUME_REALM_DEFAULT)
-
-    def test_schema_and_default(self):
-        self.assertEqual(CostumeConfig().costume_realm_type, RealmType.COSTUME_REALM_DEFAULT)
-        config = CostumeConfig(costume_realm_type='costume_realm_yuehai_youxing')
-        self.assertEqual(config.costume_realm_type, RealmType.COSTUME_REALM_YUEHAI_YOUXING)
-        self.assertIn('costume_realm_yuehai_youxing',
-                      CostumeConfig.model_json_schema()['$defs']['RealmType']['enum'])
-
-    def test_manual_tool_uses_existing_realm_members(self):
-        # Parse only: importing the tool could initialize a real device.
-        import ast
-        from pathlib import Path
-        tool = Path(__file__).resolve().parents[4] / 'tasks/Component/Costume/costume_test_realm.py'
-        tree = ast.parse(tool.read_text(encoding='utf-8'))
-        members = [node.attr for node in ast.walk(tree)
-                   if isinstance(node, ast.Attribute)
-                   and isinstance(node.value, ast.Name)
-                   and node.value.id == 'RealmType']
-        self.assertIn('COSTUME_REALM_YUEHAI_YOUXING', members)
-        for member in members:
-            with self.subTest(member=member):
-                self.assertIn(member, RealmType.__members__)
-
-    def test_previous_local_selection_is_preserved(self):
-        config = CostumeConfig(costume_realm_type='costume_realm_2')
-        self.assertEqual(config.model_dump(mode='json')['costume_realm_type'],
-                         'costume_realm_yuehai_youxing')
-
-    def test_identity_cache_reset_and_default_restore(self):
-        refs = {name: getattr(KekkaiUtilizeAssets, name) for name in _REALM_NAMES}
-        defaults = {name: (rule.file, tuple(rule.roi_front), rule.roi_back) for name, rule in refs.items()}
-        methods = {name: (rule.method, rule.threshold) for name, rule in refs.items()}
-        for rule in refs.values():
-            _ = rule.image
-            _ = rule.name
-        CostumeBase().check_costume_realm(RealmType.COSTUME_REALM_YUEHAI_YOUXING)
-        for name, rule in refs.items():
-            self.assertIs(rule, getattr(KekkaiUtilizeAssets, name))
-            self.assertEqual(rule.file, defaults[name][0])
-            self.assertEqual(rule.method, methods[name][0])
-            self.assertEqual(rule.threshold, methods[name][1])
-            self.assertEqual(tuple(rule.roi_front), defaults[name][1])
-            self.assertEqual(rule.roi_back, _REALM_REGIONS[RealmType.COSTUME_REALM_YUEHAI_YOUXING][name])
-            self.assertIsNone(rule._image)
-            self.assertNotIn('name', rule.__dict__)
-            self.assertEqual(rule.image.shape[:2], (rule.roi_front[3], rule.roi_front[2]))
-        apply_realm(RealmType.COSTUME_REALM_DEFAULT)
-        for name, rule in refs.items():
-            self.assertEqual((rule.file, tuple(rule.roi_front), rule.roi_back), defaults[name])
-            self.assertIsNone(rule._image)
-
-    def test_only_search_region_changes(self):
-        defaults = {name: deepcopy(getattr(KekkaiUtilizeAssets, name).__dict__)
-                    for name in _REALM_NAMES}
-        apply_realm(RealmType.COSTUME_REALM_YUEHAI_YOUXING)
-        for name, before in defaults.items():
-            expected = deepcopy(before)
-            expected['roi_back'] = _REALM_REGIONS[RealmType.COSTUME_REALM_YUEHAI_YOUXING][name]
-            self.assertEqual(getattr(KekkaiUtilizeAssets, name).__dict__, expected)
-
-    def test_no_duplicate_operational_templates(self):
-        from pathlib import Path
-        folder = Path(__file__).resolve().parents[4] / 'tasks/Component/CostumeRealm'
-        self.assertFalse((folder / 'assets.py').exists())
-        self.assertFalse((folder / 'realm2').exists())
-
-    def test_uncollected_icons_dialogs_and_carp_flags_untouched(self):
-        names = ('I_UTILIZE_EXP', 'I_BOX_EXP_MAX', 'I_AP_EXTRACT', 'I_EXP_EXTRACT',
-                 'I_SHI_GROWN', 'I_SHI_CARD', 'I_SHI_DEFENSE')
-        states = {name: deepcopy(getattr(KekkaiUtilizeAssets, name).__dict__) for name in names}
-        apply_realm(RealmType.COSTUME_REALM_YUEHAI_YOUXING)
-        for name in names:
-            self.assertEqual(getattr(KekkaiUtilizeAssets, name).__dict__, states[name])
-
-    def test_reapply_and_invalid_selection(self):
-        apply_realm(RealmType.COSTUME_REALM_YUEHAI_YOUXING)
-        rule = KekkaiUtilizeAssets.I_CARD_EXP
-        original = tuple(rule.roi_front)
-        rule.roi_front[0] = 0
-        apply_realm(RealmType.COSTUME_REALM_YUEHAI_YOUXING)
-        self.assertEqual(tuple(rule.roi_front), original)
-        with self.assertRaises(KeyError):
-            apply_realm('invalid')
-        self.assertEqual(tuple(rule.roi_front), original)
-
-    def test_realm_applied_before_carpbanner(self):
-        calls = []
-        class Probe(CostumeBase):
-            def get_task_name(self): return 'OfflineRealmTest'
-            def check_costume_main(self, value): pass
-            def check_costume_realm(self, value): calls.append(('realm', value))
-            def check_costume_carpbanner(self, value): calls.append(('carp', value))
-            def check_costume_battle(self, value): pass
-            def check_costume_battle_scene(self, value): pass
-            def check_costume_shikigami(self, value): pass
-            def check_costume_theme(self, value): pass
-        Probe().check_costume(CostumeConfig(costume_realm_type=RealmType.COSTUME_REALM_YUEHAI_YOUXING,
-                                           costume_carpbanner_type=CarpBannerType.COSTUME_CARPBANNER_3))
-        self.assertEqual(calls, [('realm', RealmType.COSTUME_REALM_YUEHAI_YOUXING),
-                                 ('carp', CarpBannerType.COSTUME_CARPBANNER_3)])
-
-
-if __name__ == '__main__':
-    unittest.main()

BIN
tests/tasks/KekkaiUtilize/fixtures/collected_bottle_animals.png


BIN
tests/tasks/KekkaiUtilize/fixtures/distant_realm_elements.png


+ 220 - 0
tests/tasks/KekkaiUtilize/test_realm_multiscale.py

@@ -0,0 +1,220 @@
+"""Offline regression tests for shared realm assets and distant carp banners."""
+from copy import deepcopy
+import json
+from pathlib import Path
+import unittest
+from unittest.mock import patch
+
+import cv2
+import numpy as np
+
+from module.atom.image import RuleImage, set_roi_match_padding
+from tasks.Component.Costume.config import CarpBannerType
+from tasks.Component.Costume.costume_base import CostumeBase
+from tasks.Component.CostumeCarpBanner.assets import CostumeCarpBannerAssets
+from tasks.KekkaiUtilize.assets import KekkaiUtilizeAssets
+
+ROOT = Path(__file__).resolve().parents[3]
+TARGETS = {
+    'I_SHI_GROWN': (599, 293, 45, 65),
+    'I_SHI_CARD': (777, 288, 50, 72),
+    'I_SHI_DEFENSE': (410, 288, 45, 80),
+    'I_REALM_SHIN': (308, 300, 85, 85),
+    'I_CARD_EXP': (770, 158, 70, 68),
+    'I_BOX_AP': (880, 312, 65, 65),
+    'I_BOX_EXP': (937, 312, 52, 70),
+}
+ORIGINAL_REGIONS = {
+    'I_SHI_GROWN': (519, 212, 220, 281),
+    'I_SHI_CARD': (823, 210, 252, 297),
+    'I_SHI_DEFENSE': (178, 209, 261, 274),
+    'I_REALM_SHIN': (22, 359, 393, 328),
+    'I_CARD_EXP': (829, 33, 231, 257),
+    'I_BOX_AP': (744, 336, 395, 216),
+    'I_BOX_EXP': (792, 334, 359, 232),
+}
+
+
+class Probe(CostumeBase, KekkaiUtilizeAssets):
+    """Only rule replacement is exercised; no Config or Device is created."""
+    pass
+
+
+def screenshot():
+    fixture = Path(__file__).parent / 'fixtures/distant_realm_elements.png'
+    crop = cv2.cvtColor(cv2.imdecode(np.fromfile(fixture, dtype=np.uint8), cv2.IMREAD_COLOR),
+                       cv2.COLOR_BGR2RGB)
+    image = np.zeros((720, 1280, 3), dtype=np.uint8)
+    image[150:460, 300:1000] = crop
+    return image
+
+
+def configured_rules():
+    probe = Probe()
+    for name in TARGETS:
+        setattr(probe, name, deepcopy(getattr(KekkaiUtilizeAssets, name)))
+    with patch('tasks.Component.Costume.costume_base.I18n.trans_zh_cn', return_value='carp'):
+        probe.check_costume_carpbanner(CarpBannerType.COSTUME_CARPBANNER_3)
+    return {name: getattr(probe, name) for name in TARGETS}
+
+
+class RealmMultiScaleTests(unittest.TestCase):
+    def tearDown(self):
+        set_roi_match_padding(0)
+
+    def test_all_seven_visible_elements_match_at_correct_locations(self):
+        for padding in (0, 2):
+            set_roi_match_padding(padding)
+            for name, rule in configured_rules().items():
+                with self.subTest(padding=padding, name=name):
+                    self.assertTrue(rule.match(screenshot(), threshold=0.6 if name == 'I_BOX_EXP' else None))
+                    x, y, w, h = rule.roi_front
+                    ex, ey, ew, eh = TARGETS[name]
+                    self.assertTrue(ex <= x + w / 2 < ex + ew)
+                    self.assertTrue(ey <= y + h / 2 < ey + eh)
+                    self.assertEqual(rule.last_scale, None if name == 'I_BOX_EXP' else
+                                     (0.6 if name.startswith('I_SHI_') else 1.0))
+
+    def test_missing_target_is_not_accepted(self):
+        for padding in (0, 2):
+            set_roi_match_padding(padding)
+            for name, rule in configured_rules().items():
+                with self.subTest(padding=padding, name=name):
+                    image = screenshot()
+                    x, y, w, h = TARGETS[name]
+                    image[y-5:y+h+5, x-5:x+w+5] = 0
+                    self.assertFalse(rule.match(image, threshold=0.6 if name == 'I_BOX_EXP' else None), (name, rule.last_score, rule.roi_front))
+
+    def test_empty_screen_has_no_matches(self):
+        for name, rule in configured_rules().items():
+            with self.subTest(name=name):
+                self.assertFalse(rule.match(np.zeros((720, 1280, 3), dtype=np.uint8)))
+
+    def test_cross_skin_search_regions(self):
+        expected = {
+            'I_SHI_DEFENSE': (260, 250, 210, 210),
+            'I_SHI_GROWN': (560, 250, 130, 220),
+            'I_SHI_CARD': (760, 250, 220, 220),
+            'I_REALM_SHIN': (140, 270, 270, 285),
+            'I_CARD_EXP': (750, 90, 250, 230),
+            'I_BOX_AP': (790, 285, 295, 240),
+            'I_BOX_EXP': (875, 285, 255, 240),
+            'I_BOX_EXP_MAX': (875, 285, 255, 240),
+            'I_UTILIZE_EXP': (500, 65, 220, 280),
+        }
+        for name, roi in expected.items():
+            self.assertEqual(tuple(getattr(KekkaiUtilizeAssets, name).roi_back), roi)
+            if name.startswith('I_SHI_'):
+                for skin in range(1, 4):
+                    self.assertEqual(tuple(getattr(CostumeCarpBannerAssets,
+                                                  name + '_' + str(skin)).roi_back), roi)
+
+    def test_collected_bottle_does_not_match_white_animals_at_task_threshold(self):
+        fixture = Path(__file__).parent / 'fixtures/collected_bottle_animals.png'
+        crop = cv2.cvtColor(cv2.imdecode(np.fromfile(fixture, dtype=np.uint8),
+                                      cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
+        image = np.zeros((720, 1280, 3), dtype=np.uint8)
+        image[280:640, 790:1150] = crop
+        for padding in (0, 2):
+            set_roi_match_padding(padding)
+            for name in ('I_BOX_EXP', 'I_BOX_EXP_MAX'):
+                for roi in ((875, 285, 255, 240), (790, 480, 350, 150)):
+                    rule = deepcopy(getattr(KekkaiUtilizeAssets, name))
+                    rule.roi_back = roi
+                    self.assertFalse(rule.match(image, threshold=0.6),
+                                     (name, roi, rule.last_score))
+
+    def test_bottle_click_dimensions_match_replacement_template(self):
+        rule = deepcopy(KekkaiUtilizeAssets.I_BOX_EXP)
+        height, width = rule.image.shape[:2]
+        self.assertEqual(tuple(rule.roi_front[2:]), (width, height))
+        self.assertTrue(rule.match(screenshot(), threshold=0.6))
+
+    def test_runtime_rules_keep_original_templates_and_thresholds(self):
+        defaults = {'I_REALM_SHIN': 0.9, 'I_CARD_EXP': 0.8,
+                    'I_BOX_AP': 0.7, 'I_BOX_EXP': 0.7}
+        for name, rule in configured_rules().items():
+            with self.subTest(name=name):
+                if name.startswith('I_SHI_'):
+                    source = getattr(CostumeCarpBannerAssets, name + '_3')
+                    self.assertEqual(rule.file, source.file)
+                    self.assertEqual(rule.threshold, 0.5)
+                else:
+                    self.assertEqual(rule.file, getattr(KekkaiUtilizeAssets, name).file)
+                    self.assertEqual(rule.threshold, defaults[name])
+                self.assertEqual(rule.method, 'Template matching' if name == 'I_BOX_EXP'
+                                 else 'Multi-scale template matching')
+
+    def test_original_size_templates_remain_recognizable(self):
+        for name, rule in configured_rules().items():
+            with self.subTest(name=name):
+                image = np.zeros((720, 1280, 3), dtype=np.uint8)
+                x, y, _, _ = rule.roi_back
+                template = rule.image
+                h, w = template.shape[:2]
+                image[y+10:y+10+h, x+10:x+10+w] = template
+                self.assertTrue(rule.match(image))
+                self.assertEqual(rule.last_scale, None if name == 'I_BOX_EXP' else 1.0)
+                self.assertEqual(rule.roi_front, [x+10, y+10, w, h])
+
+    def test_replacement_copies_method_and_clears_cached_dispatch(self):
+        probe = Probe()
+        probe.I_SHI_GROWN = deepcopy(KekkaiUtilizeAssets.I_SHI_GROWN)
+        target = probe.I_SHI_GROWN
+        self.assertTrue(target.is_template_match)
+        self.assertFalse(target.is_multi_scale_template_match)
+        _ = target.image
+        source = CostumeCarpBannerAssets.I_SHI_GROWN_3
+        probe.replace_img('I_SHI_GROWN', source)
+        self.assertIs(probe.I_SHI_GROWN, target)
+        self.assertIsNone(target._image)
+        self.assertFalse(target.is_template_match)
+        self.assertTrue(target.is_multi_scale_template_match)
+        self.assertTrue(target.match(screenshot()))
+        self.assertEqual(source.roi_front, [600, 277, 19, 34])
+        self.assertIsNot(target.roi_front, source.roi_front)
+
+    def test_replacement_can_return_to_original_matching_method(self):
+        probe = Probe()
+        probe.I_SHI_GROWN = deepcopy(KekkaiUtilizeAssets.I_SHI_GROWN)
+        probe.replace_img('I_SHI_GROWN', CostumeCarpBannerAssets.I_SHI_GROWN_3)
+        self.assertTrue(probe.I_SHI_GROWN.is_multi_scale_template_match)
+        probe.replace_img('I_SHI_GROWN', KekkaiUtilizeAssets.I_SHI_GROWN)
+        self.assertTrue(probe.I_SHI_GROWN.is_template_match)
+        self.assertFalse(probe.I_SHI_GROWN.is_multi_scale_template_match)
+
+    def test_replacement_can_preserve_fixed_search_region(self):
+        probe = Probe()
+        probe.I_SHI_GROWN = deepcopy(KekkaiUtilizeAssets.I_SHI_GROWN)
+        original = probe.I_SHI_GROWN.roi_back
+        probe.replace_img('I_SHI_GROWN', CostumeCarpBannerAssets.I_SHI_GROWN_3,
+                          rp_roi_back=False)
+        self.assertEqual(probe.I_SHI_GROWN.roi_back, original)
+
+    def test_manual_realm_tool_references_existing_enum_members(self):
+        import ast
+        from tasks.Component.Costume.config import RealmType
+        tool = ROOT / 'tasks/Component/Costume/costume_test_realm.py'
+        tree = ast.parse(tool.read_text(encoding='utf-8'))
+        members = [node.attr for node in ast.walk(tree)
+                   if isinstance(node, ast.Attribute)
+                   and isinstance(node.value, ast.Name)
+                   and node.value.id == 'RealmType']
+        self.assertTrue(members)
+        for member in members:
+            self.assertIn(member, RealmType.__members__)
+
+    def test_generated_classes_match_json(self):
+        for rel, assets in [('tasks/KekkaiUtilize/realm/image.json', KekkaiUtilizeAssets),
+                            ('tasks/Component/CostumeCarpBanner/cb3/image.json',
+                             CostumeCarpBannerAssets)]:
+            for row in json.loads((ROOT / rel).read_text(encoding='utf-8')):
+                with self.subTest(file=rel, name=row['itemName']):
+                    rule = getattr(assets, 'I_' + row['itemName'].upper())
+                    self.assertEqual(rule.method, row['method'])
+                    self.assertEqual(tuple(rule.roi_back), tuple(map(int, row['roiBack'].split(','))))
+                    self.assertEqual(rule.threshold, row['threshold'])
+
+
+if __name__ == '__main__':
+    unittest.main()