跳到主要内容

大肥鱼星露谷自学习采集

收藏0

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

让 AI 安装

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

forage_memory/readmap.py

6.1 KB
1# -*- coding: utf-8 -*-
2"""把模组的文字观测解析成结构化数据:位置、日期、背包、字符地图、垃圾桶、村民。
3
4所有正则都来自 spec.py,这里只负责「按章节切段 + 组装」。
5"""
6from __future__ import annotations
7
8import re
9from dataclasses import dataclass, field
10
11from . import spec
12
13SEASON_IDX = {"春": 0, "夏": 1, "秋": 2, "冬": 3}
14
15
16def section(txt: str, title: str) -> str:
17 """取 '## title' 到下一个 '## ' 之间的内容。取不到返回空串。"""
18 m = re.search(rf"^## {re.escape(title)}.*?$(.*?)(?=^## |\Z)", txt, re.M | re.S)
19 return m.group(1).strip() if m else ""
20
21
22def manhattan(a, b) -> int:
23 return abs(a[0] - b[0]) + abs(a[1] - b[1])
24
25
26@dataclass
27class Item:
28 name: str
29 stack: int
30 line: str
31 keep: bool = False # 模组标了【留着】= 收集包/任务要用的,别卖别扔
32
33
34@dataclass
35class State:
36 raw: str
37 loc: str = "?"
38 pos: tuple | None = None
39 year: int = 0
40 season: str = ""
41 day: int = 0
42 hour: int = 0
43 minute: int = 0
44 bag_free: int = 99
45 items: list = field(default_factory=list)
46 grid: dict = field(default_factory=dict)
47 trash: list = field(default_factory=list)
48 npcs: list = field(default_factory=list)
49
50 # -------------------------------------------------------------- 派生
51 @property
52 def abs_day(self) -> int:
53 """连续日序号(第1年春1日 = 112)—— 用来算「上次是哪天」。"""
54 return self.year * 112 + SEASON_IDX.get(self.season, 0) * 28 + self.day
55
56 @property
57 def clock(self) -> tuple:
58 return (self.hour, self.minute)
59
60 def berry_item(self) -> str | None:
61 """现在是哪个莓子季?不在莓子季返回 None。"""
62 for s in spec.BERRY_SEASONS:
63 if s["season"] == self.season and s["start"] <= self.day <= s["end"]:
64 return s["item"]
65 return None
66
67 def berry_count(self) -> int:
68 return sum(i.stack for i in self.items
69 if any(k in i.name for k in spec.BERRY_KEYS))
70
71 def villagers_within(self, radius: int) -> list:
72 if not self.pos:
73 return []
74 return [(n, p, manhattan(p, self.pos)) for n, p in self.npcs
75 if manhattan(p, self.pos) <= radius]
76
77 def nearby(self, radius: int) -> list:
78 """半径内的采集品和灌木:[(距离, 类型, 坐标)],类型是 'forage' / 'bush'。"""
79 if not self.pos:
80 return []
81 berries = self.berry_item() is not None
82 out = []
83 for p, ch in self.grid.items():
84 kind = None
85 if ch == spec.SYMBOLS["forage"]:
86 kind = "forage"
87 elif ch == spec.SYMBOLS["bush"] and berries:
88 kind = "bush" # 非莓子季的灌木是空的,绝不去戳
89 if kind:
90 d = manhattan(p, self.pos)
91 if d <= radius:
92 out.append((d, kind, p))
93 out.sort(key=lambda t: t[0])
94 return out
95
96
97# ------------------------------------------------------------------ 解析
98def parse(txt: str) -> State:
99 st = State(raw=txt)
100
101 m = spec.RE_DATE.search(txt)
102 if m:
103 st.year, st.season, st.day = int(m.group(1)), m.group(2), int(m.group(3))
104
105 m = spec.RE_HOUR.search(section(txt, "基本状态"))
106 if m:
107 st.hour, st.minute = int(m.group(1)), int(m.group(2))
108
109 m = spec.RE_POS.search(txt)
110 if m:
111 st.pos = (int(m.group(1)), int(m.group(2)))
112
113 st.loc = parse_loc(txt)
114
115 m = spec.RE_BAG_FREE.search(txt)
116 if m:
117 st.bag_free = int(m.group(1))
118
119 body = section(txt, "背包(序号: 物品)") or section(txt, "背包")
120 for line in body.splitlines():
121 im = spec.RE_ITEM.match(line)
122 if not im:
123 continue
124 name = im.group(2).strip()
125 # 物品名后面可能跟(水 18/40)、[可食用 +25体力]、【留着:…】,只留名字
126 name = re.split(r"[(\[【]", name)[0].strip()
127 sm = spec.RE_STACK.search(name)
128 stack = int(sm.group(1)) if sm else 1
129 if sm:
130 name = name[:sm.start()].strip()
131 st.items.append(Item(name=name, stack=stack, line=line, keep="【留着" in line))
132
133 st.grid = parse_grid(txt)
134 st.trash = parse_trash(txt)
135 st.npcs = parse_npcs(txt)
136 return st
137
138
139def parse_loc(txt: str) -> str:
140 """地点:优先「## 位置」里的英文名,退回基本状态行。"""
141 seg = section(txt, "位置")
142 m = re.search(r"\b([A-Z][A-Za-z]+(?:\s[A-Z][A-Za-z]+)?)\b", seg)
143 if m:
144 return m.group(1)
145 m = re.search(r"\b(FarmHouse|Farm|Town|Forest|Mountain|Beach|Desert|"
146 r"UndergroundMine|SeedShop|Saloon|BusStop|Railroad)\b", txt)
147 return m.group(1) if m else "?"
148
149
150def parse_grid(txt: str) -> dict:
151 """字符地图 → {(x, y): 字符}。原点从表头的「左上角坐标」取,y 用行号。"""
152 head = spec.RE_MAP_HEAD.search(txt)
153 if not head:
154 return {}
155 origin = spec.RE_MAP_ORIGIN.search(txt)
156 if not origin:
157 return {}
158 ox = int(origin.group(1))
159 seg = re.search(r"^## 文字地图.*?$(.*?)(?=^## |\Z)", txt, re.M | re.S)
160 if not seg:
161 return {}
162 grid = {}
163 for line in seg.group(1).splitlines():
164 rm = spec.RE_MAP_ROW.match(line)
165 if not rm:
166 continue
167 y, row = int(rm.group(1)), rm.group(2)
168 for i, ch in enumerate(row):
169 if ch != " ":
170 grid[(ox + i, y)] = ch
171 return grid
172
173
174def parse_trash(txt: str) -> list:
175 """垃圾桶坐标。它不是地图字符,是单独一行「可交互地图格[Garbage]」。"""
176 out = []
177 for m in spec.RE_TRASH.finditer(txt):
178 for cx, cy in spec.RE_COORD.findall(m.group(1)):
179 p = (int(cx), int(cy))
180 if p not in out:
181 out.append(p)
182 return out
183
184
185def parse_npcs(txt: str) -> list:
186 out = []
187 seg = section(txt, "人物")
188 for line in seg.splitlines():
189 m = spec.RE_NPC.match(line.strip())
190 if m:
191 out.append((m.group(1).strip(), (int(m.group(2)), int(m.group(3)))))
192 return out

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