Added allies.

This commit is contained in:
raeleus 2025-05-17 09:17:21 -07:00
parent 1a6a605251
commit 8db353f486
4 changed files with 869 additions and 11 deletions

View file

@ -344,10 +344,13 @@ function executeTurn(activeCharacter) {
if (possessiveName == "Your") possessiveName = "your"
if (activeCharacter.className != null) {
//player
state.show = "none"
return `\n[It is ${possessiveName} turn]\n`
} else {
} else if (activeCharacter.ally == false) {
//enemy
var characters = state.characters.filter(x => x.health > 0)
characters.push(...state.allies.filter(x => x.health > 0))
var target = characters[getRandomInteger(0, characters.length - 1)]
var areWord = target.name == "You" ? "are" : "is"
var targetNameAdjustedCase = target.name == "You" ? "you" : toTitleCase(target.name)
@ -388,6 +391,49 @@ function executeTurn(activeCharacter) {
}
}
return text
} else {
//ally
var enemies = state.enemies.filter(x => x.health > 0)
var target = enemies[getRandomInteger(0, enemies.length - 1)]
var areWord = target.name == "You" ? "are" : "is"
var targetNameAdjustedCase = target.name == "You" ? "you" : toTitleCase(target.name)
var attack = calculateRoll(`1d20${activeCharacter.hitModifier > 0 ? "+" + activeCharacter.hitModifier : activeCharacter.hitModifier < 0 ? activeCharacter.hitModifier : ""}`)
var hit = attack >= target.ac
var text = `\n[It is ${possessiveName} turn]\n`
if (getRandomBoolean() || activeCharacter.spells.length == 0) {
if (hit) {
state.blockCharacter = target
state.blockPreviousHealth = target.health
var damage = isNaN(activeCharacter.damage) ? calculateRoll(activeCharacter.damage) : activeCharacter.damage
target.health = Math.max(target.health - damage, 0)
text += `\n[Enemy AC: ${target.ac} Attack roll: ${attack}]\n`
text += `${activeCharacterName} attacks ${targetNameAdjustedCase} for ${damage} damage!\n`
if (target.health == 0) text += ` ${toTitleCase(target.name)} ${areWord} unconscious! \n`
else text += ` ${toTitleCase(target.name)} ${areWord} at ${target.health} health.\n`
} else text += `${activeCharacterName} attacks ${targetNameAdjustedCase} but misses!\n`
} else {
var spell = activeCharacter.spells[getRandomInteger(0, activeCharacter.spells.length - 1)]
var diceMatches = spell.match(/(?<=^.*)\d*d\d+((\+|-)\d+)?$/gi)
if (diceMatches == null) text += `${activeCharacterName} casts spell ${spell}!`
else {
if (hit) {
var damage = calculateRoll(diceMatches[0])
var spell = spell.substring(0, spell.length - diceMatches[0].length)
target.health = Math.max(target.health - damage, 0)
text += `\n[Character AC: ${target.ac} Attack roll: ${attack}]\n`
text += `${activeCharacterName} casts spell ${spell} at ${targetNameAdjustedCase} for ${damage} damage!`
if (target.health == 0) text += ` ${toTitleCase(target.name)} ${areWord} unconscious!\n`
else text += ` ${toTitleCase(target.name)} ${areWord} at ${target.health} health.\n`
} else text += `${activeCharacterName} casts spell ${spell} at ${targetNameAdjustedCase} but misses!\n`
}
}
return text
}
}
@ -2775,11 +2821,26 @@ function createEnemy(name, health, ac, hitModifier, damage, initiative, ...spell
hitModifier: hitModifier,
damage: damage,
initiative: initiative,
spells: spells
spells: spells,
ally: false
}
return enemy
}
function createAlly(name, health, ac, hitModifier, damage, initiative, ...spells) {
var ally = {
name: name,
health: health,
ac: ac,
hitModifier: hitModifier,
damage: damage,
initiative: initiative,
spells: spells,
ally: true
}
return ally
}
function getUniqueName(name) {
const letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var letterIndex = 0
@ -2807,6 +2868,11 @@ function createInitiativeOrder() {
state.initiativeOrder.push(enemy)
}
for (var ally of state.allies) {
if (ally.health <= 0) continue
state.initiativeOrder.push(ally)
}
state.initiativeOrder.sort(function(a, b) {
return b.calculatedInitiative - a.calculatedInitiative;
});