|
发表于 2015-3-11 15:57:19
|
显示全部楼层
2012年的代码,现在应该不能直接用了。只提供个思路,其他请自行领悟。- room={
- new = function ()
- local _R = {}
- setmetatable(_R, {__index = room})
- return _R
- end,
- relations={},
- name = "",
- descriptions = {},
- exits = {},
- items = {},
- }
- function look (direction)
- --~ 得到direction方向上房间的look信息并返回,如果direction为空则look当前房间。返回信息举例:
- --~ "items":
- --~ 1="店小二(xiao er)"
- --~ "name"="醉仙楼"
- --~ "descriptions":
- --~ 1="方圆数百里内提起醉仙楼可以说是无人不知,无人不晓。当年苏学士云游到"
- --~ 2="此,对醉仙楼的花雕酒赞不绝口,欣然为其题匾,留下一段传遍海内的佳话,从"
- --~ 3="此醉仙楼名声大震。楼下布置简易,顾客多是匆匆的行人,买点包子、鸡腿、米"
- --~ 4="酒就赶路去了。楼上是雅座。"
- --~ "exits":
- --~ 1="up"
- --~ 2="west"
- --~ "relations":
- --~ 1="醉仙楼二楼"
- --~ 2="〓"
- --~ 3="北大街----醉仙楼"
- R = room.new()
- R.relations = {}
- R.descriptions = {}
- R.exits = {}
- R.items = {}
- r = {}
- Send("set action draw_room")
- if direction then
- run("look " .. direction)
- else
- run("look")
- end
- Send("set action q")
- Send("set action draw_end")
- l,w = wait.regexp('设定环境变量:action = "draw_room"')
- while true do
- l,w = wait.regexp(".*")
- if string.find(l, "风景要慢慢的看。") then
- wait.time(2)
- return look(direction)
- end
- if string.find(l,'设定环境变量:action = "draw_end"') or string.find(l,'设定环境变量:action = "q"') then break end
- table.insert(r, l)
- end
- --处理房间信息
- local m = { name = rex.new("^(\\S+) \\- $"),
- exits = rex.new("这里.*的出口是(.*)"),
- ex = rex.new("(\\w+)"),
- weather = rex.new("「.*」: .*。"),
- }
- for i, v in pairs(r) do
- if string.sub(v,1,1) == ">" then
- r[i] = string.sub(v, 3, string.len(v))
- end
- if m.name:match(v) then
- _, _, R.name = m.name:match(v)
- R.name = R.name[1]
- for j = 1, i-1 do
- if string.sub(r[j],3,5) == " " then
- r[j] = trim(r[j])
- if r[j] ~= nil and r[j] ~= "" then
- table.insert(R.relations,r[j])
- end
- end
- end
- for j = i+1, #r do
- if m.exits:match(r[j]) then
- while m.ex:match(r[j]) do
- local _, _, ex = m.ex:match(r[j])
- if ex == nil then break end
- ex = ex[1]
- --~ print(ex)
- local pos_start, pos_end = string.find(r[j],ex)
- --~ r[j], _ = string.gsub(r[j], ex, "")
- r[j] = string.sub(r[j], pos_end + 1, string.len(r[j]))
- if not is_Special_exits(ex) then
- table.insert(R.exits,ex)
- end
- end
- --武当广场的出口太多会被分成两行
- if R.name == "武当广场" then
- print("对武当广场的特殊处理!")
- table.insert(R.exits, "southeast")
- table.insert(R.exits, "southdown")
- end
- table.sort(R.exits, function (v1,v2) return precede_exits[v1]
- for k = i+1, j-1 do
- r[k]=trim(r[k])
- if r[k] ~= nil and r[k] ~= "" then
- table.insert(R.descriptions, r[k])
- end
- end
- if #R.descriptions - 1 > 0 and m.weather:match(R.descriptions[#R.descriptions - 1]) then
- table.remove(R.descriptions, #R.descriptions - 1)
- end
- if m.weather:match(R.descriptions[#R.descriptions]) then
- table.remove(R.descriptions, #R.descriptions)
- end
- for k = j+1, #r do
- r[k]=trim(r[k])
- if r[k] ~= nil and r[k] ~= "" then
- table.insert(R.items, string.lower(r[k]))
- end
- end
- end
- end
- break
- end
- end
- return R
- end
复制代码 |
|