Advanced Economy & Trade Routes

Not really a dll programmer, but this discussion looks interesting.

I am guessing the selection of trade routes is done in CvCity::updateTradeRoutes(), where the disgusting for loops are.

Spoiler :
Code:
	if (!isDisorder() && !isPlundered())
	{
		iTradeRoutes = getTradeRoutes();

		FAssert(iTradeRoutes <= GC.getDefineINT("MAX_TRADE_ROUTES"));

		for (iI = 0; iI < MAX_PLAYERS; iI++)
		{
			if (GET_PLAYER(getOwnerINLINE()).canHaveTradeRoutesWith((PlayerTypes)iI))
			{
				for (pLoopCity = GET_PLAYER((PlayerTypes)iI).firstCity(&iLoop); pLoopCity != NULL; pLoopCity = GET_PLAYER((PlayerTypes)iI).nextCity(&iLoop))
				{
					if (pLoopCity != this)
					{
						if (!(pLoopCity->isTradeRoute(getOwnerINLINE())) || (getTeam() == GET_PLAYER((PlayerTypes)iI).getTeam()))
						{
							if (pLoopCity->plotGroup(getOwnerINLINE()) == plotGroup(getOwnerINLINE()) || GC.getDefineINT("IGNORE_PLOT_GROUP_FOR_TRADE_ROUTES"))
							{
								iValue = calculateTradeProfit(pLoopCity);

								[B][COLOR="Red"]for (iJ = 0; iJ < iTradeRoutes; iJ++)
								{
									if (iValue > paiBestValue[iJ])
									{
										for (iK = (iTradeRoutes - 1); iK > iJ; iK--)
										{
											paiBestValue[iK] = paiBestValue[(iK - 1)];
											m_paTradeCities[iK] = m_paTradeCities[(iK - 1)];
										}

										paiBestValue[iJ] = iValue;
										m_paTradeCities[iJ] = pLoopCity->getIDInfo();

										break;
									}
								}[/COLOR][/B]
							}
						}
					}
				}
			}
		}
	}

But the last loop in red seems redundant to me. You might as well simply use paiBestValue to store iValue and City ID of each potential city, then do a sort outside the nested loop to get the top X Cities with best iValue.

Effectively, it takes the last step of "Loop over the number of trade routes possible for our city and find the best" out of the nested for loop.
 
Not really a dll programmer, but this discussion looks interesting.

I am guessing the selection of trade routes is done in CvCity::updateTradeRoutes(), where the disgusting for loops are.

Spoiler :
Code:
	if (!isDisorder() && !isPlundered())
	{
		iTradeRoutes = getTradeRoutes();

		FAssert(iTradeRoutes <= GC.getDefineINT("MAX_TRADE_ROUTES"));

		for (iI = 0; iI < MAX_PLAYERS; iI++)
		{
			if (GET_PLAYER(getOwnerINLINE()).canHaveTradeRoutesWith((PlayerTypes)iI))
			{
				for (pLoopCity = GET_PLAYER((PlayerTypes)iI).firstCity(&iLoop); pLoopCity != NULL; pLoopCity = GET_PLAYER((PlayerTypes)iI).nextCity(&iLoop))
				{
					if (pLoopCity != this)
					{
						if (!(pLoopCity->isTradeRoute(getOwnerINLINE())) || (getTeam() == GET_PLAYER((PlayerTypes)iI).getTeam()))
						{
							if (pLoopCity->plotGroup(getOwnerINLINE()) == plotGroup(getOwnerINLINE()) || GC.getDefineINT("IGNORE_PLOT_GROUP_FOR_TRADE_ROUTES"))
							{
								iValue = calculateTradeProfit(pLoopCity);

								[B][COLOR="Red"]for (iJ = 0; iJ < iTradeRoutes; iJ++)
								{
									if (iValue > paiBestValue[iJ])
									{
										for (iK = (iTradeRoutes - 1); iK > iJ; iK--)
										{
											paiBestValue[iK] = paiBestValue[(iK - 1)];
											m_paTradeCities[iK] = m_paTradeCities[(iK - 1)];
										}

										paiBestValue[iJ] = iValue;
										m_paTradeCities[iJ] = pLoopCity->getIDInfo();

										break;
									}
								}[/COLOR][/B]
							}
						}
					}
				}
			}
		}
	}

But the last loop in red seems redundant to me. You might as well simply use paiBestValue to store iValue and City ID of each potential city, then do a sort outside the nested loop to get the top X Cities with best iValue.

Effectively, it takes the last step of "Loop over the number of trade routes possible for our city and find the best" out of the nested for loop.
I'd been thinking on this and had figured something along those lines might be possible... There ARE probably a number of ways to vastly improve the processing on the current mechanism and since I'd suggest anything either mod does be done via a game option shell, this original mechanism should certainly be as reworked as is humanly possible to improve the processing. THAT I may work on tooling around with a bit along the lines you suggest quite soon.
 
Top Bottom