Changing Game.AIPlay exit condition ?

Thrasybulos

Prince
Joined
May 4, 2023
Messages
520
Currently, that function takes a number of turns as a parameter and runs for that number of turns, no matter what.

Would it be possible to have it exit sooner, if an AI triggers a victory condition?
 
You mean CyGame.setAIAutoPlay(int iTurns)?

You can always stop autoplay by setting CyGame.setAIAutoPlay(0) again. How and under which circumstances to do that depends on your specific goals. I am not sure how easy it is to open the dev console while autoplay is going, but that would be one way of doing it manually. If you want it to automatically stop on a specific condition, you would need to add that code to an event handler. Since autoplay is only relevant for the turn change, it is probably sufficient to use "BeginGameTurn" or "EndGameTurn" for this purpose.
 
Thanks for the answer, it's giving me something to have a look into, at least. :thumbsup:

I run a lot of AIPlay games in the background, and actively monitoring them is not an option.

There are two issues with AIPlay "overshooting" the victory trigger:
  • The replay file isn't saved
  • Endgame scores aren't available (and I'd like to have them available)
Having it stop when victory is triggered should fix both issues (that's what happens when I luck into the actual endgame turn when inputing the number of turns).
 
Just tested calling "game.aiplay 0" from the console while aiplay was already running : it does stop aiplay and triggers the replay. So I would indeed get both the replay file and a look at the endgame scores of the remaining AIs.
But...
But it doesn't respawn the lion unit for the human player, so that means the only option is to leave the game: no way to have a look around, and more problematic, no way to save the game (I intend to use the save parser dll which was published a short while ago to extract data about the game : while I could get the same information from the replay file, there's no parser for that format to my knowledge, and writing my own is way beyond the time and effort I'm willing to allocate).
:(

I could obviously set the autosave every turn with no limit to the number of files, but I'm trying to avoid that (it slows the game, and having a constant write stream to the disk is not ideal).
 
I could obviously set the autosave every turn with no limit to the number of files, but I'm trying to avoid that (it slows the game
Does it? I mean, I have set it for every turn with a limit of 500 files but I don't feel any difference.
 
Yeah, that sounds right. I think if you want a more automated approach you need to implement it in Python.
 
But it doesn't respawn the lion unit for the human player, so that means the only option is to leave the game: no way to have a look around, and more problematic, no way to save the game [...] I could obviously set the autosave every turn [...].
In CvGame::doTurn, it says:
C++:
	if (getAIAutoPlay() > 0)
	{
		changeAIAutoPlay(-1);
		if (getAIAutoPlay() == 0)
			reviveActivePlayer();
	}
Sounds like game.aiplay 1 on the developer console might do the trick. Or CyGame().reviveActivePlayer() on the Python console (holding Shift in addition to tilde). Doing that from a Python event handler sounds the most promising to me. reviveActivePlayer should set the AI play turns to 0 too:
C++:
void CvGame::reviveActivePlayer()
{
	if (!(GET_PLAYER(getActivePlayer()).isAlive()))
	{
		setAIAutoPlay(0);
I recall being unable to open autosaves created during AI play; not sure if that's working at all.

There's also the AI Auto Play mod component (originally by jdog5000). Merging that into a mod will require recompilation of the DLL, but numerous mods already include it and may have improved it slightly. Also has its issue: I don't think autosaves work quite correctly either, key presses aren't reliably registered while it's running, the human civ remains intact but, except when playing on Noble, its handicap is a mix of Noble and of the AI settings of the human handicap. It does have a key combination (Ctrl+Shift+X) for starting and stopping.

Regarding the human civ, the cleanest treatment (using the Noble handicap for all?) might be Autorun in CivilizationIV.ini. Doesn't stop any more flexibly than game.aiplay though.
 
Thanks a lot.
Both options (game.aiplay 1 and CyGame().reviveActivePlayer()) indeed work from the console.
The second option might be the better one to call from Python as it doesn't induce a 1-turn delay.

Now I'll have to try and see about that (I have experience neither with Python nor with Civ's SDK, so I expect to be fumbling around a bit. :D ).

Does it? I mean, I have set it for every turn with a limit of 500 files but I don't feel any difference.
When playing the game normally, I agree it doesn't make a difference.
But with aiplay it does (well, at least it did on my previous, old computer: maybe it wouldn't on the new one, haven't tried it out actually).
But if @f1rpo is correct and the files can't be opened, that's a moot point anyway. :(

There's also the AI Auto Play mod component (originally by jdog5000).
Yes, I know about that one. @Keler, who's also running a lot of AI games, uses it.
But the fact the player isn't passive is a major drawback for me (the big one being getting dragged in a war through a DP), so I'd rather work with aiplay.
 
Apart from the handicap inconsistency (unless I'm missing something), the human player can just be a regular participant when using the AI Auto Play mod. That too could be fixed in the DLL. But, as it is, I can see why you'd rather have a side-lined player. Ought to interfere with AI starting locations; but I guess the AI Survivor enthusiasts have thought about that.
 
Back
Top Bottom