Skip to content

New example: Item replacer #8

@gesior

Description

@gesior

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions