Class: Olib::Item

Inherits:
Gameobj_Extender show all
Defined in:
lib/Olib/item.rb

Overview

special item wrapper class to wrap item types with custom, common interactions becoming syntactic sugar

Instance Attribute Summary collapse

Attributes inherited from Gameobj_Extender

#type

Instance Method Summary collapse

Methods inherited from Gameobj_Extender

#__extend__

Constructor Details

#initialize(obj = nil) ⇒ Item

Returns a new instance of Item.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/Olib/item.rb', line 9

def initialize(obj=nil)
  @props = Hash.new

  @props[:name]       = obj.name
  @props[:after_name] = obj.after_name

  define :tags, []
  obj.type.split(',').map { |t| tag(t) }

  if is?('jar') && @props[:after_name] =~ /containing (.*+?)/
    tag Dictionary.gems[:singularize].call @props[:after_name].gsub('containing', '').strip
  end

  if is?('gem')
    tag Dictionary.gems[:singularize].call @props[:name]
  end

  if is?('jar') && @props[:after_name].nil?
    tag('empty')
  end

  if Vars.teleporter && Vars.teleporter == @props[:name]
    tag('teleporter')
  end

  super(obj)
end

Instance Attribute Details

#propsObject

Returns the value of attribute props.



7
8
9
# File 'lib/Olib/item.rb', line 7

def props
  @props
end

Instance Method Details

#_drag(target) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/Olib/item.rb', line 128

def _drag(target)
  Olib.wrap("_drag ##{@id} ##{target.id}") { |line|
    # order of operations is important here for jars

    raise Olib::Errors::DoesntExist          if line =~ Dictionary.put[:failure][:ne]
    raise Olib::Errors::Mundane              if line =~ Dictionary.put[:success]
    
    if line =~ Dictionary.put[:failure][:full]
      tag 'full'
      raise Olib::Errors::ContainerFull
    end
  }
  self
end

#_inspectObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/Olib/item.rb', line 315

def _inspect

  return self if has? 'inspect'

  in_inspect = false

  Olib.wrap_stream(action 'inspect') { |line|

    raise Olib::Errors::Mundane  if line =~ /^<prompt/ and in_inspect

    # skip first inspect line because it's useless for info

    if line =~ /You carefully inspect|You carefully count|goat/
      in_inspect = true
    end


    if in_inspect
      
      if line =~ /^You estimate that (?:.*?) can store (?:a|an|some) ([a-zA-Z -]+) amount with enough space for ([a-zA-Z ]+)/
        @props['space']           = $1
        @props['number_of_items'] = $2
      end
        

      
      if line =~ /^You determine that you could wear the (.*?) ([a-zA-Z ]+)/
        @props['location']= $2
      end
      
      if line =~ /allows you to conclude that it is ([a-zA-Z ]+)/

        if line =~ Dictionary.size
          @props['shield_type'] = $1
        else
          Dictionary.armors.each do |type, re| @props['armor_type'] = type if line =~ re end
        end
        
      end

      if line =~ /suitable for use in unarmed combat/
        @props['weapon_type']= "uac"
      end

      if line =~ /requires skill in ([a-zA-Z ]+) to use effectively/
    
        @props['weapon_type']= $1
        if line =~ /It appears to be a modified ([a-zA-Z -]+)/
          @props['weapon_base']= $1
        else
          @props['weapon_base']= @noun
        end
      end            
      
      if line =~ /^It looks like this item has been mainly crafted out of ([a-zA-Z -]+)./
        @props['material']= $1
        raise Olib::Errors::Mundane
      end
      
      if line =~ /can hold liquids/
        @props['liquid_container']=true
      end

    end
    
  }
  
  return self
end

#acquire_from_shopObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/Olib/item.rb', line 93

def acquire_from_shop
  take
  if pullable?
    return pull
  else
    Olib.wrap(action "buy"){ |line|
      raise Olib::Errors::InsufficientFunds if line =~ /The merchant frowns/
      raise Olib::Errors::Mundane            if line =~ /You hand over/
    }
  end
  self
end

#action(verb) ⇒ Object



179
180
181
# File 'lib/Olib/item.rb', line 179

def action(verb)
  "#{verb} ##{@id}"
end

#add(*items) ⇒ Object



183
184
185
186
187
188
# File 'lib/Olib/item.rb', line 183

def add(*items)
  items.each { |item|
    item._drag(self)
  }
  self
end

#analyzeObject



281
282
283
# File 'lib/Olib/item.rb', line 281

def analyze
  # reserved

end

#buyObject



68
69
70
71
# File 'lib/Olib/item.rb', line 68

def buy
  fput action 'buy'
  self
end

#buyable?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/Olib/item.rb', line 85

def buyable?
  is? 'buyable'
end

#costObject



89
90
91
# File 'lib/Olib/item.rb', line 89

