JavaScript

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

JavaScript

Post by Holladerwaldelf »

Has anyone tried the JavaScript features of the maptool 1.4.1 series? Any good or bad experiences?
Is there a forum to exchange macro code using JavaScript?

WarBride
Kobold
Posts: 4
Joined: Mon May 02, 2016 11:53 am

Re: JavaScript

Post by WarBride »

I second this question - what is the general experience in using the JavaScript implementation?
Is there a specific forum breakout to share JavaScript code?

Any help would be appreciated. Thanks for your time.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: JavaScript

Post by wolph42 »

AFAIK the current release does not support js. It will be implemented but that hasn't been done yet.

That said, I have some scripts ready as discussed with one of the DEVs, whether they work will depend on the final implementation choices.

Here's one such script (A* pathing):

Code: Select all

var openField = [{"x":11,"y":10},{"x":12,"y":10},{"x":11,"y":11},{"x":10,"y":12},{"x":13,"y":10},{"x":12,"y":11},{"x":11,"y":12},{"x":14,"y":10},{"x":13,"y":11},{"x":12,"y":12},{"x":11,"y":13},{"x":15,"y":10},{"x":14,"y":11},{"x":13,"y":12},{"x":12,"y":13},{"x":11,"y":14},{"x":16,"y":10},{"x":15,"y":11},{"x":14,"y":12},{"x":13,"y":13},{"x":11,"y":15},{"x":17,"y":10},{"x":16,"y":11},{"x":15,"y":12},{"x":14,"y":13},{"x":11,"y":16},{"x":17,"y":11},{"x":16,"y":12},{"x":15,"y":13},{"x":18,"y":10},{"x":12,"y":16},{"x":17,"y":12},{"x":16,"y":13},{"x":19,"y":10},{"x":13,"y":16},{"x":17,"y":13},{"x":15,"y":15},{"x":14,"y":16},{"x":16,"y":15},{"x":15,"y":16},{"x":17,"y":15},{"x":16,"y":16},{"x":15,"y":17},{"x":18,"y":15},{"x":17,"y":16},{"x":16,"y":17},{"x":15,"y":18},{"x":19,"y":15},{"x":18,"y":16},{"x":17,"y":17},{"x":16,"y":18},{"x":15,"y":19},{"x":19,"y":16},{"x":18,"y":17},{"x":17,"y":18},{"x":16,"y":19},{"x":19,"y":17},{"x":18,"y":18},{"x":17,"y":19},{"x":19,"y":18},{"x":18,"y":19},{"x":19,"y":19},{"x":3,"y":11},{"x":10,"y":4},{"x":4,"y":11},{"x":3,"y":12},{"x":11,"y":4},{"x":10,"y":5},{"x":5,"y":11},{"x":4,"y":12},{"x":3,"y":13},{"x":6,"y":10},{"x":12,"y":4},{"x":11,"y":5},{"x":6,"y":11},{"x":5,"y":12},{"x":4,"y":13},{"x":3,"y":14},{"x":13,"y":4},{"x":12,"y":5},{"x":11,"y":7},{"x":7,"y":11},{"x":6,"y":12},{"x":5,"y":13},{"x":4,"y":14},{"x":3,"y":15},{"x":14,"y":4},{"x":12,"y":6},{"x":6,"y":13},{"x":12,"y":7},{"x":11,"y":8},{"x":8,"y":11},{"x":7,"y":12},{"x":5,"y":14},{"x":4,"y":15},{"x":3,"y":16},{"x":15,"y":4},{"x":5,"y":15},{"x":13,"y":7},{"x":11,"y":9},{"x":16,"y":4},{"x":12,"y":8},{"x":9,"y":11},{"x":3,"y":17},{"x":8,"y":12},{"x":4,"y":16},{"x":7,"y":13},{"x":6,"y":14},{"x":12,"y":9},{"x":17,"y":4},{"x":8,"y":13},{"x":14,"y":7},{"x":4,"y":17},{"x":5,"y":16},{"x":13,"y":8},{"x":9,"y":12},{"x":7,"y":14},{"x":6,"y":15},{"x":18,"y":4},{"x":8,"y":14},{"x":15,"y":7},{"x":14,"y":8},{"x":13,"y":9},{"x":9,"y":13},{"x":7,"y":15},{"x":5,"y":17},{"x":6,"y":16},{"x":16,"y":7},{"x":15,"y":8},{"x":14,"y":9},{"x":9,"y":14},{"x":8,"y":15},{"x":7,"y":16},{"x":6,"y":17},{"x":17,"y":7},{"x":16,"y":8},{"x":15,"y":9},{"x":9,"y":15},{"x":8,"y":16},{"x":7,"y":17},{"x":20,"y":10},{"x":6,"y":18},{"x":17,"y":8},{"x":16,"y":9},{"x":9,"y":16},{"x":8,"y":17},{"x":21,"y":10},{"x":6,"y":19},{"x":17,"y":9},{"x":9,"y":17},{"x":20,"y":15},{"x":15,"y":20},{"x":21,"y":15},{"x":20,"y":16},{"x":16,"y":20},{"x":15,"y":21},{"x":21,"y":16},{"x":20,"y":17},{"x":17,"y":20},{"x":16,"y":21},{"x":21,"y":17},{"x":20,"y":18},{"x":18,"y":20},{"x":17,"y":21},{"x":1,"y":5},{"x":21,"y":18},{"x":20,"y":19},{"x":19,"y":20},{"x":18,"y":21},{"x":2,"y":5},{"x":1,"y":6},{"x":21,"y":19},{"x":19,"y":21},{"x":18,"y":22},{"x":3,"y":5},{"x":2,"y":6},{"x":1,"y":7},{"x":18,"y":23},{"x":4,"y":5},{"x":3,"y":6},{"x":2,"y":7},{"x":18,"y":24},{"x":5,"y":5},{"x":4,"y":6},{"x":3,"y":7},{"x":18,"y":25},{"x":6,"y":5},{"x":5,"y":6},{"x":4,"y":7},{"x":7,"y":4},{"x":18,"y":26},{"x":7,"y":5},{"x":6,"y":6},{"x":5,"y":7},{"x":8,"y":4},{"x":18,"y":27},{"x":7,"y":6},{"x":6,"y":7},{"x":9,"y":4},{"x":19,"y":27},{"x":8,"y":5},{"x":7,"y":7},{"x":6,"y":8},{"x":9,"y":5},{"x":6,"y":9},{"x":21,"y":3},{"x":21,"y":4},{"x":21,"y":5},{"x":6,"y":20},{"x":21,"y":6},{"x":6,"y":21},{"x":21,"y":7},{"x":6,"y":22},{"x":21,"y":8},{"x":6,"y":23},{"x":21,"y":9},{"x":6,"y":24},{"x":6,"y":25},{"x":6,"y":26},{"x":6,"y":27},{"x":20,"y":20},{"x":21,"y":20},{"x":20,"y":21},{"x":21,"y":21},{"x":20,"y":27},{"x":21,"y":27},{"x":22,"y":27},{"x":23,"y":27},{"x":24,"y":27},{"x":25,"y":27},{"x":26,"y":27},{"x":27,"y":27},{"x":28,"y":27}]


