Lua code not working

Hulfgar

Emperor
Joined
Mar 31, 2008
Messages
1,798
Location
France
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 :

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
 
aSyntheticPetrolPlant[plotID] = {};

aSyntheticPetrolPlant[plotID] = nil;

I may not know a whole lot about Lua but this is what looks like the significant difference between your two functions.
Does the print piece work? For the function that doesn't, that is.
Does it work when you repair the improvement?
Btw:
ReConnectCoal( plotID, playerID );
end
print("SyntheticPetrolPlant Pillaged at plot ID:", plotID);
Vs.
print("SyntheticPetrolPlant Built or Repaired", plotID);
DisconnectCoal( plotID, playerID );
end

Finally, once more with the, 'don't know lua' but does this actually work?
if( aSyntheticPetrolPlant[plotID]~=nil and aSyntheticPetrolPlant[plotID].PlayerID ) then
The first clause will, but what exactly does the second check? Doesn't it just grab an ID?

Anyways, all I got, sorry.
 
Are you sure you don't have the functionality (or parts of it, anyway) of "ReConnectCoal( plotID, playerID )" and "DisconnectCoal( plotID, playerID )" reversed?

In ReConnectCoal( plotID, playerID ) you are saving the plot ID and player ID info into your subsidiary table
Code:
aSyntheticPetrolPlant[plotID] = {};
aSyntheticPetrolPlant[plotID].PlayerID = playerID;
but within the same function you are adding 6 coal back to the empire stocks. If you are adding the coal back to the empire stocks, why are you saving plot information for the improvement?

In DisconnectCoal( plotID, playerID ) you are "nil"-ing the plot ID and player ID info back out of your subsidiary table
Code:
aSyntheticPetrolPlant[plotID] = nil;
but within the same function you are removing 6 coal from to the empire stocks. If you are removing the coal from the empire stocks, is this not the condition wherein you should save the plot information for the improvement?

One general point: SerialEvents are often not reliable. I know the unit created one will sometimes fire twice for every unit created. It also seems to fire when the game is re-loading and every unit is "placed" (and is therefore being created) back onto the map during the saved-game loading process. Perhaps your fundamental problem could be the serial event.
 
Back
Top Bottom