[Lua] Using UI-commands for AI players

Tiramisu

Warlord
Joined
Aug 16, 2013
Messages
148
Location
Leonberg (BW, Germany)
There are some interesting UI-commands that work for human players (i.e. for the local player I guess), but I would also like to use these commands for the AI.

E.g. you can send an envoy to a city state using
Code:
UI.RequestPlayerOperation( playerID, PlayerOperations.GIVE_INFLUENCE_TOKEN, parameters);
Or you can liberate a city with CityManager.RequestCommand, which would actually allow reviving killed civilizations (apparently this is the only way to do that using Lua functions).

Now if I try these functions in Firetuner for AI players, then nothing happens. I wonder if there is a way to use these kind of commands also for AI players. Did anyone try something similar?
 
I think Firaxis has designed UI functions to not work with the AI (maybe to prevent issues in MP by hooking on UI files), when it does it's maybe considered as a bug on their side.

OTOH we lack the corresponding gameplay functions, so hacking is all we have until (if) they release the DLL source code or add new gameplay functions (that is done only for their scenario so far)

Side note: reviving a civ should be possible by creating a new city, for major civs at least (minors had issues IIRC what I had tested for historical spawning dates)

Edit: maybe see also Player GetDiplomacy in gameplay context for your example
 
Last edited:
I fear you are right, but still I am hoping that someone finds some hacks. It would be really nice.
I tried to play around with autoplay functions, but I have not found anything useful, yet.

Side note: reviving a civ should be possible by creating a new city, for major civs at least (minors had issues IIRC what I had tested for historical spawning dates)

I am afraid that this does not work for major civs. In one of my tests I spawned a city of a killed major civ with firetuner, but its AI was literally brain-dead. It was not doing anything. Also I could not choose any diplomatic actions with that civ.

Edit: maybe see also Player GetDiplomacy in gameplay context for your example

I have not found any function belonging to Player:GetDiplomacy() that revives a player.
The only way I know is to give a captured city to the human player and then liberate that city with the UI function. However, this method would grant some unwanted rewards to the human player like 3 envoys, if the revived civ is a city state.
That is why I would like to let the AI liberate the city, if it was possible.
 
I think we could use a workaround:
When the player is hitting the next turn button we could use the AutoplayManager to change the local player to the next player. Then it would be possible to use the UI functions for that player, but we would have to prevent the human player to see the UI screen of the next player's civilzation.
I know this is not a very satisfying workaround, but it would allow modders to do lots of great stuff like using UnitManager.RequestOperation to override the AI behavior.
 
I tried this out and it works, but every time I change the local player the game is freezing for a short time, which increases the AI turn duration. Can anyone tell me what is causing this lag?
Here is an example code that I am using in my tests:
Code:
local iOriginalLocalPlayerID = 0
function ChangeLocalPlayer( iPlayerID )
    if( iPlayerID == iOriginalLocalPlayerID ) then return end
    local iNextPlayer = GetNextAlivePlayerID( iPlayerID )
    print("Changing to Player " .. iNextPlayer)
    PlayerManager.SetLocalPlayerAndObserver( iNextPlayer )
    PlayerManager.SetLocalObserverTo( iOriginalLocalPlayerID ) --make sure human player does not see the UI of iNextPlayer
   
    print("Local Player ID: " .. Game.GetLocalPlayer())
end
GameEvents.PlayerTurnStartComplete.Add( ChangeLocalPlayer );

function GetNextAlivePlayerID( iPlayerID )
    for i = iPlayerID + 1, GameDefines.MAX_PLAYERS-1, 1 do
        if( Players[i]:IsAlive() ) then
            return i
        end
    end
    for i = 0, GameDefines.MAX_PLAYERS-1, 1 do
        if( Players[i]:IsAlive() ) then
            return i
        end
    end
    return -1
end
 
There are 2 functions in UnitManager: RequestCommand() and RequestOperation() (UI context). Do you think they have the same limitation i.e. only LocalPlayer can use them?
I wrote a function to help GWAMs activate and it uses MoveUnitToPlot() from Civ6Common, which in turn calls UnitManager.RequestOperation(). Works flawlessly when called from the tuner, but I am not convinced they move during AutoPlay.

Also, there are 2 functions in GamePlay context: MoveUnit() and PlaceUnit(), but I never used those. Do they work for AI, anyone knows?
 
As a general rule if a function works at all in a GameplayScript it works regardless of "who" it is used on.

The only differences would be those specific player, city, or unit methods that are AI-linked and not AI-linked. But I haven't checked to see if for example
Code:
Player:GetAi_Military()
works in a GameplayScript. I'm pretty sure tho that "GetAi_Military" is only meant for use on AI players as it has methods within it that would be irrelevant to a Human.
 
Back
Top Bottom