<commit_before>local Gamestate = require 'vendor/gamestate'
local Level = require 'level'
local window = require 'window'
local state = Gamestate.new()

local Wardrobe = {}
Wardrobe.__index = Wardrobe

function Wardrobe.create(character)
    local drobe = {}
    setmetatable(drobe, Wardrobe)

    drobe.character = character
    drobe.count = 1

    drobe.image = love.graphics.newImage(character.costumes[1].sheet)
    drobe.image:setFilter('nearest', 'nearest')
    drobe.mask = love.graphics.newQuad(0, character.offset, 48, 27,
                                       drobe.image:getWidth(),
                                       drobe.image:getHeight())
    return drobe
end

function Wardrobe:newCharacter()
    local sprite = self.character.new(self.image)
    sprite.ow = self.character.ow
    return sprite
end

function Wardrobe:getCostume()
    return self.character.costumes[self.count]
end

function Wardrobe:prevCostume()
    self.count = (self.count - 1)
    if self.count == 0 then
      self.count = (# self.character.costumes)
    end
    self:loadCostume()
end

function Wardrobe:nextCostume()
    self.count = math.max((self.count + 1) % (# self.character.costumes + 1), 1)
    self:loadCostume()
end

function Wardrobe:loadCostume()
    self.image = love.graphics.newImage(self.character.costumes[self.count].sheet)
    self.mask = love.graphics.newQuad(0, self.character.offset, 48, 27,
                                       self.image:getWidth(),
                                       self.image:getHeight())
end

function Wardrobe:draw(x, y, flipX)
    love.graphics.drawq(self.image, self.mask, x, y, 0, flipX, 1)
end


local main_selections = {}
main_selections[0] = {}
main_selections[1] = {}
main_selections[1][0] = Wardrobe.create(require 'characters/troy')
main_selections[1][1] = Wardrobe.create(require 'characters/shirley')
main_selections[1][2] = Wardrobe.create(require 'characters/pierce')
main_selections[0][0] = Wardrobe.create(require 'characters/jeff')
main_selections[0][1] = Wardrobe.create(require 'characters/britta')
main_selections[0][2] = Wardrobe.create(require 'characters/abed')
main_selections[0][3] = Wardrobe.create(require 'characters/annie')

local alt_selections = {}
alt_selections[0] = {}
alt_selections[1] = {}
alt_selections[1][0] = Wardrobe.create(require 'characters/fatneil')
alt_selections[1][1] = Wardrobe.create(require 'characters/chang')
alt_selections[1][2] = Wardrobe.create(require 'characters/vicedean')

local main_selected = true
local selections = main_selections

function state:init()
    self.side = 0 -- 0 for left, 1 for right
    self.level = 0 -- 0 through 3 for characters
    self.screen = love.graphics.newImage("images/selectscreen.png")
    self.arrow = love.graphics.newImage("images/arrow.png")
    self.tmp = love.graphics.newImage('images/jeff.png')
end

function state:enter(previous)
    self.previous = previous
    self.music = love.audio.play("audio/opening.ogg", "stream", true)
end

function state:wardrobe()
    return selections[self.side][self.level]
end

function state:keypressed(key)
    local level = self.level
    local options = 4

    if key == 'left' or key == 'right' or key == 'a' or key == 'd' then
        self.side = (self.side - 1) % 2
    elseif key == 'up' or key == 'w' then
        level = (self.level - 1) % options
    elseif key == 'down' or key == 's' then
        level = (self.level + 1) % options
    end

    if key == 'tab' then
        if self.level == 3 and self.side == 1 then
            return
        elseif love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
            self:wardrobe():prevCostume()
        else
            self:wardrobe():nextCostume()
        end
        return
    end

    self.level = level

    if key == 'escape' then
        Gamestate.switch('home')
        return
    end
    
    if key == 'return' and self.level == 3 and self.side == 1 then
        if main_selected then
            selections = alt_selections
            main_selected = false
        else
            selections = main_selections
            main_selected = true
        end
    elseif key == 'return' then
        local wardrobe = self:wardrobe()

        if wardrobe then
            local level = Gamestate.get('overworld')
            level:reset()
            Gamestate.switch('overworld', wardrobe:newCharacter())
        end
    end
end
<commit_msg>Prevent crashes on selection screen.<commit_after>local Gamestate = require 'vendor/gamestate'
local Level = require 'level'
local window = require 'window'
local state = Gamestate.new()

local Wardrobe = {}
Wardrobe.__index = Wardrobe

function Wardrobe.create(character)
    local drobe = {}
    setmetatable(drobe, Wardrobe)

    drobe.character = character
    drobe.count = 1

    drobe.image = love.graphics.newImage(character.costumes[1].sheet)
    drobe.image:setFilter('nearest', 'nearest')
    drobe.mask = love.graphics.newQuad(0, character.offset, 48, 27,
                                       drobe.image:getWidth(),
                                       drobe.image:getHeight())
    return drobe
end

function Wardrobe:newCharacter()
    local sprite = self.character.new(self.image)
    sprite.ow = self.character.ow
    return sprite
end

function Wardrobe:getCostume()
    return self.character.costumes[self.count]
end

function Wardrobe:prevCostume()
    self.count = (self.count - 1)
    if self.count == 0 then
      self.count = (# self.character.costumes)
    end
    self:loadCostume()
end

function Wardrobe:nextCostume()
    self.count = math.max((self.count + 1) % (# self.character.costumes + 1), 1)
    self:loadCostume()
end

function Wardrobe:loadCostume()
    self.image = love.graphics.newImage(self.character.costumes[self.count].sheet)
    self.mask = love.graphics.newQuad(0, self.character.offset, 48, 27,
                                       self.image:getWidth(),
                                       self.image:getHeight())
end

function Wardrobe:draw(x, y, flipX)
    love.graphics.drawq(self.image, self.mask, x, y, 0, flipX, 1)
end


local main_selections = {}
main_selections[0] = {}
main_selections[1] = {}
main_selections[1][0] = Wardrobe.create(require 'characters/troy')
main_selections[1][1] = Wardrobe.create(require 'characters/shirley')
main_selections[1][2] = Wardrobe.create(require 'characters/pierce')
main_selections[0][0] = Wardrobe.create(require 'characters/jeff')
main_selections[0][1] = Wardrobe.create(require 'characters/britta')
main_selections[0][2] = Wardrobe.create(require 'characters/abed')
main_selections[0][3] = Wardrobe.create(require 'characters/annie')

local alt_selections = {}
alt_selections[0] = {}
alt_selections[1] = {}
alt_selections[1][0] = Wardrobe.create(require 'characters/fatneil')
alt_selections[1][1] = Wardrobe.create(require 'characters/chang')
alt_selections[1][2] = Wardrobe.create(require 'characters/vicedean')

local main_selected = true
local selections = main_selections

function state:init()
    self.side = 0 -- 0 for left, 1 for right
    self.level = 0 -- 0 through 3 for characters
    self.screen = love.graphics.newImage("images/selectscreen.png")
    self.arrow = love.graphics.newImage("images/arrow.png")
    self.tmp = love.graphics.newImage('images/jeff.png')
end

function state:enter(previous)
    self.previous = previous
    self.music = love.audio.play("audio/opening.ogg", "stream", true)
end

function state:wardrobe()
    return selections[self.side][self.level]
end

function state:keypressed(key)
    local level = self.level
    local options = 4

    if key == 'left' or key == 'right' or key == 'a' or key == 'd' then
        self.side = (self.side - 1) % 2
    elseif key == 'up' or key == 'w' then
        level = (self.level - 1) % options
    elseif key == 'down' or key == 's' then
        level = (self.level + 1) % options
    end

    if key == 'tab' then
        if self.level == 3 and self.side == 1 then
            return
        elseif love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
            local wardrobe = self:wardrobe()
            if wardrobe then
                wardrobe:prevCostume()
            end
        else
            local wardrobe = self:wardrobe()
            if wardrobe then
                wardrobe:nextCostume()
            end
        end
        return
    end

    self.level = level

    if key == 'escape' then
        Gamestate.switch('home')
        return
    end
    
    if key == 'return' and self.level == 3 and self.side == 1 then
        if main_selected then
            selections = alt_selections
            main_selected = false
        else
            selections = main_selections
            main_selected = true
        end
    elseif key == 'return' then
        local wardrobe = self:wardrobe()

        if wardrobe then
            local level = Gamestate.get('overworld')
            level:reset()
            Gamestate.switch('overworld', wardrobe:newCharacter())
        end
    end
end

{'fraction_non_alphanumeric': 0.1206915659980051, 'fraction_numerical': 0.0128560345782999, 'mean_word_length': 2.791596638655462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7411223', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2783, 'n_words': 638}