Пример #1
0
void TimerTick() 
{
    ChickenCheck();
    Delay++;
	if(TakeNextTP)
	{
		if(IsTownLevel(GetPlayerArea()))
		TakeNextTP=false;
	}
	if(CampOn)
	{
		D2CLIENT_clickMap(3,Camp.x,Camp.y,8);
	}
	if(CampUnitOn)
	{
		D2CLIENT_clickMap(3,Camp.x,Camp.y,8);
	}
	if(Flash)
	{
		if(FindUnit(Auto.GetPlayerID(),0)!=NULL)
		{
			FlashPlayer(Auto.GetPlayerID());						
			FlashDelay=0;
		}
		if(FindUnit(Auto.GetPlayerID(),0)==NULL)
		{
			FlashDelay=-1;
		}
	}
}
Пример #2
0
void CAirScript::Update()
{
	std::deque<int>::iterator pi;
	int num=0;
	for(pi=planes.begin();pi!=planes.end();++pi){
		if(uh->units[*pi]==0){
			CUnit* u;
			if(!(num&1))
				u=unitLoader.LoadUnit(FindUnit(armhawk_name),float3(1000+(num&1)*5000,500,2100+num*120),(num&1),false,0,NULL);
			else
				u=unitLoader.LoadUnit(FindUnit(corvamp_name),float3(1000+(num&1)*5000,500,2100+num*120),(num&1),false,0,NULL);
			u->pos.y=ground->GetHeight(1000+(num&1)*5000,2100+num*120)+350;
			u->experience=0.3f;
			u->speed.x=2.8f;
			((CAirMoveType*)u->moveType)->SetState(AAirMoveType::AIRCRAFT_FLYING);
			*pi=u->id;

			Command c;
			c.id=CMD_PATROL;
			c.options=0;
			c.params.push_back(7000-(num&1)*6500);
			c.params.push_back(200);
			c.params.push_back(2500+num*60);

			Command c2;
			c2.id=CMD_MOVE_STATE;
			c2.params.push_back(1);
			u->commandAI->GiveCommand(c2);
			u->commandAI->GiveCommand(c);
		}
		num++;
	}
}
Пример #3
0
UnitAny* GetPlayer() {
UnitAny* pUnit=(UnitAny*)FindUnit(Auto.GetPlayerID(),0);
	if(pUnit==NULL) {
		Auto.Init();
	}

	return (UnitAny*)FindUnit(Auto.GetPlayerID(),0);
}
Пример #4
0
//=================================================================================================
// only remove alive units for now
bool Game::RemoveUnit(LevelAreaContext* lac, Unit* unit)
{
	assert(lac && unit);

	lac->AddRef();
	LevelAreaContext::Entry* entry;
	int unit_index;
	if(FindUnit(lac, unit, &entry, &unit_index))
	{
		if(entry->active)
		{
			unit->to_remove = true;
			to_remove.push_back(unit);
		}
		else
			RemoveElementIndex(entry->area->units, unit_index);
		lac->Free();
		return true;
	}
	else
	{
		lac->Free();
		return false;
	}
}
Пример #5
0
void CAirScript::GameStart()
{
	doRoll=!configHandler.Get("ReflectiveWater",1);

	ENTER_MIXED;
	tcp=camera->pos;
	tcf=camera->forward;
	ENTER_SYNCED;
	for(int a=0;a<10;++a){
		CUnit* u;
		if(gs->randFloat()<0.5f)
			u=unitLoader.LoadUnit(FindUnit(armhawk_name),float3(1650,300,2100+a*150),0,false,0,NULL);
		else
			u=unitLoader.LoadUnit(FindUnit(armfig_name),float3(1650,300,2100+a*150),0,false,0,NULL);
		u->pos.y=350;
		u->experience=0.3f;
		((CAirMoveType*)u->moveType)->SetState(AAirMoveType::AIRCRAFT_FLYING);
		planes.push_back(u->id);
		Command c2;
		c2.id=CMD_MOVE_STATE;
		c2.params.push_back(1);
		u->commandAI->GiveCommand(c2);

		Command c;
		c.id=CMD_PATROL;
		c.options=0;
		c.params.push_back(6570);
		c.params.push_back(0);
		c.params.push_back(2560);
		u->commandAI->GiveCommand(c);

		if(gs->randFloat()<0.5f){
			u=unitLoader.LoadUnit(FindUnit(corvamp_name),float3(3880,300,2100+a*150),1,false,0,NULL);
			((CAirMoveType*)u->moveType)->SetState(AAirMoveType::AIRCRAFT_FLYING);
		}else{
			u=unitLoader.LoadUnit(FindUnit(corape_name),float3(3880,300,2100+a*150),1,false,0,NULL);
		}
		u->pos.y=350;
		u->experience=0.3f;
		planes.push_back(u->id);
		u->commandAI->GiveCommand(c2);
		c.params[0]=500;
		u->commandAI->GiveCommand(c);
	}
}
Пример #6
0
	// data array 할당
	static bool SetDataArray(MkHashMap<MkHashStr, MkDataUnitInterface*>& unitTable, const MkHashStr& key, const MkArray<DataType>& values)
	{
		MkDataUnit<DataType>* unitPtr = FindUnit(unitTable, key);
		bool ok = (unitPtr != NULL);
		if (ok)
		{
			*unitPtr = values;
		}
		return ok;
	}
