Example #1
0
void cRAI::ValidateAllUnits()
{
	for(map<int,UnitInfo>::iterator iU=Units.begin(); iU!=Units.end(); iU++)
	{
		if( !ValidateUnit(iU->first) )
		{
			// The iterator has becomes invalid at this point
			ValidateAllUnits();
			return;
		}
	}
}
Example #2
0
bool cRAI::ValidateUnitList(map<int,UnitInfo*>* UL)
{
	int ULsize = UL->size();
	for(map<int,UnitInfo*>::iterator iU=UL->begin(); iU!=UL->end(); iU++)
	{
		if( !ValidateUnit(iU->first) )
		{
			// The iterator has becomes invalid at this point
			if( ULsize == 1 ) // if true then the list is now empty, and may have been deleted (UL is invalid)
				return false;
			else
				return ValidateUnitList(UL);
		}
	}
	return true;
}
OGRErr OGRSpatialReference::Validate(OGR_SRSNode *poRoot)
{
    if( !EQUAL(poRoot->GetValue(),"GEOGCS")
        && !EQUAL(poRoot->GetValue(),"PROJCS")
        && !EQUAL(poRoot->GetValue(),"LOCAL_CS")
        && !EQUAL(poRoot->GetValue(),"GEOCCS")
        && !EQUAL(poRoot->GetValue(),"VERT_CS")
        && !EQUAL(poRoot->GetValue(),"COMPD_CS"))
    {
        CPLDebug( "OGRSpatialReference::Validate",
                  "Unrecognised root node `%s'\n",
                  poRoot->GetValue() );
        return OGRERR_CORRUPT_DATA;
    }

/* -------------------------------------------------------------------- */
/*      For a COMPD_CS, validate subparameters and head & tail cs       */
/* -------------------------------------------------------------------- */
    if( EQUAL(poRoot->GetValue(),"COMPD_CS") )
    {
        OGR_SRSNode     *poNode;
        int             i;

        for( i = 1; i < poRoot->GetChildCount(); i++ )
        {
            poNode = poRoot->GetChild(i);

            if( EQUAL(poNode->GetValue(),"GEOGCS") ||
                EQUAL(poNode->GetValue(),"PROJCS") ||
                EQUAL(poNode->GetValue(),"LOCAL_CS") ||
                EQUAL(poNode->GetValue(),"GEOCCS") ||
                EQUAL(poNode->GetValue(),"VERT_CS") ||
                EQUAL(poNode->GetValue(),"COMPD_CS") )
            {
                OGRErr eErr = Validate(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"AUTHORITY") )
            {
                OGRErr eErr = ValidateAuthority(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else
            {
                CPLDebug( "OGRSpatialReference::Validate",
                          "Unexpected child for COMPD_CS `%s'.\n",
                          poNode->GetValue() );

                return OGRERR_CORRUPT_DATA;
            }
        }

        return OGRERR_NONE;
    }

/* -------------------------------------------------------------------- */
/*      Validate VERT_CS                                                */
/* -------------------------------------------------------------------- */
    if( EQUAL(poRoot->GetValue(),"VERT_CS") )
    {
        OGR_SRSNode     *poNode;
        int             i;
        int             bGotVertDatum = FALSE;
        int             bGotUnit = FALSE;
        int             nCountAxis = 0;

        for( i = 1; i < poRoot->GetChildCount(); i++ )
        {
            poNode = poRoot->GetChild(i);

            if( EQUAL(poNode->GetValue(),"VERT_DATUM") )
            {
                OGRErr eErr = ValidateVertDatum(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
                bGotVertDatum = TRUE;
            }
            else if( EQUAL(poNode->GetValue(),"UNIT") )
            {
                OGRErr eErr = ValidateUnit(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
                bGotUnit = TRUE;
            }
            else if( EQUAL(poNode->GetValue(),"AXIS") )
            {
                OGRErr eErr = ValidateAxis(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
                nCountAxis ++;
            }
            else if( EQUAL(poNode->GetValue(),"AUTHORITY") )
            {
                OGRErr eErr = ValidateAuthority(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else
            {
                CPLDebug( "OGRSpatialReference::Validate",
                          "Unexpected child for VERT_CS `%s'.\n",
                          poNode->GetValue() );

                return OGRERR_CORRUPT_DATA;
            }
        }

        if (!bGotVertDatum)
        {
            CPLDebug( "OGRSpatialReference::Validate",
                      "No VERT_DATUM child in VERT_CS.\n" );

            return OGRERR_CORRUPT_DATA;
        }

        if (!bGotUnit)
        {
            CPLDebug( "OGRSpatialReference::Validate",
                      "No UNIT child in VERT_CS.\n" );

            return OGRERR_CORRUPT_DATA;
        }

        if (nCountAxis > 1)
        {
            CPLDebug( "OGRSpatialReference::Validate",
                      "Too many AXIS children in VERT_CS.\n" );

            return OGRERR_CORRUPT_DATA;
        }
        return OGRERR_NONE;
    }

/* -------------------------------------------------------------------- */
/*      For a PROJCS, validate subparameters (other than GEOGCS).       */
/* -------------------------------------------------------------------- */
    if( EQUAL(poRoot->GetValue(),"PROJCS") )
    {
        OGR_SRSNode     *poNode;
        int             i;

        for( i = 1; i < poRoot->GetChildCount(); i++ )
        {
            poNode = poRoot->GetChild(i);

            if( EQUAL(poNode->GetValue(),"GEOGCS") )
            {
                /* validated elsewhere */
            }
            else if( EQUAL(poNode->GetValue(),"UNIT") )
            {
                OGRErr eErr = ValidateUnit(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"PARAMETER") )
            {
                if( poNode->GetChildCount() != 2 )
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "PARAMETER has wrong number of children (%d),"
                              "not 2 as expected.\n",
                              poNode->GetChildCount() );
                    
                    return OGRERR_CORRUPT_DATA;
                }
                else if( CSLFindString( (char **)papszParameters,
                                        poNode->GetChild(0)->GetValue()) == -1)
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "Unrecognised PARAMETER `%s'.\n",
                              poNode->GetChild(0)->GetValue() );
                    
                    return OGRERR_UNSUPPORTED_SRS;
                }
            }
            else if( EQUAL(poNode->GetValue(),"PROJECTION") )
            {
                if( poNode->GetChildCount() != 1 && poNode->GetChildCount() != 2 )
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "PROJECTION has wrong number of children (%d),"
                              "not 1 or 2 as expected.\n",
                              poNode->GetChildCount() );
                    
                    return OGRERR_CORRUPT_DATA;
                }
                else if( CSLFindString( (char **)papszProjectionSupported,
                                        poNode->GetChild(0)->GetValue()) == -1
                      && CSLFindString( (char **)papszProjectionUnsupported,
                                        poNode->GetChild(0)->GetValue()) == -1)
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "Unrecognised PROJECTION `%s'.\n",
                              poNode->GetChild(0)->GetValue() );
                    
                    return OGRERR_UNSUPPORTED_SRS;
                }
                else if( CSLFindString( (char **)papszProjectionSupported,
                                        poNode->GetChild(0)->GetValue()) == -1)
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "Unsupported, but recognised PROJECTION `%s'.\n",
                              poNode->GetChild(0)->GetValue() );
                    
                    return OGRERR_UNSUPPORTED_SRS;
                }

                if (poNode->GetChildCount() == 2)
                {
                    poNode = poNode->GetChild(1);
                    if( EQUAL(poNode->GetValue(),"AUTHORITY") )
                    {
                        OGRErr eErr = ValidateAuthority(poNode);
                        if (eErr != OGRERR_NONE)
                            return eErr;
                    }
                    else
                    {
                        CPLDebug( "OGRSpatialReference::Validate",
                                "Unexpected child for PROJECTION `%s'.\n",
                                poNode->GetValue() );

                        return OGRERR_CORRUPT_DATA;
                    }
                }
            }
            else if( EQUAL(poNode->GetValue(),"AUTHORITY") )
            {
                OGRErr eErr = ValidateAuthority(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"AXIS") )
            {
                OGRErr eErr = ValidateAxis(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"EXTENSION") )
            {
                // We do not try to control the sub-organization of 
                // EXTENSION nodes.
            }
            else
            {
                CPLDebug( "OGRSpatialReference::Validate",
                          "Unexpected child for PROJCS `%s'.\n",
                          poNode->GetValue() );
                
                return OGRERR_CORRUPT_DATA;
            }
        }
    }
    
