-
Notifications
You must be signed in to change notification settings - Fork 3
/
intown.lic
576 lines (517 loc) · 19.8 KB
/
intown.lic
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
=begin
Stop scripts and run other commands when you have been in town (or in certain out-of-town rooms) for a set amount of time.
Unless configured otherwise, you are 'in town' if:
Your current room's location is one of the town locations (e.g. "Wehnimer's Landing")
Your current room is a node or supernode.
Your current room is tagged as voln
Your current room name ends with "Table]"
By default, kills ;spellactive after being in town for 30 seconds.
For details, see ;intown help
author: LostRanger ([email protected])
game: any
tags: utility
required: Lich >= 4.6.0.
version: 0.1 (2019-09-27)
changelog:
version 0.1 (2019-09-27)
* Initial release; experimental
=end
module InTownScript
VERSION = '0.1 (2019-09-27) EXPERIMENTAL'
VERSION_INT = 1_000
CONFIG_VERSION = 1
DEFAULT_TAGS = %w(node supernode town voln)
DEFAULT_DELAY = 30
TOWN_EXACT_LOCATIONS = Set.new(
[
"Cysaegir",
"Icemule Trace",
"Kharam-Dzu",
"Mist Harbor",
"Moonshine Manor",
"Solhaven",
"Ta'Illistim Embassies",
"Ta'Illistim",
"Ta'Vaalor Inns",
"Ta'Vaalor",
"Wehnimer's Landing",
"Zul Logoth",
"the town of Icemule Trace",
"the town of Kharam-Dzu",
"the town of River's Rest",
"the town of Solhaven",
"the town of Sylvarraend",
"the town of Wehnimer's Landing",
"the village of Cysaegir",
"the city of Ta'Illistim",
"the city of Ta'Vaalor",
"the hamlet of Ravelin",
"the Isle of Four Winds", # Because Mist Harbor...
]
)
TOWN_LOCATION_PATTERN = /inside the (?:.* town|glacier-locked|elven city|elven fortress)|Guild$/
# Because room descriptions are disabled in TSC all the time...
TOWN_ROOMS = Set.new(
[
"[Town Square Central]"
]
)
def self.echo(what)
respond "[#{@script.name}: #{what}]"
end
def self.send_formatted(msg, mono=true)
unless XMLData.send_fake_tags
msg = REXML::Text.new(msg, respect_whitespace: true).to_s
msg.gsub!(/\*\*(.*?)\*\*/, '<preset id="whisper">\1</preset>')
msg = "<output class=\"mono\" />\n#{msg}\n<output class=\"\" />" if mono
puts msg
else
msg.gsub!('**', '')
respond msg
end
end
def self.send_help_formatted(msg)
unless XMLData.send_fake_tags
msg = REXML::Text.new(msg, respect_whitespace: true).to_s
msg.gsub!(/\*\*(.*?)\*\*/, '<preset id="whisper">\1</preset>')
msg.gsub!(/`(.*?)`/, '<preset id="speech">\1</preset>')
msg.gsub!("#{name} help", "</preset><d cmd=\"#{name} help\">#{name} help</d><preset id=\"whisper\">")
puts "<output class=\"mono\" />\n#{msg}\n<output class=\"\" />"
else
msg.gsub!('**', '')
msg.gsub!('`', '')
respond msg
end
end
def self.show_changelog
prev = GameSettings[:current_version] || 0
changelog = {
1_000 => [
"Version 0.1 changes:",
'',
" * Dummy changelog entry.",
],
}
#
# if prev != VERSION_INT
# msg = []
# msg << "<preset id='speech'>--------------------</preset>"
# msg << "<preset id='speech'>Welcome to #{$lich_char}#{@script.name}</preset> <preset id='whisper'>#{VERSION}</preset>"
# msg << ''
# changelog.each do |version, notes|
# if version > prev or version == VERSION_INT
# msg << "<preset id='whisper'>#{notes[0]}</preset>"
# msg += notes[1..-1]
# msg << ''
# end
# end
# msg << '<output class="mono" />'
# msg << "For a more detailed changelog: <d>#{$lich_char}repo info #{@script.name}</d>."
# msg << "To enable automatic updates: <d>#{$lich_char}repo set-updatable #{@script.name}</d>`."
# msg << "For details on new features: <d>#{$lich_char}#{@script.name} help</d>"
# msg << " or visit: #{HELP_URL}"
# msg << "For more LostRanger scripts: https://bit.ly/lostranger-scripts"
# msg << '<output class="" />'
# msg << ''
# msg << "This message will only be displayed once unless a new version is downloaded."
# msg << "<preset id='speech'>--------------------</preset>"
# send msg
# GameSettings[:current_version] = VERSION_INT
# sleep 1.5
# end
end
def self.send(what)
unless XMLData.send_fake_tags
return _respond(what)
end
unless what.is_a?(Array)
what = [what]
end
return _respond what.map{|line|
if line =~ /^\s*<\/?output[^>]*>$/
nil
else
line = line.gsub(/<preset.*?>(.*?)<\/preset>/, "\\1")
line.gsub!('<pushBold/>', monsterbold_start)
line.gsub!('<popBold/>', monsterbold_end)
line.gsub!(/<\/?d.*?>/, '')
line
end
}.keep_if{|x| x}
end
def self.show_help
script = "#{$lich_char}#{@script.name}"
msg = []
msg << "**This is #{script} version #{VERSION}**"
msg << "**Basic Usage:**"
msg << " **#{script}**"
msg << " Start this script."
msg << " NOTE: In-Town actions will not trigger until you have left an area considered 'in town' and returned."
msg << ''
msg << " **#{script} NOW**"
msg << " Trigger all In-Town Actions now."
msg << ''
msg << " **#{script} DELAY** `[x]`"
msg << " Sets or shows the delay (in seconds) before In-Town Actions trigger."
msg << " A delay of 0 seconds can be used to trigger actions immediately."
msg << " Default: #{script} delay #{DEFAULT_DELAY}"
msg << ''
msg << " **#{script} TAGS** [`tags`]"
msg << " Sets or shows which tags trigger In-Town status."
msg << " Default: #{script} tags #{DEFAULT_TAGS.join(' ')}"
msg << ''
msg << " **#{script} TABLES** [**ON**|**OFF**]"
msg << " Set whether tables should be considered In-Town rooms."
msg << " Default: #{script} tables on"
msg << ''
msg << "**Command List:**"
msg << " Commands that manipulate the command list take an optional `[#]`, which inserts the command at "
msg << " the specified position on the list. See **#{script} ADD** for details."
msg << ''
msg << " **#{script} LIST**"
msg << " Show the current command list and all setings."
msg << ''
msg << " **#{script} DELETE** `#`"
msg << " Delete a command from the command list. (Note: subsequent commands will be renumbered.)"
msg << ''
msg << " **#{script} ADD** `[#]` `COMMAND`"
msg << " Adds a command to the command list. This command will be run using Lich's normal command"
msg << " mechanism ('fput'), which will account for RT and typeahead but cannot trigger aliases, scripts"
msg << " or Lich commands."
msg << " If an optional `[#]` number is specified, the command will be inserted into that position of the"
msg << " command list. Otherwise, it is added at the end."
msg << " Example: #{script} add 1 say I'm Home! (inserts to the beginning of the list)"
msg << " Example: #{script} add sit (appends to the end of the list)"
msg << ''
msg << " **#{script} RUN** `[#]` `COMMAND`"
msg << " Adds a command to the command list, which is sent as if it was typed in your client."
msg << " This can trigger aliases, start scripts, and run other Lich commands, but it does NOT account"
msg << " for typeahead or RT."
msg << " Example: #{script} run ;enhance off"
msg << ''
msg << " **#{script} KILL** `[#]` `SCRIPT`"
msg << " Kill the specified script if it is running."
msg << " Example: #{script} kill spellactive"
msg << ''
msg << " **#{script} PAUSE** `[#]` `SCRIPT`"
msg << " Pause the specified script if it is running."
msg << " Example: #{script} kill spellactive"
return send_help_formatted(msg.join("\n"))
end
def self.cmd_list
script = "#{$lich_char}#{@script.name}"
msg = []
commands = CharSettings[:commands]
tags = CharSettings[:tags]
tables = CharSettings[:tables]
if commands and commands.length > 0 then
msg << "Current command list:"
ix = 0
commands.each do |command, args|
ix += 1
msg << " #{ix.to_s.rjust(2)}: #{script} #{command.to_s} #{args}"
end
else
msg << "No commands are currently defined."
end
msg << ""
msg << "You must remain in town for #{CharSettings[:delay]} seconds before commands will trigger."
if tags and tags.length > 0 then
msg << "Rooms with the following tags are also considered as in-town: #{tags.join(' ')}"
end
if tables
msg << "Tables are also considered as in-town."
else
msg << "Tables are NOT considered as in-town."
end
msg << "See `#{script} help` for details."
respond msg
end
def self.cmd_delay(args)
unless args
echo "Current delay is #{CharSettings[:delay]} second(s)."
echo "Use #{$lich_char}#{@script.name} DELAY x to set a delay of x seconds."
return
end
unless args =~ /^\d+/
echo "Delay must be an integer 0 or greater."
return
end
CharSettings[:delay] = args.to_i
echo "Delay set to #{CharSettings[:delay]} second(s)."
end
def self.cmd_tags(args)
unless args
tags = CharSettings[:tags]
if tags and tags.length > 0
echo "Rooms with the following tags are considered as in-town: #{tags.join(' ')}"
else
echo "No room tags are currently considered as in-town."
end
echo "Use #{$lich_char}#{@script.name} TAGS tag1 tag2 tag3... to set a list of tags as being in-town."
echo "Use #{$lich_char}#{@script.name} TAGS NONE to remove the list of in-town room tags."
return
end
if args.downcase == 'none'
tags = CharSettings[:tags] = []
echo "List of room tags cleared."
else
tags = CharSettings[:tags] = args.split(/[\s;,]+/)
echo "List of room tags set to: #{tags.join(' ')}"
end
@mutex.synchronize {
@tags = Set.new(tags)
}
end
def self.cmd_tables(args)
unless args
if CharSettings[:tables]
echo "Tables are considered as in-town."
echo "Use #{$lich_char}#{@script.name} TABLES OFF to disable this setting."
else
echo "Tables are NOT considered as in-town."
echo "Use #{$lich_char}#{@script.name} TABLES ON to enable this setting."
end
return
end
args = args.downcase
if args == 'on'
CharSettings[:tables] = true
echo "Tables are now considered as being in-town."
elsif args == 'off'
CharSettings[:tables] = false
echo "Tables are no longer considered as being in-town."
end
end
def self.cmd_delete(args)
n = args.to_i
if n < 1
echo "Usage: `#{$lich_char}#{@script.name} DELETE #` to delete a command from the command list."
echo "Use `#{$lich_char}#{@script.name} LIST` to see the current commands list."
elsif n > CharSettings[:commands].length
echo "No such command ##{n}."
echo "Use `#{$lich_char}#{@script.name} LIST` to see the current commands list."
else
cmd = CharSettings[:commands][n-1]
CharSettings[:commands].delete_at(n-1)
echo "Deleted command: #{cmd[0].to_s} #{cmd[1]}"
end
end
def self.cmdlist_command(cmd, sym, args, argname='COMMAND')
n = nil
if args
if args =~ /^(\d+)\s*(.*)\s*/
n = $1.to_i
args = $2
end
end
unless args
echo "Usage: `#{$lich_char}#{@script.name} #{cmd} #{argname}` to add to the end of the command list."
echo "Usage: `#{$lich_char}#{@script.name} #{cmd} [#] #{argname}` to insert at the specified position of the command list."
return
end
if n and n > CharSettings[:commands].length
n = nil
end
if n
CharSettings[:commands].insert(n-1, [sym, args])
echo "Command inserted at position ##{n}"
else
CharSettings[:commands] << [sym, args]
echo "Command inserted at position ##{CharSettings[:commands].length}"
end
end
def self.do_command(input)
command, args = input.split(/\s+/, 2)
command.downcase!
case command
when 'list'
cmd_list
when 'help'
show_help
when 'now'
cmd_now
when 'delay'
cmd_delay(args)
when 'tag', 'tags'
cmd_tags(args)
when 'table', 'tables'
cmd_tables(args)
when 'del', 'delete', 'remove'
cmd_delete(args)
when 'add'
cmdlist_command('add', :add, args)
when 'run'
cmdlist_command('run', :run, args)
when 'kill'
cmdlist_command('kill', :run, args, 'SCRIPT')
when 'pause'
cmdlist_command('pause', :pause, args, 'SCRIPT')
end
end
def self.execute_commands
CharSettings[:commands].each do |command, args|
case command
when :add
fput args
when :run
do_client args
when :kill
Script.kill(args) if Script.running?(args)
when :pause
Script.pause(args) if Script.running?(args) and not Script.paused?(args)
end
end
end
def self.cmd_now
@mutex.synchronize do
@in_town = true
@in_town_at = nil
execute_commands
end
end
def self.run(script)
@opened_locker = false
@script = script
# @script.want_downstream = false
# @script.want_downstream_xml = true
show_changelog
prev_config_version = (CharSettings[:config_version] || 0)
if prev_config_version == 0
echo "This appears to be my first time running on this character. Initializing default settings."
elsif prev_config_version > CONFIG_VERSION
echo "Downgraded from config version #{prev_config_version} to #{CONFIG_VERSION}. Resetting settings to defaults."
prev_config_version = 0
else
# TODO: Version upgrades here
end
CharSettings[:config_version] = CONFIG_VERSION
if prev_config_version == 0
CharSettings[:delay] = 30
CharSettings[:tags] = DEFAULT_TAGS
CharSettings[:tables] = true
CharSettings[:commands] = [
[:kill, 'spellactive']
]
cmd_list
end
@tags = Set.new(CharSetting[:tags])
@mutex = Mutex.new
if script.vars[0]
do_command(script.vars[0])
exit
end
echo "version #{VERSION} starting."
@in_town = self.in_town?
@in_town_at = nil
if @in_town
echo "You appear to be starting this script while in town. Actions will not trigger unless you leave town and re-enter it."
echo "To trigger actions now, do `#{$lich_char}#{script.name} now`"
end
@timer_thread = nil
@watcher_thread = nil
before_dying do
@timer_thread.kill if @timer_thread
@watcher_thread.kill if @watcher_thread
@timer_thread = @watcher_thread = nil
UpstreamHook.remove("intown-command-hook")
end
command_pattern = /(?:<c>)?\s*#{Regexp::escape("#{$lich_char}#{script.name}")}(?:\s+(.*))?$/
UpstreamHook.add("intown-command-hook", proc{|line|
next line unless line =~ command_pattern
unless $1
echo "already running."
next nil
end
@script.unique_buffer.push($1)
# do_command($1)
next nil
})
@timer_thread = Thread.new {
while true
duration = nil
@mutex.synchronize {
if @in_town or @in_town_at.nil?
duration = nil
else
duration = @in_town_at - Time.now
end
if duration and duration <= 0
@in_town = in_town?
@in_town_at = nil
if @in_town
# respond "Triggering!"
execute_commands
end
duration = nil
end
}
if duration.nil?
# respond "Sleeping forever"
sleep
else duration > 0
# respond "Sleeping #{duration}s"
sleep duration
end
end
}
# Room watching thread.
@watcher_thread = Thread.new {
n = $room_count
while true
get
next if n == $room_count
n = $room_count
check_in_town
end
}
while line = unique_get
do_command(line)
end
end
def self.check_in_town
@mutex.synchronize {
now_in_town = self.in_town?
unless now_in_town
@in_town = false
@in_town_at = nil
return
end
return if @in_town
# Skip clock shenanigans if delay is 0.
if CharSettings[:delay] == 0
@in_town = true
@in_town_at = nil
execute_commands
return
end
# Do nothing if in_town_at is already set.
return if @in_town_at
@in_town_at = Time.now + CharSettings[:delay]
echo "You appear to be in town. Triggering in-town actions in #{CharSettings[:delay]} seconds."
@timer_thread.run
# else
# @in_town = false
# @in_town_at = nil
# end
}
end
def self.in_town?
title = XMLData.room_title
return true if title.end_with?(" Table]") and CharSettings[:tables]
return true if TOWN_ROOMS.include?(title)
rm = Room.current
return false unless rm and rm.id
if rm.location
return true if TOWN_EXACT_LOCATIONS.include?(rm.location)
return true if TOWN_LOCATION_PATTERN =~ rm.location
end
return false unless rm.tags
return false unless rm.tags.length > 0
rm.tags.each do |tag|
return true if @tags.include?(tag)
end
return false
end
end
InTownScript.run(script)