Пример #7
0
	// single data 할당
	static bool SetSingleData(MkHashMap<MkHashStr, MkDataUnitInterface*>& unitTable, const MkHashStr& key, const DataType& value, unsigned int index)
	{
		MkDataUnit<DataType>* unitPtr = FindUnit(unitTable, key);
		bool ok = (unitPtr != NULL);
		if (ok)
		{
			ok = unitPtr->SetValue(value, index);
		}
		return ok;
	}
Пример #8
0
	// data array 반환
	static bool GetDataArray(const MkHashMap<MkHashStr, MkDataUnitInterface*>& unitTable, const MkHashStr& key, MkArray<DataType>& buffers)
	{
		MkDataUnit<DataType>* unitPtr = FindUnit(unitTable, key);
		bool ok = (unitPtr != NULL);
		if (ok)
		{
			buffers = unitPtr->GetArray();
		}
		return ok;
	}
Пример #9
0
Unit BrigadeClass::GetNextUnitElement (void)
	{
	c_element++;
	while (c_element < elements)
		{
		if (element[c_element])
			return FindUnit(element[c_element]);
		c_element++;
		}
	c_element=0;
	return NULL;
	}
Пример #10
0
int FalconDivertMessage::Process(uchar autodisp)
{
    Flight		flight = (Flight)FindUnit(EntityId());
    CampEntity	target = NULL;

    if (autodisp || !flight || !TheCampaign.IsLoaded())
        return 0;

    if (dataBlock.mission > 0)
    {
        // Check for target viability
        target = (CampEntity)vuDatabase->Find(dataBlock.targetID);
        if (target && target->IsUnit() && ((Unit)target)->Father())
            target = ((Unit)target)->GetFirstUnitElement();
        if ((!target || (target->IsUnit() && ((UnitClass*)target)->IsDead())) && dataBlock.mission > 0)
            return 0;
        // Set with new element's ID
        dataBlock.targetID = target->Id();

        // Make radio call
        PlayDivertRadioCalls(target,dataBlock.mission,flight, 0);

        // Return receipt
        if (flight->IsLocal() && (dataBlock.flags & REQF_NEEDRESPONSE))
        {
            CampEntity	e = (CampEntity) vuDatabase->Find(dataBlock.requesterID);
            if (e->IsUnit())
                ((Unit)e)->SendUnitMessage(dataBlock.targetID,FalconUnitMessage::unitRequestMet,dataBlock.mission,flight->GetTeam(),0);
        }

        // KCK: This is kinda hackish - Basically, for player leads, keep reposting this message
        // (every few seconds) until the player replies.
        if (flight->GetComponentLead() == FalconLocalSession->GetPlayerEntity() && flight == FalconLocalSession->GetPlayerFlight())
        {
            // Trying to track down a potential bug here.. It's hard enough to
            // get diverts I figure I'll let QA do the testing..
            // ShiAssert (!"Show this to Kevin K.");
            memcpy(&sLastDivert.dataBlock,&dataBlock,sizeof(dataBlock));
            sDivertFlight = flight->Id();
            sLastReply = DIVERT_WAIT_FOR_REPLY;
            sNextRepost = vuxGameTime + RESEND_DIVERT_TIME * CampaignSeconds;
            flight->SetDiverted(1);		// Set diverted to prevent additional calls
            return 0;
        }

        // Apply the divert
        ApplyDivert (flight, this);
    }
    else
        PlayDivertRadioCalls(NULL,DIVERT_DENIGNED,flight,0);

    return 1;
}
Пример #11
0
/*****************************************************************************
 *
 * GetUnit
 *
 *****************************************************************************/
