Jump to content

How I can listen to game data between the game and the AI?


ramtzok1
 Share

Recommended Posts

Hello,

I have a project where I want to connect Python to the game to implement a Machine learning on it.

Now is there a way I can listen to the messages the game sends to me with Python?

I have looked at Common-API and PetraBot and I have seen that it uses json format to serialize and deserialize game data but I don't fully understand  how I can listen to it from a Python file.

Thank you for your answer. :)

 

Link to comment
Share on other sites

51 minutes ago, ramtzok1 said:

Hello,

I have a project where I want to connect Python to the game to implement a Machine learning on it.

Now is there a way I can listen to the messages the game sends to me with Python?

I have looked at Common-API and PetraBot and I have seen that it uses json format to serialize and deserialize game data but I don't fully understand  how I can listen to it from a Python file.

Thank you for your answer. :)

Well you could find a way to hook yourself to the c++ interface of the AI with Python.

https://trac.wildfiregames.com/browser/ps/trunk/source/simulation2/components/ICmpAIInterface.cpp

https://trac.wildfiregames.com/browser/ps/trunk/source/simulation2/components/ICmpAIManager.cpp

https://trac.wildfiregames.com/browser/ps/trunk/source/simulation2/components/CCmpAIManager.cpp

Or you could write a script in JavaScript that reads files written by Python so

Game does stuff -> Js stores in file -> Python reads file -> Python does stuff -> Python writes files -> Js loads file -> Game does stuff

 

 

 

 

  • Like 1
Link to comment
Share on other sites

35 minutes ago, stanislas69 said:

Well you could find a way to hook yourself to the c++ interface of the AI with Python.

https://trac.wildfiregames.com/browser/ps/trunk/source/simulation2/components/ICmpAIInterface.cpp

https://trac.wildfiregames.com/browser/ps/trunk/source/simulation2/components/ICmpAIManager.cpp

https://trac.wildfiregames.com/browser/ps/trunk/source/simulation2/components/CCmpAIManager.cpp

Or you could write a script in JavaScript that reads files written by Python so

Game does stuff -> Js stores in file -> Python reads file -> Python does stuff -> Python writes files -> Js loads file -> Game does stuff

 

 

 

 

1
1

In term of performance, hooking into C++ interface is much faster?

Edited by ramtzok1
Link to comment
Share on other sites

5 hours ago, (-_-) said:

Or you can dump the stuff to stdout and let python read it from there. Not something I have attempted myself.

Do you have any idea where should I look for in order to start?

 

7 hours ago, stanislas69 said:

C++ will always be faster :)

Thank you!

As I said to (-_-), do you have any idea where to look for inside these files or perhaps other files to start hooking/dumping?

Link to comment
Share on other sites

23 hours ago, ramtzok1 said:

Now is there a way I can listen to the messages the game sends to me with Python?

The game doesn't send much to the AI. Just some events and gameState every n turns IIRC. The exact things sent would be in `AIInterface.js`. There are `GetRepresentation()` and `GetFullRepresentation()` functions which are exposed to C++ in `ICmpAIInterface`. From there, it's sent to the AI via `AIManager` AFAIK.

Those are the files where you should probably start off with.

Edited by Guest
Link to comment
Share on other sites

3 hours ago, (-_-) said:

The game doesn't send much to the AI. Just some events and gameState every n turns IIRC. The exact things sent would be in `AIInterface.js`. There are `GetRepresentation()` and `GetFullRepresentation()` functions which are exposed to C++ in `ICmpAIInterface`. From there, it's sent to the AI via `AIManager` AFAIK.

Those are the files where you should probably start off with.

I understand.

Do you have any idea how can I debug the JavaScript files?

Link to comment
Share on other sites

6 hours ago, stanislas69 said:

What would you have wanted ? You can literallyprint everything in a function by writing warn(uneval(object) and it will be saved in the interestinglog.html in the game data folder.

 

1 hour ago, (-_-) said:

If that would be too spammy, you can also log things at (or every) n turns.

I see.

Thank you very much for your help. :)

  • Like 1
Link to comment
Share on other sites

5 minutes ago, (-_-) said:

(Why not tensorFlow?)

It's too early for me to talk about the ML itself. My objectives Right now are to establish a connection between the game and the machine learning work environment, understand how I can grab valuable information from the game, process it, and report it back to the game.

Link to comment
Share on other sites

12 hours ago, (-_-) said:

I mentioned it because using it may make this connecting a lot easier.

Was referring to https://js.tensorflow.org/, not just TensorFlow.

Sorry for my misunderstanding.

You may be right but as I explored it, there is a chance I maybe prefer to use other libraries that Python can provide like "Keras" and "Scikit learn" regardless to TensorFlow.js, moreover I can't place any .js file inside the AI folder which isn't related to the AI itself because the game tries to load it, fails (Can't import Tensorflow) and then crashes.

Link to comment
Share on other sites

32 minutes ago, ramtzok1 said:

I looked at the game code, it uses SpiderMonkey.

Won't be any problem to use V8 for example?

 

As long as you run native js code no. Might need to hold back on some es6 features you'd normally use with npm like promises but apart from that you should be fine. Anyway most of your code will be python anyway.

The only things you might need to be careful about are Engine calls. We define some CPP functions in JS using C++ interfaces. But you won't have to use any of those outside of the game anyway.


Since you want to implement machine learning, you might want to also have a look at commands.txt files that are generated when playing the game. Those files contain all the commands from all the players. You might get some useful information by taking some of the ones that are uploaded to the forums by good players and trying to find patterns on those files with KNN and stuff on what makes someone win. It might outline some decisive moves. Might also get things wrong if your new ai tries the same thing on a different map with a different civilization.

Link to comment
Share on other sites

  • 2 weeks later...
On 12/2/2018 at 8:28 PM, stanislas69 said:

As long as you run native js code no. Might need to hold back on some es6 features you'd normally use with npm like promises but apart from that you should be fine. Anyway most of your code will be python anyway.

The only things you might need to be careful about are Engine calls. We define some CPP functions in JS using C++ interfaces. But you won't have to use any of those outside of the game anyway.


Since you want to implement machine learning, you might want to also have a look at commands.txt files that are generated when playing the game. Those files contain all the commands from all the players. You might get some useful information by taking some of the ones that are uploaded to the forums by good players and trying to find patterns on those files with KNN and stuff on what makes someone win. It might outline some decisive moves. Might also get things wrong if your new ai tries the same thing on a different map with a different civilization.

Hey,

It has been a while since I updated the thread.

I tried to make a local server between Python and JS using Flask.

I can't make a connection in Javascript side with the server, I tried using Ajax and jQuery but the game says every time $ (Ajax symbol) or jQuery.ajax() is undefined.

Do you know what is wrong?

Link to comment
Share on other sites

4 minutes ago, ramtzok1 said:

Hey,

It has been a while since I updated the thread.

I tried to make a local server between Python and JS using Flask.

I can't make a connection in Javascript side with the server, I tried using Ajax and jQuery but the game says every time $ (Ajax symbol) or jQuery.ajax() is undefined.

Do you know what is wrong?

Well the game doesn't have JQuery so you need to add the js file somewhere :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...