Lua Questions


If you are interested in helping with tiles, vaults, patches or documentation, this is the place for that.

User avatar

Ziggurat Zagger

Posts: 5832

Joined: Thursday, 10th February 2011, 18:30

Post Friday, 20th January 2012, 15:14

Lua Questions

1) Is there a LUA command to retrieve the last message posted to the console?

2) Do messages have unique id's so I don't have to check a message twice, or are messages just text only? For example, if I get a queue of messages from the console and the last 3 messages were all "A goblin comes into view." is there a way to distinguish them from each other?

3) How do I check the suffix of a string in lua to compare if it ends with some specific text? EDIT: answered.
Last edited by XuaXua on Friday, 20th January 2012, 17:57, edited 1 time in total.
"Be aware that a lot of people on this forum, such as mageykun and XuaXua, have a habit of making things up." - minmay a.k.a. duvessa
Did I make a lame complaint? Check for Bingo!
Totally gracious CSDC Season 2 Division 4 Champeen!

dk

User avatar

Snake Sneak

Posts: 104

Joined: Wednesday, 7th December 2011, 22:20

Location: Germany

Post Friday, 20th January 2012, 17:53

Re: Lua Questions

XuaXua wrote:...
3) How do I check the suffix of a string in lua to compare if it ends with some specific text?


To compare if str ends with other_str (of the top of my head, didn't test it):

string.sub(str, -string.len(other_str)) == other_str

For this message the author dk has received thanks:
XuaXua

dk

User avatar

Snake Sneak

Posts: 104

Joined: Wednesday, 7th December 2011, 22:20

Location: Germany

Post Friday, 20th January 2012, 18:17

Re: Lua Questions

XuaXua wrote:...
2) Do messages have unique id's so I don't have to check a message twice, or are messages just text only? For example, if I get a queue of messages from the console and the last 3 messages were all "A goblin comes into view." is there a way to distinguish them from each other?
...


It seems a message consists of the following (after a quick look at the source):

- channel
- parameter for that channel
- text
- repeats
- turn
- join (may this message be joined with others?)

So I don't think it's possible to distinguish messages from each other other than these properties.

EDIT: Since the LUA-function crawl.messages just returns a single string, this would not help you here.

Feel free to correct me if I'm wrong :-)
Last edited by dk on Friday, 20th January 2012, 18:31, edited 1 time in total.

For this message the author dk has received thanks:
XuaXua

dk

User avatar

Snake Sneak

Posts: 104

Joined: Wednesday, 7th December 2011, 22:20

Location: Germany

Post Friday, 20th January 2012, 18:29

Re: Lua Questions

XuaXua wrote:1) Is there a LUA command to retrieve the last message posted to the console?
...


Try crawl.messages(x), where x is the number of messages you want to retrieve. So, to get the last message, use crawl.messages(1).

The function will return the messages newline seperated (it returns a single string).

For this message the author dk has received thanks:
XuaXua
User avatar

Ziggurat Zagger

Posts: 5832

Joined: Thursday, 10th February 2011, 18:30

Post Friday, 20th January 2012, 19:17

Re: Lua Questions

dk wrote:
XuaXua wrote:1) Is there a LUA command to retrieve the last message posted to the console?
...


Try crawl.messages(x), where x is the number of messages you want to retrieve. So, to get the last message, use crawl.messages(1).

The function will return the messages newline seperated (it returns a single string).


Figured you might know the answer offhand, whats the character for newline in lua strings?

Reason I'm asking:
I'm trying to create the whole "pause tab on autofight if a new monster comes into view" ability in the autofight.lua.
I want to track message state so we can see if the prior message is the same as the current message and therefore should be ignored, etc.
The one outstanding issue I know we'll see is if we have the same message twice, which is possible with, say, a bunch of green rats or a nest of hydras (hydrae?) and failing to pause on the subsequent message because it's identical to the prior.
"Be aware that a lot of people on this forum, such as mageykun and XuaXua, have a habit of making things up." - minmay a.k.a. duvessa
Did I make a lame complaint? Check for Bingo!
Totally gracious CSDC Season 2 Division 4 Champeen!

dk

User avatar

Snake Sneak

Posts: 104

Joined: Wednesday, 7th December 2011, 22:20

Location: Germany

Post Friday, 20th January 2012, 20:53

Re: Lua Questions

A good starting point would be this quick hack:

Add the following function to autofight.lua:

  Code:
local function comes_into_view_msg(message)
  local m = " into view."
  return string.find(message, m) ~= nil
end


And insert the following lines at top of the attack function:

  Code:

  local lastmessage = crawl.messages(1):gsub("\n", "")

  if lastmessage ~= nil and
    lastmessage ~= glastmessage and
    comes_into_view_msg(lastmessage) then
    crawl.mpr("Beware of getting TAB-killed!")
   glastmessage = lastmessage
   return
  else
    glastmessage = lastmessage
  end
 


Add this line to your init-file:
  Code:
force_more_message = Beware of getting TAB-killed!


This will basically do what you want. The only problem is when multiple messages are displayed while tabbing:

See an enemy.
Press TAB.
You move towards the enemy.
You see a new enemy and a pile of junk, so the following two messages are displayed:
1) An Orc comes into view.
2) You see here a dagger.
The last message is 'You see here a dagger' so the exit condition is not triggered.

For this message the author dk has received thanks:
XuaXua
User avatar

Ziggurat Zagger

Posts: 5832

Joined: Thursday, 10th February 2011, 18:30

Post Friday, 20th January 2012, 21:02

Re: Lua Questions

That's why I'd use tab to track the last messages read and maintain a list that it updates.
"Be aware that a lot of people on this forum, such as mageykun and XuaXua, have a habit of making things up." - minmay a.k.a. duvessa
Did I make a lame complaint? Check for Bingo!
Totally gracious CSDC Season 2 Division 4 Champeen!

Return to Contributions

Who is online

Users browsing this forum: No registered users and 5 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.