<commit_before>#include "common.h"

#include "hex/basics/hexgrid.h"
#include "hex/game/game.h"
#include "hex/resources/resources.h"
#include "hex/view/tile_painter.h"
#include "hex/view/view.h"


TilePainter::TilePainter(Game *game, GameView *view, Resources *resources):
        game(game), view(view), resources(resources) {
}

void TilePainter::repaint(Point offset, int len) {
    // Sanity check coordinates
    if (offset.y < 0 || offset.y >= game->level.height)
        return;
    if (offset.x < 0) {
        len += offset.x;
        offset.x = 0;
    }
    if (offset.x + len >= game->level.width) {
        len = game->level.width - offset.x;
    }

    // Set view defs
    for (int i = 0; i < len; i++) {
        Point tile_pos(offset.x + i, offset.y);

        TileType *tile_type = game->level.tiles[tile_pos].type;
        TileView& tile_view = view->level_view.tile_views[tile_pos];
        TileViewDef *view_def = resources->get_tile_view_def(tile_type->name);
        tile_view.view_def = view_def;
        tile_view.variation = rand();
        tile_view.phase = rand();
    }

    for (int i = offset.y - 1; i <= offset.y + 1; i++) {
        if (i < 0 || i >= game->level.height)
            continue;

        for (int j = offset.x - 1; j < offset.x + len + 1; j++) {
            if (j < 0 || j >= game->level.width)
                continue;

            Point tile_pos(j, i);
            paint_transitions(tile_pos);
            paint_roads(tile_pos);
            paint_mountains(tile_pos);
        }
    }
}

void TilePainter::paint_transitions(const Point& tile_pos) {
    TileView& tile_view = view->level_view.tile_views[tile_pos];

    tile_view.transitions.clear();

    TileViewDef *view_def = tile_view.view_def;
    if (view_def == NULL)
        return;

    for (std::vector<TransitionDef>::iterator iter = view_def->transitions.begin(); iter != view_def->transitions.end(); iter++) {
        TransitionDef& def = *iter;
        bool match = true;
        for (std::set<int>::iterator dir_iter = def.dirs.begin(); dir_iter != def.dirs.end(); dir_iter++) {
            Point neighbour_pos;
            get_neighbour(tile_pos, *dir_iter, &neighbour_pos);
            if (!game->level.contains(neighbour_pos)) {
                match = false;
                continue;
            }
            TileViewDef *neighbour_def = view->level_view.tile_views[neighbour_pos].view_def;
            if (neighbour_def == NULL) {
                match = false;
                continue;
            }
            if (def.type_names.count(neighbour_def->name) == 0) {
                match = false;
                continue;
            }
        }

        if (match && def.images.size() > 0) {
            int r = rand() % def.images.size();
            tile_view.transitions.push_back(def.images[r].image);
        }
    }
}


void TilePainter::paint_roads(const Point& tile_pos) {
    TileView& tile_view = view->level_view.tile_views[tile_pos];

    tile_view.roads.clear();
    TileViewDef *view_def = tile_view.view_def;
    if (view_def == NULL)
        return;
    Tile& tile = game->level.tiles[tile_pos];
    if (!tile.road)
        return;

    for (int dir = 0; dir < 6; dir++) {
        Point neighbour;
        get_neighbour(tile_pos, dir, &neighbour);
        if (!game->level.contains(neighbour))
            continue;

        Tile& neighbour_tile = game->level.tiles[neighbour];
        if (!neighbour_tile.road)
            continue;

        tile_view.roads.push_back(view_def->roads[dir].image);
    }
}

void TilePainter::paint_mountains(const Point& tile_pos) {
    TileView& tile_view = view->level_view.tile_views[tile_pos];
    if (tile_view.view_def == NULL)
        return;

    Tile& tile = game->level.tiles[tile_pos];

    switch (tile.mountain) {
        case 1:
            tile_view.mountain = tile_view.view_def->mountains[0].image;
            break;
        case 2:
            tile_view.mountain = tile_view.view_def->mountains[1].image;
            break;
        case 3:
            tile_view.mountain = tile_view.view_def->mountains[2].image;
            break;
        case 4:
            tile_view.mountain = tile_view.view_def->mountains[3].image;
            break;
        case 5:
            tile_view.mountain = tile_view.view_def->mountains[4].image;
            break;
        default:
            tile_view.mountain = NULL;
    }
}<commit_msg>Fix crashes from having no road and mountain images defined.<commit_after>#include "common.h"

