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, Jewel, Jewelry, Jar, Scroll, Uncommon, Wand

Constant Summary collapse

TYPES =
[
  "reagent",
  ["spirit beast talismans", true],
  "alchemy product",
  ["alchemy equipment", true],
  "note",
  ["cursed", true],
  ["ammo", true],
  "box",
  "gem",
  "herb",
  ["food", true],
  ["uncommon", true],
  "valuable",
  "plinite",
  ["ebongate", true],
  ["quest", true],
  ["jewelry", true],
  ["junk", true],
  "lockpick",
  "magic",
  "scroll",
  "skin",
  "wand",
  "toy",
  ["armor", true],
  "weapon",
  ["clothing", true],
  "instrument",
  "jar",
  "lm trap",
  "lm tool",
  "scarab",
  #"consignment",
  #"gemshop",
  #"pawnshop",
  #"furrier"
]
SOLD =
/([\d]+) silver/
WRONG_SHOP =
/That's not quite my field/
WORTHLESS =
/worthless/

Instance Attribute Summary collapse

Attributes inherited from Gameobj_Extender

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Gameobj_Extender

#__extend__, #at, #echo

Constructor Details

#initialize(obj, container = nil) ⇒ Item

When created, it should be passed an instance of GameObj

Example:

Olib::Item.new(GameObj.right_hand)

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/Olib/core/item.rb', line 71

def initialize(obj, container = nil)
  @container          = container
  @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) }

  #tag @props[:name]

  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


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

def container
  @container
end

#propsObject

Returns the value of attribute props


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

def props
  @props
end

Class Method Details

.type_methodsObject


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/Olib/core/item.rb', line 54

def self.type_methods
  TYPES.map { |type|
    if type.class == Array then
      [ Olib.methodize(type.first).to_sym, type.first ]
    elsif ["x"].include?(type.chars.last)
      [ Olib.methodize(type + "es").to_sym, type ]
    else
      [ Olib.methodize(type + "s").to_sym, type ]
    end
  }
end

Instance Method Details

#==(other) ⇒ Object


161
162
163
# File 'lib/Olib/core/item.rb', line 161

def ==(other)
  @id == other.id
end

#_drag(target) ⇒ Object


223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/Olib/core/item.rb', line 223

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


467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
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
# File 'lib/Olib/core/item.rb', line 467

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


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

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


287
288
289
# File 'lib/Olib/core/item.rb', line 287

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

#add(*items) ⇒ Object


291
292
293
294
295
296
# File 'lib/Olib/core/item.rb', line 291

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

#affordable?Boolean

Returns:

  • (Boolean)

169
170
171
172
173
# File 'lib/Olib/core/item.rb', line 169

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

#analyzeObject


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

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


143
144
145
146
147
# File 'lib/Olib/core/item.rb', line 143

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

#buyable?Boolean

Returns:

  • (Boolean)

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

def buyable?
  is? "buyable"
end

#costObject


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

def cost
  @props["cost"]
end

#crawlObject


137
138
139
140
141
# File 'lib/Olib/core/item.rb', line 137

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

#define(key, val) ⇒ Object


298
299
300
301
# File 'lib/Olib/core/item.rb', line 298

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

#dropObject


461
462
463
464
465
# File 'lib/Olib/core/item.rb', line 461

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

#exists?Boolean

Returns:

  • (Boolean)

157
158
159
# File 'lib/Olib/core/item.rb', line 157

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

#give(target, onfailure = nil) ⇒ Object


332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/Olib/core/item.rb', line 332

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)

149
150
151
# File 'lib/Olib/core/item.rb', line 149

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

#inObject


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

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)

112
113
114
# File 'lib/Olib/core/item.rb', line 112

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

#lookObject


536
537
538
539
540
541
542
543
# File 'lib/Olib/core/item.rb', line 536

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)

153
154
155
# File 'lib/Olib/core/item.rb', line 153

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

#priceObject


555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/Olib/core/item.rb', line 555

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


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/Olib/core/item.rb', line 303

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)

165
166
167
# File 'lib/Olib/core/item.rb', line 165

def pullable?
  is? "pullable"
end

#readObject


575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/Olib/core/item.rb', line 575

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]+)/
        spell = OpenStruct.new(name: $2, num: $1.to_i)        #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


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/Olib/core/item.rb', line 345

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


272
273
274
275
276
277
278
279
280
# File 'lib/Olib/core/item.rb', line 272

def sell
  take
  case result = dothistimeout("sell ##{@id}", 3, (SOLD | WRONG_SHOP | WORTHLESS))
  when SOLD        then [:sold, OpenStruct.new(name: self.name, price: $1.to_i)]
  when WRONG_SHOP  then [:wrong_shop, self]
  when WORTHLESS   then [:worthless, self]
  else                  [:unhandled_case, result]
  end
end

#shakeObject


241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/Olib/core/item.rb', line 241

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


256
257
258
259
260
261
262
263
264
265
266
# File 'lib/Olib/core/item.rb', line 256

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


237
238
239
# File 'lib/Olib/core/item.rb', line 237

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

#tag(*tags) ⇒ Object


116
117
118
119
# File 'lib/Olib/core/item.rb', line 116

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

#tagsObject


125
126
127
# File 'lib/Olib/core/item.rb', line 125

def tags
  @props[:tags]
end

#takeObject


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

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


545
546
547
548
549
550
551
552
553
# File 'lib/Olib/core/item.rb', line 545

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


103
104
105
# File 'lib/Olib/core/item.rb', line 103

def to_s
  @props.to_s
end

#turnObject


282
283
284
285
# File 'lib/Olib/core/item.rb', line 282

def turn
  fput action "turn"
  self
end

#untag(*remove) ⇒ Object


121
122
123
# File 'lib/Olib/core/item.rb', line 121

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

#use(&block) ⇒ Object


129
130
131
# File 'lib/Olib/core/item.rb', line 129

def use(&block)
  Use.new(self, &block)
end

#wear(onfailure = nil) ⇒ Object


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/Olib/core/item.rb', line 370

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)

133
134
135
# File 'lib/Olib/core/item.rb', line 133

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