Hooks
static bool BeforeFindClosestCity(const PlayerTypes ePlayer, const CvPlot& kPlot, const CvCity*& result);static void AfterFindClosestCity(const PlayerTypes ePlayer, const CvPlot& kPlot, const CvCity*& result);static const CvCity* ReplaceFindClosestCity(const CvCity* (*original)(const PlayerTypes, const CvPlot&), const PlayerTypes ePlayer, const CvPlot& kPlot);CivRegisterBefore<CivHooks::FindClosestCity>(host, &BeforeFindClosestCity);CivRegisterAfter<CivHooks::FindClosestCity>(host, &AfterFindClosestCity);CivRegisterReplace<CivHooks::FindClosestCity>(host, &ReplaceFindClosestCity);
Code
792const CvCity* FindClosestCity(const PlayerTypes ePlayer, const CvPlot& kPlot)793{794 {795 const CvCity* pkPlotCity = GetCity(ePlayer, &kPlot);796 if(pkPlotCity != NULL)797 return pkPlotCity;798 }799800 const CvMap& kMap = GC.getMap();801802 const int iPlotX = kPlot.getX();803 const int iPlotY = kPlot.getY();804805 const int iMapHeight = kMap.getGridHeight();806 const int iMapWidth = kMap.getGridWidth();807808 const bool bWorldWrap = kMap.isWrapX();809810 // Do a search radiating from the search plot811 for(int iDist = 1; iDist < iMapHeight || iDist < iMapWidth; ++iDist)812 {813 // Center Row814 if(iPlotX - iDist >= 0)815 {816 const CvPlot* pkTestPlot = kMap.plot(iPlotX - iDist, iPlotY);817 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);818 if(pkCity) return pkCity;819 }820 else if(bWorldWrap)821 {822 int iX = iPlotX - iDist;823 while(iX < 0) iX += iMapWidth;824825 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY);826 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);827 if(pkCity) return pkCity;828 }829830 if(iPlotX + iDist < iMapWidth)831 {832 const CvPlot* pkTestPlot = kMap.plot(iPlotX + iDist, iPlotY);833 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);834 if(pkCity) return pkCity;835 }836 else if(bWorldWrap)837 {838 int iX = iPlotX + iDist;839 while(iX >= iMapWidth) iX -= iMapWidth;840841 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY);842 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);843 if(pkCity) return pkCity;844 }845846 // Top and Bottom Rows847 const int iRowOffset = (iPlotY % 2 == 0)? 0 : 1;848 for(int iRow = 1; iRow < iDist; ++iRow)849 {850 const int iRowWidth = (iDist * 2) - (iRow - 1);851852 {853 // Left-most plot in the current row854 int iX = iPlotX - (iRowWidth / 2);855 if(iRow % 2 == 1) iX += iRowOffset;856857 if(iX < 0)858 {859 if(bWorldWrap)860 do861 {862 iX += iMapWidth;863 }864 while(iX < 0);865 }866867 if(iX >= 0)868 {869 if(iPlotY - iRow >= 0)870 {871 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY - iRow);872 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);873 if(pkCity) return pkCity;874 }875876 if(iPlotY + iRow < iMapHeight)877 {878 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY + iRow);879 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);880 if(pkCity) return pkCity;881 }882 }883 }884885 {886 // Right-most plot in the current row887 int iX = iPlotX + (iRowWidth / 2) - 1;888 if(iRow % 2 == 1) iX += iRowOffset;889890 if(iX >= iMapWidth)891 {892 if(bWorldWrap)893 do894 {895 iX -= iMapWidth;896 }897 while(iX >= iMapWidth);898 }899900 if(iX < iMapWidth)901 {902 if(iPlotY - iRow >= 0)903 {904 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY - iRow);905 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);906 if(pkCity) return pkCity;907 }908909 if(iPlotY + iRow < iMapHeight)910 {911 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY + iRow);912 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);913 if(pkCity) return pkCity;914 }915 }916 }917 }918919 // Top-most and Bottom-most rows920 {921 const int iRowWidth = iDist + 1;922 for(int iPlot = 0; iPlot < iRowWidth; ++iPlot)923 {924 int iX = iPlotX + iPlot - (iRowWidth / 2);925 if(iDist % 2 == 1) iX += iRowOffset;926927 if(iX < 0)928 {929 if(!bWorldWrap)930 continue;931932 do933 {934 iX += iMapWidth;935 }936 while(iX < 0);937 }938 else if(iX >= iMapWidth)939 {940 if(!bWorldWrap)941 continue;942943 do944 {945 iX -= iMapWidth;946 }947 while(iX >= iMapWidth);948 }949950 if(iPlotY - iDist >= 0)951 {952 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY - iDist);953 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);954 if(pkCity) return pkCity;955 }956957 if(iPlotY + iDist < iMapHeight)958 {959 const CvPlot* pkTestPlot = kMap.plot(iX, iPlotY + iDist);960 const CvCity* pkCity = GetCity(ePlayer, pkTestPlot);961 if(pkCity) return pkCity;962 }963 }964 }965 }966967 return NULL;968}