struct DevUnit *GetUnit (ULONG unit_num, struct MyBase *base) {
struct DevUnit *unit;

    unit = FindUnit (unit_num, base);

    if (unit == NULL) {
        Debug ("\n Unit not found, creating new");
        unit = CreateUnit (unit_num, base);    
    
        if (unit != NULL) {
            Debug ("\n Adding unit to base->units list");
            AddTail ((APTR)&(base->units), (APTR)unit);
        }
    }
   


   return unit;
}
Пример #12
0
double UnitEdit::GetValue(BOOL *err)
{
	double Value;
	char Extra[20]="";
	int error=0;
	char *temp=GetString();
	if (sscanf(temp,"%lf%s",&Value,Extra)<=0) error=U_INVALIDNUMBER;
	// search for Extra modifier
	if (UnitID==U_FIXED)
	{
		if (*Extra && strcmp(Extra,Units)) error=U_WRONGUNITS;
	}
	else
	{
		if (!FindUnit(Extra,Value)) error=U_WRONGUNITS;
	}

	if (err) *err=error;
	delete[] temp;
	return Value;
}
Пример #13
0
void BrigadeClass::RemoveChild (VU_ID	eid)
	{
	int		i=0,j;
	Unit	e;

	while (i < elements)
		{
		if (element[i] == eid)
			{
			for (j=i; j < MAX_UNIT_CHILDREN-1; j++)
				{
				element[j] = element[j+1];
				e = FindUnit(element[j]);
//				if (e)
//					((Battalion)e)->SetUnitElement(j);
				}
			element[j] = FalconNullId;
			elements--;
			}
		else
			i++;
		}
	}
