PDA

View Full Version : Lich: My loot function won't loot



Renian
03-13-2008, 06:41 AM
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]

SpiffyJr
03-13-2008, 09:21 AM
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.

Deathravin
03-13-2008, 12:48 PM
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 ><

SpiffyJr
03-13-2008, 12:50 PM
Thanks, I think? :D

Deathravin
03-13-2008, 02:29 PM
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

Renian
03-13-2008, 04:55 PM
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.

BigWorm
03-13-2008, 06:18 PM
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:


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:


[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:


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

#testloot
f = File.new("c:/Lich/scripts/loot.dat", "r")
$array = f.readlines.map { |line| line.chomp }
f.closewhich 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.


if array.include?( maybe_loot )
... something
else
... something else
end

Hopefully this will help you improve your script.

Renian
03-13-2008, 06:24 PM
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.

Shaelun
03-17-2008, 11:40 PM
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):


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.