forked from matt-lowe/lich-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiz_to_lich.lic
More file actions
199 lines (180 loc) · 6.23 KB
/
wiz_to_lich.lic
File metadata and controls
199 lines (180 loc) · 6.23 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
=begin
This here thing here tries to turn Wizard and Stormfront scripts into Lich scripts.
Place the script in your Lich script directory and do ";wiz_to_lich scriptname.cmd"
and it will create scriptname.lic.
author: Tillmen (tillmen@lichproject.org)
game: any
tags: core
version: 0.2
required: Lich >= 4.6.0
changelog:
0.2 (2015-11-06):
don't kill the converted script when it starts a Lich script
=end
unless $SAFE == 0
echo "this script must be trusted to work (;trust #{script.name})"
exit
end
if script.vars[1].nil? or (script.vars[1] =~ /^help$/i)
respond
respond "Usage: ;wiz_to_lich <scriptname.cmd>"
respond
exit
end
if File.exists?(script.vars[1])
file_name = script.vars[1]
elsif File.exists?($script_dir + script.vars[1])
file_name = $script_dir + script.vars[1]
else
echo "file does not exist: #{script.vars[1]}"
exit
end
out_file_name = file_name.sub(/cmd$|wiz$/i, 'lic')
if File.exists?(out_file_name)
echo "output file already exists: #{out_file_name}"
exit
end
input_lines = File.open(file_name) { |f| f.readlines }.collect { |l| l.chomp }
counter_action = {
'add' => '+',
'sub' => '-',
'subtract' => '-',
'multiply' => '*',
'divide' => '/',
'set' => ''
}
setvars = Array.new
input_lines.each { |line|
begin
# respond line.inspect
setvars.push($1) if line =~ /[\s\t]*setvariable\s+([^\s\t]+)[\s\t]/i and not setvars.include?($1)
rescue
respond $!
respond $!.backtrace[0..1]
respond line.inspect
exit
end
}
has_counter = input_lines.find { |line| line =~ /%c/i }
has_nextroom = input_lines.find { |line| line =~ /nextroom/i }
fixstring = proc { |str|
while not setvars.empty? and (str =~ /%(#{setvars.join('|')})%/io)
str.gsub!('%' + $1 + '%', '#{' + $1.downcase + '}')
end
str.gsub!(/%c(?:%)?/i, '#{c}')
str.gsub!(/%s(?:%)?/i, '#{Settings[\'sav\']}')
while str =~ /%([0-9])(?:%)?/
str.gsub!(/%#{$1}(?:%)?/, '#{script.vars[' + $1 + ']}')
end
str
}
fixline = proc { |line|
if line =~ /^[\s\t]*[A-Za-z0-9_\-']+:/i
line = line.downcase.strip
elsif line =~ /^([\s\t]*)counter\s+(add|sub|subtract|divide|multiply|set)\s+([0-9]+)/i
line = "#{$1}c #{counter_action[$2]}= #{$3}"
elsif line =~ /^([\s\t]*)counter\s+(add|sub|subtract|divide|multiply|set)\s+(.*)/i
indent, action, arg = $1, $2, $3
line = "#{indent}c #{counter_action[action]}= #{fixstring.call(arg.inspect)}.to_i"
elsif line =~ /^([\s\t]*)save[\s\t]+"?(.*?)"?[\s\t]*$/i
indent, arg = $1, $2
line = "#{indent}Settings['sav'] = #{fixstring.call(arg.inspect)}"
elsif line =~ /^([\s\t]*)echo[\s\t]+(.+)/i
indent, arg = $1, $2
line = "#{indent}echo #{fixstring.call(arg.inspect)}"
elsif line =~ /^([\s\t]*)waitfor[\s\t]+(.+)/i
indent, arg = $1, $2
line = "#{indent}waitfor #{fixstring.call(Regexp.escape(arg).inspect.gsub("\\\\ ", ' '))}"
elsif line =~ /^([\s\t]*)put[\s\t]+\.(.+)$/i
indent, arg = $1, $2
if arg.include?(' ')
line = "#{indent}Script.start(#{fixstring.call(arg.slice(/^[^\s]+/).inspect)}, #{fixstring.call(arg.sub(/^[^\s]+\s+/,'').inspect)})\n#{indent}exit"
else
line = "#{indent}Script.start(#{fixstring.call(arg.inspect)})\n#{indent}exit"
end
elsif line =~ /^([\s\t]*)put[\s\t]+;(.+)$/i
indent, arg = $1, $2
if arg.include?(' ')
line = "#{indent}Script.start(#{fixstring.call(arg.slice(/^[^\s]+/).inspect)}, #{fixstring.call(arg.sub(/^[^\s]+\s+/,'').inspect)})"
else
line = "#{indent}Script.start(#{fixstring.call(arg.inspect)})"
end
elsif line =~ /^([\s\t]*)(put|move)[\s\t]+(.+)/i
indent, cmd, arg = $1, $2, $3
line = "#{indent}waitrt?\n#{indent}clear\n#{indent}#{cmd.downcase} #{fixstring.call(arg.inspect)}"
elsif line =~ /^([\s\t]*)goto[\s\t]+(.+)/i
indent, arg = $1, $2
line = "#{indent}goto #{fixstring.call(arg.downcase.inspect)}"
elsif line =~ /^([\s\t]*)waitforre[\s\t]+(.+)/i
indent, arg = $1, $2
line = "#{indent}waitforre #{arg}"
elsif line =~ /^([\s\t]*)pause[\s\t]*(.*)/i
indent, arg = $1, $2
arg = '1' if arg.empty?
arg = '0'+arg.strip if arg.strip =~ /^\.[0-9]+$/
line = "#{indent}pause #{arg}"
elsif line =~ /^([\s\t]*)match[\s\t]+([^\s\t]+)[\s\t]+(.+)/i
indent, label, arg = $1, $2, $3
line = "#{indent}match #{fixstring.call(label.downcase.inspect)}, #{fixstring.call(Regexp.escape(arg).inspect.gsub("\\\\ ", ' '))}"
elsif line =~ /^([\s\t]*)matchre[\s\t]+([^\s\t]+)[\s\t]+(.+)/i
indent, label, regex = $1, $2, $3
line = "#{indent}matchre #{fixstring.call(label.downcase.inspect)}, #{regex}"
elsif line =~ /^([\s\t]*)setvariable[\s\t]+([^\s\t]+)[\s\t]+(.+)/i
indent, var, arg = $1, $2, $3
line = "#{indent}#{var.downcase} = #{fixstring.call(arg.inspect)}"
elsif line =~ /^([\s\t]*)deletevariable[\s\t]+(.+)/i
line = "#{$1}#{$2.downcase} = nil"
elsif line =~ /^([\s\t]*)nextroom\b/i
line = "#{$1}nextroom.call"
elsif line =~ /^([\s\t]*)(wait|exit|echo)\b/i
line = "#{$1}#{$2.downcase}"
elsif line =~ /^([\s\t]*)matchwait\b/i
line = "#{$1}matchwait"
elsif line =~ /^([\s\t]*)if_([0-9])[\s\t]+(.*)/i
indent, num, stuff = $1, $2, $3
line = "#{indent}if script.vars[#{num}]\n#{indent}\t#{fixline.call($3)}\n#{indent}end"
elsif line =~ /^([\s\t]*)shift\b/i
line = "#{$1}script.vars.shift"
else
echo "unknown line: #{line}"
line = '#' + line
end
}
lich_block = false
input_lines.each_index { |idx|
if lich_block
if input_lines[idx] =~ /\}[\s\t]*LICH[\s\t]*$/
input_lines[idx] = input_lines[idx].sub(/\}[\s\t]*LICH[\s\t]*$/, '')
lich_block = false
else
next
end
elsif input_lines[idx] =~ /^[\s\t]*#|^[\s\t]*$/
next
elsif input_lines[idx] =~ /^[\s\t]*LICH[\s\t]*\{/
input_lines[idx] = input_lines[idx].sub(/LICH[\s\t]*\{/, '')
if input_lines[idx] =~ /\}[\s\t]*LICH[\s\t]*$/
input_lines[idx] = input_lines[idx].sub(/\}[\s\t]*LICH[\s\t]*$/, '')
else
lich_block = true
end
else
input_lines[idx] = fixline.call(input_lines[idx])
end
}
if has_counter or has_nextroom
input_lines.each_index { |idx|
next if input_lines[idx] =~ /^[\s\t]*#/
input_lines.insert(idx, '')
if has_counter
input_lines.insert(idx, 'c = 0')
end
if has_nextroom
input_lines.insert(idx, "nextroom = proc { room_count = XMLData.room_count; wait_while { room_count == XMLData.room_count } }")
end
input_lines.insert(idx, '')
break
}
end
File.open(out_file_name, 'w') { |f| f.write input_lines.join("\n").concat("\n") }
echo 'done'