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