BlueGit

timfa questward

Files

questward / shared / definitions / mobCombat.js

mobCombat.js

mobCombat.js 2.7 KiB Download Raw
1import { mobDefinition, mobMaxHealthForLevel } from "./mobs.js";
2import { SkillId, UNARMED_ATTACK, ALL_SKILL_IDS } from "./skills.js";
3
4export const DEFAULT_AGGRO_RANGE_TILES = 5;
5
6/** Living mobs with health can fight unless explicitly non-combat (pigeon, etc.). */
7export 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
16export function mobCanFightType (mobTypeId)
17{
18 return mobCanFight(mobDefinition(mobTypeId));
19}
20
21/** Aggressive mobs auto-engage players in range with line of sight. */
22export function mobIsAggressive (def)
23{
24 return !!(def?.aggressive && mobCanFight(def));
25}
26
27export 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. */
34export 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. */
50export 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 */
57export 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 */
67export 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
79export function mobHealthForLevel (def, level)
80{
81 return mobMaxHealthForLevel(def, level);
82}
83
84/**
85 * AI chance (1–100) that a fighting mob *attempts* a known spell instead of melee.
86 * Separate from channel success ({@link spellCastSuccessChancePercent}).
87 */
88export 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
94export function fallbackMobAttackProfile ()
95{
96 return { ...UNARMED_ATTACK, mobAttackSkill: 1 };
97}