Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.08 KB | None | 0 0
  1. =begin
  2.  
  3.   Author  : Xeldrake
  4.   PURPOSE : Example code to demonstrate the use of XML stream captures and
  5.             utilizing line = get to pull lines from the console and matching
  6.             with regex
  7.  
  8.             You'll work with arrays, hash tables, named regex, and XML stream
  9.             data.
  10.  
  11.   DATE    : 21 June 2018
  12.   Version : 1.0
  13.  
  14. =end
  15.  
  16. #-------------------------------------------------------------------------------
  17. # FUNCTIONS
  18. #-------------------------------------------------------------------------------
  19.  
  20. def get_containers()
  21.   # return an array of hashtables for your containers including disks. Ignores
  22.   # nested containers like boxes. Hash return :id :noun :name key|value pairs.
  23.  
  24.   start_inv  = %/You are currently wearing and carrying/ # start of containers
  25.   end_inv    = %/displayed.)/                            # end of containers
  26.    
  27.   format     = [
  28.                /(?<!(\s){3})(a|an)\s\<a exist=(?:'|")(?<id>.*?)(?:'|") /       ,
  29.               /noun=(?:'|")(?<noun>.*?)(?:'|")>(?<name>.*?)\<\/a>/            ,
  30.  ]
  31.  
  32.  storage         = /#{format[0]}#{format[1]}/ # connect the format regex
  33.  
  34.  your_disk       = /#{Char.name}\sdisk/       # pattern for your disk in room
  35.  
  36.  containers_hash = Hash.new                   # for each hash that's matched
  37.   containers      = Array.new                  # for holding container hashes
  38.  
  39.  
  40.   Script.current.want_downstream_xml = true    # start reading raw xml
  41.  
  42.   fput "inv full containers"                   # call in game command
  43.  
  44.   #-----------------------------------------------------------------------------
  45.   # look at each line of xml stream and match on containers only
  46.  
  47.   while line = get                             # one line of console text
  48.    
  49.     inv_start        = true if line.include?(start_inv)
  50.  
  51.     containers_hash  = Hash.new                # reset hash each line
  52.    
  53.     if inv_start == true
  54.      
  55.       #-------------------------------------------------------------------------
  56.       # start processing lines
  57.  
  58.       if line =~ storage
  59.        
  60.         values = (storage.match line).captures # grab just named capures
  61.        
  62.         containers_hash[:id]   = values[0]     # first value  : item id
  63.         containers_hash[:noun] = values[1]     # second value : noun
  64.         containers_hash[:name] = values[2]     # thirst value : name
  65.                    
  66.         containers.push(containers_hash)       # add this hash to the array
  67.       end
  68.  
  69.     end
  70.    
  71.     # done looking at containers
  72.     break if line.include?(end_inv)            
  73.   end # loop close
  74.  
  75.   Script.current.want_downstream_xml = false   # stop reading raw xml
  76.  
  77.   #-----------------------------------------------------------------------------
  78.   # let's get the disk, hash it, and add it to the array
  79.  
  80.   on_ground = GameObj.loot  # enumerates things on the groud
  81.  
  82.   for loot in on_ground
  83.     if loot.name =~ your_disk
  84.       containers_hash = Hash.new  # clear the hash for reuse
  85.      
  86.       containers_hash[:id]   = loot.id    # item id
  87.       containers_hash[:noun] = loot.noun  # noun
  88.       containers_hash[:name] = loot.name  # name
  89.                    
  90.       containers.push(containers_hash)    # add this hash to the array
  91.     end
  92.   end
  93.    
  94.   return containers                       # returns array container hashes
  95. end
  96.  
  97. #-------------------------------------------------------------------------------
  98. # BEGIN PROGRAM
  99. #-------------------------------------------------------------------------------
  100.  
  101. #-------------------------------------------------------------------------------
  102. # call the get_containers() function and store the return value in "containers"
  103.  
  104. containers = get_containers()
  105.  
  106. #-------------------------------------------------------------------------------
  107. # go through each hash table, outputting the id, noun, and name values. I
  108. # would suggest that you use the ID to refer to each container instead of the
  109. # noun or name to prevent any naming issues
  110.  
  111. for i in containers
  112.   echo "#{i[:id]} #{i[:noun]} #{i[:name]}"
  113.   fput "look in ##{i[:id]}"
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement