Beispiel #1
0
void CThreatMap::EnemyEnterLOS(CEnemyUnit* enemy)
{
	// Possible cases:
	// (1) Unknown enemy that has been detected for the first time
	// (2) Unknown enemy that was only in radar enters LOS
	// (3) Known enemy that already was in LOS enters again

	enemy->SetInLOS();

	if (enemy->GetDPS() < 0.1f) {
		if (enemy->GetThreat() > .0f) {  // (2)
			// threat prediction failed when enemy was unknown
			if (enemy->IsHidden()) {
				enemy->ClearHidden();
			} else {
				DelEnemyUnitAll(enemy);
			}
			hostileUnits.erase(enemy->GetId());
			peaceUnits[enemy->GetId()] = enemy;
			enemy->SetThreat(.0f);
			enemy->SetRange(0);
			enemy->SetDecloakRange(GetCloakRange(enemy));
		} else if (peaceUnits.find(enemy->GetId()) == peaceUnits.end()) {
			peaceUnits[enemy->GetId()] = enemy;
			enemy->SetDecloakRange(GetCloakRange(enemy));
		} else if (enemy->IsHidden()) {
			enemy->ClearHidden();
		} else {
			DelDecloaker(enemy);
		}

		AIFloat3 pos = enemy->GetUnit()->GetPos();
		circuit->GetTerrainManager()->CorrectPosition(pos);
		enemy->SetPos(pos);
		enemy->SetKnown();

		AddDecloaker(enemy);
		return;
	}

	if (hostileUnits.find(enemy->GetId()) == hostileUnits.end()) {
		hostileUnits[enemy->GetId()] = enemy;
	} else if (enemy->IsHidden()) {
		enemy->ClearHidden();
	} else if (enemy->IsKnown()) {
		DelEnemyUnit(enemy);
	} else {
		DelEnemyUnitAll(enemy);
	}

	AIFloat3 pos = enemy->GetUnit()->GetPos();
	circuit->GetTerrainManager()->CorrectPosition(pos);
	enemy->SetPos(pos);
	enemy->SetRange(GetEnemyUnitRange(enemy));
	enemy->SetDecloakRange(GetCloakRange(enemy));
	enemy->SetThreat(GetEnemyUnitThreat(enemy));
	enemy->SetKnown();

	AddEnemyUnit(enemy);
}
Beispiel #2
0
void CThreatMap::EnemyDestroyed(int enemyUnitID, int) {
	std::map<int, EnemyUnit>::const_iterator it = enemyUnits.find(enemyUnitID);

	if (it != enemyUnits.end()) {
		const EnemyUnit& enemyUnit = it->second;

		DelEnemyUnit(enemyUnit);
		enemyUnits.erase(enemyUnitID);
	}
}
Beispiel #3
0
void CThreatMap::EnemyDamaged(CEnemyUnit* enemy)
{
	auto it = hostileUnits.find(enemy->GetId());
	if ((it == hostileUnits.end()) || !enemy->IsInLOS()) {
		return;
	}

	DelEnemyUnit(enemy);
	enemy->SetThreat(GetEnemyUnitThreat(enemy));
	AddEnemyUnit(enemy);
}
Beispiel #4
0
void CThreatMap::EnemyDamaged(int enemyUnitID, int) {
	std::map<int, EnemyUnit>::iterator it = enemyUnits.find(enemyUnitID);

	if (it != enemyUnits.end()) {
		EnemyUnit& enemyUnit = it->second;

		DelEnemyUnit(enemyUnit);
			enemyUnit.threat = GetEnemyUnitThreat(enemyUnit);
		AddEnemyUnit(enemyUnit, 1.0f);
	}
}
Beispiel #5
0
void CThreatMap::EnemyEnterRadar(CEnemyUnit* enemy)
{
	// Possible cases:
	// (1) Unknown enemy wanders at radars
	// (2) Known enemy that once was in los wandering at radar
	// (3) EnemyEnterRadar invoked right after EnemyEnterLOS in area with no radar

	enemy->SetInRadar();

	if (enemy->IsInLOS()) {  // (3)
		return;
	}

	if (enemy->GetDPS() < 0.1f) {  // (2)
		if (enemy->IsHidden()) {
			enemy->ClearHidden();
		} else {
			DelDecloaker(enemy);
		}

		AIFloat3 pos = enemy->GetUnit()->GetPos();
		circuit->GetTerrainManager()->CorrectPosition(pos);
		enemy->SetPos(pos);

		AddDecloaker(enemy);
		return;
	}

	bool isNew = false;
	auto it = hostileUnits.find(enemy->GetId());
	if (it == hostileUnits.end()) {  // (1)
		std::tie(it, isNew) = hostileUnits.emplace(enemy->GetId(), enemy);
	} else if (enemy->IsHidden()) {
		enemy->ClearHidden();
	} else {
		DelEnemyUnit(enemy);
	}

	AIFloat3 pos = enemy->GetUnit()->GetPos();
	circuit->GetTerrainManager()->CorrectPosition(pos);
	enemy->SetPos(pos);
	if (isNew) {  // unknown enemy enters radar for the first time
		enemy->SetThreat(enemy->GetDPS());  // TODO: Randomize
		enemy->SetRange(CEnemyUnit::RangeType::MAX, rangeDefault);
		enemy->SetRange(CEnemyUnit::RangeType::AIR, rangeDefault);
		enemy->SetRange(CEnemyUnit::RangeType::LAND, rangeDefault);
		enemy->SetRange(CEnemyUnit::RangeType::WATER, rangeDefault);
		enemy->SetRange(CEnemyUnit::RangeType::CLOAK, distCloak);
	}

	AddEnemyUnit(enemy);
}
Beispiel #6
0
void CThreatMap::Update() {
	currMaxThreat = 0.0f;

	// account for moving units
	for (std::map<int, EnemyUnit>::iterator it = enemyUnits.begin(); it != enemyUnits.end(); ++it) {
		EnemyUnit& e = it->second;

		DelEnemyUnit(e);
			e.pos    = ai->ccb->GetUnitPos(e.id);
			e.threat = GetEnemyUnitThreat(e);
		AddEnemyUnit(e, 1.0f);

		currMaxThreat = std::max(currMaxThreat, e.threat);
	}

	// TODO: staggered updates
	if (threatMapTexID >= 0) {
		if (currMaxThreat > 0.0f) {
			for (int i = 0; i < area; i++) {
				threatCellsVis[i] = (threatCellsRaw[i] - THREATVAL_BASE) / currMaxThreat;
			}

			ai->cb->DebugDrawerUpdateOverlayTexture(threatMapTexID, &threatCellsVis[0], 0, 0, width, height);
		}
	}

	#if (LUA_THREATMAP_DEBUG == 1)
	{
		std::string luaDataStr;
		std::stringstream luaDataStream;
			luaDataStream << "local threatMapArray = GG.AIThreatMap;\n";

		// just copy the entire map
		for (int i = 0; i < area; i++) {
			luaDataStream << "threatMapArray[" << i << "]";
			luaDataStream << " = ";
			luaDataStream << (threatCellsRaw[i] - THREATVAL_BASE);
			luaDataStream << " / ";
			luaDataStream << currMaxThreat;
			luaDataStream << ";\n";
		}

		luaDataStr = luaDataStream.str();

		ai->cb->CallLuaRules("[AI::KAIK::ThreatMap::Update]", -1, NULL);
		ai->cb->CallLuaRules(luaDataStr.c_str(), -1, NULL);
	}
	#endif
}
Beispiel #7
0
void CThreatMap::EnemyDestroyed(CEnemyUnit* enemy)
{
	auto it = hostileUnits.find(enemy->GetId());
	if (it == hostileUnits.end()) {
		if (!enemy->IsHidden()) {
			DelDecloaker(enemy);
		}
		peaceUnits.erase(enemy->GetId());
		return;
	}

	if (!enemy->IsHidden()) {
		DelEnemyUnit(enemy);
	}
	hostileUnits.erase(it);
}
Beispiel #8
0
void CThreatMap::Update() {
	// reset every frame
	currMaxThreat = 0.0f;

	// account for moving units
	for (std::map<int, EnemyUnit>::iterator it = enemyUnits.begin(); it != enemyUnits.end(); ++it) {
		EnemyUnit& e = it->second;

		DelEnemyUnit(e);
			e.pos    = ai->ccb->GetUnitPos(e.id);
			e.threat = GetEnemyUnitThreat(e);
		AddEnemyUnit(e, 1.0f);

		currMaxThreat = std::max(currMaxThreat, e.threat);
	}


	#if (LUA_THREATMAP_DEBUG == 1)
	std::string luaDataStr;
	std::stringstream luaDataStream;
		luaDataStream << "local threatMapArray = GG.AIThreatMap;\n";
	#endif

	// just copy the entire map
	for (int i = 0; i < area; i++) {
		#if (LUA_THREATMAP_DEBUG == 1)
		luaDataStream << "threatMapArray[" << i << "]";
		luaDataStream << " = ";
		luaDataStream << threatCells[i];
		luaDataStream << " / ";
		luaDataStream << currMaxThreat;
		luaDataStream << ";\n";
		#endif
	}

	#if (LUA_THREATMAP_DEBUG == 1)
	luaDataStr = luaDataStream.str();

	ai->cb->CallLuaRules("[AI::KAIK::ThreatMap::Update]", -1, NULL);
	ai->cb->CallLuaRules(luaDataStr.c_str(), -1, NULL);
	#endif
}