Detecting if a plot has a luxury resource

turingmachine

Emperor
Joined
May 4, 2008
Messages
1,438
So I need to detect if a plot has a luxury resource. I know how to do this if I manually list each luxury resource type and go through them, but there must be an easier way, right (plus that way wouldn't work if someone is using a mod that adds new luxury resources).

Is there an shorter way to detect if a plot has a luxury resource?

Thanks!
 
Use plot:GetResourceType() and then see if GameInfo.Resources[resourceType].Happiness > 0.
 
Okay, so the previous advice worked great; however, I realized I need one more step.

I need to check if the plot has an improvement which connects the luxury resource. So for example, that the citrus tile has a plantation instead of say a farm; that the fur tile is improved with a camp instead of a farm, etc.

Again, I can do this checking individual improvements for individual resources, but a general way like with checking for luxuries would be preferred.

Thanks again.
 
You mean plot:GetImprovementType()?

EDIT: Then you'd compare your resource above with GameInfo.Improvement_ResourceTypes[improvementType].ResourceType to see if you can find a match [note you'll need a loop since there can be multiple--you're not clear on how you're going to use this, so you can decide whether to skip the check if it's a Great Person improvement on a strategic resource].
 
I need to check if the plot has an improvement which connects the luxury resource. So for example, that the citrus tile has a plantation instead of say a farm; that the fur tile is improved with a camp instead of a farm, etc.

Again, I can do this checking individual improvements for individual resources, but a general way like with checking for luxuries would be preferred.

In the standard game, you can't build a farm (say) on a luxury resource, but you can only build the improvement that connects the resource - this is true for all luxuries. So if a lux has an improvement, by definition it is the one that connects the lux to the trade network. So a simple test for pPlot:GetImprovementType() ~= -1 will suffice (and you may also want to test pillaged status)
 
You mean plot:GetImprovementType()?

EDIT: Then you'd compare your resource above with GameInfo.Improvement_ResourceTypes[improvementType].ResourceType to see if you can find a match [note you'll need a loop since there can be multiple--you're not clear on how you're going to use this, so you can decide whether to skip the check if it's a Great Person improvement on a strategic resource].

In the standard game, you can't build a farm (say) on a luxury resource, but you can only build the improvement that connects the resource - this is true for all luxuries. So if a lux has an improvement, by definition it is the one that connects the lux to the trade network. So a simple test for pPlot:GetImprovementType() ~= -1 will suffice (and you may also want to test pillaged status)

@Whoward69: The check would be to ensure compatibility with any mod that might decide to improve something with a farm.

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).

@Nutty

Improvement_ResourceTypes seems to be the right entry in the improvements xml file, but I'm unsure how to do the final formatting (multiple improvements connecting the resource shouldn't be a problem as its only for luxuries not strategic so there should only be one improvement, 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).

Would it be something like this:

Code:
if not (plot:GetResourceType() == -1) then
	local pResource = plot:GetResourceType(); 
	if GameInfo.Resources[pResource].Happiness > 0 then
		if not (plot:GetImprovementType() == -1) then
			local pImprovement = plot:GetImprovementType(); 
			if GameInfo.Improvement_ResourceTypes[pImprovement].ResourceType == pResource then
 
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.
 
Code:
				local condition = "ImprovementType = '".. GameInfo.Improvements[iImprovement].Type .."'";
				for row in GameInfo.Improvement_ResourceTypes(condition) do
					if (row.ResourceType == GameInfo.Resources[iResource].Type) then

This is great. Would you mind if I also used the snippet to check if the resource tile is connected to the trade network?
 
Would you mind if I also used the snippet
Feel free (in general as to anybody's code snippets, and in particular as to mine)! No need to even ask, but I'm glad my relatively meager skills have been put to use [though it's largely a copy-paste-rearrange of Firaxis code]!
 
Back
Top Bottom