Resource Revealed

GeneN

Chieftain
Joined
Apr 11, 2017
Messages
16
Does anyone have an algo suitable for a tuner panel that determines if a resource (e.g., Iron) should be revealed to a player. That is, has the player researched the required tech (e.g., Bronze Working)? I'm writing a tuner panel to identify the location of resources on the map and would like to filter the list so that only appropriate resources are listed.
 
try:
Code:
player:IsResourceVisible(resourceID)

Edit: oops, see LeeS post below for correct syntax
 
Last edited:
Thanks for the hint! Unfortunately, Player:GetResources():IsResourceVisible(resourceID) doesn't work for me in a tuner panel. At least the following tuner panel code prints a list of resources names, associated IDs and correct resource counts. However, when the line using IsResourceVisible is not commented, the code only prints the header and playerId.

Suggestions?

<EnterAction>
print("Test Entered")
local player, resource, resourceName, resources, resourceID, isVisible, resCount
local resourceIdByName = {}
player = Game.GetPlayers{Human = true}[1]
print("Human playerId: " .. player:GetID())
for resource in GameInfo.Resources() do
resourceName = string.gsub(resource.ResourceType, "RESOURCE_", "")
resourceIdByName[resourceName] = resource.Index
end
resources = player:GetResources()
for resourceName, resourceID in pairs(resourceIdByName) do
isVisible = true
-- isVisible = resources:IsResourceVisible(resourceID)
resCount = resources:GetResourceAmount(resourceID)
if isVisible then print(resourceName .. "\t" .. resourceID .. "\t" .. resCount) end
end
</EnterAction>
 
just wrap with code wraps. code inside the [] starts the code-block, and /code inside the [] ends the code block.

If I try to show you what is needed directly the forum just auto-formats it into the code-block instead of allowing me to show it as text.

So as like this (except without the . )

[.code]some code that you want in a block[./code]

If I then remove the period signs it turns into
Code:
some code that you want in a block
 
Try
Code:
--====================================================================================================================
--	Check whether a resource is revealed to a player
--====================================================================================================================

function ResourceIsRevealedToPlayer(iPlayer, iResource)
	local tResourceData = GameInfo.Resources[iResource]
	if tResourceData ~= nil then
		local tTech = GameInfo.Technologies[tResourceData.PrereqTech]
		if (tTech ~= nil) then
			return Players[iPlayer]:GetTechs():HasTech(tTech.Index)
		else
			print("ResourceIsRevealedToPlayer: " .. tResourceData.ResourceType .. " has no prerequisite tech, checking against civics")
			local tCivic = GameInfo.Civics[tResourceData.PrereqCivic]
			if (tCivic ~= nil) then
				return Players[iPlayer]:GetCulture():HasCivic(tCivic.Index)
			else
				print("ResourceIsRevealedToPlayer: " .. tResourceData.ResourceType .. " has no prerequisite civic or tech, returning true")
				return true
			end
		end
	else
		print("ResourceIsRevealedToPlayer: invalid resource data for resource Index # " .. tostring(iResource) .. ", returning false")
	end
	return false
end
Untested yet but I think there are no coding errors. I've used a slightly different version in Gameplay scripts and it works without error messages for invalid functions and the like, but I just added the Civics check and tightened the code a bit.
 
First, thank you for providing the help understanding how to add code to a post. Most useful!
Second, I'm not yet sophisticated enough to know where to put a function that supports a live tuner panel, so I couldn't test your function code directly. However, the approach works great. Thank you so much!
 
Back
Top Bottom