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

Resources not connecting to Trade Network

Discussion in 'Civ5 - SDK / LUA' started by sukritact, Feb 14, 2014.

  1. sukritact

    sukritact Artist and Modder

    Joined:
    Sep 21, 2010
    Messages:
    2,881
    Location:
    Bangkok
    I've been trying to make an ability where mines have a chance of discovering metal-type luxury resources.

    I've managed to make the resource appear but it's not connecting to the trade network; so the player doesn't actually receive the resource. They just get a mine with Gold/Silver/Copper in it. I can't figure why it's so or how I could fix this.

    I've just been triggering Plot:SetResourceType on GameEvents.BuildFinished. But here's the relevant function:

    Code:
    local iMine = GameInfoTypes.IMPROVEMENT_MINE
    
    function MineDiscovery(iPlayer, iX, iY, iImprovement)
    	local pPlayer = Players[iPlayer]
    	if (pPlayer:IsMinorCiv()) or (pPlayer:IsBarbarian()) then
    		return
    	end
    	if (iImprovement ~= iMine) then
    		print("not mine")
    		return
    	end
    	
    	--Must be in woking distance of a city you own
    	local bStop = true
    	local pPlot = Map.GetPlot(iX, iY)
    	for pAdjacentPlot in PlotAreaSweepIterator(pPlot, 3, SECTOR_NORTH, DIRECTION_CLOCKWISE, DIRECTION_OUTWARDS, CENTRE_INCLUDE) do
    		if pAdjacentPlot:IsCity() then
    			local pCity = pAdjacentPlot:GetPlotCity()
    			if pCity:GetOwner() == iPlayer then
    				bStop = false
    				break
    			end
    		end
    	end
    	if (bStop) then
    		return
    	end
    	
    	local iPercentage = GetPersistentProperty("iPercentage_P" .. iPlayer)
    	iRandom = math.random(1,10)
    	print(iRandom, iPercentage)
    	if iRandom > iPercentage then
    		print("Percentage failed")
    		return
    	end
    	if pPlot:GetResourceType() ~= -1 then
    		print("Resource failed")
    		return
    	end
    	
    	pPlot:SetResourceType(iLuxury, 1)
    	iPercentage = iPercentage - 1
    	if iPercentage < 1 then
    		iPercentage = 1
    	end
    	
    	SetPersistentProperty("iPercentage_P" .. iPlayer, iPercentage)
    end
    
    GameEvents.BuildFinished.Add(MineDiscovery)
     
  2. LastSword

    LastSword Prince

    Joined:
    Feb 24, 2013
    Messages:
    1,010
    It's because there was no resource there while the build was finished. It seems like game is updating resources only on build/destroy.

    Anyway, one of the possible solutions should be to recreate mine.
    1) Set Improvement pillaged and repair it.
    2) Remove improvement, add resource, add improvement.
    3) Etc.

    If this is not working, you may try:
    -remove improvement
    -add resource
    -set build to almost done, so worker would finish it in next turn (it would make it +1 turn buildtime in exchange of resource)

    I think that you can also iterate through all Cities owned by Player in order to check the distance.
     
  3. sukritact

    sukritact Artist and Modder

    Joined:
    Sep 21, 2010
    Messages:
    2,881
    Location:
    Bangkok
    Yes; that seems to be it. Thank you!
     
  4. whoward69

    whoward69 DLL Minion

    Joined:
    May 30, 2011
    Messages:
    8,518
    Location:
    Near Portsmouth, UK
    If you change the resource on an owned tile, while the visual changes the connected resource does not. Basically you have to remove the tile from being worked by the city (if necessary), set the owner of the tile to nobody, change the resource, change the ownership back, set the tile being worked (if necessary) and then add the improvement.

    Look in IGE for the actual code to do this.
     

Share This Page