# How can I structure my ASCII terrain object?

So I have this object which represents a simple ASCII "overworld". Note the code is broken, but it's for demonstration purposes...

var TerrainObj = {
w: { name: 'water', glyph: 'w' },
g: { name: 'ground', glyph: 'g' },

terrain: [[w, w, w, w],
[w, g, g, w],
[w, g, w, w],
[g, g, w, w]],

generate_display: function(terrain) {
...
Code that returns an array the same as terrain but containing
the glyphs from the objects in terrain
...
}
}


So as you can see, the object contains the data for different types of terrain cell, the overworld itself, and a method to covert the terrain info into an array of glyphs for printing to the screen. But of course the problem with this code is that in the terrain array, there will be an reference error to each object.

I want to be able to write out the terrain array without using more than a one-letter name, because presumably to get this to work I'd have to do something like

terrain: function() {
var map = [[this.w, this.w, this.w, ...],
...
...
];
}


So is there a nicer way I can produce my "semantic" terrain layer that references objects but without me having to write lengthy references to them in my array declaration?

• Why don't you store your terrain in byte form (0,0,0,0,1,0,1,0,0,1,1) with just terrain types and use that to access objects in terraintypes terraintypes[terrain[i]] array? – Kromster Sep 25 '13 at 9:05
• Thanks, that sounds like a good solution. I was hoping to use alphanumeric characters for readability, but thinking about it, I suppose I could get round this by just parsing a text file with characters that correspond to objects... – njp Sep 25 '13 at 10:18

So, from what I've read, it appears that you want be able to write the map without much work. Here's a few ideas:

1) Create a small application/app/whatever you want to build the maps. It could be as simple as making a bunch of check boxes if you only have two types of terrain. Or a bunch of square text fields that you can tab through and type into. This approach will be quite useful if you end up making lots of maps.

2) Declare your array like so:

terrain: function() {
var map = [['w', 'w', 'w', ...],
...
...
];
}


By doing that, you can simply index the other table with the value of whatever location you need in the table. So, if you need square 3,3 you can get all of its info with:

TerrainObj[map[2][2]].WhateverProperty


I apologize for any messed up JavaScript syntax I may have given you.

• You can also preprocess the map from ["www...",...] if the maps are too big for 'w',  (5 chars for each tile) to be practical. – David X Jan 8 '15 at 9:10