<commit_before>#include "Base.h"
#include "TerrainPatch.h"
#include "Terrain.h"
#include "MeshPart.h"
#include "Scene.h"
#include "Game.h"

namespace gameplay
{

/**
 * @script{ignore}
 */
template <class T> T clamp(T value, T min, T max) { return value < min ? min : (value > max ? max : value); }

#define TERRAINPATCH_DIRTY_MATERIAL 1
#define TERRAINPATCH_DIRTY_BOUNDS 2
#define TERRAINPATCH_DIRTY_LEVEL 4
#define TERRAINPATCH_DIRTY_ALL (TERRAINPATCH_DIRTY_MATERIAL | TERRAINPATCH_DIRTY_BOUNDS | TERRAINPATCH_DIRTY_LEVEL)

/**
 * Custom material auto-binding resolver for terrain.
 * @script{ignore}
 */
class TerrainAutoBindingResolver : RenderState::AutoBindingResolver
{
    bool resolveAutoBinding(const char* autoBinding, Node* node, MaterialParameter* parameter);
};
static TerrainAutoBindingResolver __autoBindingResolver;
static int __currentPatchIndex = -1;

TerrainPatch::TerrainPatch() :
    _terrain(NULL), _row(0), _column(0), _camera(NULL), _level(0), _bits(TERRAINPATCH_DIRTY_ALL)
{
}

TerrainPatch::~TerrainPatch()
{
    for (size_t i = 0, count = _levels.size(); i < count; ++i)
    {
        Level* level = _levels[i];

        SAFE_RELEASE(level->model);
        SAFE_DELETE(level);
    }

    while (_layers.size() > 0)
    {
        deleteLayer(*_layers.begin());
    }
    SAFE_RELEASE(_camera);
}

TerrainPatch* TerrainPatch::create(Terrain* terrain, unsigned int index,
                                   unsigned int row, unsigned int column,
                                   float* heights, unsigned int width, unsigned int height,
                                   unsigned int x1, unsigned int z1, unsigned int x2, unsigned int z2,
                                   float xOffset, float zOffset,
                                   unsigned int maxStep, float verticalSkirtSize)
{
    // Create patch
    TerrainPatch* patch = new TerrainPatch();
    patch->_terrain = terrain;
    patch->_index = index;
    patch->_row = row;
    patch->_column = column;

    // Add patch lods
    for (unsigned int step = 1; step <= maxStep; step *= 2)
    {
        patch->addLOD(heights, width, height, x1, z1, x2, z2, xOffset, zOffset, step, verticalSkirtSize);
    }

    // Set our bounding box using the base LOD mesh
    BoundingBox& bounds = patch->_boundingBox;
    bounds.set(patch->_levels[0]->model->getMesh()->getBoundingBox());

    return patch;
}

unsigned int TerrainPatch::getMaterialCount() const<commit_msg>Fix crash when removing terrain nodes.<commit_after>#include "Base.h"
#include "TerrainPatch.h"
#include "Terrain.h"
#include "MeshPart.h"
#include "Scene.h"
#include "Game.h"

namespace gameplay
{

/**
 * @script{ignore}
 */
template <class T> T clamp(T value, T min, T max) { return value < min ? min : (value > max ? max : value); }

#define TERRAINPATCH_DIRTY_MATERIAL 1
#define TERRAINPATCH_DIRTY_BOUNDS 2
#define TERRAINPATCH_DIRTY_LEVEL 4
#define TERRAINPATCH_DIRTY_ALL (TERRAINPATCH_DIRTY_MATERIAL | TERRAINPATCH_DIRTY_BOUNDS | TERRAINPATCH_DIRTY_LEVEL)

/**
 * Custom material auto-binding resolver for terrain.
 * @script{ignore}
 */
class TerrainAutoBindingResolver : RenderState::AutoBindingResolver
{
    bool resolveAutoBinding(const char* autoBinding, Node* node, MaterialParameter* parameter);
};
static TerrainAutoBindingResolver __autoBindingResolver;
static int __currentPatchIndex = -1;

TerrainPatch::TerrainPatch() :
    _terrain(NULL), _row(0), _column(0), _camera(NULL), _level(0), _bits(TERRAINPATCH_DIRTY_ALL)
{
}

TerrainPatch::~TerrainPatch()
{
    for (size_t i = 0, count = _levels.size(); i < count; ++i)
    {
        Level* level = _levels[i];

        SAFE_RELEASE(level->model);
        SAFE_DELETE(level);
    }

    while (_layers.size() > 0)
    {
        deleteLayer(*_layers.begin());
    }
    
    if (_camera != NULL)
    {
    	_camera->removeListener(this);
    	SAFE_RELEASE(_camera);
    }
}

TerrainPatch* TerrainPatch::create(Terrain* terrain, unsigned int index,
                                   unsigned int row, unsigned int column,
                                   float* heights, unsigned int width, unsigned int height,
                                   unsigned int x1, unsigned int z1, unsigned int x2, unsigned int z2,
                                   float xOffset, float zOffset,
                                   unsigned int maxStep, float verticalSkirtSize)
{
    // Create patch
    TerrainPatch* patch = new TerrainPatch();
    patch->_terrain = terrain;
    patch->_index = index;
    patch->_row = row;
    patch->_column = column;

    // Add patch lods
    for (unsigned int step = 1; step <= maxStep; step *= 2)
    {
        patch->addLOD(heights, width, height, x1, z1, x2, z2, xOffset, zOffset, step, verticalSkirtSize);
    }

    // Set our bounding box using the base LOD mesh
    BoundingBox& bounds = patch->_boundingBox;
    bounds.set(patch->_levels[0]->model->getMesh()->getBoundingBox());

    return patch;
}

unsigned int TerrainPatch::getMaterialCount() const
{'fraction_non_alphanumeric': 0.1116, 'fraction_numerical': 0.008, 'mean_word_length': 3.0008, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4486039', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1545, 'n_words': 400}