Sorry, I forgot we were only dealing with luxuries.
@Whoward69: The check would be to ensure compatibility with any mod that might decide to improve something with a farm.
...but even if it wasn't I only need to check if the resource is connected to the network so it doesn't matter which of the multiple improvements is connecting it).
I'm trying to understand the idea of a mod with which you're trying to preserve compatibility... the modded farm can now be built atop luxuries, but only connects certain new luxuries to the trading network? I guess that would give you a false positive in that case...
There is also the issue of the Moai which can be built on resources but doesn't connect them (I'm not sure about the other UIs; it might be the same with Terrace Farms and gems on hills).
FWIW, the only improvements with BuildableOnResources=1 in the Improvements table and no corresponding entry in Improvement_ResourceTypes (i.e., the GP improvements) are the Fort and the Moai.
Would it be something like this:
One note: Hungarian notation is a nice convention to let you know what type you're expecting for a variable. A "p" before a variable name means the variable is a pointer (in the CiV context, it'll be a pointer to table object). However, GetResourceType() and GetImprovementType() return integers, meaning you'd use an "i" before the variable name; the integer refers to the ID column of the Improvements or Resources table. Using the convention properly will tip you off if you're trying to compare an integer to a string (as you were in your code fragment).
I'm actually not an expert in Lua, nor CiV-specific Lua, but I think this will work:
Code:
local iResource = pPlot:GetResourceType();
if (iResource) then
if (GameInfo.Resources[iResource].Happiness > 0) then
local iImprovement = pPlot:GetImprovementType();
if (iImprovement) then
if not (pPlot:IsImprovementPillaged()) then
local condition = "ImprovementType = '".. GameInfo.Improvements[iImprovement].Type .."'";
for row in GameInfo.Improvement_ResourceTypes(condition) do
if (row.ResourceType == GameInfo.Resources[iResource].Type) then
Note I added the pillage check as whoward69 recommended. Also, I added the loop I mentioned above. Perhaps not necessary for luxuries, but if you're that concerned with preserving mod compatibility, it can't hurt.