Setting adjacency bonuses through Lua

StandardGaussian

Chieftain
Joined
Mar 8, 2017
Messages
15
Hey there,

A while back, I was trying to implement an odd sort of adjacency bonus detailed in this topic:
https://forums.civfanatics.com/threads/specific-resource-adjacency-bonuses-for-districts.612141

My workaround works 99% of the time, though of course it's far from ideal: you get a magic building all of a sudden, which isn't intuitive, and it's possible to stop meeting the adjacency requirements but still get the bonus (though the chances are kind of small).

However, since @Gleb Bazov kindly implemented a script to help me with culture bombs on improvements over strategic resources, I figured it would be best to re-implement that adjacency bonus in Lua, especially because I can't implement a desired leader trait in xml/sql either, so starting to learn how to mod Civ through Lua seemed like a good plan. So far, so good in terms of getting all the relevant districts and checking the surrounding plots for viable resources, but I have no idea how to actually go about setting the adjacency bonus in Lua. The limited API that's been gleaned by @Gedemon so far doesn't seem to show many setter methods, and it seems like knowledge about how to manipulate the game world (rather than just read it for UI purposes) is being done by word of mouth.

I took a stab at guessing the setter method, assuming that it would be a member method of a district object (insofar as Lua allows such things), but something like:

Code:
 pDist:SetAdjacencyBonuses("YIELD_PRODUCTION", 2)

Where pDist comes from a table populated by:

Code:
function GetAllPlayerDistrictsOfType(PlayerID, xDistrictType)
    local tTemporaryTable = {}
    local pPlayer = Players[PlayerID]
    if (pPlayer == nil) then
        return tTemporaryTable
    end
    local pPlayerCities = pPlayer:GetCities()
    if (pPlayerCities == nil) then
        return tTemporaryTable
    end
    local districtID = GameInfo.Districts[xDistrictType].Index
    print ("District ID is " .. districtID)
    for k, pCity in pPlayerCities:Members() do
        local pDistrict = pCity:GetDistricts():GetDistrict(districtID)
        if (pDistrict ~= nil) then
            print ("Found appropriate district. Adding to district table.")
            table.insert(tTemporaryTable,pDistrict)
        else
            print ("District not found.")
        end
    end
    return tTemporaryTable
end

predictably yields the result "function expected instead of nil"

Since setting plot ownership is done through WorldBuilder for the culture bomb:
Code:
WorldBuilder.CityManager():SetPlotOwner( pPickPlot:GetX(), pPickPlot:GetY(), pPlayer, pCityID )
I assume something similar is true for hand-crafting adjacency bonuses through Lua, but without a reliable API reference I feel pretty clueless what that might look like. What does WorldBuilder.CityManager() return? What are the parameters for the function I'm looking for? Can anyone lend a hand for this?
 
Last edited:
It doesn't matter what method you are using to get the district object because the issue is in attempting to use a non-existent function: SetAdjacencyBonuses

So far as I'm aware there isn't such a function for Player, City (or the dirstricts within cities), Unit, Plot, or Map.
 
@StandardGaussian LeeS is entirely correct - there is no such function (and nothing with the functionality you require that we are aware of). The best you could do, as far as I can see, is to simulate the adjacency bonus by triggerring a dummy building to be placed in a city when a district has the requisite adjacency (and remove it if/when adjacency is no longer present). This is fairly easy. I've been using the code from one of LeeS' mods as a standard for placing/removing buildings.

EDIT: The dummy building would obviously grant the yield that would otherwise be given via an adjacency bonus.
 
Top Bottom