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.
- An introduction to downstream hooks is utilized to supress output
- from being displayed on the game window.
- DATE : 21 June 2018
- Version : 1.0
- Credit : I utilized some example code from Ondreian to provide XML and hook
- handling.
- =end
- #-------------------------------------------------------------------------------
- # FUNCTIONS
- #-------------------------------------------------------------------------------
- def downstream_hook(stop_match, command, hook_name)
- # begin a downstream hook until the regex value matches the line you want to
- # stop on. Ensure that you know the underlying XML pattern to match or the
- # hook will remain active
- DownstreamHook.add(hook_name, Proc.new do |line|
- stop_match.each do |key,value|
- if line =~ value
- DownstreamHook.remove(hook_name)
- end
- end
- nil
- end)
- put command
- end
- #-------------------------------------------------------------------------------
- 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
- stop_match = {
- :stop => /.*displayed.\).*/,
- }
- 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
- downstream_hook(stop_match,'inv full containers',"__hide_inv")
- 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()
- #-------------------------------------------------------------------------------
- # Walk the 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 where the game will not understand
- # what you are referring to.
- for i in containers
- echo "ID: #{i[:id]} NOUN: #{i[:noun]} NAME: #{i[:name]}"
- #fput "look in ##{i[:id]}"
- end
- #-------------------------------------------------------------------------------
- # SAMPLE OUTPUT
- #-------------------------------------------------------------------------------
- =begin
- >;_example_list_containers
- --- Lich: _example_list_containers active.
- [_example_list_containers]>inv full containers
- [_example_list_containers: ID: 136889573 NOUN: backsack NAME: caramel-hued nubuck backsack]
- [_example_list_containers: ID: 136889638 NOUN: bag NAME: oiled light leather courier bag]
- [_example_list_containers: ID: 136889640 NOUN: greatcloak NAME: feathered olive green greatcloak]
- [_example_list_containers: ID: 136889649 NOUN: pouch NAME: waxed black leather pouch]
- [_example_list_containers: ID: 136889650 NOUN: sheath NAME: leather sheath]
- [_example_list_containers: ID: 136889652 NOUN: thigh-quiver NAME: silver-ringed black leather thigh-quiver]
- [_example_list_containers: ID: 136982514 NOUN: disk NAME: Rabbits disk]
- =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement