BlueGit

timfa questward

Files

questward / shared / definitions / mobCombat.js

mobCombat.js

mobCombat.js 2.0 KiB Download Raw
1import { mobDefinition } from "./mobs.js";
2import { SkillId, UNARMED_ATTACK } 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;
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)
35{
36 const attackSkill = Math.max(1, def?.attackSkill | 0 || 1);
37 return {
38 mode: "melee",
39 hitSkillId: SkillId.unarmed,
40 damageSkillId: SkillId.unarmed,
41 cooldownMs: def?.attackCooldownMs ?? 900,
42 mobAttackSkill: attackSkill
43 };
44}
45
46/** @deprecated Use shared/combat/rolls.js — kept for reference tests only. */
47export function mobHitChance (attackSkill)
48{
49 const skill = Math.max(1, attackSkill | 0 || 1);
50 return Math.min(0.95, 0.45 + skill * 0.03);
51}
52
53/** @deprecated Use shared/combat/rolls.js */
54export function mobDamage (attackSkill, baseDamage = 1)
55{
56 const skill = Math.max(1, attackSkill | 0 || 1);
57 return Math.max(1, (baseDamage | 0) + Math.floor(skill / 5));
58}
59
60export function mobSkillLevels (def)
61{
62 const attackSkill = Math.max(1, def?.attackSkill | 0 || 1);
63 const agilitySkill = Math.max(1, def?.agilitySkill ?? attackSkill);
64 const defenceSkill = Math.max(1, def?.defenceSkill ?? attackSkill);
65 return {
66 [SkillId.unarmed]: attackSkill,
67 [SkillId.agility]: agilitySkill,
68 [SkillId.defence]: defenceSkill
69 };
70}
71
72export function fallbackMobAttackProfile ()
73{
74 return { ...UNARMED_ATTACK, mobAttackSkill: 1 };
75}