Example #1
0
void CGroupOfGenerators::AddItems (SItemAddCtx &Ctx)

//	AddItems
//
//	Add items

	{
	int i, j;

	//	If we need to adjust counts, then do a separate algorithm

	if (SetsAverageValue())
		{
		//	Get the count adjustment.

		Metric rCountAdj = GetCountAdj(Ctx.iLevel);
		Metric rLoops = floor(rCountAdj);
		Metric rLastLoopAdj = rCountAdj - rLoops;

		//	Loop if we have extra items

		int iFullLoops = (int)rLoops;
		for (i = 0; i < iFullLoops + 1; i++)
			{
			//	For a full loop we just add the items

			if (i < iFullLoops)
				AddItemsInt(Ctx);

			//	Otherwise we need to add partial items

			else
				{
				//	Add the items to a private list.

				CItemList LocalList;
				CItemListManipulator ItemList(LocalList);
				SItemAddCtx LocalCtx(ItemList);
				LocalCtx.iLevel = Ctx.iLevel;
				AddItemsInt(LocalCtx);

				//	Now loop over the items and adjust the count appropriately.

				for (j = 0; j < LocalList.GetCount(); j++)
					{
					const CItem &Item = LocalList.GetItem(j);
					int iOriginalCount = Item.GetCount();

					//	Adjust the count

					Metric rNewCount = iOriginalCount * rLastLoopAdj;
					Metric rNewCountInt = floor(rNewCount);
					int iNewCount = (int)rNewCountInt;

					Metric rExtra = rNewCount - rNewCountInt;
					int iExtraChance = (int)(100000.0 * rExtra);
					if (mathRandom(0, 100000) < iExtraChance)
						iNewCount++;

					//	Add the item with the new count

					if (iNewCount > 0)
						{
						if (iNewCount == iOriginalCount)
							Ctx.ItemList.AddItem(Item);
						else
							{
							CItem NewItem(Item);
							NewItem.SetCount(iNewCount);
							Ctx.ItemList.AddItem(NewItem);
							}
						}
					}
				}
			}
		}
	else
		AddItemsInt(Ctx);
	}