Skip to content

Instantly share code, notes, and snippets.

@ondreian
Last active July 22, 2016 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondreian/0dd72ce96cb6b2afc74a9c9b25700d3c to your computer and use it in GitHub Desktop.
Save ondreian/0dd72ce96cb6b2afc74a9c9b25700d3c to your computer and use it in GitHub Desktop.
Bounty runner for GSIV
require "olib"
find_guard_and_ask = proc {
Bounty.find_guard.ask_for_bounty
}
Bounty.on(:guard_bounty, &find_guard_and_ask)
Bounty.on(:guard_turn_in, &find_guard_and_ask)
Bounty.on(:failed_bounty) {
Bounty.cooldown!
Go2.advguild
Bounty.ask_for_bounty
}
Bounty.on(:no_bounty) {
Bounty.cooldown!
Go2.advguild
Bounty.ask_for_bounty
}
Bounty.on(:remove) {
Bounty.remove
}
Bounty.on(:turn_in) {
Mind.rest!
Go2.advguild
Bounty.ask_for_bounty
Go2.origin if Bounty.cooldown?
}
Bounty.on(:bandits) {
Go2.origin
puts Olib.monsterbold "************"
puts Olib.monsterbold "you have #{Bounty.n} bandits around the #{Bounty.area}"
puts Olib.monsterbold "************"
exit
}
Bounty.on(:get_gem_bounty) {
Go2.rebase.gemshop
Bounty.ask_for_bounty
}
Bounty.on(:gem) {
remainder = Bounty.n - Containers.Pants.find_by_tags(Bounty.gem).length
if remainder > 0
Olib.run 'warehouse', 'pick', '--gem', Bounty.gem, '--n', remainder
end
if Containers.Pants.find_by_tags(Bounty.gem).length < Bounty.n
raise Olib::Errors::Fatal
end
Go2.gemshop
Containers.Pants
.find_by_tags(Bounty.gem)
.take(Bounty.n)
.each(&:sell)
Go2.origin
}
Bounty.on(:get_herb_bounty) {
Go2.npchealer
Bounty.ask_for_bounty
}
Bounty.on(:herb) {
remainder = Bounty.n - Containers.Pants.where(name: Bounty.herb).length
if remainder > 0
Olib.run 'warehouse', 'pick', '--herb', Bounty.herb, '--n', remainder
end
if Containers.Pants.where(name: Bounty.herb).length < Bounty.n
raise Olib::Errors::Fatal
end
Go2.npchealer
Containers.Pants
.where(name: Bounty.herb)
.take(Bounty.n)
.each { |herb|
herb.take.give Bounty.npc
}
Go2.origin
}
Bounty.on(:escort){
Go2.origin
if ["Wehnimer's Landing", "Solhaven", "Icemule Trace"].include? Bounty.destination
puts Olib.monsterbold "************"
puts Olib.monsterbold "you have an escort bounty to #{Bounty.destination} from #{Room.current.location}"
puts Olib.monsterbold "************"
exit
end
Bounty.remove
}
if Olib.CLI.help? || Olib.CLI.flag?('--help')
menu = Olib::HelpMenu.new
menu.cmd '', 'run ;0bounty in forever move'
menu.cmd 'help', 'display this menu'
menu.cmd 'remove', "force remove your current bounty"
menu.write
elsif Olib.CLI.cmd?('remove') || Olib.CLI.flag?('--remove')
Bounty.exec(:remove)
Go2.origin
else
Go2.rebase
loop {
begin
Bounty.exec
rescue Olib::Errors::Fatal
Bounty.exec(:remove).cooldown!
Go2.rebase
end
sleep 1
}
end
=begin
Use a private shop to stockpile gems among friends
Dependencies: 0lib
Required: Lich 4.3.12
m444w on the PC
=end
if $SAFE > 0
echo "This script needs to be trusted to work properly. (;trust #{script.name})"
exit
end
begin
require "Olib"
rescue LoadError
echo "This script requires Olib to work, attempting to recover..."
result = %x{gem install Olib}
require "Olib"
echo "Recovery worked..."
end
class Warehouse
@@rooms = {}
# Where we keep types of jars
@@rooms[:fulls] = 24462
@@rooms[:empties] = 24463
@@rooms[:partials] = 24464
@@herb_rooms = [
24462,
23915,
23913,
23576,
23626,
3446,
23627
]
# Reserved
#@@rooms[:herbs] = 23913
#@@rooms[:skins] = 23915
# maintenance routines catalog
# for selecting which jars at which times
# large If/Else statements suck
@@routines = {}
@@teleportation_room = 'pawnshop'
# if we should teleport to or from FWI after logic runs
@@should_teleport = false
def Warehouse.partials
Room[@@rooms[:partials]]
end
def Warehouse.fulls
Room[@@rooms[:fulls]]
end
def Warehouse.empties
Room[@@rooms[:empties]]
end
def Warehouse.rooms_by_tag
rooms = {}
rooms[Warehouse.fulls] = 'full'
rooms[Warehouse.empties] = 'empty'
rooms[Warehouse.partials] = 'partial'
rooms
end
def Warehouse.get_cash
if Char.wealth < 100
Go2.bank
Char.withdraw(100)
end
Warehouse
end
def Warehouse.ensure_realm(id)
@@start_room = Room.current.id
if Room.current.id == 4
echo "You are in an unmapped room jackass"
exit
end
return Warehouse if Room.current.id == id
if Map.findpath(Room.current.id, id).nil?
Go2.room(@@teleportation_room)
@@should_teleport = @@should_teleport ? false : true
Go2.fwi
end
Go2.room id
# don't do something crazy if a shop or path has changed
if Room.current.id != id
Warehouse.go2_origin
echo "ERROR: a safe path to [ #{Room[id].title} - #{id} ] could not be found, protecting you from crawling a random area."
exit
end
return Warehouse
end
def Warehouse.go2_origin
Go2.fwi if @@should_teleport
Go2.room(@@start_room)
end
def Warehouse.jars(*tags)
Shop::Playershop.find_by_tags *[ 'jar' ].concat(tags)
end
def Warehouse.on(id, &block)
@@routines[id] = block
end
def Warehouse.routine(namespace=Room.current.id)
@@routines[namespace]
end
def Warehouse.get_empty_jar
start = Room.current.id
Go2.room Warehouse.empties.id
jar = Warehouse.jars('empty').first.acquire_from_shop
Go2.room start
jar
end
def Warehouse.stock
Warehouse.get_cash.ensure_realm(Warehouse.partials.id)
tags = ['gem']
tags.push Olib.CLI.gem if Olib.CLI.gem.class != TrueClass
gems = Lootsack
.find_by_tags(*tags)
.group_by{ |gem| [gem.tags - ['gem']].first }
Warehouse.routine(:stock_gem).call(*gems.shift) while !gems.empty?
Warehouse.go2_origin
end
def Warehouse.count(jars)
jars.map { |jar| jar.props[:number] }.reduce(&:+) || 0
end
def Warehouse.pick_gem(g=Olib.CLI.gem, n=Olib.CLI.n.to_i)
Warehouse.get_cash.ensure_realm(Warehouse.partials.id)
partials = Warehouse.jars(g).each(&:in)
partial_count = Warehouse.count(partials)
# only check full room first if we aren't allowing partial picking
if partial_count >= n
total = partial_count
else
echo "Not enough in partial room [#{partial_count}]..."
echo "Checking full room..."
Go2.room Warehouse.fulls.id
fulls = Warehouse.jars(g).each(&:in)
full_count = Warehouse.count(fulls)
total = partial_count + full_count
# go back to partials room if there were gems there
Go2.room Warehouse.partials.id if partial_count > 0
end
if total == 0
Warehouse.go2_origin
raise Olib::Errors::Fatal.new "\nYou are out of #{g} in the warehouse\n"
end
# quit if we aren't allowing partial picks
if total < n && !CLI.flags.include?(:allow_partials)
respond Olib.CLI
Warehouse.go2_origin
raise Olib::Errors::Fatal.new "\nquiting as partial ordering not enabled\nadd --allow-partials flag if you want to change this behavior"
end
begin
took = Warehouse.routine(:gem_acquisition_loop).call(n, partials) unless partials.empty?
took = took || 0
took = took + Warehouse.routine(:gem_acquisition_loop).call(n - took, fulls) unless took == n || fulls.empty?
rescue Olib::Errors::ContainerFull
jar.add(Char.left).shop_sell(1)
end
Warehouse.go2_origin
echo "took #{took} of #{g}"
end
def Warehouse.pick_herb(herb=Olib.CLI.herb, n=Olib.CLI.n)
echo Olib.CLI
@@start_room = Room.current.id
rooms = @@herb_rooms.clone
total = n
while ( !rooms.empty? && total > 0 )
total = total - Warehouse.routine(:pick_herb).call(rooms.shift, n, herb)
end
Warehouse.go2_origin
end
def Warehouse.maintain
Warehouse.get_cash.ensure_realm(Warehouse.partials.id)
@@rooms.each { |room, id| Warehouse.tidy_area(id) }
Warehouse.go2_origin
end
def Warehouse.tidy_area(id)
Go2.room id
jars = Warehouse.jars
.each { |jar| jar.in } # check how many are in it
jars_requiring_maintenance = jars.select(&Warehouse.routine)
unless jars_requiring_maintenance.empty?
jars_requiring_maintenance.each { |jar|
jar.acquire_from_shop
Containers.Lootsack(jar)
}
Warehouse.rooms_by_tag.each { |room, type|
Warehouse.transfer room.id, jars_requiring_maintenance.select { |jar|
jar.is? type
}
}
end
end
def Warehouse.transfer(id, list)
unless list.empty?
Go2.room id
list.each { |jar|
begin
jar.take.shop_sell(1)
rescue Olib::Errors::Fatal
Containers.Lootsack.add jar
echo "Error: could not find room for #{jar}"
end
}
end
end
end
before_dying {
stop_script 'go2' if running? 'go2'
}
Warehouse.on(Warehouse.partials.id) { |jar|
jar.is?('empty') || jar.is?('full')
}
Warehouse.on(Warehouse.fulls.id) { |jar|
jar.is?('partial') || jar.is?('empty')
}
Warehouse.on(Warehouse.empties.id) { |jar|
jar.is?('full') || jar.is?('partial')
}
Warehouse.on(:stock_gem) { |type, gems|
begin
jar = Warehouse.jars(type).first
next if jar.nil? && Olib.CLI.gem == TrueClass
if jar.nil?
Warehouse.get_empty_jar.add(*gems).shop_sell(1)
else
jar.acquire_from_shop.add(*gems).shop_sell(1)
end
rescue Olib::Errors::ContainerFull
echo "handling full jar..."
Containers.Lootsack(GameObj.left_hand) if GameObj.left_hand
Go2.room Warehouse.fulls.id
jar.shop_sell(1)
retry
end
}
Warehouse.on(:gem_acquisition_loop) { |limit, jars|
took = 0
loop {
begin
break if jars.empty? || took >= limit
jar = jars.shift.acquire_from_shop
while jar.props[:number] > 0 && took < limit
jar.shake
Containers.Lootsack(GameObj.left_hand)
took = took + 1
end # end shaking the jar
raise Olib::Errors::Mundane if Room.current.id == Warehouse.fulls.id
jar.shop_sell(1)
# rescue from an empty jar being put in the wrong room
rescue Olib::Errors::Fatal
echo "recovering from empty jar..."
Containers.Lootsack(GameObj.left_hand)
took = took + 1
return_room = Room.current.id
Go2.room Warehouse.empties.id
jar.shop_sell(1)
Go2.room return_room
retry
# rescue from a partial being placed back in the full room
rescue Olib::Errors::Mundane
echo "recovering from a partial jar"
Go2.room Warehouse.partials.id
jar.shop_sell(1)
Go2.room Warehouse.fulls.id
end
sleep 0.1
} # partial jars are now empty or took the total we needed
return took
}
Warehouse.on(:pick_herb) { |room, n, herb|
Warehouse.ensure_realm(room)
options = Shop::Playershop.where(name: herb)
echo "Options: #{options}"
took = 0
return 0 if options.empty?
loop {
current = options.shift
current.take
loop {
begin
break if !current.exists? || took >= n
current.acquire_from_shop
took = took + 1
Containers.Lootsack.add Char.right
rescue Olib::Errors::InsufficientFunds
current_room = Room.current.id
Char.withdraw( (n-took)*current.cost )
Go2.room current_room
retry
end
}
break if took == n || options.empty?
}
return took
}
if Olib.CLI.cmd.nil? || Olib.CLI.help?
menu = Olib::HelpMenu.new
menu.flag("help", "display this menu")
menu.cmd("pick", "get things from the warehouse")
menu.flag("gem", "the gem to get from the warehouse, [requires quotes if gem includes spaces]")
menu.flag("herb", "the herb to get from the warehouse [requires quotes if herb includes spaces]")
menu.flag("n", "max number of items to pick")
menu.flag("allow-partials", "allows partial picks if ;warehouse detects there are fewer than the number requested")
menu.flag("sell", "sell the gems to the local Gemshop in the town you came from")
#menu.flag("bounty", "autoparses your bounty, picks the necessary items which are not already in your lootsack, and (sells|gives) them to the appropiate NPC")
menu.cmd('m', "run in maintainence mode to help make sure all the jars/gems/herbs are where they should be")
menu.cmd("stock", "add items from your lootsack")
menu.flag("gem", 'stock a particular type of gem in your lootsack')
menu.flag("n", "max number of items to stock")
menu.write
elsif Olib.CLI.cmd?('pick') && Olib.CLI.flag?('--gem')
Warehouse.pick_gem
elsif Olib.CLI.cmd?('pick') && Olib.CLI.flag?('--herb')
Warehouse.pick_herb
elsif Olib.CLI.cmd?('stock') && Olib.CLI.flag?('--gem')
Warehouse.stock
elsif Olib.CLI.cmd?('bounty')
echo "not yet implemented"
elsif Olib.CLI.cmd? 'm'
echo "entering maintenance mode..."
Warehouse.maintain
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment