Get AI to work a plot after spawning resource

Pazyryk

Deity
Joined
Jun 13, 2008
Messages
3,584
I know I've seen this covered before, but I can't find it...

After I spawn a resource on a plot near a city, how do I get the AI (either AI player or city manager for human) to reevaluate what plots to work?

Will this do it?:
Code:
city:SetPopulation(city:GetPopulation(), true)
 
You have to jump through many hoops to get that to work - remove citizen from plot if being worked by a city, remove ownership of plot from any city, add resource, reinstate ownership of plot to city, and then finally set the plot as being worked by the city.

The order is very important as the number of resources "owned" by the player is updated in some very unobvious points in the C++ code

I have some Lua code to do it in one of my mods, but it was taken (with permission) from IGE, so I'd start there.
 
Code:
--
-- Helper function to change the resource/improvement on a given plot
-- allows for the plot being free, owned, owned by a city, and worked by a city
--
function ChangeResource(pPlot, iResource, iCount, iImprovement)
  local iOwner = pPlot:GetOwner()
  if (iOwner ~= -1) then
    local pCity = pPlot:GetWorkingCity()
    local bWorking = false
    local bForced = false
  
    if (pCity ~= nil) then
      bWorking = pCity:IsWorkingPlot(pPlot)
	  if (bWorking) then
	    bForced = pCity:IsForcedWorkingPlot(pPlot)
	    pCity:AlterWorkingPlot(pCity:GetCityPlotIndex(pPlot))
	  end
    end

    pPlot:SetOwner(-1)
    pPlot:SetResourceType(iResource, iCount)
    pPlot:SetOwner(iOwner)
    pPlot:SetImprovementType(iImprovement)
  
    if (bWorking) then
      if (bForced) then
	    pCity:AlterWorkingPlot(pCity:GetCityPlotIndex(pPlot))
	  else
        Network.SendDoTask(pCity:GetID(), TaskTypes.TASK_CHANGE_WORKING_PLOT, 0)
	  end
    end
  else
    pPlot:SetResourceType(iResource, iCount)
    pPlot:SetImprovementType(iImprovement)
  end
end
 
Back
Top Bottom