/* -------------------------------------------------------------------- */
/*      Validate GEOGCS if found.                                       */
/* -------------------------------------------------------------------- */
    OGR_SRSNode *poGEOGCS = poRoot->GetNode( "GEOGCS" );

    if( poGEOGCS != NULL )
    {
        OGR_SRSNode     *poNode;
        int             i;

        for( i = 1; i < poGEOGCS->GetChildCount(); i++ )
        {
            poNode = poGEOGCS->GetChild(i);

            if( EQUAL(poNode->GetValue(),"DATUM") )
            {
                /* validated elsewhere */
            }
            else if( EQUAL(poNode->GetValue(),"PRIMEM") )
            {
                if( poNode->GetChildCount() < 2 
                    || poNode->GetChildCount() > 3 )
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "PRIMEM has wrong number of children (%d),"
                              "not 2 or 3 as expected.\n",
                              poNode->GetChildCount() );
                    
                    return OGRERR_CORRUPT_DATA;
                }
            }
            else if( EQUAL(poNode->GetValue(),"UNIT") )
            {
                OGRErr eErr = ValidateUnit(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"AXIS") )
            {
                OGRErr eErr = ValidateAxis(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"AUTHORITY") )
            {
                OGRErr eErr = ValidateAuthority(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else
            {
                CPLDebug( "OGRSpatialReference::Validate",
                          "Unexpected child for GEOGCS `%s'.\n",
                          poNode->GetValue() );
                
                return OGRERR_CORRUPT_DATA;
            }
        }

        if( poGEOGCS->GetNode("DATUM") == NULL )
        {
            CPLDebug( "OGRSpatialReference::Validate",
                      "No DATUM child in GEOGCS.\n" );
            
            return OGRERR_CORRUPT_DATA;
        }
    }