void SCH_REFERENCE_LIST::Annotate( bool aUseSheetNum, int aSheetIntervalId, int aStartNumber,
      SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap )
{
    if ( componentFlatList.size() == 0 )
        return;

    int LastReferenceNumber = 0;
    int NumberOfUnits, Unit;

    /* calculate index of the first component with the same reference prefix
     * than the current component.  All components having the same reference
     * prefix will receive a reference number with consecutive values:
     * IC .. will be set to IC4, IC4, IC5 ...
     */
    unsigned first = 0;

    // calculate the last used number for this reference prefix:
#ifdef USE_OLD_ALGO
    int minRefId = 0;

    // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
    if( aUseSheetNum )
        minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId;

    LastReferenceNumber = GetLastReference( first, minRefId );
#else
    int minRefId;

    // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
    if( aUseSheetNum )
        minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId + 1;
    else
        minRefId = aStartNumber + 1;

    // For multi units components, when "keep order of multi unit" option is selected,
    // store the list of already used full references.
    // The algorithm try to allocate the new reference to components having the same
    // old reference.
    // This algo works fine as long as the previous annotation has no duplicates.
    // But when a hierarchy is reannotated with this option, the previous anotation can
    // have duplicate references, and obviously we must fix these duplicate.
    // therefore do not try to allocate a full reference more than once when trying
    // to keep this order of multi units.
    // inUseRefs keep trace of previously allocated references
    std::unordered_set<wxString> inUseRefs;

    // This is the list of all Id already in use for a given reference prefix.
    // Will be refilled for each new reference prefix.
    std::vector<int>idList;
    GetRefsInUse( first, idList, minRefId );
#endif
    for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
    {
        if( componentFlatList[ii].m_Flag )
            continue;

        // Check whether this component is in aLockedUnitMap.
        SCH_REFERENCE_LIST* lockedList = NULL;
        for( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair : aLockedUnitMap )
        {
            unsigned n_refs = pair.second.GetCount();
            for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
            {
                SCH_REFERENCE &thisRef = pair.second[thisRefI];

                if( thisRef.IsSameInstance( componentFlatList[ii] ) )
                {
                    lockedList = &pair.second;
                    break;
                }
            }
            if( lockedList != NULL ) break;
        }

        if(  ( componentFlatList[first].CompareRef( componentFlatList[ii] ) != 0 )
          || ( aUseSheetNum && ( componentFlatList[first].m_SheetNum != componentFlatList[ii].m_SheetNum ) )  )
        {
            // New reference found: we need a new ref number for this reference
            first = ii;
#ifdef USE_OLD_ALGO
            minRefId = 0;

            // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
            if( aUseSheetNum )
                minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId;

            LastReferenceNumber = GetLastReference( ii, minRefId );

#else
            // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
            if( aUseSheetNum )
                minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId + 1;
            else
                minRefId = aStartNumber + 1;

            GetRefsInUse( first, idList, minRefId );
#endif
        }

        // Annotation of one part per package components (trivial case).
        if( componentFlatList[ii].GetLibPart()->GetUnitCount() <= 1 )
        {
            if( componentFlatList[ii].m_IsNew )
            {
#ifdef USE_OLD_ALGO
                LastReferenceNumber++;
#else
                LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
                componentFlatList[ii].m_NumRef = LastReferenceNumber;
            }

            componentFlatList[ii].m_Unit  = 1;
            componentFlatList[ii].m_Flag  = 1;
            componentFlatList[ii].m_IsNew = false;
            continue;
        }

        // Annotation of multi-unit parts ( n units per part ) (complex case)
        NumberOfUnits = componentFlatList[ii].GetLibPart()->GetUnitCount();

        if( componentFlatList[ii].m_IsNew )
        {
#ifdef USE_OLD_ALGO
            LastReferenceNumber++;
#else
            LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
            componentFlatList[ii].m_NumRef = LastReferenceNumber;

            if( !componentFlatList[ii].IsUnitsLocked() )
                componentFlatList[ii].m_Unit = 1;

            componentFlatList[ii].m_Flag = 1;
        }

        // If this component is in aLockedUnitMap, copy the annotation to all
        // components that are not it
        if( lockedList != NULL )
        {
            unsigned n_refs = lockedList->GetCount();

            for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
            {
                SCH_REFERENCE &thisRef = (*lockedList)[thisRefI];

                if( thisRef.IsSameInstance( componentFlatList[ii] ) )
                {
                    // This is the component we're currently annotating. Hold the unit!
                    componentFlatList[ii].m_Unit = thisRef.m_Unit;
                }

                if( thisRef.CompareValue( componentFlatList[ii] ) != 0 )
                    continue;

                if( thisRef.CompareLibName( componentFlatList[ii] ) != 0 )
                    continue;

                // Find the matching component
                for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
                {
                    if( ! thisRef.IsSameInstance( componentFlatList[jj] ) )
                        continue;

                    wxString ref_candidate = buildFullReference( componentFlatList[ii] );

                    // propagate the new reference and unit selection to the "old" component,
                    // if this new full reference is not already used (can happens when initial
                    // multiunits components have duplicate references)
                    if( inUseRefs.find( ref_candidate ) == inUseRefs.end() )
                    {
                        componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
                        componentFlatList[jj].m_Unit = thisRef.m_Unit;
                        componentFlatList[jj].m_IsNew = false;
                        componentFlatList[jj].m_Flag = 1;
                        // lock this new full reference
                        inUseRefs.insert( ref_candidate );
                        break;
                    }
                }
            }
        }

        else
        {
            /* search for others units of this component.
            * we search for others parts that have the same value and the same
            * reference prefix (ref without ref number)
            */
            for( Unit = 1; Unit <= NumberOfUnits; Unit++ )
            {
                if( componentFlatList[ii].m_Unit == Unit )
                    continue;

                int found = FindUnit( ii, Unit );

                if( found >= 0 )
                    continue; // this unit exists for this reference (unit already annotated)

                // Search a component to annotate ( same prefix, same value, not annotated)
                for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
                {
                    if( componentFlatList[jj].m_Flag )    // already tested
                        continue;

                    if( componentFlatList[ii].CompareRef( componentFlatList[jj] ) != 0 )
                        continue;

                    if( componentFlatList[jj].CompareValue( componentFlatList[ii] ) != 0 )
                        continue;

                    if( componentFlatList[jj].CompareLibName( componentFlatList[ii] ) != 0 )
                        continue;

                    if( !componentFlatList[jj].m_IsNew )
                        continue;

                    // Component without reference number found, annotate it if possible
                    if( !componentFlatList[jj].IsUnitsLocked()
                        || ( componentFlatList[jj].m_Unit == Unit ) )
                    {
                        componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
                        componentFlatList[jj].m_Unit   = Unit;
                        componentFlatList[jj].m_Flag   = 1;
                        componentFlatList[jj].m_IsNew  = false;
                        break;
                    }
                }
            }
        }
    }
}
void SCH_REFERENCE_LIST::Annotate( bool aUseSheetNum, int aSheetIntervalId  )
{
    if ( componentFlatList.size() == 0 )
        return;

    int LastReferenceNumber = 0;
    int NumberOfUnits, Unit;

    /* Components with an invisible reference (power...) always are re-annotated. */
    ResetHiddenReferences();

    /* calculate index of the first component with the same reference prefix
     * than the current component.  All components having the same reference
     * prefix will receive a reference number with consecutive values:
     * IC .. will be set to IC4, IC4, IC5 ...
     */
    unsigned first = 0;

    /* calculate the last used number for this reference prefix: */
#ifdef USE_OLD_ALGO
    int minRefId = 0;

    // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
    if( aUseSheetNum )
        minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId;

    LastReferenceNumber = GetLastReference( first, minRefId );
#else
    int minRefId = 1;

    // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
    if( aUseSheetNum )
        minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId + 1;

    // This is the list of all Id already in use for a given reference prefix.
    // Will be refilled for each new reference prefix.
    std::vector<int>idList;
    GetRefsInUse( first, idList, minRefId );
#endif
    for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
    {
        if( componentFlatList[ii].m_Flag )
            continue;

        if(  ( componentFlatList[first].CompareRef( componentFlatList[ii] ) != 0 )
          || ( aUseSheetNum && ( componentFlatList[first].m_SheetNum != componentFlatList[ii].m_SheetNum ) )  )
        {
            /* New reference found: we need a new ref number for this reference */
            first = ii;
#ifdef USE_OLD_ALGO
            minRefId = 0;

            // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
            if( aUseSheetNum )
                minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId;

            LastReferenceNumber = componentFlatList.GetLastReference( ii, minRefId );
#else
            minRefId = 1;

            // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
            if( aUseSheetNum )
                minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId + 1;

            GetRefsInUse( first, idList, minRefId );
#endif
        }

        // Annotation of one part per package components (trivial case).
        if( componentFlatList[ii].GetLibComponent()->GetPartCount() <= 1 )
        {
            if( componentFlatList[ii].m_IsNew )
            {
#ifdef USE_OLD_ALGO
                LastReferenceNumber++;
#else
                LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
                componentFlatList[ii].m_NumRef = LastReferenceNumber;
            }

            componentFlatList[ii].m_Unit  = 1;
            componentFlatList[ii].m_Flag  = 1;
            componentFlatList[ii].m_IsNew = false;
            continue;
        }

        /* Annotation of multi-part components ( n parts per package ) (complex case) */
        NumberOfUnits = componentFlatList[ii].GetLibComponent()->GetPartCount();

        if( componentFlatList[ii].m_IsNew )
        {
#ifdef USE_OLD_ALGO
            LastReferenceNumber++;
#else
            LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
            componentFlatList[ii].m_NumRef = LastReferenceNumber;

            if( !componentFlatList[ii].IsPartsLocked() )
                componentFlatList[ii].m_Unit = 1;

            componentFlatList[ii].m_Flag = 1;
        }

        /* search for others units of this component.
         * we search for others parts that have the same value and the same
         * reference prefix (ref without ref number)
         */
        for( Unit = 1; Unit <= NumberOfUnits; Unit++ )
        {
            if( componentFlatList[ii].m_Unit == Unit )
                continue;

            int found = FindUnit( ii, Unit );

            if( found >= 0 )
                continue; /* this unit exists for this reference (unit already annotated) */

            /* Search a component to annotate ( same prefix, same value, not annotated) */
            for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
            {
                if( componentFlatList[jj].m_Flag )    // already tested
                    continue;

                if( componentFlatList[ii].CompareRef( componentFlatList[jj] ) != 0 )
                    continue;

                if( componentFlatList[jj].CompareValue( componentFlatList[ii] ) != 0 )
                    continue;

                if( componentFlatList[jj].CompareLibName( componentFlatList[ii] ) != 0 )
                    continue;

                if( !componentFlatList[jj].m_IsNew )
                    continue;

                /* Component without reference number found, annotate it if possible */
                if( !componentFlatList[jj].IsPartsLocked()
                    || ( componentFlatList[jj].m_Unit == Unit ) )
                {
                    componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
                    componentFlatList[jj].m_Unit   = Unit;
                    componentFlatList[jj].m_Flag   = 1;
                    componentFlatList[jj].m_IsNew  = false;
                    break;
                }
            }
        }
    }
}
Пример #16
0
Unit BrigadeClass::GetFirstUnitElement (void)
	{
	c_element = 0;
	return FindUnit(element[c_element]);
	}
