Graal Forums

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

DustyPorViva 02-12-2010 08:46 AM

Default Tile Detection
 
This is a function that works pretty much like default tile detection(slashing bushes). The function itself is pretty flexible as to allow it to work in various amount of ways(for slashing or picking up an object, for example). It's expanded to allow you to define what tiles can be interacted with, and how much power it takes to destroy them. This allows you to, for example, allow players to slash lava rocks with a level 2 sword. Or hammer blackrocks with a level 3 hammer instead of a level 2. It also returns the data of the object you've 'hit' for further customization.

Parameters are pretty self-explanatory, I think. cdir is the direction to check, as this is in relation to the player. pwr is the amount of power needed to destroy the object, as specified in the array. The powers I mapped out are in relation to player sword powers, but you can specify whatever you'd like. doleap is whether or not to place a leap, you wouldn't want to do this if you are lifting an object. offsetx/y are how much to offset the check x/y from the default position. This is useful, for example, when the player is on a horse you need to offset the y by -1.

By the way, the tile arrays in the objectdata array follows this format:

[0][1]
[2][3]


New addition:
Different power types. The array map now handles different types of damage. You can add new powers by:
Adding the type to the 'temp.powerlayout'. There are 3 types currently. You can remove or add to them.
The number of objects is important because the object array will reflect this number. If you add a type, you must add to the
powers array in the 'temp.objectdata' value. Defaultly, if no type is specified when you call the function it reverts to the sword power
array.

PHP Code:

