Module: Lich::Gemstone::Stance

Defined in:
documented/gemstone/stance.rb

Overview

Read and change the character's combat stance.

Combines the stance-change logic that historically lived in many scripts (Spell#cast, bigshot, ebounty, eloot, ecleanse) into a shared interface that sends the command and waits for the game to confirm. Handles both named stances and numeric percent-of-defense values when Stance Perfection (cman stance N) is trained.

Constant Summary collapse

NAMES =

Stance names recognized by the game, ordered from most offensive to most defensive.

Each name is a valid argument to stance or cman stance commands. Used to validate input and map numeric ranges to band names.

Returns:

See Also:

%w[offensive advance forward neutral guarded defensive].freeze
BANDS =

Percent-of-defense band each named stance occupies. The game reports stance_value as a 0..100 number; checkstance has always used these ranges to decide which name a value belongs to.

{
  'offensive' => (0..0),
  'advance'   => (1..20),
  'forward'   => (21..40),
  'neutral'   => (41..60),
  'guarded'   => (61..80),
  'defensive' => (81..100),
}.freeze
ACCEPTED =

The lines that mean the stance change took effect.

[
  /^You (?:are now in|move into) an? \w+ stance/,
  /^You fall back into an? \w+ stance/,
].freeze
DECLINED =

The lines that mean the game declined to change stance.

[
  /^You are unable to change your stance\./,
  /^Cast Roundtime in effect/,
].freeze
CONFIRM =

Every line the game can answer a stance change with. Shared with Spell#cast, which used to carry its own copy. Derived from ACCEPTED and DECLINED so the set dothistimeout waits on can never drift out of sync with the set change treats as a refusal.

Regexp.union(*ACCEPTED, *DECLINED).freeze
REFUSED =

Pattern matching game responses that refuse a stance change.

Matches when the game explicitly declines to change stance (e.g., unable to change during a cast roundtime or due to other game state). Used by .change to distinguish refusal from acceptance.

Examples:

"Cast Roundtime in effect" =~ Stance::REFUSED #=> 0

Returns:

  • (Regexp)

See Also:

Regexp.union(*DECLINED).freeze
DEFAULT_TIMEOUT =
3

Class Method Summary collapse

Class Method Details

.at?(target) ⇒ Boolean

Returns whether the character is already in the target stance.

Returns:

  • (Boolean) —

    whether the character is already in the target stance



142
143
144
# File 'documented/gemstone/stance.rb', line 142

def self.at?(target)
  at_normalized?(*normalize(target))
end

.change(target, wait: true, force: false, timeout: DEFAULT_TIMEOUT) ⇒ Boolean

Change stance and, by default, wait for the game to confirm.

Parameters:

  • target (String, Symbol, Integer) —

    see normalize

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

    wait for CONFIRM (true) or fire through fput (false)

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

    send even when already in the target stance

  • timeout (Numeric) (defaults to: DEFAULT_TIMEOUT) —

    seconds to wait for confirmation

Returns:

  • (Boolean) —

    true when in the requested stance, false when the game refused, no confirmation arrived, or the character is dead



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'documented/gemstone/stance.rb', line 166

def self.change(target, wait: true, force: false, timeout: DEFAULT_TIMEOUT)
  name, percent = normalize(target)
  return false if Status.dead?
  return true if !force && at_normalized?(name, percent)

  command = if percent && perfection?
              "cman stance #{percent}"
            else
              "stance #{name}"
            end

  unless wait
    fput command
    return true
  end

  waitrt?
  result = dothistimeout(command, timeout, CONFIRM)
  return false if result.nil?
  return false if result =~ REFUSED
  true
end

.current ⇒ String

Returns current stance name as the game reports it.

Returns:

  • (String) —

    current stance name as the game reports it



91
92
93
# File 'documented/gemstone/stance.rb', line 91

def self.current
  Char.stance
end

.normalize(target) ⇒ Array(String, Integer|nil)

Resolve anything a script might hand us into [name, percent].

Parameters:

  • target (String, Symbol, Integer) —

    a stance name, a prefix (off/adv/for/neu/gua/def), or a multiple of ten from 0 to 100

Returns:

  • (Array(String, Integer|nil)) —

    the band name and, for numeric targets, the exact percent requested

Raises:

  • (ArgumentError) —

    when target is not a stance



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'documented/gemstone/stance.rb', line 124

def self.normalize(target)
  case target
  when Integer
    normalize_percent(target)
  when Symbol
    normalize(target.to_s)
  when String
    stripped = target.strip.downcase
    return normalize_percent(stripped.to_i) if stripped =~ /\A\d+\z/
    name = NAMES.find { |n| n.start_with?(stripped) } if stripped.length >= 3
    raise ArgumentError, "Stance: unknown stance #{target.inspect}" if name.nil?
    [name, nil]
  else
    raise ArgumentError, "Stance: unknown stance #{target.inspect}"
  end
end

.perfection? ⇒ Boolean

Returns whether Stance Perfection (cman stance N) is trained.

Returns:

  • (Boolean) —

    whether Stance Perfection (cman stance N) is trained



101
102
103
104
105
106
# File 'documented/gemstone/stance.rb', line 101

def self.perfection?
  return false unless defined?(Lich::Gemstone::CMan)
  Lich::Gemstone::CMan.known?('stance_perfection')
rescue StandardError
  false
end

.safest ⇒ String

The stance to fall back to when the caller just wants to be safe: defensive, unless a cast roundtime is running, in which case the game will not allow defensive and guarded is the best available.

Returns:



113
114
115
# File 'documented/gemstone/stance.rb', line 113

def self.safest
  checkcastrt > 0 ? 'guarded' : 'defensive'
end

.value ⇒ Integer

Returns current stance as a 0..100 percentage.

Returns:

  • (Integer) —

    current stance as a 0..100 percentage



96
97
98
# File 'documented/gemstone/stance.rb', line 96

def self.value
  Char.percent_stance
end