Variable for city resistance for a certain civ

AW Arcaeca

Deus Vult
Joined
Mar 10, 2013
Messages
3,019
Location
Operation Padlock ground zero
I'm trying to make a new civ, which for simplicity I'll just say are the Berbers (which is true) and their UA includes only having half the normal resistance turns for capturing a city, but other civs that capture a Berber city deal with twice the regular resistance.

I'm no Lua programmer, but would this code work, based on what I found in lines 704 - 710 of CityView.lua?

Spoiler :
Code:
if (activePlayer:civilizationBerber()) then
	if (pCity:IsResistance()) then
			Controls.ResistanceIcon:SetHide(false);
			Controls.ResistanceIcon:LocalizeAndSetToolTip("TXT_KEY_CITY_RESISTANCE", pCity:GetResistanceTurns(pCity/2));
		else
			Controls.ResistanceIcon:SetHide(true);
		end

This is just guessing here, and assuming pCity represents the population. And how would you make all other civs get double resistance?
 
pCity is a table that represents the city object, so you can call functions on it. Most likely, changing this line will only change the number displayed on the UI, not the actual number of turns the city will spend in resistance. (Actually, I'm fairly sure of this, because you're just changing the string, nothing in the actual gameplay)

Just to demonstrate, if you wanted to display that lower number:

Code:
if (activePlayer:civilizationBerber()) then
	if (pCity:IsResistance()) then
			Controls.ResistanceIcon:SetHide(false);
			Controls.ResistanceIcon:LocalizeAndSetToolTip("TXT_KEY_CITY_RESISTANCE", pCity:GetResistanceTurns()/2);
		else
			Controls.ResistanceIcon:SetHide(true);
		end

But again, that will only change the UI, not the actual effects.
 
Code:
pCity:ChangeResistanceTurns(-math.floor(pCity:GetResistanceTurns()*0.5))
for half resistance


Code:
pCity:ChangeResistanceTurns(pCity:GetResistanceTurns())
for double resistance


in a function added to GameEvents.CityCaptureComplete
 
Code:
pCity:ChangeResistanceTurns(-math.floor(pCity:GetResistanceTurns()*0.5))
for half resistance


Code:
pCity:ChangeResistanceTurns(pCity:GetResistanceTurns())
for double resistance


in a function added to GameEvents.CityCaptureComplete

well, thanks for the coding, but where in there do I add the variables for being Berber and capturing a city, and where also do I add the variable for a Berber city being captured? And more importantly (maybe) what would the variable be?

And also, where is GameEvents.CityCaptureComplete?
 
Try this:

Code:
function ResistanceChanges(iOldOwner, bIsCapital, iCityX, iCityY, iNewOwner, iPop, bConquest)
	local pNewOwner = Players[iNewOwner];
	if pNewOwner ~= pOldOwner then 
	local pCity = Map.GetPlot(iCityX, iCityY):GetPlotCity();
		if(pNewOwner:GetCivilizationType() == GameInfoTypes["CIVILIZATION_[B]MY_CIV[/B]"]) then
			print("The berbers have captured a foreign city")
			pCity:ChangeResistanceTurns(-math.floor(pCity:GetResistanceTurns()*0.5))
		elseif pCity:GetOriginalOwner() == GameInfoTypes["CIVILIZATION_[B]MY_CIV[/B]"]) then
			print("A foreign civ has captured a Berber city")
			pCity:ChangeResistanceTurns(pCity:GetResistanceTurns())
		end
	end
end

GameEvents.CityCaptureComplete.Add(ResistanceChanges)
Replace MY_CIV with the name of your Civ.
 
So does that alone make Berber resistance turns change, or do I need to add something to the typical trait format:
Spoiler :
Code:
<GameData>
	<Traits>
		<Row>
			<Type>TRAIT_YOUR_CIV_TRAIT</Type>
			<Description>TXT_KEY_TRAIT_YOUR_CIV_TRAIT</Description>
			<ShortDescription>TXT_KEY_TRAIT_YOUR_CIV_TRAIT_SHORT</ShortDescription>
		</Row>
	</Traits>
</GameData>

Like maybe after <ShortDescription> add

Code:
<BerberResistChanges>true</BerberResistChanges>
But then I believe you would need to add that to the table using SQL, like
Code:
ALTER TABLE Traits 
ADD COLUMN BerberResistChanges boolean default 0;
But then I guess you would need to define what exactly the BerberResistChanges does...
Does that all sound about right?
 
