ramtzok1 Posted November 30, 2018 Report Share Posted November 30, 2018 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. Quote Link to comment Share on other sites More sharing options...
Lion.Kanzen Posted November 30, 2018 Report Share Posted November 30, 2018 @(-_-) @elexis @stanislas69 Quote Link to comment Share on other sites More sharing options...
Stan` Posted November 30, 2018 Report Share Posted November 30, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted November 30, 2018 Author Report Share Posted November 30, 2018 (edited) 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 November 30, 2018 by ramtzok1 Quote Link to comment Share on other sites More sharing options...
Stan` Posted November 30, 2018 Report Share Posted November 30, 2018 C++ will always be faster 1 Quote Link to comment Share on other sites More sharing options...
Guest Posted November 30, 2018 Report Share Posted November 30, 2018 Or you can dump the stuff to stdout and let python read it from there. Not something I have attempted myself. Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted November 30, 2018 Author Report Share Posted November 30, 2018 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? Quote Link to comment Share on other sites More sharing options...
Guest Posted December 1, 2018 Report Share Posted December 1, 2018 (edited) 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 December 1, 2018 by Guest Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 1, 2018 Author Report Share Posted December 1, 2018 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? Quote Link to comment Share on other sites More sharing options...
Guest Posted December 1, 2018 Report Share Posted December 1, 2018 Aside from logging and printing stuff, no. Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 1, 2018 Author Report Share Posted December 1, 2018 5 minutes ago, (-_-) said: Aside from logging and printing stuff, no. What a bummer... :/ Quote Link to comment Share on other sites More sharing options...
Stan` Posted December 1, 2018 Report Share Posted December 1, 2018 40 minutes ago, ramtzok1 said: What a bummer... :/ 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. Quote Link to comment Share on other sites More sharing options...
Guest Posted December 1, 2018 Report Share Posted December 1, 2018 If that would be too spammy, you can also log things at (or every) n turns. Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 1, 2018 Author Report Share Posted December 1, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
Stan` Posted December 1, 2018 Report Share Posted December 1, 2018 You can also try to hook Python on the Js using stuff like webkit I think.https://stackoverflow.com/questions/8284765/how-do-i-call-a-javascript-function-from-python Quote Link to comment Share on other sites More sharing options...
Guest Posted December 1, 2018 Report Share Posted December 1, 2018 (Why not tensorFlow?) Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 1, 2018 Author Report Share Posted December 1, 2018 15 minutes ago, stanislas69 said: You can also try to hook Python on the Js using stuff like webkit I think.https://stackoverflow.com/questions/8284765/how-do-i-call-a-javascript-function-from-python Very interesting! I think I will be able to use the common-api if I manage to make the connection. Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 1, 2018 Author Report Share Posted December 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
Guest Posted December 2, 2018 Report Share Posted December 2, 2018 (edited) I mentioned it because using it may make this connecting a lot easier. Was referring to https://js.tensorflow.org/, not just TensorFlow. Edited December 2, 2018 by Guest Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 2, 2018 Author Report Share Posted December 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 2, 2018 Author Report Share Posted December 2, 2018 21 hours ago, stanislas69 said: You can also try to hook Python on the Js using stuff like webkit I think.https://stackoverflow.com/questions/8284765/how-do-i-call-a-javascript-function-from-python I looked at the game code, it uses SpiderMonkey. Won't be any problem to use V8 for example? Quote Link to comment Share on other sites More sharing options...
Stan` Posted December 2, 2018 Report Share Posted December 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 11, 2018 Author Report Share Posted December 11, 2018 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? Quote Link to comment Share on other sites More sharing options...
Stan` Posted December 11, 2018 Report Share Posted December 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
ramtzok1 Posted December 11, 2018 Author Report Share Posted December 11, 2018 5 minutes ago, stanislas69 said: Well the game doesn't have JQuery so you need to add the js file somewhere If I'm not wrong, the command: Engine.IncludeModule should help me? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.