import { M5Item } from "../items/M5Item"; import { M5Attribute, M5CharacterCalculatedData, M5ItemMod, M5ItemType, M5ModOperation, M5ModResult, M5RollData, M5Skill, M5SkillCalculated } from "../M5Base"; import M5ModAggregate from "./M5ModAggregate"; export class M5Character extends Actor { // constructor( // data: ConstructorParameters[0], // context?: ConstructorParameters[1] // ) { // super(data, context) // this.prepareDerivedData() // } static attributeMinMax(attribute: M5Attribute) { return Math.min(100, Math.max(0, attribute.value + attribute.bonus)); } static attributeBonus(attribute: M5Attribute) { const value = this.attributeMinMax(attribute); if (value > 95) return 2; if (value > 80) return 1; if (value > 20) return 0; if (value > 5) return -1; return -2; } static loadValue(attribute: M5Attribute) { const value = this.attributeMinMax(attribute); if (value > 99) return 35; if (value > 95) return 30; if (value > 80) return 25; if (value > 60) return 20; if (value > 30) return 15; if (value > 10) return 10; if (value > 0) return 5; return 0; } static heavyLoadValue(attribute: M5Attribute) { const value = this.attributeMinMax(attribute); if (value > 99) return 50; if (value > 95) return 45; if (value > 80) return 40; if (value > 60) return 35; if (value > 30) return 30; if (value > 10) return 25; if (value > 0) return 20; return 0; } static maxLoadValue(attribute: M5Attribute) { const value = this.attributeMinMax(attribute); if (value > 99) return 90; if (value > 95) return 80; if (value > 80) return 75; if (value > 60) return 70; if (value > 30) return 60; if (value > 10) return 50; if (value > 0) return 40; return 0; } static thrustLoadValue(attribute: M5Attribute) { const value = this.attributeMinMax(attribute); if (value > 99) return 200; if (value > 95) return 170; if (value > 80) return 150; if (value > 60) return 140; if (value > 30) return 120; if (value > 10) return 70; if (value > 0) return 50; return 0; } derivedData( skip: { mods?: boolean; skills?: boolean; items?: boolean; spells?: boolean; effects?: boolean; containers?: boolean; kampfkuenste?: boolean; encumbrance?: boolean; class?: boolean; } = {} ): M5CharacterCalculatedData { let ret: M5CharacterCalculatedData = { level: 0, attributes: { st: { value: 0, bonus: 0, mods: [] }, gs: { value: 0, bonus: 0, mods: [] }, gw: { value: 0, bonus: 0, mods: [] }, ko: { value: 0, bonus: 0, mods: [] }, in: { value: 0, bonus: 0, mods: [] }, zt: { value: 0, bonus: 0, mods: [] }, au: { value: 0, bonus: 0, mods: [] }, pa: { value: 0, bonus: 0, mods: [] }, wk: { value: 0, bonus: 0, mods: [] }, }, stats: { lp: { value: 0, mods: [] }, ap: { value: 0, mods: [] }, lpProtection: { value: 0, mods: [] }, apProtection: { value: 0, mods: [] }, defense: { value: 0, mods: [] }, damageBonus: { value: 0, mods: [] }, attackBonus: { value: 0, mods: [] }, defenseBonus: { value: 0, mods: [] }, movement: { value: 0, mods: [] }, resistanceMind: { value: 0, mods: [] }, resistanceBody: { value: 0, mods: [] }, spellCasting: { value: 0, mods: [] }, brawl: { value: 0, mods: [] }, brawlFw: 0, poisonResistance: { value: 0, mods: [] }, enduranceBonus: 0, perception: { value: 0, mods: [] }, perceptionFW: 0, drinking: { value: 0, mods: [] }, drinkingFW: 0, hoard: 0, encumbrance: 0, load: 0, heavyLoad: 0, thrustLoad: 0, loadMax: 0, }, skillMods: {}, skills: { innate: {}, general: {}, combat: {}, language: {}, custom: {}, }, gear: { weapons: {}, defensiveWeapons: {}, armor: {}, items: {}, containers: {}, effects: {}, }, spells: {}, kampfkuenste: {}, } as M5CharacterCalculatedData; const context = this as any; if (!context) return null; const data = (this as any).system; if (!data) return null; ret.level = M5Character.levelFromExp(data.info.race === "Zwerg" ? Math.min(data.calc.stats?.hoard * 2 || 0, data.es) : data.es); ret.attributes.st.value = M5Character.attributeMinMax(data.attributes.st); // TODO item effects ret.attributes.gs.value = M5Character.attributeMinMax(data.attributes.gs); ret.attributes.gw.value = M5Character.attributeMinMax(data.attributes.gw); ret.attributes.ko.value = M5Character.attributeMinMax(data.attributes.ko); ret.attributes.in.value = M5Character.attributeMinMax(data.attributes.in); ret.attributes.zt.value = M5Character.attributeMinMax(data.attributes.zt); ret.attributes.au.value = M5Character.attributeMinMax(data.attributes.au); ret.attributes.pa.value = M5Character.attributeMinMax(data.attributes.pa); ret.attributes.wk.value = M5Character.attributeMinMax(data.attributes.wk); ret.attributes.st.bonus = M5Character.attributeBonus(data.attributes.st); ret.attributes.gs.bonus = M5Character.attributeBonus(data.attributes.gs); ret.attributes.gw.bonus = M5Character.attributeBonus(data.attributes.gw); ret.attributes.ko.bonus = M5Character.attributeBonus(data.attributes.ko); ret.attributes.in.bonus = M5Character.attributeBonus(data.attributes.in); ret.attributes.zt.bonus = M5Character.attributeBonus(data.attributes.zt); ret.attributes.au.bonus = M5Character.attributeBonus(data.attributes.au); ret.attributes.pa.bonus = M5Character.attributeBonus(data.attributes.pa); ret.attributes.wk.bonus = M5Character.attributeBonus(data.attributes.wk); ret.stats.lp = this.modResult(data.lp); ret.stats.ap = this.modResult(data.ap); ret.stats.lpProtection = this.modResult(0); ret.stats.apProtection = this.modResult(0); ret.stats.defense = this.modResult(M5Character.defenseFromLevel(ret.level)); ret.stats.damageBonus = this.modResult(Math.floor(ret.attributes.st.value / 20) + Math.floor(ret.attributes.gs.value / 30) - 3); ret.stats.attackBonus = this.modResult(ret.attributes.gs.bonus); ret.stats.defenseBonus = this.modResult(ret.attributes.gw.bonus); ret.stats.movement = this.modResult(data.movement); ret.stats.resistanceMind = this.modResult(ret.stats.defense.value + (data.info.race === "Mensch" ? ret.attributes.in.bonus : this.raceBonus(data.info.race))); ret.stats.resistanceBody = this.modResult(ret.stats.defense.value + (data.info.race === "Mensch" ? ret.attributes.ko.bonus : this.raceBonus(data.info.race))); ret.stats.spellCasting = this.modResult((data.info.magicUsing ? M5Character.spellCastingFromLevel(ret.level) : 3) + ret.attributes.zt.bonus); ret.stats.brawl = this.modResult(Math.floor((ret.attributes.st.value + ret.attributes.gw.value) / 20)); ret.stats.brawlFw = ret.stats.brawl.value + ret.stats.attackBonus.value + (data.info.race === "Zwerg" ? 1 : 0); ret.stats.poisonResistance = this.modResult(30 + Math.floor(ret.attributes.ko.value / 2)); ret.stats.enduranceBonus = Math.floor(ret.attributes.ko.value / 10) + Math.floor(ret.attributes.st.value / 20); ret.stats.perception = this.modResult(0); ret.stats.perceptionFW = 6; ret.stats.drinking = this.modResult(0); ret.stats.drinkingFW = Math.floor(ret.attributes.ko.value / 10); ret.stats.hoardMin = M5Character.levelThreshold.at(ret.level - 1) / 2; ret.stats.hoardNext = M5Character.levelThreshold.at(ret.level) / 2; ret.stats.wealth = parseFloat((data.info.gold + data.info.silver / 10 + data.info.copper / 100).toPrecision(3)); ret.stats.hoard = 0; ret.stats.load = M5Character.loadValue(data.attributes.st); ret.stats.heavyLoad = M5Character.heavyLoadValue(data.attributes.st); ret.stats.loadMax = M5Character.maxLoadValue(data.attributes.st); ret.stats.thrustLoad = M5Character.thrustLoadValue(data.attributes.st); ret.stats.encumbrance = 0; if (!skip?.mods) { const aggregate = new M5ModAggregate(data, ret); context.items ?.filter((item) => (item.type === "item" || item.type === "effect" || item.type === "armor" || item.type === "container" || item.type === "class") && item.system.equipped) .forEach((item) => { const mods = item.system.mods; //console.log("Actor item mods", mods) Object.keys(mods).forEach((modIndex) => { const mod = mods[modIndex] as M5ItemMod; aggregate.push(mod, item.name); }); }); ret.skillMods = aggregate.calculate(); } if (!skip?.containers) { context.items ?.filter((item) => item.type === "container") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); let label = item.name; if (item.system.magic) { label += "*"; } let icon = item.img; let rollable = false; for (let key in item.system.rolls.formulas) { rollable = item.system.rolls.formulas[key]?.enabled; if (rollable) { break; } } ret.gear.containers[item.id] = { label: label, icon: icon, magic: item.system.magic, valuable: item.system?.valuable, hoarded: item.system?.hoarded, calc: item.system.calc, equipped: item.system?.equipped, weight: item.system.weight || 0, value: item.system.value || 0, currency: item.system.currency || "", quantity: item.system.quantity || 0, rollExist: rollable, }; }); } if (!skip?.items) { context.items ?.filter((item) => item.type === "item") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); let label = item.name; if (item.system.magic) { label += "*"; } if (item.system.valuable) { ret.stats.wealth += parseFloat(this.calculateValue(item.system.value * item.system.quantity, item.system.currency).toPrecision(3)); } if (item.system.hoarded) { ret.stats.hoard += parseFloat(this.calculateValue(item.system.value * item.system.quantity, item.system.currency).toPrecision(3)); } if (!!item.system.containerId) { ret.gear.containers[item.system.containerId].weight += parseFloat((item.system.weight * item.system.quantity).toPrecision(4)); if (ret.gear.containers[item.system.containerId].equipped) { ret.stats.encumbrance += item.system.weight * item.system.quantity; } } else if (item.system.equipped) { ret.stats.encumbrance += item.system.weight * item.system.quantity; } let icon = item.img; let rollable = false; // console.log(item.system.rolls.formulas.map((p) => p.enabled)); for (let key in item.system.rolls.formulas) { rollable = item.system.rolls.formulas[key]?.enabled; if (rollable) { break; } } ret.gear.items[item.id] = { label: label, icon: icon, magic: item.system.magic, calc: item.system.calc, equipped: item.system?.equipped, valuable: item.system?.valuable, hoarded: item.system?.hoarded, weight: item.system.weight || 0, containerId: item.system.containerId || "", value: item.system.value || 0, currency: item.system.currency || "", quantity: item.system.quantity || 0, rollExist: rollable, }; }); context.items ?.filter((item) => item.type === "weapon") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); let label = item.name; if (item.system.magic) { label += "*(" + (item.system.stats.attackBonus < 0 ? "" : "+") + item.system.stats.attackBonus + "/" + (item.system.stats.damageBonus < 0 ? "" : "+") + item.system.stats.damageBonus + ")"; } if (item.system.valuable) { ret.stats.wealth += this.calculateValue(item.system.value * item.system.quantity, item.system.currency); } if (item.system.hoarded) { ret.stats.hoard += this.calculateValue(item.system.value * item.system.quantity, item.system.currency); } if (!!item.system.containerId) { ret.gear.containers[item.system.containerId].weight += item.system.weight; if (ret.gear.containers[item.system.containerId].equipped) { ret.stats.encumbrance += item.system.weight; } } else if (item.system.equipped) { ret.stats.encumbrance += item.system.weight || 0; } ret.gear.weapons[item.id] = { label: label, skillId: item.system.skillId, magic: item.system.magic, valuable: item.system?.valuable, hoarded: item.system?.hoarded, value: item.system.value || 0, currency: item.system.currency || "", calc: item.system.calc, special: item.system.special, damageBase: item.system.damageBase, equipped: item.system?.equipped, weight: item.system.weight || 0, containerId: item.system.containerId || "", }; }); context.items ?.filter((item) => item.type === "defensiveWeapon") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); let label = item.name; if (item.system.magic) { label += "*(" + (item.system.stats.defenseBonus < 0 ? "" : "+") + item.system.stats.defenseBonus + ")"; } if (item.system.valuable) { ret.stats.wealth += this.calculateValue(item.system.value * item.system.quantity, item.system.currency); } if (item.system.hoarded) { ret.stats.hoard += this.calculateValue(item.system.value * item.system.quantity, item.system.currency); } if (!!item.system.containerId) { ret.gear.containers[item.system.containerId].weight += item.system.weight; if (ret.gear.containers[item.system.containerId].equipped) { ret.stats.encumbrance += item.system.weight; } } else if (item.system.equipped) { ret.stats.encumbrance += item.system.weight || 0; } ret.gear.defensiveWeapons[item.id] = { label: label, skillId: item.system.skillId, magic: item.system.magic, valuable: item.system?.valuable, hoarded: item.system?.hoarded, value: item.system.value || 0, currency: item.system.currency || "", defenseBonus: item.system.stats.defenseBonus, calc: item.system.calc, equipped: item.system?.equipped, weight: item.system.weight || 0, containerId: item.system.containerId || "", }; }); context.items ?.filter((item) => item.type === "armor") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); let label = item.name; if (item.system.magic) { label += "*"; } if (item.system.valuable) { ret.stats.wealth += this.calculateValue(item.system.value * item.system.quantity, item.system.currency); } if (item.system.hoarded) { ret.stats.hoard += this.calculateValue(item.system.value * item.system.quantity, item.system.currency); } if (!!item.system.containerId) { ret.gear.containers[item.system.containerId].weight += item.system.weight; if (ret.gear.containers[item.system.containerId].equipped) { ret.stats.encumbrance += item.system.weight; } } else if (item.system.equipped) { ret.stats.encumbrance += 0; } else { ret.stats.encumbrance += item.system.weight || 0; } ret.gear.armor[item.id] = { label: label, magic: item.system.magic, valuable: item.system?.valuable, hoarded: item.system?.hoarded, value: item.system.value || 0, currency: item.system.currency || "", lpProtection: item.system.lpProtection, calc: item.system.calc, equipped: item.system?.equipped, weight: item.system.weight || 0, containerId: item.system.containerId || "", }; }); // if (!skip?.encumbrance) { // const item = context.items.filter((x) => x.name === "Belastung"); // if (ret.stats.encumbrance > ret.stats.heavyLoad) { // if (item.length === 0) { // this.createEffect("Belastung", [{ id: "movement", operation: M5ModOperation.DIVISION, type: M5ModType.STAT, value: 2 }]); // } else if (item.length === 1) { // item[0].update({ // data: { // equipped: true, // }, // }); // } else if (item.length === 2) { // item[1]?.delete(); // } // } else if (ret.stats.encumbrance <= ret.stats.heavyLoad) { // if (item.length === 1) { // item[0]?.update({ // data: { // equipped: false, // }, // }); // } // } // } // if (!skip?.encumbrance) { // const item = context.items.filter((x) => x.name === "Höchstlast"); // if (ret.stats.encumbrance > ret.stats.loadMax) { // if (item.length === 0) { // this.createEffect("Höchstlast", [{ id: "ap", operation: M5ModOperation.SUBTRACT, type: M5ModType.STAT, value: 1 }]); // } else if (item.length === 1) { // item[0].update({ // data: { // equipped: true, // }, // }); // } else if (item.length === 2) { // item[1]?.delete(); // } // } else if (ret.stats.encumbrance <= ret.stats.loadMax) { // if (item.length === 1) { // item[0]?.update({ // data: { // equipped: false, // }, // }); // } // } // } } if (!skip?.class) { const item = context.items ?.filter((item) => item.type === "class") .forEach((item) => { if (item) { if (typeof data.info.class === "string") { data.info.class = {}; } data.info.class[item.id] = item.name; if (item.system.magicUsing) { data.info.magicUsing = item.system.magicUsing; } } }); } if (!skip?.effects) { context.items ?.filter((item) => item.type === "effect") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); let label = item.name; if (item.system.magic) { label += "*"; } ret.gear.effects[item.id] = { label: label, magic: item.system.magic, calc: item.system.calc, equipped: item.system?.equipped || false, duration: item.system?.duration || { time: 0, unit: "" }, }; }); } if (!skip?.skills) { context.items ?.filter((item) => item.type === "skill") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); const skillMap = ret.skills[item.system.type]; skillMap[item.id] = { label: item.name, fw: item.system.fw, attribute: item.system.attribute, pp: item.system.pp, calc: item.system.calc, } as M5SkillCalculated; // Adjust attribute Aussehen based on Athletik skill if (item.name === "Athletik") { ret.attributes.au.value += Math.floor(item.system.fw / 3); } // Adjust stat Bewegungsweite based on Laufen skill if (item.name === "Laufen") { ret.stats.movement.value += Math.floor(item.system.fw / 3); } }); } if (!skip?.spells) { context.items ?.filter((item) => item.type === "spell") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); ret.spells[item.id] = { label: item.name, process: "midgard5.spell-process-" + item.system.process, calc: item.system.calc, type: item.system.type, castDuration: item.system.castDuration || 0, ap: item.system.ap || 0, range: item.system.range || 0, effectTarget: item.system.effectTarget, effectArea: item.system.effectArea, effectDuration: item.system.effectDuration || 0, equipped: item.system?.equipped || false, }; }); } if (!skip?.kampfkuenste) { context.items ?.filter((item) => item.type === "kampfkunst") .sort((a, b) => a?.sort - b?.sort) .forEach((item) => { item.prepareDerivedData(); ret.kampfkuenste[item.id] = { label: item.name, isKido: item.system.isKido, type: item.system.type, variante: item.system.variante, calc: item.system.calc, }; }); } return ret; } raceBonus(race: string) { switch (race) { case "Elf": return 2; case "Gnom": return 4; case "Halbling": return 4; case "Zwerg": return 3; default: return 0; } } prepareDerivedData() { console.log("M5Character", "prepareDerivedData"); const data = (this as any).system; data.calc = this.derivedData({}); } override getRollData(): any { return { c: (this as any).system, i: null, iType: null, rolls: {}, res: {}, } as M5RollData; } static readonly levelThreshold: Array = [ 0, 100, 250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000, 12500, 15000, 17500, 20000, 22500, 25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000, 75000, 80000, 85000, 90000, 95000, 100000, 105000, 110000, 115000, 120000, 125000, 130000, 135000, 140000, 145000, 150000, 155000, 160000, 165000, 170000, 175000, 180000, 185000, 190000, 195000, 200000, 205000, 210000, 215000, 220000, 225000, 230000, 235000, 240000, 245000, 250000, 255000, 260000, 265000, 270000, 275000, 280000, ]; static levelFromExp(exp: number): number { const ret = M5Character.levelThreshold.findIndex((val) => val > exp); return ret === -1 ? M5Character.levelThreshold.length : ret; } static readonly defenseThreshold: Array<[number, number]> = [ [30, 18], [25, 17], [20, 16], [15, 15], [10, 14], [5, 13], [2, 12], [1, 11], ]; static defenseFromLevel(lvl: number): number { const ret = M5Character.defenseThreshold.find((val) => val[0] <= lvl); return ret ? ret[1] : M5Character.defenseThreshold[M5Character.defenseThreshold.length - 1][1]; } static readonly spellCastingThreshold: Array<[number, number]> = [ [20, 18], [15, 17], [10, 16], [8, 15], [6, 14], [4, 13], [2, 12], [1, 11], ]; static spellCastingFromLevel(lvl: number): number { const ret = M5Character.spellCastingThreshold.find((val) => val[0] <= lvl); return ret ? ret[1] : M5Character.spellCastingThreshold[M5Character.spellCastingThreshold.length - 1][1]; } skillBonus(skill: M5Skill, skillName?: string) { const data = (this as any).system; return data.calc?.attributes[skill.attribute]?.bonus ?? 0; } skillEw(skill: M5Skill, skillName?: string) { const bonus = this.skillBonus(skill, skillName); return skill.fw + bonus; } attribute(name: string): M5Attribute { const data = (this as any).system; return data?.attributes[name]; } async createSkill(skillName: string): Promise { const itemData = { name: skillName, type: "skill", }; return (this as any).createEmbeddedDocuments("Item", [itemData]).then((docs) => { const item = docs[0]; return item; }); } createItem(itemName: string, itemType: M5ItemType, options?: any): Promise { const itemData = { name: itemName, type: itemType, data: options, }; return (this as any).createEmbeddedDocuments("Item", [itemData]).then((docs) => { const item = docs[0]; return item; }); } async createEffect(itemName: string, mods: M5ItemMod[]): Promise { const itemData = { name: itemName, type: "effect", system: { mods: mods, equipped: true }, }; return (this as any).createEmbeddedDocuments("Item", [itemData]).then((docs) => { const item = docs[0]; return item; }); } getItem(itemId: string): any { if (!(this as any).items) return null; return (this as any).getEmbeddedDocument("Item", itemId); } private calculateValue(value: number, currency: string): number { switch (currency) { case "gold": return value; case "silver": return parseFloat((value / 10).toPrecision(3)); case "copper": return parseFloat((value / 100).toPrecision(3)); default: return 0; } } private modResult(value: number): M5ModResult { return { value: value, mods: [ { item: (game as Game).i18n.localize("TYPES.Actor.character"), operation: M5ModOperation.SET, value: value, }, ], }; } }