Archived

This topic is now archived and is closed to further replies.

tkmagic55

Raise/lower isometric maps

Recommended Posts

I''m currently working on a game with an isometric map like in transport tycoon and I have problems to raise/lower terrain. Each tile has a level where even numbers 0, 2 ... is plain squares while odd numbers represent the slopes. E.g. 1 is the slope between level 0 and 2 ... My problem is how to calculate the right level when I raise / lower the squares. Does anyone have an example of how to do this ? / Tommy

Share this post


Link to post
Share on other sites
Umm, well, you would find the the values of the squares around it first. If all the tiles around it are on the same level then the raised tile should be an even. If the tile on the left side is lower than the one on the right, it should be the number in between. (IE. tile on right=4 and tile on left=6, raised tile should equal 3).

Share this post


Link to post
Share on other sites
This is some code from Mickey Kawick''s book... I did not write this and I don''t know if it works

  
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <conio.h>

int NumTiles = 49, MapWidth = 7, MapHeight = 7;
int StandardHeight = 2;

int HeightMap[] =
{
1,1,1,1,1,1,1,
1,1,1,0,1,1,1,
1,0,0,0,0,1,1,
1,1,1,0,1,1,1,
1,1,1,0,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1
};

int Dir[][2] = {{-1,-1},{ 0,-1},{ 1,-1},{ 1, 0},{ 1, 1},{ 0, 1},{-1, 1},{-1, 0}};
int NumDir = 8;



struct SIMPLETILE
{
char Height;
char Tile;
};

SIMPLETILE* tiles;


void InitTiles()
{
tiles = new SIMPLETILE[NumTiles];
for(int i=0; i < NumTiles; i++)
{
tiles[i].Height = HeightMap[i];
}
}

int GetTileHeight(int x, int y, int height)
{
if(x<0) return height;
if(y<0) return height;
if(x>=MapWidth) return height;
if(y>=MapHeight) return height;
return tiles[x + (y*MapWidth)].Height;
}

int CountHighNeighbors(int x, int y)
{
int num = 0;
int Height = GetTileHeight(x,y,0);
for(int i=0; i<NumDir; i++)
{
if(GetTileHeight(x+Dir[i][0], y+Dir[i][1],Height) > Height) num++;
}
return num;
}

int CountLowNeighbors(int x, int y)
{
int num = 0;
int Height = GetTileHeight(x,y,0);
for(int i=0; i<NumDir; i++)
{
if(GetTileHeight(x+Dir[i][0], y+Dir[i][1],Height) < Height) num++;
}
return num;
}


void ModifyMaskAndCountWhenAppropriate(int& num, int& mask)
{
if(num <2) return;
int array[] = {mask&128,mask&64,mask&32,mask&16,mask&8,mask&4,mask&2,mask&1};
for(int i=0; i<8; i++)
{
if(array[i]) break;
}
if(i==8)
{
num=0,mask=0;
return;
}
int which = i;
int count = num;
while(count)
{
if(which>=8) which=0;
if(array[which++]==0) break;
count--;
}
if(count>0)
{
which -=2;
count = num;
while(count)
{
if(which<0) which = 7;
if(array[which--]==0) break;
count--;
}
}
if(count>0) return;
mask &= 85; num = 0;
if(mask&64) num++;
if(mask&16) num++;
if(mask&4) num++;
if(mask&1) num++;
}

