<commit_before>#include "rWeaponMachinegun.h"

#include "de/hackcraft/world/World.h"

#include "de/hackcraft/psi3d/GLF.h"
#include "de/hackcraft/psi3d/GLS.h"
#include "de/hackcraft/psi3d/Primitive.h"

#include <cassert>

std::string rWeaponMachinegun::cname = "MACHINEGUN";
unsigned int rWeaponMachinegun::cid = 7743;

rWeaponMachinegun::rWeaponMachinegun(Entity* obj) {
    object = obj;

    //cout << "cMachineGun()\n";
    clipSize = 75;
    depotSize = 2;
    remainingAmmo = clipSize;
    remainingClips = depotSize;
    if (WEAPONSOUND) {
        //ALuint buffer = alutCreateBufferHelloWorld();
        ALuint buffer = alutCreateBufferFromFile("data/org/freesound/machinegun.wav");
        alGenSources(1, &soundSource);
        //if (alGetError() != AL_FALSE || !alIsSource(soundSource)) throw "sorry, could not create audio source for machine gun";
        alSourcei(soundSource, AL_BUFFER, buffer);
        alSourcef(soundSource, AL_PITCH, 1.0f);
        alSourcef(soundSource, AL_GAIN, 1.5f);
        alSourcei(soundSource, AL_LOOPING, AL_FALSE);
    }
}

void rWeaponMachinegun::fire() {
    if (!ready()) return;
    triggered = true;

    if (remainingAmmo > 0) {
        remainingAmmo--;
        if (remainingAmmo == 0 && remainingClips != 0) {
            timeReloading = 2.5;
        } else {
            timeReloading = 0.5;
        }
    }

    float* source = weaponPosef;
    float* pos = &source[12];

    {
        Particle* s = new Particle();
        assert(s != NULL);
        s->fuel = 4;

        vector_cpy(s->pos, pos);

        float nrm[3];
        float pos2[] = {0, +3, 0};
        matrix_apply2(source, pos2);
        vector_sub(nrm, pos2, s->pos);

        vector_scale(s->vel, nrm, 1.4);
        const float variation = 0.01;
        loopi(3) s->vel[i] += (50 - (rand() % 100)) * variation;
        castoffParticles.push_back(s);
    }


    {
        Particle* s = new Particle();
        assert(s != NULL);
        s->fuel = 1.0f;

        vector_cpy(s->pos, pos);

        float nrm[3];
        float pos2[] = {0, 0, -1};
        matrix_apply2(source, pos2);
        vector_sub(nrm, pos2, s->pos);

        vector_scale(s->vel, nrm, 200);
        const float variation = 0.03;
        loopi(3) s->vel[i] += (50 - (rand() % 100)) * variation;
        shrapnelParticles.push_back(s);
    }

    playSourceIfNotPlaying();
}

