tModLoader Platforms aren't considered solid blocks

LucLuz

Terrarian
I was making some things for my npc and i noticed that he just don't work when on a platform, my code is checking if he's on the ground before doing some things like jumping and adding friction, so i want to know if there's any way to make platforms count as solid tiles.

Code:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using System;

namespace LucTerraMod.Content.AI
{
    public class ModFighterAI
    {
        public static float Speed = 1f;
        public static float JumpPower = 7f;
        public static bool collidingY = false;

        public static void RunAI(NPC npc) {
            npc.TargetClosest(true);
            npc.spriteDirection = npc.direction;

            Friction(npc);
            Move(npc);
            CheckObstacles(npc);
        }

        private static void Move(NPC npc) {
            if (npc.ai[0] != 1f) {
                if (Math.Abs(npc.velocity.X) == 0 || Math.Sign(npc.velocity.X) == npc.direction) {
                    npc.velocity.X = Speed * npc.direction;
                }
            }
        }

        private static void CheckObstacles(NPC npc) {
            int tileX = (int)((npc.Center.X + npc.direction * npc.width / 2 + npc.direction) / 16);
            int tileY = (int)((npc.position.Y + npc.height -1f) / 16f);

            bool blockeddown = WorldGen.SolidOrSlopedTile(tileX, tileY);
            bool blockedfront = WorldGen.SolidOrSlopedTile(tileX, tileY - 1);
            bool blockedup = WorldGen.SolidOrSlopedTile(tileX, tileY -2);
            bool holeunder = !WorldGen.SolidOrSlopedTile(tileX, tileY + 1);
            collidingY = Collision.SolidCollision(npc.position, npc.width, npc.height + 1);

            if (blockedup && collidingY) {
                Jump(npc, 1);
            } else if (blockedfront && collidingY) {
                Jump(npc);
            } else if (blockeddown && collidingY) {
                npc.velocity.Y += -3.5f;
            } else if (holeunder && collidingY) {
                Jump(npc, 1.7f);
            }
        }

        private static void Jump(NPC npc, float extraforce = 0f) {
            npc.velocity.Y = -(JumpPower + extraforce);
        }

        private static void Friction(NPC npc) {
            if (collidingY) {
                npc.velocity.X *= 0.8f;
            }
        }
    }
}
 
The behavior you probably want can maybe be found from Collision.SolidCollision(). Though, the issue with this function is that it's for many tiles and also takes into account the size of half blocks. If you just want to check if a tile is "solid", you could use Tile.HasUnactuatedTile, Main.tileSolid, and Main.tileSolidTop. Typically, tiles that are "solid" but have Main.tileSolidTop as true are things like platforms and maybe the tinker's workshop and will be considered as non-solid for things like liquids.
I looked and couldn't find any built in function that does this, so here is a short one that can be used in the same way as WorldGen.SolidOrSlopedTile(), but as you wish:
C#:
public static bool CollidableTile(int tileX, int tileY, bool allowSolidTop = true)
{
    Tile t = Main.tile[tileX, tileY];
    return t.HasUnactuatedTile && Main.tileSolid[t.TileType] && (allowSolidTop || !Main.tileSolidTop[t.TileType]);
}
 
Last edited:
Back
Top Bottom