def cost
  @props['cost']
end

#crawlObject



62
63
64
65
66
# File 'lib/Olib/item.rb', line 62

def crawl
  take
  tap._inspect.read.look
  self
end

#define(key, val) ⇒ Object



190
191
192
193
# File 'lib/Olib/item.rb', line 190

def define(key, val)
  @props[key] = val
  self
end

#give(target, onfailure = nil) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/Olib/item.rb', line 224

def give(target, onfailure=nil)
  Olib.wrap_stream("give ##{@id} to #{target}", 30) { |line|
    
  }

  self
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/Olib/item.rb', line 73

def has?(key)
  !@props[key].nil?
end

#inObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/Olib/item.rb', line 106

def in
  return self if has? 'contents'
  Olib.wrap(action 'look in') { |line|
    raise Olib::Errors::Mundane      if line=~/^There is nothing in there./
    
    # handle jar data

    if line =~ /Inside (.*?) you see (?<number>[\d]+) portion(|s) of (?<type>.*?).  It is (more than |less than|)(?<percent>[a-z ]+)./ 
      data = line.match(/Inside (.*?) you see (?<number>[\d]+) portion(|s) of (?<type>.*?).  It is (more than |less than|)(?<percentage>[a-z ]+)./)
      tag data[:percentage] == 'full' ? "full" : "partial"
      define :number, data[:number].to_i
      raise Olib::Errors::Mundane
    end

    #handle empty jars

    if line =~ /The (.*?) is empty./
      tag 'empty'
      raise Olib::Errors::Mundane
    end
  }
  self
end

#is?(tag) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/Olib/item.rb', line 41

def is?(tag)
  @props[:tags].include?(tag)
end

#lookObject



384
385
386
387
388
389
390
391
# File 'lib/Olib/item.rb', line 384

def look
  return self if has? 'show'
  Olib.wrap(action 'look') { |line|
    raise Olib::Errors::Mundane      if line=~/^You see nothing unusual.|^You can't quite get a good look at/
    define 'show', line  unless line=~/<prompt|You take a closer look/
  }
  self
end

#missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/Olib/item.rb', line 77

def missing?(key)
  !has?(key)
end

#priceObject



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/Olib/item.rb', line 403

def price
  return self if(has? 'price' or has? 'info')
  Olib.wrap(action 'get') { |line|

    if line =~ /(\d+) silvers/
      define 'price', line.match(/(?<price>\d+) silvers/)[:price]
      raise Olib::Errors::Mundane
    end

    if line =~ /You can't pick that up/
      define "info", true
      raise Olib::Errors::Mundane
    end

    Script.log "unmatched price: #{line}"
    
  }
  self
end

#pull(onfailure = nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/Olib/item.rb', line 195

def pull(onfailure=nil)
  refresh_instance = false
  original_right   = GameObj.right_hand
  original_left    = GameObj.left_hand
  Olib.wrap(action "pull") { |line|
    
    if line =~ /^You pull/
      if line =~ /There (are|is) ([\d]+) left/
        refresh_instance = true
      end
      raise Olib::Errors::Mundane
    end

    if line =~ /I'm afraid that you can't pull that./
      if onfailure
        onfailure.call(self)
      else 
        raise Olib::Errors::DoesntExist 
      end
    end
  }
  # for stacked items in shops

  if refresh_instance
    return Item.new(GameObj.left_hand) if original_left.nil? && !GameObj.left_hand.nil?
    return Item.new(GameObj.right_hand)
  end
  self
end

#pullable?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/Olib/item.rb', line 81

def pullable?
  is? 'pullable'
end

#readObject



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/Olib/item.rb', line 423

