Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Tile functions (https://forums.graalonline.com/forums/showthread.php?t=134258008)

DustyPorViva 02-15-2010 02:27 AM

Tile functions
 
A group of functions I've made that can be quite useful when working with tiles. These are mainly for translating level data to tile data to image data(pics1) and back and forth to any degree. For example, if you wish to make your own level editor you will have to parse the .NW format, which uses base64 into tile data. Then you'll have to make that tile data into pixel data, to draw the tile images..

I realize there are already some base64 functions in GS2, but I couldn't seem to get them to work for the .NW tile format, so I made my own.

Anyways, the functions and how to use them:

TilesToImage(tile) - Converts a tile into pixel position on pics1. Should work for all other tileset types as well. This also supports and returns arrays of tiles like so:
TilesToImage({tile1,tile2,tile3,tile4})
So you could use it like so:
temp.pixeldata = TilesToImage(681);
temp.pixeldata = TilesToImage({681,682,697,698});
Which would return the pixel coordinates of the chair tiles.


ImageToTile(pixelX,pixelY) - Converts the pixel position of a tileset image into a tile number. Does NOT support multiple entries. Works like so:
temp.chairtile = ImageToTile(400,160);
Returns 681, which is the upper-left chair tile of the pics1 indoor chair.


Base64ToTile(base64string)
Base64ToTile({base64,base64,base64,base64})
Converts .NW base64 data into tile data. Also supports multiple entries like TilesToImage().
temp.chairtile = Base64ToTile("Kp");
temp.chairtile = Base64ToTile({"Kp","Kq","K5","K6"});
First will return 681, and the second will return {681,682,697,698}.


TileToBase64(tile)
TileToBase64({tile1,tile2,tile3,tile4})
This one will convert tile data into base64 strings, compatible with the .NW format. Examples:
temp.chairtile = Base64ToTile(681); returns "Kp"
temp.chairtile = Base64ToTile({681,682,697,698}); returns {"Kp","Kq","K5","K6"}

PHP Code:

function TilesToImage(t) {
  if (
t.type() == 0) {
    
temp.px = (int(t%16)*16)+(int(t/512)*256);
    
temp.py = (int(t/16)*16)%512;
    return {
px,py};
  } else if (
t.type() == 3) {
    
temp.imgarray = new[0];
    for (
temp.t) {
      
temp.px = (int(i%16)*16)+(int(i/512)*256);
      
temp.py = (int(i/16)*16)%512;
      
imgarray.add({px,py}); 
    }
    return 
imgarray;
  } else return -
1;
}

function 
ImageToTile(tx,ty) {
  return 
this.tile=((int(tx/16)%16)+(int(tx/256)*512))+((int(ty/16)*16)%512);
}

function 
Base64ToTile(t) {
  
temp.base64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  if (
t.type() == 1) {
    return 
base64.pos(t.substring(0,1))*64 base64.pos(t.substring(1,1));
  } else if (
t.type() == 3) {
    
temp.decodearray = new[0];
    for (
temp.tdecodearray.add(base64.pos(i.substring(0,1))*64 base64.pos(i.substring(1,1))); 
    return 
decodearray;
  } else return -
1;
}

function 
TileToBase64(t) {
  
temp.base64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  if (
t.type() == 0) {
    return 
base64.substring(int(t/64),1) @ base64.substring(t%64,1);
  } else if (
t.type() == 3) {
    
temp.encodearray = new[0];
    for (
temp.tencodearray.add(base64.substring(int(i/64),1) @ base64.substring(i%64,1));
    return 
encodearray;
  } else return -
1;



12171217 02-15-2010 04:25 AM

Nice collection there, useful.

cbk1994 02-15-2010 04:57 AM

I'm not a fan of differing return types like you've used— I find it better to always return an array in cases like that and simply use returnValue[0] if you only gave it one parameter.

Either way, nice collection as Downsider said :).


All times are GMT +2. The time now is 11:01 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.