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