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