tModLoader Official tModLoader Help Thread

Help How to Fix Head
20160629105318_1.jpg

Code:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;


namespace MarcinMod.NPCs.Boss
{
    public class ForestDragon_Head : ModNPC
    {
        public override void SetDefaults()
        {
            npc.name = "ForestDragon";
            npc.displayName = "Forest Dragon";
            npc.soundHit = 1;
            npc.soundKilled = 1;
            npc.lifeMax = 125;
            npc.damage = 20;
            npc.defense = 17;
            npc.knockBackResist = 0.5f;
            npc.width = 45; //22
            npc.height = 45; //18
            npc.boss = true;
            npc.lavaImmune = true;
            npc.noGravity = true;
            npc.noTileCollide = true;
            Main.npcFrameCount[npc.type] = 1;
            npc.value = Item.buyPrice(0, 0, 1, 45);
            npc.npcSlots = 1f;
            npc.netAlways = true;
        }

        public override bool PreAI()
        {
            if (Main.netMode != 1)
            {
                if (npc.ai[0] == 0)
                {
                    npc.realLife = npc.whoAmI;
                    int latestNPC = npc.whoAmI;

                    int WormLength = 1;
                    for (int i = 0; i < WormLength; ++i)
                    {
                        latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body"), npc.whoAmI, 0, latestNPC);
                        Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                        Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
                    }
                    int WormLength2 = 1;
                    for (int i = 0; i < WormLength2; ++i)
                    {
                        latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Legs"), npc.whoAmI, 0, latestNPC);
                        Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                        Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
                    }
                    int WormLength3 = 4;
                    for (int i = 0; i < WormLength3; ++i)
                    {
                        latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body"), npc.whoAmI, 0, latestNPC);
                        Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                        Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
                    }
                    for (int i = 0; i < WormLength2; ++i)
                    {
                        latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Legs"), npc.whoAmI, 0, latestNPC);
                        Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                        Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
                    }
                    for (int i = 0; i < WormLength; ++i)
                    {
                        latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body2"), npc.whoAmI, 0, latestNPC);
                        Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                        Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
                    }
                    for (int i = 0; i < WormLength; ++i)
                    {
                        latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body3"), npc.whoAmI, 0, latestNPC);
                        Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                        Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
                    }
                    latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Tail"), npc.whoAmI, 0, latestNPC);
                    Main.npc[(int)latestNPC].realLife = npc.whoAmI;
                    Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;

                    npc.ai[0] = 1;
                    npc.netUpdate = true;
                }
                int minTilePosX = (int)(npc.position.X / 16.0) - 1;
                int maxTilePosX = (int)((npc.position.X + npc.width) / 16.0) + 2;
                int minTilePosY = (int)(npc.position.Y / 16.0) - 1;
                int maxTilePosY = (int)((npc.position.Y + npc.height) / 16.0) + 2;
                if (minTilePosX < 0)
                    minTilePosX = 0;
                if (maxTilePosX > Main.maxTilesX)
                    maxTilePosX = Main.maxTilesX;
                if (minTilePosY < 0)
                    minTilePosY = 0;
                if (maxTilePosY > Main.maxTilesY)
                    maxTilePosY = Main.maxTilesY;

                bool collision = false;

                float speed = 8f;
                float acceleration = 0.07f;

                Vector2 npcCenter = new Vector2(npc.position.X + npc.width * 0.5f, npc.position.Y + npc.height * 0.5f);
                float targetXPos = Main.player[npc.target].position.X + (Main.player[npc.target].width / 2);
                float targetYPos = Main.player[npc.target].position.Y + (Main.player[npc.target].height / 2);

                float targetRoundedPosX = (float)((int)(targetXPos / 16.0) * 16);
                float targetRoundedPosY = (float)((int)(targetYPos / 16.0) * 16);
                npcCenter.X = (float)((int)(npcCenter.X / 16.0) * 16);
                npcCenter.Y = (float)((int)(npcCenter.Y / 16.0) * 16);
                float dirX = targetRoundedPosX - npcCenter.X;
                float dirY = targetRoundedPosY - npcCenter.Y;

                float length = (float)Math.Sqrt(dirX * dirX + dirY * dirY);
                if (!collision)
                {
                    Rectangle rectangle1 = new Rectangle((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height);
                    int maxDistance = 1000;
                    bool playerCollision = true;
                    for (int index = 0; index < 255; ++index)
                    {
                        if (Main.player[index].active)
                        {
                            Rectangle rectangle2 = new Rectangle((int)Main.player[index].position.X - maxDistance, (int)Main.player[index].position.Y - maxDistance, maxDistance * 2, maxDistance * 2);
                            if (rectangle1.Intersects(rectangle2))
                            {
                                playerCollision = false;
                                break;
                            }
                        }
                    }

                    if (!collision)
                    {
                        npc.TargetClosest(true);
                        float absDirX = Math.Abs(dirX);
                        float absDirY = Math.Abs(dirY);
                        float newSpeed = speed / length;
                        dirX = dirX * newSpeed;
                        dirY = dirY * newSpeed;
                        if (npc.velocity.X > 0.0 && dirX > 0.0 || npc.velocity.X < 0.0 && dirX < 0.0 || (npc.velocity.Y > 0.0 && dirY > 0.0 || npc.velocity.Y < 0.0 && dirY < 0.0))
                        {
                            if (npc.velocity.X < dirX)
                                npc.velocity.X = npc.velocity.X + acceleration;
                            else if (npc.velocity.X > dirX)
                                npc.velocity.X = npc.velocity.X - acceleration;
                            if (npc.velocity.Y < dirY)
                                npc.velocity.Y = npc.velocity.Y + acceleration;
                            else if (npc.velocity.Y > dirY)
                                npc.velocity.Y = npc.velocity.Y - acceleration;
                            if (Math.Abs(dirY) < speed * 0.2 && (npc.velocity.X > 0.0 && dirX < 0.0 || npc.velocity.X < 0.0 && dirX > 0.0))
                            {
                                if (npc.velocity.Y > 0.0)
                                    npc.velocity.Y = npc.velocity.Y + acceleration * 2f;
                                else
                                    npc.velocity.Y = npc.velocity.Y - acceleration * 2f;
                            }
                            if (Math.Abs(dirX) < speed * 0.2 && (npc.velocity.Y > 0.0 && dirY < 0.0 || npc.velocity.Y < 0.0 && dirY > 0.0))
                            {
                                if (npc.velocity.X > 0.0)
                                    npc.velocity.X = npc.velocity.X + acceleration * 2f;
                                else
                                    npc.velocity.X = npc.velocity.X - acceleration * 2f;
                            }
                        }
                        else if (absDirX > absDirY)
                        {
                            if (npc.velocity.X < dirX)
                                npc.velocity.X = npc.velocity.X + acceleration * 1.1f;
                            else if (npc.velocity.X > dirX)
                                npc.velocity.X = npc.velocity.X - acceleration * 1.1f;
                            if (Math.Abs(npc.velocity.X) + Math.Abs(npc.velocity.Y) < speed * 0.5)
                            {
                                if (npc.velocity.Y > 0.0)
                                    npc.velocity.Y = npc.velocity.Y + acceleration;
                                else
                                    npc.velocity.Y = npc.velocity.Y - acceleration;
                            }
                        }
                        else
                        {
                            if (npc.velocity.Y < dirY)
                                npc.velocity.Y = npc.velocity.Y + acceleration * 1.1f;
                            else if (npc.velocity.Y > dirY)
                                npc.velocity.Y = npc.velocity.Y - acceleration * 1.1f;
                            if (Math.Abs(npc.velocity.X) + Math.Abs(npc.velocity.Y) < speed * 0.5)
                            {
                                if (npc.velocity.X > 0.0)
                                    npc.velocity.X = npc.velocity.X + acceleration;
                                else
                                    npc.velocity.X = npc.velocity.X - acceleration;
                            }
                        }
                    }
                    npc.rotation = (float)Math.Atan2(npc.velocity.Y, npc.velocity.X) + 1.57f;

                }

               
            }
            return false;
        }
    }
}
 
help plshow do i update tmodloader when it is already installed with out losing my stuf?
Just install the latest version of tModLoader.
Since tModLoader is still a WIP, there is an ever so slight chance of your save files getting corrupted with the latest version of tModLoader.
With the latest update, however, this is not the case.
 
ok so i opended the zip extracted the files the opended it up and double cliked the instaler but it said it had no place to install 2
[doublepost=1467239694,1467239366][/doublepost]
You have installed the earlier versions of tModLoader, correct?
Open up the zip file and run the installer. If that doesn't work, follow the instructions in the readme file.
it says winods can not find a destonation file
 
ok so i opended the zip extracted the files the opended it up and double cliked the instaler but it said it had no place to install 2
[doublepost=1467239694,1467239366][/doublepost]
it says winods can not find a destonation file

so i am copying the newest files and copying them into where the other files are correct?
 
ok
[doublepost=1467240234,1467240176][/doublepost]thats the vannial
[doublepost=1467240289][/doublepost]wait i found it ok do i need to replace all of the old files?
[doublepost=1467240511][/doublepost]
im not exactly sure where i am to copy the files tho sorry im kinda new to this

ok thank you so much for the help
 
I'm having trouble with a NPC i'm working on, called the Dark Ninja. Whenever he spawns, his animation is too fast (i made a three part animation) and he bobs up and down very rapidly. It's weird. I also want to make him as fast as Bone Lee.

Here's the code:
Code:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace TacticalMod.NPCs
{
  public class StealthNinja : ModNPC
  {
  public override void SetDefaults()
  {
  npc.name = "Dark Ninja";
  npc.displayName = "Dark Ninja";
  npc.width = 50;
  npc.height = 40;
  npc.damage = 45;
  npc.defense = 10;
  npc.lifeMax = 600;
  npc.soundHit = 1;
  npc.soundKilled = 1;
  npc.value = 60f;
       npc.aiStyle = 3;
  npc.knockBackResist = 0.5f;
  Main.npcFrameCount[npc.type] = 3;
  aiType = NPCID.PossessedArmor;
  animationType = NPCID.PossessedArmor;
  }

  public override float CanSpawn(NPCSpawnInfo spawnInfo)
  {
  return spawnInfo.spawnTileY < Main.rockLayer && !Main.dayTime ? 1f : 1f;
  }
  public override void FindFrame(int frameHeight)
  {
  npc.frameCounter -= 0.01f;
  npc.frameCounter %= Main.npcFrameCount[npc.type];
  int frame = (int)npc.frameCounter;
  npc.frame.Y = frame * frameHeight;
  npc.spriteDirection = npc.direction;
  }
  public override void NPCLoot()
  {
  {
  Item.NewItem((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height, mod.ItemType("Tactite"), 1);
  }

  }
  }
}
and the animation:
StealthNinja.png

If anyone could help me that'd be great.
 
Last edited:
After, you can go classic loop:

Code:
public override void FindFrame(int frameHeight)
{
int height =  51;  //(height of the sprite for me, rarely equals to hitbox)
npc.frameCounter --;
if(npc.frameCounter < 0){
npc.frameCounter=60;
npc.frame.Y = (npc.frame.Y+height) % height* 3; //3 or Main.npcFrameCount[npc.type]
npc.spriteDirection = npc.direction;
}
if(npc.velocity == Vector2.Zero) npc.frame.Y=height*2
}

After, i am not sure.
I see a thing, also, you need maybe add others things if your ninja have velocity == Vector2.Zero where always you take the 3th frame.
Sorry if i have mistake.
 
After, you can go classic loop:

Code:
public override void FindFrame(int frameHeight)
{
int height =  51;  //(height of the sprite for me, rarely equals to hitbox)
npc.frameCounter --;
if(npc.frameCounter < 0){
npc.frameCounter=60;
npc.frame.Y = (npc.frame.Y+height) % height* 3; //3 or Main.npcFrameCount[npc.type]
npc.spriteDirection = npc.direction;
}
if(npc.velocity == Vector2.Zero) npc.frame.Y=height*2
}

After, i am not sure.
I see a thing, also, you need maybe add others things if your ninja have velocity == Vector2.Zero where always you take the 3th frame.
Sorry if i have mistake.
Just a little FYI: there is already a variable available that holds the height of one animation frame: frameHeight; the parameter of the FindFrame method.
 
Back
Top Bottom