fix: mine_resource 채굴 로직 개선 및 JSON 파싱 안정성 향상

- 광석 위치로 이동 후 실제 자원 존재 여부를 재확인하여 실패한 타일을 제외하는 로직 추가
- JSON 파싱 시 중괄호 및 대괄호 균형을 추적하고, 잘린 응답 복구 로직을 개선하여 안정성 향상
- README.md에 변경 사항 및 기능 설명 추가
This commit is contained in:
21in7
2026-03-25 21:37:15 +09:00
parent 9a2185ca4b
commit 7abdf8713a
5 changed files with 63 additions and 15 deletions

View File

@@ -192,20 +192,36 @@ rcon.print("ALL_EXCLUDED")
return False, f"좌표 파싱 실패: {find_result}"
# 2. 광석 위치로 걸어가기
print(f" [채굴] 광석({ox},{oy})으로 이동... (시도 {round_num+1}, 제외: {len(failed_positions)}개)")
ok, msg = self.move(ox, oy)
print(f" [채굴] 광석({mine_x:.2f},{mine_y:.2f})으로 이동... (시도 {round_num+1}, 제외: {len(failed_positions)}개)")
ok, msg = self.move(mine_x, mine_y)
if not ok:
print(f" [채굴] 이동 실패: {msg}")
failed_positions.add((ox, oy))
continue
# 2.5. 이동 후 실제로 해당 광석이 있는지 재확인
# Factorio는 자원이 "타일"이 아니라 "엔티티"라서, 좌표 미세오차/경계로 인해
# move 후에도 p.position 근처에 자원이 없으면 채굴을 시도하지 않고 다음 후보로 넘어간다.
near_lua = P + f"""
local res = p.surface.find_entities_filtered{{position = p.position, radius = 1.2, name = "{ore}"}}
if res and #res > 0 then rcon.print("YES") else rcon.print("NO") end
"""
has_ore = self.rcon.lua(near_lua).strip() == "YES"
if not has_ore:
failed_positions.add((ox, oy))
continue
# 3. 현재 위치에서 채굴 시도
stall_count = 0
last_item = self._get_item_count(ore)
mined_this_tile = False
for tick in range(300): # 최대 30초
self.rcon.lua(P + f"p.mining_state = {{mining = true, position = {{{mine_x}, {mine_y}}}}}")
# mining_state로 “우클릭 유지”에 가까운 수동 채굴을 시뮬레이션
self.rcon.lua(P + f"""
p.update_selected_entity({{x = {mine_x}, y = {mine_y}}})
p.mining_state = {{mining = true, position = {{x = {mine_x}, y = {mine_y}}}}}
""")
time.sleep(0.1)
if tick % 8 == 7:
@@ -215,17 +231,29 @@ rcon.print("ALL_EXCLUDED")
total_mined = current - before_count
return True, f"{ore} {total_mined}개 채굴 완료"
# 아이템이 아직 안 늘었더라도, 채굴 진행률이 올라가기 시작하면 “채굴 시작됨”으로 간주
prog_raw = self.rcon.lua(P + "rcon.print(tostring(p.character_mining_progress or 0))")
try:
prog = float(prog_raw)
except:
prog = 0.0
if current > last_item:
stall_count = 0
last_item = current
mined_this_tile = True
else:
stall_count += 1
if prog > 0.02:
mined_this_tile = True
# 채굴은 "바로 아이템 카운트가 오르지" 않을 수 있어 너무 빨리 포기하지 않도록 완충
if stall_count >= 5:
# 진행률이 0에 가까운데도 오랫동안 아이템이 안 늘면, 그 타일은 접근 불가로 보고 중단
if stall_count >= 20 and prog <= 0.02:
break
# mined_this_tile=false인 경우(진행 시작 전)만 stall_count로 종료
# mined_this_tile=true인 경우에는 items가 늦게 증가해도 루프 끝까지 기다림
self.rcon.lua(P + "p.mining_state = {mining = false}")
total_mined = self._get_item_count(ore) - before_count