def read
  return self if has? 'read'
  scroll    = false
  multiline = false
  Olib.wrap_stream(action 'read') {  |line|

    raise Olib::Errors::Mundane  if line =~ /^<prompt/ and (multiline or scroll)
    raise Olib::Errors::Mundane if line =~ /There is nothing there to read|You can't do that./

    # if we are in a multiline state

    @props['read'] = @props['read'].concat line if multiline

    # capture spell

    if scroll && line =~ /\(([0-9]+)\) ([a-zA-Z'\s]+)/
        n    = $1
        name = $2
        spell = {'n' => $1, 'name' => $2}
        #Client.notify "Spell detected ... (#{$1}) #{$2}"

        @props['spells'].push spell

    # begin scroll

    elsif line =~ /It takes you a moment to focus on the/
      scroll = true
      @props['spells'] = Array.new 

    # open multiline

    elsif line =~ /^In the (.*?) language, it reads/
      multiline          = true
      @props['read']     = "#{line}\n"
      @props['language'] = $1

    # alert to unknown

    elsif line =~ /but the language is not one you know.  It looks like it's written in (.*?)./
      Script.log "Please find a friend that can read for #{$1} in #{XMLData.room_title}"
      echo "Please find a friend that can read for #{$1} in #{XMLData.room_title}"
      raise Olib::Errors::Mundane
   
    end
    
  }
  return self
end

#remove(onfailure = nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/Olib/item.rb', line 232

def remove(onfailure=nil)
  
  unless GameObj.inv.map(&:id).include? @id
    if onfailure
      onfailure.call(self)
    else 
      raise Olib::Errors::DoesntExist 
    end
  end
  
  Olib.wrap(action "remove") { |line|        
    if line =~ /You cannot remove|You better get a sharp knife/
      if onfailure
        onfailure.call(self)
      else 
        raise Olib::Errors::DoesntExist 
      end
    end
      
    raise Olib::Errors::Mundane if GameObj.right_hand.id == @id || GameObj.left_hand.id == @id
  }

  self
end

#shakeObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/Olib/item.rb', line 146

def shake
  # make sure we have a count so we need to match fewer lines

  self.in if is? 'jar' and missing? :number
  
  Olib.wrap(action "shake"){ |line|
    raise Olib::Errors::Fatal    if line =~ /you realize that it is empty/
    if line =~ /fall into your/
      @props[:number] = @props[:number]-1
      raise Olib::Errors::Fatal.new "Jar is now empty\n you should rescue this to handle it gracefully" if @props[:number] == 0
      raise Olib::Errors::Mundane
    end
  }
  self
end

#shop_sell(amt) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/Olib/item.rb', line 161

def shop_sell(amt)
  if GameObj.right_hand.id != @id
    raise Olib::Errors::Fatal
  end

  Olib.wrap("shop sell #{amt}") {|line|
    raise Olib::Errors::Mundane if line =~ /^You place your/
    raise Olib::Errors::Fatal   if line =~ /There's no more room for anything else right now./
  }

end

#stashObject



142
143
144
# File 'lib/Olib/item.rb', line 142

def stash
  _drag GameObj[props['container']]
end

#tag(*tags) ⇒ Object



45
46
47
48
# File 'lib/Olib/item.rb', line 45

def tag(*tags)
  @props[:tags].concat(tags)
  self
end

#tagsObject



54
55
56
# File 'lib/Olib/item.rb', line 54

def tags
  @props[:tags]
end

#takeObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/Olib/item.rb', line 285

def take
  return self if has? 'cost'

  Olib.wrap(action 'get') { |line|
    raise Errors::DoesntExist    if line=~ Olib::Dictionary.get[:failure][:ne]
    raise Errors::HandsFull      if line=~ Olib::Dictionary.get[:failure][:hands_full]
    raise Errors::TooHeavy       if line=~ Olib::Dictionary.get[:failure][:weight]
    
    if line=~ Olib::Dictionary.get[:success] 
      raise Olib::Errors::Mundane
    end  

    if line =~ Olib::Dictionary.get[:failure][:buy]
      define 'cost', line.match(Olib::Dictionary.get[:failure][:buy])[:cost].to_i
    end

    if line=~ /You'll have to buy it if you want it/
      tag 'buyable'
      raise Olib::Errors::Mundane
    end

    if line=~ /You can PULL/
      tag 'pullable'
      raise Olib::Errors::Mundane
    end

  }
  self
end

#tapObject



393
394
395
396
397
398
399
400
401
# File 'lib/Olib/item.rb', line 393

def tap
  return self if has? 'description'
  Olib.wrap(action 'tap') { |line|
    next unless line=~ /You tap (.*?) (on|in)/
    define 'description', $1 
    raise Olib::Errors::Mundane         
  }
  self
end

#to_sObject



37
38
39
# File 'lib/Olib/item.rb', line 37

def to_s
  @props.to_s
end

#turnObject



174
175
176
177
# File 'lib/Olib/item.rb', line 174

def turn
  fput action 'turn'
  self
end

#untag(*remove) ⇒ Object



50
51
52
# File 'lib/Olib/item.rb', line 50

def untag(*remove)
  @props[:tags] = @props[:tags].select { |tag| !remove.include?(tag) }
end

#wear(onfailure = nil) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/Olib/item.rb', line 257

def wear(onfailure=nil)
  if GameObj.right_hand.id != @id || GameObj.left_hand.id != @id
    if onfailure
      onfailure.call(self)
    else 
      raise Olib::Errors::DoesntExist 
    end      
  end

  Olib.wrap(action "wear") { |line|        
    if line =~ /You can't wear that.|You can only wear/
      if onfailure
        onfailure.call(self)
      else 
        raise Olib::Errors::DoesntExist 
      end
    end
    raise Olib::Errors::Mundane if GameObj.inv.map(&:id).include? @id
  }

  self

end

#worn?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/Olib/item.rb', line 58

def worn?
  GameObj.inv.collect { |item| item.id }.include? @id
end