Class: Lich::Common::GameObj

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

Overview

Represents a game object in the Lich game.

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 GameObj instance.

Parameters:

  • id (Integer)

    the unique identifier for the game object

  • noun (String)

    the noun representing the game object

  • name (String)

    the name of the game object

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

    optional prefix for the name

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

    optional suffix for the name



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/common/gameobj.rb', line 33

def initialize(id, noun, name, before = nil, after = nil)
  @id = 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.



24
25
26
# File 'lib/common/gameobj.rb', line 24

def after_name
  @after_name
end

#before_nameObject

Returns the value of attribute before_name.



24
25
26
# File 'lib/common/gameobj.rb', line 24

def before_name
  @before_name
end

#idObject (readonly)

Returns the value of attribute id.



23
24
25
# File 'lib/common/gameobj.rb', line 23

def id
  @id
end

#nameObject

Returns the value of attribute name.



24
25
26
# File 'lib/common/gameobj.rb', line 24

def name
  @name
end

#nounObject

Returns the value of attribute noun.



24
25
26
# File 'lib/common/gameobj.rb', line 24

def noun
  @noun
end

Class Method Details

.[](val) ⇒ GameObj?

Retrieves a game object by its identifier, noun, or name.

Examples:

GameObj['some_id']

Parameters:

  • val (String, Regexp)

    the identifier, noun, or name to search for

Returns:

  • (GameObj, nil)

    the found game object or nil if not found



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/common/gameobj.rb', line 144

def GameObj.[](val)
  if val.class == String
    if val =~ /^\-?[0-9]+$/
      @@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 }
    elsif val.split(' ').length == 1
      @@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
      @@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.class == 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.

Examples:

game_obj.contents

Returns:

  • (Array)

    a duplicate of the contents array



134
135
136
# File 'lib/common/gameobj.rb', line 134

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

#empty?Boolean

Checks if the game object is empty.

Examples:

game_obj.empty?

Returns:

  • (Boolean)

    always returns false



125
126
127
# File 'lib/common/gameobj.rb', line 125

def empty?
  false
end

#full_nameString

Constructs the full name of the game object.

Examples:

game_obj.full_name

Returns:

  • (String)

    the full name including before and after names



172
173
174
# File 'lib/common/gameobj.rb', line 172

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

#GameObjString

Returns the noun of the game object.

Examples:

game_obj.GameObj

Returns:

  • (String)

    the noun of the game object



163
164
165
# File 'lib/common/gameobj.rb', line 163

def GameObj
  @noun
end

#sellableString?

Note:

This method will load sellable data if it is empty.

Checks if the game object is sellable.

Examples:

game_obj.sellable

Returns:

  • (String, nil)

    a comma-separated list of sellable types or nil if none found



68
69
70
71
72
73
74
75
76
# File 'lib/common/gameobj.rb', line 68

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 status of the game object.

Examples:

game_obj.status

Returns:

  • (String, nil)

    the status of the object or ‘gone’ if not found



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

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) ⇒ nil

Sets the status of the game object.

Examples:

game_obj.status = 'active'

Parameters:

  • val (String)

    the status to set

Returns:

  • (nil)

    returns nil if the object is not an NPC or PC



101
102
103
104
105
106
107
108
109
# File 'lib/common/gameobj.rb', line 101

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 the noun of the game object.

Examples:

game_obj.to_s

Returns:

  • (String)

    the noun of the game object



116
117
118
# File 'lib/common/gameobj.rb', line 116

def to_s
  @noun
end

#typeString?

Note:

This method will load type data if it is empty.

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

Examples:

game_obj.type

Returns:

  • (String, nil)

    a comma-separated list of types or nil if none found



51
52
53
54
55
56
57
58
59
60
# File 'lib/common/gameobj.rb', line 51

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