BlueGit

timfa questward

Files

questward / shared / definitions / mobCombat.js

mobCombat.js

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