Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.78 KB | None | 0 0
  1. =begin
  2.  
  3.   Author  : Xeldrake
  4.  
  5.   PURPOSE : Example code to demonstrate the use of XML stream captures and
  6.             utilizing line = get to pull lines from the console and matching
  7.             with regex.
  8.  
  9.             You'll work with arrays, hash tables, named regex, and XML stream
  10.             data.
  11.  
  12.             An introduction to downstream hooks is utilized to supress output
  13.             from being displayed on the game window.
  14.  
  15.   DATE    : 21 June 2018
  16.  
  17.   Version : 1.0
  18.  
  19.   Credit  : I utilized some example code from Ondreian to provide XML and hook
  20.             handling.
  21.  
  22. =end
  23.  
  24. #-------------------------------------------------------------------------------
  25. # FUNCTIONS
  26. #-------------------------------------------------------------------------------
  27.  
  28. def downstream_hook(stop_match, command, hook_name)
  29.   # begin a downstream hook until the regex value matches the line you want to
  30.   # stop on. Ensure that you know the underlying XML pattern to match or the
  31.   # hook will remain active
  32.  
  33.   DownstreamHook.add(hook_name, Proc.new do |line|
  34.     stop_match.each do |key,value|
  35.       if line =~ value
  36.         DownstreamHook.remove(hook_name)
  37.       end
  38.    
  39.     end
  40.     nil
  41.   end)
  42.  
  43.   put command
  44. end
  45.  
  46. #-------------------------------------------------------------------------------
  47.  
  48. def get_containers()
  49.   # return an array of hashtables for your containers including disks. Ignores
  50.   # nested containers like boxes. Hash return :id :noun :name key|value pairs.
  51.  
  52.   start_inv  = %/You are currently wearing and carrying/ # start of containers
  53.   end_inv    = %/displayed.)/                            # end of containers
  54.  
  55.   stop_match = {
  56.       :stop => /.*displayed.\).*/,
  57.   }
  58.  
  59.   format     = [
  60.                /(?<!(\s){3})(a|an)\s\<a exist=(?:'|")(?<id>.*?)(?:'|") /       ,
  61.               /noun=(?:'|")(?<noun>.*?)(?:'|")>(?<name>.*?)\<\/a>/            ,
  62.  ]
  63.  
  64.  storage         = /#{format[0]}#{format[1]}/ # connect the format regex
  65.  
  66.  your_disk       = /#{Char.name}\sdisk/       # pattern for your disk in room
  67.  
  68.  containers_hash = Hash.new                   # for each hash that's matched
  69.   containers      = Array.new                  # for holding container hashes
  70.  
  71.  
  72.   Script.current.want_downstream_xml = true    # start reading raw xml
  73.  
  74.   downstream_hook(stop_match,'inv full containers',"__hide_inv")
  75.  
  76.   while line = get                             # one line of console text
  77.    
  78.     inv_start        = true if line.include?(start_inv)
  79.            
  80.     containers_hash  = Hash.new                # reset hash each line
  81.    
  82.     if inv_start == true
  83.      
  84.       #-------------------------------------------------------------------------
  85.       # start processing lines
  86.  
  87.       if line =~ storage
  88.        
  89.         values = (storage.match line).captures # grab just named capures
  90.        
  91.         containers_hash[:id]   = values[0]     # first value  : item id
  92.         containers_hash[:noun] = values[1]     # second value : noun
  93.         containers_hash[:name] = values[2]     # thirst value : name
  94.                    
  95.         containers.push(containers_hash)       # add this hash to the array
  96.       end
  97.  
  98.     end
  99.    
  100.     # done looking at containers
  101.     break if line.include?(end_inv)            
  102.   end # loop close
  103.  
  104.   Script.current.want_downstream_xml = false   # stop reading raw xml
  105.  
  106.   #-----------------------------------------------------------------------------
  107.   # let's get the disk, hash it, and add it to the array
  108.  
  109.   on_ground = GameObj.loot  # enumerates things on the groud
  110.  
  111.   for loot in on_ground
  112.     if loot.name =~ your_disk
  113.       containers_hash = Hash.new  # clear the hash for reuse
  114.      
  115.       containers_hash[:id]   = loot.id    # item id
  116.       containers_hash[:noun] = loot.noun  # noun
  117.       containers_hash[:name] = loot.name  # name
  118.                    
  119.       containers.push(containers_hash)    # add this hash to the array
  120.     end
  121.   end
  122.    
  123.   return containers                       # returns array container hashes
  124. end
  125.  
  126. #-------------------------------------------------------------------------------
  127. # BEGIN PROGRAM
  128. #-------------------------------------------------------------------------------
  129.  
  130. #-------------------------------------------------------------------------------
  131. # call the get_containers() function and store the return value in "containers"
  132.  
  133. containers = get_containers()
  134.  
  135. #-------------------------------------------------------------------------------
  136. # Walk the hash table, outputting the id, noun, and name values. I
  137. # would suggest that you use the ID to refer to each container instead of the
  138. # noun or name to prevent any naming issues where the game will not understand
  139. # what you are referring to.
  140.  
  141. for i in containers
  142.   echo "ID: #{i[:id]} NOUN: #{i[:noun]} NAME: #{i[:name]}"
  143.   #fput "look in ##{i[:id]}"
  144. end
  145.  
  146. #-------------------------------------------------------------------------------
  147. # SAMPLE OUTPUT
  148. #-------------------------------------------------------------------------------
  149.  
  150. =begin
  151.  
  152. >;_example_list_containers
  153. --- Lich: _example_list_containers active.
  154.  
  155. [_example_list_containers]>inv full containers
  156.  
  157. [_example_list_containers: ID: 136889573 NOUN: backsack NAME: caramel-hued nubuck backsack]
  158. [_example_list_containers: ID: 136889638 NOUN: bag NAME: oiled light leather courier bag]
  159. [_example_list_containers: ID: 136889640 NOUN: greatcloak NAME: feathered olive green greatcloak]
  160. [_example_list_containers: ID: 136889649 NOUN: pouch NAME: waxed black leather pouch]
  161. [_example_list_containers: ID: 136889650 NOUN: sheath NAME: leather sheath]
  162. [_example_list_containers: ID: 136889652 NOUN: thigh-quiver NAME: silver-ringed black leather thigh-quiver]
  163. [_example_list_containers: ID: 136982514 NOUN: disk NAME: Rabbits disk]
  164.  
  165. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement