<commit_before>// Demo of heavily simplified sprite engine
// by <NAME> 2009
// used as base for lab 4 in TSBK03.
// OpenGL 3 conversion 2013.

#ifdef __APPLE__
#include <OpenGL/gl3.h>
#include "../../common/mac/MicroGlut.h"
// uses framework Cocoa
#else
#include <GL/gl.h>
#include "../../common/Linux/MicroGlut.h"
#endif

#include <stdlib.h>
#include <time.h>
#include "../../common/LoadTGA.h"
#include "SpriteLight.h"
#include "../../common/GL_utilities.h"

// Lgg till egna globaler hr efter behov.

float maxDistanceSq = 20000;
float cohesionWeight = 0.002;
float alignmentWeight = 0.01;
float avoidanceWeight = 0.5;
float randomWeight = 0.3;
SpritePtr blackSheep;

FPoint calculateAvoidance(SpritePtr i, SpritePtr j){
    FPoint avoidance;
    avoidance.h = i->position.h - j->position.h;
    avoidance.v = i->position.v - j->position.v;

    float distance = sqrt(pow(avoidance.h, 2) + pow(avoidance.v, 2));

    avoidance.h *= (1-distance/sqrt(maxDistanceSq))/distance;
    avoidance.v *= (1-distance/sqrt(maxDistanceSq))/distance;

    return avoidance;
}

void SpriteBehavior() // Din kod!
{
    // Lgg till din labbkod hr. Det gr bra att ndra var som helst i
    // koden i vrigt, men mycket kan samlas hr. Du kan utg frn den
    // globala listroten, gSpriteRoot, fr att kontrollera alla sprites
    // hastigheter och positioner, eller arbeta frn egna globaler.

    SpritePtr i = gSpriteRoot;
    SpritePtr j;

    while (i != NULL){
        unsigned count = 0;
        j = gSpriteRoot;
        i->speedDiff = (FPoint){0,0};
        i->averagePosition = (FPoint){0,0};
        i->avoidanceVector = (FPoint){0,0};
        i->speedSetter = (FPoint){0,0};

        while (j != NULL){
            if (i != j && pow(j->position.h - i->position.h, 2) + pow(j->position.v - i->position.v, 2) < maxDistanceSq ){
                i->averagePosition.h += j->position.h;
                i->averagePosition.v += j->position.v;
                
                i->speedDiff.h += j->speed.h - i->speed.h;
                i->speedDiff.v += j->speed.v - i->speed.v;
                
                FPoint avoidance = calculateAvoidance(i, j);
                i->avoidanceVector.h += avoidance.h;
                i->avoidanceVector.v += avoidance.v;
                
                count += 1;
            }
            j = j->next;
        }

        if (count != 0 ){
            i->averagePosition.h /= count;
            i->averagePosition.v /= count;
            i->speedDiff.h /= count;
            i->speedDiff.v /= count;
            i->avoidanceVector.h /= count;
            i->avoidanceVector.v /= count;
            i->speedSetter.h = (i->averagePosition.h - i->position.h)*cohesionWeight 
                + i->speedDiff.h*alignmentWeight
                + i->avoidanceVector.h*avoidanceWeight;
            i->speedSetter.v = (i->averagePosition.v - i->position.v)*cohesionWeight 
                + i->speedDiff.v*alignmentWeight
                + i->avoidanceVector.v*avoidanceWeight;
        }
        i = i->next;
    }

    i = gSpriteRoot;
    while ( i != NULL ){
        //printf("%f\n", i->averagePosition.h*cohesionWeight);
        i->speed.h += i->speedSetter.h;
        i->speed.v += i->speedSetter.v;
        i = i->next;
    }
    blackSheep->speed.h += (rand()/(float)RAND_MAX - 0.5)*randomWeight;
    blackSheep->speed.v += (rand()/(float)RAND_MAX - 0.5)*randomWeight;


}

// Drawing routine
void Display()
{
    SpritePtr sp;

    glClearColor(0, 0, 0.2, 1);
    glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    DrawBackground();

    SpriteBehavior(); // Din kod!

    // Loop though all sprites. (Several loops in real engine.)
    sp = gSpriteRoot;
    do
    {
        HandleSprite(sp); // Callback in a real engine
        DrawSprite(sp);
        sp = sp->next;
    } while (sp != NULL);

    glutSwapBuffers();
}

void Reshape(int h, int v)
{
    glViewport(0, 0, h, v);
    gWidth = h;
    gHeight = v;
}

void Timer(int value)
{
    glutTimerFunc(20, Timer, 0);
    glutPostRedisplay();
}

// Example of user controllable parameter
float someValue = 0.0;

