Author Topic: Lua functions and events for chat messages [Interface Discussion]  (Read 1565 times)

CruzR

  • Guest
I've implemented lua functions and events for handling chat messages here. This is just a draft, please don't merge into the master branch (yet). Since nobody suggested any specific changes, i changed the commit message and merged with origin/master. It can now merged into the main git repo.
An example scenario can be found here.

Let me explain how it currently works:
  • When someone enters a chat message, the chat message string and the faction id are stored in a list; after this, the lua code inside the 'chatEvent' XML node is executed.
  • The lua function 'chatMessage()' returns the message string of the first list element; the function 'chatMessageFaction()' returns the faction id of the first list element.
  • The function 'popChatMessage()' is used to remove the first list element after you're done with the message.

However, I think that this interface could still be improved, so I'd like to get feedback from both the gae devs and the community.

Possible changes/improvements:
  • The lua functions could take an integer as parameter which determines which list element to choose for the operation.
  • The functions 'chatMessage()' and 'chatMessageFaction()' could be merged into one function which returns a lua table. (This was my original plan, but I couldn't figure out how to do it.)

If you have any additional suggestions on how else to improve the interface, just post your ideas.

My questions are: How do you like this interface? What should be improved?
« Last Edit: 16 October 2011, 18:28:49 by CruzR »

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #1 on: 12 October 2011, 02:15:34 »
Awesome work CruzR! And for those who don't want to have to download a file and comb through the folders, here's the example:
Code: (XML) [Select]
<?xml version="1.0" standalone="yes" ?>
<scenario>
<difficulty value="1" />
<players>
<player control="cpu" faction="magic" team="2" />
<player control="human" faction="tech" team="1" />
<player control="human" faction="magic" team="3" />
</players>
<map value="riverside" />
<tileset value="forest" />
<tech-tree value="magitech" />
<fog-of-war value="false" />
<default-resources value="true" />
<default-units value="true" />
<default-victory-conditions value="false" />
<scripts>
<startup>
debugLog('startup works!')
consoleMsg('startup works!')
</startup>
<chatEvent>

debugLog('chatEvent works!')
consoleMsg('chatEvent works!')

message = chatMessage()
faction = chatMessageFaction()

debugLog(message)
consoleMsg(message)
debugLog(faction)
consoleMsg(faction)

if message == 'I will always win!' then
if faction == 0 then
setPlayerAsWinner(1)
endGame()
elseif faction == 1 then
setPlayerAsWinner(0)
endGame()
end
elseif message == 'Simon says: I will always win!' then
setPlayerAsWinner(faction)
endGame()
end

popChatMessage()
</chatEvent>
</scripts>
</scenario>

So to my understanding, you can use
Code: [Select]
if chatMessage() == 'Hello' then
consoleMsg('Hi')
end
popChatMessage()
To check to see if the last chat message was "Hello", and if so, reply with a "Hi". Not sure how useful chatMessageFaction() is, though, since only the player can send messages, and multiplayer scenarios are not currently possible, and would be quite some time in the future, if ever (though that would be an interesting concept).

[small]Totally offtopic, but does anyone remember the name of the Lua function that sent a coloured message to the player's console area, as though they were receiving a message from another player? It seems to have slipped through my documentation on the wiki...[/small]
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

CruzR

  • Guest
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #2 on: 12 October 2011, 14:36:22 »
Awesome work CruzR!

Thanks ;-)

Quote
So to my understanding, you can use
Code: [Select]
if chatMessage() == 'Hello' then
consoleMsg('Hi')
end
popChatMessage()

To check to see if the last chat message was "Hello", and if so, reply with a "Hi".

Exactly.

Quote
Not sure how useful chatMessageFaction() is, though, since only the player can send messages, and multiplayer scenarios are not currently possible, and would be quite some time in the future, if ever (though that would be an interesting concept).

Well, my implementation currently isn't suitable for multiplayer anyway ... but you never know whether there might be multiplayer scenarios in gae one day. In addition, this can be used to write cross-scenario lua modules (since the player doesn't always have to have faction id 0).

Quote
[small]Totally offtopic, but does anyone remember the name of the Lua function that sent a coloured message to the player's console area, as though they were receiving a message from another player? It seems to have slipped through my documentation on the wiki...[/small]

I think these are two functions:
addActor(name, color) or addActor(name, r, g, b), where
  • name is a string containing the name for the actor to add
  • color is a string in hex rgb format
  • r, g, b are integers containing the rgb color value.

And:
addDialog(actor, msg), where
  • actor is a string containing the actor name
  • msg is a string containing the message to print.

will

  • Golem
  • ******
  • Posts: 783
    • View Profile
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #3 on: 12 October 2011, 14:52:27 »
I would imagine that a script ought to be able to filter message each message that is sent or received by a player: for each message in, it can let it through or replace it or suppress it.  And at any point in can choose to send new messages, with a choice of whether they are echoed to the player's own console.

And there should be shared state in the script for these different event hooks, so it can consume all sent messages and then, a while later, send them.  With this, it is not necessary for all messages that are understood by scripts to be one-liners.

