<commit_before>package com.floor13.game.util

import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.graphics.g2d.TextureRegion

import ktx.collections.*

import com.floor13.game.core.map.Map
import com.floor13.game.core.map.Tile
import com.floor13.game.core.map.Ground
import com.floor13.game.core.map.Wall
import com.floor13.game.core.map.Door

/**
 * @param atlas atlas for resolving textures
 */
class SidedTextureResolver(
        private val atlas: TextureAtlas,
        private val map: Map
) {
    private val cache: Array<Array<TextureRegion?>> = Array(
            map.size,
            { Array(map.getOrNull(0)?.size ?: 0, { null as TextureRegion? })}
    )

    private val variants = gdxArrayOf<TextureRegion>()
    
    fun getTexture(name: String, x: Int, y: Int): TextureRegion {
        return cache[x][y] ?: {
            val suffix = StringBuilder()

            val tile = map[x][y]
        
            // order convention: urld
            if (y == 0 || map[x][y - 1] sameTileTypeAs tile)
                suffix.append('u')
            if (x == map.size - 1 || map[x + 1][y] sameTileTypeAs tile)
                suffix.append('r')
            if (x == 0 || map[x - 1][y] sameTileTypeAs tile)
                suffix.append('l')
            if (y == (map.getOrNull(0)?.size ?: 0) - 1 || map[x][y + 1] sameTileTypeAs tile)
                suffix.append('d')

            val exactName = "${name}_${suffix}"
            val genericName = "${name}_a"

            variants.clear()
            for (region in atlas.getRegions())
                if (region.name == exactName || region.name == genericName)
                    variants.add(region)

            cache[x][y] = variants.random()
            cache[x][y]!!
        }()
    }

    private infix fun Tile.sameTileTypeAs(tile: Tile): Boolean {
        // review this solution if you added additional Tile types
        return this is Ground && tile is Ground || (!(this is Ground) && !(tile is Ground))
    }
}<commit_msg>Fix side texture resolver logic<commit_after>package com.floor13.game.util

import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.graphics.g2d.TextureRegion

import ktx.collections.*

import com.floor13.game.core.map.Map
import com.floor13.game.core.map.Tile
import com.floor13.game.core.map.Ground
import com.floor13.game.core.map.Wall
import com.floor13.game.core.map.Door

/**
 * @param atlas atlas for resolving textures
 */
class SidedTextureResolver(
        private val atlas: TextureAtlas,
        private val map: Map
) {
    private val cache: Array<Array<TextureRegion?>> = Array(
            map.size,
            { Array(map.getOrNull(0)?.size ?: 0, { null as TextureRegion? })}
    )

    private val variants = gdxArrayOf<TextureRegion>()
    
    fun getTexture(name: String, x: Int, y: Int): TextureRegion {
        return cache[x][y] ?: {
            val suffix = StringBuilder()

            val tile = map[x][y]
        
            // order convention: urld
            if (y == (map.getOrNull(0)?.size ?: 0) - 1 || map[x][y + 1] sameTileTypeAs tile)
                suffix.append('u')
            if (x == map.size - 1 || map[x + 1][y] sameTileTypeAs tile)
                suffix.append('r')
            if (x == 0 || map[x - 1][y] sameTileTypeAs tile)
                suffix.append('l')
            if (y == 0 || map[x][y - 1] sameTileTypeAs tile)
                suffix.append('d')

            val exactName = "${name}_${suffix}"
            val genericName = "${name}_a"

            variants.clear()
            for (region in atlas.getRegions())
                if (region.name == exactName || region.name == genericName)
                    variants.add(region)

            cache[x][y] = variants.random()
            cache[x][y]!!
        }()
    }

    private infix fun Tile.sameTileTypeAs(tile: Tile): Boolean {
        // review this solution if you added additional Tile types
        return this is Ground && tile is Ground || (!(this is Ground) && !(tile is Ground))
    }
}
{'fraction_non_alphanumeric': 0.1329708353929807, 'fraction_numerical': 0.012852199703410776, 'mean_word_length': 2.115473441108545, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4665590', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1234, 'n_words': 306}