[Help] Making Civs start near certain Resources

Harkodos

Warlord
Joined
Feb 9, 2016
Messages
197
I am currently trying to program in a unique Trait for a Civilization where they are predetermined to appear near sources of Gems and Pearls, and I thought I would go about this by using The Pridelands civilization as an example. However, I do not believe the Lua I have rigged up is working (perhaps because I do not have the right setting in ModBuddy; I referred to whoward69's list but I did not see anything that was conflicting with what I had). In any case, here is the code I have:

Code:
-- Steven_Luxuries
-- Author: Harkodos
-- Base: Tpangolin, Neirei the Forgiven
--------------------------------------------------------------
local TheCrystalGemsCiv = GameInfoTypes.CIVILIZATION_AMERICA--Civ ID;
LuxuryTab = {}
LuxuryTab[GameInfoTypes.RESOURCE_GEMS] = true
LuxuryTab[GameInfoTypes.RESOURCE_PEARLS] = true

GameEvents.PlayerCityFounded.Add(function(iPlayer, iCityX, iCityY)
        if Players[iPlayer]:GetCivilizationType() == TheCrystalGemsCiv then
                local kplot = Map.GetPlot(iCityX, iCityY);
                local kCity = kplot:GetPlotCity();
                if kCity:IsOriginalMajorCapital() then
                        AddGemsAndPearls(kCity)
                end
        end
end)

function AddGemsandPearls(kCity)
        PlotCount = 0;
        PlotVT = {}
        for i = 1, kCity:GetNumCityPlots() -1 do
                if kCity:GetCityIndexPlot(i) ~= nil then
                        local iPlot = kCity:GetCityIndexPlot(i);
                        if iPlot:GetResourceType(-1) == -1 then
                                if ResourceValid(iPlot) then
                                        PlotCount = PlotCount + 1;
                                        PlotVT[PlotCount] = iPlot;
                                end
                        end
                end
        end
        RandomPlot = nil;
        for i = 1, math.min(PlotCount, 2) do
                OldRandom = RandomPlot;
                RandomPlot = math.random(1, PlotCount);
                while OldRandom == RandomPlot do
                        RandomPlot = math.random(1, PlotCount);
                end
                PlotVT[RandomPlot]:SetResourceType(GetResource(PlotVT[RandomPlot]), 1);
        end
        PlotVT = nil;
end

function GetResource(iPlot)
        ResCount = 0;
        ResVT = {}
        for iRes, bool in pairs(LuxuryTab) do
                if iPlot:CanHaveResource(iRes) then
                        ResCount = ResCount + 1;
                        ResVT[ResCount] = iRes;
                end
        end
        if ResCount > 0 then
                return ResVT[math.random(1, ResCount)];
        else
                print("It cannot be! It's impossibru!")
                return -1;
        end
end

function ResourceValid(iPlot)
        for iRes, bool in pairs(LuxuryTab) do
                if iPlot:CanHaveResource(iRes) then
                        return true;
                end
        end
        return false;
end
I'm assuming I may have removed something important, or that the title of The Prideland's UA is misleading and that using their Lua code for a base was a mistake.

Any thoughts?
 
Check your Lua.log:

Code:
15: attempt to call global 'AddGemsAndPearls' (a nil value)

You named the function AddGemsandPearls - Lua is case sensitive. (Also, note that, contrary to the description of the Pridelands' UA, it doesn't actually make you start near the resources but rather spawns them nearby once the capital is founded.) It also will only place them on tiles that could have them randomly - not much of a problem for the Pridelands given the large variety of resources involved, but for you this'll mean it'll only fire if there's Coast and/or Hills nearby.
 
Also, once the function name issue has been corrected, what happens if there are 0 valid plots for the desired reosurces?
Code:
for i = 1, math.min(PlotCount, 2) do
PlotCount in such a case will still be '0'.
Code:
math.min(0, 2) == 0
a loop structured as
Code:
for i = 1, math.min(0, 2) do
will increment 'i' by '1' every time through the loop, and never get to '0'.
 
Also, I noticed you're using math.random(). That will cause desyncs if someone tries to use your mod in multiplayer - use Game.Rand() instead.
 
I mostly just copy-pasted what was in there, I'm only slowly understanding what everything in the Lua does (I'm mostly aware of how to add Social Policies to Civs now).

Check your Lua.log:

Code:
15: attempt to call global 'AddGemsAndPearls' (a nil value)

You named the function AddGemsandPearls - Lua is case sensitive. (Also, note that, contrary to the description of the Pridelands' UA, it doesn't actually make you start near the resources but rather spawns them nearby once the capital is founded.) It also will only place them on tiles that could have them randomly - not much of a problem for the Pridelands given the large variety of resources involved, but for you this'll mean it'll only fire if there's Coast and/or Hills nearby.

@Klisz
Don't know how I missed that. Thanks for the catch! Also, the Civ that this will be the base for will eventually be set to start near Coastlines as a priority, so at least Pearls will be given a good chance to appear.

@LeeS
So, are you suggesting I replace everything below:
Code:
for i = 1, math.min(PlotCount, 2) do
with:
Code:
math.min(0, 2) == 0
 
@LeeS
So, are you suggesting I replace everything below:
Code:
for i = 1, math.min(PlotCount, 2) do
with:
Code:
math.min(0, 2) == 0
No.

I was saying this was what the code will translate to if we stick in actual numerical values that can result in specific circumstances as a method to determine how the code will react under these specific circumstances. It is a method I employ to check for oddball circumstances that might result from my code as written. I write down what a bit of the code will be in terms of real possible numerical results, and then see what further parts of the code will 'translate to', in order to then determine how the code will react to such real possible numerical results.
 
Hmmm, I see. I am starting to consider adding more values to the list (such as Stone, Salt, maybe Iron, and Coal). The original code had about 6 items in the Luxuries category; I'm curious: will adding more to the code as it is now help fix the solution, or is that not the real problem?
 
Adding more luxury, bonus, or strategic resources to the list would tend to lessen the possibility of a failure to get anything because the start position 'sucked'. Just keep in mind that with strategic resources you also have to tell the game what the resource-amount of the tile is. For example, does the tile give 2 Horses or 4 Horses.
 
Hmmm, interesting. How would one set that up for a strategic resource? The Pridelands had horses defined, but I didn't see anywhere in the code that suggested the amount of Horses would be defined.
It literally looks like this:
Code:
AnimalTab[GameInfoTypes.RESOURCE_HORSE] = true
And that's pretty much all that can be found on that matter.
 
Back
Top Bottom