|
| 1 | +/// <reference types="../../CTAutocomplete" /> |
| 2 | + |
| 3 | +import Dungeon from "../../BloomCore/dungeons/Dungeon"; |
| 4 | + |
| 5 | +global.soshimee ??= {}; |
| 6 | +global.soshimee.events ??= {}; |
| 7 | +global.soshimee.events.dungeonRoom ??= {}; |
| 8 | + |
| 9 | +const listeners = global.soshimee.events.dungeonRoom.listeners ??= []; |
| 10 | + |
| 11 | +let roomX = -1; |
| 12 | +let roomZ = -1; |
| 13 | +let notified = false; |
| 14 | + |
| 15 | +const trigger = global.soshimee.events.dungeonRoom.trigger ??= register("tick", () => { |
| 16 | + if (!Dungeon.inDungeon) { |
| 17 | + if (!notified) { |
| 18 | + for (let listener of listeners) { |
| 19 | + listener(0); |
| 20 | + } |
| 21 | + roomX = -1; |
| 22 | + roomZ = -1; |
| 23 | + notified = true; |
| 24 | + } |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const prevRoomX = roomX; |
| 29 | + const prevRoomZ = roomZ; |
| 30 | + const x = Player.getX(); |
| 31 | + const z = Player.getZ(); |
| 32 | + if (x < -200 || z < -200 || x > 0 || z > 0) { |
| 33 | + if (!notified) { |
| 34 | + for (let listener of listeners) { |
| 35 | + listener(0); |
| 36 | + } |
| 37 | + roomX = -1; |
| 38 | + roomZ = -1; |
| 39 | + notified = true; |
| 40 | + } |
| 41 | + return; |
| 42 | + } |
| 43 | + roomX = Math.floor((x + 200) / 32); |
| 44 | + roomZ = Math.floor((z + 200) / 32); |
| 45 | + |
| 46 | + if (prevRoomX != roomX || prevRoomZ != roomZ) { |
| 47 | + const cx = -185 + roomX * 32; |
| 48 | + const cz = -185 + roomZ * 32; |
| 49 | + |
| 50 | + const roofHeight = getHighestBlock(cx, cz); |
| 51 | + |
| 52 | + for (let listener of listeners) { |
| 53 | + listener(getCore(cx, cz), getRotation(cx, roofHeight, cz)); |
| 54 | + } |
| 55 | + |
| 56 | + notified = false; |
| 57 | + } |
| 58 | +}).unregister(); |
| 59 | + |
| 60 | +const offsets = [[-15, -15], [15, -15], [15, 15], [-15, 15]]; |
| 61 | +const blacklisted = [5, 54]; |
| 62 | +function hashCode(s) { |
| 63 | + return s.split("").reduce((a, b) => { |
| 64 | + a = ((a << 5) - a) + b.charCodeAt(0); |
| 65 | + return a & a; |
| 66 | + }, 0); |
| 67 | +} |
| 68 | +function getCore(x, z) { |
| 69 | + let blockIds = "" |
| 70 | + for (let y = 140; y >= 12; --y) { |
| 71 | + let block = World.getBlockAt(x, y, z); |
| 72 | + if (blacklisted.includes(block.type.getID())) continue; |
| 73 | + blockIds += block.type.getID(); |
| 74 | + } |
| 75 | + return hashCode(blockIds); |
| 76 | +}; |
| 77 | +function getHighestBlock(x, z) { |
| 78 | + for (let y = 255; y > 0; --y) { |
| 79 | + let id = World.getBlockAt(x, y, z)?.type?.getID(); |
| 80 | + if (id == 0 || id == 41) continue; |
| 81 | + return y; |
| 82 | + } |
| 83 | + return null; |
| 84 | +} |
| 85 | +function getRotation(x, y, z) { |
| 86 | + if (!y) return; |
| 87 | + |
| 88 | + for (let i = 0; i < offsets.length; ++i) { |
| 89 | + let [dx, dz] = offsets[i]; |
| 90 | + let [nx, nz] = [x + dx, z + dz]; |
| 91 | + let block = World.getBlockAt(nx, y, nz); |
| 92 | + if (block.type.getID() !== 159 || block.getMetadata() !== 11) continue; |
| 93 | + return i; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export function addListener(listener) { |
| 98 | + if (listeners.length === 0) trigger.register(); |
| 99 | + listeners.push(listener); |
| 100 | +} |
| 101 | + |
| 102 | +export function removeListener(listener) { |
| 103 | + const index = listeners.indexOf(listener); |
| 104 | + if (index === -1) return false; |
| 105 | + listeners.splice(index, 1); |
| 106 | + if (listeners.length === 0) trigger.unregister(); |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +export default { addListener, removeListener }; |
0 commit comments