Map editor

Page 3/4
1 | 2 | | 4

By jltursan

Prophet (2542)

jltursan's picture

25-09-2021, 17:14

MSX script for Tiled?, cool!, it would be great to have them available Smile

By MsxKun

Paragon (1031)

MsxKun's picture

25-09-2021, 23:00

You mean something like this?

var customMapFormat = {
    name: "MSX Raw Binary",
    extension: "map",

    write: function(map, fileName) {
        var m = [];

        currlayer = map.selectedLayers;
        var layer = map.layerAt(currlayer);
        var sx = layer.width;
        var sy = layer.height;

        if (layer.isTileLayer) {
		var rows = [];
		for (y = 0; y < layer.height; ++y) {
		        for (x = 0; x < layer.width; ++x)
		        m.push(layer.cellAt(x, y).tileId);	    
		}
	}
  	
	var newmap = new Uint8Array(m).buffer;
	return newmap;
    },
}

tiled.registerMapFormat("MSX Map", customMapFormat)

I made another with layer support, not sure if ever tested it... :/ And a custom one to export Godzilla Kun levels as CAS.
It's Javascript, btw.

By sd_snatcher

Prophet (3473)

sd_snatcher's picture

26-09-2021, 00:04

MsxKun wrote:

You mean something like this?

Do you know that not all heroes wear capes? Big smile

By sd_snatcher

Prophet (3473)

sd_snatcher's picture

26-09-2021, 00:46

Weird. I tried the script made by MsxKun, but while it does show up in the export menu, it doesn't save any file. At least on macOS.

By sd_snatcher

Prophet (3473)

sd_snatcher's picture

26-09-2021, 01:55

Update: I found on the docs how to write a binary file.

By Bengalack

Champion (406)

Bengalack's picture

26-09-2021, 09:47

Actually, I didn't know that the scripts could be executed from within Tiled. Learning new stuff all the time Smile It's a nice feature (as there's an output window) but doesn't change much for me though.

The script I have is highly specialized for the game I'm making, so it cannot be used directly by others.

But there are some basics, though. And it is quite simple. Below I have cut out a small set which opens a json (=map) file, and gets some properties and saves data to a binary (".ltm") file which can be read or included directly in your msx game (/rom). This code snippet may serve as a starting point for those who know (or want to know) python.

The snippet needs there to be a map saved from Tiled with a (bitmap/tile-) layer called "Background" of any size, using one tileset or more.

Because of cut'n'paste it might not run directly, but you get the idea. Two first bytes written are width (16bit) and next two bytes are height (16bit), and then the tiles follow. The json-file is nicely organized hierarchical file, so it is easy to find what you are looking for and grab via the built-in json-functionality. In practise you need to load the tilesets too. Tiled supports multiple sets in your map, just make sure to differenciate them by the "guid" (--"firstguid") before you save/export to your own file. If you use only one tileset (no more than 256 tiles, you can ignore this as firstguid will likely be 0)

import json
import os
import sys

filename_jsn1_r = sys.argv[ 1 ] + '.json'  # tilemap
filename        = sys.argv[ 5 ]            # output - all other files excl. ext

pathname    = 'content' + os.path.sep + 'tilemaps'

json_file   = open( pathname  + os.path.sep + filename_jsn1_r, 'r' )
ltm_file    = open( pathname + os.path.sep + filename + '.LTM','wb' )

tm_data = json.load( json_file )

tilesets = tm_data[ 'tilesets' ]

if len( tilesets )==0:
	return bail ( '\n\tError: There are no tilesets referenced in this map\n' )

offset = tileset[ 'firstgid' ]

height = tm_data[ 'height' ] # in tiles, not pixels
width = tm_data[ 'width' ]   # in tiles, not pixels

ltm_file.write( width.to_bytes( 2, byteorder='little' ) )
ltm_file.write( height.to_bytes( 2, byteorder='little' ) )

print ( "Playfield size: %d, %d" % ( width, height ) )

layer_list = tm_data[ 'layers' ]

if len( layer_list )==0:
	return bail ( '\n\tError: There are no layers in this map\n' )

background_layer = None

for layer in layer_list:
	if layer[ 'name' ] == 'Background':
		background_layer = layer 

if background_layer is None:
	return bail ( '\n\tError: One of the required layers is missing from the map file\n' )

background_list = background_layer[ 'data' ]
backgroundarray = bytearray( height*width )

for byte in background_list:
	( tilesetid, offset ) = getTilesetContext( byte ) # Custom function
	byte = byte - offset
	backgroundarray[ i ] = byte
	i = i + 1

ltm_file.write( backgroundarray ) # Write to the file. We are now done with this
ltm_file.close()

Functions getTilesetContext and bail are excluded to keep things simpler.

By Bengalack

Champion (406)

Bengalack's picture

26-09-2021, 10:14

My example above runs outside Tiled, and is part of my "tool-chain". I have been using Tiled 1.4. I see now that the newer versions of Tiled have better scripting support (!!!), like MsxKun shows, and this is probably the way to go. I'll try to upgrade and see if I can benefit from it.

I do find the scripting support quite "hidden", though. Like there is no trace of it in the gui at all, even not what I have upgraded to 1.7.2. Less i more eh? Smile

By MsxKun

Paragon (1031)

MsxKun's picture

26-09-2021, 10:14

sd_snatcher wrote:

Weird. I tried the script made by MsxKun, but while it does show up in the export menu, it doesn't save any file. At least on macOS.

Strange. I use linux Tiled, but should be the same. I remember trying python on tiled first but didnt work cause cant remember why. Then enabled js plugin on tiled and solved.
Maybe forgot some line when copy-pasted? Cant check now.

By MsxKun

Paragon (1031)

MsxKun's picture

26-09-2021, 10:16

So make sure JavaScript plugin is enabled and name the file something.js
I dont remember any extra step.

By theNestruo

Champion (317)

theNestruo's picture

26-09-2021, 10:36

I prefer to have my tools outside Tiled because I can integrate them in a makefile or in a build script (but other people may prefer otherwise!)

I'll add more options to this thread:

  • PCXTOOLS (C) include TMX2BIN that can be used to convert the CSV part of a XML-based .TMX file to binary. It is quite basic but, for example, it was enough for me to design Stevedore stages.
  • For World Rally, I coded an oddly specific utility (also in C). It is not code to be proud of; please ignore it.
  • More recently, I've also coded TMX-2-NAMTBL-sprites (Java). Again, it is an oddly specific utility.

For me, the main advantage of Tiled is that its files are text-based and easily parseable, so it is easy to code a quick-n-dirty tool in your language of choice. Even more: sometimes you can copy the CSV part of the map, add a "db" at the beginning of each line... and you already have the data as assembly code! :D

That said, if you want to code your own tools and are familiar with C or Java, please don't hesitate to use whatever part you want of any of them!

Page 3/4
1 | 2 | | 4