1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
-- 函数:重置情怀梦副本的冷却计时器和别名
local function reset_qinghuaimeng_timer()
local now_time = get_time()
-- 设置冷却时间为 24 小时 + 5 分钟
local cooldown_time = now_time + 24 * 60 * 60 + 300
var["qinghuaimeng_start_time"] = cooldown_time
send("alias 挑战情怀梦 " .. cooldown_time)
log_message("情怀梦副本冷却计时器已重置。", C.G, "<系统>")
end

-- 函数:根据层数设置技能
local function setup_qinghuaimeng_skills(level_num)
local beiskills_var = "qhm_beiskills_" .. level_num
local pfm_var = "qhm_pfm_" .. level_num

send("alias bei_skills " .. expand(var[beiskills_var]))
send("alias pfm " .. expand(var[pfm_var]))
send("set wimpycmd hp " .. var["char_id"] .. "\\pfm")
send("bei_skills")
end

-- 函数:在执行回调前,有选择地执行一个 "yun_pfm"
local function execute_with_optional_yun_pfm(callback_func)
check_busy2(function()
if var["yun_pfm"] and var["yun_pfm"] ~= "" then
exec(var["yun_pfm"])
check_busy2(function()
if callback_func then callback_func() end
end)
else
if callback_func then callback_func() end
end
end)
end

-- 提取一个公共的启动函数
local function start_first_floor()
var.qhm_current_floor_killers = 0
log_message("第一层杀敌计数已重置。", C.Y, "<解密>")
g(70, function()
send("set wimpy 100")
wait(3, function()
setup_qinghuaimeng_skills(1)
wait(3, function()
close_fight()
exec("qinghuaimeng;up;mazemap")
log_message("☆第一层开始☆", C.Y, "<解密>")
exec("hp " .. var["char_id"])
end)
end)
end)
end

-- 函数:准备迷宫文件并解析为二维表格
local function prepare_maze_file_and_parse(level)
log_to_txt("qinghuaimeng", var.id)
local id = GetVar("id") or ""
if id == "" then
echo("错误:无法获取角色ID。")
return nil
end

Run("#var _path %syspath()")
local path = GetVar("_path")
Run("#unvar _path")
local log_filepath = path .. id .. "/log/" .. id .. "qinghuaimeng.txt"

local file = io.open(log_filepath, "r")
if not file then
echo("错误:无法打开迷宫文件!路径: " .. log_filepath)
return nil
end
local content = file:read("*all")
file:close()

