示例#1
0
void CEnergyGrid::Init()
{
	for (auto& kv : circuit->GetCircuitDefs()) {
		const std::map<std::string, std::string>& customParams = kv.second->GetUnitDef()->GetCustomParams();
		auto it = customParams.find("pylonrange");
		if (it != customParams.end()) {
			pylonRanges[kv.first] = utils::string_to_float(it->second);
		}
	}
	// FIXME: const names
	const char* names[] = {"armestor", "armsolar", "armwin"};
	for (const char* name : names) {
		CCircuitDef* cdef = circuit->GetCircuitDef(name);
		rangePylons[pylonRanges[cdef->GetId()]] = cdef->GetId();
	}

	CMetalManager* metalManager = circuit->GetMetalManager();
	const CMetalData::Clusters& clusters = metalManager->GetClusters();
	const CMetalData::Graph& clusterGraph = metalManager->GetGraph();

	linkedClusters.resize(clusters.size(), false);

	links.reserve(boost::num_edges(clusterGraph));
	CMetalData::Graph::edge_iterator edgeIt, edgeEnd;
	std::tie(edgeIt, edgeEnd) = boost::edges(clusterGraph);
	for (; edgeIt != edgeEnd; ++edgeIt) {
		const CMetalData::EdgeDesc& edgeId = *edgeIt;
		int idx0 = boost::source(edgeId, clusterGraph);
		int idx1 = boost::target(edgeId, clusterGraph);
		links.emplace_back(idx0, clusters[idx0].geoCentr, idx1, clusters[idx1].geoCentr);
	}
	linkIt = boost::make_iterator_property_map(&links[0], boost::get(&CMetalData::SEdge::index, clusterGraph));

	ownedClusters = CMetalData::Graph(boost::num_vertices(clusterGraph));
}
示例#2
0
void CEnergyGrid::ReadConfig()
{
	const Json::Value& root = circuit->GetSetupManager()->GetConfig();
	const std::string& cfgName = circuit->GetSetupManager()->GetConfigName();
	const Json::Value& pylon = root["economy"]["energy"]["pylon"];
	for (const Json::Value& pyl : pylon) {
		CCircuitDef* cdef = circuit->GetCircuitDef(pyl.asCString());
		if (cdef == nullptr) {
			circuit->LOG("CONFIG %s: has unknown UnitDef '%s'", cfgName.c_str(), pyl.asCString());
		} else {
			rangePylons[pylonRanges[cdef->GetId()]] = cdef->GetId();
		}
	}
}
示例#3
0
void CAllyTeam::Init(CCircuitAI* circuit)
{
	if (initCount++ > 0) {
		return;
	}

	TeamRulesParam* trp = circuit->GetTeam()->GetTeamRulesParamByName("start_box_id");
	if (trp != nullptr) {
		int boxId = trp->GetValueFloat();
		startBox = circuit->GetGameAttribute()->GetSetupData().GetStartBox(boxId);
		delete trp;
	}

	metalManager = std::make_shared<CMetalManager>(circuit, &circuit->GetGameAttribute()->GetMetalData());
	if (metalManager->HasMetalSpots() && !metalManager->HasMetalClusters() && !metalManager->IsClusterizing()) {
		metalManager->ClusterizeMetal();
	}
	// Init after parallel clusterization
	circuit->GetScheduler()->RunParallelTask(CGameTask::emptyTask, std::make_shared<CGameTask>(&CMetalManager::Init, metalManager));

	energyLink = std::make_shared<CEnergyGrid>(circuit);
	defence = std::make_shared<CDefenceMatrix>(circuit);
	pathfinder = std::make_shared<CPathFinder>(&circuit->GetGameAttribute()->GetTerrainData());

	// TODO: Move factory selection into CFactoryManager?
	//       Can't figure how as this should work per ally team.
	const char* factories[] = {
		"factorycloak",
		"factoryamph",
		"factoryhover",
		"factoryjump",
		"factoryshield",
		"factoryspider",
		"factorytank",
		"factoryveh",
		"factoryplane",
		"factorygunship",
		"factoryship",
	};
	const int size = sizeof(factories) / sizeof(factories[0]);
	factoryBuilds.reserve(size);
	std::map<STerrainMapMobileType::Id, float> percents;
	CTerrainData& terrainData = circuit->GetGameAttribute()->GetTerrainData();
	const std::vector<STerrainMapImmobileType>& immobileType = terrainData.areaData0.immobileType;
	const std::vector<STerrainMapMobileType>& mobileType = terrainData.areaData0.mobileType;
	for (const char* fac : factories) {
		CCircuitDef* cdef = circuit->GetCircuitDef(fac);
		STerrainMapImmobileType::Id itId = cdef->GetImmobileId();
		if ((itId < 0) || !immobileType[itId].typeUsable) {
			continue;
		}

		STerrainMapMobileType::Id mtId = cdef->GetMobileId();
		if (mtId < 0) {
			factoryBuilds.push_back(cdef->GetId());
			percents[cdef->GetId()] = 60.0 + rand() / (float)RAND_MAX * 50.0;
		} else if (mobileType[mtId].typeUsable) {
			factoryBuilds.push_back(cdef->GetId());
			float shift = rand() / (float)RAND_MAX * 40.0 - 20.0;
			percents[cdef->GetId()] = mobileType[mtId].areaLargest->percentOfMap + shift;
		}
	}
	auto cmp = [circuit, &percents](const CCircuitDef::Id aId, const CCircuitDef::Id bId) {
		return percents[aId] > percents[bId];
	};
	std::sort(factoryBuilds.begin(), factoryBuilds.end(), cmp);
}