FreeOrion

Forums for the FreeOrion project
It is currently Thu May 23, 2013 12:21 am

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Python in FreeOrion
PostPosted: Tue Jun 15, 2010 8:01 pm 
Offline
Space Krill

Joined: Mon May 31, 2010 5:00 pm
Posts: 4
I don't know any C++, however I'd consider myself good at Python (after all I use python all day at work). Knowing part of this game is done in Python I was wondering if I could be of any help. Unfortunately I've never worked on a mixed language project and have no experience with writing Python code to be used in a C++ app. So for someone new to this kind of stuff and a total Python geek, what could I dive into?


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Tue Jun 15, 2010 9:45 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7891
Location: Vancouver, BC
Python 2.6 in FreeOrion is currently used only for AI scripting.

The relevant .py files are located in the /default/AI directory. To start AI programming, all you need to do is edit those files, or add new ones as appropriate.

There is some out of date documentation of the API on the wiki. Most it is still accurate, but there have been some changes since it was updated.

If you run into problems, you can always look at the C++ code that exposes the interface to Python. It might be tricky to get all the details of what various functions expect or do, but at the least you can see what functions, properties and objects are available in the general order-giving interface code and the various class exposing code files.

To start, looking over the existing Python code is probably the best way to start.


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 5:08 pm 
Offline
Space Floater

Joined: Sun Aug 15, 2010 5:20 pm
Posts: 32
I'm having a bit difficulties when I'm trying to experiment with the Python scripts. I read somewhere that you don't have to rebuild the project if you modify the scripts. So all that has to be done is to modify, save the script and run freeorion.exe?


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 5:14 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7891
Location: Vancouver, BC
Yes.

It might actually be possible to modify the AI scripts while the program is running, though I haven't tried this myself.

Just make sure you're editing scripts in the right location, which is within the resource directory the progam is currently using, which is generally the default directory inside the FreeOrion directory where the executables / binaries are located. If you're got an old config.xml or have modified the resource directory, it could be somewhere else, though.

Also, if you're trying to modify files in Program Files on Vista or Win7, it might cause problems because the OS doesn't like you modifying files there. So, it's suggested to put the resource directory somewhere else / use the SDK if developing AI scripts.


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 5:31 pm 
Offline
Space Floater

Joined: Sun Aug 15, 2010 5:20 pm
Posts: 32
Geoff the Medio wrote:
Yes.

It might actually be possible to modify the AI scripts while the program is running, though I haven't tried this myself.

Just make sure you're editing scripts in the right location, which is within the resource directory the progam is currently using, which is generally the default directory inside the FreeOrion directory where the executables / binaries are located. If you're got an old config.xml or have modified the resource directory, it could be somewhere else, though.


I'm editing files in the ...\FreeOrion\default\AI. I'm trying to create some kind of a debug file from the scripts that I can see what's happening, but for example the code below doesn't do anything.

Code:
# called when a new game is started (but not when a game is loaded).  should clear any pre-existing state
# and set up whatever is needed for AI to generate orders
def startNewGame():
    print "New game started"

   f = open('c:\FreeOrionDev\pytDebugfile.txt', 'w')
   f.write('startnewgame\n')
   f.close()

    # initialize AIstate
    global foAIstate
    foAIstate = AIstate.AIstate()
    print "Initialized foAIstate class"


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 5:35 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7891
Location: Vancouver, BC
What is "..." in that path?

Check AI_1.log for error messages and debug output. Your attempts to open and write a file might not be successful, and the error message might tell you why.

To generate debug output, just use print "whatever you want to say" and then read it in the log file.


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 5:45 pm 
Offline
Space Floater

Joined: Sun Aug 15, 2010 5:20 pm
Posts: 32
Geoff the Medio wrote:
What is "..." in that path?

Check AI_1.log for error messages and debug output. Your attempts to open and write a file might not be successful, and the error message might tell you why.

To generate debug output, just use print "whatever you want to say" and then read it in the log file.


Ok, that's a good hint. :)


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 6:24 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7891
Location: Vancouver, BC
etintel wrote:
that's a good hint. :)

Which is why it's mentioned on the Python API wiki page.


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 6:43 pm 
Offline
Space Floater

Joined: Sun Aug 15, 2010 5:20 pm
Posts: 32
Geoff the Medio wrote:
etintel wrote:
that's a good hint. :)

Which is why it's mentioned on the Python API wiki page.


But it doesn't say the name or location of the file.


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Wed Aug 25, 2010 6:58 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7891
Location: Vancouver, BC
etintel wrote:
But it doesn't say the name or location of the file.

Now it does.


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Thu Aug 26, 2010 12:52 pm 
Offline
Space Floater

Joined: Sun Aug 15, 2010 5:20 pm
Posts: 32
I would like to add function immediateneighbors(...) in universe.cpp to the python API. What do I have to do? Also how do you loop through an array that has a double as an index?


Top
 Profile  
 
 Post subject: Re: Python in FreeOrion
PostPosted: Thu Aug 26, 2010 4:28 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7891
Location: Vancouver, BC
etintel wrote:
I would like to add function immediateneighbors(...) in universe.cpp to the python API. What do I have to do?

If you want to return a std::map<double, int> to Python, you'll need to use the map_indexing_suite to expose that class to Python, similar to how the vector_indexing_suite is used in PythonAI.cpp (section titled in comments: STL Containers) to expose the vectors of int and std::string. Then you should be able to add the function to the Universe class in PythonUniverseWrapper.cpp using .def, similar to:
Code:
            .def("systemHasStarlane",           &Universe::SystemHasVisibleStarlanes)
            .def("systemsConnected",            &Universe::SystemsConnected)
I don't know whether you'd need to do anything with return value policies for this function, but if you did, return by value would seem appropriate.

Quote:
Also how do you loop through an array that has a double as an index?

It's a map, not an array. You can use
Code:
std::map<double, int>::iterator it = map.begin(); it != map.end(); ++it) { ... }
to iterate through it in C++. In Python, I'm not sure exactly how the resulting data structure would appear and function, but presumably it would be similar to a Python dict.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group