void MouseUp()
    {
        for( int i = 0; i < g_app->m_location->m_buildings.Size(); ++i )
        {
            if( g_app->m_location->m_buildings.ValidIndex(i) )
            {
                Building *building = g_app->m_location->m_buildings[i];
                for( int p = 0; p < building->GetNumPorts(); ++p )
                {
                    Vector3 portPos, portFront;
                    building->GetPortPosition( p, portPos, portFront );

                    g_app->m_location->SpawnEntities( portPos, 0, -1, Entity::TypeDarwinian, 3, g_zeroVector, 30.0 );
                }
            }
        }
    }
void AIObjectiveMarker::AdvanceDefensive()
{
    AIObjective *objective = (AIObjective *)g_app->m_location->GetBuilding( m_objectiveId );
    if( objective && objective->m_active )
    {
        Building *b = g_app->m_location->GetBuilding( m_objectiveBuildingId );
        if( b )
        {
            if( b->m_type == Building::TypeSolarPanel &&
                b->GetNumPorts() == 0 )
            {
                m_objectiveBuildingId = -1;
            }
        }
        else
        {
            m_objectiveBuildingId = -1;
        }

        int teamCounts[NUM_TEAMS];
        memset( teamCounts, 0, sizeof(int) * NUM_TEAMS );
        for( int i = 0; i < NUM_TEAMS; ++i )
        {
            if( i == g_app->m_location->GetMonsterTeamId() ) continue;
            if( i == g_app->m_location->GetFuturewinianTeamId() ) continue;
            teamCounts[i] = g_app->m_location->m_entityGrid->GetNumFriends( m_pos.x, m_pos.z, m_scanRange, i );
        }

        int currentWinner = 0;
        int totalDefenders = 0;
        for( int i = 0; i < NUM_TEAMS; ++i )
        {
            if( g_app->m_location->IsDefending(i) ) 
            {
                totalDefenders += teamCounts[i];
                if( totalDefenders >= 15 ||
                    (b && g_app->m_location->IsDefending( b->m_id.GetTeamId() ) ) )
                {
                    currentWinner = i;
                    break;
                }
            }

            if( teamCounts[i] > (teamCounts[currentWinner] * 2 )  )
                currentWinner = i;
        }

        SetTeamId( currentWinner );
    }


    int id = AI::FindNearestTarget( m_pos );
    AITarget *target = (AITarget *)g_app->m_location->GetBuilding( id );
    if( target )
    {
        for( int i = 0; i < NUM_TEAMS; ++i )
        {
            if( g_app->m_location->IsDefending(i) )
            {
                AI *ai = AI::s_ai[i];
                if( ai )
                {
                    if( ai->m_currentObjective == m_objectiveId )
                    {
                        if(g_app->m_location->IsFriend(m_id.GetTeamId(), i )) 
                        {
                            target->m_priority[i] = 1.0;
                            target->m_aiObjectiveTarget[i] = true;
                        }
                        else
                        {
                            target->m_aiObjectiveTarget[i] = false;
                        }
                    }
                    else
                    {
                        target->m_aiObjectiveTarget[i] = false;
                    }
                }
            }
        }
    }
}
void AIObjectiveMarker::AdvanceStandard()
{
    AIObjective *objective = (AIObjective *)g_app->m_location->GetBuilding( m_objectiveId );
    if( objective && objective->m_active )
    {
        if( m_objectiveBuildingId != -1 )
        {
            Building *b = g_app->m_location->GetBuilding( m_objectiveBuildingId );
            if( b && !b->m_destroyed )
            {
                if( b->m_type == Building::TypeSolarPanel &&
                    b->GetNumPorts() == 0 )
                {
                    m_objectiveBuildingId = -1;
                }
                else
                {
                    SetTeamId( b->m_id.GetTeamId() );
                }
            }
            else
            {
                AppDebugOut("Registered Objective Building missing\n");
                m_objectiveBuildingId = -1;
            }
        }

        if( m_objectiveBuildingId == -1 )
        {
            if( m_scanRange == 0.0 )
            {
                SetTeamId(m_defaultTeam);
            }
            else
            {
                bool include[NUM_TEAMS];
                memset( include, true, sizeof(bool) * NUM_TEAMS );
                include[g_app->m_location->GetMonsterTeamId()] = false;
                include[g_app->m_location->GetFuturewinianTeamId()] = false;
                int total = g_app->m_location->m_entityGrid->GetNumNeighbours( m_pos.x, m_pos.z, m_scanRange, include );
                if( total > 0 )
                {
                    int teamCounts[NUM_TEAMS];
                    memset( teamCounts, 0, sizeof(int) * NUM_TEAMS );
                    for( int i = 0; i < NUM_TEAMS; ++i )
                    {
                        if( i == g_app->m_location->GetMonsterTeamId() ) continue;
                        if( i == g_app->m_location->GetFuturewinianTeamId() ) continue;
                        teamCounts[i] = g_app->m_location->m_entityGrid->GetNumFriends( m_pos.x, m_pos.z, m_scanRange, i );
                    }

                    int currentWinner = 0;
                    for( int i = 0; i < NUM_TEAMS; ++i )
                    {
                        if( teamCounts[i] > (teamCounts[currentWinner] * 4 ) &&
                            teamCounts[currentWinner] < 10) currentWinner = i;
                    }

                    SetTeamId( currentWinner );
                }
                else
                {
                    SetTeamId(255);
                }
            }
        }
    }

    int id = AI::FindNearestTarget( m_pos );
    AITarget *target = (AITarget *)g_app->m_location->GetBuilding( id );
    if( target )
    {
        for( int i = 0; i < NUM_TEAMS; ++i )
        {
            if( g_app->m_location->IsAttacking(i) )
            {
                AI *ai = AI::s_ai[i];
                if( ai )
                {
                    if( ai->m_currentObjective == m_objectiveId )
                    {
                        if( m_armourObjective == 0 )
                        {
                            if(!g_app->m_location->IsFriend(m_id.GetTeamId(), i )) 
                            {
                                target->m_priority[i] = 1.0;
                                target->m_aiObjectiveTarget[i] = true;
                            }
                            else
                            {
                                target->m_aiObjectiveTarget[i] = false;
                            }
                        }
                        else
                        {
                            target->m_aiObjectiveTarget[i] = false;
                        }
                    }
                    else if( m_pickupAvailable )
                    {
                        target->m_priority[i] = 1.0;
                        target->m_aiObjectiveTarget[i] = true;
                    }
                    else
                    {
                        target->m_aiObjectiveTarget[i] = false;
                    }
                }
            }
        }
    }
}