Example #1
0
// operates in terms of GetScore() (which is recursive for factories)
const UnitDef* CUnitTable::GetUnitByScore(int builderID, UnitCategory cat) {
	if (cat == CAT_LAST) {
		return NULL;
	}

	const UnitDef* builderDef  = ai->cb->GetUnitDef(builderID);
	const UnitDef* tempUnitDef = NULL;

	const std::vector<int>& defs = categoryData.GetDefsForUnitCat(cat);
	float tempScore = 0.0f;
	float bestScore = 0.0f;

	// if we are a builder on side i, then templist must have
	// at least i + 1 elements (templist[0], ..., templist[i])
	// but if a mod is not symmetric (eg. no builders for side
	// 1) this assumption fails; enabling this breaks PURE 0.6
	// however

	// iterate over all types in defs (eg. Core groundDefenses)
	for (unsigned int i = 0; i != defs.size(); i++) {
		int tempUnitDefID = defs[i];

		// if our builder can build the i-th unit
		if (CanBuildUnit(builderDef->id, tempUnitDefID)) {
			// get the unit's heuristic score (based on current income)
			tempScore = GetScore(unitTypes[tempUnitDefID].def, cat);

			if (tempScore > bestScore) {
				bestScore = tempScore;
				tempUnitDef = unitTypes[tempUnitDefID].def;
			}
		}
	}


	// if we didn't find a unit to build with score > 0 (ie.
	// if builder has no build-option matching this category)
	// then return NULL instead of first option on build menu
	// (to prevent radar farms and other bizarro side-effects)
	return ((bestScore > 0.0f)? tempUnitDef: NULL);
}
// operates in terms of GetScore() (which is recursive for factories)
const UnitDef* CUnitTable::GetUnitByScore(int builderUnitID, int category) {
	if (category == LASTCATEGORY)
		return 0x0;

	vector<int>* tempList = 0;
	const UnitDef* builderDef = ai->cb->GetUnitDef(builderUnitID);
	const UnitDef* tempUnitDef = 0;
	int side = GetSide(builderUnitID);
	float tempScore = 0.0f;
	float bestScore = 0.0f;

	switch (category) {
		case CAT_ENERGY:
			tempList = ground_energy;
			break;
		case CAT_MEX:
			tempList = metal_extractors;
			break;
		case CAT_MMAKER:
			tempList = metal_makers;
			break;
		case CAT_G_ATTACK:
			tempList = ground_attackers;
			break;
		case CAT_DEFENCE:
			tempList = ground_defences;
			break;
		case CAT_BUILDER:
			tempList = ground_builders;
			break;
		case CAT_FACTORY:
			tempList = ground_factories;
			break;
		case CAT_MSTOR:
			tempList = metal_storages;
			break;
		case CAT_ESTOR:
			tempList = energy_storages;
			break;
		case CAT_NUKE:
			tempList = nuke_silos;
			break;
	}

	// if we are a builder on side i, then templist must have
	// at least i + 1 elements (templist[0], ..., templist[i])
	// but if a mod is not symmetric (eg. no builders for side
	// 1) this assumption fails; enabling this breaks PURE 0.6
	// however
	//
	// if (tempList->size() >= side + 1) {
		// iterate over all units for <side> in tempList (eg. Core ground_defences)
		for (unsigned int i = 0; i != tempList[side].size(); i++) {
			int tempUnitDefID = tempList[side][i];

			// if our builder can build the i-th unit
			if (CanBuildUnit(builderDef->id, tempUnitDefID)) {
				// get the unit's heuristic score (based on current income)
				tempScore = GetScore(unitTypes[tempUnitDefID].def, category);

				if (tempScore > bestScore) {
					bestScore = tempScore;
					tempUnitDef = unitTypes[tempUnitDefID].def;
				}
			}
		}
	// }

	// if we didn't find a unit to build with score > 0 (ie.
	// if builder has no build-option matching this category)
	// then return NULL instead of first option on build menu
	// (to prevent radar farms and other bizarro side-effects)
	return ((bestScore > 0.0f)? tempUnitDef: NULL);
}