59 lines
No EOL
1.5 KiB
JavaScript
59 lines
No EOL
1.5 KiB
JavaScript
// memory/locations.js
|
|
const { getMemory, saveMemory } = require('./index');
|
|
const config = require('../config.json');
|
|
|
|
function setSafeZone(db, label, position, bounds = { x: config.safeZone.xBound, y: config.safeZone.yBound, z: config.safeZone.zBound }) {
|
|
saveMemory(db, `safezone:${label}`, { center: position, bounds });
|
|
}
|
|
|
|
function getSafeZones(db, callback) {
|
|
db.all(`SELECT label, data FROM memory WHERE label LIKE 'safezone:%'`, [], (err, rows) => {
|
|
if (err) return callback(err, []);
|
|
const zones = rows.map(row => ({
|
|
label: row.label,
|
|
...JSON.parse(row.data)
|
|
}));
|
|
callback(null, zones);
|
|
});
|
|
}
|
|
|
|
function getHomeZone(db, callback) {
|
|
getSafeZones(db, (err, zones) => {
|
|
if (err) return callback(err, null);
|
|
const home = zones.find(z => z.label === 'safezone:home');
|
|
callback(null, home);
|
|
});
|
|
}
|
|
|
|
function isInSafeZone(pos, zones) {
|
|
return zones.some(zone => {
|
|
const { center, bounds } = zone;
|
|
return (
|
|
Math.abs(pos.x - center.x) <= bounds.x &&
|
|
Math.abs(pos.y - center.y) <= bounds.y &&
|
|
Math.abs(pos.z - center.z) <= bounds.z
|
|
);
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
setSafeZone,
|
|
getSafeZones,
|
|
getHomeZone,
|
|
isInSafeZone
|
|
};
|
|
|
|
|
|
/*
|
|
|
|
use this is areas to determine a safeZone:
|
|
|
|
const { getSafeZones, isInSafeZone } = require('../memory/locations');
|
|
|
|
getSafeZones(db, (err, zones) => {
|
|
if (isInSafeZone(bot.entity.position, zones)) {
|
|
bot.chat("You're inside a safe zone.");
|
|
}
|
|
});
|
|
|
|
*/ |