what is it exactly that Python handles in CivIV? Is it simply a matter of interface? Is the AI completely ruled by C++?
Yes to both. I've noticed your
posts in the K-Mod subforum too – the AStar Python utility in K-Mod is part of BUG (purely a UI mod), which uses it for highlighting tiles that human units can reach in one turn. (But that feature is disabled by default.) For AI pathfinding, an A* star implementation in the EXE (C++, closed source) was originally used; I think that's the only example of AI code outside of the (GameCore) DLL, and K-Mod has reimplemented that more efficiently in the DLL (KmodPathFinder class).
There are also a bunch of "callback" Python functions (
CvGameInterface.py) through which modders can alter the game rules and, in a few instances, also the AI. However, the AI callbacks and AI functions exposed to Python by the DLL are too few to rewrite significant portions of the AI in Python; I think the idea is only to make minor additions (perhaps for making the AI at least somewhat capable of using some novel element of gameplay) in Python that run before or after the AI code in the DLL.
It shouldn't be too difficult to give Python more control over the AI by exposing more DLL functions and adding more callbacks, however, Civ4's Python system uses Python version 2.4, and my understanding is that integration with a more recent version would be a major undertaking. (Similarly, the DLL is stuck at Visual C++ 2003.)
I don't think performance – overhead of calls between DLL and Python and just interpreted code being much slower than compiled – would have to be a major obstacle so long as pathfinding (at least in AdvCiv the biggest time hog) and other low-level routines remain in the DLL. For example, I expect that a good part of the AI war planning logic that I've written would be fast enough in Python.
Your posts bring to mind
this AI project for the Caveman2Cosmos mod. [
Edit: A month later, all threads and posts by
@JaguarDemon are gone. Don't know what's up with that.] The thread linked there about the abandoned Machine Learning project also
touches upon the issue of upgrading the Python system. I don't believe (for one second) that a full re-design of the AI is feasible as a one-person project, let alone for the most complex Civ mod (but, then, I've only skimmed through that thread). Imo the most efficient approach to improving the AI in one person's spare time is to work with what we have as much as possible. That's not to say that this will accomplish a great deal – I'd agree that BBAI, K-Mod and AdvCiv haven't accomplished any quantum leap –, and of course it's not all about efficiency. Engaging with those old programming languages and largely unstructured AI logic is somewhat dispiriting and not necessarily all that useful as a learning experience.
Maybe you could tackle some well-defined subproblem that the AI needs to solve? For example, I've replaced the AI logic for war planning (essentially scrapping the existing code, i.e. not exactly heeding my present-day advice) and have largely rewritten the city placement AI. [Let me link to some code here, let's say, the entry points
UWAI::Agent::doWar and
AIFoundValue::evaluate.] Unit movement (
CvUnitAI), i.e. worker management and combat tactics, remains a weak spot. Those are big chunks to bite off, however, that seem pretty tightly coupled with the rest of the DLL. Difficult for me to imagine moving that to Python.