Module: Lich::DragonRealms::DRC

Defined in:
documented/dragonrealms/commons/common.rb

Overview

DragonRealms Commons: shared utility methods for scripts running in GemStone IV's DragonRealms shard. Methods here handle command-sending with intelligent retries, item manipulation, room parsing, stance, music, and orchestration of multiple scripts. All methods are module functions (class methods).

Defined Under Namespace

Classes: Item

Constant Summary collapse

XML_TAG_PATTERN =

Pattern for XML tags

/<[^>]+>/.freeze
WAIT_RESPONSE_PATTERN =

Pattern for game wait/roundtime responses in bput; shared with fput, see lib/common/wait_response.rb.

Lich::Common::WAIT_RESPONSE_PATTERN
COLLECT_CLUTTER_ATTEMPTS =

Most collect attempts per DRC.collect call while the room is too cluttered. Chosen to equal DRC.forage?'s default tries (5), which is its own literal and does not follow this constant.

5
COLLECT_MESSAGES =

Collect command response messages

[
  'As you rummage around',
  'believe you would probably have better luck trying to find a dragon',
  'if you had a bit more luck',
  'The room is too cluttered',
  'one hand free to properly collect',
  'You are sure you knew',
  'You begin to forage around,',
  'You begin scanning the area before you',
  'You begin exploring the area, searching for',
  'You find something dead and lifeless',
  'You cannot collect anything',
  'you fail to find anything',
  'You forage around but are unable to find anything',
  'You manage to collect a pile',
  'You survey the area and realize that any collecting efforts would be futile',
  'You wander around and poke your fingers',
  'You forage around for a while and manage to stir up a small mound of fire ants!'
].freeze
RETREAT_ESCAPE_MESSAGES =

Retreat command response patterns

[
  /You are already as far away as you can get/,
  /You retreat from combat/,
  /You sneak back out of combat/,
  /Retreat to where/,
  /There's no place to retreat to/
].freeze
RETREAT_MESSAGES =

Patterns matched during a RETREAT command to detect a partial retreat (e.g. pushed back, attempt blocked), as opposed to escape patterns that indicate successful exit from combat.

Returns:

  • (Array<Regexp>) —

    ordered patterns indicating retreat state changes

See Also:

[
  /retreat/,
  /sneak/,
  /grip on you/,
  /grip remains solid/,
  /You try to back/,
  /You must stand first/,
  /You stop advancing/,
  /You are already/
].freeze
BPUT_DEFAULT_TIMEOUT =

Seconds bput waits for a matching reply when the caller passes no 'timeout'. retreat also uses it as its hold budget, so a hold costs about as long as one RETREAT that drew no recognised reply.

15
ASSESS_TEACH_TEACHER_PATTERN =

Assess teach parsing patterns

/(?<teacher>.*) is teaching a class on (?<skill>.*) which is still open to new students/.freeze
ASSESS_TEACH_SKILL_FILTER_PATTERN =

Pattern to extract the filtered skill name from an ASSESS TEACH line. Matches lines like "Magic (compared to what you already know) Sorcery".

Examples:

Match and extract skill

line = "Advanced Magic (compared to what you already know) Sorcery"
match = line.match(ASSESS_TEACH_SKILL_FILTER_PATTERN)
match[:filtered_skill] #=> "Sorcery"

Returns:

  • (Regexp) —

    pattern with a named capture (?<filtered_skill>) for the skill name

/.* \(compared to what you already know\) (?<filtered_skill>.*)/.freeze
COMMON_RANGED_WEAPONS_PATTERN =

Common ranged weapon nouns

