;==================================
; Script Name: Freddy's Luna Buyer
; Author: Freddy
; Version: 1.0
; Client Tested with: 5. Something
; EUO version tested with: 1.5 (Build 0064)
; Shard: ALL
; Revision Date 11 May 2018
; Creation Date: 11/07/2011
; Uses Neo's buy subs. Thanks neo
;==================================
Luna Reg Buyer:
This script will stand in the corner of the mage shop and buy regs from the mages/alchemists while hidden. It's been updated to reflect the changes made to the bank system. 
Will not work with EJ accounts (if that's still a thing) 
;==================================
; Script Name: Freddy's Luna Buyer
; Author: Freddy
; Version: 1.0
; Client Tested with: 5. Something
; EUO version tested with: 1.5 (Build 0064)
; Shard: ALL
; Revision Date 11 May 2018
; Creation Date: 11/07/2011
; Uses Neo's buy subs. Thanks neo 
;==================================
set %vendor ; Item ID of the mage reg vendors separated with an underscore like xxxxx_xxxxx_xxxxx_ 
set %item_type_buy DPF_JUF_KZF_WZF_SZF_JZF_KUF_RZF_MZF_ ;these are all the mage regs ID, you can change it to buy whatever they sell. wont work with potions or spell scrolls tho blank scrolls work just fine
set %bank_container ; item id of a container in your bank to place the regs
set %keep_hidden #true ;set to false if you don't want to hide 
set %delay_between_loops 60 ;in seconds the amount of time to wait after scanning every vendor
;-----------------------Don't edit below----------------------
;-------------------------------------------------------------
set #lpc 1000
gosub separate_string %item_type_buy _
for %u 1 %t_num
{
  set %item . %u %return . %u
}
set %itemcnt %t_num
set %t_num 0
gosub separate_string %vendor _
for %u 1 %t_num
{
  set %vendor . %u %return . %u
}
set %vendcnt %t_num
set %t_num 0
event macro 3 0 bank
wait 15
set %bankid #contid
mainloop:
for %vendor_buy 1 %vendcnt
{
  wait 15
  set %vendor %vendor . %vendor_buy
  for %itm 1 %itemcnt
  {
    wait 15
    if %keep_hidden = #true
    {
      while H notin #charstatus
      {
        event macro 13 21
        wait 12s
      }
    }
    gosub get_gold
    wait 10
    while #result <> #false
    {
      set %num 999
      set %buy_this %item . %itm
      if %buy_this in wuf_dpf
      {
        set %num #maxweight - #weight
      }
      if #gold < 2
        gosub get_gold
      event property %vendor
      if alchemist in #property
        set %position 5
      else
        set %position 2
      gosub buy %vendor %buy_this %num %position
      if #result = #true
      {
sto_loop:
        finditem %bank_container *
        if #findkind = -1
        {
          event macro 3 0 bank
          wait 1s
          set #lobjectid %bank_container
          wait 10
          event macro 17 0
          wait 1s
          goto sto_loop
        }
        finditem %buy_this C_ , #backpackid
        if #findkind <> -1
        {
          wait 20
          exevent drag #findid #findstack
          wait 12
          exevent dropc %bank_container
          wait 12
        }
      }
    }
  }
}
wait %delay_between_loops , s
goto mainloop
;-------------------------------------------------------------
;-------------------------------------------------------------
sub buy
  gosub buy_books %1 %2 %3 %4
  if #result = #true
    return #true
  else
  return #false
;-------------------------------------------------------------
;-------------------------------------------------------------
sub buy_books ;neo
  namespace push
  namespace local neobuy
  set !vendor %1
  set !buy %2
  set !amount %3
  set !buyposition %4
  set !shopyes #false
  finditem !vendor G_8
  repeat
    exevent popup #findid
    gosub gumpwait normal_gump null
    wait 5
    set !x #contposx + 30
    set !y #contposy + ( 20 * !buyposition )
    click !x !y f dmc
    gosub gumpwait bill_gump null
  until #result
  set !item_x #CONTPOSX + 11
  set !item_y #CONTPOSY - 150
  set !next_x #CONTPOSX + 71
  set !next_y #CONTPOSY - 15
  set !accept_x #CONTPOSX + 42
  set !accept_y #CONTPOSY + 205
  getshopinfo
  wait 5
  for !i 1 #shopcnt
  {
    getshopinfo
    set !shopid #shopitemid
    if #shopitemtype in !buy
    {
      set !shopyes #true
      for %x 1 3
        click !item_x !item_y f dmc
      getshopinfo
      wait 10
      if #shopItemMax < !amount
      {
        set !amount #shopItemMax
      }
      setShopItem #shopitemid !amount
      break
    }
    set %timer3 #scnt + 5
    repeat
      if #scnt > %timer3
        return #false
      click !next_x !next_y f dmc
      getshopinfo
      if #shopcnt = 0
        return #false
    until #shopitemid <> !shopid || !i = #shopcnt
  }
  click !accept_x !accept_y f dmc
  wait 20
  if !shopyes <> #true
    set #result #false
  if !shopyes
    set #result #true
  namespace pop
return #result
;-------------------------------------------------------------
;-------------------------------------------------------------
sub get_gold
  return
gloop:
  if #gold <= 4999
  {
    finditem POF C_ , %bankid
    for #findindex 1 #findcnt
    {
      if #findstack > 5000
      {
        set %get 5000 - #gold
        exevent drag #findid %get
        wait 12
        exevent dropc #backpackid
        wait 12
        return
      }
    }
    if #gold < 2000
    {
      finditem EWH C_ , %bankid
      if #findkind <> -1
      {
        for #findindex 1 #findcnt
        {
          event property #findid
          str len #property
          if #findcol = 52 && #strres >= 52
          {
            set #lobjectid #findid
            wait 10
            event macro 17 0
            wait 30
            goto gloop
          }
        }
      }
    }
    if #gold < 2000
    {
      set %get 5000 - #gold
      event macro 3 0 withdraw %get
      wait 15
    }
    if #gold < 2000
    {
      display You don't have any gold!
      halt
    }
  }
return
;----------------------------------------------------------------------
;----------------------------------------------------------------------
sub separate_string
  set %t_num 0
  str Count %1 %2
  for %i 1 #strres
  {
    set %var %2
    str len %1
    set %length #strres
    str pos %1 %var
    set %del #strres - 1
    str left %1 %del %length
    set %return . %i #strres
    set %del1 %del + 1
    str del %1 1 %del1
    set %1 #strres
    set %t_num %t_num + 1
  }
return
;----------------------------------------------------------------------
;----------------------------------------------------------------------
;======= Gumpwait Sub ======= ;neo
sub gumpwait
  namespace push
  namespace local gumpwait
  set !contname %1
  set !contsize %2
  set !timeout #SCNT + 5
  if !contsize = null
  {
    repeat
    until #contname = !contname || #scnt >= !timeout
    {
      if #contname = !contname
      {
        namespace pop
        return #true
      }
      namespace pop
      return #false
    }
  }
  repeat
  until ( #contname = !contname && #contsize = !contsize ) || #scnt >= !timeout
  if #contname = !contname && #contsize = !contsize
  {
    namespace pop
    return #true
  }
  namespace pop
return #false
;======= End Sub =======