-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
67 lines (57 loc) · 1.87 KB
/
Copy pathmain.lua
File metadata and controls
67 lines (57 loc) · 1.87 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
local runtime = require 'zilscript.runtime'
local test_format = require 'zilscript.test_format'
local story_module = arg[1] or "infocom.zork1.zork1"
-- Create game environment
local env = runtime.create_game_env()
-- Load bootstrap
if not runtime.init(env) then
os.exit(1)
end
-- Install ZIL support and load modules
env.require('zilscript')
if not runtime.load_modules(env, { story_module }, {save_lua = true}) then
os.exit(1)
end
local esc = "\27["
local function highlight(text)
if type(env.DESCS) == "table" then
for _, dir in ipairs(env.DESCS) do
local fmt = esc .. "1;32m%s" .. esc .. "0m"
local cap = dir:sub(1,1):upper() .. dir:sub(2)
text = text:gsub("(%f[%a]" .. dir .. "%f[%A])", function(m) return fmt:format(m) end)
text = text:gsub("(%f[%a]" .. cap .. "%f[%A])", function(m) return fmt:format(m) end)
end
end
if type(env.DIRS) == "table" then
for _, dir in ipairs(env.DIRS) do
local fmt = esc .. "1;36m%s" .. esc .. "0m"
local cap = dir:sub(1,1):upper() .. dir:sub(2)
text = text:gsub("(%f[%a]" .. dir .. "%f[%A])", function(m) return fmt:format(m) end)
text = text:gsub("(%f[%a]" .. cap .. "%f[%A])", function(m) return fmt:format(m) end)
end
end
return text
end
-- Create env as a coroutine
local game = runtime.create_game(env)
-- Start the game and get initial output
local res = game:start()
io.write(highlight(res))
local input
repeat
input = io.read()
if input then
io.write("\n")
res = game:resume(input)
-- Check if result is a test response (table with status)
if type(res) == "table" and res.status then
io.write(test_format.format_test_result(res) .. "\n")
else
io.write(highlight(res))
end
end
until not input or not game:is_running()
-- local ast = parser.parse_file "infocom/zork1/actions.zil"
-- local res = compiler.compile(ast)
-- print(parser.view(ast, 0))
-- print(res.body)