Militia when declared war upon

AW Arcaeca

Deus Vult
Joined
Mar 10, 2013
Messages
2,984
Location
Operation Padlock ground zero
I'm thinking of expanding the UA for my Swiss civ. Anyone who's aware of it should know it's fairly bland at the moment. :p

So I was thinking something along the lines of
When another major civilization declares war on you, receive 1 melee or gunpowder unit based on your current technologies in all cities.

I may have to reword it a little, but basically that means 1 warrior per city if you're in the ancient era when someone declares war on you, 1 spearman in the classical until you can get swordsmen (will still require iron), then pikemen once they're available, then longswordsmen, then musketman... and continuing down the melee > gunpowder upgrade path

Except I don't know how to detect when you've been declared war on. This is what I have so far:
Code:
function Swiss_WarMilitia(pTeam, mTeam, true)
	local pPlayer = Players[playerID]
	local pTeam = pPlayer:GetTeam()
	if (pPlayer:IsAlive()) then
		if (pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_SWITZERLAND) then
			for i, mTeam in pairs(Teams) do
				if (mTeam ~= pTeam) then
(*Meant to have the Events.WarStateChange hook, in case you're wondering about the parameters)
And then I hit a figurative brick wall of lua. :(

Of course the player would have to have the appropriate technologies, and I'm trying to decide whether or not the militia should be taken away after the war ends, and whether or not to give them combat penalties to nerf it a little.

Any suggestions on how to code it?
Or on how to buff or nerf this UA addition?
 
Somebody made a mod with the almost exact same effect. You should look for So-and-so's Militia Mod or something like that. That should have good enough code for you to use.
 
That's by FramedArchitect, I believe. I don't have it, because I usually do the declaring, but I think it does exactly what AgressiveWimp wants.

Darn it AW, you got to Switzerland first. I'm going to become a citizen before too long and wanted to make a Swiss civ in honor of that. You even grabbed my UU Reislaufer:mad:.

:joke:
 
Somebody made a mod with the almost exact same effect. You should look for So-and-so's Militia Mod or something like that. That should have good enough code for you to use.
Good deal :thumbsup:
That's by FramedArchitect, I believe. I don't have it, because I usually do the declaring, but I think it does exactly what AgressiveWimp wants.

Darn it AW, you got to Switzerland first. I'm going to become a citizen before too long and wanted to make a Swiss civ in honor of that. You even grabbed my UU Reislaufer:mad:.

:joke:
FramedArchitect, huh? Thanks.
I downloaded the mod and.... :wow: got hit by a huge wall of (at least to me) incomprehensible lua. :crazyeye: Hopefully I'll be able to dig out what coding is activated upon being declared war on...

Also, I didn't even have the first Swiss civ. :p It was uploaded along with my thread on 11/02/2013... quite a while back. Still, there's plenty of great stuff from Switzerland you can use as uniques. The all-important swiss bank, maybe an alpenhorn concert hall?
(If it makes you feel any better, the Reislaufer used to be called the Pontifical Guard - and still has a derivative of that name in the files - until McKurosu, another Swiss modder, suggested I change the name.)
 
I can imagine in a pvp game a swiss player just trading out for people to declare war on him then immediately declaring peace, just so he gets more units (or for free if they're allies against a common foe).

I think it's a great idea for an UA anyway.
 
I can imagine in a pvp game a swiss player just trading out for people to declare war on him then immediately declaring peace, just so he gets more units (or for free if they're allies against a common foe).

I think it's a great idea for an UA anyway.
Which is why I'm thinking the units either need to disband after the war ends or cost double maintenance afterwards or something.
 
I just had a great idea (that I would like to keep for my own Swiss civ thank you very much!) of making Swiss units fight tons more effectively in friendly territory. It would deter attackers very effectively and would be much easier to code. The only thing I don't know is what you'd call that trait...:p
 
I just had a great idea (that I would like to keep for my own Swiss civ thank you very much!) of making Swiss units fight tons more effectively in friendly territory. It would deter attackers very effectively and would be much easier to code. The only thing I don't know is what you'd call that trait...:p
Neutrality, perhaps? That effect currently comprises about half of the UA of my Swiss civ. To quote:
"+25% bonus to units fighting in friendly territory, but -10% penalty for units fighting outside friendly territory. -15% enemy spy effectiveness in all cities."
The espionage clause was only recently added, and the friendly territory clause used to extremely OP (originally it was 75%), and deemed to be too bland:
1) Most extremely ridiculously overpowered thing ever
2) Most tedious UA ever (sorry man but since the beginning of modding this game I have seen literally DOZENS of civs with "unique" ability "receive X percent bonus on friendly territory"

Otherwise mod is OK, but I really recommend changing UA on... well, everything is better than Generic Friendly Territory Bonus :p
But, do you what you want. I used to want to do something like " extra gold per turn equal to the sum of x% of the gold per turn of all your opponents" (swiss banking) but I have no idea how to code that.

Anyway, back on track: This is literally the only line of code I could find that mentions anything about being at war, and it doesn't seem to specify who started the war.
Code:
if Teams[otherPlayer:GetTeam()]:IsAtWar(pPlayer:GetTeam()) then
Which could be a problem since I'm specifically looking for a way to give militia when Switzerland is declared war on, not when they declare war.
 
I downloaded the mod and.... :wow: got hit by a huge wall of (at least to me) incomprehensible lua. :crazyeye: Hopefully I'll be able to dig out what coding is activated upon being declared war on...

Hi, my mod raises militia based on enemy troop movement into hostile territory (GameEvents.UnitSetXY), not declarations of war.

If you want to spawn units based on DoW then you probably run into some issues that require a timer (as my mod does), otherwise you can get some major unit spam. You can use Events.WarStateChanged to detect DoW.

Let me know if I can help unwind my "crazy" code.

best,
FA

edit~
If you need help with this: " extra gold per turn equal to the sum of x% of the gold per turn of all your opponents", I can see how to do this fairly easily, though it would seem to scale oddly with number of opponents.
 
Hi, my mod raises militia based on enemy troop movement into hostile territory (GameEvents.UnitSetXY), not declarations of war.

If you want to spawn units based on DoW then you probably run into some issues that require a timer (as my mod does), otherwise you can get some major unit spam. You can use Events.WarStateChanged to detect DoW.

Let me know if I can help unwind my "crazy" code.

best,
FA

edit~
If you need help with this: " extra gold per turn equal to the sum of x% of the gold per turn of all your opponents", I can see how to do this fairly easily, though it would seem to scale oddly with number of opponents.
Erm, well, I assume it's this part of the code:
Code:
function RaiseMilitia(iPlayer)
	local pPlayer = Players[iPlayer]
	local militiaID = GameInfoTypes[GetMilitiaType(pPlayer)]
	local iHandicapMod = gHandicap
	local pUnit;
	local iCount = 0
	local iX = -1
	local iY = -1

	if bMilitiaHandicap then
		if pPlayer:IsHuman() then
			iHandicapMod = -gHandicap
		end
	end

	for pCity in pPlayer:Cities() do
		if pCity then
			local x, y =  pCity:GetX(), pCity:GetY()
			if bCityMilitia then
				if IsValidMilitiaCity(pCity) then
					if ( pCity:GetPopulation() >= gMilitiaMinPop ) then 
						pUnit = pPlayer:InitUnit(militiaID, x, y)
						pUnit:JumpToNearestValidPlot()
						iCount = iCount + 1
					end
				end
			end
			if pCity:IsCapital() then
				iX, iY = x, y
				pUnit = pPlayer:InitUnit(militiaID, x, y)
				pUnit:JumpToNearestValidPlot()
				iCount = iCount + 1
				for i = 0, pCity:GetNumCityPlots() - 1, 1 do
					local pPlot = pCity:GetCityIndexPlot( i );
					if pPlot and ( pPlot:GetOwner() == iPlayer ) then
						local improvementID = pPlot:GetImprovementType()
						if ( improvementID ~= NO_IMPROVEMENT ) then
							local iSpawnChance = (GetMilitiaSpawnChance(improvementID, iHandicapMod)) or 1
							if ( math.random(1, 100) <= iSpawnChance ) then
								pUnit = pPlayer:InitUnit(militiaID, pPlot:GetX(), pPlot:GetY())
								pUnit:JumpToNearestValidPlot()
								iCount = iCount + 1
							end
						end
					end
				end
			end
		end
	end
	return iCount, iX, iY
end
 
AW, here's another idea for an advantage (can I please keep it?):
Enemy units take 5 damage for every turn spent within your borders, and 10 damage for every turn spent next to a mountain.
That would probably be hugely difficult to code, and probably a bit overpowered, but there's a reason Switzerland never gets attacked in the real world.
 
AW, here's another idea for an advantage (can I please keep it?):
You can, if I can get this original effect to work. :)
That would probably be hugely difficult to code, and probably a bit overpowered, but there's a reason Switzerland never gets attacked in the real world.
...Full-time military, tunnels and bridges rigged with explosive charges, difficulty mobilizing troops past the Alps, and, honestly, a lack of natural resources. I'm assuming your idea is based on the third reason?
 
AW, here's another idea for an advantage (can I please keep it?):
Enemy units take 5 damage for every turn spent within your borders, and 10 damage for every turn spent next to a mountain.
That would probably be hugely difficult to code, and probably a bit overpowered, but there's a reason Switzerland never gets attacked in the real world.

Surprisingly, this is not that difficult to do. I have a very similar trait for my Hive Civilization (part of my Alpha Centauri civs) called Perimeter Defense. If the civ is at war then every city scans its tiles looking for enemies. If it finds any, it reduces their hp by X amount (never actually killing them). It would be easy to add extra damage for units adjacent to particular terrains (i.e. mountains).

Here's the lua code:

Code:
function OnPlayerDoTurn(playerID)
	local player = Players[playerID]
	if player:IsBarbarian() then return end
	if player:IsMinorCiv() then return end
	if not player:IsAlive() then return end

	local traitInfo = GetTraitInfo(player)
	if traitInfo.HappinessPerGarrison > 0 then
		if Teams[player:GetTeam()]:GetAtWarCount(false) > 0 then
			DoPerimeterDefense(playerID)
		end
	end
end
GameEvents.PlayerDoTurn.Add( OnPlayerDoTurn )

function DoPerimeterDefense(playerID)
	local player = Players[playerID]
	local pTeam = Teams[player:GetTeam()]

	local enemies = {}
	for city in player:Cities() do
		for plot in PlotAreaSpiralIterator(city:Plot(), g_PerimeterRadius, SECTOR_SOUTH, true, true, false) do
			local unit = plot:GetUnit(0)
			if unit then
				local ownerID = unit:GetOwner();
				if ownerID ~= playerID then
					if pTeam:IsAtWar(Players[ownerID]:GetTeam()) then
						table.insert(enemies, unit)
					end
				end
			end
		end
	end

	if #enemies > 0 then
		for _,unit in ipairs(enemies) do
			local maxHP = unit:GetMaxHitPoints()
			local damage = unit:GetDamage() + math.floor( maxHP * g_PerimeterDamage )
			if damage >= maxHP then
				damage = maxHP - 1
			end
			unit:SetDamage(damage)
		end
	end
end

This code optimizes the search routine by using whowards' plot iterator tool (available in my mod or on this site). Also this requires a database addition to the Traits file (I called it "HappinessPerGarrison"), which allows you to assign this ability to a civ.
 
Surprisingly, this is not that difficult to do. I have a very similar trait for my Hive Civilization (part of my Alpha Centauri civs) called Perimeter Defense. If the civ is at war then every city scans its tiles looking for enemies. If it finds any, it reduces their hp by X amount (never actually killing them). It would be easy to add extra damage for units adjacent to particular terrains (i.e. mountains).

Here's the lua code:

Code:
function OnPlayerDoTurn(playerID)
	local player = Players[playerID]
	if player:IsBarbarian() then return end
	if player:IsMinorCiv() then return end
	if not player:IsAlive() then return end

	local traitInfo = GetTraitInfo(player)
	if traitInfo.HappinessPerGarrison > 0 then
		if Teams[player:GetTeam()]:GetAtWarCount(false) > 0 then
			DoPerimeterDefense(playerID)
		end
	end
end
GameEvents.PlayerDoTurn.Add( OnPlayerDoTurn )

function DoPerimeterDefense(playerID)
	local player = Players[playerID]
	local pTeam = Teams[player:GetTeam()]

	local enemies = {}
	for city in player:Cities() do
		for plot in PlotAreaSpiralIterator(city:Plot(), g_PerimeterRadius, SECTOR_SOUTH, true, true, false) do
			local unit = plot:GetUnit(0)
			if unit then
				local ownerID = unit:GetOwner();
				if ownerID ~= playerID then
					if pTeam:IsAtWar(Players[ownerID]:GetTeam()) then
						table.insert(enemies, unit)
					end
				end
			end
		end
	end

	if #enemies > 0 then
		for _,unit in ipairs(enemies) do
			local maxHP = unit:GetMaxHitPoints()
			local damage = unit:GetDamage() + math.floor( maxHP * g_PerimeterDamage )
			if damage >= maxHP then
				damage = maxHP - 1
			end
			unit:SetDamage(damage)
		end
	end
end

This code optimizes the search routine by using whowards' plot iterator tool (available in my mod or on this site). Also this requires a database addition to the Traits file (I called it "HappinessPerGarrison"), which allows you to assign this ability to a civ.
So, does this decrease enemy health by 1 each turn?
Code:
damage = maxHP - 1
And where would I add in the requirement of being Swiss? I know it's "if (player:GetCivilizationType() == GameInfoTypes.CIVILIZATION_SWITZERLAND) then", but where do I add it?

Though I would prefer the militia UA, since - anyone from Switzerland here correct me if I'm wrong - but I think all male citizens have to enter the military for a bit then return to their normal lives, so much of the citizen population is trained to use military arms, and Switzerland can raise a very large militia in case of war. Is that correct?
 
Though I would prefer the militia UA, since - anyone from Switzerland here correct me if I'm wrong - but I think all male citizens have to enter the military for a bit then return to their normal lives, so much of the citizen population is trained to use military arms, and Switzerland can raise a very large militia in case of war. Is that correct?

Yes, that is. The Swiss Army is made up of 95% conscripts and 5% professional soldiers. Boot camp goes on for about half a year, then they are discharged and can be called up until age 34 for conscripts and 50 for officers. If you are found unfit to enter the military you have to pay extra taxes for ten years.

Women are not conscripted; they can join voluntarily. To become a professional soldier you must go to boot camp and then to various different schools where you are educated further-then you become part of that 5% (of which 1% is drill sergeants, :lol:).
 
Top Bottom