Results 1 to 9 of 9

Thread: Lich: My loot function won't loot

  1. #1

    Default Lich: My loot function won't loot

    This script of mine, which I intend to add to hunting scripts, doesn't seem to realize that it has matches.


    #testloot
    f = File.new("c:/Lich/scripts/loot.dat", "r")
    $array = f.readlines
    f.close

    f2 = File.new("c:/Lich/scripts/misc.dat", "r")
    $misc = f2.readlines
    f2.close

    def loot()
    #fput "loot"
    #target_line = matchtimeout 3, "You search", "Could not find"
    #if target_line =~ /You search/
    checkloot.each do |maybeloot|
    echo "Found a possible."
    $array.each do |loot|
    echo "Comparing " + loot + " and " + maybeloot
    if maybeloot == loot
    echo "Got one!"
    $other = false
    fput "get " + loot
    $misc.each do |other|
    if other == loot
    $other = true
    end
    if $other != true && other != loot
    $other = false
    end
    end
    if $other == true
    fput "put " + loot + " in my cloak"
    target_line = matchtimeout 2, "won't fit", "You put"
    if target_line == "won't fit"
    fput "put " + loot + " in my backpack"
    end
    else
    fput "put " + loot + " in my pouch"
    end
    end
    end
    end
    #end
    end

    loot()


    Here's the output.

    [Ant Hill Table]
    On the inner side of one of the table legs, a small nest is visible. Dozens of ants scurry about on the floor as they indulge happily in spilled brew and ale from the previous messy patrons. Serving as a most unique centerpiece, a piece of amber has been cut into the shape of a star and holds the fossilized remains of several unknown tiny creatures. You also see a golden beryl gem.
    [loottest: Found a possible.]
    [loottest: Comparing dust
    and gem]
    [loottest: Comparing sapphire
    and gem]
    [loottest: Comparing diamond
    and gem]
    [loottest: Comparing emerald
    and gem]
    [loottest: Comparing ruby
    and gem]
    [loottest: Comparing gem
    and gem]
    [loottest: Comparing chest
    and gem]
    [loottest: Comparing box
    and gem]
    [loottest: Comparing trunk
    and gem]
    [loottest: Comparing strongbox
    and gem]
    [loottest: Comparing coffer
    and gem]
    [loottest: Comparing flask
    and gem]
    [loottest: Comparing ring
    and gem]
    [loottest: Comparing crystal
    and gem]
    [loottest: Comparing deathstone
    and gem]
    [loottest: Comparing brooch
    and gem]
    [loottest: Comparing stein
    and gem]
    [loottest: Comparing sphere
    and gem]
    [loottest: Comparing pitcher
    and gem]
    [loottest: Comparing blazestar
    and gem]
    [loottest: Comparing chalcedony
    and gem]
    [loottest: Comparing glimaerstone
    and gem]
    [loottest: Comparing lapis lazuli
    and gem]
    [loottest: Comparing pearl
    and gem]
    [loottest: Comparing topaz
    and gem]
    [loottest: Comparing blazestar
    and gem]
    [loottest: Comparing obsidian
    and gem]
    [loottest: Comparing wand
    and gem]
    [loottest: Comparing coral
    and gem]
    [loottest: Comparing heliodor
    and gem]
    [loottest: Comparing peridot
    and gem]
    [loottest: Comparing quartz
    and gem]
    [loottest: Comparing moonstone
    and gem]
    [loottest: Comparing faenor-bloom
    and gem]
    [loottest: Comparing dreamstone
    and gem]
    [loottest: Comparing stone
    and gem]
    [loottest: Comparing turquoise
    and gem]
    [loottest: Comparing zircon
    and gem]
    [loottest: Comparing band
    and gem]
    [loottest: Comparing bracelet
    and gem]
    [loottest: Comparing talisman
    and gem]
    [loottest: Comparing crown
    and gem]
    [loottest: Comparing garnet
    and gem]
    [loottest: Comparing statue
    and gem]
    [loottest: Comparing palimpsest
    and gem]
    [loottest: Comparing paper
    and gem]
    [loottest: Comparing scroll and gem]

  2. #2

    Default

    Do something like this instead...

    #
    # Gems
    #
    gemdb = [ "faenor-bloom", "star ruby", "aetherstone", "agate", "ambergris", "azurite", "beryl", "bloodjewel", "bloodstone", "bluerock", "caederine", "cordierite", "deathstone", "doomstone", "dreamstone", "eostone", "firestone", "galena", "geode", "hyacinth", "onyx", "pyrite", "peridot", "rhimar", "riftshard", "riftstone", "rosepar", "wyrdshard", "spinel", "shard", "faenor", "blazestar", "despanal", "feystone", "diamond", "emerald", "ruby", "pearl", "sapphire", "opal", "jacinth", "garnet", "moonstone", "topaz", "gem", "coral", "sunstone", "starstone", "quartz", "heliodor", "glimaerstone", "carbuncle", "turquoise", "chalcedony", "tourmaline", "obsidian", "zircon", "amethyst", "lapis", "amber" , "sphene" , "shell", "stone" ]

    loot = checkloot
    gems = loot & gemdb

    gems.each do |x|
    grabLoot(x, Settings["gem"])
    end

    grabLoot is just a definition that opens/closes the container and grabs the item.

  3. #3

    Default

    I hate people that get coding...

    Example. My wife will write out an essay or something and I'll find a way to say a sentence she has in a more consice, more pleasing-to-read way. She hates me for that. How I can just see how to write something to make it sound good.

    Real coders always will take 200 lines I wrote and condense it down to 5 lines of code that works faster and more efficently than mine.

    I need to take some classes ><
    Discord: Valendr0s#6885

  4. #4

    Default

    Thanks, I think?

  5. #5

    Default

    I don't know what I don't know; meaning I don't know if that's the right way to do that. But what I do know is that I wouldn't have thought to do that code like that... As usual... and mine would have been probably twice as long as Renian's
    Discord: Valendr0s#6885

  6. #6

    Default

    Thanks SpiffyJr for the information on grabLoot. Handy.

    But what I'm concerned about is why when I load stuff from a .dat file and get it to compare "gem" == "gem" it doesn't return true. The .dat file would be superior to use instead of the array because it could call it in any script.

  7. #7
    Join Date
    Jul 2003
    Location
    St. Louis, MO
    Posts
    4,271

    Default

    This would be easier to debug if we could see the file you're reading from, but from the output that your script uses, I'm guessing it's formated like:
    Code:
    sapphire
    diamond
    emerald
    gem
    Your problem is that you're including the newline at the end of each line when you compare your strings. That's why your output looks like:
    Code:
    [loottest: Comparing palimpsest
    and gem]
    [loottest: Comparing paper
    and gem]
    [loottest: Comparing scroll and gem]
    Guessing the last line of your file does NOT include a newline from the output of the compare with scroll.

    Anyway, the problem is that your array is populated with incorrect values and looks something like this: [ "diamond\n", "sapphire\n", "emerald\n" ]. Since the string you're reading from the game does not include the newline, the script doesn't recognize them as matching because they aren't the same and don't actually match.

    This is a very common mistake, so don't feel bad about making it. Since this is a common operation in most programming languages, Ruby has a built in function that does the work for you, chomp(). So the solution is to change:
    Code:
    #testloot
    f = File.new("c:/Lich/scripts/loot.dat", "r")
    $array = f.readlines
    f.close
    to
    Code:
    #testloot
    f = File.new("c:/Lich/scripts/loot.dat", "r")
    $array = f.readlines.map { |line| line.chomp }
    f.close
    which should fix your problem. This just tells the script to chomp each line of input before returning it back to be added to the array.

    Also, you're reinventing the wheel a bit in the way you're doing things. Testing whether a string of arrays contains a specified string is also a very common programming idiom and Ruby again makes it easy for us to do so. Instead of testing each value yourself using array.each, you can use the include? function that Ruby provides.
    Code:
    if array.include?( maybe_loot )
        ... something
    else
        ... something else
    end
    Hopefully this will help you improve your script.
    Quote Originally Posted by Patrick McGoohan
    I am not a number, I am a free man!

  8. #8

    Default

    OMG THX, I didn't consider the newline.
    I'm surprised f.readlines.chomp alone doesn't work though. Odd. But you fixed my script. Thanks a ton.

  9. #9
    Join Date
    Nov 2004
    Location
    Upstate NY
    Posts
    428

    Default

    f.readlines returns an array consisting of each line read from the file (in this case f) -- which you obviously already know. The problem is that the chomp method works on strings, not arrays. If you find it more intuitive (I do, personally), you can use the collect method instead of the map method -- they're identical (synonyms).

    You can make it work the way you want, if you're so inclined (you'd have to put this at the beginning of your script):
    Code:
    class Array
    
      def chomp
        self.collect { |line|
          if line.kind_of? String
            line.chomp
          else
            line
          end
        }
      end
    
    end
    Then you can just $array.chomp and have it do the right thing.
    Last edited by Shaelun; 03-17-2008 at 10:55 PM.
    I visualize a time when we will be to robots what dogs are to humans, and I'm rooting for the machines. -- Claude Shannon

Similar Threads

  1. Lich's move function and Sneaking (617)
    By Overlordz in forum The Lich Project
    Replies: 1
    Last Post: 06-10-2014, 11:25 AM
  2. loot-be-gone
    By Kramble in forum The Lich Project
    Replies: 4
    Last Post: 01-13-2011, 08:37 PM
  3. Azbounty + loot-be-gone + loot + waitloot
    By Afk in forum The Lich Project
    Replies: 2
    Last Post: 11-19-2010, 11:01 AM
  4. Lich Assistance with Loot Grabber Script
    By deck in forum Scripting Discussion
    Replies: 12
    Last Post: 01-09-2006, 02:02 AM
  5. Lich Script - critter death/loot
    By Van in forum Scripting Discussion
    Replies: 3
    Last Post: 12-22-2005, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •