• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

New to Lua, Help please (1 Viewer)

eammonwright

Newcomer
Joined
Nov 10, 2020
Messages
5
Reaction score
1
Hello everyone. I'm new here and very new to Lua. I have been learning it by making small scripts for a mud that I play. I am trying to take the next step in my learning by combining a few scripts together to make a functional bot. I dont care about the cheating aspect of it as I wont be running it. But gaming is the easiest way for me to learn new languages. Is this the right place to get help for something like that?
 

eammonwright

Newcomer
Joined
Nov 10, 2020
Messages
5
Reaction score
1
Lua:
require "wait"


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

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

  local ArrayBeach = 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}  -- 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


This is the array and what I am trying to accomplish. I have it laid out of what I want to do, and have the array set up. The mobs on the beach are Lobsters, Crabs, and Gulls. I have the following script written to check stats and attack when I see them.

Lua:
require "wait"
wait.make (function ()
   Send("hp")
   local line, wildcards = wait.match("*      HP [ */* ]     SP [ */* ]     EP [ */* ]")
   if tonumber(wildcards[2]) > 250 and tonumber(wildcards[4]) > 250 and tonumber(wildcards [6]) > 90 then

DoAfter (2,"attack lobster")

else
    wait.time (60)
    Send ("look")
end -- if
end)

oviously the names of the creatures are changed so there is a script for each.
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
91
Reaction score
32
Location
Victoria BC
Website
winlua.net
Hi! I've never made a bot, but your script looks cool. You didn't really ask a question. What is your question?

Can you tell me about this:

Lua:
local ArrayBeach = 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}  -- and so on

What does the "var = var = ..." assignment do?
 

eammonwright

Newcomer
Joined
Nov 10, 2020
Messages
5
Reaction score
1
Somehow the post is showing up weird for me. Var should not be anywhere in my code that I know of.

Lua:
require "wait"


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

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

  local ArrayBeach = 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"}

  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

Ok so I can program on a very amateur level. i'm trying to learn lua by making this bot. Above is the Loop routine for my character to roam this beach in the game. That is the array thats defined above. So in essence, the bot would start at the entry point to the array and walk each step in the array. As he comes across an enemy it runs the script below, depending on the enemy. He kills it, then waits until he is healed up, then continues on the array path where he left off. I wrote the layout of the loop above, and wrote the combat script below, but I guess this is where I get confused. How do I add the two together, and how do I make the features all work that i mentioned. I know this is a lot, but building it piece by piece helps me learn, and its pretty fun too.

Lua:
require "wait"
wait.make (function ()
Send("hp")
local line, wildcards = wait.match("* HP [ */* ] SP [ */* ] EP [ */* ]")
if tonumber(wildcards[2]) > 250 and tonumber(wildcards[4]) > 250 and tonumber(wildcards [6]) > 90 then

DoAfter (2,"attack lobster")

else
wait.time (60)
Send ("look")
end -- if
end)

Thanks for taking an interest :)
 

GavinW

Newcomer
Creator of RiscLua
Joined
Oct 21, 2020
Messages
40
Reaction score
17
Age
82
Location
UK
Website
www.wra1th.plus.com
Eammon, I think you misunderstood dinsdale247's last remark. The var referred to ArrayBeach, I believe. If I enter (with Lua 5.4.0) something like that I get an error:
Lua:
> local x = x = { a, b, c }
stdin:1: unexpected symbol near '='
I think your fourth (nonblank) line must have been typed in error. An assignment statement does not have a value (unlike, say, in C). So x = y = z simply raises an error in Lua.
 

eammonwright

Newcomer
Joined
Nov 10, 2020
Messages
5
Reaction score
1
AH, gotcha. Yes that is apparently different with lua syntax.

Code:
local ArrayBeach = 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"}

Should be
Code:
local 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"}

So theres one error down. Now I just need to figure out the rest of it :)
 

eammonwright

Newcomer
Joined
Nov 10, 2020
Messages
5
Reaction score
1
I never mentioned this before. I am using MushClient. for Muds, if you guys are familiar with those. So MushClient already has a lot of these functions and libraries built in like the .wait function. So if your running this code through another IDE you will get lots errors like this
no file 'C:\Program Files\Java\jre1.8.0_211\bin\lua\wait.lua'
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
91
Reaction score
32
Location
Victoria BC
Website
winlua.net
Ah, yes that was my next question, were you were running this. I found the MudClient API here: MUSHclient script functions list

Okay, so I see on the MUSHClient forum that Mr. Gammon was giving you a hand. In the first script, you will notice a block of comments. Those are the notes about where you should put more code. Each one of those lines needs to be replaced by real code. You need to add some code to wait for the room to change, check your hp, then decide to attack.

There seems to be some reference to a wait.match function. I couldn't find any documentation on the 'wait' module? Perhaps find that?

oh, here, I found it: Gammon Forum : MUSHclient : Lua : Lua modules supplied with MUSHclient

So:

1) move then wait for the room to change. That means you need to do a wait.match(...) for some text I presume. This will be a wait.match call right in your large function
2) You already have code to "Send" the hp command to check your health and save the values. You need to add that to your large function. I would say do a print of the values so you know what you are getting back.
3) You don't have any code for looking for enemies. You need to figure out how to do that. I assume its another wait.match and then you parse the text for values?
4) then do your attack.

Give it a shot and post back. Remember to give us the "debug output" including any errors. Use print statements to show what your doing.

Good luck! If I were you, I would look for any MUSHClient modules that you can find and start reviewing the code.
 
Top