Jump to content

Put Multi-Line Data Into JS 3 Dimensional Array?


JeZxLee
 Share

Recommended Posts

Hi,

I need to get below data into a 3 dimensional JS array
Any help would be appreciated, thanks!

var MapBoard = new Array(5)
    var level;
    for (level = 0; level < 5; level++)
    {
        MapBoard[level] = new Array(28);
            var indexX;
            for (indexX = 0; indexX < 28; indexX++)
            {
                MapBoard[level][indexX] = new Array(28);
            }
    }
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,2,1,1,1,1,1,0,0,0,1,1,1,1,1,2,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,1,1,1,1,1,0,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,1,1,0,1,1,0,1,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,2,1,1,1,1,1,0,0,0,1,1,1,1,1,2,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;


Thanks!

JeZxLee

Link to comment
Share on other sites

It's a little hard to understand what you want, but, from your representation of the source data it looks like you want a conversion from a 2d structure into a 3d one, if so, you need to clarify how to read the data (as its arbitrary to you, its impossible for us to guess). i.e. are the integers height information? does a 0 mean only 1 layer, whereas 2 means 3 layers, indicating height? Or, is the data a collection of triplets? [x, y, z][x, y, z] joined together?

If I assume the first case where integers indicate height then get your defined height and width, then add another array defining height and fill it in based on the integers from your source data (or, from your source code you look like you want your first array to define height/z, then x and y, thats fine too). 

Depending on how you want to access this data you might also want to consider a sparse structure. You create a layer (this can be a 2d array, x and y) and add an array defining layer data on top, but only for the layers that exist, I believe this is how Ultima 7 defined their map structure i.e. each [x, y] has a floor, so they have an array (this could be a linked list for other languages) of 1 with data about the floor, if the tile has something on top (i.e. a bush on top of grass) then you add another item to the height/layer list that defines the bush data. When you render this list you work your way through the array and for each tile render each layer item in order (from bottom to top). This is quite good for rendering, relatively simple, saves some memory bytes as most layers are empty, but if you want to access layer 3 of [10, 10] (for example) and you're using a simple layered array approach (so, for example, data[10][10][3]) you'll have to handle the invalid access error you'll get, which is fairly easy to do (you might want to add getters/setters to help here rather than raw array access).

If you really want brownie points you could create an api for accessing your map data, such that map.get(10, 10, 3) will return layer 3 for tile [10, 10], by creating this api/interface you could totally abstract yourself from the underlying data structure i.e. if a stacked array data[10][10][3] becomes unwieldy (it's not very performant) then you could switch to a flattened array, as you have the api implementation shielding you the rest of your app would work and you could play with the best way to represent the data.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...