void Key(unsigned char key,
        __attribute__((unused)) int x,
        __attribute__((unused)) int y)
{
    switch (key)
    {
        case '+':
            cohesionWeight += 0.0001;
            printf("cohesionWeight = %f\n", cohesionWeight);
            break;
        case '-':
            cohesionWeight -= 0.0001;
            printf("cohesionWeight = %f\n", cohesionWeight);
            break;
        case 0x1b:
            exit(0);
    }
}

void Init()
{

    srand(time(NULL));
    TextureData *sheepFace, *blackFace, *dogFace, *foodFace;

    LoadTGATextureSimple("img/leaves.tga", &backgroundTexID); // Bakgrund

    sheepFace = GetFace("img/sheep.tga"); // Ett fr
    blackFace = GetFace("img/blackie.tga"); // Ett svart fr
    dogFace = GetFace("img/dog.tga"); // En hund
    foodFace = GetFace("img/mat.tga"); // Mat

    NewSprite(sheepFace, 100, 200, 1, 1);
    NewSprite(sheepFace, 200, 100, 1.5, -1);
    NewSprite(sheepFace, 250, 200, -1, 1.5);
    NewSprite(sheepFace, 200, 200, -1, 1.5);
    NewSprite(sheepFace, 100, 100, 1, 1.5);
    NewSprite(sheepFace, 500, 200, 1.5, 1);
    NewSprite(sheepFace, 200, 300, -1, 1.5);
    NewSprite(sheepFace, 100, 300, -1.5, 1.5);
    NewSprite(sheepFace, 300, 100, -1, 1.5);
    blackSheep = NewSprite(blackFace, 400, 400, 1, 1.5);

}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(800, 600);
    glutInitContextVersion(3, 2);
    glutCreateWindow("SpriteLight demo / Flocking");

    glutDisplayFunc(Display);
    glutTimerFunc(20, Timer, 0); // Should match the screen synch
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Key);

    InitSpriteLight();
    Init();

    glutMainLoop();
    return 0;
}
<commit_msg>HANDIN 4-5: Field of view.<commit_after>// Demo of heavily simplified sprite engine
// by <NAME> 2009
// used as base for lab 4 in TSBK03.
// OpenGL 3 conversion 2013.

#ifdef __APPLE__
#include <OpenGL/gl3.h>
#include "../../common/mac/MicroGlut.h"
// uses framework Cocoa
#else
#include <GL/gl.h>
#include "../../common/Linux/MicroGlut.h"
#endif

#include <stdlib.h>
#include <time.h>
#include "../../common/LoadTGA.h"
#include "SpriteLight.h"
#include "../../common/GL_utilities.h"

// Lgg till egna globaler hr efter behov.

float maxDistanceSq = 20000;
float cohesionWeight = 0.002;
float alignmentWeight = 0.01;
float avoidanceWeight = 0.5;
float randomWeight = 0.3;
SpritePtr blackSheep;

FPoint calculateAvoidance(SpritePtr i, SpritePtr j){
    FPoint avoidance;
    avoidance.h = i->position.h - j->position.h;
    avoidance.v = i->position.v - j->position.v;

    float distance = sqrt(pow(avoidance.h, 2) + pow(avoidance.v, 2));

    avoidance.h *= (1-distance/sqrt(maxDistanceSq))/distance;
    avoidance.v *= (1-distance/sqrt(maxDistanceSq))/distance;

    return avoidance;
}

