<commit_before>var entity = {
    x: 0,
    y: 0,
    color: "#FF00FF",
    attackPower: 3,
    hp: 20,
    needsDestroy: false,

    setTo: function(x,y) {
        this.x = x
        this.y = y
    },

    move: function(offX, offY) {
        const x = this.x + offX
        const y = this.y + offY
        var entityAtPosition = this.entityAt(x, y)
        if(entityAtPosition){
            this.attack(entityAtPosition)
        } else if (isColliderAt(x, y)) {
            var interactable = getInteractableAt(x,y)
            if(interactable && interactable.type == "treasure"){
                console.log("Congratulations! You've found a treasure chest!")
                level[y] = changeStringAtIndex(level[y], x, " ")
            }
        } else {
            this.x += offX
            this.y += offY
        }
    },

    show: function() {
        fill(this.color)
        rect(this.x*cellsize, this.y*cellsize, cellsize, cellsize)
    },

    attack: function (entity){
        entity.takeDamage(this.attackPower)
    },

    takeDamage: function(damage){
        this.hp -= damage
        if(this.hp <= 0){
            this.needsDestroy = true
        }
    },

    entityAt: function(x,y) {
        var entities = enemies.slice()
        entities.push(p)

        var entityToReturn = undefined

        entities.forEach( (entity) => {
            if(entity.x == x && entity.y == y){
                entityToReturn = entity
            }
        })
        return(entityToReturn)
    }
}

var enemy =  {
    x: 0,
    y: 0,
    color: "#FF00FF",
    type: "enemy",
    spottingDistance: 10,

    move: function() {
        this.simplePathfind()
    },

    simplePathfind: function() {
        // simple pathfinding (NOT COLLISIONPROOF!)

        const playerPosition = {x: p.x, y: p.y}

        const possibleMoves = [
            {x: 1, y: 0},
            {x: -1, y: 0},
            {x: 0, y: -1},
            {x: 0, y:1}
        ]

        if(distance({x: this.x, y: this.y}, playerPosition) <= this.spottingDistance){

            var canStillMove = true
            for(var o = 0; o < possibleMoves.length; o++){
                var move = possibleMoves[o]
                var possibleEntity = this.entityAt(move.x + this.x, move.y + this.y)
                if(possibleEntity){
                    console.log("player attacked")
                    if(possibleEntity.type === "player"){
                        this.attack(possibleEntity)
                        canStillMove = false
                        break;
                    }
                }
            }

            if(canStillMove){
                var bestMove = {x: 0, y: 0}

                for(var i = 0; i < possibleMoves.length; i++){
                    var newMove = {x: this.x + possibleMoves[i].x, y: this.y + possibleMoves[i].y}
                    if(distance(newMove,playerPosition)<=distance({x: this.x + bestMove.x, y: this.y + bestMove.y },playerPosition)){
                        bestMove = possibleMoves[i]
                    }
                }

                if(this.entityAt(bestMove.x + this.x, bestMove.y + this.y) == undefined){
                    this.x += bestMove.x
                    this.y += bestMove.y
                }
            }
        } else {
            // move around a bit
            const moveIndex = floor(random(possibleMoves.length + 10))

            if(moveIndex < possibleMoves.length){
                const randomMove = possibleMoves[moveIndex]
                const x = this.x + randomMove.x
                const y = this.y + randomMove.y

                if(!isColliderAt(x,y)){
                    this.x += randomMove.x
                    this.y += randomMove.y
                } 
            }
        }
    }
}
<commit_msg>enemies now cant travel through walls (and treasures)<commit_after>var entity = {
    x: 0,
    y: 0,
    color: "#FF00FF",
    attackPower: 3,
    hp: 20,
    needsDestroy: false,

    setTo: function(x,y) {
        this.x = x
        this.y = y
    },

    move: function(offX, offY) {
        const x = this.x + offX
        const y = this.y + offY
        var entityAtPosition = this.entityAt(x, y)
        if(entityAtPosition){
            this.attack(entityAtPosition)
        } else if (isColliderAt(x, y)) {
            var interactable = getInteractableAt(x,y)
            if(interactable && interactable.type == "treasure"){
                console.log("Congratulations! You've found a treasure chest!")
                level[y] = changeStringAtIndex(level[y], x, " ")
            }
        } else {
            this.x += offX
            this.y += offY
        }
    },

    show: function() {
        fill(this.color)
        rect(this.x*cellsize, this.y*cellsize, cellsize, cellsize)
    },

    attack: function (entity){
        entity.takeDamage(this.attackPower)
    },

    takeDamage: function(damage){
        this.hp -= damage
        if(this.hp <= 0){
            this.needsDestroy = true
        }
    },

    entityAt: function(x,y) {
        var entities = enemies.slice()
        entities.push(p)

        var entityToReturn = undefined

        entities.forEach( (entity) => {
            if(entity.x == x && entity.y == y){
                entityToReturn = entity
            }
        })
        return(entityToReturn)
    }
}

var enemy =  {
    x: 0,
    y: 0,
    color: "#FF00FF",
    type: "enemy",
    spottingDistance: 10,

    move: function() {
        this.simplePathfind()
    },

    simplePathfind: function() {
        // simple pathfinding (NOT COLLISIONPROOF!)

        const playerPosition = {x: p.x, y: p.y}

        const possibleMoves = [
            {x: 1, y: 0},
            {x: -1, y: 0},
            {x: 0, y: -1},
            {x: 0, y:1}
        ]

        if(distance({x: this.x, y: this.y}, playerPosition) <= this.spottingDistance){

            var canStillMove = true
            for(var o = 0; o < possibleMoves.length; o++){
                var move = possibleMoves[o]
                var possibleEntity = this.entityAt(move.x + this.x, move.y + this.y)
                if(possibleEntity){
                    console.log("player attacked")
                    if(possibleEntity.type === "player"){
                        this.attack(possibleEntity)
                        canStillMove = false
                        break;
                    }
                }
            }

            if(canStillMove){
                var bestMove = {x: 0, y: 0}

                for(var i = 0; i < possibleMoves.length; i++){
                    var newMove = {x: this.x + possibleMoves[i].x, y: this.y + possibleMoves[i].y}
                    if(distance(newMove,playerPosition)<=distance({x: this.x + bestMove.x, y: this.y + bestMove.y },playerPosition)){
                        bestMove = possibleMoves[i]
                    }
                }
                var x = this.x + bestMove.x
                var y = this.y + bestMove.y
                if(!isColliderAt(x,y) && this.entityAt(x,y) == undefined){
                    this.x += bestMove.x
                    this.y += bestMove.y
                }
            }
        } else {
            // move around a bit
            const moveIndex = floor(random(possibleMoves.length + 10))

            if(moveIndex < possibleMoves.length){
                const randomMove = possibleMoves[moveIndex]
                const x = this.x + randomMove.x
                const y = this.y + randomMove.y

                if(!isColliderAt(x,y)){
                    this.x += randomMove.x
                    this.y += randomMove.y
                }
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.12159165035877365, 'fraction_numerical': 0.0073059360730593605, 'mean_word_length': 1.3393347574000611, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '606367', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2227, 'n_words': 495}