[SDK] Improved Air Interception

Afforess

The White Wizard
Joined
Jul 31, 2007
Messages
12,239
Location
Austin, Texas
Improved Air interception rewrites the standard BTS air interception formula. Normally, the air interception chance of a given tile is just the best unit's interception chance. So, if you have 5 fighters with 20% interception each, your interception chance is 20%. This is the way BTS works.

Improved Air Interception changes the formula to be dependent on the number of units present and the interception of said units. In the same example as above, with 5 units that have 20% interception chance each, the total interception chance would be 1.0 - 0.80^5. (For those without a calculator, that is ~ 67.232%)

As aircraft become more damaged, their air interception abilities drop, as a factor of their health. A unit with 20% interception, but only 50% health would have an effective interception chance of 10%; and this is correctly reflected in the above formula.

Improved Air Interception does not alter the way combat works, nor the actual results of combat, it just changes the chances of interception.

The changes are too small to be worth a full download. If you do include Improved Air Interception in your mod, please give me credit. ;)
Code:
Spoiler :

CvUnit.cpp:
Modified:
Code:
bool CvUnit::interceptTest(const CvPlot* pPlot)
{
	if (GC.getGameINLINE().getSorenRandNum(100, "Evasion Rand") >= evasionProbability())
	{
		CvUnit* pInterceptor = bestInterceptor(pPlot);
		if (pInterceptor != NULL)
		{
/************************************************************************************************/
/* Afforess	                  Start		 03/6/10                                                */
/*                                                                                              */
/*  Better Air Interception                                                                     */
/************************************************************************************************/
			int iInterceptionOdds = pInterceptor->currInterceptionProbability();
			if (GC.getDefineINT("BETTER_AIR_INTERCEPTION"))
				iInterceptionOdds = interceptionChance(pPlot);
/************************************************************************************************/
/* Afforess	                     END                                                            */
/************************************************************************************************/	
			if (GC.getGameINLINE().getSorenRandNum(100, "Intercept Rand (Air)") < iInterceptionOdds)
			{
				fightInterceptor(pPlot, false);

				return true;
			}
		}
	}


	return false;
}

Add this function to the end of CvUnit, it is a new addition.

Code:
int CvUnit::interceptionChance(const CvPlot* pPlot) const
{
	int iValue;
	int iLoop;
	int iI;
	int iNoInterceptionChanceTimes100 = 10000;
	CvUnit* pLoopUnit;

	for (iI = 0; iI < MAX_PLAYERS; iI++)
	{
		if (GET_PLAYER((PlayerTypes)iI).isAlive())
		{
			if (isEnemy(GET_PLAYER((PlayerTypes)iI).getTeam()) && !isInvisible(GET_PLAYER((PlayerTypes)iI).getTeam(), false, false))
			{
				for(pLoopUnit = GET_PLAYER((PlayerTypes)iI).firstUnit(&iLoop); pLoopUnit != NULL; pLoopUnit = GET_PLAYER((PlayerTypes)iI).nextUnit(&iLoop))
				{
					if (pLoopUnit->canAirDefend())
					{
						if (!pLoopUnit->isMadeInterception())
						{
							if ((pLoopUnit->getDomainType() != DOMAIN_AIR) || !(pLoopUnit->hasMoved()))
							{
								if ((pLoopUnit->getDomainType() != DOMAIN_AIR) || (pLoopUnit->getGroup()->getActivityType() == ACTIVITY_INTERCEPT))
								{
									if (plotDistance(pLoopUnit->getX_INLINE(), pLoopUnit->getY_INLINE(), pPlot->getX_INLINE(), pPlot->getY_INLINE()) <= pLoopUnit->airRange())
									{
										iValue = pLoopUnit->currInterceptionProbability();
										if (iValue > 0 && iValue < 100)
										{
											iNoInterceptionChanceTimes100 *= (100 - iValue);
											iNoInterceptionChanceTimes100 /= 100;
										}
										else if (iValue >= 100)
											return 100;
									}
								}
							}
						}
					}
				}
			}
		}
	}

	return 100 - (iNoInterceptionChanceTimes100 / 100);
}

CvUnit.h:
(Functionally, this can go wherever you want in CvUnit.h)
Code:
int interceptionChance(const CvPlot* pPlot) const;

And to the end of GlobalAltDefines.XML:

Code:
<!--Afforess:
	Enables Air Interception chances being Multiplicitive. 3 Planes with 20% intercept chance would intercept incoming planes at .80^3 of the time. The Firaxis method is to intercept at highest intercept chance on the tile.-->
	<Define>
		<DefineName>BETTER_AIR_INTERCEPTION</DefineName>
		<iDefineIntVal>1</iDefineIntVal>
	</Define>

There are no known bugs, it has been tested and works, and is provided "As Is" with no further support from me. For those who want to try it out first hand w/o compiling a new DLL are welcome to try out my Rise of Mankind Expansion Project, A New Dawn.
 
Nice, will add it to my mod. Thank you. :goodjob:
BTW, does this change all intercepting or just air units'?

All interception, but if you wanted to restrict it, you could add an extra if check in the pLoopUnit.
 
Nice! :) I've thought about doing this for a while, but I didn't really know how to approach it. One question: Does this work with ground and naval units too? I.e., five SAM Infantry...etc.

EDIT: D'uh, just saw NotSoGoods question! :)
 
Hi, Afforess.

As you are working on an improved air interception I offer you a part of CCV for your project. The Advanced Interception System of CCV makes older systems like flak or fighters useless against modern air units like highaltitude bombers or modern supersonic fighters. Just test it and PM me if you want it.

Yours Thomas
 
Just so you know, this is now folded (partly) into the Merged Mod. :) Partly meaning for non-air interceptors only.
 
Is this working correctly? In your file the description says:

Enables Air Interception chances being Multiplicitive. 3 Planes with 20% intercept chance would intercept incoming planes at .80^3 of the time.(...)

0.80^3 would be 0.512 so a chance of nearly 50%?

what about 3 planes with 50% intercept chance? this would be 0.125 = 12.5% Chance???
 
If the mod works as stated, the calculation is correct.

3 planes with 20%:
80% chance to miss for first plane.
Out of these 80% chance, still got 2 planes which can intercept.
So, 80% chance out of the first 80% to miss both planes = 0.8^2 = 64% chance to evade both planes.
Same for last, thus, 51.2% chance to evade all 3

Edit: I think he meant evade not intercept.
Chance to intercept is 1 - (0.8^3)
 
Top Bottom