Class: Lich::Common::GameObj

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

Overview

Represents a game object in the Lich system.

Examples:

Creating a game object

obj = GameObj.new(1, "noun", "name")

Direct Known Subclasses

RoomObj

Constant Summary collapse

@@loot =
Array.new
@@npcs =
Array.new
@@npc_status =
Hash.new
@@pcs =
Array.new
@@pc_status =
Hash.new
@@inv =
Array.new
@@contents =
Hash.new
@@right_hand =
nil
@@left_hand =
nil
@@room_desc =
Array.new
@@fam_loot =
Array.new
@@fam_npcs =
Array.new
@@fam_pcs =
Array.new
@@fam_room_desc =
Array.new
@@type_data =
Hash.new
@@type_cache =
Hash.new
@@sellable_data =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, noun, name, before = nil, after = nil) ⇒ GameObj

Initializes a new game object.

Parameters:

  • id (String)

    The object ID.

  • noun (String)

    The object noun.

  • name (String)

    The object name.

  • before (String, nil) (defaults to: nil)

    The string to prepend to the name.

  • after (String, nil) (defaults to: nil)

    The string to append to the name.



35
36
37
38
39
40
41
42
43
44
45
# File 'documented/common/gameobj.rb', line 35

def initialize(id, noun, name, before = nil, after = nil)
  @id = id.is_a?(Integer) ? id.to_s : id
  @noun = noun
  @noun = 'lapis' if @noun == 'lapis lazuli'
  @noun = 'hammer' if @noun == "Hammer of Kai"
  @noun = 'ball' if @noun == "ball and chain" # DR item 'ball and chain' doesn't work.
  @noun = 'mother-of-pearl' if (@noun == 'pearl') and (@name =~ /mother\-of\-pearl/)
  @name = name
  @before_name = before
  @after_name = after
end

Instance Attribute Details

#after_nameObject

Returns the value of attribute after_name.



26
27
28
# File 'documented/common/gameobj.rb', line 26

def after_name
  @after_name
end

#before_nameObject

Returns the value of attribute before_name.



26
27
28
# File 'documented/common/gameobj.rb', line 26

def before_name
  @before_name
end

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'documented/common/gameobj.rb', line 25

def id
  @id
end

#nameObject

Returns the value of attribute name.



26
27
28
# File 'documented/common/gameobj.rb', line 26

def name
  @name
end

#nounObject

Returns the value of attribute noun.



26
27
28
# File 'documented/common/gameobj.rb', line 26

def noun
  @noun
end

Class Method Details

.[](val) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'documented/common/gameobj.rb', line 124