void ResolveTile(int x, int y)
{
SIMPLETILE* tile = &tiles[x + (y*MapWidth)];
int numhigh = CountHighNeighbors(x,y);
int numlow = CountLowNeighbors(x,y);
int height = GetTileHeight(x,y,0);

tile->Tile = 0;
if( (numhigh == 0 && numlow == 0) || (numhigh == 8 && numlow == 8) )
{
return;
}
int upleft = GetTileHeight(x+Dir[0][0], y+Dir[0][1],height);
int up = GetTileHeight(x+Dir[1][0], y+Dir[1][1],height);
int upright = GetTileHeight(x+Dir[2][0], y+Dir[2][1],height);
int right = GetTileHeight(x+Dir[3][0], y+Dir[3][1],height);
int downright = GetTileHeight(x+Dir[4][0], y+Dir[4][1],height);
int down = GetTileHeight(x+Dir[5][0], y+Dir[5][1],height);
int downleft = GetTileHeight(x+Dir[6][0], y+Dir[6][1],height);
int left = GetTileHeight(x+Dir[7][0], y+Dir[7][1],height);
if(numhigh && numlow)
{
return;
}
else if(numhigh)
{
int mask =
( (upleft > height) ? 128:0 ) + ( (up > height) ? 64:0 ) +
( (upright > height) ? 32:0 ) + ( (right > height) ? 16:0 ) +
( (downright > height) ? 8:0 ) + ( (down > height) ? 4:0 ) +
( (downleft > height) ? 2:0 ) + ( (left > height) ? 1:0 );
ModifyMaskAndCountWhenAppropriate(numhigh,mask);
switch(numhigh)
{
case 1:
{
if(mask == 128) { tile->Tile = 5; return; }
if(mask == 64) { tile->Tile = 6; return; }
if(mask == 32) { tile->Tile = 7; return; }
if(mask == 16) { tile->Tile = 8; return; }
if(mask == 8) { tile->Tile = 1; return; }
if(mask == 4) { tile->Tile = 2; return; }
if(mask == 2) { tile->Tile = 3; return; }
if(mask == 1) { tile->Tile = 4; return; }
}
break;
case 2:
{
if(mask&64)
{
if(mask&16) { tile->Tile = 24; return; }
if(mask&1) { tile->Tile = 23; return; }
}
if(mask&4)
{
if(mask&16) { tile->Tile = 21; return; }
if(mask&1) { tile->Tile = 22; return; }
}
if(mask&64 && mask&4)
{
tile->Height = up;
tile->Tile = 0;
return;
}
if(mask&16 && mask&1)
{
tile->Height = left;
tile->Tile = 0;
return;
}
if(mask&128 && mask&8) { tile->Tile = 17; return; }
if(mask&32 && mask&2) { tile->Tile = 18; return; }

}
break;
case 3:
case 4:
case 5:
case 6:
case 7: tile->Tile = 0;
break;
}
}
else
{
int mask =
( (upleft < height) ? 128:0 ) + ( (up < height) ? 64:0 ) +
( (upright < height) ? 32:0 ) + ( (right < height) ? 16:0 ) +
( (downright < height) ? 8:0 ) + ( (down < height) ? 4:0 ) +
( (downleft < height) ? 2:0 ) + ( (left < height) ? 1:0 );

ModifyMaskAndCountWhenAppropriate(numlow,mask);

switch(numlow)
{
case 1:
{
if(mask == 128) { tile->Tile = 13; return; }
if(mask == 64) { tile->Tile = 14; return; }
if(mask == 32) { tile->Tile = 15; return; }
if(mask == 16) { tile->Tile = 16; return; }
if(mask == 8) { tile->Tile = 9; return; }
if(mask == 4) { tile->Tile = 10; return; }
if(mask == 2) { tile->Tile = 11; return; }
if(mask == 1) { tile->Tile = 12; return; }
}
break;
case 2:
{
if(mask&64)
{
if(mask&16) { tile->Tile = 22; return; }
if(mask&1) { tile->Tile = 21; return; }
}
if(mask&4)
{
if(mask&16) { tile->Tile = 23; return; }
if(mask&1) { tile->Tile = 24; return; }
}
if(mask&64 && mask&4)
{
tile->Height = up;
tile->Tile = 0; return;
}
if(mask&16 && mask&1)
{
tile->Height = up;
tile->Tile = 0; return;
}
if(mask&128 && mask&8) { tile->Tile = 19; return; }
if(mask&32 && mask&2) { tile->Tile = 20; return; }
}
break;
case 3:
case 4:
case 5:
case 6:
case 7: tile->Tile = 0;
break;
}
}
}

int main(int argc, char* argv[])
{


InitTiles();
for(int i=0; i<MapHeight; i++)
{
for(int j=0; j<MapWidth; j++)
{
int x = tiles[i*MapWidth+j].Height;
if(x > 9) std::cout << x<< " ";
else std::cout << " " << x << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;


for(i=0; i<MapHeight; i++)
{
for(int j=0; j<MapWidth; j++)
{
ResolveTile(j,i);
int x= tiles[i*MapWidth+j].Tile;
if(x>9) std::cout << x<< " ";
else std::cout << " " << x << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;


for(i=0; i<MapHeight; i++)
{
for(int j=0; j<MapWidth; j++)
{
int x= tiles[i*MapWidth+j].Tile;
if(x>9) std::cout << x<< " ";
else std::cout << " " << x << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;


getch();

delete (tiles);

return 0;
}



Dave "Dak Lozar" Loeser

Share this post


Link to post
Share on other sites