[patch] Interactive python console via chat

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

[patch] Interactive python console via chat

#1 Post by Cjkjvfnby »

This patch allow to run interactive console in AI python environment.

I suggest to turn it on by default for test builds.

Usage:
enter help in chat
enter start 2
enter print fo.getEmpire().name
See screenshoot example https://drive.google.com/file/d/0B12sy1 ... FjVE0/view

PS. it is possible to use remote debugger:
It allow to inspect all variables in global scope and have interactive console.

PyDev
http://pydev.org/manual_adv_remote_debugger.html
http://stackoverflow.com/questions/25018869/pydev-how-to-invoke-debugging-specific-command-from-console-with-breakpoints/25065948#25065948 wrote:Note that if you want to use the interactive console in the context of a breakpoint, a different approach would be selecting a stack frame (in the debug view) > right-clicking it > pydev > Debug Console (or you can also in the debug view create a new console view connected to the debug session for the same effect).
Attachments

[The extension patch has been deactivated and can no longer be displayed.]

If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [patch] Interactive python console via chat

#2 Post by Geoff the Medio »

Pretty neat, and seemingly useful. Seems to work for me. Nice that it's just an AI .py file change.

It's a bit awkward to have to send it to a particular AI, though... For me, only AI_5 responds to "help", and messages sent directly to one of the others get no response. (Click an AI name in the empires list to specify chat message recipients...)

Also, the list of AI ids is coloured, seemingly to better identify them. But the AI colours in the list of ids doesn't match the corresponding AI names, which is a bit weird... so perhaps it would be useful to also include the AI name in the list of IDs to make it clear that the number in an AI name isn't the same as its "debug" id?

It would also be preferable to have the debug init chat messages be looked up in the stringtable before being sent to the player.

Can you use this mechanism to force an AI to issue an order (or send an arbitrary chat message or diplomacy message)? A means to tell the debugged AI to not end its turn as soon as possible, or to otherwise interrupt the next normal turn-playing-in-response-to-server-update sequence would be useful.

User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

Re: [patch] Interactive python console via chat

#3 Post by Cjkjvfnby »

Geoff the Medio wrote:Pretty neat, and seemingly useful. Seems to work for me. Nice that it's just an AI .py file change.

It's a bit awkward to have to send it to a particular AI, though... For me, only AI_5 responds to "help", and messages sent directly to one of the others get no response. (Click an AI name in the empires list to specify chat message recipients...)

Chat sends messages to all AI but only first response. AI did not know if chat to all or only for him.
Also, the list of AI ids is coloured, seemingly to better identify them. But the AI colours in the list of ids doesn't match the corresponding AI names, which is a bit weird... so perhaps it would be useful to also include the AI name in the list of IDs to make it clear that the number in an AI name isn't the same as its "debug" id?
OK, chat dialog looks strange. I don't like that AI_? in it.
It would also be preferable to have the debug init chat messages be looked up in the stringtable before being sent to the player.
I don't expect to see it release build. So why it should be translated?
Can you use this mechanism to force an AI to issue an order (or send an arbitrary chat message or diplomacy message)? A means to tell the debugged AI to not end its turn as soon as possible, or to otherwise interrupt the next normal turn-playing-in-response-to-server-update sequence would be useful.
I think it is has same rights as generateOrders but triggers form other place. I expect, you can call any code from freeOrionAIInterface. I expect that then you call chat generateOrders is already finished. You can change variable in chat and access it in generateOrders.

upd. patch attached
Attachments

[The extension patch has been deactivated and can no longer be displayed.]

If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [patch] Interactive python console via chat

#4 Post by Geoff the Medio »