You can add tags for it in XML or SQL, but that'd require extra coding to connect the Lua to it and it's not really necessary. It'd make it more modular, but that's up to you.
 
Terrible sorry to resurrect a thread from some 3 months ago, but it seems that the coding doesn't really work...

Since I never actually got around to testing this lua for my mod, I set up a controlled experiment in WorldBuilder with the intention of the enemy capturing a Berber city and the Berbers to capture an enemy city, to compare the resistance turns.

Both cities start out w/ 10 population, when captured are taken down to 5 - shouldn't that make 2 (or 3) resistance turns for the Berbers and 10 for the enemy? But they both get 5 turns... :confused:
No clues from the logs, no errors from ModBuddy, InGameUIAddin entry, VFS=false...

See for yourself:
 

Attachments

Code:
function ResistanceChanges(iOldOwner, bIsCapital, iCityX, iCityY, iNewOwner, iPop, bConquest)
  local pNewOwner = Players[iNewOwner];
  [COLOR="Red"]-- pNewOwner is an object, even if pOldOwner is correctly initialised
  -- you should not compare objects
  -- as there is no guarantee that Players[iOldOwner] == Players[iOldOwner],
  -- depending on how the objects are allocated, you should
  -- do iOldOwner ~= iNewOwner (which will actually always be true,
  -- as you can never capture a city from yourself)[/COLOR]
  if pNewOwner ~= pOldOwner then [COLOR="red"]-- pOldOwner is never declared/initialised so is just ni[/COLOR]l
  local pCity = Map.GetPlot(iCityX, iCityY):GetPlotCity();
    if(pNewOwner:GetCivilizationType() == GameInfoTypes["CIVILIZATION_BERBER"]) then
      print("The berbers have captured a foreign city")
      pCity:ChangeResistanceTurns(-math.floor(pCity:GetResistanceTurns()*0.5))
      [COLOR="red"]-- GetOriginalOwner() returns a player id, not a Civilization id, so the condition is unlikely to ever be true[/COLOR]
   [COLOR="red"] -- Missing an opening bracket ( on the next line, so the code will never run (due to the syntax error) - THERE WILL BE AN ERROR IN LUA.LOG[/COLOR]
    elseif pCity:GetOriginalOwner() == GameInfoTypes["CIVILIZATION_BERBER"]) then
      print("A foreign civ has captured a Berber city")
      pCity:ChangeResistanceTurns(pCity:GetResistanceTurns())
    end
  end
end
 
So is this code marginally closer then?
Code:
function BerberResistanceChanges(iOldOwner, bIsCapital, iCityX, iCityY, iNewOwner, iPop, bConquest)
	if iOldOwner ~= iNewOwner then
		local pCity = Map.GetPlot(iCityX, iCityY):GetPlotCity();
		-- Berber captures enemy city
		if (iNewOwner:GetCivilizationType() == GameInfoTypes.CIVILIZATION_BERBER) then
			print("The Berbers have captured a foreign city!")
			pCity:ChangeResistanceTurns(-math.floor(pCity:GetResistanceTurns()*0.5))
		-- Berber city captured
		elseif pCity:GetOriginalOwner() = Players[playerID] and (Players[playerID]:GetCivilizationType() == GameInfoTypes.CIVILIZATION_BERBER) then
			[B][COLOR="Blue"]-- since apparently City.GetOriginalOwner returns a player ID, as you said,
			-- can we link the returned ID with the civilization?
			-- Or is it better to say
			-- elseif (iOldOwner:GetCivilizationType() == GameInfoTypes.CIVILIZATION_BERBER) then
			-- ?[/COLOR][/B]
			print ("A foreign civ has captured a Berber city!")
			pCity:ChangeResistanceTurns(pCity:GetResistanceTurns())
			[B][COLOR="Blue"]-- but doesn't there need to be a function to multiply pCityGetResistanceTurns()
			-- by 2 here?
			-- like maybe pCity:GetResistanceTurns(-math.floor(pCity:GetResistanceTurns()*2))
			-- ?[/COLOR][/B]
		end
	end
end
GameEvents.CityCaptureComplete.Add(BerberResistanceChanges)
 
Back
Top Bottom