// This function will check a location's status
// (a location is "valid" if it is on the grid, is not an "obstacle",
// and has not yet been visited by our algorithm)
// Returns "Valid", "Invalid", "Blocked", or "Goal"
var locationStatus = function(location, grid) {
	var y = location.y;
	var x = location.x;
	var tmpCoord = {x:x,y:y};
	// check if the coord is NOT part of the openField (blocked) else if goal else its just a valid cell
	
	if (openField.findIndex(obj => obj.x==tmpCoord.x && obj.y==tmpCoord.y)	== -1)
									{ return 'Blocked' } 
	else if (grid[x][y] === 'Goal')	{ return 'Goal'	   }
	else 							{ return 'Valid'   }
};





// Explores the grid from the given location in the given
// direction
var exploreInDirection = function(currentLocation, direction, grid) {
	var newPath = currentLocation.path.slice();
	var x = currentLocation.x;
	var y = currentLocation.y;

	if 		(direction === 'North')	{ y -= 1 } 
	else if	(direction === 'East')	{ x += 1 }
	else if	(direction === 'South') { y += 1 }
	else if (direction === 'West')	{ x -= 1 }

	//newPath.push({x:x,y:y});
	newPath.push("<br>x: " + x + " , y: " + y);

	var newLocation = {
		x: x,
		y: y,
		path: newPath,
		status: 'Unknown'
	};
	newLocation.status = locationStatus(newLocation, grid);

	// If this new location is valid, mark it as 'Visited'
	if (newLocation.status === 'Valid') {
		grid[newLocation.x][newLocation.y] = 'Visited';
	}
    //document.write("checking: "+x+","+y+","+newLocation.status+"<br>")

	return newLocation;
};




