<commit_before>/* OpenHoW
 * Copyright (C) 2017-2019 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../engine.h"
#include "../frontend.h"
#include "../audio.h"
#include "../Map.h"

#include "GameManager.h"
#include "SPGameMode.h"

GameManager *GameManager::instance_ = nullptr;

GameManager::GameManager() {

}

GameManager::~GameManager() {

}

void GameManager::Tick() {
    FrontEnd_Tick();

    if(active_mode_ == nullptr) {
        return;
    }

    if(ambient_emit_delay_ < g_state.sim_ticks) {
        const AudioSample* sample = ambient_samples_[rand() % MAX_AMBIENT_SAMPLES];
        u_assert(sample != nullptr, "Audio sample was unexpectedly freed!\n");

        float volume = 0.5f + (rand() % 10 / 1.0f);
        LogDebug("Volume: %f\n", volume);
        PLVector3 position = {
                static_cast<float>(rand() % MAP_PIXEL_WIDTH), 0,
                static_cast<float>(rand() % MAP_PIXEL_WIDTH)
        };

        AudioManager::GetInstance()->PlayLocalSound(sample, position, { 0, 0, 0 }, true, volume);
        ambient_emit_delay_ = g_state.sim_ticks + TICKS_PER_SECOND + rand() % (7 * TICKS_PER_SECOND);
    }

    active_mode_->Tick();
}

void GameManager::LoadMap(const std::string &name) {
    FrontEnd_SetState(FE_MODE_LOADING);

    if(active_map_ != nullptr) {

    }

    try {
        active_map_ = new Map(name);
    } catch(const std::runtime_error &e) {
        Error("Failed to load map, aborting!\n%s\n", e.what());
    }

    MapManifest* manifest = MapManager::GetInstance()->GetManifest(name);
    std::string sample_ext = "d";
    if(manifest->time != "day") {
        sample_ext = "n";
    }

    for(unsigned int i = 1, idx = 0; i < 4; ++i) {
        if(i < 3) {
            ambient_samples_[idx++] = AudioManager::GetInstance()->CacheSample(
                    "audio/amb_" + std::to_string(i) + sample_ext + ".wav", false);<commit_msg>ensure sounds are played at tallest level point + use GenerateRandomf rather than rand<commit_after>/* OpenHoW
 * Copyright (C) 2017-2019 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../engine.h"
#include "../frontend.h"
#include "../audio.h"
#include "../Map.h"

#include "GameManager.h"
#include "SPGameMode.h"

GameManager *GameManager::instance_ = nullptr;

GameManager::GameManager() {

}

GameManager::~GameManager() {

}

void GameManager::Tick() {
    FrontEnd_Tick();

    if(active_mode_ == nullptr) {
        return;
    }

    if(ambient_emit_delay_ < g_state.sim_ticks) {
        const AudioSample* sample = ambient_samples_[rand() % MAX_AMBIENT_SAMPLES];
        if(sample != nullptr) {
            PLVector3 position = {
                    plGenerateRandomf(MAP_PIXEL_WIDTH),
                    active_map_->GetMaxHeight(),
                    plGenerateRandomf(MAP_PIXEL_WIDTH)
            };
            AudioManager::GetInstance()->PlayLocalSound(sample, position, { 0, 0, 0 }, true, 0.5f);
        }

        ambient_emit_delay_ = g_state.sim_ticks + TICKS_PER_SECOND + rand() % (7 * TICKS_PER_SECOND);
    }

    active_mode_->Tick();
}

void GameManager::LoadMap(const std::string &name) {
    FrontEnd_SetState(FE_MODE_LOADING);

    if(active_map_ != nullptr) {

    }

    try {
        active_map_ = new Map(name);
    } catch(const std::runtime_error &e) {
        Error("Failed to load map, aborting!\n%s\n", e.what());
    }

    MapManifest* manifest = MapManager::GetInstance()->GetManifest(name);
    std::string sample_ext = "d";
    if(manifest->time != "day") {
        sample_ext = "n";
    }

    for(unsigned int i = 1, idx = 0; i < 4; ++i) {
        if(i < 3) {
            ambient_samples_[idx++] = AudioManager::GetInstance()->CacheSample(
                    "audio/amb_" + std::to_string(i) + sample_ext + ".wav", false);
{'fraction_non_alphanumeric': 0.12916085309946182, 'fraction_numerical': 0.008969503687462627, 'mean_word_length': 3.0144, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '645264', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1539, 'n_words': 437}