function CheckTiles(cdir,pwrdata,doleap,offsetx,offsety) {
  
// A LAYOUT OF DAMAGE TYPES, USED TO FIND THE CORRECT DAMAGE IN THE ARRAY
  
temp.powerlayout = {"sword","glove","hammer"};

  
// THEN FIND THE INDEX OF THE TYPE. IF NO TYPE FOUND, RESORT TO SWORD POWER
  
temp.dmgtype powerlayout.index(pwrdata[0]) == -powerlayout.index(pwrdata[0]);
  
temp.pwr pwrdata[1] == null pwrdata pwrdata[1];

  
temp.objectdata = {
    
//name,tilecheck,tilereplace,leaptype,powers{sword,glove,hammer},canhide(true/false)
    
{"swamp"     ,{0x1A4,0x1A5,0x1B4,0x1B5},{0x2A7,0x2A8,0x2B7,0x2B8},1,{1,0,0},0},
    {
"bush"      ,{0x2  ,0x3  ,0x12 ,0x13 },{0x2A5,0x2A6,0x2B5,0x2B6},0,{1,1,1},1},
    {
"stake"     ,{0x14A,0x14B,0x15A,0x15B},{0x70A,0x70B,0x71A,0x71B},3,{0,0,1},0},
    {
"sign"      ,{0x200,0x201,0x210,0x211},{0x70A,0x70B,0x71A,0x71B},3,{2,1,1},0},
    {
"rock"      ,{0x22 ,0x23 ,0x32 ,0x33 },{0x72A,0x72B,0x73A,0x73B},2,{2,2,2},1},
    {
"blackrock" ,{0x3DE,0x3DF,0x3EE,0x3EF},{0x72A,0x72B,0x73A,0x73B},2,{3,3,3},1},
    {
"vase"      ,{0x2AC,0x2AD,0x2BC,0x2BD},{0x6EA,0x6EB,0x6FA,0x6FB},2,{2,1,1},0},
    {
"desertbush",{0x35E,0x35F,0x36E,0x36F},{0x388,0x389,0x389,0x388},0,{1,1,1},1},
    {
"lavaswamp" ,{0xBE8,0xBE9,0xBF8,0xBF9},{0xC92,0xC93,0xCA2,0xCA3},1,{1,0,0},0},
    {
"lavabush"  ,{0xA02,0xA03,0xA12,0xA13},{0xC90,0xC91,0xCA0,0xCA1},0,{2,3,1},1},
    {
"lavasign"  ,{0xC52,0xC53,0xC62,0xC63},{0xC50,0xC51,0xC60,0xC61},3,{1,1,1},0},
    {
"lavastone" ,{0xA22,0xA23,0xA32,0xA33},{0xC70,0xC71,0xC80,0xC81},2,{3,3,2},1},
    {
"lavavase"  ,{0xC65,0xC66,0xC75,0xC76},{0xC72,0xC73,0xC82,0xC83},2,{3,1,1},1},
    {
"snowswamp" ,{0x838,0x839,0x848,0x849},{0x2A7,0x2A8,0x2B7,0x2B8},1,{1,1,0},0},
    {
"snowbush"  ,{0x7AC,0x7AD,0x7BC,0x7BD},{0xF02,0xF03,0xF12,0xF13},0,{1,0,1},1},
    {
"snowsign"  ,{0x7EB,0x7EC,0x7FB,0x7FC},{0xFE5,0xFE6,0xFF5,0xFF6},3,{2,3,1},0},
  };

  
// FIND THE CHECKX AND CHECKY 
  
temp.px int(player.+ (cdir==? -1.5 : (cdir==3.5 1.5))) - offsetx;
  
temp.py int(player.+ (cdir==0    : (cdir==4   2))) - offsety;

  
temp.hitobj = -1// INITIATE NO FOUND OBJECT AS -1
  
for (temp.i=0;i<objectdata.size();i++) {
    
// CHECK IF THE TILE IS IN THE ARRAY
    
if (tiles[px,pyin objectdata[i][1]) {
      
temp.objectdata[i][1].index(tiles[px,py]); // FIND POSITION OF TILE IN ARRAY
      
px px-(j%2);                                 // APPLY X OFFSET
      
py py-int(j/2);                              // APPLY Y OFFSET
      
hitobj i;                                    // ASSIGN FOUND OBJECT
      
break;                                         // STOP CHECKING
    
}
  }

  
// CREATE A BLOCK ARRAY OF THE TILES FOR COMPARISON
  
temp.checktiles = {tiles[px,py],tiles[px+1,py],tiles[px,py+1],tiles[px+1,py+1]};
  
// IF WHOLE BLOCK DOES NOT MATCH THE OBJECT, RETURN NULL (aka not a whole bush)
  
if (@checktiles != @objectdata[hitobj][1]) return;
  
// IF NO OBJECT IS FOUND OR BUG OCCURS, RETURN NULL
  
if (hitobj || hitobj >= objectdata.size()) return;
  
// IF PLAYER DOESN'T HAVE ENOUGH POWER TO DESTROY, RETURN NULL
  // OR IF POWER == 0, RETURN NULL SINCE IT CAN'T BE DESTROYED
  
if (pwr objectdata[hitobj][4][dmgtype] || objectdata[hitobj][4][dmgtype] == 0) return;

  
// REPLACE TILES
  
for (temp.i=0;i<4;i++) {
    
tiles[px+(i%2),py+int(i/2)] = objectdata[hitobj][2][i];
  }
  
updateboard(px,py,2,2); // UPDATE BOARD

  // IF LEAPS ARE SET TO TRUE, PUT A LEAP.
  
if (doleap == trueputleaps(carrydata[carryobj][2],px,py);

  
// RETURN THE DATA OF OBJECT FOR FURTHER USAGE
  
return objectdata[hitobj];


Examples of using it:
Sword:
CheckTiles(player.dir,player.swordpower,true);
CheckTiles(player.dir,{"sword",player.swordpower}, true);
Sword on Horse:
CheckTiles((player.dir+1)%4,{"sword",player.swordp ower},true,0,-1);

Lifting:
PHP Code:

// Call the function in a variable, so it will assign the object data to the var
temp.liftdata CheckTiles(player.dir,{"glove",player.glovepower},false);
// Set the player's attr[3] as carryimage_object.png
// For example, carryimage_bush.png
if (liftdata != nullplayer.attr[3] = "carryimage_" liftdata[0] @ ".png"


DustyPorViva 02-12-2010 07:02 PM

Fixed the power system. The only thing is it may complicate things a bit. I've explained it as best I could in the original post, however.

edit: Fixed a bug that wasn't detecting powers right.

Soala 02-13-2010 12:28 AM

Someone sticky this.

DustyPorViva 03-09-2010 08:56 AM

Minor oversight with leaps fixed:
PHP Code:

function CheckTiles(cdir,pwrdata,doleap,offsetx,offsety) {
  
// A LAYOUT OF DAMAGE TYPES, USED TO FIND THE CORRECT DAMAGE IN THE ARRAY
  
temp.powerlayout = {"sword","glove","hammer"};

  
// THEN FIND THE INDEX OF THE TYPE. IF NO TYPE FOUND, RESORT TO SWORD POWER
  
temp.dmgtype powerlayout.index(pwrdata[0]) == -powerlayout.index(pwrdata[0]);
  
temp.pwr pwrdata[1] == null pwrdata pwrdata[1];

  
temp.objectdata = {
    
//name,tilecheck,tilereplace,leaptype,powers{sword,glove,hammer},canhide(true/false)
    
{"swamp"     ,{0x1A4,0x1A5,0x1B4,0x1B5},{0x2A7,0x2A8,0x2B7,0x2B8}, 1,{1,0,0},0},
    {
"bush"      ,{0x2  ,0x3  ,0x12 ,0x13 },{0x2A5,0x2A6,0x2B5,0x2B6}, 0,{1,1,1},1},
    {
"stake"     ,{0x14A,0x14B,0x15A,0x15B},{0x70A,0x70B,0x71A,0x71B},-1,{0,0,1},0},
    {
"sign"      ,{0x200,0x201,0x210,0x211},{0x70A,0x70B,0x71A,0x71B}, 3,{2,1,1},0},
    {
"rock"      ,{0x22 ,0x23 ,0x32 ,0x33 },{0x72A,0x72B,0x73A,0x73B}, 2,{2,2,2},1},
    {
"blackrock" ,{0x3DE,0x3DF,0x3EE,0x3EF},{0x72A,0x72B,0x73A,0x73B}, 2,{3,3,3},1},
    {
"vase"      ,{0x2AC,0x2AD,0x2BC,0x2BD},{0x6EA,0x6EB,0x6FA,0x6FB}, 2,{2,1,1},0},
    {
"desertbush",{0x35E,0x35F,0x36E,0x36F},{0x388,0x389,0x389,0x388}, 0,{1,1,1},1},
    {
"lavaswamp" ,{0xBE8,0xBE9,0xBF8,0xBF9},{0xC92,0xC93,0xCA2,0xCA3}, 1,{1,0,0},0},
    {
"lavabush"  ,{0xA02,0xA03,0xA12,0xA13},{0xC90,0xC91,0xCA0,0xCA1}, 0,{2,3,1},1},
    {
"lavasign"  ,{0xC52,0xC53,0xC62,0xC63},{0xC50,0xC51,0xC60,0xC61}, 3,{1,1,1},0},
    {
"lavastone" ,{0xA22,0xA23,0xA32,0xA33},{0xC70,0xC71,0xC80,0xC81}, 2,{3,3,2},1},
    {
"lavavase"  ,{0xC65,0xC66,0xC75,0xC76},{0xC72,0xC73,0xC82,0xC83}, 2,{3,1,1},1},
    {
"snowswamp" ,{0x838,0x839,0x848,0x849},{0x2A7,0x2A8,0x2B7,0x2B8}, 1,{1,1,0},0},
    {
"snowbush"  ,{0x7AC,0x7AD,0x7BC,0x7BD},{0xF02,0xF03,0xF12,0xF13}, 0,{1,0,1},1},
    {
"snowsign"  ,{0x7EB,0x7EC,0x7FB,0x7FC},{0xFE5,0xFE6,0xFF5,0xFF6}, 3,{2,3,1},0},
  };

  
// FIND THE CHECKX AND CHECKY 
  
temp.px int(player.+ (cdir==? -1.5 : (cdir==3.5 1.5))) - offsetx;
  
temp.py int(player.+ (cdir==0    : (cdir==4   2))) - offsety;

  
temp.hitobj = -1// INITIATE NO FOUND OBJECT AS -1
  
for (temp.i=0;i<objectdata.size();i++) {
    
// CHECK IF THE TILE IS IN THE ARRAY
    
if (tiles[px,pyin objectdata[i][1]) {
      
temp.objectdata[i][1].index(tiles[px,py]); // FIND POSITION OF TILE IN ARRAY
      
px px-(j%2);                                 // APPLY X OFFSET
      
py py-int(j/2);                              // APPLY Y OFFSET
      
hitobj i;                                    // ASSIGN FOUND OBJECT
      
break;                                         // STOP CHECKING
    
}
  }

  
// CREATE A BLOCK ARRAY OF THE TILES FOR COMPARISON
  
temp.checktiles = {tiles[px,py],tiles[px+1,py],tiles[px,py+1],tiles[px+1,py+1]};
  
// IF WHOLE BLOCK DOES NOT MATCH THE OBJECT, RETURN NULL (aka not a whole bush)
  
if (@checktiles != @objectdata[hitobj][1]) return;
  
// IF NO OBJECT IS FOUND OR BUG OCCURS, RETURN NULL
  
if (hitobj || hitobj >= objectdata.size()) return;
  
// IF PLAYER DOESN'T HAVE ENOUGH POWER TO DESTROY, RETURN NULL
  // OR IF POWER == 0, RETURN NULL SINCE IT CAN'T BE DESTROYED
  
if (pwr objectdata[hitobj][4][dmgtype] || objectdata[hitobj][4][dmgtype] == 0) return;

  
// REPLACE TILES
  
for (temp.i=0;i<4;i++) {
    
tiles[px+(i%2),py+int(i/2)] = objectdata[hitobj][2][i];
  }
  
updateboard(px,py,2,2); // UPDATE BOARD

  // IF LEAPS ARE SET TO TRUE, PUT A LEAP.
  
if (doleap == true && objectdata[hitobj][3] >= 0putleaps(objectdata[hitobj][3],px,py);

  
// RETURN THE DATA OF OBJECT FOR FURTHER USAGE
  
return objectdata[hitobj];



xAndrewx 05-04-2010 07:56 PM

Does this work serverside so all players see this? =o

DustyPorViva 05-04-2010 09:04 PM

Quote:

Originally Posted by xAndrewx (Post 1574092)
Does this work serverside so all players see this? =o

Default tiles(like bushes and signs) automatically are sent to other players. They also automatically respawn. Other objects can be easily fixed to send the data to the serverside and update them there(I did it for the hammer on Unholy Nation).

xAndrewx 05-05-2010 11:28 PM

Awesome- I know this sounds stupid but are you using the default lift system too? Or would I need to code the part of being hit and throwing it etc


All times are GMT +2. The time now is 08:16 PM.

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