void CThreatMap::update(int frame) { static const unitCategory catsCanShootGround = ASSAULT|SNIPER|ARTILLERY|SCOUTER/*|PARALYZER*/; if ((frame - lastUpdateFrame) < MULTIPLEXER) return; const bool isWaterMap = !ai->gamemap->IsWaterFreeMap(); std::list<ThreatMapType> activeTypes; std::list<ThreatMapType>::const_iterator itMapType; reset(); int numUnits = ai->cbc->GetEnemyUnits(&ai->unitIDs[0], MAX_UNITS_AI); /* Add enemy threats */ for (int i = 0; i < numUnits; i++) { const int uid = ai->unitIDs[i]; const UnitDef* ud = ai->cbc->GetUnitDef(uid); if (ud == NULL) continue; const UnitType* ut = UT(ud->id); const unitCategory ecats = ut->cats; if ((ecats&ATTACKER).none() || ai->cbc->IsUnitParalyzed(uid) || ai->cbc->UnitBeingBuilt(uid)) continue; // ignore unamred, paralyzed & being built units if ((ecats&AIR).any() && (ecats&ASSAULT).none()) continue; // ignore air fighters & bombers // FIXME: using maxWeaponRange below (twice) is WRONG; we need // to calculate different max. ranges per each threatmap layer // FIXME: think smth cleverer if (ud->maxWeaponRange > MAX_WEAPON_RANGE_FOR_TM) continue; // ignore units with extra large range const float3 upos = ai->cbc->GetUnitPos(uid); activeTypes.clear(); if ((ecats&ANTIAIR).any() && upos.y >= 0.0f) { activeTypes.push_back(TMT_AIR); } if (((ecats&SEA).any() || upos.y >= 0.0f) && ((ecats&ANTIAIR).none() || (catsCanShootGround&ecats).any())) { activeTypes.push_back(TMT_SURFACE); } if (isWaterMap && (ecats&TORPEDO).any()) { activeTypes.push_back(TMT_UNDERWATER); } if (activeTypes.empty()) continue; const float uRealX = upos.x / PATH2REAL; const float uRealZ = upos.z / PATH2REAL; const float range = (ud->maxWeaponRange + 100.0f) / PATH2REAL; float powerT = ai->cbc->GetUnitPower(uid); const float power = (ecats&COMMANDER).any() ? powerT/20.0f : powerT; float3 pos(0.0f, 0.0f, 0.0f); const int R = (int) ceil(range); for (int z = -R; z <= R; z++) { for (int x = -R; x <= R; x++) { pos.x = x; pos.z = z; if (pos.Length2D() <= range) { pos.x += uRealX; pos.z += uRealZ; const int mx = int(round(pos.x)); const int mz = int(round(pos.z)); if (isInBounds(mx, mz)) { for (itMapType = activeTypes.begin(); itMapType != activeTypes.end(); ++itMapType) { int id = ID(mx, mz); maps[*itMapType][id] += power; maxPower[*itMapType] = std::max(maps[*itMapType][id], maxPower[*itMapType]); } } } } } /* for (itMapType = activeTypes.begin(); itMapType != activeTypes.end(); ++itMapType) { maxPower[*itMapType] = std::max<float>(power, maxPower[*itMapType]); } */ } #if !defined(BUILDING_AI_FOR_SPRING_0_81_2) if (ai->cb->IsDebugDrawerEnabled()) { std::map<ThreatMapType, int>::iterator i; for (i = handles.begin(); i != handles.end(); ++i) { float power = maxPower[i->first]; // normalize the data... for (int j = 0, N = X*Z; j < N; j++) maps[i->first][j] /= power; // update texturemap ai->cb->DebugDrawerUpdateOverlayTexture(i->second, maps[i->first], 0, 0, X, Z); // restore the original data... for (int j = 0, N = X*Z; j < N; j++) maps[i->first][j] *= power; } } #endif if (drawMap != TMT_NONE) visualizeMap(drawMap); lastUpdateFrame = frame; }
int main(int argc, char **argv) { //Initialize MPI MPI_Init(NULL, NULL); int whoAmI; MPI_Comm_rank(MPI_COMM_WORLD, &whoAmI); // Set up problem printf("Setting up problem...\n"); fflush(stdout); double *A = (double*)malloc(sizeof(double)*(M*N)*(M*N)); double *b = (double*)malloc(sizeof(double)*(M*N)); double *x = (double*)malloc(sizeof(double)*(M*N)); setupProblem(A, b, M, N); memset(x, (char)0, sizeof(double)*M*N); if (0 == whoAmI) { printf("ok\n"); fflush(stdout); } // [Debug] Uncomment next line of code for printing a matrix/vector to a file. // Attention: Only do this for small matrices (i.e. M,N <= 8)! //printMatrixToFile("A.dat", A, M, N); if(0 == whoAmI) { printf("Solving...\n"); fflush(stdout); //resetTime(); } unsigned int iteration; for (iteration = 0; iteration < MAX_ITERATIONS; ) { //visualize first, to see initial solution: if(0 == whoAmI) { printf("iterations: %d\n", iteration); fflush(stdout); // visualize unsigned char *pixels; visualizeMap(x, &pixels, M*N); // save bitmap printf("Saving bitmap..."); fflush(stdout); char filename[64]; sprintf(filename, "images/heatmap%d.bmp", iteration); if (!saveBMP(filename, pixels, M, N, 0)) { printf("fail!\n"); fflush(stdout); return 1; } else { printf("ok\n"); fflush(stdout); } free(pixels); } // solve unsigned int iterationsDone = jacobi_solve(A, b, x, M*N, 10, 0.001); if (iterationsDone < 10) break; else iteration += iterationsDone; } if(0 == whoAmI) { //double timeNeededForSolving = getTime(); //printf("End of computation!\nTime needed for solving: %fs\n", timeNeededForSolving); } free(A); free(b); free(x); MPI_Finalize(); return 0; }