跳到主要内容

大肥鱼星露谷自学习采集

收藏0

让 AI 玩星露谷时记住哪儿能翻垃圾桶、哪儿会结果莓子,路过顺手捡走。其他收集品采集原模组有,需自行安装 BlindWhaleFarmer 模组。

让 AI 安装

暮色鼠鼠最新版本 0.1.1更新于 2026-11-13下载 0 次

forage_memory/detour.py

8.8 KB
1# -*- coding: utf-8 -*-
2"""顺路动作:翻垃圾桶、采莓子、捡采集品。
3
4三件事的共同点 —— 都是「路过时多走几步就能白拿」的动作,所以:
5 * 有距离预算(直线 N 格 ≈ 往返 2N 格路)
6 * 有人在场不翻垃圾桶(被村民看见扣 25 好感)
7 * 背包没空位就先别捡(捡了会掉地上,等于白跑)
8"""
9from __future__ import annotations
10
11from . import readmap, spec
12from .memory import Memory
13
14JUNK = spec.JUNK_KEYS
15
16
17class Ranger:
18 def __init__(self, client, memory: Memory, cfg: dict | None = None):
19 self.c = client
20 self.m = memory
21 self.cfg = {
22 "do_trash": True,
23 "do_bush": True,
24 "do_forage": True,
25 "trash_safe_radius": 10, # 这个范围内有村民就不翻
26 "trash_detour_radius": 6, # 路过时:直线 6 格 ≈ 往返 12 格
27 "trash_idle_radius": 12, # 闲逛时放宽
28 "bush_detour_radius": 12,
29 "bush_idle_radius": 16,
30 "forage_radius": 16,
31 "bag_min_free": 1, # 至少留 1 格才动手
32 "budget": 4, # 一次最多跑几个点
33 }
34 self.cfg.update(cfg or {})
35 self.log: list[str] = []
36
37 # -------------------------------------------------------------- 工具
38 def _say(self, msg: str) -> None:
39 self.log.append(msg)
40
41 def _junk(self, st) -> int:
42 """翻出来的纯垃圾直接扔回垃圾桶,不占背包也不占箱子。"""
43 n = 0
44 for it in st.items:
45 if it.keep:
46 continue
47 if any(k in it.name for k in JUNK):
48 r = self.c.trash_item(it.name)
49 if "失败" not in r[:15]:
50 n += 1
51 self._say(f"扔掉垃圾 {it.name} x{it.stack}")
52 return n
53
54 def _can_pick(self, st, what: str) -> bool:
55 if st.bag_free >= int(self.cfg["bag_min_free"]):
56 return True
57 self._say(f"背包只剩 {st.bag_free} 格,先不{what}(捡了会掉地上)")
58 return False
59
60 # -------------------------------------------------------------- 学习
61 def scan(self, st) -> int:
62 """(每次读状态顺手调一次)把视野里的垃圾桶和「会结果的灌木」记进记忆。
63
64 灌木只在莓子季才学:灌木满地都是,但不是每丛都结果,
65 非莓子季记下来只会攒一堆「去了是空」的坐标。
66 """
67 new = 0
68 loc = st.loc
69 if not loc or loc == "?":
70 return 0
71 for p in st.trash:
72 if self.m.note(loc, "trash", p, st.abs_day):
73 new += 1
74 if st.berry_item():
75 for p, ch in st.grid.items():
76 if ch == spec.SYMBOLS["bush"] and self.m.note(loc, "bush", p, st.abs_day):
77 new += 1
78 self.m.visited(loc, st.abs_day)
79 if new:
80 self.m.save()
81 return new
82
83 # -------------------------------------------------------------- 翻垃圾桶
84 def trash(self, st, radius: int | None = None, budget: int | None = None) -> int:
85 if not self.cfg["do_trash"] or not st.pos:
86 return 0
87 radius = int(radius if radius is not None else self.cfg["trash_detour_radius"])
88 budget = int(budget if budget is not None else self.cfg["budget"])
89 loc = st.loc
90
91 cands = self.m.candidates(loc, "trash", st.pos, st.abs_day, radius)
92 # 视野里新出现、还没记下的也一起处理(今天翻过的跳过)
93 done = self.m.done_today(loc, "trash", st.abs_day)
94 have = {p for _d, p in cands}
95 for p in st.trash:
96 if p in done or p in have:
97 continue
98 d = readmap.manhattan(p, st.pos)
99 if d <= radius:
100 cands.append((d, p))
101 if not cands:
102 return 0
103
104 safe = int(self.cfg["trash_safe_radius"])
105 seen = st.villagers_within(safe)
106 if seen:
107 self._say("附近有人(" + "、".join(f"{n}({d}格)" for n, _, d in seen)
108 + f"),不翻垃圾桶")
109 return 0
110
111 got = 0
112 for d, p in sorted(cands)[:budget]:
113 if st.villagers_within(safe):
114 self._say("有人走近,停手")
115 break
116 r = self.c.interact(p)
117 ok = "失败" not in r[:15] and "错误" not in r[:15]
118 self.m.mark(loc, "trash", p, st.abs_day, ok)
119 if not ok:
120 continue
121 got += 1
122 self._say(f"翻垃圾桶({p[0]},{p[1]}) 距{d}格: {r.splitlines()[0][:60] if r.strip() else ''}")
123 nst = readmap.parse(self.c.state())
124 self._junk(nst)
125 if not nst.grid or not nst.pos:
126 break
127 st = nst # 背包变了,后面的判断用新的
128 if got:
129 self.m.save()
130 return got
131
132 # -------------------------------------------------------------- 采莓子
133 def bush(self, st, radius: int | None = None, budget: int | None = None) -> int:
134 if not self.cfg["do_bush"] or not st.pos:
135 return 0
136 if st.berry_item() is None:
137 return 0 # 不是莓子季,灌木是空的
138 if not self._can_pick(st, "采莓子"):
139 return 0
140 radius = int(radius if radius is not None else self.cfg["bush_detour_radius"])
141 budget = int(budget if budget is not None else self.cfg["budget"])
142 loc = st.loc
143
144 cands = self.m.candidates(loc, "bush", st.pos, st.abs_day, radius)
145 done = self.m.done_today(loc, "bush", st.abs_day)
146 have = {p for _d, p in cands}
147 for p, ch in st.grid.items():
148 if ch != spec.SYMBOLS["bush"] or p in done or p in have:
149 continue
150 d = readmap.manhattan(p, st.pos)
151 if d <= radius:
152 cands.append((d, p))
153 if not cands:
154 return 0
155
156 got = 0
157 for d, p in sorted(cands)[:budget]:
158 before = st.berry_count()
159 r = self.c.interact(p)
160 after = readmap.parse(self.c.state())
161 now = after.berry_count()
162 ok = now > before or ("成功" in r and "没有" not in r and "莓" in r)
163 # 只有真采到才记 —— 戳了没反应说明这丛不结果
164 self.m.mark(loc, "bush", p, st.abs_day, ok)
165 if ok:
166 got += 1
167 self._say(f"采莓子({p[0]},{p[1]}) 距{d}格: {r.splitlines()[0][:40] if r.strip() else ''}")
168 st = after
169 if st.bag_free < int(self.cfg["bag_min_free"]):
170 self._say("背包满了,莓子先不采")
171 break
172 if got:
173 self.m.save()
174 return got
175
176 # -------------------------------------------------------------- 捡采集品
177 def forage(self, st, radius: int | None = None, budget: int | None = None) -> int:
178 if not self.cfg["do_forage"] or not st.pos:
179 return 0
180 if not self._can_pick(st, "捡采集品"):
181 return 0
182 radius = int(radius if radius is not None else self.cfg["forage_radius"])
183 budget = int(budget if budget is not None else self.cfg["budget"] * 2)
184
185 todo = [t for t in st.nearby(radius) if t[1] == "forage"]
186 got = 0
187 for _d, _kind, p in todo[:budget]:
188 r = self.c.interact(p)
189 if "成功" in r and "没有" not in r:
190 got += 1
191 self._say(f"捡采集品({p[0]},{p[1]})")
192 st = readmap.parse(self.c.state())
193 if st.bag_free < int(self.cfg["bag_min_free"]):
194 self._say("背包满了,停止采集")
195 break
196 return got
197
198 # -------------------------------------------------------------- 组合
199 def travel(self, location: str) -> str:
200 """去某张图:出发前扫一次(回程路上的点也算),到了再扫一次。
201
202 「往返 12 格」这个口径就是靠出发前那次扫实现的。
203 """
204 self.trash(radius=int(self.cfg["trash_detour_radius"]))
205 r = self.c.travel_to(location)
206 st = readmap.parse(self.c.state())
207 self.scan(st)
208 self.trash(st, radius=int(self.cfg["trash_detour_radius"]))
209 self.bush(st, radius=int(self.cfg["bush_detour_radius"]))
210 return r
211
212 def idle_round(self) -> int:
213 """闲逛时的一轮:范围放宽,捡干净。"""
214 st = readmap.parse(self.c.state())
215 self.scan(st)
216 n = 0
217 n += self.forage(st, radius=int(self.cfg["forage_radius"]))
218 st = readmap.parse(self.c.state())
219 n += self.bush(st, radius=int(self.cfg["bush_idle_radius"]))
220 st = readmap.parse(self.c.state())
221 n += self.trash(st, radius=int(self.cfg["trash_idle_radius"]))
222 return n

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。