Пример #17
0
	// single data 반환
	static bool GetSingleData(const MkHashMap<MkHashStr, MkDataUnitInterface*>& unitTable, const MkHashStr& key, DataType& buffer, unsigned int index)
	{
		MkDataUnit<DataType>* unitPtr = FindUnit(unitTable, key);
		return (unitPtr != NULL) ? unitPtr->GetValue(buffer, index) : false;
	}
void SCH_REFERENCE_LIST::Annotate( bool aUseSheetNum, int aSheetIntervalId,
      SCH_MULTI_UNIT_REFERENCE_MAP aLockedUnitMap )
{
    if ( componentFlatList.size() == 0 )
        return;

    int LastReferenceNumber = 0;
    int NumberOfUnits, Unit;

    // Components with an invisible reference (power...) always are re-annotated.
    ResetHiddenReferences();

    /* calculate index of the first component with the same reference prefix
     * than the current component.  All components having the same reference
     * prefix will receive a reference number with consecutive values:
     * IC .. will be set to IC4, IC4, IC5 ...
     */
    unsigned first = 0;

    // calculate the last used number for this reference prefix:
#ifdef USE_OLD_ALGO
    int minRefId = 0;

    // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
    if( aUseSheetNum )
        minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId;

    LastReferenceNumber = GetLastReference( first, minRefId );
#else
    int minRefId = 1;

    // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
    if( aUseSheetNum )
        minRefId = componentFlatList[first].m_SheetNum * aSheetIntervalId + 1;

    // This is the list of all Id already in use for a given reference prefix.
    // Will be refilled for each new reference prefix.
    std::vector<int>idList;
    GetRefsInUse( first, idList, minRefId );
#endif
    for( unsigned ii = 0; ii < componentFlatList.size(); ii++ )
    {
        if( componentFlatList[ii].m_Flag )
            continue;

        // Check whether this component is in aLockedUnitMap.
        SCH_REFERENCE_LIST* lockedList = NULL;
        BOOST_FOREACH( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair, aLockedUnitMap )
        {
            unsigned n_refs = pair.second.GetCount();
            for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
            {
                SCH_REFERENCE &thisRef = pair.second[thisRefI];

                if( thisRef.IsSameInstance( componentFlatList[ii] ) )
                {
                    lockedList = &pair.second;
                    break;
                }
            }
            if( lockedList != NULL ) break;
        }

        if(  ( componentFlatList[first].CompareRef( componentFlatList[ii] ) != 0 )
          || ( aUseSheetNum && ( componentFlatList[first].m_SheetNum != componentFlatList[ii].m_SheetNum ) )  )
        {
            // New reference found: we need a new ref number for this reference
            first = ii;
#ifdef USE_OLD_ALGO
            minRefId = 0;

            // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
            if( aUseSheetNum )
                minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId;

            LastReferenceNumber = componentFlatList.GetLastReference( ii, minRefId );
#else
            minRefId = 1;

            // when using sheet number, ensure ref number >= sheet number* aSheetIntervalId
            if( aUseSheetNum )
                minRefId = componentFlatList[ii].m_SheetNum * aSheetIntervalId + 1;

            GetRefsInUse( first, idList, minRefId );
#endif
        }

        // Annotation of one part per package components (trivial case).
        if( componentFlatList[ii].GetLibComponent()->GetUnitCount() <= 1 )
        {
            if( componentFlatList[ii].m_IsNew )
            {
#ifdef USE_OLD_ALGO
                LastReferenceNumber++;
#else
                LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
                componentFlatList[ii].m_NumRef = LastReferenceNumber;
            }

            componentFlatList[ii].m_Unit  = 1;
            componentFlatList[ii].m_Flag  = 1;
            componentFlatList[ii].m_IsNew = false;
            continue;
        }

        // Annotation of multi-unit parts ( n units per part ) (complex case)
        NumberOfUnits = componentFlatList[ii].GetLibComponent()->GetUnitCount();

        if( componentFlatList[ii].m_IsNew )
        {
#ifdef USE_OLD_ALGO
            LastReferenceNumber++;
#else
            LastReferenceNumber = CreateFirstFreeRefId( idList, minRefId );
#endif
            componentFlatList[ii].m_NumRef = LastReferenceNumber;

            if( !componentFlatList[ii].IsUnitsLocked() )
                componentFlatList[ii].m_Unit = 1;

            componentFlatList[ii].m_Flag = 1;
        }

        // If this component is in aLockedUnitMap, copy the annotation to all
        // components that are not it
        if( lockedList != NULL )
        {
            unsigned n_refs = lockedList->GetCount();
            for( unsigned thisRefI = 0; thisRefI < n_refs; ++thisRefI )
            {
                SCH_REFERENCE &thisRef = (*lockedList)[thisRefI];
                if( thisRef.IsSameInstance( componentFlatList[ii] ) )
                {
                    // This is the component we're currently annotating. Hold the unit!
                    componentFlatList[ii].m_Unit = thisRef.m_Unit;
                }

                if( thisRef.CompareValue( componentFlatList[ii] ) != 0 ) continue;
                if( thisRef.CompareLibName( componentFlatList[ii] ) != 0 ) continue;

                // Find the matching component
                for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
                {
                    if( ! thisRef.IsSameInstance( componentFlatList[jj] ) ) continue;
                    componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
                    componentFlatList[jj].m_Unit = thisRef.m_Unit;
                    componentFlatList[jj].m_IsNew = false;
                    componentFlatList[jj].m_Flag = 1;
                    break;
                }
            }
        }

        else
        {
            /* search for others units of this component.
            * we search for others parts that have the same value and the same
            * reference prefix (ref without ref number)
            */
            for( Unit = 1; Unit <= NumberOfUnits; Unit++ )
            {
                if( componentFlatList[ii].m_Unit == Unit )
                    continue;

                int found = FindUnit( ii, Unit );

                if( found >= 0 )
                    continue; // this unit exists for this reference (unit already annotated)

                // Search a component to annotate ( same prefix, same value, not annotated)
                for( unsigned jj = ii + 1; jj < componentFlatList.size(); jj++ )
                {
                    if( componentFlatList[jj].m_Flag )    // already tested
                        continue;

                    if( componentFlatList[ii].CompareRef( componentFlatList[jj] ) != 0 )
                        continue;

                    if( componentFlatList[jj].CompareValue( componentFlatList[ii] ) != 0 )
                        continue;

                    if( componentFlatList[jj].CompareLibName( componentFlatList[ii] ) != 0 )
                        continue;

                    if( !componentFlatList[jj].m_IsNew )
                        continue;

                    // Component without reference number found, annotate it if possible
                    if( !componentFlatList[jj].IsUnitsLocked()
                        || ( componentFlatList[jj].m_Unit == Unit ) )
                    {
                        componentFlatList[jj].m_NumRef = componentFlatList[ii].m_NumRef;
                        componentFlatList[jj].m_Unit   = Unit;
                        componentFlatList[jj].m_Flag   = 1;
                        componentFlatList[jj].m_IsNew  = false;
                        break;
                    }
                }
            }
        }
    }
CUnit* CGameManager::FindUnit(Vec2D pos)
{
	return FindUnit(pos.nPosX, pos.nPosY);
}