You can imagine scripts that communicate with each other without their messages to each other being displayed to either player.  This might make very interesting team-based collective scripts possible; AI meta-players, even.

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #4 on: 12 October 2011, 18:51:07 »
I think these are two functions:
addActor(name, color) or addActor(name, r, g, b), where
  • name is a string containing the name for the actor to add
  • color is a string in hex rgb format
  • r, g, b are integers containing the rgb color value.

And:
addDialog(actor, msg), where
  • actor is a string containing the actor name
  • msg is a string containing the message to print.
Thanks, was trying to remember those. Added them to the wiki so they won't get lost again.

I hope that I'll be able to use this feature to create several new types of scenarios, including one where you command a computer controlled AI by telling it commands: and it will fill in the blanks, but is faced with a superior foe, so you'd have to smartly decide when to attack, what to produce, etc; As well, a scenario where you can tell your AI ally when to attack, and a few basic other commands. I may even try and experiment with the concept of diplomacy.
« Last Edit: 18 June 2016, 18:59:09 by filux »
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

Psychedelic_hands

  • Guest
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #5 on: 13 October 2011, 09:39:26 »
This is AWESOME;D
Nice work CruzR, thanks!
I'm imagining the possiblities now... This would be excellent for Lua AI one day.... (Soon'ish hopefully  ;))

hmmmm would this http://sourceforge.net/apps/trac/glestae/ticket/190 be possible using the same sort of thing as this?

CruzR

  • Guest
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #6 on: 17 October 2011, 12:13:26 »
This is AWESOME;D
Nice work CruzR, thanks!
I'm imagining the possiblities now... This would be excellent for Lua AI one day.... (Soon'ish hopefully  ;))
Thanks ;-)

Quote
hmmmm would this http://sourceforge.net/apps/trac/glestae/ticket/190 be possible using the same sort of thing as this?
Well, of course you could script some sort of AI reaction on your chat command... but I think the ticket is rather different from what is possible right now ...

silnarm

  • Local Moderator
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #7 on: 27 October 2011, 20:20:42 »
Nice work CruzR, I've been meaning to doctor up LuaArguments so you can return a table (the cleaner syntax of your original plan is much nicer), but have not had a chance to get to it yet :( Extra long week-end starting in 12 or so hours though, so I'll get that to you soon hopefully :)

I would imagine that a script ought to be able to filter message each message that is sent or received by a player: for each message in, it can let it through or replace it or suppress it.  And at any point in can choose to send new messages, with a choice of whether they are echoed to the player's own console.

Good idea, should only need a suppressChatMessage() and an addChatText(), although addDialog() could probably be modified to accept player names to handle the second.

Quote
And there should be shared state in the script for these different event hooks, so it can consume all sent messages and then, a while later, send them.  With this, it is not necessary for all messages that are understood by scripts to be one-liners.

You can imagine scripts that communicate with each other without their messages to each other being displayed to either player.  This might make very interesting team-based collective scripts possible; AI meta-players, even.

There is only one LuaState when the game is running anyway, so the engine doesn't need to do anything special here, it can be done from Lua already (or will be with the above added function(s)).

I've added the following query functions,
Code: [Select]
getPlayerCount()
getHumanPlayerIndex()
getPlayerName()
getPlayerTeam()
getPlayerColour()

These are meant to be helpful for Omega's "give orders to semi-AI players via chat" scenarios, hopefully there are  :-\
There are also some undocumented AI helper functions I added some time ago, not going to reveal anything more than that at this point, because I'm not sure what state I left them in ;)
Glest Advanced Engine - Code Monkey

Timeline | Downloads

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #8 on: 28 October 2011, 04:29:25 »
I've added the following query functions,
Code: [Select]
getPlayerCount()
getHumanPlayerIndex()
getPlayerName()
getPlayerTeam()
getPlayerColour()

These are meant to be helpful for Omega's "give orders to semi-AI players via chat" scenarios, hopefully there are  :-\
Despite that we can only have pre-defined game settings for the scenarios (ie, the factions, number of players, order of players, etc is already set in the scenario), these may come in handy in generic functions, as well as give more room for control should we ever see "player-chooseable" settings on scenarios.

There are also some undocumented AI helper functions I added some time ago, not going to reveal anything more than that at this point, because I'm not sure what state I left them in ;)
*Contemplates digging through commits. Decides against it* Sounds good.
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

silnarm

  • Local Moderator
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Lua functions and events for chat messages [Interface Discussion]
« Reply #9 on: 30 October 2011, 05:39:42 »
Tables can be returned... I had branched you on github a while back, but didn't update first and the branch is gone/renamed/something... not sure if a pull request would be more trouble now than its worth ;)

https://github.com/silnarm/GAE/commit/c41c3a5650c18dffa3ed9edd0f2a39a6d519626b

Edit: Ha... Lua supports multiple return values btw!! probably would have been just as easy/effective for two values... oh well, will be nice to be able to return tables none the less.
Glest Advanced Engine - Code Monkey

Timeline | Downloads

 

anything