if level == 1 then
content = content:gsub("情怀梦境迷宫第一层:", "")
content = content:gsub("情怀梦境迷宫第一层行走方向提示:(.*)", "")
content = content:gsub("C:当前位置 B:宝藏位置 E:出口位置", "")
elseif level == 2 then
content = content:gsub("情怀梦境迷宫第二层:\n?", "")
content = content:gsub("情怀梦境迷宫第二层行走方向提示:[^\n]*\n?", "")
content = content:gsub("C:当前位置 B:宝藏位置 E:出口位置\n?", "")
content = content:gsub("^\n+", ""):gsub("\n+$", "")
local formatted_lines = {}
for line in content:gmatch("[^\n]+") do
local new_line = ""
for i = 1, #line do
local char = line:sub(i, i)
if i == 1 or char ~= " " then
new_line = new_line .. char
else
local is_near_pipe = (i > 1 and line:sub(i - 1, i) == " |") or (i < #line and line:sub(i, i + 1) == "| ")
if ((i - 1) % 4 == 0) and not is_near_pipe then
new_line = new_line .. ":"
else
new_line = new_line .. char
end
end
end
table.insert(formatted_lines, new_line)
end
content = table.concat(formatted_lines, "\n")
end

file = io.open(log_filepath, "w")
file:write(content)
file:close()

local maze = {}
for line in content:gmatch("[^\n]+") do
local row = {}
for i = 1, #line do
table.insert(row, line:sub(i, i))
end
table.insert(maze, row)
end

return #maze > 0 and maze or nil
end


-- 函数:获取相反的方向
local function get_opposite_direction(dir)
local opposites = { north = "south", south = "north", east = "west", west = "east" }
return opposites[dir]
end


-- 全局的继续移动函数句柄,用于在触发器中恢复移动流程
__qhm_continue_movement = nil

-- 函数:按顺序执行移动指令,并在完成后执行回调
local function execute_maze_movement(directions_to_move, callback_after_all_moves, is_heading_to_exit)
if not directions_to_move or #directions_to_move == 0 then
if callback_after_all_moves then callback_after_all_moves() end
return
end

local move_index = 1
local single_step, killer_check_and_loop, wait_for_enemy_to_disappear

wait_for_enemy_to_disappear = function(callback_when_clear)
if not var.qhm_enemy_present then
if callback_when_clear then callback_when_clear() end
else
log_message("等待敌人消失...", C.G, "<系统>")
wait(1, function() wait_for_enemy_to_disappear(callback_when_clear) end)
end
end

single_step = function()
-- 寻路完成,执行最终回调并清理
if move_index > #directions_to_move then
__qhm_continue_movement = nil -- 清理句柄
if callback_after_all_moves then callback_after_all_moves() end
return
end

-- 在接近出口时检查杀敌数,如果不够则进入刷怪模式
if is_heading_to_exit and
(tonumber(var["qhm_killer_target_num"]) or 0) > 0 and
#directions_to_move >= 2 and
move_index == #directions_to_move - 1 then

local current_killers = var.qhm_current_floor_killers or 0
local target_killers = tonumber(var["qhm_killer_target_num"] or 0)

if current_killers < target_killers then
log_message("即将到达出口,但本层杀敌数不足,启动刷怪模式。", C.Y, "<解密>")
var.is_grinding_killers = true
killer_check_and_loop()
return
end
end

-- 正常移动步骤
wait_for_enemy_to_disappear(function()
local direction = directions_to_move[move_index]
check_busy2(function()
-- 设置继续函数,以便触发器可以调用
__qhm_continue_movement = single_step
-- 打开监听“无战斗”的触发器
open_trigger("otherquest_qinghuaimeng_12")

exec("go " .. direction .. ";kill wanderer")
move_index = move_index + 1
end)
end)
end

killer_check_and_loop = function()
local current_killers = var.qhm_current_floor_killers or 0
local target_killers = tonumber(var["qhm_killer_target_num"] or 0)

-- 杀敌数达标,退出刷怪模式,继续寻路
if current_killers >= target_killers then
log_message("本层已达到杀敌数量要求 (" .. current_killers .. "/" .. target_killers .. "),继续前往出口。", C.G, "<解密>")
var.is_grinding_killers = false
__qhm_continue_movement = nil
single_step()
return
end

log_message("本层杀敌数未达标 (" .. current_killers .. "/" .. target_killers .. "),开始来回移动遇敌。", C.Y, "<系统>")

local forward_dir = directions_to_move[#directions_to_move - 1]
local backward_dir = get_opposite_direction(forward_dir)

if not forward_dir or not backward_dir then
log_message("错误:无法确定来回移动的方向。继续正常寻路。", C.R, "<解密>")
var.is_grinding_killers = false
__qhm_continue_movement = nil
single_step()
return
end

-- 定义刷怪的单步移动函数
local function grind_move(dir, next_func)
wait_for_enemy_to_disappear(function()
check_busy2(function()
-- 设置继续函数,指向下一步刷怪动作
__qhm_continue_movement = next_func
open_trigger("otherquest_qinghuaimeng_12")
exec("go " .. dir .. ";kill wanderer")
end)
end)
end

-- 定义来回移动的循环
local move_backward_step
local move_forward_step = function() grind_move(forward_dir, move_backward_step) end
move_backward_step = function() grind_move(backward_dir, killer_check_and_loop) end -- 完成一次来回后,重新检查杀敌总数

-- 开始第一次移动
move_forward_step()
end

-- 启动寻路
single_step()
end

-- 函数:将A*算法返回的坐标路径转换为方向指令列表
local function convert_path_to_directions(path)
if not path or #path < 2 then return {} end
local directions = {}
for i = 1, #path - 1 do
local p_r, p_c = path[i]:match("^(%d+),(%d+)$")
local c_r, c_c = path[i+1]:match("^(%d+),(%d+)$")
p_r, p_c, c_r, c_c = tonumber(p_r), tonumber(p_c), tonumber(c_r), tonumber(c_c)

local direction
if c_r < p_r then direction = "north"
elseif c_r > p_r then direction = "south"
elseif c_c < p_c then direction = "west"
elseif c_c > p_c then direction = "east"
end
if direction then table.insert(directions, direction) end
end
return directions
end

-- 函数:将字符坐标转换为单元格坐标
local function get_cell_coords(r_char, c_char)
if type(r_char) ~= "number" or type(c_char) ~= "number" then return nil, nil end
local r_cell = math.floor(r_char / 2)
local c_cell = math.floor((c_char - 3) / 4) + 1
return r_cell, c_cell
end

-- 函数:将单元格坐标转换为字符坐标
local function get_char_coords(r_cell, c_cell)
if type(r_cell) ~= "number" or type(c_cell) ~= "number" then return nil, nil end
local r_char = r_cell * 2
local c_char = (c_cell - 1) * 4 + 3
return r_char, c_char
end


add_alias("otherquest_qinghuaimeng", function(params)
var["killer_master"] = nil
qinghuaimeng_box = var["qinghuaimeng_box"] or 0
qinghuaimeng_killer_num_1 = 1
var["qinghuaimeng_2"]=1
open_qinghuaimeng_triggers()
log_message("☆☆☆"..C.G.."挑战情怀梦副本开始"..C.M.."☆☆☆", C.M, "<解密>")
set_start_time("情怀梦")
var["qinghuaimeng_killer_num"] = 0
var.is_grinding_killers = false
-- 初始化敌人存在标志和寻路句柄
var.qhm_enemy_present = false
__qhm_continue_movement = nil
var["qhm_killer_target_num"] = var["qhm_killer_target_num"] or 0
log_message("每层目标杀敌数: " .. var["qhm_killer_target_num"] .. " (设置为0则不启用)", C.Y, "<解密>")
reset_qinghuaimeng_timer()

if var["qhm_da_huandan"] and var["qhm_da_huandan"] == 1 then
buy_da_huandan(var["qhm_da_huandan_number"], function()
close_trigger("otherquest_qinghuaimeng_5")
check_busy2(function()
wait(3, start_first_floor)
end)
end)
else
start_first_floor()
end

add_trigger("otherquest_qinghuaimeng_2", "s*看起来(.*)想杀死你!$", function(params)
var["idle"] = 0
-- 设置敌人存在标志
var.qhm_enemy_present = true
end)


add_trigger("otherquest_qinghuaimeng_3", "s*(.*)一个闪身就不见了。$", function(params)
-- 清除敌人存在标志
var.qhm_enemy_present = false

var["qinghuaimeng_killer_num"] = (var["qinghuaimeng_killer_num"] or 0) + 1
var.qhm_current_floor_killers = (var.qhm_current_floor_killers or 0) + 1

if var["killer_master"] then
var["killer_master_count"] = (var["killer_master_count"] or 0) + 1
if var["killer_master_count"] == 1 then
-- 第一个NPC消失,准备挑战下一个
exec("alias action 准备挑战")
elseif var["killer_master_count"] == 2 then
-- 第二个NPC消失,继续走迷宫
var["killer_master_count"] = nil -- 重置计数器以备下一层
exec("mazemap")
end
end

execute_with_optional_yun_pfm(function()
exec("yun qi;yun jing;yun jingli")
-- 战斗结束后,如果正在寻路或刷怪,则恢复流程
if __qhm_continue_movement then
local func_to_call = __qhm_continue_movement
__qhm_continue_movement = nil
log_message("战斗结束,恢复移动...", C.G, "<系统>")
func_to_call()
end
end)
end)


add_trigger("otherquest_qinghuaimeng_4", "s*(.*)神志迷糊,脚下一个不稳,倒在地上昏了过去。$", function(params)
var.qhm_enemy_present = false
-- 如果在寻路中昏迷,尝试恢复流程
if __qhm_continue_movement then
local func_to_call = __qhm_continue_movement
__qhm_continue_movement = nil
log_message("昏迷后恢复,继续移动...", C.Y, "<系统>")
func_to_call()
end

exec("kill wanderer")
end)

add_trigger("otherquest_qinghuaimeng_6", "s*总步数: 0$", function(params)
open_trigger("otherquest_qinghuaimeng_11")
local n = collectgarbage("count")
collectgarbage("collect")
if var["killer_master"] and var["killer_master"] > 0 then
echo("\n" .. C.x .. "LUA内存清理:" .. C.c .. n .. C.x .. " --> " .. C.y .. collectgarbage("count"))
if (var["qinghuaimeng_2"] or 0) == 1 then
close_qinghuaimeng_triggers()
close_trigger("otherquest_qinghuaimeng_6")
open_trigger("otherquest_qinghuaimeng2_1")
open_triggers("otherquest_qinghuaimeng", 1, 5)
open_triggers("otherquest_qinghuaimeng", 7, 18)
var["killer_master"] = nil
check_busy2(function()
setup_qinghuaimeng_skills(1)
var.qhm_current_floor_killers = 0
log_message("第二层杀敌计数已重置。", C.Y, "<解密>")

exec("up2;mazemap")
log_message("☆第二层开始☆", C.Y, "<解密>")
end)
else
exec("leave")
qinghuaimeng_killer_num=var["qinghuaimeng_killer_num"] - 4
log_message("挑战情怀梦共杀killer" .. "【" .. qinghuaimeng_killer_num .. "】" .. "个。", C.Y, "<解密>")
var["qinghuaimeng_killer_num"] = 0
end
end
end)

add_trigger("otherquest_qinghuaimeng_8", "\^\\s(.*)梦境掌控者\\s+(\\S+)\\((.*)\\)", function(params)
close_trigger("otherquest_qinghuaimeng_8")
var["killer_name"] = params[2]
var["killer_id"] = string.lower(params[3])
var["pfm_id"] = var["killer_id"]
var["killer_master"] = 1
execute_with_optional_yun_pfm(function()
exec("yun qi;yun jing;yun jingli")
exec("kill " .. var["killer_id"])
end)
end)

add_trigger("otherquest_qinghuaimeng_9", "\^\\s*你把\\s+\"action\"\\s+设定为\\s+\"准备挑战", function(params)
open_trigger("otherquest_qinghuaimeng_8")
if var["killer_master"] == 1 then
exec("look")
end
end)

setup_hp_monitor("otherquest_qinghuaimeng_10")

add_trigger("otherquest_qinghuaimeng_11", "\^\\s*你被奖励了(.*)枚情怀币!", function(params)
close_qinghuaimeng_triggers()
set_end_time("情怀梦")
var["qinghuaimeng_tongbao"] = params[1]
log_message("完成情怀梦副本,获得" .. var["qinghuaimeng_tongbao"] .. "情怀币。", C.Y, "<解密>")
local time_diff = calculate_time_diff("情怀梦", 1)
log_message("任务耗时:" .. time_diff .. "。", C.Y)
log_message("☆☆☆"..C.G.."挑战情怀梦副本结束"..C.M.."☆☆☆", C.M, "<解密>")
reset_qinghuaimeng_timer()
exec("changejob")
end)


-- 新增触发器:处理“这里没有这个人。”,用于在无战斗时继续移动
add_trigger("otherquest_qinghuaimeng_12", "^这里没有这个人。$", function()
close_trigger("otherquest_qinghuaimeng_12") -- 立即关闭,避免重复触发
if __qhm_continue_movement then
local func_to_call = __qhm_continue_movement
__qhm_continue_movement = nil -- 清除句柄,防止重入
func_to_call() -- 执行下一步移动
end
end)


add_alias("qinghuaimeng_skills_1", function(params) setup_qinghuaimeng_skills(1) end)
add_alias("qinghuaimeng_skills_2", function(params) setup_qinghuaimeng_skills(2) end)

add_trigger("otherquest_qinghuaimeng_13", "\^\\s*请明天再来情怀梦境吧。", function(params)
close_qinghuaimeng_triggers()
log_message("情怀梦副本,时间间隔不够。", C.R, "<解密>")
reset_qinghuaimeng_timer()
exec("changejob")
end)

add_trigger("otherquest_qinghuaimeng_15", "\^\\s*宝藏之地都被你破坏了,还找什么呢!", function(params)
unset_timer("timer")
qinghuaimeng_box = 0
exec("mazemap")
end)
add_trigger("otherquest_qinghuaimeng_16", "\^\\s*你得到了(.*)枚情怀币,但是好像也触碰到了什么机关!", function(params)
open_trigger("otherquest_qinghuaimeng_6")
unset_timer("timer")
qinghuaimeng_box = 0
var["qinghuaimeng_tongbao_1"] = params[1]
exec("mazemap")
log_message("挖到宝箱,获得" .. var["qinghuaimeng_tongbao_1"] .. "情怀币。", C.Y, "<解密>")
end)
add_trigger("otherquest_qinghuaimeng_17", "\^\\s*你从宝藏中得到了一个(.*)。", function(params)
var["qinghuaimeng_tongbao_2"] = params[1]
unset_timer("timer")
exec("mazemap")
log_message("挖到宝箱,获得" .. var["qinghuaimeng_tongbao_2"] .. "。", C.Y, "<解密>")
end)
end)

-- 迷宫第一层寻路
add_trigger("otherquest_qinghuaimeng_1", "s*情怀梦境迷宫第一层行走方向提示:(.*)$", function(params)
local maze = prepare_maze_file_and_parse(1)
if not maze then return end

local graph, startRoom, treasureRoom, endRoom = {}, nil, nil, nil
local function isLetter(ch) return ch == 'B' or ch == 'C' or ch == 'E' or ch == 'O' end
local function isVertex(m, r, c)
if m and m[r] and m[r][c] and isLetter(m[r][c]) then
if not m[r][c-1] or not isLetter(m[r][c-1]) then return true end
end
return false
end

local idxrow, idxcol = 0, 0
for i, row in ipairs(maze) do
idxcol = 1
for j, cell in ipairs(row) do
if isVertex(maze, i, j) then
local node_id = idxrow .. "," .. idxcol
graph[node_id] = {}
if cell == 'C' then startRoom = { idxrow, idxcol } end
if cell == 'B' then treasureRoom = { idxrow, idxcol } end
if cell == 'E' then endRoom = { idxrow, idxcol } end
if maze[i][j+1] then
if isLetter(maze[i][j+1]) then
if maze[i][j+1] == 'C' then startRoom = { idxrow, idxcol } end
if maze[i][j+1] == 'B' then treasureRoom = { idxrow, idxcol } end
if maze[i][j+1] == 'E' then endRoom = { idxrow, idxcol } end
end
end
idxcol = idxcol + 1
end
end
if i % 2 == 1 then idxrow = idxrow + 1 end
end

idxrow, idxcol = 0, 0
for i, row in ipairs(maze) do
idxcol = 1
for j, cell in ipairs(row) do
if isVertex(maze, i, j) then
local node_id = idxrow .. "," .. idxcol
if j > 2 and maze[i][j - 2] == '-' then table.insert(graph[node_id], idxrow .. "," .. (idxcol - 1)) end
if j < #row and maze[i][j + 2] == '-' then table.insert(graph[node_id], idxrow .. "," .. (idxcol + 1)) end
if i > 2 and maze[i - 1][j] == '|' then table.insert(graph[node_id], (idxrow - 1) .. "," .. idxcol) end
if i < #maze and maze[i + 1][j] == '|' then table.insert(graph[node_id], (idxrow + 1) .. "," .. idxcol) end
idxcol = idxcol + 1
end
end
if i % 2 == 1 then idxrow = idxrow + 1 end
end

if not startRoom or not endRoom then
echo("错误:未能在一层迷宫中找到起点(C)或终点(E)。")
return
end

local start_node = table.concat(startRoom, ",")
local goal_node, final_callback
local is_going_to_exit = false

if startRoom[1] == endRoom[1] and startRoom[2] == endRoom[2] then
echo("总步数: 0"); echo("已在出口 E。")
check_busy2(function() wait(2, function() exec("look") end) end)
return
end

if qinghuaimeng_box ~= 0 and treasureRoom then
goal_node = table.concat(treasureRoom, ",")
is_going_to_exit = false
final_callback = function()
echo("已到达宝藏 B!")
var.qinghuaimeng_box = 1
close_trigger("otherquest_qinghuaimeng_6")
set_timer("timer", 1, function() exec("search") end)
end
else
goal_node = table.concat(endRoom, ",")
is_going_to_exit = true
final_callback = function()
echo("已成功到达出口 E。")
check_busy2(function() wait(2, function() setup_qinghuaimeng_skills(2); exec("look") end) end)
end
end

local heuristic = function(n1, n2) local x1,y1=n1:match("(%d+),(%d+)") local x2,y2=n2:match("(%d+),(%d+)") return math.abs(x1-x2)+math.abs(y1-y2) end
local get_neighbors = function(node) local n = {}; for _, neighbor in ipairs(graph[node] or {}) do table.insert(n, {room=neighbor, dir="", cost=1}) end return n end
local path, _ = astar_search(start_node, goal_node, {heuristic_func=heuristic, get_neighbors_func=get_neighbors})

if path then
local directions = convert_path_to_directions(path)
echo("总步数: " .. #directions)
execute_maze_movement(directions, final_callback, is_going_to_exit)
else
echo("错误:未能为第一层迷宫找到路径。")
end
end)

-- 迷宫第二层寻路
add_trigger("otherquest_qinghuaimeng2_1", "s*情怀梦境迷宫第二层行走方向提示:(.*)$", function(params)
local maze = prepare_maze_file_and_parse(2)
if not maze then return end

local startRoom_char, treasureRoom_char, endRoom_char
for r, row in ipairs(maze) do
for c, char in ipairs(row) do
if char == "C" then startRoom_char = {r, c}
elseif char == "B" then treasureRoom_char = {r, c}
elseif char == "E" then endRoom_char = {r, c}
end
end
end
if not startRoom_char then echo("错误:未找到起点 'C'!"); return end

local start_r, start_c = get_cell_coords(startRoom_char[1], startRoom_char[2])
if not start_r then echo("错误:起点 'C' 坐标转换失败!"); return end
local start_node = start_r .. "," .. start_c

local goal_node, final_callback, target_description
local is_going_to_exit = false

local treasure_r, treasure_c
if treasureRoom_char then
treasure_r, treasure_c = get_cell_coords(treasureRoom_char[1], treasureRoom_char[2])
end

if treasure_r and treasure_c then
goal_node, target_description = treasure_r .. "," .. treasure_c, "宝藏 B"
is_going_to_exit = false
final_callback = function()
echo("已到达 " .. target_description .. "!")
var.qinghuaimeng_box = 1; var["qinghuaimeng_2"] = 0
set_timer("timer", 1, function() exec("search") end)
end
else
local end_r, end_c
if endRoom_char then
end_r, end_c = get_cell_coords(endRoom_char[1], endRoom_char[2])
end

if end_r and end_c then
goal_node, target_description = end_r .. "," .. end_c, "出口 E"
is_going_to_exit = true
final_callback = function()
echo("已成功到达 " .. target_description .. "。")
check_busy2(function() wait(2, function() setup_qinghuaimeng_skills(2); exec("look") end) end)
end
else
echo("错误:迷宫中未找到有效目标点 (B或E)。"); open_triggers("otherquest_qinghuaimeng", 1, 18); return
end
end
echo("确定目标为: " .. target_description .. " (" .. goal_node .. ")")

local heuristic = function(n1, n2) local y1,x1=n1:match("(%d+),(%d+)") local y2,x2=n2:match("(%d+),(%d+)") return math.abs(y1-y2)+math.abs(x1-x2) end
local get_neighbors_level2 = function(node_str)
local r_cell, c_cell = node_str:match("^(%d+),(%d+)$")
r_cell, c_cell = tonumber(r_cell), tonumber(c_cell)
local neighbors = {}
local moves = {{0, 1, "east"}, {0, -1, "west"}, {1, 0, "south"}, {-1, 0, "north"}}
for _, move in ipairs(moves) do
local dr, dc, dir = move[1], move[2], move[3]
local next_r, next_c = r_cell + dr, c_cell + dc
local r_char, c_char = get_char_coords(r_cell, c_cell)
if r_char and c_char then
local wall_r, wall_c = r_char + dr, c_char + (dc * 2)
if maze[wall_r] and maze[wall_r][wall_c] and maze[wall_r][wall_c] ~= '|' and maze[wall_r][wall_c] ~= '-' then
table.insert(neighbors, {room = next_r .. "," .. next_c, dir = dir, cost = 1})
end
end
end
return neighbors
end
local path, _ = astar_search(start_node, goal_node, {heuristic_func=heuristic, get_neighbors_func=get_neighbors_level2, max_iterations=5000})

open_triggers("otherquest_qinghuaimeng", 1, 18)
if path then
local directions = convert_path_to_directions(path)
echo("总步数: " .. #directions)
execute_maze_movement(directions, final_callback, is_going_to_exit)
else
echo("错误:未能为第二层迷宫找到路径。")
end
end)
function close_qinghuaimeng_triggers()
close_triggers("otherquest_qinghuaimeng", 1, 18)
end
function open_qinghuaimeng_triggers()
open_triggers("otherquest_qinghuaimeng", 1, 18)
end