Page 1 of 1

Lua Questions

PostPosted: Friday, 20th January 2012, 15:14
by XuaXua
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.

Re: Lua Questions

PostPosted: Friday, 20th January 2012, 17:53
by dk
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

Re: Lua Questions

PostPosted: Friday, 20th January 2012, 18:17
by dk
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 :-)

Re: Lua Questions

PostPosted: Friday, 20th January 2012, 18:29
by dk
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).

Re: Lua Questions

PostPosted: Friday, 20th January 2012, 19:17
by XuaXua
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.

Re: Lua Questions

PostPosted: Friday, 20th January 2012, 20:53
by dk
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.

Re: Lua Questions

PostPosted: Friday, 20th January 2012, 21:02
by XuaXua
That's why I'd use tab to track the last messages read and maintain a list that it updates.