1. We have added the ability to collapse/expand forum categories and widgets on forum home.
    Dismiss Notice
  2. Photobucket has changed its policy concerning hotlinking images and now requires an account with a $399.00 annual fee to allow hotlink. More information is available at: this link.
    Dismiss Notice
  3. All Civ avatars are brought back and available for selection in the Avatar Gallery! There are 945 avatars total.
    Dismiss Notice
  4. To make the site more secure, we have installed SSL certificates and enabled HTTPS for both the main site and forums.
    Dismiss Notice
  5. Civ6 is released! Order now! (Amazon US | Amazon UK | Amazon CA | Amazon DE | Amazon FR)
    Dismiss Notice
  6. Dismiss Notice
  7. Forum account upgrades are available for ad-free browsing.
    Dismiss Notice

[SDK] Improved Air Interception

Discussion in 'Civ4 - Mod Components' started by Afforess, Mar 31, 2010.

  1. Afforess

    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.
     
  2. NotSoGood

    NotSoGood Chieftain

    Joined:
    Jan 25, 2009
    Messages:
    1,077
    Location:
    Finland
    Nice, will add it to my mod. Thank you. :goodjob:
    BTW, does this change all intercepting or just air units'?
     
  3. Afforess

    Afforess The White Wizard

    Joined:
    Jul 31, 2007
    Messages:
    12,239
    Location:
    Austin, Texas
    All interception, but if you wanted to restrict it, you could add an extra if check in the pLoopUnit.
     
  4. Cybah

    Cybah Chieftain

    Joined:
    Jun 22, 2007
    Messages:
    1,479
    is this compatible with BBAI?
     
  5. Afforess

    Afforess The White Wizard

    Joined:
    Jul 31, 2007
    Messages:
    12,239
    Location:
    Austin, Texas
    Yes, it does not conflict with any mods. ;)
     
  6. Ninja2

    Ninja2 Great Engineer

    Joined:
    Nov 17, 2005
    Messages:
    1,142
    Location:
    Denmarkia
    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! :)
     
  7. Cybah

    Cybah Chieftain

    Joined:
    Jun 22, 2007
    Messages:
    1,479
    sure...........
     
  8. keldath

    keldath LivE LonG AnD PrOsPeR

    Joined:
    Dec 20, 2005
    Messages:
    6,134
    Location:
    israel
    Afforess your a sdk machine huh?

    great work. with everything you done so far.
     
  9. Thomas SG

    Thomas SG CCV-designer

    Joined:
    Aug 10, 2007
    Messages:
    1,164
    Location:
    Germany
    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
     
  10. Ahriman

    Ahriman Tyrant

    Joined:
    Jun 8, 2008
    Messages:
    13,266
    Location:
    Washington, DC
    We might integrate this into Dune Wars.

    Great job Afforess.
     
  11. Ninja2

    Ninja2 Great Engineer

    Joined:
    Nov 17, 2005
    Messages:
    1,142
    Location:
    Denmarkia
    Just so you know, this is now folded (partly) into the Merged Mod. :) Partly meaning for non-air interceptors only.
     
  12. Afforess

    Afforess The White Wizard

    Joined:
    Jul 31, 2007
    Messages:
    12,239
    Location:
    Austin, Texas
    Use it however you like. ;)
     
  13. Cybah

    Cybah Chieftain

    Joined:
    Jun 22, 2007
    Messages:
    1,479
    Is this working correctly? In your file the description says:

    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???
     
  14. platyping

    platyping Sleeping Dragon

    Joined:
    Oct 22, 2010
    Messages:
    4,626
    Location:
    Emerald Dreams
    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)
     
  15. Cybah

    Cybah Chieftain

    Joined:
    Jun 22, 2007
    Messages:
    1,479
    Ah. Thanks. :)
     
  16. OrionVeteran

    OrionVeteran Chieftain

    Joined:
    Dec 25, 2003
    Messages:
    2,405
    Location:
    Newport News VA

Share This Page