void SpriteBehavior() // Din kod!
{
    // Lgg till din labbkod hr. Det gr bra att ndra var som helst i
    // koden i vrigt, men mycket kan samlas hr. Du kan utg frn den
    // globala listroten, gSpriteRoot, fr att kontrollera alla sprites
    // hastigheter och positioner, eller arbeta frn egna globaler.

    SpritePtr i = gSpriteRoot;
    SpritePtr j;

    while (i != NULL){
        unsigned count = 0;
        j = gSpriteRoot;
        i->speedDiff = (FPoint){0,0};
        i->averagePosition = (FPoint){0,0};
        i->avoidanceVector = (FPoint){0,0};
        i->speedSetter = (FPoint){0,0};


        while (j != NULL){
            FPoint vector_between;
            vector_between.h = j->position.h - i->position.h;
            vector_between.v = j->position.v - i->position.v;

            float length = sqrt(pow(vector_between.h, 2) + pow(vector_between.v, 2));
            float speed_abs = sqrt(pow(i->speed.h, 2) + pow(i->speed.v, 2));

            float angular_stuff = (vector_between.h/length)*i->speed.h/speed_abs + (vector_between.v/length)*i->speed.v/speed_abs;
            
            if (i != j && 
                    pow(j->position.h - i->position.h, 2) + pow(j->position.v - i->position.v, 2) < maxDistanceSq && 
                    angular_stuff > -0.5
               ){
                i->averagePosition.h += j->position.h;
                i->averagePosition.v += j->position.v;

                i->speedDiff.h += j->speed.h - i->speed.h;
                i->speedDiff.v += j->speed.v - i->speed.v;

                FPoint avoidance = calculateAvoidance(i, j);
                i->avoidanceVector.h += avoidance.h;
                i->avoidanceVector.v += avoidance.v;

                count += 1;
            }
            j = j->next;
        }

        if (count != 0 ){
            i->averagePosition.h /= count;
            i->averagePosition.v /= count;
            i->speedDiff.h /= count;
            i->speedDiff.v /= count;
            i->avoidanceVector.h /= count;
            i->avoidanceVector.v /= count;
            i->speedSetter.h = (i->averagePosition.h - i->position.h)*cohesionWeight 
                + i->speedDiff.h*alignmentWeight
                + i->avoidanceVector.h*avoidanceWeight;
            i->speedSetter.v = (i->averagePosition.v - i->position.v)*cohesionWeight 
                + i->speedDiff.v*alignmentWeight
                + i->avoidanceVector.v*avoidanceWeight;
        }
        i = i->next;
    }

    i = gSpriteRoot;
    while ( i != NULL ){
        //printf("%f\n", i->averagePosition.h*cohesionWeight);
        i->speed.h += i->speedSetter.h;
        i->speed.v += i->speedSetter.v;
        i = i->next;
    }
    blackSheep->speed.h += (rand()/(float)RAND_MAX - 0.5)*randomWeight;
    blackSheep->speed.v += (rand()/(float)RAND_MAX - 0.5)*randomWeight;


}

// Drawing routine
void Display()
{
    SpritePtr sp;

    glClearColor(0, 0, 0.2, 1);
    glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    DrawBackground();

    SpriteBehavior(); // Din kod!

    // Loop though all sprites. (Several loops in real engine.)
    sp = gSpriteRoot;
    do
    {
        HandleSprite(sp); // Callback in a real engine
        DrawSprite(sp);
        sp = sp->next;
    } while (sp != NULL);

    glutSwapBuffers();
}

void Reshape(int h, int v)
{
    glViewport(0, 0, h, v);
    gWidth = h;
    gHeight = v;
}

void Timer(int value)
{
    glutTimerFunc(20, Timer, 0);
    glutPostRedisplay();
}

// Example of user controllable parameter
float someValue = 0.0;

void Key(unsigned char key,
        __attribute__((unused)) int x,
        __attribute__((unused)) int y)
{
    switch (key)
    {
        case '+':
            cohesionWeight += 0.0001;
            printf("cohesionWeight = %f\n", cohesionWeight);
            break;
        case '-':
            cohesionWeight -= 0.0001;
            printf("cohesionWeight = %f\n", cohesionWeight);
            break;
        case 0x1b:
            exit(0);
    }
}

void Init()
{

    srand(time(NULL));
    TextureData *sheepFace, *blackFace, *dogFace, *foodFace;

    LoadTGATextureSimple("img/leaves.tga", &backgroundTexID); // Bakgrund

    sheepFace = GetFace("img/sheep.tga"); // Ett fr
    blackFace = GetFace("img/blackie.tga"); // Ett svart fr
    dogFace = GetFace("img/dog.tga"); // En hund
    foodFace = GetFace("img/mat.tga"); // Mat

    NewSprite(sheepFace, 100, 200, 1, 1);
    NewSprite(sheepFace, 200, 100, 1.5, -1);
    NewSprite(sheepFace, 250, 200, -1, 1.5);
    NewSprite(sheepFace, 200, 200, -1, 1.5);
    NewSprite(sheepFace, 100, 100, 1, 1.5);
    NewSprite(sheepFace, 500, 200, 1.5, 1);
    NewSprite(sheepFace, 200, 300, -1, 1.5);
    NewSprite(sheepFace, 100, 300, -1.5, 1.5);
    NewSprite(sheepFace, 300, 100, -1, 1.5);
    blackSheep = NewSprite(blackFace, 400, 400, 1, 1.5);

}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(800, 600);
    glutInitContextVersion(3, 2);
    glutCreateWindow("SpriteLight demo / Flocking");

    glutDisplayFunc(Display);
    glutTimerFunc(20, Timer, 0); // Should match the screen synch
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Key);

    InitSpriteLight();
    Init();

    glutMainLoop();
    return 0;
}

{'fraction_non_alphanumeric': 0.14843938056647676, 'fraction_numerical': 0.029206451095241916, 'mean_word_length': 2.2696747114375655, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 124, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4177092', 'n_tokens_mistral': 4744, 'n_tokens_neox': 4306, 'n_words': 942}