-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.mod
More file actions
271 lines (253 loc) · 7.63 KB
/
search.mod
File metadata and controls
271 lines (253 loc) · 7.63 KB
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
exitback={east="w",e="w",south="n",s="n",west="e",w="e",north="s",n="s",southeast="nw",se="nw",southwest="ne",sw="ne",northeast="sw",ne="sw",northwest="se",nw="se",eastup="wd",eu="wd",eastdown="wu",ed="wu",southup="nd",su="nd",southdown="nu",sd="nu",westup="ed",wu="ed",westdown="eu",wd="eu",northup="sd",nu="sd",northdown="su",nd="su",up="d",u="d",down="u",d="u",enter="out",out="enter",cross="cross"}
defsearchdepth=5
do_search=function(fstep,ffail,search_ok,search_fail,depth)
walking=searchfor
if depth==nil then
_searchdepth=defsearchdepth
else
_searchdepth=depth
end
hook(hooks.steptimeout,searchfor["steptimeout"])
searchfor["init"]()
searchfor["ok"]=search_ok
searchfor["fail"]=search_fail
walkend=searchfor["end"]
hook(hooks.step,fstep)
hook(hooks.searchfrofail,ffail)
searchfor["nextroom"]=_roomid
run("l")
end
searchfor={}
_searchdepth=defsearchdepth
searchfor["nextroom"]=0
searchfor["steptimeout"]=function()
if hashook(hooks.steptimeout) then
searchfor["guarded"]()
end
end
searchfor["end"]=function(s)
hook(hooks.steptimeout,nil)
hook(hooks.step,nil)
hook(hooks.searchfrofail,nil)
walking=nil
EnableTriggerGroup("search",false)
if ((s~="")and(s~=nil)) then
call(searchfor[s])
end
searchfor["ok"]=nil
searchfor["fail"]=nil
end
searchfor["init"]=function()
_searchfordata={}
_searchfordata[1]=nil
_searchfordata[2]=nil
_searchfordata[3]=nil
_searchfordata[4]=nil
_searchfordata[5]=nil
_searchfordata[6]=nil--每层的出口数据,一个列表,以层为参数
_searchforback={}--每层的返回数据,一个列表,以层为参数
_searchforcount={}--每层的出口总数,一个数字,以层为参数
_searchforindex={}--每层的当前出口,一个数字,以层为参数
_searchforlevel=1 --当前层数
_stepcallback=nil
searchfor["step"]=0
_searchforcallbackfaild=nil
EnableTriggerGroup("search",true)
end
searchfor["nextcmd"]=function(exit)
if searchfor["nextroom"]~=-1 then _roomid=searchfor["nextroom"] else _roomid=-1 end
if (_searchfordata[_searchforlevel]==nil) then
_searchfordata[_searchforlevel]={}
_searchforindex[_searchforlevel]=0
_searchforcount[_searchforlevel]=0
if exit==nil then exit={} end
for k,v in pairs(exit) do
if (exitback[v]~=nil) and (exitback[exitback[searchfor["step"]]]~=exitback[v]) then
_searchforcount[_searchforlevel]=_searchforcount[_searchforlevel]+1
_searchfordata[_searchforlevel][_searchforcount[_searchforlevel]]=v
end
end
end
_searchforindex[_searchforlevel]=_searchforindex[_searchforlevel]+1
if ((_searchforindex[_searchforlevel]>_searchforcount[_searchforlevel])or(_searchforlevel>_searchdepth)) then
if (_searchforlevel==1) then
callhook(hooks.searchfrofail)
searchfor["end"]("fail")
return false
end
searchfor["step"]=_searchforback[_searchforlevel]
_searchforlevel=_searchforlevel-1
else
searchfor["step"]=_searchfordata[_searchforlevel][_searchforindex[_searchforlevel]]
_searchforlevel=_searchforlevel+1
_searchfordata[_searchforlevel]=nil
_searchforback[_searchforlevel]=exitback[searchfor["step"]]
end
if _roomid~=-1 then
searchfor["nextroom"]=getexitroom(_roomid,searchfor["step"])
else
searchfor["nextroom"]=-1
end
return true
end
searchfor["next"]=function(exit)
if _roomname~=nil and _roomname~="" then
if maze[_roomname]~=nil then
if _roomname~=mazename then
mazename=_roomname
searchfor["nextcmd"](exit)
end
mazestep=searchfor["step"]
maze[_roomname]()
return
end
end
initmaze()
if searchfor["nextcmd"](exit)~=false then
run(searchfor["step"])
end
end
searchfor["guarded"]=function()
ResetTimer("on_steptimeout")
if not(hashook(hooks.steptimeout)) then return end
if not(hashook(hooks.step)) then return end
searchfor["nextroom"]=getexitroom(searchfor["nextroom"],exitback[searchfor["step"]])
_searchforlevel=_searchforlevel-1
searchfor["next"](nil)
end
xiaoerguard=function(name, line, wildcards)
searchfor["guarded"]()
end
--------------------------------
steppath={}
steppath["maxstep"]=0
do_steppath=function(path,pstep,pfail,path_ok,path_fail,path_maxstep,path_onmaxstep)
walking=steppath
steppath["path"]=path
steppath["ok"]=path_ok
steppath["fail"]=path_fail
walkend=steppath["end"]
steppath["pstep"]=pstep
steppath["pfail"]=pfail
steppath["maxstep"]=getnum(path_maxstep)
steppath["steptype"]=0
steppath["onmaxstep"]=path_onmaxstep
EnableTriggerGroup("steppath",true)
do_walk(path[1]["loc"],steppath["arrive"],path_fail)
end
steppath["arrive"]=function()
walking=steppath
hook(hooks.steptimeout,steppath_fail)
hook(hooks.step,steppath["pstep"])
hook(hooks.maxstep,steppath["onmaxstep"])
hook(hooks.searchfrofail,steppath["pfail"])
steppath["index"]=0
steppath["step"]=nil
steppath["steppath"]=0
steppath["nextroom"]=_roomid
steppath["next"]()
if steppath["maxstep"]>1 then
steppath["steppath"]=1
steppath.resume=steppath["runmaxstep"]
doghook()
busytest(steppath["runmaxstep"])
end
end
steppath["end"]=function(s)
hook(hooks.steptimeout,nil)
hook(hooks.step,nil)
hook(hooks.maxstep,nil)
hook(hooks.searchfrofail,nil)
walking=nil
EnableTriggerGroup("steppath",false)
if ((s~="")and(s~=nil)) then
call(steppath[s])
end
steppath.resume=nil
dogunhook()
steppath["ok"]=nil
steppath["fail"]=nil
end
steppath["nextmaxstep"]=function()
if steppath["maxstep"]<2 or walking~=steppath then return end
if _roomid==-1 then
do_walk(steppath["path"][1]["loc"],steppath["arrive"],path_fail)
return
end
if (steppath["index"]>#steppath["path"]) then
steppath["end"]("ok")
return
end
busytest(steppath["runmaxstep"])
end
steppath["runmaxstep"]=function()
local steps,steptype=getmaxstepsinpath(steppath["path"],steppath["index"],steppath["maxstep"])
steppath["steptype"]=steptype
if steptype==1 then
steps=steps..walkmaxstepmcmd
end
run(steps)
end
steppath["next"]=function()
if _roomname~=nil and _roomname~="" then
if maze[_roomname]~=nil then
if _roomname~=mazename then
mazename=_roomname
steppath["index"]=steppath["index"]+1
_roomid=steppath["nextroom"]
steppath["step"]=steppath["path"][steppath["index"]]["step"]
steppath["nextroom"]=getexitroom(_roomid,steppath["step"])
end
mazestep=steppath["step"]
maze[_roomname]()
return
end
end
initmaze()
steppath["index"]=steppath["index"]+1
_roomid=steppath["nextroom"]
if (steppath["index"]>#steppath["path"])then
if steppath["maxstep"]<2 then
steppath["end"]("ok")
end
return
end
steppath["step"]=steppath["path"][steppath["index"]]["step"]
steppath["nextroom"]=getexitroom(_roomid,steppath["step"])
if steppath["maxstep"]<2 then
run(steppath["step"])
elseif steppath["steptype"]==2 then
run(walkmaxstepmcmd)
end
end
steppath["getnextroom"]=function()
if _roomname~=nil and _roomname~="" then
if maze[_roomname]~=nil then
if _roomname==mazename then
return(_roomid)
end
end
end
return steppath["nextroom"]
end
steppath_fail=function(n,l,w)
local nextsameroom=getnextsameroom(_roomid)
if nextsameroom>-1 then
steppath["index"]=nextsameroom
if (steppath["index"]==#steppath["path"]) then
steppath["end"]("ok")
return
end
steppath["step"]=steppath["path"][steppath["index"]]["step"]
steppath["nextroom"]=getexitroom(_roomid,steppath["step"])
run(steppath["step"])
else
steppath["end"]("fail")
end
end
getnextsameroom=function(thisloc)
for v=steppath["index"]+1,#steppath["path"],1 do
if steppath["path"][v]["loc"]==thisloc then return v end
end
return -1
end