ScriptSourceCode PendingScript::getSource(const KURL& documentURL, bool& errorOccurred) const
{
    if (resource()) {
        errorOccurred = resource()->errorOccurred() || m_integrityFailure;
        ASSERT(resource()->isLoaded());
        if (m_streamer && !m_streamer->streamingSuppressed())
            return ScriptSourceCode(m_streamer, resource());
        return ScriptSourceCode(resource());
    }
    errorOccurred = false;
    return ScriptSourceCode(m_element->textContent(), documentURL, startingPosition());
}
Esempio n. 2
0
ReflectPair runABM(int nSamples, double azimuthalAngle, 
        double polarAngle, bool disableSieve, InterfaceList &interfaceList) {
    int startState;
    int reflectedState;
    int transmittedState;
    int absorbedState = -2;
    int numReflected   = 0;
    int numTransmitted = 0;
    int numAbsorbed    = 0;

    double sp = sin(polarAngle);
    vec3 startingPosition(
        cos(azimuthalAngle)*sp,
        sin(azimuthalAngle)*sp,
        cos(polarAngle)
    );

    startingPosition.z *= -1; /* Leaf interfaces are listed adaxial-first, 
                                 orientation is generally assumed with respect to abaxial */

    if(startingPosition.z < 0) {
        startState = 0;
        reflectedState = -1;
        transmittedState = interfaceList.size();
    } else {
        startState = interfaceList.size() - 1;
        reflectedState = startState + 1;
        transmittedState = -1;
    }

    for(int i = 0; i < nSamples; i++) {
        vec3 direction(startingPosition);
        int state = startState;
        interfaceList.prepareForSample();

        while(state != reflectedState && state != transmittedState && state != absorbedState) {
            const ABMInterface &interface = interfaceList.getInterface(state);

            double n1;
            double n2;
            double perturbanceReflect;
            double perturbanceRefract;
            int reflectState;
            int refractState;
            double thickness;
            double absorption;
            vec3 normal(0,0,0);

            if(direction.z < 0) {
                normal.z = 1.0;
                n1 = interface.nAbove;
                n2 = interface.nBelow;
                perturbanceReflect = interface.perturbanceDownAbove;
                perturbanceRefract = interface.perturbanceDownBelow;
                refractState = state + 1;
                reflectState = state - 1;
                thickness    = interface.thicknessAbove;
                absorption   = interface.absorptionAbove;
            } else {
                normal.z = -1.0;
                n1 = interface.nBelow;
                n2 = interface.nAbove;
                perturbanceReflect = interface.perturbanceUpBelow;
                perturbanceRefract = interface.perturbanceUpAbove;
                reflectState = state + 1;
                refractState = state - 1;
                thickness  = interface.thicknessBelow;
                absorption = interface.absorptionBelow;
            }

            double normalAngle = -direction.Dot(normal);
            if(thickness > 0 && freePathLength(direction, normal, normalAngle, absorption, disableSieve) < thickness) {
                state = absorbedState;
                break;
            } else {
                if(RANDOM_FUNCTION() < fresnellCoefficient(direction, normal, normalAngle, n1, n2)) {
                    state = reflectState;
                    direction = reflect(direction, normal, normalAngle);
                    if(perturbanceReflect != INFINITY) {
                        direction = brakkeScattering(direction, perturbanceReflect);
                    }
                } else {
                    state = refractState;
                    direction = refract(direction, normal, normalAngle, n1, n2);
                    if(perturbanceRefract != INFINITY) {
                        direction = brakkeScattering(direction, perturbanceRefract);
                    }
                }
            }
        }

        if(state == reflectedState) {
            numReflected++;
        } else if(state == transmittedState) {
            numTransmitted++;
        } else {
            numAbsorbed++;
        }
    }
    return ReflectPair((double)numReflected / nSamples, (double)numTransmitted / nSamples);
}
Esempio n. 3
0
/**
 * Expert: Intermediate but plays corner first and counters a corner with center
 */
int expertAI(ticTacToeBoard* selector, unsigned short int* corners, unsigned short int* sides, unsigned short int center, char* charPtr, unsigned short int* intPtr, int defense)
{
    // Looping variables
    int i;
    int k;

    // Random int to be set
    int random;

    int play;
    int emptySpaces[9];

    // If play is greater than or equal to 3
    if(intPtr[0] >= 3){

        // Check if AI wins with given play
        play = aiCheckWin(selector, charPtr, defense);

        if(play==20){

            // Set player back to AI
            charPtr[40] = charPtr[42];

            //Fills all empty positions into array of integers
            for(i = 0; i < 9; i++)
            {
                if(charPtr[30 + i] == ' ')
                {
                    emptySpaces[k] = i;
                    k++;
                }
            }

            // Sets new seed
            srand(time(NULL));

            // Rolls random int
            random = rand() % k;
            random = emptySpaces[random];

            //Pulls a random int from array and plays in that position
            charPtr[30 + random] = charPtr[40];
            play = random;
        }
    }

    if(intPtr[0] == 2){
        play = 0;

        // Switches player to check their move
        switchPlayer(charPtr);
        for(i = 0; i < 4; i++)
        {
            // If user plays corner, play center
            if((charPtr[30 + corners[i]] == charPtr[40]) || (charPtr[30 + sides[i]] == charPtr[40]) && (charPtr[30 + center] == ' '))
            {
                // Switches to AI to counter move
                switchPlayer(charPtr);
                charPtr[30 + center] = charPtr[40];
                play = center;
            }
        }
        if(play == 0){
            // Set player back to AI
            charPtr[40] = charPtr[42];

            //Fills all empty positions into array of integers
            for(i = 0; i < 9; i++)
            {
                if(charPtr[30 + i]==' '){
                    emptySpaces[k] = i;
                    k++;
                }
            }

            // Sets new seed
            srand(time(NULL));

            // Rolls random int
            random = rand() % k;
            random = emptySpaces[random];

            //Pulls a random int from array and plays in that position
            charPtr[30 + random] = charPtr[40];
            play = random;
            switchPlayer(charPtr);
        }
    }

    else if(intPtr[0] == 1){
        play = startingPosition(charPtr,corners);
    }

    printf("\n                        **AI played position %i**\n", play);

    return 0;
}