Class: Lich::DragonRealms::DRC::Item

Inherits:
Object
  • Object
show all
Defined in:
documented/dragonrealms/commons/common.rb

Overview

Items class. Name is the noun of the object. Leather/metal boolean. Is the item worn (defaults to true). Does it hinder lockpicking? (false) Item.new(name:'gloves', leather:true, worn:true, hinders_locks:true, adjective:'ring', bound:true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, leather: nil, worn: false, hinders_locks: nil, container: nil, swappable: false, tie_to: nil, adjective: nil, bound: false, wield: false, transforms_to: nil, transform_text: nil, transform_verb: nil, lodges: true, skip_repair: false, ranged: nil, needs_unloading: nil) ⇒ void

Initializes an Item instance with equipment properties. Auto-detects whether the item is a ranged weapon if ranged is not specified. Auto-sets needs_unloading to match ranged status unless specified.

Examples:

DRC::Item.new(name: 'sword', leather: false, worn: true)
DRC::Item.new(name: 'crate', adjective: 'wooden', container: 'storage')

Parameters:

  • name (String, nil) (defaults to: nil) —

    the item noun

  • leather (Boolean, nil) (defaults to: nil) —

    whether the item is leather (vs. metal)

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

    whether the item is worn on the body (default false)

  • hinders_locks (Boolean, nil) (defaults to: nil) —

    whether wearing this item affects lockpicking success

  • container (String, nil) (defaults to: nil) —

    container noun if the item holds things

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

    whether the item can be swapped to/from hands (default false)

  • tie_to (String, nil) (defaults to: nil) —

    a limb to tie/attach the item to

  • adjective (String, nil) (defaults to: nil) —

    the adjective part of a two-word item name

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

    whether the item is bound to the character (default false)

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

    whether the item should be actively wielded (default false)

  • transforms_to (String, nil) (defaults to: nil) —

    noun the item transforms into after use

  • transform_text (String, nil) (defaults to: nil) —

    game output text indicating the transform

  • transform_verb (String, nil) (defaults to: nil) —

    verb to trigger the transformation

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

    whether the item can lodge in a body part (default true)

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

    whether to skip repair for this item (default false)

  • ranged (Boolean, nil) (defaults to: nil) —

    whether this is a ranged weapon; auto-detected if nil

  • needs_unloading (Boolean, nil) (defaults to: nil) —

    whether the item needs unloading after use; defaults to match ranged status if nil



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'documented/dragonrealms/commons/common.rb', line 732

def initialize(name: nil, leather: nil, worn: false, hinders_locks: nil, container: nil, swappable: false, tie_to: nil, adjective: nil, bound: false, wield: false, transforms_to: nil, transform_text: nil, transform_verb: nil, lodges: true, skip_repair: false, ranged: nil, needs_unloading: nil)
  @name = name
  @leather = leather
  @worn = worn
  @hinders_lockpicking = hinders_locks
  @container = container
  @swappable = swappable
  @tie_to = tie_to
  @adjective = adjective
  @bound = bound
  @wield = wield
  @transforms_to = transforms_to
  @transform_verb = transform_verb
  @transform_text = transform_text
  @lodges = lodges.nil? ? true : lodges
  @skip_repair = skip_repair
  @ranged = ranged.nil? ? ranged_weapon?(name) : ranged
  @needs_unloading = needs_unloading.nil? ? @ranged : needs_unloading
end

Instance Attribute Details

#adjective ⇒ Object (readonly)

Returns the value of attribute adjective.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def adjective
  @adjective
end

#bound ⇒ Object (readonly)

Returns the value of attribute bound.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def bound
  @bound
end

#container ⇒ Object (readonly)

Returns the value of attribute container.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def container
  @container
end

#hinders_lockpicking ⇒ Object (readonly)

Returns the value of attribute hinders_lockpicking.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def hinders_lockpicking
  @hinders_lockpicking
end

#leather ⇒ Object (readonly)

Returns the value of attribute leather.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def leather
  @leather
end

#lodges ⇒ Object (readonly)

Returns the value of attribute lodges.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def lodges
  @lodges
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def name
  @name
