1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Resource Revealed

Discussion in 'Mod Creation Help' started by GeneN, May 22, 2017.

  1. GeneN

    GeneN Chieftain

    Joined:
    Apr 11, 2017
    Messages:
    16
    Gender:
    Male
    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.
     
  2. Gedemon

    Gedemon Modder Moderator

    Joined:
    Oct 4, 2004
    Messages:
    8,860
    Location:
    France
    try:
    Code:
    player:IsResourceVisible(resourceID)
    Edit: oops, see LeeS post below for correct syntax
     
    Last edited: May 23, 2017
  3. LeeS

    LeeS Imperator

    Joined:
    Jul 23, 2013
    Messages:
    6,614
    Location:
    Illinois, USA
  4. GeneN

    GeneN Chieftain

    Joined:
    Apr 11, 2017
    Messages:
    16
    Gender:
    Male
    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>
     
  5. GeneN

    GeneN Chieftain

    Joined:
    Apr 11, 2017
    Messages:
    16
    Gender:
    Male
    BTW, could someone tell me how to upload code in a reply?
     
  6. LeeS

    LeeS Imperator

    Joined:
    Jul 23, 2013
    Messages:
    6,614
    Location:
    Illinois, USA
    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
     
  7. LeeS

    LeeS Imperator

    Joined:
    Jul 23, 2013
    Messages:
    6,614
    Location:
    Illinois, USA
    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.
     
  8. GeneN

    GeneN Chieftain

    Joined:
    Apr 11, 2017
    Messages:
    16
    Gender:
    Male
    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!
     

Share This Page