tModLoader How to reach/access map data ?

Eggmasstree

Terrarian
Hello !
Im new to modding and I need some help.
So i'm trying to make an unnerf rod of discord (When you had no CD on TP) and i have 2 problems.

- I need to get the tile type under my mouse position (Or maybe more like "around" my mouse position) to see if I can TP or not. And I can't find anything about "world" related tiles - something like a huge array of all the tiles of the map. Sorry if a similar questions has been asked before. I've been checking every map/tile related classes in http://blushiemagic.github.io/tModLoader/html/index.html but I cant find anything about tile type and their position in the world.

- And less importantly, when you use the actual rod of discord you have a little "fade to black" effect. I wonder if you can do something similar with tmodloader :)

Thanks for the help :)
 
To get the cursor's position in the world, you can use the static property Main.MouseWorld, which will get you the position of the mouse in the world in pixels. Tiles are stored in a two dimensional array, and tiles are 16 by 16 pixels large. So to convert your mouse position to tiles, divide your mouse position by 16 to get the xth and yth coordinate of the tile your cursor is in. Then, you can get the tile in the array, check its type and do whatever you want with it.

You would end up with something like this:

Code:
Vector2 mousePosition = Main.MouseWorld;

int tileX = (int)(mousePosition.X / 16f);
int tileY = (int)(mousePosition.Y / 16f);

Tile cursorTile = Main.tile[tileX, tileY];

As for fading in, you will probably just want to use the same Teleport method that the Rod of Discord uses. It's in Player, so that would be:
Code:
player.Teleport(destination, 1);
The 1 is the style, which is also the one the Rod of Discord uses. I suppose you can experiment with 0 and 2, but don't use 4, as that's reserved for the Portal Gun.
 
To get the cursor's position in the world, you can use the static property Main.MouseWorld, which will get you the position of the mouse in the world in pixels. Tiles are stored in a two dimensional array, and tiles are 16 by 16 pixels large. So to convert your mouse position to tiles, divide your mouse position by 16 to get the xth and yth coordinate of the tile your cursor is in. Then, you can get the tile in the array, check its type and do whatever you want with it.

You would end up with something like this:

Code:
Vector2 mousePosition = Main.MouseWorld;

int tileX = (int)(mousePosition.X / 16f);
int tileY = (int)(mousePosition.Y / 16f);

Tile cursorTile = Main.tile[tileX, tileY];

As for fading in, you will probably just want to use the same Teleport method that the Rod of Discord uses. It's in Player, so that would be:
Code:
player.Teleport(destination, 1);
The 1 is the style, which is also the one the Rod of Discord uses. I suppose you can experiment with 0 and 2, but don't use 4, as that's reserved for the Portal Gun.

I almost got everything and was about to post a reply. Thanks for the tp thingy and the '/16'. I would have never find this by myself :x

here's what I had

Code:
Vector2 mouse_position = new Vector2(Main.mouseX + Main.screenPosition.X, Main.mouseY + Main.screenPosition.Y);

Tile tileMouse = new Tile();
tileMouse = Main.tile[Convert.ToInt32(Main.screenPosition.X), Convert.ToInt32(Main.screenPosition.Y)];

Since Im not used to UI yet is there a way to print out text in chat ? For some debug ? So far I can kill myself and write down a message with it but I didnt find a way to print out a message like a player.
Thanks for the help :)
 
If anyone's intersted in final code :

Code:
public override void UseStyle(Player player)
  {
  bool canTP = true;

  if (player.itemTime == 0)
  {
   
  Vector2 mousePosition = Main.MouseWorld;
   
  List<Tile> L = new List<Tile>();

  //Checking all tiles around cursor (on 2x3)
  for (int k = 0; k < 2; k++)
  {
  for (int i = 0; i < 3; i++)
  {
  L.Add(Main.tile[(int)((mousePosition.X + k * 16) / 16f), (int)((mousePosition.Y - i * 16) / 16f)]);
  }
  }
   

  Vector2 truePosition = new Vector2();

  //x.y of tp position
  truePosition.X = ((int)mousePosition.X) - 8;
  truePosition.Y = ((int)mousePosition.Y) - 40;

  //Checking if any of the tiles around the cursor are active or solid
  //If any is solid or active, then set a bool to false
  L.ForEach(delegate (Tile u)
  {
  if (u.active() == true)
  {
  if(Main.tileSolid[u.type] == true)
  {
  canTP = false;
  }
  }
  });

  //tp the player
  if (canTP)
  {
  player.Teleport(truePosition, 1);
  }
   
  player.itemTime = item.useTime;
  }


  }
 
Back
Top Bottom