/* -------------------------------------------------------------------- */
/*      Validate DATUM/SPHEROID.                                        */
/* -------------------------------------------------------------------- */
    OGR_SRSNode *poDATUM = poRoot->GetNode( "DATUM" );

    if( poDATUM != NULL )
    {
        OGR_SRSNode     *poSPHEROID;
        int             bGotSpheroid = FALSE;
        int             i;

        if( poDATUM->GetChildCount() == 0 )
        {
            CPLDebug( "OGRSpatialReference::Validate",
                      "DATUM has no children." );
            
            return OGRERR_CORRUPT_DATA;
        }

        for( i = 1; i < poDATUM->GetChildCount(); i++ )
        {
            OGR_SRSNode *poNode;
            poNode = poDATUM->GetChild(i);

            if( EQUAL(poNode->GetValue(),"SPHEROID") )
            {
                poSPHEROID = poDATUM->GetChild(1);
                bGotSpheroid = TRUE;

                if( poSPHEROID->GetChildCount() != 3 
                    && poSPHEROID->GetChildCount() != 4 )
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "SPHEROID has wrong number of children (%d),"
                              "not 3 or 4 as expected.\n",
                              poSPHEROID->GetChildCount() );
                    
                    return OGRERR_CORRUPT_DATA;
                }
                else if( CPLAtof(poSPHEROID->GetChild(1)->GetValue()) == 0.0 )
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                              "SPHEROID semi-major axis is zero (%s)!\n",
                              poSPHEROID->GetChild(1)->GetValue() );
                    return OGRERR_CORRUPT_DATA;
                }
            }
            else if( EQUAL(poNode->GetValue(),"AUTHORITY") )
            {
                OGRErr eErr = ValidateAuthority(poNode);
                if (eErr != OGRERR_NONE)
                    return eErr;
            }
            else if( EQUAL(poNode->GetValue(),"TOWGS84") )
            {
                if( poNode->GetChildCount() != 3 
                    && poNode->GetChildCount() != 7)
                {
                    CPLDebug( "OGRSpatialReference::Validate",
                   "TOWGS84 has wrong number of children (%d), not 3 or 7.\n",
                              poNode->GetChildCount() );
                    return OGRERR_CORRUPT_DATA;
                }
            }
            else
            {
                CPLDebug( "OGRSpatialReference::Validate",
                          "Unexpected child for DATUM `%s'.\n",
                          poNode->GetValue() );
                
                return OGRERR_CORRUPT_DATA;
            }
        }

        if( !bGotSpheroid )
        {
            CPLDebug( "OGRSpatialReference::Validate",
                      "No SPHEROID child in DATUM.\n" );
            
            return OGRERR_CORRUPT_DATA;
        }
    }        

