Hooks
static bool BeforeCvLuaPlayer_lGetRecommendedFoundCityPlots(lua_State* L, int& result);static void AfterCvLuaPlayer_lGetRecommendedFoundCityPlots(lua_State* L, int& result);static int ReplaceCvLuaPlayer_lGetRecommendedFoundCityPlots(int (*original)(lua_State*), lua_State* L);CivRegisterBefore<CivHooks::CvLuaPlayer_lGetRecommendedFoundCityPlots>(host, &BeforeCvLuaPlayer_lGetRecommendedFoundCityPlots);CivRegisterAfter<CivHooks::CvLuaPlayer_lGetRecommendedFoundCityPlots>(host, &AfterCvLuaPlayer_lGetRecommendedFoundCityPlots);CivRegisterReplace<CivHooks::CvLuaPlayer_lGetRecommendedFoundCityPlots>(host, &ReplaceCvLuaPlayer_lGetRecommendedFoundCityPlots);
Code
9323int CvLuaPlayer::lGetRecommendedFoundCityPlots(lua_State* L)9324{9325 CvPlayerAI* pkPlayer = GetInstance(L);93269327 CvUnit* pFoundingUnit = NULL;93289329 //Get first selected unit that can found cities.9330 CvEnumerator<ICvUnit1> selectedUnits(GC.GetEngineUserInterface()->GetSelectedUnits());9331 while(selectedUnits.MoveNext())9332 {9333 auto_ptr<ICvUnit1> pUnit(selectedUnits.GetCurrent());9334 if(pUnit.get() != NULL)9335 {9336 CvUnit* pkUnit = GC.UnwrapUnitPointer(pUnit.get());9337 if(pkUnit != NULL && pkUnit->isFound())9338 {9339 pFoundingUnit = pkUnit;9340 break;9341 }9342 }9343 }93449345 if(pFoundingUnit == NULL)9346 return 0;93479348 int iSettlerDistance;9349 int iDistanceDropoff;9350 int iSettlerX, iSettlerY;9351 int iPathTurns;9352 int iValue;9353 int iDanger;93549355 // Get coordinates for settler's plot9356 iSettlerX = pFoundingUnit->getX();9357 iSettlerY = pFoundingUnit->getY();93589359 int iEvalDistance = GC.getSETTLER_EVALUATION_DISTANCE();9360 int iDistanceDropoffMod = GC.getSETTLER_DISTANCE_DROPOFF_MODIFIER();9361 int iBeginSearchX = iSettlerX - iEvalDistance;9362 int iBeginSearchY = iSettlerY - iEvalDistance;9363 int iEndSearchX = iSettlerX + iEvalDistance;9364 int iEndSearchY = iSettlerY + iEvalDistance;93659366 CvMap& kMap = GC.getMap();93679368 TeamTypes eUnitTeam = pFoundingUnit->getTeam();93699370 CvCity* pCapital = pkPlayer->getCapitalCity();9371 int iCapArea = NULL;9372 if(pCapital)9373 {9374 iCapArea = pCapital->getArea();9375 }93769377 WeightedPlotVector aBestPlots;9378 aBestPlots.reserve((iEvalDistance+1) * 2);93799380 for(int iPlotX = iBeginSearchX; iPlotX != iEndSearchX; iPlotX++)9381 {9382 for(int iPlotY = iBeginSearchY; iPlotY != iEndSearchY; iPlotY++)9383 {9384 CvPlot* pPlot = kMap.plot(iPlotX, iPlotY);9385 if(!pPlot)9386 {9387 continue;9388 }93899390 //if (!pPlot->isVisible(pUnit->getTeam(), false /*bDebug*/))9391 if(!pPlot->isRevealed(eUnitTeam))9392 {9393 continue;9394 }93959396 // Can't actually found here!9397 if(!pkPlayer->canFound(iPlotX, iPlotY))9398 {9399 continue;9400 }94019402 //// This operation is just for settling on the same continent as the capital9403 //if(pCapital && pPlot->getArea() != iCapArea)9404 //{9405 // continue;9406 //}94079408 // Do we have to check if this is a safe place to go?9409 if(!pPlot->isVisibleEnemyUnit(pkPlayer->GetID()))9410 {9411 iSettlerDistance = plotDistance(iPlotX, iPlotY, iSettlerX, iSettlerY);94129413 //iValue = pPlot->getFoundValue(pkPlayer->GetID());9414 iValue = pkPlayer->AI_foundValue(iPlotX, iPlotY, -1, false);94159416 iDistanceDropoff = (iDistanceDropoffMod * iSettlerDistance) / iEvalDistance;9417 iValue = iValue * (100 - iDistanceDropoff) / 100;9418 iDanger = pkPlayer->GetPlotDanger(*pPlot);9419 if(iDanger < 1000)9420 {9421 iValue = ((1000 - iDanger) * iValue) / 1000;94229423 aBestPlots.push_back(pPlot, iValue);9424 }9425 }9426 }9427 }94289429 int iReturnSize = 0;9430 int iFailLimit = 20; // Paths can be really slow to create, bail if we fail too many times.9431 #define MAX_RECCOMEND_RETURN 39432 CvPlot* aPlots[MAX_RECCOMEND_RETURN];94339434 uint uiListSize;9435 if ((uiListSize = aBestPlots.size()) > 0)9436 {9437 aBestPlots.SortItems(); // highest score will be first.9438 for (uint i = 0; i < uiListSize; ++i ) 9439 {9440 CvPlot* pPlot = aBestPlots.GetElement(i);9441 bool bCanFindPath = pFoundingUnit->GeneratePath(pPlot, MOVE_TERRITORY_NO_UNEXPLORED, true, &iPathTurns);9442 if(bCanFindPath)9443 {9444 aPlots[iReturnSize] = pPlot;9445 ++iReturnSize;9446 if (iReturnSize == MAX_RECCOMEND_RETURN)9447 break;9448 }9449 else9450 {9451 if (iFailLimit-- == 0)9452 break;9453 }9454 }9455 }94569457 lua_createtable(L, iReturnSize, 0);9458 if (iReturnSize > 0)9459 {9460 int iPositionIndex = lua_gettop(L);9461 for(int i = 0; i < iReturnSize; i++)9462 {9463 CvLuaPlot::Push(L, aPlots[i]);9464 lua_rawseti(L, iPositionIndex, i + 1);9465 }9466 }94679468 return 1;9469}