def GameObj.[](val)
  unless val.is_a?(String) || val.is_a?(Regexp)
    respond "--- Lich: error: GameObj[] passed with #{val.class} #{val} via caller: #{caller[0]}"
    respond "--- Lich: error: GameObj[] supports String or Regexp only"
    Lich.log "--- Lich: error: GameObj[] passed with #{val.class} #{val} via caller: #{caller[0]}\n\t"
    Lich.log "--- Lich: error: GameObj[] supports String or Regexp only\n\t"
    if val.is_a?(Integer)
      respond "--- Lich: error: GameObj[] converted Integer #{val} to String to continue"
      val = val.to_s
    else
      return
    end
  end
  if val.is_a?(String)
    if val =~ /^\-?[0-9]+$/ # ID lookup
      # excludes @@room_desc ID lookup due to minimal use case, but could be added in future if desired
      @@inv.find { |o| o.id == val } || @@loot.find { |o| o.id == val } || @@npcs.find { |o| o.id == val } || @@pcs.find { |o| o.id == val } || [@@right_hand, @@left_hand].find { |o| o.id == val } || @@room_desc.find { |o| o.id == val } || @@contents.values.flatten.find { |o| o.id == val }
    elsif val.split(' ').length == 1 # noun lookup
      @@inv.find { |o| o.noun == val } || @@loot.find { |o| o.noun == val } || @@npcs.find { |o| o.noun == val } || @@pcs.find { |o| o.noun == val } || [@@right_hand, @@left_hand].find { |o| o.noun == val } || @@room_desc.find { |o| o.noun == val }
    else # name lookup
      @@inv.find { |o| o.name == val } || @@loot.find { |o| o.name == val } || @@npcs.find { |o| o.name == val } || @@pcs.find { |o| o.name == val } || [@@right_hand, @@left_hand].find { |o| o.name == val } || @@room_desc.find { |o| o.name == val } || @@inv.find { |o| o.name =~ /\b#{Regexp.escape(val.strip)}$/i } || @@loot.find { |o| o.name =~ /\b#{Regexp.escape(val.strip)}$/i } || @@npcs.find { |o| o.name =~ /\b#{Regexp.escape(val.strip)}$/i } || @@pcs.find { |o| o.name =~ /\b#{Regexp.escape(val.strip)}$/i } || [@@right_hand, @@left_hand].find { |o| o.name =~ /\b#{Regexp.escape(val.strip)}$/i } || @@room_desc.find { |o| o.name =~ /\b#{Regexp.escape(val.strip)}$/i } || @@inv.find { |o| o.name =~ /\b#{Regexp.escape(val).sub(' ', ' .*')}$/i } || @@loot.find { |o| o.name =~ /\b#{Regexp.escape(val).sub(' ', ' .*')}$/i } || @@npcs.find { |o| o.name =~ /\b#{Regexp.escape(val).sub(' ', ' .*')}$/i } || @@pcs.find { |o| o.name =~ /\b#{Regexp.escape(val).sub(' ', ' .*')}$/i } || [@@right_hand, @@left_hand].find { |o| o.name =~ /\b#{Regexp.escape(val).sub(' ', ' .*')}$/i } || @@room_desc.find { |o| o.name =~ /\b#{Regexp.escape(val).sub(' ', ' .*')}$/i }
    end
  elsif val.is_a?(Regexp) # name only lookup when passed a Regexp
    @@inv.find { |o| o.name =~ val } || @@loot.find { |o| o.name =~ val } || @@npcs.find { |o| o.name =~ val } || @@pcs.find { |o| o.name =~ val } || [@@right_hand, @@left_hand].find { |o| o.name =~ val } || @@room_desc.find { |o| o.name =~ val }
  end
end

Instance Method Details

#contentsArray

Retrieves the contents of the game object.

Returns:

  • (Array)

    A duplicate of the contents array.



120
121
122
# File 'documented/common/gameobj.rb', line 120

def contents
  @@contents[@id].dup
end

#empty?Boolean

Checks if the game object is empty.

Returns:

  • (Boolean)

    Always returns false.



114
115
116
# File 'documented/common/gameobj.rb', line 114

def empty?
  false
end

#full_nameString

Constructs the full name of the game object.

Returns:

  • (String)

    The full name including before and after strings.



157
158
159
# File 'documented/common/gameobj.rb', line 157

def full_name
  "#{@before_name}#{' ' unless @before_name.nil? or @before_name.empty?}#{name}#{' ' unless @after_name.nil? or @after_name.empty?}#{@after_name}"
end

#GameObjObject



151
152
153
# File 'documented/common/gameobj.rb', line 151

def GameObj
  @noun
end

#sellableString?

Retrieves the sellable status of the game object.

Returns:

  • (String, nil)

    A comma-separated string of sellable types or nil if none found.



70
71
72
73
74
75
76
77
78
# File 'documented/common/gameobj.rb', line 70

def sellable
  GameObj.load_data if @@sellable_data.empty?
  list = @@sellable_data.keys.find_all { |t| (@name =~ @@sellable_data[t][:name] or @noun =~ @@sellable_data[t][:noun]) and (@@sellable_data[t][:exclude].nil? or @name !~ @@sellable_data[t][:exclude]) }
  if list.empty?
    nil
  else
    list.join(',')
  end
end

#statusString?

Retrieves the current status of the game object.

Returns:

  • (String, nil)

    The status of the object or β€˜gone’ if not found.



82
83
84
85
86
87
88
89
90
91
92
# File 'documented/common/gameobj.rb', line 82

def status
  if @@npc_status.keys.include?(@id)
    @@npc_status[@id]
  elsif @@pc_status.keys.include?(@id)
    @@pc_status[@id]
  elsif @@loot.find { |obj| obj.id == @id } or @@inv.find { |obj| obj.id == @id } or @@room_desc.find { |obj| obj.id == @id } or @@fam_loot.find { |obj| obj.id == @id } or @@fam_npcs.find { |obj| obj.id == @id } or @@fam_pcs.find { |obj| obj.id == @id } or @@fam_room_desc.find { |obj| obj.id == @id } or (@@right_hand.id == @id) or (@@left_hand.id == @id) or @@contents.values.find { |list| list.find { |obj| obj.id == @id } }
    nil
  else
    'gone'
  end
end

#status=(val) ⇒ Object

Sets the status of the game object.

Parameters:

  • val (String)

    The status value to set.



96
97
98
99
100
101
102
103
104
# File 'documented/common/gameobj.rb', line 96

def status=(val)
  if @@npcs.any? { |npc| npc.id == @id }
    @@npc_status[@id] = val
  elsif @@pcs.any? { |pc| pc.id == @id }
    @@pc_status[@id] = val
  else
    nil
  end
end

#to_sString

Returns a string representation of the game object.

Returns:

  • (String)

    The noun of the game object.



108
109
110
# File 'documented/common/gameobj.rb', line 108

def to_s
  @noun
end

#typeString?

Retrieves the type of the game object based on its name and noun.

Returns:

  • (String, nil)

    A comma-separated string of types or nil if none found.



49
50
51
52
53
54
55
56
57
58
# File 'documented/common/gameobj.rb', line 49

def type
  GameObj.load_data if @@type_data.empty?
  return @@type_cache[@name] if @@type_cache.key?(@name)
  list = @@type_data.keys.find_all { |t| (@name =~ @@type_data[t][:name] or @noun =~ @@type_data[t][:noun]) and (@@type_data[t][:exclude].nil? or @name !~ @@type_data[t][:exclude]) }
  if list.empty?
    return @@type_cache[@name] = nil
  else
    return @@type_cache[@name] = list.join(',')
  end
end

#type?(type_to_check) ⇒ Boolean

Checks if the game object is of a specific type.

Parameters:

  • type_to_check (String)

    The type to check against.

Returns:

  • (Boolean)

    True if the object is of the specified type, false otherwise.



63
64
65
66
# File 'documented/common/gameobj.rb', line 63

def type?(type_to_check)
  # handle nil types
  return self.type.to_s.split(',').any?(type_to_check)
end