Hi,
I'm trying to make an improvement consume coal when it is build to produce synthetic oil.
The oil production is working great and the oil is disconnected from the empire resource if the improvement is lost or pillaged.
Coal is correctly disconnected when the improvement is working.
But I don't get the coal resource back when the improvement is pillaged or destroyed and I don't see what's wrong with the code :
and last question : is it possible to bind the ability to build the improvement to a resource's supply?
In this case the improvement should be buildable only if the player has 6 or more free coal units.
Thanks
I'm trying to make an improvement consume coal when it is build to produce synthetic oil.
The oil production is working great and the oil is disconnected from the empire resource if the improvement is lost or pillaged.
Coal is correctly disconnected when the improvement is working.
But I don't get the coal resource back when the improvement is pillaged or destroyed and I don't see what's wrong with the code :
Code:
print("--- SyntheticPetrolPlant ---");
include( "SaveUtils" ); MY_MOD_NAME = "SPP_CoalControl";
updateSaveData()
local aSyntheticPetrolPlant = {};
local mapMaxX, mapMaxY = Map.GetGridSize();
function onImprovementCreated(iHexX, iHexY)
local pPlot = Map.GetPlot(ToGridFromHex(iHexX, iHexY));
if (pPlot~= nil) then
local improvementType = pPlot:GetImprovementType();
if(improvementType == GameInfoTypes.IMPROVEMENT_SYNTH_PETROL_PLANT) then
local plotID = pPlot:GetX() + pPlot:GetY() * mapMaxX;
local playerID = pPlot:GetOwner();
local isPillaged = pPlot:IsImprovementPillaged();
if(isPillaged) then
if( aSyntheticPetrolPlant[plotID]~=nil and aSyntheticPetrolPlant[plotID].PlayerID ) then
--need to give back Coal resource
ReConnectCoal( plotID, playerID );
end
print("SyntheticPetrolPlant Pillaged at plot ID:", plotID);
else
if(aSyntheticPetrolPlant[plotID]==nil) then
print("SyntheticPetrolPlant Built or Repaired", plotID);
DisconnectCoal( plotID, playerID );
end
end
end
end
end
Events.SerialEventImprovementCreated.Add(onImprovementCreated)
function onImprovementDestroyed(iHexX, iHexY)
local plotX, plotY = ToGridFromHex(iHexX, iHexY);
local plotID = plotX + plotY * mapMaxX;
if(aSyntheticPetrolPlant[plotID]) then
print("onImprovementDestroyed: SyntheticPetrolPlantDestroyed");
ReConnectCoal( plotID, aSyntheticPetrolPlant[plotID].PlayerID );
end
end
Events.SerialEventImprovementDestroyed.Add(onImprovementDestroyed)
function onPlotOwnershipChange(iHexX, iHexY)
local plotX, plotY = ToGridFromHex(iHexX, iHexY);
local plotID = plotX + plotY * mapMaxX;
if(aSyntheticPetrolPlant[plotID]) then
print("onPlotOwnershipChange: SyntheticPetrolPlant Ownership has changed");
ReConnectCoal( plotID, aSyntheticPetrolPlant[plotID].PlayerID );
local pPlot = Map.GetPlot(ToGridFromHex(iHexX, iHexY));
if(pPlot) then
local newOwner = pPlot:GetOwner();
DisconnectCoal( plotID, newOwner );
else
print("ERROR in SyntheticPetrolPlantControl: Map.GetPlot returned nil");
end
end
end
Events.SerialEventHexCultureChanged.Add(onPlotOwnershipChange)
function ReConnectCoal( plotID, playerID )
local pPlayer = Players[playerID];
local iResourceID = GameInfoTypes.RESOURCE_COAL;
local iResourceNum = 6;
if(pPlayer) then
pPlayer:ChangeNumResourceTotal(iResourceID, iResourceNum);
end
aSyntheticPetrolPlant[plotID] = {};
aSyntheticPetrolPlant[plotID].PlayerID = playerID;
updateSaveData(plotID);
print("Reconnected resource ", iResourceID, " for player ", playerID, "amount:", iResourceNum);
end
function DisconnectCoal( plotID, playerID )
local pPlayer = Players[playerID];
local iResourceID = GameInfoTypes.RESOURCE_COAL;
local iResourceNum = 6;
if(pPlayer) then
pPlayer:ChangeNumResourceTotal(iResourceID, -iResourceNum);
end
aSyntheticPetrolPlant[plotID] = nil;
updateSaveData(plotID);
print("Disconnected resource ", iResourceID, " for player ", playerID, "amount:", iResourceNum);
end
function updateSaveData(plotID)
local pPlayer = Players[Game.GetActivePlayer()];
save( pPlayer, plotID, aSyntheticPetrolPlant[plotID] );
end
function loadSaveData()
local pPlayer = Players[Game.GetActivePlayer()];
local wholeTable = load( pPlayer );
for plotID, v in pairs(wholeTable) do
print("plotID:",plotID);
for ii,kk in pairs(v) do
print(ii,kk);
end
aSyntheticPetrolPlant[plotID] = v;
end
end
loadSaveData(); --call on startup
and last question : is it possible to bind the ability to build the improvement to a resource's supply?
In this case the improvement should be buildable only if the player has 6 or more free coal units.
Thanks