本文整理汇总了C++中CGroup::firstUnit方法的典型用法代码示例。如果您正苦于以下问题:C++ CGroup::firstUnit方法的具体用法?C++ CGroup::firstUnit怎么用?C++ CGroup::firstUnit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CGroup
的用法示例。
在下文中一共展示了CGroup::firstUnit方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addUnitOnCreated
void CEconomy::addUnitOnCreated(CUnit& unit) {
unitCategory c = unit.type->cats;
if ((c&MEXTRACTOR).any()) {
CGroup *group = requestGroup();
group->addUnit(unit);
takenMexes[group->key] = group->pos();
CUnit *builder = ai->unittable->getUnit(group->firstUnit()->builtBy);
if (builder) {
assert(group->key != builder->group->key);
takenMexes.erase(builder->group->key);
}
}
else if(unit.type->def->needGeo) {
CGroup *group = requestGroup();
group->addUnit(unit);
takenGeo[group->key] = group->pos();
CUnit *builder = ai->unittable->getUnit(group->firstUnit()->builtBy);
if (builder) {
assert(group->key != builder->group->key);
takenGeo.erase(builder->group->key);
}
}
}
示例2: update
void CMilitary::update(int frame) {
int busyScoutGroups = 0;
std::vector<int> keys;
std::vector<int> occupied;
std::map<int, bool> isOccupied;
std::map<int, ATask*>::iterator itTask;
std::map<MilitaryGroupBehaviour, std::map<int, CGroup*>* >::iterator itGroup;
TargetsFilter tf;
// NOTE: we store occupied targets in two formats because vector is used
// for list of targets (which can be used if no suitable primary
// targets are found), second one is used for TargetFilter to filter out
// occupied targets when searching for primary targets
for (itTask = ai->tasks->activeTasks[TASK_ATTACK].begin(); itTask != ai->tasks->activeTasks[TASK_ATTACK].end(); ++itTask) {
AttackTask *task = (AttackTask*)itTask->second;
occupied.push_back(task->target);
isOccupied[task->target] = true;
}
for(itGroup = groups.begin(); itGroup != groups.end(); itGroup++) {
int target = -2;
MilitaryGroupBehaviour behaviour = itGroup->first;
// setup common target filter params per behaviour...
tf.reset();
switch(behaviour) {
case SCOUT:
tf.threatRadius = 300.0f;
tf.threatFactor = 1000.0f;
break;
case ENGAGE:
tf.threatFactor = 0.001f;
break;
case BOMBER:
tf.threatFactor = 100.0f;
// TODO: replace constant with maneuvering radius of plane?
tf.threatRadius = 1000.0f;
break;
case AIRFIGHTER:
tf.threatFactor = 100.0f;
break;
}
// NOTE: start with random group ID because some groups can't reach the
// target (e.g. Fleas); this helps to overcome the problem when there
// is a target, but first group can't reach it, and AI constantly
// trying to add same task again and again which leads to attack stall
keys.clear();
util::GetShuffledKeys<int, CGroup*>(keys, *(itGroup->second));
const std::vector<CategoryMatcher>& targetBlocks = ai->intel->targets[behaviour];
for (int i = 0; i < keys.size(); ++i) {
CGroup *group = (*(itGroup->second))[keys[i]];
// if group is busy, don't bother...
if (group->busy || !group->canPerformTasks()) {
if (group->busy) {
if (behaviour == SCOUT)
busyScoutGroups++;
if (drawTasks)
visualizeTasks(group);
}
continue;
}
// NOTE: each group can have different score on the same target
// because of their disposition, strength etc.
tf.scoreCeiling = std::numeric_limits<float>::max();
tf.excludeId = &isOccupied;
// setup custom target filter params per current group...
switch(behaviour) {
case SCOUT:
if ((group->cats&AIR).any())
tf.threatCeiling = 1.1f;
else
tf.threatCeiling = (std::max<float>((float)MAX_SCOUTS_IN_GROUP / group->units.size(), 1.0f)) * group->strength - EPS;
break;
case BOMBER:
tf.threatCeiling = group->strength + group->firstUnit()->type->dps;
break;
}
// basic target selection...
if (target != -1) {
for(int b = 0; b < targetBlocks.size(); b++) {
target = group->selectTarget(ai->intel->enemies.getUnits(targetBlocks[b]), tf);
}
}
bool isAssembling = isAssemblingGroup(group);
if ((!isAssembling && behaviour == SCOUT) || target < 0) {
// scan for better target among existing targets...
tf.excludeId = NULL;
int assistTarget = group->selectTarget(occupied, tf);
if (assistTarget >= 0 && assistTarget != target) {
//.........这里部分代码省略.........
示例3: update
void CEconomy::update() {
int buildersCount = 0;
int assistersCount = 0;
int maxTechLevel = ai->cfgparser->getMaxTechLevel();
/* See if we can improve our eco by controlling metalmakers */
controlMetalMakers();
/* If we are stalling, do something about it */
preventStalling();
/* Update idle worker groups */
std::map<int, CGroup*>::iterator i;
for (i = activeGroups.begin(); i != activeGroups.end(); ++i) {
CGroup *group = i->second;
CUnit *unit = group->firstUnit();
if ((group->cats&MOBILE).any() && (group->cats&BUILDER).any())
buildersCount++;
if ((group->cats&MOBILE).any() && (group->cats&ASSISTER).any() && (group->cats&BUILDER).none())
assistersCount++;
if (group->busy || !group->canPerformTasks())
continue;
if ((group->cats&FACTORY).any()) {
ai->tasks->addTask(new FactoryTask(ai, *group));
continue;
}
if ((group->cats&STATIC).any() && (group->cats&BUILDER).none()) {
// we don't have a task for current unit type yet (these types are:
// MEXTRACTOR & geoplant)
continue;
}
float3 pos = group->pos();
// NOTE: we're using special algo for commander to prevent
// it walking outside the base
if ((unit->type->cats&COMMANDER).any()) {
tryFixingStall(group);
if (group->busy) continue;
/* If we don't have a factory, build one */
if (ai->unittable->factories.empty() || mexceeding) {
unitCategory factory = getNextTypeToBuild(unit, FACTORY, maxTechLevel);
if (factory.any())
buildOrAssist(*group, BUILD_FACTORY, factory);
if (group->busy) continue;
}
/* If we are exceeding and don't have estorage yet, build estorage */
if (eexceeding && !ai->unittable->factories.empty()) {
if (ai->unittable->energyStorages.size() < ai->cfgparser->getMaxTechLevel())
buildOrAssist(*group, BUILD_ESTORAGE, ESTORAGE);
if (group->busy) continue;
}
// NOTE: in NOTA only static commanders can build TECH1 factories
if ((unit->type->cats&STATIC).any()) {
/* See if this unit can build desired factory */
unitCategory factory = getNextTypeToBuild(unit, FACTORY, maxTechLevel);
if (factory.any())
buildOrAssist(*group, BUILD_FACTORY, factory);
if (group->busy) continue;
factory = getNextTypeToBuild(unit, BUILDER|STATIC, maxTechLevel);
if (factory.any())
// TODO: invoke BUILD_ASSISTER building algo instead of
// BUILD_FACTORY to properly place static builders?
buildOrAssist(*group, BUILD_FACTORY, factory);
if (group->busy) continue;
}
// build nearby metal extractors if possible...
if (mstall && !ai->gamemap->IsMetalMap()) {
// NOTE: there is a special hack withing buildOrAssist()
// to prevent building unnecessary MMAKERS
buildOrAssist(*group, BUILD_MPROVIDER, MEXTRACTOR);
if (group->busy) continue;
}
// see if we can build defense
tryBuildingDefense(group);
if (group->busy) continue;
tryAssistingFactory(group);
if (group->busy) continue;
}
else if ((unit->type->cats&BUILDER).any()) {
tryFixingStall(group);
if (group->busy) continue;
/* See if this unit can build desired factory */
unitCategory factory = getNextTypeToBuild(unit, FACTORY, maxTechLevel);
if (factory.any())
buildOrAssist(*group, BUILD_FACTORY, factory);
if (group->busy) continue;
//.........这里部分代码省略.........
示例4: buildOrAssist
void CEconomy::buildOrAssist(CGroup& group, buildType bt, unitCategory include, unitCategory exclude) {
ATask* task = canAssist(bt, group);
if (task != NULL) {
ai->tasks->addTask(new AssistTask(ai, *task, group));
return;
}
if ((group.cats&BUILDER).none())
return;
float3 pos = group.pos();
float3 goal = pos;
unitCategory catsWhere = canBuildWhere(group.cats);
unitCategory catsWhereStrict = canBuildWhere(group.cats, true);
if (bt == BUILD_EPROVIDER) {
if (!windmap)
exclude |= WIND;
if (!worthBuildingTidal)
exclude |= TIDAL;
}
else if (bt == BUILD_MPROVIDER) {
if ((include&MEXTRACTOR).any()) {
goal = getClosestOpenMetalSpot(group);
if (goal != ERRORVECTOR)
if (goal.y < 0.0f)
exclude |= (LAND|AIR);
else
exclude |= (SEA|SUB);
}
}
const CUnit* unit = group.firstUnit();
bool isComm = (unit->type->cats&COMMANDER).any();
std::multimap<float, UnitType*> candidates;
// retrieve the allowed buildable units...
if ((include&MEXTRACTOR).any())
ai->unittable->getBuildables(unit->type, include|catsWhereStrict, exclude, candidates);
else
ai->unittable->getBuildables(unit->type, include|catsWhere, exclude, candidates);
std::multimap<float, UnitType*>::iterator i = candidates.begin();
int iterations = candidates.size() / (ai->cfgparser->getTotalStates() - state + 1);
bool affordable = false;
if (!candidates.empty()) {
/* Determine which of these we can afford */
while(iterations >= 0) {
if (canAffordToBuild(unit->type, i->second))
affordable = true;
else
break;
if (i == --candidates.end())
break;
iterations--;
i++;
}
}
else if (bt != BUILD_MPROVIDER) {
return;
}
else {
goal = ERRORVECTOR;
}
/* Perform the build */
switch(bt) {
case BUILD_EPROVIDER: {
if (i->second->def->needGeo) {
goal = getClosestOpenGeoSpot(group);
if (goal != ERRORVECTOR) {
// TODO: prevent commander walking?
if (!eRequest && ai->pathfinder->getETA(group, goal) > 450.0f) {
goal = ERRORVECTOR;
}
}
}
else if ((i->second->cats&EBOOSTER).any()) {
goal = ai->coverage->getNextClosestBuildSite(unit, i->second);
}
if (goal == ERRORVECTOR) {
goal = pos;
if (i != candidates.begin())
--i;
else
++i;
}
if (i != candidates.end())
ai->tasks->addTask(new BuildTask(ai, bt, i->second, group, goal));
break;
}
case BUILD_MPROVIDER: {
bool canBuildMMaker = (eIncome - eUsage) >= METAL2ENERGY || eexceeding;
//.........这里部分代码省略.........