end

#needs_unloading ⇒ Object (readonly)

Returns the value of attribute needs_unloading.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def needs_unloading
  @needs_unloading
end

#ranged ⇒ Object (readonly)

Returns the value of attribute ranged.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def ranged
  @ranged
end

#skip_repair ⇒ Object (readonly)

Returns the value of attribute skip_repair.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def skip_repair
  @skip_repair
end

#swappable ⇒ Object (readonly)

Returns the value of attribute swappable.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def swappable
  @swappable
end

#tie_to ⇒ Object (readonly)

Returns the value of attribute tie_to.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def tie_to
  @tie_to
end

#transform_text ⇒ Object (readonly)

Returns the value of attribute transform_text.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def transform_text
  @transform_text
end

#transform_verb ⇒ Object (readonly)

Returns the value of attribute transform_verb.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def transform_verb
  @transform_verb
end

#transforms_to ⇒ Object (readonly)

Returns the value of attribute transforms_to.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def transforms_to
  @transforms_to
end

#wield ⇒ Object (readonly)

Returns the value of attribute wield.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def wield
  @wield
end

#worn ⇒ Object (readonly)

Returns the value of attribute worn.



704
705
706
# File 'documented/dragonrealms/commons/common.rb', line 704

def worn
  @worn
end

Class Method Details

.from_text(text) ⇒ Object

Convenience method to parse the text of an item as shown when in your hands, with or without an adjective, into an Item class instance. Originally designed to support DRCI and equipmanager methods.



794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'documented/dragonrealms/commons/common.rb', line 794

def self.from_text(text)
  return nil if text.nil? || text.to_s.strip.empty?

  normalized = text
               .sub('.', ' ') # convert 'foo.bar' => 'foo bar' so more easily split into adjective and noun
               .squeeze(' ') # condense repeated runs of whitespace with a single space
               .strip # remove leading/trailing whitespace
  parts = normalized.split
  if parts.size > 1
    DRC::Item.new(adjective: parts.first, name: parts.last)
  else
    DRC::Item.new(name: normalized)
  end
end

Instance Method Details

#ranged_weapon?(noun) ⇒ Boolean

Checks whether a noun refers to a known ranged weapon (common or racial). Matches against COMMON_RANGED_WEAPONS_PATTERN and RACIAL_RANGED_WEAPONS_PATTERN.

Examples:

item.ranged_weapon?('bow') #=> true
item.ranged_weapon?('sword') #=> false

Parameters:

  • noun (String, nil) —

    the item noun

Returns:

  • (Boolean) —

    true if noun matches a known ranged weapon pattern, false otherwise



783
784
785
786
787
788
789
790
# File 'documented/dragonrealms/commons/common.rb', line 783

def ranged_weapon?(noun)
  return false if noun.nil?

  return true if COMMON_RANGED_WEAPONS_PATTERN.match?(noun)
  return true if RACIAL_RANGED_WEAPONS_PATTERN.match?(noun)

  false
end

#short_name ⇒ String

Returns a dot-separated short name combining adjective and noun. Used to uniquely identify items in storage commands where the adjective is necessary to disambiguate.

Examples:

item = DRC::Item.new(name: 'crate', adjective: 'wooden')
item.short_name #=> "wooden.crate"

Returns:

  • (String) —

    "adjective.noun" if adjective is set, else just "noun"



760
761
762
# File 'documented/dragonrealms/commons/common.rb', line 760

def short_name
  @adjective ? "#{@adjective}.#{@name}" : @name
end

#short_regex ⇒ Regexp

Returns a regex matching this item by adjective and noun. Used to verify the item is held or match it in room output.

Examples:

item = DRC::Item.new(name: 'bow', adjective: 'longbow')
item.short_regex #=> /\blongbow.*\bbow/i

Returns:

  • (Regexp) —

    pattern /\badjective.*\bnoun/i if adjective set, else /\bnoun/i



771
772
773
# File 'documented/dragonrealms/commons/common.rb', line 771

def short_regex
  @adjective ? /\b#{@adjective}.*\b#{@name}/i : /\b#{@name}/i
end