/* -------------------------------------------------------------------- */
/*      If this is projected, try to validate the detailed set of       */
/*      parameters used for the projection.                             */
/* -------------------------------------------------------------------- */
    OGRErr  eErr;

    eErr = ValidateProjection(poRoot);
    if( eErr != OGRERR_NONE )
        return eErr;

/* -------------------------------------------------------------------- */
/*      Final check.                                                    */
/* -------------------------------------------------------------------- */
    if( EQUAL(poRoot->GetValue(),"GEOCCS") )
        return OGRERR_UNSUPPORTED_SRS;

    return OGRERR_NONE;
}
Example #4
0
void cRAI::Update()
{
	frame=cb->GetCurrentFrame();
	if(frame%FUPDATE_MINIMAL)
		return;

	if( RAIDEBUGGING ) *l<<"\nUpdate("<<frame<<")";

	if(!(frame%FUPDATE_POWER))
	{	// Old Code, ensures a unit won't just go permanently idle, hopefully unnecessary in the future
		ValidateAllUnits();
		for(map<int,UnitInfo>::iterator iU=Units.begin(); iU!=Units.end(); iU++)
			if( !cb->UnitBeingBuilt(iU->first) && !iU->second.AIDisabled && iU->second.udrBL->task > 1 &&
				frame > iU->second.lastUnitIdleFrame+FUPDATE_UNITS && iU->second.UE == 0 && cb->GetCurrentUnitCommands(iU->first)->size() == 0 )
			{
//				*l<<"\nWARNING: Unit was Idle  Name="<<iU->second.ud->humanName<<"("<<iU->first<<")";
				UnitIdle(iU->first);
			}
	}

	while( eventSize > 0 && eventList[0]->frame <= frame )
	{
		switch( eventList[0]->type )
		{
		case 1: // Checks for idleness
//			*l<<"\n(u1)";
			if( ValidateUnit(eventList[0]->unitID) ) // if false, the event will be removed elsewhere
			{
				if( !IsHumanControled(eventList[0]->unitID,eventList[0]->unitI) )
				{
//					*l<<" Stopping "<<eventList[0]->unitI->ud->humanName<<"("<<eventList[0]->unitID<<")";
					eventList[0]->unitI->lastUnitIdleFrame = -1;
					if( eventList[0]->unitI->ud->speed == 0 )
					{
						int unit = eventList[0]->unitID;
						UpdateEventRemove(eventList[0]);
						UnitIdle(unit);
					}
					else
					{	// doesn't seem to work for factories  Spring-Version(v0.76b1)
						Command c;
						c.id=CMD_STOP;
						cb->GiveOrder(eventList[0]->unitID, &c);
						UpdateEventRemove(eventList[0]);
					}
				}
				else
				{
					eventList[0]->frame = frame;
					UpdateEventReorderFirst();
				}
			}
			break;
		case 2: // Few Unit Monitoring
//			*l<<"\n(u2)";
			if( ValidateUnit(eventList[0]->unitID) ) // if false, the event will be removed elsewhere
			{
				if( !eventList[0]->unitI->inCombat &&
					!IsHumanControled(eventList[0]->unitID,eventList[0]->unitI) &&
					eventList[0]->unitI->BuildQ != 0 &&
					cb->GetCurrentUnitCommands(eventList[0]->unitID)->front().id < 0 )
				{
					if( eventList[0]->lastPosition == 0 )
						eventList[0]->lastPosition = new float3;
					float3 position = cb->GetUnitPos(eventList[0]->unitID);
					if( eventList[0]->unitI->BuildQ->creationID.size() > 0 )
					{
						eventList[0]->lastPosition->x = -1.0;
						float3 conPosition = cb->GetUnitPos(eventList[0]->unitI->BuildQ->creationID.front());
						if( abs(int(position.x-conPosition.x)) + 4.0 < 8.0*eventList[0]->unitI->ud->xsize/2.0 + 8.0*eventList[0]->unitI->BuildQ->creationUD->ud->xsize/2.0 &&
							abs(int(position.z-conPosition.z)) + 4.0 < 8.0*eventList[0]->unitI->ud->ysize/2.0 + 8.0*eventList[0]->unitI->BuildQ->creationUD->ud->ysize/2.0 )
						{	// most likely, the commander built something on top of himself
							Command c;
							c.id = CMD_RECLAIM;
							c.params.push_back(eventList[0]->unitI->BuildQ->creationID.front());
							cb->GiveOrder(eventList[0]->unitID,&c);
							if( B->BP->NeedResourceSite(eventList[0]->unitI->BuildQ->creationUD->ud) )
							{
								c.params.clear();
								c.id = CMD_MOVE;
								c.options = SHIFT_KEY;
								if( position.x < conPosition.x )
									c.params.push_back(position.x - ((position.x-conPosition.x)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->xsize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->xsize/2.0) );
								else
									c.params.push_back(position.x + ((position.x-conPosition.x)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->xsize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->xsize/2.0) );
								c.params.push_back(position.y);
								if( position.z < conPosition.z )
									c.params.push_back(position.z - ((position.z-conPosition.z)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->ysize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->ysize/2.0) );
								else
									c.params.push_back(position.z + ((position.z-conPosition.z)/position.distance2D(conPosition))*(8.0*eventList[0]->unitI->ud->ysize/2.0 +8.0*eventList[0]->unitI->BuildQ->creationUD->ud->ysize/2.0) );
								cb->GiveOrder(eventList[0]->unitID,&c);
							}
						}
					}
					else if( position == *eventList[0]->lastPosition )
					{	// most likely, the commander is stuck at the starting point
//						if( eventList[0]->unitI->area == 0 && TM->udMobileType.find(eventList[0]->unitI->ud->id)->second != 0 )
//						{} // trapped forever
						eventList[0]->unitI->lastUnitIdleFrame = -1;
						Command c;
						c.id = CMD_MOVE;
						float f = (40.0+(rand()%401)/10.0);
						if( rand()%2 == 0 )
							c.params.push_back(position.x + f );
						else
							c.params.push_back(position.x - f );
						c.params.push_back(position.y);
						f = (40.0+(rand()%401)/10.0);
						if( rand()%2 == 0 )
							c.params.push_back(position.z + f );
						else
							c.params.push_back(position.z - f );
						cb->GiveOrder(eventList[0]->unitID,&c);
						*eventList[0]->lastPosition = position;
					}
					else
						*eventList[0]->lastPosition = position;
				}
				if( Units.size() >= 10 )
				{
					if( eventList[0]->lastPosition != 0 )
						delete eventList[0]->lastPosition;
					UpdateEventRemove(eventList[0]);
				}
				else
					UpdateEventReorderFirst();
			}
			break;
		case 3: // Initiatization
//			*l<<"\n(u3)";
			if( frame >= 210 || ( cb->GetMetalIncome()>0 && cb->GetMetalIncome()<0.9*cb->GetMetalStorage() ) || ( cb->GetEnergyIncome()>0 && cb->GetEnergyIncome()<0.9*cb->GetEnergyStorage() ) )
			{
				*l<<"\nInitiated=true  Frame="<<frame<<" Metal-Income="<<cb->GetMetalIncome()<<" Energy-Income="<<cb->GetEnergyIncome()<<"\n";
				UpdateEventRemove(eventList[0]);
				B->UpdateUDRCost();
				for(map<int,UnitInfo>::iterator i=Units.begin(); i!=Units.end(); i++ )
					if( !i->second.AIDisabled  )
					{
						if( Units.size() < 10 && i->second.ud->movedata != 0 )
							UpdateEventAdd(2,frame+FUPDATE_MINIMAL,i->first,&i->second);
						if( cb->GetCurrentUnitCommands(i->first)->size() == 0 )
							UnitIdle(i->first);
					}
				B->bInitiated=true;
			}
			else
				UpdateEventReorderFirst();
			break;
		default:
			*l<<"(ERROR)";
			UpdateEventRemove(eventList[0]);
			break;
		}
	}

	if(!(frame%FUPDATE_POWER))
	{
		B->PM->Update();
		SWM->Update();
		if(!(frame%FUPDATE_BUILDLIST))
			B->UpdateUDRCost();
	}
	if( RAIDEBUGGING ) *l<<"#";
}