Esempio n. 1
0
void PGTaskMgr::Initialize() {

    std::for_each(m_Task.begin(), m_Task.end(), safe_delete());
    m_Task.clear();

    // build Mercator Reference Grid
    // find center of Task
    double minlat = 0.0, minlon = 0.0, maxlat = 0.0, maxlon = 0.0;
    for (int curwp = 0; ValidTaskPoint(curwp); ++curwp) {
        if (curwp == 0) {
            maxlat = minlat = WayPointList[Task[curwp].Index].Latitude;
            maxlon = minlon = WayPointList[Task[curwp].Index].Longitude;
        } else {
            minlat = std::min(minlat, WayPointList[Task[curwp].Index].Latitude);
            maxlat = std::max(maxlat, WayPointList[Task[curwp].Index].Latitude);

            minlon = std::min(minlon, WayPointList[Task[curwp].Index].Longitude);
            maxlon = std::max(maxlon, WayPointList[Task[curwp].Index].Longitude);
        }
    }

    m_Grid.lat0 = deg2rad(minlat + maxlat) * 1 / 2;
    m_Grid.lon0 = deg2rad(minlon + maxlon) * 1 / 2;
    m_Grid.k0 = 1;
    m_Grid.false_e = 0.0; // ????
    m_Grid.false_n = 0.0; // ????

    // build task point list
    for (int curwp = 0; ValidTaskPoint(curwp); ++curwp) {
        int TpType = 0;
        double Radius;
        GetTaskSectorParameter(curwp, &TpType, &Radius);
        switch (TpType) {
            case CIRCLE:
                AddCircle(curwp);
                break;
            case SECTOR:
            case DAe:
                AddSector(curwp);
                break;
            case LINE:
                AddLine(curwp);
                break;
            case CONE:
                AddCone(curwp);
                break;
            case ESS_CIRCLE:
                AddEssCircle(curwp);
                break;
        }
    }
}
Esempio n. 2
0
bool AAIBrain::ExpandBase(SectorType sectorType)
{
    if(sectors[0].size() >= cfg->MAX_BASE_SIZE)
        return false;

    // now targets should contain all neighbouring sectors that are not currently part of the base
    // only once; select the sector with most metalspots and least danger
    AAISector *best_sector = 0;
    float best_rating  = 0, my_rating;
    int spots;
    float dist;

    int max_search_dist = 1;

    // if aai is looking for a water sector to expand into ocean, allow greater search_dist
    if(sectorType == WATER_SECTOR &&  baseWaterRatio < 0.1)
        max_search_dist = 3;

    for(int search_dist = 1; search_dist <= max_search_dist; ++search_dist)
    {
        for(list<AAISector*>::iterator t = sectors[search_dist].begin(); t != sectors[search_dist].end(); ++t)
        {
            // dont expand if enemy structures in sector && check for allied buildings
            if(IsSafeSector(*t) && (*t)->allied_structures < 3 && map->team_sector_map[(*t)->x][(*t)->y] == -1)
            {
                // rate current sector
                spots = (*t)->GetNumberOfMetalSpots();

                my_rating = 1.0f + (float)spots;

                if(sectorType == LAND_SECTOR)
                    // prefer flat sectors without water
                    my_rating += ((*t)->flat_ratio - (*t)->water_ratio) * 16.0f;
                else if(sectorType == WATER_SECTOR)
                {
                    // check for continent size (to prevent aai to expand into little ponds instead of big ocean)
                    if((*t)->water_ratio > 0.1 &&  (*t)->ConnectedToOcean())
                        my_rating += 8.0f * (*t)->water_ratio;
                    else
                        my_rating = 0;
                }
                else // LAND_WATER_SECTOR
                    my_rating += ((*t)->flat_ratio + (*t)->water_ratio) * 8.0f;

                // minmize distance between sectors
                dist = 0.1f;

                for(list<AAISector*>::iterator sector = sectors[0].begin(); sector != sectors[0].end(); ++sector)
                    dist += fastmath::sqrt( ((*t)->x - (*sector)->x) * ((*t)->x - (*sector)->x) + ((*t)->y - (*sector)->y) * ((*t)->y - (*sector)->y) );

                my_rating /= (dist * fastmath::sqrt(map->GetEdgeDistance( &(*t)->GetCenter() )) );

                // choose higher rated sector
                if(my_rating > best_rating)
                {
                    best_rating = my_rating;
                    best_sector = *t;
                }
            }
        }
    }

    if(best_sector)
    {
        // add this sector to base
        AddSector(best_sector);

        // debug purposes:
        if(sectorType == LAND_SECTOR)
        {
            fprintf(ai->file, "\nAdding land sector %i,%i to base; base size: %i", best_sector->x, best_sector->y, sectors[0].size());
            fprintf(ai->file, "\nNew land : water ratio within base: %f : %f\n\n", baseLandRatio, baseWaterRatio);
        }
        else
        {
            fprintf(ai->file, "\nAdding water sector %i,%i to base; base size: %i", best_sector->x, best_sector->y, sectors[0].size());
            fprintf(ai->file, "\nNew land : water ratio within base: %f : %f\n\n", baseLandRatio, baseWaterRatio);
        }

        // update neighbouring sectors
        UpdateNeighbouringSectors();
        UpdateBaseCenter();

        // check if further expansion possible
        if(sectors[0].size() == cfg->MAX_BASE_SIZE)
            expandable = false;

        freeBaseSpots = true;

        return true;
    }


    return false;
}
Esempio n. 3
0
BOOL CAirportsManager::LoadData( const CString& _strDBPath )
{
	Clear();
	m_strDBPath = _strDBPath;
//	ReadCommonAirports(m_strCommonDBPath) ;
	CFile* pFile = NULL;
	CString sFileName = _strDBPath + "\\" + _strAirportFileName;
	BOOL bRet = TRUE;
	// airport file
	try
	{
		pFile = new CFile(sFileName, CFile::modeRead | CFile::shareDenyNone);
		CArchive ar(pFile, CArchive::load);
		char line[1024];
		//skip a line
		CString csLine;
		ar.ReadString( csLine );
		//read line 2
		ar.ReadString( csLine );
		csLine.MakeUpper();
		strcpy( line, csLine );
		if(_stricmp(line, "AIRPORTS DATABASE") == 0)
		{
			//skip the column names
			ar.ReadString( csLine );
			//read the values
			//read a line
			ar.ReadString( csLine );
			csLine.MakeUpper();
			strcpy( line, csLine );
			while( *line != '\0' )
			{
				CAirport* pAP = new CAirport();
				char* b = line;
				char* p = NULL;
				int c = 1;
				while((p = strchr(b, ',')) != NULL)
				{
					*p = '\0';
					switch(c)
					{
					case 1: //IATA code
						pAP->m_sIATACode = b;
						break;
					case 2: //Desc
						pAP->m_sLongName = b;
						break;
                    case 3: // ICAO code...
                        pAP->m_sICAOCode = b;
                        break;
                    case 4: // ARP coordinates...
                        pAP->m_sARPCoordinates = b;
                        break;
                    case 5: // City...
                        pAP->m_sCity = b;
                        break;
                    case 6: // Country...
                        pAP->m_sCountry = b;
                        break;
                    case 7: // Country code...
                        pAP->m_sCountryCode = b;
                        break;
                    case 8: // Elevation...
                        pAP->m_sElevation = b;
                        break;
                    case 9: // Number of runways...
                        pAP->m_sNumberOfRunways = b;
                        break;
                    case 10: // Maximum runway length...
                        pAP->m_sMaximumRunwayLength = b;
                        break;
                    case 11: // Alternate airport ICAO code...
                        pAP->m_sAlternateAirportICAOCode = b;
                        break;
					default:
						CString tmp = b;
						break;
					}
					b = ++p;
					c++;
				}
				if(b!= NULL)
				{
                    pAP->m_sAlternateAirportICAOCode = b;
				}
				if(!IsExist(pAP))
				  m_vAirports.push_back(pAP);
				else
					delete pAP ;
				ar.ReadString( csLine );
				csLine.MakeUpper();
				strcpy( line, csLine );
			}
		}
		else {
			bRet = FALSE;
		}
		ar.Close();
		delete pFile;
	}
	catch(CException* e)
	{
		AfxMessageBox("Error reading Airports file: " + sFileName, MB_ICONEXCLAMATION | MB_OK);
		e->Delete();
		if(pFile != NULL)
			delete pFile;
		bRet = FALSE;
	}

	// sector file
	pFile = NULL;
	sFileName = _strDBPath + "\\" + _strSectorFileName;
	try
	{
		pFile = new CFile(sFileName, CFile::modeRead | CFile::shareDenyNone);
		CArchive ar(pFile, CArchive::load);
		char line[2048];
		CString csLine;
		ar.ReadString( csLine );
		//read line 2
		ar.ReadString( csLine );
		csLine.MakeUpper();
		strcpy( line, csLine );
		if(_stricmp(line, "SECTORS DATABASE") == 0)
		{
			//skip the column names
			ar.ReadString( csLine );
			//read the values
			//read a line
			ar.ReadString( csLine );
			csLine.MakeUpper();
			strcpy( line, csLine );
			while( *line != '\0')
			{
				//CSector* pS = new CSector();
				CString sSectorName;
				CString sAirport;
				char* b = line;
				char* p = NULL;
				int c = 1;
				while((p = strchr(b, ',')) != NULL)
				{
					*p = '\0';
					switch(c)
					{
					case 1: //name
						sSectorName = b;
						break;
					case 2: //airport(s)
						sAirport = b;
						break;
					default: //more airports
						CString tmp = b;
						sAirport = sAirport + "," + tmp;
						break;
					}
					b = ++p;
					c++;
				}
				if(b!=NULL) // the last column did not have a comma after it
				{
					switch(c)
					{
					case 1: //name
						sSectorName = b;
						break;
					case 2: //airport(s)
						sAirport = b;
						break;
					default: //more airports
						CString tmp = b;
						sAirport = sAirport + "," + tmp;
						break;
					}
				}
				CAIRPORTFILTER apFilter;
				while(TRUE) {
					int nIdx = sAirport.Find(',');
					if(nIdx != -1) { // found a comma
						apFilter.sIATACode = sAirport.Left(nIdx);
						AddSector(sSectorName, apFilter);
						sAirport = sAirport.Right(sAirport.GetLength()-nIdx-1);
					}
					else if(!sAirport.IsEmpty()){ // no comma, but string not empty
						apFilter.sIATACode = sAirport;
						AddSector(sSectorName, apFilter);
						break;
					}
					else { // no comma, string empty
						break;
					}
				}
				ar.ReadString( csLine );
				csLine.MakeUpper();
				strcpy( line, csLine );
			}
		}
		else
			bRet = FALSE;
		ar.Close();
		delete pFile;
	}
	catch(CException* e) {
		AfxMessageBox("Error reading Sectors file: " + sFileName, MB_ICONEXCLAMATION | MB_OK);
		e->Delete();
		if(pFile != NULL)
			delete pFile;
		bRet = FALSE;
	}
	return bRet;
}