Lua Question About Parameters and Arguments

Genghis.Khan

Person
Joined
Jun 9, 2012
Messages
934
Location
Somewhere
Basically is it possible to do something like this:

Code:
function GetOwnerLocalUnitsPower[SIZE="3"]([B]plot[/B])[/SIZE]
	local buildingID = GameInfo.Buildings["BUILDING_COLONY_FAKE"].ID;
	local pPlayer = Players[iPlayer];
	for pCity in pPlayer:Cities() do
	local pCenterPlot = pCity:Plot()
	if pCenterPlot = plot then
		if pCity:IsHasBuilding(buildingID) then
			local iCityName = pCity:GetName()
			local m_CityNames = {}
			local iCityOwner = pCity:GetOwner()
			local NE = DirectionTypes.DIRECTION_NORTHEAST
			local NE = DirectionTypes.DIRECTION_NORTHEAST
			local E = DirectionTypes.DIRECTION_EAST
			local SE = DirectionTypes.DIRECTION_SOUTHEAST
			local SW = DirectionTypes.DIRECTION_SOUTHWEST
			local W = DirectionTypes.DIRECTION_WEST
			local NW = DirectionTypes.DIRECTION_NORTHWEST	
			local NEPlot = Map.PlotDirection(pCenterPlot:GetX(), pCenterPlot:GetY(), NE);
			local EPlot = Map.PlotDirection(pCenterPlot:GetX(), pCenterPlot:GetY(), E);
			local SEPlot = Map.PlotDirection(pCenterPlot:GetX(), pCenterPlot:GetY(), SE);
			local SWPlot = Map.PlotDirection(pCenterPlot:GetX(), pCenterPlot:GetY(), SW);
			local WPlot = Map.PlotDirection(pCenterPlot:GetX(), pCenterPlot:GetY(), W);
			local NWPlot = Map.PlotDirection(pCenterPlot:GetX(), pCenterPlot:GetY(), NW);						
			local NWNWPlot = Map.PlotDirection(NWPlot:GetX(), NWPlot:GetY(), NW);
			local NWNEPlot = Map.PlotDirection(NWPlot:GetX(), NWPlot:GetY(), NE);
			local NWWPlot = Map.PlotDirection(NWPlot:GetX(), NWPlot:GetY(), W);	
			local NENEPlot = Map.PlotDirection(NEPlot:GetX(), NEPlot:GetY(), NE);
			local NENWPlot = Map.PlotDirection(NEPlot:GetX(), NEPlot:GetY(), NW);
			local NEEPlot = Map.PlotDirection(NEPlot:GetX(), NEPlot:GetY(), E);	
			local EEPlot = Map.PlotDirection(EPlot:GetX(), EPlot:GetY(), E);
			local WWPlot = Map.PlotDirection(WPlot:GetX(), WPlot:GetY(), W);
			local SWSWPlot = Map.PlotDirection(SWPlot:GetX(), SWPlot:GetY(), SW);
			local SWSEPlot = Map.PlotDirection(SWPlot:GetX(), SWPlot:GetY(), SE);
			local SWWPlot = Map.PlotDirection(SWPlot:GetX(), SWPlot:GetY(), W);	
			local SESEPlot = Map.PlotDirection(SEPlot:GetX(), SEPlot:GetY(), SE);
			local SESWPlot = Map.PlotDirection(SEPlot:GetX(), SEPlot:GetY(), SW);
			local SEEPlot = Map.PlotDirection(SEPlot:GetX(), SEPlot:GetY(), E);	
			local iOwnerLocalUnitPower = (pCenterPlot:GetUnitPower(iCityOwner) + NEPlot:GetUnitPower(iCityOwner) + EPlot:GetUnitPower(iCityOwner) + SEPlot:GetUnitPower(iCityOwner) + NWPlot:GetUnitPower(iCityOwner) + WPlot:GetUnitPower(iCityOwner) + SWPlot:GetUnitPower(iCityOwner) + NWWPlot:GetUnitPower(iCityOwner) + NWNWPlot:GetUnitPower(iCityOwner) + NWNEPlot:GetUnitPower(iCityOwner) + NEEPlot:GetUnitPower(iCityOwner) + NENEPlot:GetUnitPower(iCityOwner) + NENWPlot:GetUnitPower(iCityOwner) + SEEPlot:GetUnitPower(iCityOwner) + SESEPlot:GetUnitPower(iCityOwner) + SESWPlot:GetUnitPower(iCityOwner) + SWWPlot:GetUnitPower(iCityOwner) + SWSWPlot:GetUnitPower(iCityOwner) + SWSEPlot:GetUnitPower(iCityOwner) + EEPlot:GetUnitPower(iCityOwner) +  WWPlot:GetUnitPower(iCityOwner))       
			print("Player units have" ..iOwnerLocalUnitsPower.. "strenght in the city of" ..CityName.. "!")
			return íOwnerLocalUnitsPower
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add( GetOwnerLocalUnitsPower );					
				
