| 1 | import { mobDefinition, mobMaxHealthForLevel } from "./mobs.js"; |
| 2 | import { SkillId, UNARMED_ATTACK, ALL_SKILL_IDS } from "./skills.js"; |
| 3 | |
| 4 | export const DEFAULT_AGGRO_RANGE_TILES = 5; |
| 5 | |
| 6 | /** Living mobs with health can fight unless explicitly non-combat (pigeon, etc.). */ |
| 7 | export function mobCanFight (def) |
| 8 | { |
| 9 | if (!def || def.untargetable || def.freeFlight) |
| 10 | { return false; } |
| 11 | if (def.canFight === false) |
| 12 | { return false; } |
| 13 | return Number(def.health ?? def.maxHealth ?? 0) > 0 || def.usePlayerBody === true; |
| 14 | } |
| 15 | |
| 16 | export function mobCanFightType (mobTypeId) |
| 17 | { |
| 18 | return mobCanFight(mobDefinition(mobTypeId)); |
| 19 | } |
| 20 | |
| 21 | /** Aggressive mobs auto-engage players in range with line of sight. */ |
| 22 | export function mobIsAggressive (def) |
| 23 | { |
| 24 | return !!(def?.aggressive && mobCanFight(def)); |
| 25 | } |
| 26 | |
| 27 | export function mobAggroRangeTiles (def) |
| 28 | { |
| 29 | const range = def?.aggroRangeTiles | 0; |
| 30 | return range > 0 ? range : DEFAULT_AGGRO_RANGE_TILES; |
| 31 | } |
| 32 | |
| 33 | /** Melee profile for a mob: one attackSkill drives hit chance and damage. */ |
| 34 | export function mobMeleeAttackProfile (def, level = null) |
| 35 | { |
| 36 | const attackSkill = Math.max( |
| 37 | 1, |
| 38 | level != null ? (level | 0) : (def?.attackSkill | 0 || 1) |
| 39 | ); |
| 40 | return { |
| 41 | mode: "melee", |
| 42 | hitSkillId: SkillId.unarmed, |
| 43 | damageSkillId: SkillId.unarmed, |
| 44 | cooldownMs: def?.attackCooldownMs ?? 900, |
| 45 | mobAttackSkill: attackSkill |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | /** @deprecated Use shared/combat/rolls.js — kept for reference tests only. */ |
| 50 | export function mobHitChance (attackSkill) |
| 51 | { |
| 52 | const skill = Math.max(1, attackSkill | 0 || 1); |
| 53 | return Math.min(0.95, 0.45 + skill * 0.03); |
| 54 | } |
| 55 | |
| 56 | /** @deprecated Use shared/combat/rolls.js */ |
| 57 | export function mobDamage (attackSkill, baseDamage = 1) |
| 58 | { |
| 59 | const skill = Math.max(1, attackSkill | 0 || 1); |
| 60 | return Math.max(1, (baseDamage | 0) + Math.floor(skill / 5)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Flat skill map for a mob at the given level (all combat-relevant skills equal the level). |
| 65 | * Falls back to def.attackSkill when level is omitted (legacy wildlife). |
| 66 | */ |
| 67 | export function mobSkillLevels (def, level = null) |
| 68 | { |
| 69 | const attackSkill = Math.max( |
| 70 | 1, |
| 71 | level != null ? (Math.floor(Number(level) || 1)) : (def?.attackSkill | 0 || 1) |
| 72 | ); |
| 73 | const levels = {}; |
| 74 | for (const skillId of ALL_SKILL_IDS) |
| 75 | { levels[skillId] = attackSkill; } |
| 76 | return levels; |
| 77 | } |
| 78 | |
| 79 | export function mobHealthForLevel (def, level) |
| 80 | { |
| 81 | return mobMaxHealthForLevel(def, level); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Mob spell cast chance % derived from spawner/creature level (1–100). |
| 86 | * Editor shows this as a label; it is not authorable per spell. |
| 87 | */ |
| 88 | export function mobSpellCastChancePercent (level) |
| 89 | { |
| 90 | const lv = Math.max(1, Math.min(99, Math.floor(Number(level) || 1))); |
| 91 | return Math.max(1, Math.min(100, lv)); |
| 92 | } |
| 93 | |
| 94 | export function fallbackMobAttackProfile () |
| 95 | { |
| 96 | return { ...UNARMED_ATTACK, mobAttackSkill: 1 }; |
| 97 | } |