Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New example: Item replacer #8

Open
gesior opened this issue Jun 22, 2022 · 0 comments
Open

New example: Item replacer #8

gesior opened this issue Jun 22, 2022 · 0 comments

Comments

@gesior
Copy link

gesior commented Jun 22, 2022

I made useful tool using OTBM2JSON.
Replaces item IDs on map. For people who want to convert map from one .otb to another.

const otbm2json = require("./otbm2json");

// FROM_ID: TO_ID
var replaceArray = {
    5091: 5090,
    12659: 11703,
}

const mapData = otbm2json.read("before.otbm");

let notReplaced = {};

function replaceItem(itemid) {
    if (!replaceArray[itemid]) {
        notReplaced[itemid] = itemid;
    }
    return replaceArray[itemid] ? replaceArray[itemid] : itemid;
}

function replaceItemRecursive(item) {
    if (item.type === otbm2json.HEADERS.OTBM_ITEM) {
        item.id = replaceItem(item.id);
        if (item.content && item.content.length) {
            item.content.forEach(function (containerItem) {
                replaceItemRecursive(containerItem)
            });
        }
    }
}

// Go over all nodes
mapData.data.nodes.forEach(function (n) {
    n.features.forEach(function (e) {
        if (e.type !== otbm2json.HEADERS.OTBM_TILE_AREA) {
            return;
        }
        e.tiles.forEach(function (tile) {
            if (tile.type !== otbm2json.HEADERS.OTBM_TILE && tile.type !== otbm2json.HEADERS.OTBM_HOUSETILE) {
                return;
            }

            tile.tileid = replaceItem(tile.tileid);
            if (tile.items && tile.items.length) {
                tile.items.forEach(function (item) {
                    replaceItemRecursive(item)
                });
            }
        });
    });
});
// Write the output to OTBM using the library
otbm2json.write("after.otbm", mapData);

console.log(notReplaced);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant