Class: Olib::Item

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

Overview

this is the structure for a base Object wraps an instance of GameObj and adds the ability for tags, queries

Direct Known Subclasses

Box, Clothing, Herb, Jar, Jewel, Jewelry, Scroll, Uncommon, Unknown, Wand

Instance Attribute Summary collapse

Attributes inherited from Gameobj_Extender

#type

Instance Method Summary collapse

Methods inherited from Gameobj_Extender

#__extend__, #at, #echo

Constructor Details

#initialize(obj) ⇒ Item

When created, it should be passed an instance of GameObj

Example:

Olib::Item.new(GameObj.right_hand)


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

def initialize(obj)
  @props              = Hash.new
  @props[:name]       = obj.name
  @props[:after_name] = obj.after_name
  @props[:before_name]= obj.before_name
  @props[:desc]       = [obj.before_name, obj.name, obj.after_name].compact.join(' ')
  @props[:noun]       = obj.noun
  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

#containerObject

Returns the value of attribute container.



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

def container
  @container
end

#propsObject

Returns the value of attribute props.



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

def props
  @props
end

Instance Method Details

#_drag(target) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/Olib/core/item.rb', line 153

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



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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/core/item.rb', line 397

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/Olib/core/item.rb', line 113

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

#action(verb) ⇒ Object



217
218
219
# File 'lib/Olib/core/item.rb', line 217

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

#add(*items) ⇒ Object



221
222
223
224
225
226
# File 'lib/Olib/core/item.rb', line 221

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

#affordable?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/Olib/core/item.rb', line 99

def affordable?
  take
  return true if pullable?
  cost <= Char.smart_wealth
end

#analyzeObject



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
# File 'lib/Olib/core/item.rb', line 324

def analyze
  fput "analyze ##{@id}"
  should_detect = false
  begin
    Timeout::timeout(1) do
      while(line = get)
        next                        if Olib::Dictionary.ignorable?(line)
        next                        if line =~ /sense that the item is free from merchant alteration restrictions|and sense that the item is largely free from merchant alteration restrictions|these can all be altered by a skilled merchant|please keep the messaging in mind when designing an alterations|there is no recorded information on that item|The creator has also provided the following information/
        @props['max_light'] = true  if line =~ /light as it can get/
        @props['max_deep']  = true  if line =~ /pockets could not possibly get any deeper/
        @props['max_deep']  = false if line =~ /pockets deepened/
        @props['max_light'] = false if line =~ /talented merchant lighten/
        if line =~ /Casting Elemental Detection/
          should_detect = true 
          next 
        end
        break                       if line =~ /pockets deepened|^You get no sense of whether|light as it can get|pockets could not possibly get any deeper|talented merchant lighten/
        @props['analyze'] = String.new unless @props['analyze']
        @props['analyze'].concat line.strip
        @props['analyze'].concat " "
      end
    end
  
  rescue Timeout::Error
    # Silent
  end
  detect if should_detect
  temp_analysis = @props['analyze'].split('.').map(&:strip).map(&:downcase).reject {|ln| ln.empty? }
  @props['analyze'] = temp_analysis unless temp_analysis.empty?
  return self
end

#buyObject



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

def buy
  Char.deplete_wealth cost
  fput action 'buy'
  self
end

#buyable?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/Olib/core/item.rb', line 105

def buyable?
  is? 'buyable'
end

#costObject



109
110
111
# File 'lib/Olib/core/item.rb', line 109

def cost
  @props['cost']
end

#crawlObject



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

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

#define(key, val) ⇒ Object



228
229
230
231
# File 'lib/Olib/core/item.rb', line 228

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

#dropObject



391
392
393
394
395
# File 'lib/Olib/core/item.rb', line 391

def drop
  Script.log("#{Time.now} > dropped #{to_s}")
  fput action "drop"
  self
end

#exists?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/Olib/core/item.rb', line 91

def exists?
  GameObj[@id].nil?
end

#give(target, onfailure = nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/Olib/core/item.rb', line 262

def give(target, onfailure=nil)
  Olib.wrap_stream("give ##{@id} to #{target}", 30) { |line|
    raise Olib::Errors::Mundane if line=~ /This looks perfect/

    if line =~ /glances at you and moves out of your reach./
      raise Olib::Errors::Fatal.new "You appear to have an invalid NPC definition"
    end

  }

  self
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/Olib/core/item.rb', line 83

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

#inObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/Olib/core/item.rb', line 129

def in
  return self if has? 'contents'
  Olib.wrap(action 'look in') { |line|
    if line=~/^There is nothing in there.|You gaze through (.*?) and see.../
      raise Olib::Errors::Mundane
    end
    
    # 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

determine if Item is something

example:

item.is?("jar")

Returns:

  • (Boolean)


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

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

#lookObject



466
467
468
469
470
471
472
473
# File 'lib/Olib/core/item.rb', line 466

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 time|You take a closer look/
  }
  self
end

#missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/Olib/core/item.rb', line 87

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

#priceObject



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/Olib/core/item.rb', line 485

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



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

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)


95
96
97
# File 'lib/Olib/core/item.rb', line 95

def pullable?
  is? 'pullable'
end

#readObject



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/Olib/core/item.rb', line 505

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



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/Olib/core/item.rb', line 275

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

#sellObject



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/Olib/core/item.rb', line 198

def sell
  price = 0
  take
  Olib.wrap( action "sell" ){ |line|
    raise Olib::Errors::Fatal.new "#{to_s} is not sellable here" if GameObj.right_hand.id == @id

    if line =~ /([\d]+) silver/
      price = $1.to_i 
      raise Olib::Errors::Mundane
    end
  }
  price
end

#shakeObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/Olib/core/item.rb', line 171

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



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/Olib/core/item.rb', line 186

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



167
168
169
# File 'lib/Olib/core/item.rb', line 167

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

#tag(*tags) ⇒ Object



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

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

#tagsObject



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

def tags
  @props[:tags]
end

#takeObject



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
383
384
385
386
387
388
389
# File 'lib/Olib/core/item.rb', line 356

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
      raise Olib::Errors::Mundane
    end

    if line =~ /let me help you with that/
      raise Olib::Errors::Mundane
    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



475
476
477
478
479
480
481
482
483
# File 'lib/Olib/core/item.rb', line 475

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



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

def to_s
  @props.to_s
end

#turnObject



212
213
214
215
# File 'lib/Olib/core/item.rb', line 212

def turn
  fput action 'turn'
  self
end

#untag(*remove) ⇒ Object



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

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

#wear(onfailure = nil) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/Olib/core/item.rb', line 300

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)


67
68
69
# File 'lib/Olib/core/item.rb', line 67

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