Game Development Reference
In-Depth Information
for ( USHORT i = 1; i < NumBuckets; i++ ) {
TotalScore += mvTargets[i].Score;
} // end for
return TotalScore;
}
USHORT CAgent::ScoreToWeight(double ThisScore, double TotalScore)
{
// note the 0.5 addition to round rather than simply truncate
USHORT Weight = USHORT( ( TotalScore / ThisScore ) + 0.5 );
return Weight;
}
As we can see, SumScores() simply loops through a number of items in
mvTargets set by the value NumBuckets and totals their scores. We can then pass
TotalScore into ScoreToWeight with each of the records' scores to calculate the
new weight value. Despite its simplicity, we have split ScoreToWeight() out as a
separate function. We never know when we may want to modify (and likely com-
plicate) the algorithm later.
In this example, notice that we don't store the actual weight of each bucket but
rather use it immediately to calculate the new edge of that bucket. If we ever
needed to, we could calculate the weight of any bucket by comparing its edge
to that of the bucket preceding it. On the other hand, depending on how often
our data changed and how much of the data we expected to change at any one
time, there are times when storing the weights would be advantageous. This is
the approach we used in Chapter 12.
Because we need to sort our target vector to determine the top n targets (in this
case, eight), we put the call to BuildEdges() into SelectTarget() after we have
already scored and sorted the target information.
void CAgent::SelectTarget()
{
ScoreAllTargets();
std::sort( mvTargets.begin(), mvTargets.end() );
Search WWH ::




Custom Search