// Start location will be in the following format:
// [x, y]
var findShortestPath = function(startCoordinates, grid) {
	var x = startCoordinates[0];
	var y = startCoordinates[1];
	// Each "location" will store its coordinates
	// and the shortest path required to arrive there
	var location = {
		x: x,
		y: y,
		path: [],
		status: 'Start'
	};

	// Initialize the queue with the start location already inside
	var queue = [location];

	// Loop through the grid searching for the 'goal'
	while (queue.length > 0) {
		// remove the first entry of the queue
		var currentLocation = queue.shift();

		// Explore North
		var newLocation = exploreInDirection(currentLocation, 'North', grid);
		if (newLocation.status === 'Goal') {
			return newLocation.path;
		} else if (newLocation.status === 'Valid') {
			//add newLocation to the array
			queue.push(newLocation);
		}

		// Explore East
		var newLocation = exploreInDirection(currentLocation, 'East', grid);
		if (newLocation.status === 'Goal') {
			return newLocation.path;
		} else if (newLocation.status === 'Valid') {
			queue.push(newLocation);
		}

		// Explore South
		var newLocation = exploreInDirection(currentLocation, 'South', grid);
		if (newLocation.status === 'Goal') {
			return newLocation.path;
		} else if (newLocation.status === 'Valid') {
			queue.push(newLocation);
		}

		// Explore West
		var newLocation = exploreInDirection(currentLocation, 'West', grid);
		if (newLocation.status === 'Goal') {
			return newLocation.path;
		} else if (newLocation.status === 'Valid') {
			queue.push(newLocation);
		}
	}

	// No valid path found
	return false;
};




// //[h:wolphsLib.findPath(openField, startCoord, goalCoord)]
// export.mtsNamespace='wolphsLib';
// export.mts.findPath = {
//	func: function(openField, startCoord, goalCoord) {
		
		//  --------------------------------------------------- findPath () --------------------------------------------

		//arrow function is new syntax these two lines do the same thing:
		//var aVar = a.map(function(s){ return s.length });
		//var aVar = a.map( s => s.length );

		//first find the min/max x/y in the openField, thats the parameter for the entire field
		var grid = [];
        var minX = Math.min.apply(Math, openField.map(val => val.x));
		var maxX = Math.max.apply(Math, openField.map(val => val.x));
		var minY = Math.min.apply(Math, openField.map(val => val.y));
		var maxY = Math.max.apply(Math, openField.map(val => val.y));


        //then create a grid of each cell that is within the given boundary coordinates
		for(i = 0; i <= maxX; i++) {
            grid[i] = [];
 			for(j = 0; j <= maxY; j++) {
				grid[i][j] = 'Empty';
				grid[i][j] = 'x:' + i + 'y:'+j;
			}
		}

var startCoord	= {x:18, y:4};
var goalCoord	= {x:21, y:6};
var goalCoord	= {x:13, y:10};


        // This is how we would represent the grid with obstacles above
		grid[startCoord.x][startCoord.y] = "Start";
		grid[goalCoord.x][goalCoord.y] = "Goal";

document.write(findShortestPath([startCoord.x,startCoord.y], grid));
//	}
//}
this script finds the shortest path between two coords, given an array of available coords where the path can run.

the current script can be run inside any html (to test) and has instructions to finally insert it inside an MT environment.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: JavaScript

Post by aliasmask »

There is a dev version you can download to test out javascript.

http://forums.rptools.net/viewtopic.php?f=1&t=26736

WarBride
Kobold
Posts: 4
Joined: Mon May 02, 2016 11:53 am

Re: JavaScript

Post by WarBride »

Thanks for the example Javascript!

Post Reply

Return to “Macros”