Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- Author : Xeldrake
- PURPOSE : Example code to demonstrate the use of XML stream captures and
- utilizing line = get to pull lines from the console and matching
- with regex
- You'll work with arrays, hash tables, named regex, and XML stream
- data.
- DATE : 21 June 2018
- Version : 1.0
- =end
- #-------------------------------------------------------------------------------
- # FUNCTIONS
- #-------------------------------------------------------------------------------
- def get_containers()
- # return an array of hashtables for your containers including disks. Ignores
- # nested containers like boxes. Hash return :id :noun :name key|value pairs.
- start_inv = %/You are currently wearing and carrying/ # start of containers
- end_inv = %/displayed.)/ # end of containers
- format = [
- /(?<!(\s){3})(a|an)\s\<a exist=(?:'|")(?<id>.*?)(?:'|") / ,
- /noun=(?:'|")(?<noun>.*?)(?:'|")>(?<name>.*?)\<\/a>/ ,
- ]
- storage = /#{format[0]}#{format[1]}/ # connect the format regex
- your_disk = /#{Char.name}\sdisk/ # pattern for your disk in room
- containers_hash = Hash.new # for each hash that's matched
- containers = Array.new # for holding container hashes
- Script.current.want_downstream_xml = true # start reading raw xml
- fput "inv full containers" # call in game command
- #-----------------------------------------------------------------------------
- # look at each line of xml stream and match on containers only
- while line = get # one line of console text
- inv_start = true if line.include?(start_inv)
- containers_hash = Hash.new # reset hash each line
- if inv_start == true
- #-------------------------------------------------------------------------
- # start processing lines
- if line =~ storage
- values = (storage.match line).captures # grab just named capures
- containers_hash[:id] = values[0] # first value : item id
- containers_hash[:noun] = values[1] # second value : noun
- containers_hash[:name] = values[2] # thirst value : name
- containers.push(containers_hash) # add this hash to the array
- end
- end
- # done looking at containers
- break if line.include?(end_inv)
- end # loop close
- Script.current.want_downstream_xml = false # stop reading raw xml
- #-----------------------------------------------------------------------------
- # let's get the disk, hash it, and add it to the array
- on_ground = GameObj.loot # enumerates things on the groud
- for loot in on_ground
- if loot.name =~ your_disk
- containers_hash = Hash.new # clear the hash for reuse
- containers_hash[:id] = loot.id # item id
- containers_hash[:noun] = loot.noun # noun
- containers_hash[:name] = loot.name # name
- containers.push(containers_hash) # add this hash to the array
- end
- end
- return containers # returns array container hashes
- end
- #-------------------------------------------------------------------------------
- # BEGIN PROGRAM
- #-------------------------------------------------------------------------------
- #-------------------------------------------------------------------------------
- # call the get_containers() function and store the return value in "containers"
- containers = get_containers()
- #-------------------------------------------------------------------------------
- # go through each hash table, outputting the id, noun, and name values. I
- # would suggest that you use the ID to refer to each container instead of the
- # noun or name to prevent any naming issues
- for i in containers
- echo "#{i[:id]} #{i[:noun]} #{i[:name]}"
- fput "look in ##{i[:id]}"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement