| 1 | import { ItemId } from "../../../shared/definitions/items.js"; |
| 2 | import { combatLevelFromSkills } from "../../../shared/combat/combatLevel.js"; |
| 3 | import |
| 4 | { |
| 5 | combatLogoutCoinThreshold, |
| 6 | cowardiceHourKey |
| 7 | } from "../../../shared/combat/combatLogout.js"; |
| 8 | import |
| 9 | { |
| 10 | CHURCH_LETTER_AUTHOR, |
| 11 | QUESTWARD_GIFT_AUTHOR, |
| 12 | QUESTWARD_GIFT_ORG, |
| 13 | formatChurchOfflineResurrectionBody, |
| 14 | formatCombatLogoutCowardiceBody |
| 15 | } from "../../../shared/courier/systemLetters.js"; |
| 16 | import { defaultPlayerSkills } from "../../../shared/skills/state.js"; |
| 17 | |
| 18 | /** |
| 19 | * Apply PvP combat-logout cowardice (skull + bank letter), honouring the |
| 20 | * once-per-hour forgiveness. Returns whether a penalty was applied. |
| 21 | */ |
| 22 | export function applyCombatLogoutCowardice (simulation, characterId, { |
| 23 | coinsDropped = null, |
| 24 | now = Date.now() |
| 25 | } = {}) |
| 26 | { |
| 27 | const id = characterId | 0; |
| 28 | const skills = simulation.stateDb.getSkills(id) ?? defaultPlayerSkills(); |
| 29 | const threshold = combatLogoutCoinThreshold(combatLevelFromSkills(skills)); |
| 30 | let amount; |
| 31 | if (coinsDropped == null) |
| 32 | { amount = threshold; } |
| 33 | else if ((coinsDropped | 0) > threshold) |
| 34 | { return { applied: false, forgiven: false, amount: 0, threshold }; } |
| 35 | else |
| 36 | { amount = Math.max(0, threshold - (coinsDropped | 0)); } |
| 37 | |
| 38 | if (amount <= 0) |
| 39 | { return { applied: false, forgiven: false, amount: 0, threshold }; } |
| 40 | |
| 41 | const hour = cowardiceHourKey(now); |
| 42 | const forgivenHour = simulation.stateDb.getCowardiceForgivenHour(id); |
| 43 | if (forgivenHour !== hour) |
| 44 | { |
| 45 | simulation.stateDb.setCowardiceForgivenHour(id, hour); |
| 46 | return { applied: false, forgiven: true, amount, threshold }; |
| 47 | } |
| 48 | |
| 49 | simulation.stateDb.addKillCounter(id, amount, now); |
| 50 | simulation.refreshCharacterSkull?.(id, { force: true }); |
| 51 | dispatchCombatLogoutBankLetter(simulation, id); |
| 52 | return { applied: true, forgiven: false, amount, threshold }; |
| 53 | } |
| 54 | |
| 55 | export function dispatchCombatLogoutBankLetter (simulation, characterId) |
| 56 | { |
| 57 | const row = simulation.stateDb.getCharacter(characterId); |
| 58 | if (!row || !simulation.courier) |
| 59 | { return; } |
| 60 | const originTileX = row.tile_x | 0; |
| 61 | const originTileY = row.tile_y | 0; |
| 62 | const originDim = row.dimension_id ?? undefined; |
| 63 | const bank = simulation.courier.nearestBank?.(originTileX, originTileY, originDim); |
| 64 | simulation.courier.enqueueSystemLetter({ |
| 65 | characterId, |
| 66 | originTileX: bank?.tileX ?? originTileX, |
| 67 | originTileY: bank?.tileY ?? originTileY, |
| 68 | originDimensionId: bank?.dimensionId ?? originDim, |
| 69 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 70 | authorOrganization: QUESTWARD_GIFT_ORG, |
| 71 | body: formatCombatLogoutCowardiceBody() |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | export function dispatchChurchOfflineResurrectionLetter (simulation, characterId, tileX, tileY) |
| 76 | { |
| 77 | if (!simulation.courier) |
| 78 | { return; } |
| 79 | const altar = nearestAltarTile(simulation, tileX, tileY); |
| 80 | simulation.courier.enqueueSystemLetter({ |
| 81 | characterId, |
| 82 | originTileX: altar?.tileX ?? (tileX | 0), |
| 83 | originTileY: altar?.tileY ?? (tileY | 0), |
| 84 | originDimensionId: altar?.dimensionId, |
| 85 | authorDisplayName: CHURCH_LETTER_AUTHOR, |
| 86 | body: formatChurchOfflineResurrectionBody() |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | function nearestAltarTile (simulation, tileX, tileY) |
| 91 | { |
| 92 | const altars = simulation.altarTiles ?? []; |
| 93 | if (!altars.length) |
| 94 | { return null; } |
| 95 | let best = null; |
| 96 | let bestDist = Infinity; |
| 97 | for (const altar of altars) |
| 98 | { |
| 99 | const dist = Math.hypot((altar.tileX | 0) - (tileX | 0), (altar.tileY | 0) - (tileY | 0)); |
| 100 | if (dist < bestDist) |
| 101 | { |
| 102 | best = altar; |
| 103 | bestDist = dist; |
| 104 | } |
| 105 | } |
| 106 | return best; |
| 107 | } |
| 108 | |
| 109 | export function coinsDroppedFromDeathEvents (events) |
| 110 | { |
| 111 | let total = 0; |
| 112 | for (const event of events ?? []) |
| 113 | { |
| 114 | if (event.type !== "ground-item-appear") |
| 115 | { continue; } |
| 116 | const itemId = event.stack?.itemId ?? event.itemId; |
| 117 | if ((itemId | 0) !== ItemId.coins) |
| 118 | { continue; } |
| 119 | total += Math.max(0, event.stack?.quantity | 0); |
| 120 | } |
| 121 | return total; |
| 122 | } |