Lua Bot

Posted by Eammon Wright on Tue 29 Sep 2020 06:07 PM — 8 posts, 29,590 views.

#0
Ok, so those that have helped me learn so far know, i'm learning lua making a bot that will walk a square path on this beach, stop when he sees a creature, check stats, kill creature, then resume his path. I want this to run a loop so when he hits the final square he goes back to square one and starts over if that makes sense. So the next part is an Array or a Table I think from what ive read.


ArrayBeach = {w,w,w,w,w,n,e,e,e,e,e,n,w,w,w,w,w,n,e,e,e,e,e,n,w,w,w,w,w,n,e,e,e,e,e,s,s,s,s,s,s,s}

DoAfter (5,ArrayBeach[1])


Now I know very little about this still so i'm taking it one step at a time. I have the array, and when i run the keyword it just does nothing. So if someone is again willing to help, where is my first step in fixing what is wrong with this?
Australia Forum Administrator #1
You need to use "w" rather than w.

eg.


ArrayBeach = {"w", "w", ... , "s"}



Your array is filled with the contents of variables n, s, e and w which are probably all nil (because they won't exist) and thus the array is full of nils, and effectively empty.
#2
Ok so that worked great, simple mistake.


ArrayBeach = {"w","w","w","w","w","n","e","e","e","e","e","n","w","w","w","w","w","n","e","e","e","e","e","n","w","w","w","w","w","n","e","e","e","e","e","s","s","s","s","s","s","s"}

DoAfter (5,ArrayBeach[1])
DoAfter (5,ArrayBeach[2])
DoAfter (5,ArrayBeach[3])
DoAfter (5,ArrayBeach[4])
DoAfter (5,ArrayBeach[5])
DoAfter (5,ArrayBeach[6])
DoAfter (5,ArrayBeach[7])
DoAfter (5,ArrayBeach[8])
DoAfter (5,ArrayBeach[9])
DoAfter (5,ArrayBeach[10])
DoAfter (5,ArrayBeach[11])
DoAfter (5,ArrayBeach[12])
DoAfter (5,ArrayBeach[13])
DoAfter (5,ArrayBeach[14])
DoAfter (5,ArrayBeach[15])
DoAfter (5,ArrayBeach[16])
DoAfter (5,ArrayBeach[17])
DoAfter (5,ArrayBeach[18])
DoAfter (5,ArrayBeach[19])
DoAfter (5,ArrayBeach[20])
DoAfter (5,ArrayBeach[21])
DoAfter (5,ArrayBeach[22])
DoAfter (5,ArrayBeach[23])
DoAfter (5,ArrayBeach[24])
DoAfter (5,ArrayBeach[25])
DoAfter (5,ArrayBeach[26])
DoAfter (5,ArrayBeach[27])
DoAfter (5,ArrayBeach[28])
DoAfter (5,ArrayBeach[29])
DoAfter (5,ArrayBeach[30])
DoAfter (5,ArrayBeach[31])
DoAfter (5,ArrayBeach[32])
DoAfter (5,ArrayBeach[33])
DoAfter (5,ArrayBeach[34])
DoAfter (5,ArrayBeach[35])
DoAfter (5,ArrayBeach[36])
DoAfter (5,ArrayBeach[37])
DoAfter (5,ArrayBeach[38])
DoAfter (5,ArrayBeach[39])
DoAfter (5,ArrayBeach[40])
DoAfter (5,ArrayBeach[41])
DoAfter (5,ArrayBeach[42])



This is just the beggining of it but this will run the whole map, however, it only does the first doafter. After that it speedruns the whole thing. Any thoughts on that problem?
Australia Forum Administrator #3

You have heard of loops, haven’t you?

for i = 1, #ArrayBeach do  -- do whole array
  DoAfter (5, ArrayBeach[i])
end

Although that will do them all after 5 seconds. Maybe you want:

for i = 1, #ArrayBeach do  -- do whole array
  DoAfter (i * 5, ArrayBeach[i])
end

That does each one 5 seconds apart.

#4
That's Awesome, Yes sir. I was reading about loops, however, I was completley unsure if it was possible to have a loop set up, but stop when he comes across a creature, run the creature script, kill, wait and heal, then resume. Plus the loops were a little confusing for me.

Well I know it's possible, I just assumed it was wat too difficult for someone at my level, so Piece by piece it is.
Amended on Thu 01 Oct 2020 12:03 PM by Eammon Wright
Australia Forum Administrator #5

My loop won’t do that, but then neither would your long list of DoAfter do it either.

You need something we discussed elsewhere, where you build the directions into a “wait” alias, where you do something like:

  • Send a direction
  • Wait for a message indicating you have changed rooms, and see if there is a mob there (and which one it is)
  • Attack if necessary
  • Wait a few seconds perhaps
  • Advance to next direction in the list (wrapping at the end)
  • Go back to first point above.
#6
Yes sir. That's exactly what i'm looking to learn to do. So now that I have the bassic loop and map layout, what would be thr prooper way to pause that loop when I see one of those 3 creatures?
Australia Forum Administrator #7

Looking at your other thread about this you need to use the concepts there. Now I’m not going to write your bot for you, but this is the general structure:


require "wait"


wait.make (function ()  --- coroutine below here

  time_to_stop = false  -- make true elsewhere to make the loop stop

  local ArrayBeach = {"w","w","w","w","w","n","e"}  -- and so on

  local current_pos = 1 -- start at start of array
  repeat
    local direction = ArrayBeach [current_pos] -- get direction

    -- next item in array for next time around loop
    current_pos = current_pos + 1

    -- wrap at end of array
    if current_pos > #ArrayBeach then 
      current_pos = 1 
    end

    Send (direction)  -- go that way

-- ---------------------------------------------
--  Wait for room change (wait.match)
--  Find your HP etc. (wait.match)
--  Find what monsters are here (wait.match)
--  Decide to attack or not  (Send "attack")
-- ---------------------------------------------


    wait.time (5) -- allow 5 seconds before looping
  until time_to_stop

end)  -- end of coroutine