220 lines
6.6 KiB
TypeScript
220 lines
6.6 KiB
TypeScript
import { ItemData } from "@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/module.mjs"
|
|
import { ConstructorDataType } from "@league-of-foundry-developers/foundry-vtt-types/src/types/helperTypes"
|
|
import { M5Character } from "../actors/M5Character"
|
|
import { enumKeys, M5Attributes, M5ItemMod, M5ModType, M5RollData, M5RollResult, M5Skill, M5Stats } from "../M5Base"
|
|
import { M5Roll } from "../rolls/M5Roll"
|
|
|
|
export class M5Item extends Item {
|
|
static readonly SKILL = "skill"
|
|
|
|
prepareDerivedData() {
|
|
const actor = (this.actor as any)
|
|
const character = actor as M5Character
|
|
const context = (this as any).data
|
|
const calc = context.data.calc
|
|
|
|
if (context.type === "skill") {
|
|
calc.bonus = 0
|
|
|
|
if (context.data?.attribute && context.data?.attribute !== "" && character) {
|
|
const attribute = character.attribute(context.data.attribute)
|
|
if (attribute)
|
|
calc.bonus += M5Character.attributeBonus(attribute)
|
|
}
|
|
|
|
calc.ew = context.data.fw + calc.bonus
|
|
} else if (context.type === "weapon") {
|
|
calc.fw = 0
|
|
calc.bonus = 0
|
|
calc.special = context.data.special ? 2 : 0
|
|
calc.ew = calc.special + context.data.stats.attackBonus
|
|
calc.combatSkills = null
|
|
|
|
if (actor) {
|
|
const actorCalc = character.derivedData({ weapons: true, defensiveWeapons: true, armor: true, items: true, spells: true })
|
|
if (actorCalc) {
|
|
calc.ew += actorCalc.stats.attackBonus
|
|
calc.combatSkills = actorCalc.skills.combat
|
|
}
|
|
|
|
const skill = character.getItem(context.data.skillId)
|
|
//console.log("M5Item.prepareDerivedData:weapon", context.data, skill?.data?.data)
|
|
if (skill) {
|
|
skill.prepareDerivedData()
|
|
const skillData = skill.data.data
|
|
calc.ew += skillData.calc.ew
|
|
calc.bonus += skillData.calc.bonus
|
|
calc.fw += skillData.fw
|
|
}
|
|
}
|
|
} else if (context.type === "defensiveWeapon") {
|
|
calc.fw = 0
|
|
calc.bonus = 0
|
|
calc.special = context.data.special ? 2 : 0
|
|
calc.ew = calc.special + context.data.stats.defenseBonus
|
|
calc.combatSkills = null
|
|
|
|
if (actor) {
|
|
const actorCalc = character.derivedData({ weapons: true, defensiveWeapons: true, armor: true, items: true, spells: true })
|
|
if (actorCalc) {
|
|
calc.ew += actorCalc.stats.defense + actorCalc.stats.defenseBonus
|
|
calc.combatSkills = actorCalc.skills.combat
|
|
}
|
|
|
|
const skill = character.getItem(context.data.skillId)
|
|
//console.log("M5Item.prepareDerivedData:weapon", context.data, skill?.data?.data)
|
|
if (skill) {
|
|
skill.prepareDerivedData()
|
|
const skillData = skill.data.data
|
|
calc.ew += skillData.calc.ew
|
|
calc.bonus += skillData.calc.bonus
|
|
calc.fw += skillData.fw
|
|
}
|
|
}
|
|
} else if (context.type === "spell") {
|
|
calc.ew = context.data.bonus
|
|
if (actor) {
|
|
const actorCalc = character.derivedData({ weapons: true, defensiveWeapons: true, armor: true, items: true, spells: true })
|
|
if (actorCalc) {
|
|
calc.ew += actorCalc.stats.spellCasting
|
|
}
|
|
}
|
|
} else if (context.type === "mod") {
|
|
const parent = (this as any).parent
|
|
const actor = parent?.actor
|
|
const character = actor as M5Character
|
|
|
|
calc.ids = {}
|
|
|
|
switch (context.data.type as M5ModType) {
|
|
case M5ModType.ATTRIBUTE: {
|
|
for (const key of enumKeys(M5Attributes)) {
|
|
const val = M5Attributes[key]
|
|
calc.ids[key] = (game as Game).i18n.localize(`midgard5.actor-${val}-long`)
|
|
}
|
|
break
|
|
}
|
|
case M5ModType.STAT: {
|
|
for (const key of enumKeys(M5Stats)) {
|
|
const val = M5Stats[key]
|
|
calc.ids[key] = (game as Game).i18n.localize(`midgard5.mod-stat-${val}`)
|
|
}
|
|
break
|
|
}
|
|
case M5ModType.SKILL: {
|
|
if (character) {
|
|
const actorCalc = character.derivedData({ weapons: true, defensiveWeapons: true, armor: true, items: true, spells: true })
|
|
if (actorCalc) {
|
|
let category = (game as Game).i18n.localize("midgard5.innate-ability")
|
|
Object.keys(actorCalc.skills.innate).forEach(skillId => {
|
|
const skill = character.getItem(skillId)
|
|
if (skill)
|
|
calc.ids[skillId] = `${category}: ${skill.data.name}`
|
|
})
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
} else if (context.type === "item") {
|
|
calc.mods = {}
|
|
Object.keys(context.data?.mods).forEach(key => {
|
|
const mod = context.data.mods[key]
|
|
const modCalc = {}
|
|
switch (mod.type) {
|
|
case M5ModType.ATTRIBUTE: {
|
|
for (const key of enumKeys(M5Attributes)) {
|
|
const val = M5Attributes[key]
|
|
modCalc[key] = (game as Game).i18n.localize(`midgard5.actor-${val}-long`)
|
|
}
|
|
break
|
|
}
|
|
case M5ModType.STAT: {
|
|
for (const key of enumKeys(M5Stats)) {
|
|
const val = M5Stats[key]
|
|
modCalc[key] = (game as Game).i18n.localize(`midgard5.mod-stat-${val}`)
|
|
}
|
|
break
|
|
}
|
|
case M5ModType.SKILL: {
|
|
if (character) {
|
|
const actorCalc = character.derivedData({ weapons: true, defensiveWeapons: true, armor: true, items: true, spells: true })
|
|
if (actorCalc) {
|
|
let category = (game as Game).i18n.localize("midgard5.innate-ability")
|
|
Object.keys(actorCalc.skills.innate).forEach(skillId => {
|
|
const skill = character.getItem(skillId)
|
|
if (skill)
|
|
modCalc[skillId] = `${category}: ${skill.data.name}`
|
|
})
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
calc.mods[key] = modCalc
|
|
})
|
|
}
|
|
}
|
|
|
|
getRollData() {
|
|
const actor = this.actor as any
|
|
const context = (this as any).data
|
|
|
|
let ret: M5RollData = actor?.getRollData() ?? {
|
|
c: null,
|
|
i: null,
|
|
iType: null,
|
|
rolls: {},
|
|
res: {}
|
|
}
|
|
|
|
ret.i = context.data
|
|
ret.iType = context.type
|
|
return ret
|
|
}
|
|
|
|
async roll() {
|
|
const item = (this as any).data
|
|
|
|
// Initialize chat data.
|
|
const speaker = ChatMessage.getSpeaker({ actor: this.actor })
|
|
const rollMode = (game as Game).settings.get('core', 'rollMode')
|
|
const label = `[${item.type}] ${item.name}`
|
|
|
|
// If there's no roll data, send a chat message.
|
|
const formulaNames = item.data.rolls?.formulas ? Object.keys(item.data.rolls.formulas) : []
|
|
if (formulaNames.length > 0) {
|
|
const rollData = this.getRollData()
|
|
formulaNames.forEach(formulaName => {
|
|
const formula = item.data.rolls.formulas[formulaName]
|
|
rollData.rolls[formulaName] = {
|
|
formula: formula.formula,
|
|
label: formula.label,
|
|
type: formula.type,
|
|
result: "",
|
|
total: 0,
|
|
totalStr: "",
|
|
dice: {}
|
|
} as M5RollResult
|
|
})
|
|
|
|
const roll = new M5Roll(rollData, this.actor, item.name)
|
|
return roll.toMessage()
|
|
} else {
|
|
ChatMessage.create({
|
|
speaker: speaker,
|
|
rollMode: rollMode,
|
|
flavor: label,
|
|
content: item.data.description ?? ''
|
|
})
|
|
return null
|
|
}
|
|
}
|
|
|
|
getItem(itemId: string): any {
|
|
return (this as any).getEmbeddedDocument("Item", itemId)
|
|
}
|
|
|
|
}
|