BlueGit

timfa questward

Files

questward / shared / definitions / mobCombat.js

mobCombat.js

mobCombat.js 2.1 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 baseDamage: def?.baseDamage ?? 1,
43 mobAttackSkill: attackSkill
44 };
45}
46
47/** @deprecated Use shared/combat/rolls.js — kept for reference tests only. */
48export function mobHitChance (attackSkill)
49{
50 const skill = Math.max(1, attackSkill | 0 || 1);
51 return Math.min(0.95, 0.45 + skill * 0.03);
52}
53
54/** @deprecated Use shared/combat/rolls.js */
55export function mobDamage (attackSkill, baseDamage = 1)
56{
57 const skill = Math.max(1, attackSkill | 0 || 1);
58 return Math.max(1, (baseDamage | 0) + Math.floor(skill / 5));
59}
60
61export function mobSkillLevels (def)
62{
63 const attackSkill = Math.max(1, def?.attackSkill | 0 || 1);
64 const agilitySkill = Math.max(1, def?.agilitySkill ?? attackSkill);
65 const defenceSkill = Math.max(1, def?.defenceSkill ?? attackSkill);
66 return {
67 [SkillId.unarmed]: attackSkill,
68 [SkillId.dexterity]: agilitySkill,
69 [SkillId.defence]: defenceSkill
70 };
71}
72
73export function fallbackMobAttackProfile ()
74{
75 return { ...UNARMED_ATTACK, mobAttackSkill: 1 };
76}