-- Generate a Minable resource on nearby tile when Microbial Mine is Built
-- Options Are Copper, Gold, Sillica, and Minerals
local PlotBonus = {};
local PlotBonusInfo = GameInfo.PlotBonuses["PLOT_BONUS_SPAWN_BASIC_RESOURCE"];
local PossibleResourceWeights =
{
{ Resource = GameInfo.Resources["RESOURCE_SILICA"].ID, Weight = 1 },
{ Resource = GameInfo.Resources["RESOURCE_GOLD"].ID, Weight = 1 },
{ Resource = GameInfo.Resources["RESOURCE_COPPER"].ID, Weight = 1 },
{ Resource = GameInfo.Resources["RESOURCE_MINERALS"].ID, Weight = 1 },
};
function PlotBonus.FindValidResources(player, plot)
local validResources = {};
local team = Teams[player:GetTeam()];
for i,t in pairs(PossibleResourceWeights) do
local chance = t.Weight;
local resourceID = t.Resource;
local resourceInfo = GameInfo.Resources[resourceID];
local revealed = false;
if (resourceInfo.TechReveal == nil) then
revealed = true;
else
local revealTechInfo = GameInfo.Technologies[resourceInfo.TechReveal];
if (revealTechInfo == nil or team:IsHasTech(revealTechInfo.ID)) then
revealed = true;
end
end
if (revealed) then
local ignoreLatitude = false;
if (plot:CanHaveResource(resourceID, ignoreLatitude)) then
if (chance > 0) then
for i = 1, chance do
table.insert(validResources, resourceID);
end
end
end
end
end
return validResources;
end
function PlotBonus.IsValidForPlayerAtPlot(player, plot)
-- Cannot already have a resource
if (plot:HasResource()) then
return false;
end
-- This plot must be able to have a resource we are considering
local validResources = PlotBonus.FindValidResources(player, plot);
if (#validResources == 0) then
return false;
end
return true;
end
function buildOre(pPlayer, pCity)
local AddedTest = 0;
while AddedTest == 0 do
pCityPlot = Game.Rand(#pCity:GetNumCityPlots()-1, "Choosing a plot to spawn resource on") +1; --Grab a random plot in city range
local pSpecificPlot = pCity:GetCityIndexPlot(pCityPlot); -- Grab Plot's location
if PlotBonus.IsValidForPlayerAtPlot(player, pSpecificPlot) == true then-- Make sure plot can have resource added
local resourceDeck = PlotBonus.FindValidResources(player, pSpecificPlot); -- Grab list of addable resources
if (#resourceDeck > 0) then
-- Choose a resource
local roll = Game.Rand(#resourceDeck, "Choosing which resource to spawn from plot bonus") + 1;
local resourceID = resourceDeck[roll];
local resourceInfo = GameInfo.Resources[resourceID];
-- Spawn it
pSpecificPlot:SetResourceType(resourceID, 1);
-- Notify the player
local summary = Locale.ConvertTextKey(PlotBonusInfo.Description, resourceInfo.IconString, resourceInfo.Description);
local message = Locale.ConvertTextKey(PlotBonusInfo.Help, resourceInfo.IconString, resourceInfo.Description);
ShowMessage(pPlayer, pSpecificPlot, summary, message);
local AddedTest=1;
end
end
end
end
function PutOreSomewhere(player, city, building)
print(building)
print(GameInfoBuildingTypes.BUILDING_MICROIAL_MINE)
if building == GameInfoBuildingTypes.BUILDING_MICROBIAL_MINE then
local pPlayer = Players[player]
local pCity = pPlayer:GetCityByID(city)
if buildOre(pPlayer, pCity) == false then
print("No place to place a Mineable Resource.")
end
end
end
GameEvents.CityConstructed.Add(PutOreSomewhere)