/^(bow|shortbow|longbow|crossbow|stonebow|latchbow|slurbow|lockbow|pelletbow|arbalest|sling|slingshot|blowgun)$/i.freeze
RACIAL_RANGED_WEAPONS_PATTERN =
/^(jranoki|uku'uan|uku'uanstaho|chunenguti|hhr'ibu|guti|mahil|taisgwelduan|chyeb|sverfil|tangara|alaer|kari|wami|usus|srigos|href|vrope|falocisana|stof|dzelt)$/i.freeze
FLAVOR_TEXT_PATTERN =

Flavor text pattern for item descriptions https://regex101.com/r/4lGY6u/13

/\s?\b(?:(?:colorfully and )?(?:artfully|artistically|attractively|beautifully|bl?ack-|cleverly|clumsily|crudely|deeply|delicately|edged|elaborately|faintly|flamboyantly|front-|fully|gracefully|heavily|held|intricately|lavishly|masterfully|plentifully|prominantly|roughly|securely|sewn|shabbily|shadow-|simply|somberly|skillfully|sloppily|starkly|stitched|tied and|tightly|well-)\s?)?(?:accented|accentuated|acid-etched|adorned|affixed|appliqued|assembled|attached|augmented|awash|backed|back-laced|balanced|banded|batiked|beaded|bearded|bearing|bedazzled|bedecked|bejeweled|beset|bestrewn|blazoned|bordered|bound|braided|branded|brocaded|bristling|brushed|buckled|burned|buttoned|caked|camouflaged|capped|carved|caught|centered|chased|chiseled|cinched|circled|clasped|cloaked|closed|coated|cobbled together|coiled|colored|composed|concealed|connected|constructed|countoured|covered|crafted|crested|crisscrossed|crowded|crowned|cuffed|cut|dangling|dappled|decked|decorated|deformed|depicting|designed|detailed|discolored|displaying|divided|done|dotted|draped|drawn|dressed|drizzled|dusted|edged|elaborately|embedded|embell?ished|emblazed|emblazoned|embossed|embroidered(?: all over| painstakingly)?|enameled(?: across)?|encircled|encrusted|engraved|engulfed|enhanced|entwined|equipped|etched|fashioned(?: so)?|fastened|feathered|featuring|festooned|fettered|filed|filled|firestained|fit|fitted|fixed|flecked|fletched|forged|formed|framed|fringed|frosted|full|gathered|gleaming|glimmering|glittering|goldworked|growing|gypsy-set|hafted|hand-tooled|hanging|heavily(?:-beaded| covered)?|held fast|hemmed|hewn|hideously|highlighted|hilted|honed|hung|impressed|incised|ingeniously repurposed|inscribed|inlaid|inset|interlaced|interspersed|interwoven|jeweled|joined|laced(?: up)?|lacquered|laden|layered|limned|lined|linked|looped|knotted|made|marbled|marked|marred|meshed|mosaicked|mottled|mounted|oiled|oozing|outlined|ornamented|overlai(?:d|n)|padded|painted|paired|patched|pattern-welded|patterned|pinned|plumed|polished|printed|reinforced|reminiscent|rendered|revealing|riddled|ridged|rimed|ringed|riveted|sashed|scarred|scattered|scorched|sculpted|sealed|seamed|secured|securely|set|sewn|shaped|shimmering|shod|shot|shrouded|side-laced|slashed|slung|smeared|smudged|spangled|speckled|spiraled|splatter-dyed|splattered|spotted|sprinkled|stacked|surmounted|surrounded|suspended|stained|stamped|starred|stenciled|stippled|stitched(?: together)?|strapped|streaked|strengthened|strewn|striated|striped|strung|studded|swathed|swirled|tailored|tangled|tapered|tethered|textured|threaded|tied|tightly|tinged|tinted|tipped|tooled|topped|traced|trimmed|twined|veined|vivified|washed|webbed|weighted|whorled|worked|worn|woven|wrapped|wreathed|wrought)?\b ["]?\b(?:a hand-tooled|across|along|an|around|atop|bearing|belted|bright streaks|dangling|designed|detailing|down (?:each leg|one side)|dyed (?:a|and|deep|of|in|night|rust|shimmering|the|to|with)|engravings|entitled|errant pieces|featuring|flaunting|frescoed|from|Gnomish Pride|(?:encased |quartered )?in(?: the)?|into|labeled|leading|like|lining|matching|(?<!stick|slice|chunk|flask|hunk|series|set|pair|piece) of|on|out|overlayed gleaming silver|resembling|shades of color|sporting|surrounding|that|the|through|tinged somber black|titled|to|upon|WAR MONGER|with|within|\b(?:at|bearing|(?:accented |held |secured )?by|carrying|clutching|colored|cradling|dangling|depicting|(?:prominently )?displaying|embossed|etched|featuring|for(?:ming)?|holding|(?<!slice |chunk |flask |hunk |series |set |pair |piece )of|over|patterned|striped|suspending|textured|that)\b \b(?:a (?:band|beaded|brass|cascade|cluster|coral|crown|dead|.+ (?:ingot|boulder|stone|rock|nugget)|fierce|fanged|fringe|glowing|golden|grinning|howling|large|lotus|mosaic|pair|poorly|rainbow|roaring|row|silver(?:y|weave)?|small|snarling|spray|tailored|thick|tiny|trio|turquoise|yellowed)|(?:squared )?agonite (?:links|decorated)|alternating|an|(?:purple |blue )?and|ash|beaded fringe|blackened (?:steel(?: accents| bearing| with|$)|ironwood)|blue (?:gold|steel)|burnished golden|cascading layers|carved ivory|chain-lined|chitinous|(?:deep red|dull black|pale blue) cloth|cloudberry blossoms|colorful tightly|cotton candy|crimson steel|crisscrossed|curious design|curved|crystaline charm|dark (?:blue|green|grey|metals|windsteel) (?:and|exuding|glaes|hues|khor'vela|muracite|pennon|with)|dark supple|deepest|deeply blending|delicate|dusky (?:dreamweave|green-grey)|ebonwood$|emblazoned|enamel?led (?:steel|bronze)|etched|fine(?:-grained| black| crushed)|finely wrought|flame-kissed|forest|fused-together|fuzzy grey|gauze atop|gilded steel|glass eyeballs|glistening green|golden oak|grey fur|hammered|haralun|has|heavy (?:grey|pearl|silver)|horn|Ilithi cedar|inky black|interlocking silver|interwoven|iridescent|jagged interlocking plates|(?:soft dark|supple|thick|woven) (?:bolts|leather)|lightweight|long swaths|lustrous|kertig ravens|made|metal cogs|mirror-finished|mottled|multiple woods|naphtha|oak|oblong sanguine|one|onyx buttons|opposing images|overlapping|pale cerulean|pallid links|pastel-hued|pins|pitted (?:black iron|steel)|plush velvet|polished (?:bronze|hemlock|steel)|raccoon tails|ram's horns|rat pelts|raw|red and blue|rich (?:purple|golden)|riveted bindings|roughened|rowan|sanguine thornweave|scattered star|scorch marks|sculpted|shadows|shark cartilage|shifting (?:celadon|shades)|shipboard|(?:braided |cobalt |deep black |desert-tan |dusky red Taisidon |ebony |exquisite spider|fine leaf-green |flowing night|glimmering ebony |heavy |marigold |pale gold marquisette and virid |rich copper |spiral-braided |steel|unadorned black Musparan )?silk(?:cress)?|(?:coiled |shimmering )?silver(?:steel| and |y)?|sirese blue spun glitter|six crossed|slender|small bones|smoothly interlocking|snow leopard|soft brushed|somber black|sprawled|sun-bleached|steel links|stones|strips of|sunny yellow|teardrop plates|telothian|the|tiny (?:golden|indurium|scales|skull)|tightly braided|tomiek|torn|twists|two|undyed|vibrant multicolored|viscous|waves of|weighted|well-cured|white ironwood|windstorm gossamer|wintry faeweave|woven diamondwood))\b.*/.freeze
IMMOBILE_PATTERN =

The game's reply to a command while the character is held or rooted.

/^You don't seem to be able to move/.freeze
CANNOT_STAND_PATTERN =

Game responses to STAND that mean standing cannot currently succeed, so fix_standing must stop instead of looping forever (issue #3668). These are already in the STAND match list, but matching one does not change posture, so without this guard the loop spams STAND endlessly (e.g. while unconscious, plummeting, held, or overburdened).

Regexp.union(
  /unconscious|plummeting to your death|prevents you from standing|overburdened and cannot|weight of all your possessions|no room to do much of anything/,
  IMMOBILE_PATTERN
).freeze
DEFAULT_BOX_SUBSTITUTIONS =

Post-match box name rewrites (applied via gsub after a box is matched). "ironwood" -> "iron" because the game parser wants the shortened noun. Players extend this via the custom_box_substitutions setting; see box_list_to_adj_and_noun.

Returns:

  • (Array<Array(String, String)>) —

    ordered [from, to] literal pairs

[%w[ironwood iron]].freeze
DEFAULT_SCROLL_SUBSTITUTIONS_PRE =

Item-specific scroll rewrites applied before SCROLL_KEYWORD_COLLAPSE. These full game descriptions contain keywords the collapse would otherwise mangle (e.g. "icy blue vellum scroll" -> "icy scroll", not "icy blue vellum"), or must be caught before the collapse can run. Order matters and is preserved. Players extend this list via the custom_scroll_substitutions setting; see scroll_list_to_adj_and_noun.

Returns:

  • (Array<Array(String, String)>) —

    ordered [from, to] literal pairs

[
  ['large midnight-blue scale torn with symbols', 'midnight-blue scale'],
  ['icy blue vellum scroll', 'icy scroll'],
  ['green vellum scroll', 'green scroll'],
  ['fetid antelope vellum', 'antelope vellum'],
  ['papyrus roll', 'papyrus.roll'],
  ['pallid red scroll', 'pallid scroll']
].freeze
DEFAULT_SCROLL_SUBSTITUTIONS_POST =

Adjective-pair scroll rewrites applied after SCROLL_KEYWORD_COLLAPSE, reducing already-collapsed forms (e.g. "stormy grey" -> "stormy"). Order matters and is preserved. Not player-extensible (these operate on the collapsed noun, not the raw description).

Returns:

  • (Array<Array(String, String)>) —

    ordered [from, to] literal pairs

[
  ['crumpled paper', 'crumpled'],
  ['pale ricepaper', 'pale'],
  ['stormy grey', 'stormy'],
  ['mossy green', 'mossy'],
  ['dark purple', 'dark'],
  ['vibrant red', 'vibrant'],
  ['bright green', 'bright'],
  ['icy blue', 'blue'],
  ['pearl-white silk', 'silk'],
  ['ghostly white', 'white'],
  ['crinkled violet', 'crinkled'],
  ['drawing paper', 'drawing']
].freeze
SCROLL_KEYWORD_COLLAPSE =

Structural collapse: reduce " <flavor...>" to " " by keeping the noun keyword and dropping trailing flavor. Sandwiched between the pre and post substitution passes.

Returns:

  • (Regexp)
/\s(bark|leaf|ostracon|papyrus|parchment|roll|scroll|tablet|vellum|manuscript)\s.*/.freeze

Class Method Summary collapse

Class Method Details

.assess_teach ⇒ Hash{String => String}

Issues ASSESS TEACH and returns the classes being taught in the room.

Lich::Util.issue_command returns [] (never nil) when the reply does not arrive in time; that prints a bold message and returns {} like "no classes". A reply cut off before its Roundtime line prints a "cut off" message and returns the classes that did arrive, which may not be all of them. A "no one is teaching" or "you are teaching" reply returns {} with no message, whether or not its Roundtime line arrived. A complete reply does not prove it was sent in answer to this call: a reply that arrives after an earlier call timed out can still be read by the next call.

Returns:



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'documented/dragonrealms/commons/common.rb', line 952

def assess_teach
  lines, complete = capture_reply(
    'assess teach',
    /is teaching a class|No one seems to be teaching|You are teaching a class/,
    /Roundtime/,
    usexml: false
  )
  waitrt?
  if lines.empty?
    Lich::Messaging.msg("bold", "DRC: Failed to capture ASSESS TEACH output (timeout).")
    return {}
  end
  return {} if lines.any? { |l| l.match?(/No one seems to be teaching|You are teaching a class/) }

  Lich::Messaging.msg("bold", "DRC: ASSESS TEACH output was cut off (timeout); the result is incomplete.") unless complete

  parse_assess_teach_lines(lines.map(&:strip).reject(&:empty?))
end

.atmo(text, make_bold = true) ⇒ Object

Sends a message to the atmospherics window. By default the message is bold. DEPRECATED in favor of log_window



1642
1643
1644
# File 'documented/dragonrealms/commons/common.rb', line 1642

def atmo(text, make_bold = true)
  log_window(text, "atmospherics", make_bold)
end

.beep ⇒ Object

windows only I believe.



827
828
829
# File 'documented/dragonrealms/commons/common.rb', line 827

def beep
  echo("\a")
end

.bold(text) ⇒ Object

Helper function to wrap text in the necessary markup to make it render as bold in a frontend client. Used by atmo and log_window methods.



1667
1668
1669
1670
1671
# File 'documented/dragonrealms/commons/common.rb', line 1667

def bold(text)
  prefix = Frontend.supports_gsl? ? "\034GSL\r\n " : "<pushBold\/>"
  suffix = Frontend.supports_gsl? ? "\034GSM\r\n " : "<popBold\/>"
  "#{prefix}#{text}#{suffix}"
end

.box_list_to_adj_and_noun(list) ⇒ Array<String>

Take a game formatted list of boxes "a reinforced wooden strongbox and a plain ironwood crate" and return ["wooden strongbox", "iron crate"].

The recognized wood and container words are BOX_WOODS and BOX_CONTAINERS merged with the player's custom_box_woods / custom_box_containers settings, so a player can teach Lich about a box material or container it does not yet know without a Lich release. The global $box_regex (built from the same defaults) is left untouched for third-party scripts. Post-match rewrites come from DEFAULT_BOX_SUBSTITUTIONS merged with custom_box_substitutions.

Examples:

box_list_to_adj_and_noun('an ironwood crate') #=> ['iron crate']

Parameters:

  • list (String) —

    game-formatted box list (e.g. from rummage /B)

Returns:

  • (Array<String>) —

    gettable box adjective+noun names

See Also:



575
576
577
578
579
580
581
582
583
584
585
# File 'documented/dragonrealms/commons/common.rb', line 575

def box_list_to_adj_and_noun(list)
  woods = CustomSubstitutions.resolve(:custom_box_woods, BOX_WOODS, type: :names)
  containers = CustomSubstitutions.resolve(:custom_box_containers, BOX_CONTAINERS, type: :names)
  substitutions = CustomSubstitutions.resolve(:custom_box_substitutions, DEFAULT_BOX_SUBSTITUTIONS, type: :pairs)
  box_regex = /((?:#{woods.map { |wood| Regexp.escape(wood) }.join('|')}) (?:#{containers.map { |container| Regexp.escape(container) }.join('|')}))/
  list.strip
      .split(box_regex)
      .reject(&:empty?)
      .select { |item| item =~ box_regex }
      .map { |box| substitutions.reduce(box) { |current, (from, to)| current.gsub(from, to) } }
end

.bput(message, *matches) ⇒ Object

Like fput but better because will wait for RT before performing command and do smart retries. Will wait for matching text up to BPUT_DEFAULT_TIMEOUT seconds then timeout. Also recovers from some limited failures wherein we want to simply fix the issue and retry the bput, like when we're prone and need to be standing. Complex handling should be done within the calling script.



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
# File 'documented/dragonrealms/commons/common.rb', line 199

def bput(message, *matches)
  options = (matches.shift if matches.first.is_a?(Hash)) || {}
  options['timeout'] ||= BPUT_DEFAULT_TIMEOUT
  options['ignore_rt'] ||= false

  timeout = options['timeout']
  ignore_rt = options['ignore_rt']
  suppress = options['suppress_no_match']

  if options['debug']
    echo "bput.message=#{message}"
    echo "bput.options=#{options}"
    echo "bput.matches=#{matches}"
  end

  waitrt? unless ignore_rt
  log = []
  matches.flatten!
  matches.map! { |item| item.is_a?(Regexp) ? item : /#{item}/i }
  clear
  put message
  timer = Time.now
  held = []
  while (response = (replayed = held.shift) || get?) || (Time.now - timer < timeout)

    if response.nil?
      pause 0.1
      next
    end

    log += [response]
    # Wait lines read during the hold were waited out before the resend.
    next if replayed&.match?(WAIT_RESPONSE_PATTERN)

    case response
    when /^For some strange reason you are unable to do that\.  The world somehow seems frozen in place/
      # Zadraes - 13:32 It's a "You're in an area actively being updated" message
      pause 1
      put message
      timer = Time.now
      next
    when WAIT_RESPONSE_PATTERN
      unless ignore_rt
        held = wait_out_roundtime(response)
        put message
        timer = Time.now
      end
      next
    when /Sorry, you may only type ahead/
      pause 1
      put message
      timer = Time.now
      next
    when /^You can't do that while you are asleep./
      put 'wake'
      put message
      timer = Time.now
      next
    when /^You are a bit too busy performing to do that/, /^You should stop playing before you do that/
      put 'stop play'
      put message
      timer = Time.now
      next
    when /would give away your hiding place/
      release_invisibility
      put 'unhide'
      put message
      timer = Time.now
      next
    when /^You are still stunned/
      pause 0.5 while stunned?
      pause 0.5
      put message
      timer = Time.now
      next
    when /^You can't do that while entangled in a web/
      pause 0.5 while webbed?
      pause 0.5
      put message
      timer = Time.now
      next
    when /^You must be standing/, /^You should stand up first/, /^You'll need to stand up first/, /^You can't do that while (sitting|kneeling|lying)/, /^You should be sitting up/, /^You really should be standing to play/, /^After failing to draw a breath for what feels like forever/
      # Stand, then resend. When standing can't succeed (unconscious,
      # overburdened, or a hold that outlasts the timeout), resending
      # would draw the same reply again at once, so keep reading until
      # the timeout instead.
      next unless stand_before?(timer + timeout)

      waitrt?
      put message
      timer = Time.now
      next
    end

    matches.each do |match|
      if (result = response.match(match))
        return result.to_a.first
      end
    end
  end

  unless suppress
    Lich::Messaging.msg("bold", "DRC: No match was found after #{timeout} seconds for command '#{message}'")
    Lich::Messaging.msg("bold", "DRC: Messages seen: #{log.length}")
    log.reverse.each { |logged_response| Lich::Messaging.msg("bold", "DRC: > #{logged_response}") }
    Lich::Messaging.msg("bold", "DRC: Checked against: #{matches}")
  end

  ''
end

.can_see_sky? ⇒ Boolean

Checks whether the character can see the sky from their current location. Uses WEATHER command to differentiate indoors-without-sky, indoors-with-sky, and outdoors.

Examples:

DRC.can_see_sky? #=> true

Returns:

  • (Boolean) —

    true if the sky is visible (outdoors or in a windowed room), false if indoors without sky access



393
394
395
396
397
398
399
400
401
402
# File 'documented/dragonrealms/commons/common.rb', line 393

def can_see_sky?
  # If you are indoors and not able to see the sky.
  inside_no_sky = "That's a bit hard to do while inside."
  # If you are indoors but able to see the sky (e.g. a window or skylight).
  inside_yes_sky = "You glance outside"
  # If you are outdoors.
  outside = "You glance up at the sky"
  # Can we see the sky?
  bput("weather", inside_no_sky, inside_yes_sky, outside) != inside_no_sky
end

.capture_reply(command, start_pattern, end_pattern, usexml:, timeout: 5) ⇒ Array(Array<String>, Boolean)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a command with Lich::Util.issue_command and captures its reply up to and including the line that ends it.

issue_command never returns nil. On a timeout it returns [] when the start line never arrived, or the lines collected so far when the end line never arrived. Asking for the end line (include_end: true) is the only way to tell a complete reply from a cut-off one, so it is requested here and then dropped from the lines handed back.

The start line is never taken as the end line (issue_command only looks for the end on the lines after it), so a reply needs at least two lines to count as complete.

Used for replies that end on a game line such as Roundtime (COMMUNE SENSE, ASSESS TEACH, PERC MANA); see capture_to_prompt for replies that end on the prompt. Each caller prints its own timeout message.

Parameters:

  • command (String) —

    the game command to send

  • start_pattern (Regexp) —

    the first line of the reply

  • end_pattern (Regexp) —

    the line that ends the reply

  • usexml (Boolean) —

    capture the XML stream (true) or plain text (false)

  • timeout (Integer) (defaults to: 5) —

    seconds to wait for the whole reply

Returns:

  • (Array(Array<String>, Boolean)) —

    the reply lines without the end line, and true when the end line arrived (the reply is complete)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'documented/dragonrealms/commons/common.rb', line 158

def capture_reply(command, start_pattern, end_pattern, usexml:, timeout: 5)
  lines = Array(
    Lich::Util.issue_command(
      command,
      start_pattern,
      end_pattern,
      usexml: usexml,
      quiet: true,
      include_end: true,
      timeout: timeout
    )
  )
  complete = lines.length > 1 && lines.last.match?(end_pattern)
  lines = lines[0...-1] if complete
  [lines, complete]
end

.capture_to_prompt(command, start_pattern, timeout: 5) ⇒ Array(Array<String>, Boolean)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a command with Lich::Util.issue_command and captures its XML reply up to the next prompt, using capture_reply. The prompt line is dropped from the lines handed back.

Shared by the commons for replies that end on the prompt (HEALTH, PERCEIVE HEALTH, TOUCH, WEALTH); each caller prints its own timeout message.

Parameters:

  • command (String) —

    the game command to send

  • start_pattern (Regexp) —

    the first line of the reply

  • timeout (Integer) (defaults to: 5) —

    seconds to wait for the whole reply

Returns:

  • (Array(Array<String>, Boolean)) —

    the reply lines without the prompt line, and true when the prompt arrived (the reply is complete)



188
189
190
# File 'documented/dragonrealms/commons/common.rb', line 188

def capture_to_prompt(command, start_pattern, timeout: 5)
  capture_reply(command, start_pattern, /<prompt/, usexml: true, timeout: timeout)
end

.check_encumbrance(refresh = true) ⇒ Integer?

Checks the character's current encumbrance level. If refresh is true, sends ENCUMBRANCE command and parses the result; otherwise uses the cached DRStats.encumbrance value.

Examples:

DRC.check_encumbrance #=> 3 (refreshed from server)
DRC.check_encumbrance(false) #=> cached value

Parameters:

  • refresh (Boolean) (defaults to: true) —

    whether to send ENCUMBRANCE (default true)

Returns:

  • (Integer, nil) —

    the encumbrance level from $ENC_MAP, or nil if not found



1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
# File 'documented/dragonrealms/commons/common.rb', line 1098

def check_encumbrance(refresh = true)
  encumbrance = DRStats.encumbrance
  if refresh
    encumbrance_pattern = /(?:Encumbrance)\s:\s(?<encumbrance>.*)/
    result = bput('encumbrance', encumbrance_pattern)
    enc_match = result.match(encumbrance_pattern)
    encumbrance = enc_match[:encumbrance] if enc_match
  end
  $ENC_MAP[encumbrance]
end

.clean_instrument(settings, worn = true) ⇒ Boolean

Cleans a dirty instrument by wiping it with a chamois cloth and then cleaning it. Gets or removes the instrument as needed, stops any current song, and performs the full clean cycle (wipe, wring, clean).

Examples:

DRC.clean_instrument(settings) #=> true

Parameters:

  • settings (OpenStruct) —

    settings object with instrument, worn_instrument, and cleaning_cloth properties

  • worn (Boolean) (defaults to: true) —

    whether the instrument is worn (default true)

Returns:

  • (Boolean) —

    true on success, false if cloth/instrument retrieval fails



1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
# File 'documented/dragonrealms/commons/common.rb', line 1310

def clean_instrument(settings, worn = true)
  cloth = settings.cleaning_cloth
  instrument = worn ? settings.worn_instrument : settings.instrument

  unless DRCI.get_item?(cloth)
    Lich::Messaging.msg("bold", "DRC: You have no chamois cloth -- this could cause problems with playing an instrument!")
    DRC.beep
    return false
  end
  DRC.stop_playing

  if worn
    unless DRCI.remove_item?(instrument)
      Lich::Messaging.msg("bold", "DRC: Could not remove #{instrument}, putting away cloth, and not trying to clean.")
      DRCI.stow_item?(cloth)
      DRC.beep
      return false
    end
  else
    unless DRCI.get_item?(instrument)
      Lich::Messaging.msg("bold", "DRC: Could not get #{instrument}, putting away cloth, and not trying to clean.")
      DRCI.stow_item?(cloth)
      DRC.beep
      return false
    end
  end

  loop do
    case DRC.bput("wipe my #{instrument} with my #{cloth}", 'Roundtime', 'not in need of drying', 'You should be sitting up')
    when 'not in need of drying'
      break
    when 'You should be sitting up'
      DRC.fix_standing
      next
    end
    pause 1
    waitrt?

    until /you wring a dry/i =~ DRC.bput("wring my #{cloth}", 'You wring a dry', 'You wring out')
      pause 1
      waitrt?
    end
  end

  until /not in need of cleaning/i =~ DRC.bput("clean my #{instrument} with my #{cloth}", 'Roundtime', 'not in need of cleaning')
    pause 1
    waitrt?
  end

  DRCI.wear_item?(instrument) if worn
  DRCI.stow_item?(cloth)
  true
end

.collect(item, practice = true) ⇒ Object

Each refused collect kicks one pile (KICK PILE destroys it) and tries again. Capped at COLLECT_CLUTTER_ATTEMPTS collects per call so a room that stays cluttered cannot loop forever. Returns nil when it gives up (no pile, missed kick, or still cluttered after the last attempt), otherwise the result of waitrt?.



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'documented/dragonrealms/commons/common.rb', line 441

def collect(item, practice = true)
  command = practice ? "collect #{item} practice" : "collect #{item}"

  attempts = 0
  while bput(command, COLLECT_MESSAGES) == 'The room is too cluttered'
    attempts += 1
    if attempts >= COLLECT_CLUTTER_ATTEMPTS
      Lich::Messaging.msg("bold", "DRC: Room still too cluttered to collect #{item} after #{attempts} tries, giving up.")
      # Giving up still waits out any roundtime, e.g. from an earlier kick.
      waitrt?
      return
    end
    unless kick_pile?
      waitrt?
      return
    end
  end
  waitrt?
end

.do_tune(instrument, tuning = "") ⇒ Boolean

Recursively tunes an instrument by comparing it against a target pitch. Sends TUNE command with optional sharpening or flattening, and recurses based on the response. Ensures standing before tuning.

Examples:

DRC.do_tune('lute') #=> true (recursively tunes until in pitch)

Parameters:

  • instrument (String) —

    the instrument noun in hands

  • tuning (String) (defaults to: "") —

    optional tuning direction ('sharp', 'flat', or empty) (default "")

Returns:

  • (Boolean) —

    true when tuned, false if instrument is not in hands



1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'documented/dragonrealms/commons/common.rb', line 1416

def do_tune(instrument, tuning = "")
  unless instrument && DRCI.in_hands?(instrument)
    Lich::Messaging.msg("bold", "DRC: No instrument found in hands. Not trying to tune.")
    DRC.beep
    return false
  end

  case DRC.bput("tune my #{instrument} #{tuning}",
                /^You should be sitting up/,
                /After a moment, you .* flat/,
                /After a moment, you .* sharp/,
                /After a moment, you .* tune/)
  when /After a moment, you .* tune/
    Lich::Messaging.msg("plain", "DRC: Instrument tuned.")
    return true
  when /After a moment, you .* flat/
    DRC.do_tune(instrument, "sharp")
  when /After a moment, you .* sharp/
    DRC.do_tune(instrument, "flat")
  when /^You should be sitting up/
    DRC.fix_standing
    DRC.do_tune(instrument)
  end
end

.fix_dr_bullshit(string) ⇒ String

Simplifies item names by extracting the first and last words, stripping flavor text and complex descriptions. Handles the special case of "ball and chain". If the string has two words or fewer, returns it unchanged.

Examples:

DRC.fix_dr_bullshit('a large ornate wooden shield') #=> "a shield"
DRC.fix_dr_bullshit('ball and chain') #=> "ball chain"

Parameters:

  • string (String) —

    the full item description

Returns:

  • (String) —

    simplified "first last" form, or original if too short



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'documented/dragonrealms/commons/common.rb', line 1022

def fix_dr_bullshit(string)
  return string if string.split.length <= 2

  string = string.sub(' and chain', '') if string =~ /ball and chain/

  match = string.match(/(?<first>\S+) .* (?<last>\S+)/)
  return string unless match

  "#{match[:first]} #{match[:last]}"
end

.fix_standing ⇒ Boolean

Issues STAND until the character is standing, giving up when the game reports a state from which standing cannot currently succeed. Without the CANNOT_STAND_PATTERN guard these states loop forever spamming STAND, because matching the message never makes standing? true (issue #3668: safe-room spamming STAND while unconscious).

On the immobile reply (held or rooted) this pauses 1 s before returning false, so a caller that loops on fix_standing with no delay of its own sends at most about one STAND a second while the hold lasts.

Returns:

  • (Boolean) —

    true once standing, false when the game says standing cannot succeed now



843
844
845
846
847
848
849
850
851
852
853
# File 'documented/dragonrealms/commons/common.rb', line 843

def fix_standing
  case stand_result
  when :standing
    true
  when :immobile
    pause 1
    false
  else
    false
  end
end

.forage?(item, tries = 5) ⇒ Boolean

Attempts to forage for an item, retrying on cluttered room and hand-related failures. When the room is too cluttered, kicks a pile and tries again (up to the tries limit). When hands are not free, stows the right hand via DRCI.

Examples:

DRC.forage?('mushroom') #=> true
DRC.forage?('spider silk', 10) #=> false if too cluttered

Parameters:

  • item (String) —

    the forage item name

  • tries (Integer) (defaults to: 5) —

    maximum forage attempts (default 5)

Returns:

  • (Boolean) —

    true if an item was successfully foraged (inventory changed), false if the room stays too cluttered, stowing failed, or tries exhausted



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'documented/dragonrealms/commons/common.rb', line 415

def forage?(item, tries = 5)
  snapshot = "#{right_hand}#{left_hand}"
  while snapshot == "#{right_hand}#{left_hand}"
    tries > 0 ? tries -= 1 : (return false)
    case bput("forage #{item}", 'Roundtime', 'The room is too cluttered to find anything here', 'You really need to have at least one hand free to forage properly', 'You survey the area and realize that any foraging efforts would be futile')
    when 'The room is too cluttered to find anything here'
      return false unless kick_pile?
    when 'You survey the area and realize that any foraging efforts would be futile'
      return false
    when 'You really need to have at least one hand free to forage properly'
      Lich::Messaging.msg("bold", "DRC: Hands not emptied properly. Stowing right hand...")
      unless DRCI.stow_hand('right')
        Lich::Messaging.msg("bold", "DRC: Failed to stow right hand, cannot forage.")
        return false
      end
    end
    waitrt?
  end
  true
end

.get_gems(container) ⇒ Array<String>

Convenience wrapper for rummage to retrieve gems from a container.

Parameters:

  • container (String) —

    the container noun

Returns:

  • (Array<String>) —

    array of gem names

See Also:

  • #rummage


530
531
532
# File 'documented/dragonrealms/commons/common.rb', line 530

def get_gems(container)
  rummage('G', container)
end

.get_materials(container) ⇒ Array<String>

Convenience wrapper for rummage to retrieve materials from a container.

Parameters:

  • container (String) —

    the container noun

Returns:

  • (Array<String>) —

    array of material names

See Also:

  • #rummage


539
540
541
# File 'documented/dragonrealms/commons/common.rb', line 539

def get_materials(container)
  rummage('M', container)
end

.get_noun(long_name) ⇒ String?

Extracts the rightmost noun from an item's long name after removing flavor text. Scans the result for the last sequence of letters, hyphens, or apostrophes.

Examples:

DRC.get_noun('a deobar strongbox') #=> "strongbox"
DRC.get_noun('an ornate shield') #=> "shield"

Parameters:

  • long_name (String) —

    the full item description

Returns:

  • (String, nil) —

    the noun, or nil if none found



676
677
678
# File 'documented/dragonrealms/commons/common.rb', line 676

def get_noun(long_name)
  remove_flavor_text(long_name).strip.scan(/[a-z\-']+$/i).first
end

.get_skins(container) ⇒ Array<String>

Convenience wrapper for rummage to retrieve skins from a container.

Parameters:

  • container (String) —

    the container noun

Returns:

  • (Array<String>) —

    array of skin names

See Also:

  • #rummage


521
522
523
# File 'documented/dragonrealms/commons/common.rb', line 521

def get_skins(container)
  rummage('S', container)
end

.get_town_name(text) ⇒ Object

Looks up the canonical name of the town based on the given text. Utility to help identify the canonical town name based on arbitrary text. For example, "Theren" for "Therenborough" and "Haven" for "Riverhaven". It also handles missing apostrophes and the occasional space between names like "merkresh" or "Mer'Kresh" or "ainghazal" or "Ain Ghazal". Returns nil if unable to find a match.



816
817
818
819
820
821
822
823
824
# File 'documented/dragonrealms/commons/common.rb', line 816

def get_town_name(text)
  towns = $HOMETOWN_REGEX_MAP.select { |_town, regex| regex =~ text }.keys
  if towns.length > 1
    Lich::Messaging.msg("bold", "DRC: Found multiple towns that match '#{text}': #{towns}")
    Lich::Messaging.msg("bold", "DRC: Using first town that matched: #{towns.first}")
    Lich::Messaging.msg("bold", "DRC: To avoid ambiguity, please use the town's full name: https://elanthipedia.play.net/Category:Cities")
  end
  towns.first
end

.hide?(hide_type = 'hide') ⇒ Boolean

Attempts to hide using the specified hide type, retrying on common failures. Handles being too busy performing, already stalking, and timing windows. Checks hiding? after each attempt.

Examples:

DRC.hide? #=> true
DRC.hide?('stalk') #=> true

Parameters:

  • hide_type (String) (defaults to: 'hide') —

    the hide command (default 'hide', e.g. 'stalk')

Returns:

  • (Boolean) —

    true if hiding? returns true after successful attempt, false otherwise



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'documented/dragonrealms/commons/common.rb', line 994

def hide?(hide_type = 'hide')
  unless hiding?
    case bput(hide_type, 'Roundtime', 'too busy performing', 'can\'t see any place to hide yourself', 'Stalk what', 'You\'re already stalking', 'Stalking is an inherently stealthy', 'You haven\'t had enough time', 'You search but find no place to hide')
    when 'too busy performing'
      bput('stop play', 'You stop playing', 'In the name of')
      return hide?(hide_type)
    when "You're already stalking"
      put 'stop stalk'
      return hide?(hide_type)
    when 'You haven\'t had enough time'
      pause 1
      return hide?(hide_type)
    end
    pause
    waitrt?
  end
  hiding?
end

.kick_pile?(item = 'pile') ⇒ Boolean

Kicks a pile in the room (destroying it) to reduce clutter. First ensures standing, then checks for a matching object in DRRoom.room_objs.

Examples:

DRC.kick_pile? #=> true

Parameters:

  • item (String) (defaults to: 'pile') —

    the object to kick (default 'pile')

Returns:

  • (Boolean) —

    true if the kick succeeded (matched the take-a-step-back message), false otherwise



469
470
471
472
473
# File 'documented/dragonrealms/commons/common.rb', line 469

def kick_pile?(item = 'pile')
  fix_standing
  return unless DRRoom.room_objs.any? { |room_obj| room_obj.match?(/pile/) }
  bput("kick #{item}", 'I could not find', 'take a step back and run up to', 'Now what did the .* ever do to you', 'You lean back and kick your feet,') == 'take a step back and run up to'
end

.left_hand ⇒ String?

Returns the name of the item in the left hand, simplified via fix_dr_bullshit.

Examples:

DRC.left_hand #=> "sword" or nil

Returns:

  • (String, nil) —

    the item name, or nil if the hand is empty



1038
1039
1040
# File 'documented/dragonrealms/commons/common.rb', line 1038

def left_hand
  GameObj.left_hand.name == 'Empty' ? nil : fix_dr_bullshit(GameObj.left_hand.name)
end

.left_hand_noun ⇒ String?

Returns the noun of the item in the left hand from GameObj.

Examples:

DRC.left_hand_noun #=> "sword" or nil

Returns:

  • (String, nil) —

    the item noun, or nil if the hand is empty



1056
1057
1058
# File 'documented/dragonrealms/commons/common.rb', line 1056

def left_hand_noun
  GameObj.left_hand == 'Empty' ? nil : GameObj.left_hand.noun
end

.list_to_array(list) ⇒ Object

Take a game formatted list "an arrow, silver coins and a deobar strongbox" And return an array ["an arrow", "silver coins", "a deobar strongbox"] is this ever useful compared to the list_to_nouns?



546
547
548
# File 'documented/dragonrealms/commons/common.rb', line 546

def list_to_array(list)
  list.strip.split(/(?:,|(?:, |\s)?and\s?)(?:\s?<pushBold\/>\s?)?(?=\s\ba\b|\s\ban\b|\s\bsome\b|\s\bthe\b)/i).reject(&:empty?)
end

.list_to_nouns(list) ⇒ Object

Take a game formatted list "an arrow, silver coins and a deobar strongbox" And return an array of nouns ["arrow", "coins", "strongbox"]



661
662
663
664
665
666
# File 'documented/dragonrealms/commons/common.rb', line 661

def list_to_nouns(list)
  list_to_array(list)
    .map { |long_name| get_noun(long_name) }
    .compact
    .reject { |noun| noun == '' }
end

.listen?(teacher, observe_flag = false) ⇒ Boolean

Attempts to listen to a teacher's class, skipping banned skill classes for the character's guild. Barbarians and Thieves skip Thievery/Sorcery and all magic schools. Barbarians also skip Utility. If the skill is banned, stops listening and returns false. Some responses (e.g., "already listening", "no harm in listening") return true without stopping.

Examples:

DRC.listen?('Gulnora') #=> true
DRC.listen?('Gulnora', true) #=> true with observe flag

Parameters:

  • teacher (String, nil) —

    the teacher's name

  • observe_flag (Boolean) (defaults to: false) —

    whether to use 'observe' flag (default false)

Returns:

  • (Boolean) —

    true if the character is now listening to a non-banned class, false if the class is banned, the teacher could not be found, or other errors



916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'documented/dragonrealms/commons/common.rb', line 916

def listen?(teacher, observe_flag = false)
  return false if teacher.nil?
  return false if teacher.empty?

  bad_classes = %w[Thievery Sorcery]
  bad_classes += ['Life Magic', 'Holy Magic', 'Lunar Magic', 'Elemental Magic', 'Arcane Magic', 'Targeted Magic', 'Arcana', 'Attunement'] if DRStats.barbarian? || DRStats.thief?
  bad_classes += ['Utility'] if DRStats.barbarian?

  observe = observe_flag ? 'observe' : ''

  result = bput("listen to #{teacher} #{observe}", 'begin to listen to \w+ teach the .* skill', 'already listening', 'could not find who', 'You have no idea', 'isn\'t teaching a class', 'don\'t have the appropriate training', 'Your teacher appears to have left', 'isn\'t teaching you anymore', 'experience differs too much from your own', 'but you don\'t see any harm in listening', 'invitation if you wish to join this class', 'You cannot concentrate to listen to .* while in combat')
  if (skill_match = result.match(/begin to listen to \w+ teach the (?<skill>.*) skill/))
    return true if bad_classes.grep(/#{skill_match[:skill]}/i).empty?

    bput('stop listening', 'You stop listening')
  elsif result == 'already listening'
    return true
  elsif result == "but you don't see any harm in listening"
    bput('stop listening', 'You stop listening')
  end

  false
end

.log_window(text, window_name, make_bold = true, create_window = false, pre_clear_window = false) ⇒ Object

Sends a message to the specified window. Replaces the deprecated atmo method. By default the message is bold. Creates window upon request. Pre-clears window upon request.



1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
# File 'documented/dragonrealms/commons/common.rb', line 1648

def log_window(text, window_name, make_bold = true, create_window = false, pre_clear_window = false)
  if create_window
    _respond("<streamWindow id=\"#{window_name}\" title=\"#{window_name}\" location=\"center\" save=\"true\" />")
    _respond("<exposeStream id=\"#{window_name}\"/>")
  end

  if pre_clear_window
    _respond("<clearStream id=\"#{window_name}\"/>\r\n")
  end

  _respond(
    "<pushStream id=\"#{window_name}\"/>" + (make_bold ? bold(text) : text),
    "<popStream id=\"#{window_name}\" /><prompt time=\"#{XMLData.server_time.to_i}\">&gt;</prompt>"
  )
end

.message(text, make_bold = true) ⇒ Object

Sends a message to the game window. By default the message is bold. Delegates to Lich::Messaging.msg for consistent multi-frontend support.



1676
1677
1678
# File 'documented/dragonrealms/commons/common.rb', line 1676

def message(text, make_bold = true)
  Lich::Messaging.msg(make_bold ? "bold" : "plain", text, encode: false)
end

.parse_assess_teach_lines(lines) ⇒ Object

Pure parsing method for assess teach output (unit-testable)



972
973
974
975
976
977
978
979
980
981
982
983
# File 'documented/dragonrealms/commons/common.rb', line 972

def parse_assess_teach_lines(lines)
  lines.each_with_object({}) do |line, hash|
    match = line.match(ASSESS_TEACH_TEACHER_PATTERN)
    next unless match

    teacher = match[:teacher]
    skill = match[:skill]
    skill_filter = skill.match(ASSESS_TEACH_SKILL_FILTER_PATTERN)
    skill = skill_filter[:filtered_skill] if skill_filter
    hash[teacher] = skill
  end
end

.pause_all ⇒ Boolean

Note:

Must be paired with unpause_all to release the lock

Pauses all running scripts except those already paused and the current script, using a shared mutex lock. Returns false if the lock is already held by another caller. Pauses 1 second after pausing to allow pause handling to complete.

Examples:

DRC.pause_all #=> true

Returns:

  • (Boolean) —

    true if successful, false if unable to acquire the lock

See Also:

  • #unpause_all


1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
# File 'documented/dragonrealms/commons/common.rb', line 1450

def pause_all
  return false unless $pause_all_lock.try_lock

  @pause_all_no_unpause = []

  Script.running.find_all(&:paused?).each do |script|
    @pause_all_no_unpause << script
  end

  Script.running.find_all do |script|
    !script.paused? &&
      !script.no_pause_all &&
      script != Script.current
  end
        .each(&:pause)

  pause 1
  true
end

.play_song?(settings, song_list, worn = true, skip_clean = false, climbing = false, skip_tuning = false) ⇒ Boolean

Plays a song from the song_list, with automatic retry logic for common failures. Manages user variables for current song and climbing song separately. Handles instrument swaps, tuning, cleaning, and dirtyness. Climbing mode uses a dedicated UserVar for song selection.

Examples:

DRC.play_song?(settings, {"Blessing of Protection" => 2, "Elfin Chord" => 5})

Parameters:

  • settings (OpenStruct) —

    settings object with instrument, worn_instrument, cleaning_cloth properties

  • song_list (Hash, Array) —

    songs indexed by difficulty or array of [name, difficulty] pairs

  • worn (Boolean) (defaults to: true) —

    whether to use worn_instrument vs instrument (default true)

  • skip_clean (Boolean) (defaults to: false) —

    whether to skip instrument cleaning (default false)

  • climbing (Boolean) (defaults to: false) —

    whether this is for a climbing script (default false)

  • skip_tuning (Boolean) (defaults to: false) —

    whether to skip tuning (default false)

Returns:

  • (Boolean) —

    true on success or when continuing is acceptable (e.g. difficulty noted), false when unable to play (no instrument, out of safe room, etc.)



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'documented/dragonrealms/commons/common.rb', line 1199

def play_song?(settings, song_list, worn = true, skip_clean = false, climbing = false, skip_tuning = false)
  instrument = worn ? settings.worn_instrument : settings.instrument

  # The climbing caller re-picks its song when the climbing instrument
  # changes; resetting it here would play the easiest song instead.
  if climbing
    UserVars.climbing_song = song_list.first.first unless UserVars.climbing_song
  else
    reset_song_for_instrument(instrument)
    UserVars.song = song_list.first.first unless UserVars.song
  end
  song_to_play = climbing ? UserVars.climbing_song : UserVars.song
  play_command = "play #{song_to_play}"
  if instrument
    play_command = play_command + " on my #{instrument}"
  end
  fput('release ecry') if DRSpells.active_spells["Eillie's Cry"].to_i > 0
  result = bput(play_command, 'too damaged to play', 'dirtiness may affect your performance', 'slightest hint of difficulty', 'fumble slightly', /Your .+ is submerged in the water/, 'You begin a', 'You struggle to begin', 'You\'re already playing a song', 'You effortlessly begin', 'You begin some', 'You cannot play', 'Play on what instrument', 'Are you sure that\'s the right instrument', 'now isn\'t the best time to be playing', 'Perhaps you should find somewhere drier before trying to play', 'You should stop practicing', /^You really need to drain/, /Your .* tuning is off, and may hinder your performance/)
  case result
  when 'Play on what instrument', 'Are you sure that\'s the right instrument'
    unless DRCI.get_item?(instrument)
      Lich::Messaging.msg("bold", "DRC: Failed to get #{instrument}.")
      return false
    end
    if worn && !DRCI.wear_item?(instrument)
      Lich::Messaging.msg("bold", "DRC: Failed to wear #{instrument}.")
      return false
    end
    play_song?(settings, song_list, worn, skip_clean, climbing, skip_tuning)
  when 'now isn\'t the best time to be playing', 'Perhaps you should find somewhere drier before trying to play', 'You should stop practicing'
    false
  when 'You\'re already playing a song'
    fput('stop play')
    play_song?(settings, song_list, worn, skip_clean, climbing, skip_tuning)
  when 'You cannot play'
    wait_for_script_to_complete('safe-room')
  when /Your .* tuning is off, and may hinder your performance/
    Lich::Messaging.msg("bold", "DRC: Instrument out of tune. Attempting to tune it.")
    return true if DRSkill.getrank('Performance') < 20
    return true if skip_tuning
    return true unless DRC.tune_instrument(settings)

    play_song?(settings, song_list, worn, skip_clean, climbing, skip_tuning)
  when 'dirtiness may affect your performance', /^You really need to drain/
    return true if DRSkill.getrank('Performance') < 20
    return true if skip_clean
    return true unless clean_instrument(settings, worn)

    play_song?(settings, song_list, worn, skip_clean, climbing, skip_tuning)
  when 'slightest hint of difficulty', 'fumble slightly'
    true
  when 'You begin a', 'You effortlessly begin', 'You begin some'
    return true if song_to_play == song_list.to_a.last.last
    # Ignore difficulty messages if we have an offset
    return true if climbing && UserVars.climbing_song_offset

    stop_playing
    UserVars.climbing_song = song_list[UserVars.climbing_song] || song_list.first.first if climbing
    UserVars.song = song_list[UserVars.song] || song_list.first.first unless climbing
    play_song?(settings, song_list, worn, skip_clean, climbing, skip_tuning)
  when 'You struggle to begin'
    return true if song_to_play == song_list.first.first
    # Ignore difficulty messages if we have an offset
    return true if climbing && UserVars.climbing_song_offset

    stop_playing
    UserVars.climbing_song = song_list.first.first if climbing
    UserVars.song = song_list.first.first unless climbing
    play_song?(settings, song_list, worn, skip_clean, climbing, skip_tuning)
  else
    false
  end
end

.read_held_lines(held) ⇒ Array<String>

Appends every line buffered so far to held.

Parameters:

Returns:



338
339
340
341
342
343
# File 'documented/dragonrealms/commons/common.rb', line 338

def read_held_lines(held)
  while (line = get?)
    held << line
  end
  held
end

.release_invisibility ⇒ void

This method returns an undefined value.

Releases all active invisibility spells and khri meditation. Looks up invisibility spells from the spells data file and releases each active one by abbreviation. Also handles Khri Silence and Khri Vanish for Thieves.

Examples:

DRC.release_invisibility


1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# File 'documented/dragonrealms/commons/common.rb', line 1076

def release_invisibility
  get_data('spells')
    .spell_data
    .select { |_name, properties| properties['invisibility'] }
    .select { |name, _properties| DRSpells.active_spells.keys.include?(name) }
    .map { |_name, properties| properties['abbrev'] }
    .each { |abbrev| fput("release #{abbrev}") }

  # handle khri silence as it's not part of base-spells data, and method of ending it differs from spells
  bput('khri stop silence', 'You attempt to relax') if DRSpells.active_spells.keys.include?('Khri Silence')
  bput('khri stop vanish', /^You would need to start Vanish/, /^Your control over the limited subversion of reality falters/, /^You are not trained in the Vanish meditation/) if (DRStats.guild == "Thief" && invisible?)
end

.remove_flavor_text(item) ⇒ String

Strips descriptive flavor text ("... adorned with ...") from an item name, leaving the gettable noun phrase.

Applies the built-in FLAVOR_TEXT_PATTERN first, then any player-defined custom_flavor_text_patterns (regular expressions) for flavor the built-in pattern misses -- letting a player strip a new flavor phrasing without a Lich release. User patterns are compiled with a per-pattern timeout and validated/guarded by CustomSubstitutions; an invalid or runaway pattern is reported and skipped, never raising here.

Examples:

remove_flavor_text('a sword adorned with rubies of deep crimson') #=> 'a sword'

Parameters:

  • item (String) —

    the item long name

Returns:

  • (String) —

    the item name with flavor text removed

See Also:



696
697
698
699
# File 'documented/dragonrealms/commons/common.rb', line 696

def remove_flavor_text(item)
  custom_patterns = CustomSubstitutions.resolve(:custom_flavor_text_patterns, [], type: :regexes)
  CustomSubstitutions.apply_regexes(item.sub(FLAVOR_TEXT_PATTERN, ''), custom_patterns)
end

.reset_song_for_instrument(instrument) ⇒ void

This method returns an undefined value.

Clears the stored UserVar.song when the instrument changes or is first set. Logs a message indicating which instrument is new/different.

Examples:

DRC.reset_song_for_instrument('lute')

Parameters:

  • instrument (String, nil) —

    the instrument currently in use



1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
# File 'documented/dragonrealms/commons/common.rb', line 1280

def reset_song_for_instrument(instrument)
  if UserVars.instrument.nil?
    Lich::Messaging.msg("plain", "DRC: No previous instrument setting detected. Cleaning stored song data.")
    UserVars.song = nil
  elsif UserVars.instrument != instrument
    Lich::Messaging.msg("plain", "DRC: New instrument #{instrument} detected; old instrument: #{UserVars.instrument}. Resetting stored song data.")
    UserVars.song = nil
  end
  UserVars.instrument = instrument
end

.retreat(ignored_npcs = []) ⇒ Boolean?

Retreats until out of combat, standing first when the game asks.

While held or rooted (the immobile reply, to RETREAT or to STAND) it pauses 1 s and tries again for BPUT_DEFAULT_TIMEOUT seconds from the first immobile reply, so entry roundtime and earlier steps don't use up that time. That one budget covers every hold in the call, so holds that alternate with steps back can't go on unbounded. The last try goes out up to 1 s after the deadline, so holds that end within BPUT_DEFAULT_TIMEOUT of the first immobile reply still end in a retreat; longer ones return false. When the character must stand but can't (overburdened, unconscious, no room), no reply says when that ends, so it returns false. It pauses 1 s before it gives up either way, so a caller that calls it again at once sends at most about one RETREAT a second.

Parameters:

  • ignored_npcs (Array<String>) (defaults to: []) —

    NPCs that don't need a retreat: nouns, or full names / multi-word phrases (see DRRoom.unignored_npcs)

Returns:

  • (Boolean, nil) —

    nil when no other NPC is in the room, true once out of combat, false when retreating can't succeed now



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'documented/dragonrealms/commons/common.rb', line 1128

def retreat(ignored_npcs = [])
  return if DRRoom.unignored_npcs(ignored_npcs).empty?

  deadline = nil
  loop do
    case DRC.bput("retreat", *RETREAT_ESCAPE_MESSAGES, IMMOBILE_PATTERN, *RETREAT_MESSAGES)
    when *RETREAT_ESCAPE_MESSAGES
      return true
    when IMMOBILE_PATTERN
      state = :immobile
    else
      state = DRC.stand_result
    end
    next if state == :standing

    # The deadline is checked before the pause, so the last RETREAT goes
    # out up to 1 s after it and every try stays 1 s apart.
    deadline ||= Time.now + BPUT_DEFAULT_TIMEOUT if state == :immobile
    try_again = state == :immobile && Time.now < deadline
    pause 1
    return false unless try_again
  end
end

.right_hand ⇒ String?

Returns the name of the item in the right hand, simplified via fix_dr_bullshit.

Examples:

DRC.right_hand #=> "shield" or nil

Returns:

  • (String, nil) —

    the item name, or nil if the hand is empty



1047
1048
1049
# File 'documented/dragonrealms/commons/common.rb', line 1047

def right_hand
  GameObj.right_hand.name == 'Empty' ? nil : fix_dr_bullshit(GameObj.right_hand.name)
end

.right_hand_noun ⇒ String?

Returns the noun of the item in the right hand from GameObj.

Examples:

DRC.right_hand_noun #=> "shield" or nil

Returns:

  • (String, nil) —

    the item noun, or nil if the hand is empty



1065
1066
1067
# File 'documented/dragonrealms/commons/common.rb', line 1065

def right_hand_noun
  GameObj.right_hand == 'Empty' ? nil : GameObj.right_hand.noun
end

.rummage(parameter, container) ⇒ Array<String>

Rummages in a container and parses the result into item lists. Handles the various empty/error responses. If the response is unrecognized, logs a message and returns [].

The parameter codes map to item types: 'B' for boxes, 'G' for gems, 'S' for skins, 'SC' for scrolls, or any other item type as nouns.

Examples:

DRC.rummage('G', 'pack') #=> ["emerald", "diamond"]
DRC.rummage('B', 'pack') #=> ["wooden strongbox", "iron crate"]

Parameters:

  • parameter (String) —

    the rummage filter code ('B', 'G', 'S', 'SC', etc.)

  • container (String) —

    the container noun (e.g., 'pack', 'locker')

Returns:

  • (Array<String>) —

    array of parsed item names, or [] if empty/error



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
# File 'documented/dragonrealms/commons/common.rb', line 488

def rummage(parameter, container)
  result = DRC.bput("rummage /#{parameter} my #{container}", 'but there is nothing in there like that\.', 'looking for .* and see .*', 'While it\'s closed', 'I don\'t know what you are referring to', 'You feel about', 'That would accomplish nothing')

  case result
  when 'You feel about'
    release_invisibility
    return rummage(parameter, container)
  when 'but there is nothing in there like that.', 'While it\'s closed', 'I don\'t know what you are referring to', 'That would accomplish nothing'
    return []
  end

  text = result.match(/looking for .* and see (.*)\.$/).to_a[1]
  unless text
    reason = result.empty? ? 'No response' : 'Unrecognized response'
    Lich::Messaging.msg("bold", "DRC: #{reason} to 'rummage /#{parameter} my #{container}', treating container as empty.")
    return []
  end

  case parameter
  when 'B'
    box_list_to_adj_and_noun(text)
  when 'SC'
    scroll_list_to_adj_and_noun(text)
  else
    list_to_nouns(text)
  end
end

.safe_pause_list ⇒ Array<Script>, false

Note:

Must be paired with safe_unpause_list to release the lock

Pauses all running scripts except those already paused and the current script, using a shared mutex lock that guards against deadlock during pause. Returns false if the lock is already held. Makes the lock holder immune to pause for as long as it holds the lock, so a paused script does not strand a peer waiting on try_lock.

Examples:

paused = DRC.safe_pause_list #=> [<Script>, <Script>]

Returns:

  • (Array<Script>, false) —

    array of paused Script objects (not names), or false if unable to acquire the lock

See Also:

  • #safe_unpause_list


1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
# File 'documented/dragonrealms/commons/common.rb', line 1538

def safe_pause_list
  return false unless $safe_pause_lock.try_lock

  # Pausing is cooperative: a paused script is a live thread that keeps
  # every mutex it holds (Ruby frees a mutex on thread death, not on
  # pause). So a script that gets pause_script'd while holding
  # $safe_pause_lock never releases it, and every peer spins forever on
  # try_lock -> false. Make the lock-holder immune to pause for as long as
  # it owns the lock, so this deadlock cannot form. Save the prior value
  # (and the holder itself) so we restore exactly what was there for a
  # caller that was already ignoring pauses, e.g. mid-travel.
  @safe_pause_holder = Script.self
  @safe_pause_prev_ignore_pause = @safe_pause_holder&.ignore_pause
  @safe_pause_holder&.ignore_pause = true

  # Capture the exact Script objects we pause -- not their names. The
  # matching safe_unpause_list restores from this list directly instead of
  # re-scanning Script.running by name. A name + live-rescan restore
  # silently drops any script that has left Script.running between pause
  # and unpause (gone hidden, or mid start/teardown) or whose pause flags
  # changed, stranding it paused forever while the log still claims it was
  # unpaused. Holding the objects makes the restore undo exactly what the
  # pause did.
  paused_scripts = []
  Script.running.find_all { |s| !s.paused? && !s.no_pause_all && s.name != Script.self.name }.each do |s|
    s.pause
    paused_scripts << s
  end
  Lich::Messaging.msg("plain", "DRC: Pausing #{paused_scripts.map(&:name)} to run #{Script.self.name}")
  return paused_scripts
end

.safe_unpause_list(paused_scripts) ⇒ false

Note:

Releases the $safe_pause_lock

Unpauses exactly the Script objects that were paused by the matching safe_pause_list call. Only works if the current thread owns the $safe_pause_lock. Restores the lock holder's pre-lock pause immunity and releases the lock.

Examples:

paused = DRC.safe_pause_list
# ... do work ...
DRC.safe_unpause_list(paused)

Parameters:

  • paused_scripts (Array<Script>) —

    the exact Script objects returned by safe_pause_list

Returns:

  • (false) —

    always false (or logs completion)

See Also:

  • #safe_pause_list


1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
# File 'documented/dragonrealms/commons/common.rb', line 1582

def safe_unpause_list(paused_scripts)
  return false unless $safe_pause_lock.owned?

  if paused_scripts.empty?
    Lich::Messaging.msg("plain", "DRC: #{Script.self.name} has finished.")
  else
    Lich::Messaging.msg("plain", "DRC: Unpausing #{paused_scripts.map(&:name)}, #{Script.self.name} has finished.")
    # Unpause exactly the objects we paused, regardless of their current
    # Script.running visibility or flags. Skip any a peer already unpaused
    # so we neither emit spurious "is not paused" noise nor fight another
    # coordinator that has since taken ownership of the pause.
    paused_scripts.each { |s| s.unpause if s.paused? }
  end
  # Restore the holder's pre-lock pause immunity before releasing the lock,
  # so we never leave a script permanently unpausable.
  @safe_pause_holder&.ignore_pause = @safe_pause_prev_ignore_pause
  @safe_pause_holder = nil
  @safe_pause_prev_ignore_pause = nil
  $safe_pause_lock.unlock
end

.scroll_list_to_adj_and_noun(list) ⇒ Array<String>

Converts a game rummage scroll list into gettable adjective+noun forms.

Pipeline per entry: strip the leading article, strip "labeled with...", apply the pre-collapse literal substitutions (DEFAULT_SCROLL_SUBSTITUTIONS_PRE merged with the player's custom_scroll_substitutions), apply the SCROLL_KEYWORD_COLLAPSE, then apply the post-collapse substitutions (DEFAULT_SCROLL_SUBSTITUTIONS_POST).

Examples:

scroll_list_to_adj_and_noun(' an icy blue parchment') #=> ['blue parchment']

Parameters:

  • list (String) —

    game-formatted scroll list (e.g. from rummage /SC)

Returns:

  • (Array<String>) —

    gettable scroll names

See Also:



647
648
649
650
651
652
653
654
655
656
657
# File 'documented/dragonrealms/commons/common.rb', line 647

def scroll_list_to_adj_and_noun(list)
  pre_substitutions = CustomSubstitutions.resolve(:custom_scroll_substitutions, DEFAULT_SCROLL_SUBSTITUTIONS_PRE, type: :pairs)
  list_to_array(list).map do |entry|
    without_article = entry
                      .sub(/(an|some|a(?: piece of)?)\s/, '')
                      .sub(/\slabeled with.*/, '')
    with_pre = pre_substitutions.reduce(without_article) { |text, (from, to)| text.sub(from, to) }
    collapsed = with_pre.sub(SCROLL_KEYWORD_COLLAPSE, ' \1')
    DEFAULT_SCROLL_SUBSTITUTIONS_POST.reduce(collapsed) { |text, (from, to)| text.sub(from, to) }.strip
  end
end

.set_stance(skill) ⇒ String

Sets the character's defensive stance based on the skill and guild. Calculates stance points based on Defending skill rank and guild divisor, then distributes the points across evasion, parry, and shield as requested.

Examples:

DRC.set_stance('parry') #=> "Setting your stance..."
DRC.set_stance('evasion') #=> "Setting your stance..."

Parameters:

  • skill (String) —

    the skill to prioritize: 'evasion', 'parry', 'shield'

Returns:

  • (String) —

    the matched response from the STANCE SET command



1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
# File 'documented/dragonrealms/commons/common.rb', line 1612

def set_stance(skill)
  div = if DRStats.guild == 'Paladin'
          50
        elsif %w[Barbarian Ranger Trader Commoner].include?(DRStats.guild)
          60
        else
          70
        end

  points = 80 + DRSkill.getrank('Defending') / div
  secondary = points > 100 ? 100 : points
  tertiary = points > 100 ? points - 100 : 0

  stance = case skill.downcase
           when 'evasion'
             "100 #{secondary} #{tertiary}"
           when 'parry'
             "#{secondary} 100 #{tertiary}"
           when 'shield'
             "#{secondary} #{tertiary} 100"
           else
             "100 #{secondary} #{tertiary}"
           end

  DRC.bput("stance set #{stance}", /Setting your/)
end

.smart_pause_all ⇒ Array<String>

Pauses all running scripts except those already paused and the current script. Logs which scripts were paused and why (to run current script). Unlike pause_all, does not use a lock or message during unpause.

Examples:

paused = DRC.smart_pause_all #=> ["heal", "buff"]

Returns:

  • (Array<String>) —

    list of script names that were paused



1500
1501
1502
1503
1504
1505
1506
1507
1508
# File 'documented/dragonrealms/commons/common.rb', line 1500

def smart_pause_all
  paused_script_list = []
  Script.running.find_all { |s| !s.paused? && !s.no_pause_all && s.name != Script.self.name }.each do |s|
    s.pause
    paused_script_list << s.name
  end
  Lich::Messaging.msg("plain", "DRC: Pausing #{paused_script_list} to run #{Script.self.name}")
  return paused_script_list
end

.stand_before?(deadline) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Stands for bput's posture recovery. Like fix_standing, but while the STAND draws the immobile reply (held or rooted) it tries again about once a second until the deadline, so a hold that ends in time still lets bput resend the command. The other states that prevent standing (unconscious, overburdened, ...) give up at once, as there is no reply that says they have ended.

Parameters:

  • deadline (Time) —

    when bput's own timeout runs out

Returns:

  • (Boolean) —

    true once standing, false when standing can't succeed or the hold outlasts the deadline



866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'documented/dragonrealms/commons/common.rb', line 866

def stand_before?(deadline)
  loop do
    case stand_result
    when :standing
      return true
    when :immobile
      # The last STAND goes out at the deadline at the latest, so a
      # hold that ends just before it still lets bput resend. Waiting
      # for that STAND's reply can take bput up to one round trip past
      # its timeout. The clock is read once, so the pause can't be
      # negative (pause raises on a negative interval).
      remaining = deadline - Time.now
      return false if remaining <= 0

      pause [1, remaining].min
    else
      return false
    end
  end
end

.stand_result ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends STAND until standing or until the game says standing can't succeed now.

Returns:

  • (Symbol) —

    :standing; :immobile on the held-or-rooted reply; :cannot_stand for the other replies in CANNOT_STAND_PATTERN



893
894
895
896
897
898
899
900
901
902
# File 'documented/dragonrealms/commons/common.rb', line 893

def stand_result
  loop do
    return :standing if standing?

    result = bput('stand', 'You stand', 'You are so unbalanced', 'As you stand', 'You are already', 'weight of all your possessions', 'You are overburdened and cannot', 'You\'re unconscious', 'You swim back up into a vertical position', "You don't seem to be able to move to do that", 'prevents you from standing', 'You\'re plummeting to your death', 'There\'s no room to do much of anything here')
    next unless result =~ CANNOT_STAND_PATTERN

    return IMMOBILE_PATTERN.match?(result) ? :immobile : :cannot_stand
  end
end

.stop_playing ⇒ String

Stops the character from playing their current song.

Examples:

DRC.stop_playing

Returns:

  • (String) —

    the matched response text



1296
1297
1298
# File 'documented/dragonrealms/commons/common.rb', line 1296

def stop_playing
  bput('stop play', 'You stop playing your song', 'In the name of', "But you're not performing")
end

.strip_xml(lines) ⇒ Array<String>

Strips XML tags and decodes common HTML entities from game output lines.

Parameters:

  • lines (Array<String>) —

    Array of raw game output lines

Returns:

  • (Array<String>) —

    Array of non-empty, trimmed strings with XML removed



128
129
130
131
# File 'documented/dragonrealms/commons/common.rb', line 128

def strip_xml(lines)
  lines.map { |line| line.gsub(XML_TAG_PATTERN, '').gsub('&gt;', '>').gsub('&lt;', '<').strip }
       .reject(&:empty?)
end

.text2num(text_num) ⇒ Integer?

Converts English text number words into an integer. Handles words like 'one', 'hundred', 'thousand', and composites like 'twenty-three'. Logs a bold message and returns nil if an unknown word is encountered.

Examples:

DRC.text2num('twenty-three') #=> 23
DRC.text2num('one hundred') #=> 100

Parameters:

  • text_num (String) —

    English text representation (e.g., 'twenty-three', 'one hundred')

Returns:

  • (Integer, nil) —

    the numeric value, or nil if an unknown word was found



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'documented/dragonrealms/commons/common.rb', line 1161

def text2num(text_num)
  text_num = text_num.tr('-', ' ')
  split_words = text_num.split(' ')
  g = 0

  split_words.each do |word|
    x = $NUM_MAP.fetch(word, nil)
    if word.eql?('hundred') && (g != 0)
      g *= 100
    elsif word.eql?('thousand') && (g != 0)
      g *= 1000
    elsif x.nil?
      Lich::Messaging.msg("bold", "DRC: Unknown number word '#{word}' in '#{text_num}'")
      return nil
    else
      g += x
    end
  end

  g
end

.tune_instrument(settings) ⇒ Boolean

Tunes an instrument to pitch, stopping any current song and ensuring two free hands. Retrieves or removes the instrument as needed, then delegates to do_tune. Optionally rewears the instrument when done.

Examples:

DRC.tune_instrument(settings) #=> true

Parameters:

  • settings (OpenStruct) —

    settings object with instrument and worn_instrument properties

Returns:

  • (Boolean) —

    true on success, false if hands are occupied or retrieval fails



1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'documented/dragonrealms/commons/common.rb', line 1372

def tune_instrument(settings)
  instrument = settings.worn_instrument || settings.instrument

  unless instrument
    Lich::Messaging.msg("bold", "DRC: Neither worn_instrument nor instrument set. Doing nothing.")
    return false
  end

  DRC.stop_playing

  unless (DRC.left_hand.nil? && DRC.right_hand.nil?) || DRCI.in_hands?(instrument)
    Lich::Messaging.msg("bold", "DRC: Need two free hands. Not tuning now.")
    return false
  end

  if settings.worn_instrument
    unless DRCI.remove_item?(instrument) || DRCI.in_hands?(instrument)
      Lich::Messaging.msg("bold", "DRC: Could not remove #{instrument}. Not trying to tune.")
      DRC.beep
      return false
    end
  else
    unless DRCI.get_item?(instrument) || DRCI.in_hands?(instrument)
      Lich::Messaging.msg("bold", "DRC: Could not get #{instrument}. Not trying to tune.")
      DRC.beep
      return false
    end
  end
  DRC.do_tune(instrument)
  waitrt?
  pause 1
  DRCI.wear_item?(instrument) if settings.worn_instrument
  true
end

.unpause_all ⇒ Boolean

Note:

Releases the $pause_all_lock

Unpauses all scripts that were paused by the matching pause_all call. Only works if the current thread owns the pause_all lock.

Examples:

DRC.unpause_all #=> true

Returns:

  • (Boolean) —

    true if successful, false if the lock is not held

See Also:

  • #pause_all


1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
# File 'documented/dragonrealms/commons/common.rb', line 1478

def unpause_all
  return false unless $pause_all_lock.owned?

  Script.running.find_all do |script|
    script.paused? &&
      !@pause_all_no_unpause.include?(script)
  end
        .each(&:unpause)

  @pause_all_no_unpause = []
  $pause_all_lock.unlock

  true
end

.unpause_all_list(scripts_to_unpause) ⇒ void

This method returns an undefined value.

Unpauses a specific list of scripts by name. Logs completion regardless of whether the list is empty.

Examples:

DRC.unpause_all_list(['heal', 'buff'])

Parameters:

  • scripts_to_unpause (Array<String>) —

    list of script names to unpause



1517
1518
1519
1520
1521
1522
1523
1524
# File 'documented/dragonrealms/commons/common.rb', line 1517

def unpause_all_list(scripts_to_unpause)
  if scripts_to_unpause.empty?
    Lich::Messaging.msg("plain", "DRC: #{Script.self.name} has finished.")
  else
    Lich::Messaging.msg("plain", "DRC: Unpausing #{scripts_to_unpause}, #{Script.self.name} has finished.")
    Script.running.find_all { |s| s.paused? && !s.no_pause_all && scripts_to_unpause.include?(s.name) }.each(&:unpause)
  end
end

.verify_script(script_names) ⇒ Boolean

Verifies that one or more scripts exist in the Lich scripts directory. Reports bold-colored messages for each missing script.

Examples:

DRC.verify_script('heal') #=> true or false
DRC.verify_script(['heal', 'buff']) #=> true or false

Parameters:

  • script_names (String, Array<String>) —

    script name(s) to verify

Returns:

  • (Boolean) —

    true if all scripts exist, false if any are missing



353
354
355
356
357
358
359
360
361
362
363
# File 'documented/dragonrealms/commons/common.rb', line 353

def verify_script(script_names)
  script_names = [script_names] unless script_names.is_a?(Array)
  state = true
  script_names
    .reject { |name| Script.exists?(name) }
    .each do |name|
      Lich::Messaging.msg("bold", "DRC: Failed to find a script named '#{name}'")
      state = false
    end
  state
end

.wait_for_script_to_complete(name, args = [], flags = {}) ⇒ Object?

Starts a script and pauses until it finishes running. First verifies the script exists. Pauses 2 seconds after starting, then polls Script.running until the returned handle is no longer present.

Examples:

DRC.wait_for_script_to_complete('heal', ['arg1'], :silent => true)

Parameters:

  • name (String) —

    the script name to start

  • args (Array) (defaults to: []) —

    positional arguments to pass to the script

  • flags (Hash) (defaults to: {}) —

    flags to pass to start_script (e.g., :silent => true)

Returns:

  • (Object, nil) —

    the script handle if started, nil if verification failed



375
376
377
378
379
380
381
382
383
# File 'documented/dragonrealms/commons/common.rb', line 375

def wait_for_script_to_complete(name, args = [], flags = {})
  verify_script(name)
  script_handle = start_script(name, args.map { |arg| arg.to_s =~ /\s/ ? "\"#{arg}\"" : arg }, flags)
  if script_handle
    pause 2
    pause 0.5 while Script.running.include?(script_handle)
  end
  script_handle
end

.wait_out_roundtime(wait_line) ⇒ Array<String>

Waits out a "...wait N seconds." refusal before bput sends the command again, reading the lines that arrive meanwhile. bput reads them again after the resend, as it would have without the hold, but skips the wait lines among them: they arrived before the resend, so they answer an earlier command or another script's, and the command is resent once rather than once per wait line. A line that matches the caller's patterns can't mean the command was accepted: it is just as often unrelated text matching a broad pattern like 'You'.

Parameters:

  • wait_line (String) —

    the wait line that triggered the hold

Returns:

  • (Array<String>) —

    the lines read during the hold, in order



321
322
323
324
325
326
327
328
329
330
331
332
# File 'documented/dragonrealms/commons/common.rb', line 321

def wait_out_roundtime(wait_line)
  held = []
  deadline = Time.now + wait_line.match(WAIT_RESPONSE_PATTERN)[:seconds].to_i - 0.5
  loop do
    read_held_lines(held)
    break if Time.now >= deadline

    pause 0.1
  end
  waitrt?
  read_held_lines(held)
end