void rWeaponMachinegun::animate(float spf) {
    triggereded = triggered;
    if (trigger) fire();
    trigger = false;

    for (std::list<Particle*>::iterator i = shrapnelParticles.begin(); i != shrapnelParticles.end();) {
        float damage = 10;
        Particle* s = *i++;
        float dt = spf * 0.2f;
        int damaged = 0;

        loopi(5) {
            vector_muladd(s->pos, s->pos, s->vel, dt);
            damaged = this->damageByParticle(s->pos, 0.2f, 0, damage);
            if (damaged != 0) break;
        }
        s->fuel -= spf;
        if (s->fuel < 0 || damaged != 0) {
            shrapnelParticles.remove(s);
            delete s;
        }
    }

    foreachNoInc(i, castoffParticles) {
        Particle* s = *i++;
        s->vel[1] += World::instance->getGravity()[1] * spf;
        vector_muladd(s->pos, s->pos, s->vel, spf);
        s->fuel -= spf;
        if (s->fuel < 0) {
            castoffParticles.remove(s);
            delete s;
        }
    }

    foreachNoInc(i, damageParticles) {
        Particle* s = *i++;
        vector_muladd(s->pos, s->pos, s->vel, spf);
        s->fuel -= spf;
        if (s->fuel < 0) {
            damageParticles.remove(s);
            delete s;
        }
    }<commit_msg>WeaponSystem: Removing dependency on global gravity.<commit_after>#include "rWeaponMachinegun.h"

#include "de/hackcraft/world/World.h"

#include "de/hackcraft/psi3d/GLF.h"
#include "de/hackcraft/psi3d/GLS.h"
#include "de/hackcraft/psi3d/Primitive.h"

#include <cassert>

std::string rWeaponMachinegun::cname = "MACHINEGUN";
unsigned int rWeaponMachinegun::cid = 7743;

rWeaponMachinegun::rWeaponMachinegun(Entity* obj) {
    object = obj;

    //cout << "cMachineGun()\n";
    clipSize = 75;
    depotSize = 2;
    remainingAmmo = clipSize;
    remainingClips = depotSize;
    if (WEAPONSOUND) {
        //ALuint buffer = alutCreateBufferHelloWorld();
        ALuint buffer = alutCreateBufferFromFile("data/org/freesound/machinegun.wav");
        alGenSources(1, &soundSource);
        //if (alGetError() != AL_FALSE || !alIsSource(soundSource)) throw "sorry, could not create audio source for machine gun";
        alSourcei(soundSource, AL_BUFFER, buffer);
        alSourcef(soundSource, AL_PITCH, 1.0f);
        alSourcef(soundSource, AL_GAIN, 1.5f);
        alSourcei(soundSource, AL_LOOPING, AL_FALSE);
    }
}

void rWeaponMachinegun::fire() {
    if (!ready()) return;
    triggered = true;

    if (remainingAmmo > 0) {
        remainingAmmo--;
        if (remainingAmmo == 0 && remainingClips != 0) {
            timeReloading = 2.5;
        } else {
            timeReloading = 0.5;
        }
    }

    float* source = weaponPosef;
    float* pos = &source[12];

    {
        Particle* s = new Particle();
        assert(s != NULL);
        s->fuel = 4;

        vector_cpy(s->pos, pos);

        float nrm[3];
        float pos2[] = {0, +3, 0};
        matrix_apply2(source, pos2);
        vector_sub(nrm, pos2, s->pos);

        vector_scale(s->vel, nrm, 1.4);
        const float variation = 0.01;
        loopi(3) s->vel[i] += (50 - (rand() % 100)) * variation;
        castoffParticles.push_back(s);
    }


    {
        Particle* s = new Particle();
        assert(s != NULL);
        s->fuel = 1.0f;

        vector_cpy(s->pos, pos);

        float nrm[3];
        float pos2[] = {0, 0, -1};
        matrix_apply2(source, pos2);
        vector_sub(nrm, pos2, s->pos);

        vector_scale(s->vel, nrm, 200);
        const float variation = 0.03;
        loopi(3) s->vel[i] += (50 - (rand() % 100)) * variation;
        shrapnelParticles.push_back(s);
    }

    playSourceIfNotPlaying();
}

void rWeaponMachinegun::animate(float spf) {
    triggereded = triggered;
    if (trigger) fire();
    trigger = false;

    for (std::list<Particle*>::iterator i = shrapnelParticles.begin(); i != shrapnelParticles.end();) {
        float damage = 10;
        Particle* s = *i++;
        float dt = spf * 0.2f;
        int damaged = 0;

        loopi(5) {
            vector_muladd(s->pos, s->pos, s->vel, dt);
            damaged = this->damageByParticle(s->pos, 0.2f, 0, damage);
            if (damaged != 0) break;
        }
        s->fuel -= spf;
        if (s->fuel < 0 || damaged != 0) {
            shrapnelParticles.remove(s);
            delete s;
        }
    }
    
    float gravity = -9.80665f;

    foreachNoInc(i, castoffParticles) {
        Particle* s = *i++;
        s->vel[1] += gravity * spf;
        vector_muladd(s->pos, s->pos, s->vel, spf);
        s->fuel -= spf;
        if (s->fuel < 0) {
            castoffParticles.remove(s);
            delete s;
        }
    }

    foreachNoInc(i, damageParticles) {
        Particle* s = *i++;
        vector_muladd(s->pos, s->pos, s->vel, spf);
        s->fuel -= spf;
        if (s->fuel < 0) {
            damageParticles.remove(s);
            delete s;
        }
    }
{'fraction_non_alphanumeric': 0.15088105726872247, 'fraction_numerical': 0.023265418502202644, 'mean_word_length': 2.1328158689090126, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 66, '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': '6343433', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2555, 'n_words': 526}