feat: explore 메서드에 wanted_ores 매개변수 추가 및 로직 개선
- explore 메서드에 wanted_ores 매개변수를 추가하여 특정 자원을 찾을 수 있도록 개선 - 원하는 자원이 발견될 때까지 계속 이동하며, 다른 자원이 발견되더라도 즉시 멈추지 않도록 로직 수정 - 시스템 프롬프트 및 README.md에 변경 사항 반영
This commit is contained in:
@@ -41,7 +41,12 @@ class ActionExecutor:
|
||||
except Exception as e: return False, f"실행 오류: {e}"
|
||||
|
||||
# ── 탐색 ─────────────────────────────────────────────────────────
|
||||
def explore(self, direction: str = "east", max_steps: int = 200) -> tuple[bool, str]:
|
||||
def explore(
|
||||
self,
|
||||
direction: str = "east",
|
||||
max_steps: int = 200,
|
||||
wanted_ores: list[str] | None = None,
|
||||
) -> tuple[bool, str]:
|
||||
dir_map = {
|
||||
"north": "defines.direction.north", "south": "defines.direction.south",
|
||||
"east": "defines.direction.east", "west": "defines.direction.west",
|
||||
@@ -50,17 +55,39 @@ class ActionExecutor:
|
||||
}
|
||||
lua_dir = dir_map.get(direction, "defines.direction.east")
|
||||
self.rcon.lua(P + f"p.walking_state = {{walking = true, direction = {lua_dir}}}")
|
||||
wanted_ores = wanted_ores or []
|
||||
# JSON에서 single string으로 올 수도 있으니 방어
|
||||
if isinstance(wanted_ores, str):
|
||||
wanted_ores = [wanted_ores]
|
||||
|
||||
wanted_lua = "local wanted = {}\n"
|
||||
for name in wanted_ores:
|
||||
safe = str(name).replace("\\", "\\\\").replace('"', '\\"')
|
||||
wanted_lua += f'wanted["{safe}"] = true\n'
|
||||
|
||||
stuck_count, last_pos = 0, None
|
||||
for step in range(max_steps):
|
||||
time.sleep(0.1)
|
||||
if step % 20 == 0:
|
||||
result = self.rcon.lua(P + """
|
||||
result = self.rcon.lua(P + wanted_lua + """
|
||||
local ok, data = pcall(function()
|
||||
local pos = p.position
|
||||
local res = p.surface.find_entities_filtered{position = pos, radius = 50, type = "resource"}
|
||||
if #res == 0 then return "NONE:" .. string.format("%.0f,%.0f", pos.x, pos.y) end
|
||||
local counts = {}
|
||||
for _, e in ipairs(res) do counts[e.name] = (counts[e.name] or 0) + 1 end
|
||||
-- wanted_ores가 지정된 경우: 그 중 하나라도 있으면 FOUND, 아니면 UNWANTED 반환
|
||||
if wanted and next(wanted) ~= nil then
|
||||
for n, _ in pairs(wanted) do
|
||||
if counts[n] and counts[n] > 0 then
|
||||
local parts = {}
|
||||
for name, count in pairs(counts) do parts[#parts+1] = name .. "=" .. count end
|
||||
return "FOUND:" .. string.format("%.0f,%.0f", pos.x, pos.y) .. "|" .. table.concat(parts, ",")
|
||||
end
|
||||
end
|
||||
return "UNWANTED:" .. string.format("%.0f,%.0f", pos.x, pos.y)
|
||||
end
|
||||
|
||||
local parts = {}
|
||||
for name, count in pairs(counts) do parts[#parts+1] = name .. "=" .. count end
|
||||
return "FOUND:" .. string.format("%.0f,%.0f", pos.x, pos.y) .. "|" .. table.concat(parts, ",")
|
||||
@@ -72,6 +99,9 @@ if ok then rcon.print(data) else rcon.print("ERROR") end
|
||||
self.rcon.lua(P + "p.walking_state = {walking = false, direction = defines.direction.north}")
|
||||
parts = result.replace("FOUND:", "").split("|")
|
||||
return True, f"자원 발견! 위치({parts[0]}), 자원: {parts[1] if len(parts)>1 else ''}"
|
||||
if result.startswith("UNWANTED:"):
|
||||
# 원하는 자원이 아니면 계속 걷기
|
||||
continue
|
||||
if result.startswith("NONE:"):
|
||||
pos_str = result.replace("NONE:", "")
|
||||
if last_pos == pos_str: stuck_count += 1
|
||||
|
||||
Reference in New Issue
Block a user