Code
1822/// See if we want to finish any of our builds by rushing1823void CvEconomicAI::DoHurry()1824{1825 int iLoop = 0;18261827 OrderData* pOrder = 0;18281829 CvCity* pLoopCity = 0;1830 int iTurnsSaved = 0;18311832 int iHurryAmount = 0;1833 int iHurryAmountAvailable = 0;1834 int iI = 0;18351836 CvCity* pBestHurryCity = NULL;1837 int iBestHurryTurnsSaved = 0;1838 int iBestHurryAmount = 0;1839 int iBestHurryAmountAvailable = 0;1840 HurryTypes eBestHurryType = NO_HURRY;1841#if defined(MOD_AI_SMART_V3)1842 if (MOD_AI_SMART_V3)1843 {1844 //Exit if we don't have a set amount of gold, to avoid purchase overuse.1845 int comfortableGoldToHurry = 50 + (150 * m_pPlayer->GetCurrentEra());1846 int playerGold = m_pPlayer->GetTreasury()->GetGold();18471848 if (playerGold < comfortableGoldToHurry)1849 {1850 return;1851 }1852 }1853#endif18541855 // Look at each of our cities1856 for(pLoopCity = m_pPlayer->firstCity(&iLoop); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iLoop))1857 {1858 // What are we currently working on?1859 pOrder = pLoopCity->getOrderFromQueue(0);18601861#if defined(MOD_AI_SMART_V3)1862 if (MOD_AI_SMART_V3)1863 {1864 if(pOrder != NULL)1865 {1866 bool isPurchasable = pLoopCity->IsCanGoldPurchase(pOrder);1867 int prodPercentRemaining = ((pLoopCity->getProductionNeeded() - pLoopCity->getProduction()) * 100) / pLoopCity->getProductionNeeded();18681869 //We skip if the build order is more than 60% done.1870 if (prodPercentRemaining < 60)1871 {1872 continue;1873 }18741875 if (isPurchasable)1876 {1877 iTurnsSaved = pLoopCity->getProductionTurnsLeft() - 1;1878 //Also skip if we don't save any turns1879 if (iTurnsSaved < 2)1880 {1881 continue;1882 }1883 if (iBestHurryTurnsSaved < iTurnsSaved)1884 {1885 if(GC.getLogging() && GC.getAILogging() && isPurchasable)1886 {1887 static const char* orderTypeStrings[] = { "ORDER_TRAIN", "ORDER_CONSTRUCT", "ORDER_CREATE", "ORDER_PREPARE", "ORDER_MAINTAIN", "NO_ORDER" };1888 int orderIndex = ((pOrder->eOrderType < 0) || (pOrder->eOrderType > 4)) ? 5 : pOrder->eOrderType;1889 CvString strLogString;1890 strLogString.Format("DoHurry Option: order type %s, Turns Saved: %d,remaining percent = %d", orderTypeStrings[orderIndex], iTurnsSaved, prodPercentRemaining);1891 m_pPlayer->GetHomelandAI()->LogHomelandMessage(strLogString);1892 }1893 iBestHurryTurnsSaved = iTurnsSaved;1894 pBestHurryCity = pLoopCity;1895 }1896 }1897 }1898 }1899 else1900 {1901#endif1902 // Did we want to rush it?1903 if(pOrder != NULL && pOrder->bRush)1904 {1905 for(iI = 0; iI < GC.getNumHurryInfos(); iI++)1906 {1907 iHurryAmount = 0;19081909 if(pLoopCity->canHurry((HurryTypes)iI) &&1910 pLoopCity->hurryProduction((HurryTypes)iI) > 0)1911 {1912 iTurnsSaved = pLoopCity->getProductionTurnsLeft() - 1;1913 if(iTurnsSaved > 0)1914 {1915 if(GC.getHurryInfo((HurryTypes)iI)->getGoldPerProduction() > 0)1916 {1917 // Don't gold rush at all anymore (save gold to buy tiles)19181919 // iHurryAmount = pLoopCity->hurryGold((HurryTypes)iI);1920 // iHurryAmountAvailable = m_pPlayer->getGold();1921 }1922 else if(GC.getHurryInfo((HurryTypes)iI)->getProductionPerPopulation() > 0)1923 {1924 // Only pop rush if over our current Happiness limit1925 if(m_pPlayer->IsEmpireUnhappy())1926 {1927 iHurryAmount = pLoopCity->hurryPopulation((HurryTypes)iI);1928 iHurryAmountAvailable = pLoopCity->getPopulation();1929 }1930 }19311932 // Current hurry heuristic is to hurry whichever item has the most turns still to complete1933 if(iHurryAmount > 0 && iTurnsSaved >= iBestHurryTurnsSaved)1934 {1935 iBestHurryTurnsSaved = iTurnsSaved;1936 iBestHurryAmount = iHurryAmount;1937 iBestHurryAmountAvailable = iHurryAmountAvailable;1938 pBestHurryCity = pLoopCity;1939 eBestHurryType = (HurryTypes)iI;1940 }1941 }1942 }1943 }1944 }1945#if defined(MOD_AI_SMART_V3)1946 }1947#endif1948 }19491950 // Now enact the best hurry we've found (only hurry one item per turn for now)1951 if(pBestHurryCity != NULL)1952 {1953#if defined(MOD_AI_SMART_V3)1954 if (MOD_AI_SMART_V3)1955 {1956 pBestHurryCity->PurchaseCurrentOrder();1957 pBestHurryCity->AI_chooseProduction(false);1958 }1959 else1960 {1961#endif1962 pBestHurryCity->hurry(eBestHurryType);1963 pBestHurryCity->GetCityStrategyAI()->LogHurry(eBestHurryType, iBestHurryAmount, iBestHurryAmountAvailable, iBestHurryTurnsSaved);1964#if defined(MOD_AI_SMART_V3)1965 }1966#endif1967 }1968}