function RevoltColony(iPlayer) 
	for plotLoop = 0, Map.GetNumPlots() - 1, 1 do
		local improvementType = plot:GetImprovementType()
			if ( improvementType == GameInfoTypes["IMPROVEMENT_COLONY"] ) then
				local iColonyPlotX = pPlot:GetX()
				local iColonyPlotY = pPlot:GetY()
				local pColonyPlot = PlotAt:(iColonyPlotX, iColonyPlotY)
				local iImprovementOwner = pColonyPlot:GetOwner()
				local iEraModifier = GetEraModifiersForRevolt() 
				local pColonyCity = City:At(iColonyPlotX, iColonyPlotY)
				local iHandicapRevoltModifier = (2 * Game:GetHandicapType())   -- Deity = 14; Prince = 8
				local iNumColonies = PlayerGetNumColonies()
				local iCityName = pColonyCity:GetName()
				local iNumColoniesModifier = (iNumColonies + 2) * iNumColonies) -- 3*1 = 3; 4*2= 8; 5*3= 15; 6*4= 24
				--local iSpeedModifier = GetSpeedModifierForRevolt()
				local iColonyHappinessModifier = GetColonyHappiness()
				local iPublicOrderPoints = CalculatePublicOrderForRevolt(pColonyCity)
				print("PublicOrder is" ..iPublicOrderPoints.. "for the city of" ..iCityName .. "!")
				[B][SIZE="3"]local iOwnerLocalUnitsPower = GetLocalUnitsPower(pColonyPlot) [/SIZE][/B]-- test this
				print("iOwnerLocalUnitsPower is" ..iOwnerLocalUnitsPower.. "strength")
				local iCityID = pColonyCity:GetID()
				local iOurUnitsPower = GetRevoltedUnitsPower()
				local iOurUnitsPowerConverted = (iOurUnitsPower * 1.75) 
				local iNumRevoltsModifier = GetNumColonyRevolts() * 25 --2nd revolt required + 25 points
				local iRevoltCounter = ((iHandicapRevoltModifier + iEraModifier + iNumColoniesModifier + iColonyHappinessModifier - iPublicOrderForRevolt - iNumRevoltsModifier) 
				if (math.random(0, 55) + iRevoltCounter) == 100 and not iOwnerLocalUnitsPower > iOurUnitsPowerConverted then --and not iRevoltCounter < 45 then
					if not ColonyIsRevolted() = 1 then
					SpawnRevoltedUnits()
					iImprovementOwner:AddNotification(NotificationTypes.NOTIFICATION_WONDER_COMPLETED_ACTIVE_PLAYER, .. iCityName .. ' has revolted! Rebel units will spawn near the revolted Colony and attemp to reclaim those lands! ') 
				end
			end
		end
	end
end
GameEvents.PlayerDoTurn.Add( RevoltColony );


that is call a function to do something in a specific object (city/plot etc?) and is this correct?
 
Could you provide more detail as to what you're trying to do and need help with? It looks like you're trying to do is have a check that happens each turn for each city where something happens based on nearby improvements, military forces and a bunch of other factors.
 
Well, I believe you can iterate through City's plots by index and check MapDistance to be less then 3.

Example:
Code:
for i = 0, nCity:GetNumCityPlots() -1 do
		if nCity:GetCityIndexPlot(i) ~= nil then
			local iPlot = nCity:GetCityIndexPlot(i);
			if Map.PlotDistance(iPlot:GetX(), iPlot:GetY(), nCity:GetX(), nCity:GetY()) < 3 then

However, I don't truely understand the question.
 
sorry. posted to much code, and the relevant part is in the middle.

My question is if it is correct to call a function like this

Code:
local iColonyHappinessModifier = GetColonyHappiness(pColonyCity)

(of course colony city is defined above in the code)

using a parameter for a city, for example:

Code:
function GetColonyHappiness([B]city[/B]) 
	local buildingID = GameInfo.Buildings["BUILDING_COLONY_FAKE"].ID;
	local pPlayer = Players[iPlayer];
		if [B]city[/B]:IsHasBuilding(buildingID) then
			local iBaseUnhappiness = Game:GetHandicapType()
			local iColonyHappiness = city:GetLocalHappiness()
			local iColonyHappinessReturn = 0
			if iColonyHappiness >= 0 then
				iColonyHappinessReturn = iColonyHappiness - iBaseUnhappiness
				return iColonyHappinessReturn
			elseif iColonyHappiness < 0 then
				iColonyHapinessReturn = iColonyHappiness + iBaseUnhapiness
				return iColonyHappinessReturn
			end
		else
			return false
		end
	--end
end
GameEvents.PlayerDoTurn.Add( GetColonyHappiness );

that is, using the City argument/parameter/whatever and calling the function to do it with a specific city (dumb question, badly explained I know). Am I doing this the correct way?
 
It is "correct" syntax to write:

Code:
local [I]return_value[/I] = [I]function_name[/I]([I]input[/I])

And a city object can be used as input. But why are you adding "GetColonyHappiness" to PlayerDoTurn? I don't yet know what you're trying to do but it sounds like "GetColonyHappiness" should return an int that another function would use to determines if the city revolts.
 
yes, but I would need to check it every turn to update the value, because the Colony only revolts when:

Code:
(math.random(0, 55) + iRevoltCounter) == 100 and not iOwnerLocalUnitsPower > iOurUnitsPowerConverted then

and this is a value checked every turn

TLDR: It has a random component so it needs to be checked every turn
 
Back
Top Bottom