#include "hex/basics/hexgrid.h"
#include "hex/game/game.h"
#include "hex/resources/resources.h"
#include "hex/view/tile_painter.h"
#include "hex/view/view.h"


TilePainter::TilePainter(Game *game, GameView *view, Resources *resources):
        game(game), view(view), resources(resources) {
}

void TilePainter::repaint(Point offset, int len) {
    // Sanity check coordinates
    if (offset.y < 0 || offset.y >= game->level.height)
        return;
    if (offset.x < 0) {
        len += offset.x;
        offset.x = 0;
    }
    if (offset.x + len >= game->level.width) {
        len = game->level.width - offset.x;
    }

    // Set view defs
    for (int i = 0; i < len; i++) {
        Point tile_pos(offset.x + i, offset.y);

        TileType *tile_type = game->level.tiles[tile_pos].type;
        TileView& tile_view = view->level_view.tile_views[tile_pos];
        TileViewDef *view_def = resources->get_tile_view_def(tile_type->name);
        tile_view.view_def = view_def;
        tile_view.variation = rand();
        tile_view.phase = rand();
    }

    for (int i = offset.y - 1; i <= offset.y + 1; i++) {
        if (i < 0 || i >= game->level.height)
            continue;

        for (int j = offset.x - 1; j < offset.x + len + 1; j++) {
            if (j < 0 || j >= game->level.width)
                continue;

            Point tile_pos(j, i);
            paint_transitions(tile_pos);
            paint_roads(tile_pos);
            paint_mountains(tile_pos);
        }
    }
}

void TilePainter::paint_transitions(const Point& tile_pos) {
    TileView& tile_view = view->level_view.tile_views[tile_pos];

    tile_view.transitions.clear();

    TileViewDef *view_def = tile_view.view_def;
    if (view_def == NULL)
        return;

    for (std::vector<TransitionDef>::iterator iter = view_def->transitions.begin(); iter != view_def->transitions.end(); iter++) {
        TransitionDef& def = *iter;
        bool match = true;
        for (std::set<int>::iterator dir_iter = def.dirs.begin(); dir_iter != def.dirs.end(); dir_iter++) {
            Point neighbour_pos;
            get_neighbour(tile_pos, *dir_iter, &neighbour_pos);
            if (!game->level.contains(neighbour_pos)) {
                match = false;
                continue;
            }
            TileViewDef *neighbour_def = view->level_view.tile_views[neighbour_pos].view_def;
            if (neighbour_def == NULL) {
                match = false;
                continue;
            }
            if (def.type_names.count(neighbour_def->name) == 0) {
                match = false;
                continue;
            }
        }

        if (match && def.images.size() > 0) {
            int r = rand() % def.images.size();
            tile_view.transitions.push_back(def.images[r].image);
        }
    }
}


void TilePainter::paint_roads(const Point& tile_pos) {
    TileView& tile_view = view->level_view.tile_views[tile_pos];

    tile_view.roads.clear();
    TileViewDef *view_def = tile_view.view_def;
    if (view_def == NULL)
        return;
    Tile& tile = game->level.tiles[tile_pos];
    if (!tile.road)
        return;

    for (int dir = 0; dir < 6; dir++) {
        Point neighbour;
        get_neighbour(tile_pos, dir, &neighbour);
        if (!game->level.contains(neighbour))
            continue;

        Tile& neighbour_tile = game->level.tiles[neighbour];
        if (!neighbour_tile.road)
            continue;

        if (view_def->roads.size() > dir)
            tile_view.roads.push_back(view_def->roads[dir].image);
    }
}

void TilePainter::paint_mountains(const Point& tile_pos) {
    TileView& tile_view = view->level_view.tile_views[tile_pos];
    if (tile_view.view_def == NULL)
        return;

    Tile& tile = game->level.tiles[tile_pos];

    if (tile.mountain > 0 && tile_view.view_def->mountains.size() >= tile.mountain)
        tile_view.mountain = tile_view.view_def->mountains[tile.mountain - 1].image;
    else
        tile_view.mountain = NULL;
}
{'fraction_non_alphanumeric': 0.13510650817935743, 'fraction_numerical': 0.0047075438390020005, 'mean_word_length': 2.185157421289355, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 72, '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': '2736396', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2773, 'n_words': 535}