Files
nasa-tawa/src/lib/characters.lua
T

110 lines
3.1 KiB
Lua

local classes = require "data.classes"
local constants = require "data.constants"
local images = require "data.images"
local races = require "data.races"
local characters = {}
local base_stat_roll = function()
return 3 + math.random(1, 6) + math.random(1, 6) + math.random(1, 6)
end
local naive_random_name = function(length)
local vowels = "aaeeeiioouuy"
local consonants = "bcdfghjklmnpqrstvwxyz"
local text = ""
for i = 1, length do
local c
if i % 2 == 0 then
c = math.random(1, #vowels)
text = text .. vowels:sub(c, c)
else
c = math.random(1, #consonants)
text = text .. consonants:sub(c, c)
end
end
return text
end
local progress_quest_random_name = function()
local parts = {
("br|cr|dr|fr|gr|j|kr|l|m|n|pr||||r|sh|tr|v|wh|x|y|z"):split("|"),
("a|a|e|e|i|i|o|o|u|u|ae|ie|oo|ou"):split("|"),
("b|ck|d|g|k|m|n|p|t|v|x|z"):split("|"),
}
local name = ""
for i = 0, 5 do
local part = parts[i % 3 + 1]
name = name .. part[math.random(1, #part)]
end
return name
end
local random_stat_up = function(character)
local core_stats = {
"charisma", "constitution", "dexterity", "intelligence", "strength", "wisdom",
}
local selected_stat = core_stats[math.random(1, #core_stats)]
character[selected_stat] = character[selected_stat] + 1
end
characters.new = function(name)
name = name or progress_quest_random_name() -- name not affected by seed
local seed = math.random()
math.randomseed(seed)
local character = {
seed = seed,
name = name,
race = races[math.random(1, #races)],
class = classes[math.random(1, #classes)],
charisma = base_stat_roll(),
constitution = base_stat_roll(),
dexterity = base_stat_roll(),
intelligence = base_stat_roll(),
strength = base_stat_roll(),
wisdom = base_stat_roll(),
level = 1,
experience = 0,
}
character.health = math.random(1, 8) + math.floor(character.constitution / 6)
character.magic = math.random(1, 8) + math.floor(character.intelligence / 6)
for _, stat_name in ipairs(races[character.race].stats) do
character[stat_name] = character[stat_name] + math.random(1, 3)
end
for _, stat_name in ipairs(classes[character.class].stats) do
character[stat_name] = character[stat_name] + math.random(1, 3)
end
character.max_encumberance = character.strength + 10
character.inventory = {}
character.x = 1
character.y = 1
return setmetatable(character, { __index = characters, })
end
characters.level_up = function(character)
character.health = character.health + 1 + math.floor(character.constitution / 3) + math.random(1, 4)
character.magic = character.magic + 1 + math.floor(character.intelligence / 3) + math.random(1, 4)
random_stat_up(character)
random_stat_up(character)
character.max_encumberance = character.strength + 10
-- TODO this is where a spell could be added, and
end
characters.draw = function(self)
local icon = races[self.race].icon or "todo"
love.graphics.draw(images.love_image, images[icon], self.x * constants.tile_size, self.y * constants.tile_size)
end
return characters