你这个搜索算法大有问题, 比如对于候选单位分别为S10E2,S2E10,S5E5时, 谁在前谁就成为pBestUnit.
改成或也不行,那样谁靠后,谁中选.
你必须先确立力量和经验值谁优先. 比如力量优先时, 我会这样做,
if (iCombatStrength >= iBestStrength)
{
int iNowExp = targetUnit[iU]->getExperience();
if (iNowExp > iBaseExp || iCombatStrength > iBestStrength)
{
iBestStrength = iCombatStrength;
pBestUnit = targetUnit[iU];
pBestPlot = targetUnit[iU]->plot();
iBaseExp = iNowExp;
}
}
外加再最开始iBaseExp = -1, 否则如果所有单位的经验都是0,照你的算法会找不着候选人的. 当然一般情形下,能有大军,就不会这样.
另外pBestPlot不用每次都重新赋值, 最后找到pBestUnit!=Null时,直接用pBestPlot=pBestUnit.plot()就行了.
说实在,Firaxis原来的代码,很多地方,连我这个完完全全的外行人都觉得可以优化, 可想而知其效率了.
[ 本帖最后由 Khyron 于 2008-9-19 14:24 编辑 ] |