Cjkjvfnby wrote:Chat sends messages to all AI but only first response. AI did not know if chat to all or only for him.
It might be indicated by the recipient ID of the message... if it's -1, then it might have been sent to all... (not sure about this, though...)
I don't expect to see it release build. So why it should be translated?
non-english speakers might want to debug AI scripts. I would probably put this in the release... if people want to cheat by manipulating the AI, they already can.
I expect that then you call chat generateOrders is already finished.
Yes, the AI would likely already have finished its turn by the time it got an interactive debug chat message. I meant could it set up to easily tell the AI script to wait on the next turn and not issue orders or end its turn until told to do so.

User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

Re: [patch] Interactive python console via chat

#5 Post by Cjkjvfnby »

Geoff the Medio wrote:
Cjkjvfnby wrote:Chat sends messages to all AI but only first response. AI did not know if chat to all or only for him.
It might be indicated by the recipient ID of the message... if it's -1, then it might have been sent to all... (not sure about this, though...)
Message is used for diplomatic orders this get just int and str def handleChatMessage(senderID, messageText)

I don't expect to see it release build. So why it should be translated?
non-english speakers might want to debug AI scripts. I would probably put this in the release... if people want to cheat by manipulating the AI, they already can.
Ok.
I expect that then you call chat generateOrders is already finished.
Yes, the AI would likely already have finished its turn by the time it got an interactive debug chat message. I meant could it set up to easily tell the AI script to wait on the next turn and not issue orders or end its turn until told to do so.
If you want to open console inside generatingOrders you need to use remote debuger (see end of first post)

You can replace one generateOrder to another but you will not get interactive console inside it via chat.
Last edited by Dilvish on Wed Feb 18, 2015 12:42 am, edited 1 time in total.
Reason: fixed quote markers
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [patch] Interactive python console via chat

#6 Post by Geoff the Medio »

Cjkjvfnby wrote:You can replace one generateOrder to another but you will not get interactive console inside it via chat.
OK, but could you set a flag so that after the next time generateOrders runs, it doesn't end turn, so that the player could interactively control the AI via chat?

Not sure if you do this already, but if the AI is running on a computer with a graphical interface, can it generate its own window to use as an interactive console from within Python code (independent of the Ogre or SDL windows)?

User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

Re: [patch] Interactive python console via chat

#7 Post by Cjkjvfnby »

Geoff the Medio wrote:
Cjkjvfnby wrote:You can replace one generateOrder to another but you will not get interactive console inside it via chat.
OK, but could you set a flag so that after the next time generateOrders runs, it doesn't end turn, so that the player could interactively control the AI via chat?
I think yes, but need to check it.
Not sure if you do this already, but if the AI is running on a computer with a graphical interface, can it generate its own window to use as an interactive console from within Python code (independent of the Ogre or SDL windows)?
I will try to find info if it easy to do. It is not exactly what you ask, but have similar effect: http://pydev.org/manual_adv_remote_debugger.html
Last edited by Vezzra on Wed Feb 18, 2015 1:02 pm, edited 1 time in total.
Reason: Fixed quotation tag
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [patch] Interactive python console via chat

#8 Post by Geoff the Medio »

Cjkjvfnby wrote:upd. patch attached
committed

Edit: Is there any way to do multi-line python commands via chat? For example, doing a for loop that prints something for each value of the loop variable? I tried prepending my chat commands with spaces to indicate the loop body, but I think it's stripping the leading whitespace. Would this work if that wasn't removed?

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [patch] Interactive python console via chat

#9 Post by Dilvish »

Geoff the Medio wrote:Edit: Is there any way to do multi-line python commands via chat? For example, doing a for loop that prints something for each value of the loop variable?
Although not commonly used, semicolons can be used to separate python commands:

Code: Select all

 for n in [1,2,3]: print "n: %d" % n; print "and now n-squared: %d" % (n*n)
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

Re: [patch] Interactive python console via chat

#10 Post by Cjkjvfnby »

Dilvish wrote:
Geoff the Medio wrote:Edit: Is there any way to do multi-line python commands via chat? For example, doing a for loop that prints something for each value of the loop variable?
Although not commonly used, semicolons can be used to separate python commands:

Code: Select all

 for n in [1,2,3]: print "n: %d" % n; print "and now n-squared: %d" % (n*n)
I will make examples with reload and execfile when back from my trip.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Cjkjvfnby
AI Contributor
Posts: 539
Joined: Tue Jun 24, 2014 9:55 pm

Re: [patch] Interactive python console via chat

#11 Post by Cjkjvfnby »

If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [patch] Interactive python console via chat

#12 Post by Dilvish »

Cjkjvfnby wrote:I update wiki section.
It's a start, but I think it needs a fair bit more clarification (and probably we should make the operation more straightforward). For example, simply typing 'help' into the chat window currently appears to do nothing unless AI_1 is selected in the empires window, and if one then simply follows the instructions to try to debug a different AI without having changed the selection in the empires window then again nothing happens. It could be that instead of giving more info on how it needs to interact with the empires window we should instead have the AIs also check messages which get sent to 'ALL_EMPIRES' as you briefly considered above. If "help" gets sent to all empires, then just the first AI responds, and if 'start #' gets sent to all empires then that AI responds and thereafter that AI will consider all chat to all empires to be (potential) debugging commands to itself until it gets a 'stop'

I also think that the entire thing should be described right there in the Internal Debugging section, rather than just mentioning it and then referring them down to Tips and Tricks. This section should also include some description/examples of what can be done with the local vars u,e, and ai.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: [patch] Interactive python console via chat

#13 Post by Geoff the Medio »

Is this broken currently? I'm not having any success trying to start the debugging in the chat...

Or perhaps I'm just not getting any AI chat?

Edit: Seems to only be an issue with the first AI... with 2, the second has working chat messages, but still doesn't respond to "help" or "start 0" or "start 1" or similar chat messages. /Edit
Messages in Game wrote:Creating AI Clients
Generating Universe
Begin Turn 1
Saving...
Saved 43127 bytes to file: C:\Users\Geoff\AppData\Roaming\FreeOrion\save\auto\FreeOrion_Human_Player_Terran_0001_20180617_094100.sav
[17 Jun 09:41:01] AI_1: Affiliation (Turtle):
[17 Jun 09:41:12] Human_Player: help
[17 Jun 09:41:23] Human_Player: test
[17 Jun 09:41:24] Human_Player: debug
[17 Jun 09:43:33] Human_Player: help
[17 Jun 09:44:14] Human_Player: start 1
[17 Jun 09:44:17] Human_Player: start 0
[17 Jun 09:44:18] Human_Player: start 2
[17 Jun 09:44:21] Human_Player: print "test"
AI_1.log wrote:09:41:01.884209 [debug] python : DiplomaticCorp.py:85 - Received diplomatic status update to war about empire 2 and empire 1
09:41:12.265709 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: help
09:41:23.526782 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: test
09:41:24.688324 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: debug
09:43:33.222839 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: help
09:44:14.687801 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: start 1
09:44:17.376070 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: start 0
09:44:18.835064 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: start 2
09:44:21.904235 [debug] python : DiplomaticCorp.py:90 - Midgame chat received from player 1, message: print "test"

Morlic
AI Contributor
Posts: 296
Joined: Tue Feb 17, 2015 11:54 am

Re: [patch] Interactive python console via chat

#14 Post by Morlic »

It must be activated by an AI config option:
allow_debug_chat=1

You may want to start freeorion using
--ai-config .\default\python\AI\ai_debug_config.ini
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

User avatar
Dilvish
AI Lead and Programmer Emeritus
Posts: 4768
Joined: Sat Sep 22, 2012 6:25 pm

Re: [patch] Interactive python console via chat

#15 Post by Dilvish »

Morlic wrote:It must be activated by an AI config option:
allow_debug_chat=1

You may want to start freeorion using
--ai-config .\default\python\AI\ai_debug_config.ini
I've now added that info to the Wiki page on the debug mode.
If I provided any code, scripts or other content here, it's released under GPL 2.0 and CC-BY-SA 3.0

Post Reply