TConfig Need Help w/ Npc Editing

Discussion in 'Released Mods' started by guyguyga, Dec 16, 2012.

  1. guyguyga

    guyguyga Slimed Zombie

    I need help making a town npc hurt an enemy npc using collision boxes. Can someone give me a code to work with? It's named "Guard", and I can spawn it, but i can't get it to hurt enemies.
     
  2. Wigglesniff

    Wigglesniff World Feeder

    Could you upload the code that you have right now?
     
  3. guyguyga

    guyguyga Slimed Zombie

    sure. This one is named "N", but I want to change it to "Guard". Whenever I put this in my code, it says compile failed.

    public void AI()
    {
    NPC N = npc; //just naming npc as N
    N.ai(true); //make the NPC run a regular NPC's ai ,using the aistyle , and pretendType it might have been provided with.

    Rectangle NR = new Rectangle((int)N.position.X,(int)N.position.Y,N.width,N.height); //a collision box of the NPC
    foreach(NPC N2 in Main.npc)
    {
    if(N2.type==N.type || N2.townNPC) continue; //if the two are of the same type , or the checked one is a town npc , skip it
    Rectangle N2R = new Rectangle((int)N2.position.X,(int)N2.position.Y,N2.width,N2.height);
    if(!NR.Intersects(N2R)) continue; //if the boxes don't touch each other , skip

    int DMG = 106; //damage
    float Knock = 10f; //knockback
    int direct = N.direction;

    N2.StrikeNPC(DMG, Knock, direct, false, false);
    }
    }
     
  4. Electroflux

    Electroflux Blazing Wheel

    No idea but one thing I do know, his name should be Guard.
     
  5. guyguyga

    guyguyga Slimed Zombie

    Do I replace ALL the "N"s with "Guard", or do I only replace the first one?

    Also, I just got this mod that gives me an accessory that spawns blades which attack enemies in range. It's a 7 bladed cloak from someone in this forum(Can't remember name.) Can you guys tell me how to make it follow a town npc instead of me? When I edit it, do I edit the item, or the projectile?
     
  6. Wigglesniff

    Wigglesniff World Feeder

    I couldn't find anything either...
     
  7. Yoraiz0r

    Yoraiz0r The Wizard

    Le sigh......
    This code you have there is mine.......
    The blade cloak is also my thing........
    meh.

    The code is perfectly fine as is , its rather simple really.
    Also , the N thing does not need to be changed , the NPC's name has nothing to do with the code.

    If you have an NPC ini text file called Guard.ini , then make a text file called Guard.cs and put that code in there , that is all.
    -basic coding alphabet in terraria mods really , codes go into someting's cs file-

    I really don't suggest tackling 'new' kinds of AI if you don't know how to code much yet.
     
  8. guyguyga

    guyguyga Slimed Zombie

    I tried that. I put the code at the bottom of my .cs file, but it didn't work: it always failed to compile. Can you tell me what's wrong?
    public static string ChatFuncName() {
    return "Enchant";
    }
    public void ChatFunc() {
    //Called when "Enchant" is clicked on
    //Setup the interface...

    EnchantInterface.Create();
    Main.npcChatText = "";
    Main.playerInventory = true;
    //By defining the npcInterface, it will be displayed appropriately...
    }
    public class EnchantInterface : Interfaceable {
    int upgradeCost=0;
    public static void Create() {
    Config.npcInterface = new InterfaceObj(new EnchantInterface(), 3, 3); //Pass along this code instance, and the # of item slots you want to use
    Config.npcInterface.AddItemSlot(150,250);
    Config.npcInterface.AddItemSlot(230,250);
    Config.npcInterface.AddItemSlot(190,300);
    Config.npcInterface.AddText("Enchant:", 130, 230, false, 0.9f);
    Config.npcInterface.AddText("Rune:", 225, 230, false, 0.9f);
    Config.npcInterface.AddText("Result:", 140, 310, false,0.9f);
    }
    public void ButtonClicked(int button) {

    }
    public bool CanPlaceSlot(int slot, Item mouseItem) {
    //The user shouldn't be able to place things in the output slot
    if(slot==2) {
    if(mouseItem.type==0 || mouseItem.stack==0) return true;
    else return false;
    //if(Config.npcInterface.itemSlots[2].type==0) return false;
    }
    return true;
    }
    public void PlaceSlot(int slot) {
    //This is called after an item is placed into the slot, or taken
    //(both happen on left-click)
    //Slot 0: Weapon/equipment to upgrade
    //Slot 1: Items used for upgrading
    //Slot 2: Result of upgrading
    Item[] itemSlots = Config.npcInterface.itemSlots;
    if(itemSlots[0] != null && itemSlots[1]!=null) {
    if(slot==2 && upgradeCost>0) {
    itemSlots[0]=new Item();
    itemSlots[1].stack-=upgradeCost;
    upgradeCost=0;
    return;
    }
    //Create a complete copy of the item, then call its upgrade() method
    itemSlots[2] = new Item();
    MemoryStream mstream = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(mstream);
    Codable.SaveCustomData(itemSlots[0], writer);
    mstream.Position=0;
    itemSlots[2].SetDefaults(itemSlots[0].name);
    itemSlots[2].Prefix(itemSlots[0].prefix);
    Codable.LoadCustomData(itemSlots[2], new BinaryReader(mstream));
    Codable.globalRunKnowledge=true;
    if(itemSlots[2].RunMethod("Upgrade", itemSlots[1])) {
    //Method ran, now let's get the return value
    int amtUsed = (int)Codable.customMethodReturn;
    if(amtUsed==0) itemSlots[2]=new Item();
    else upgradeCost=amtUsed;
    }
    else {
    upgradeCost=0;
    itemSlots[2]=new Item();
    }
    Codable.globalRunKnowledge=false;
    }
    else upgradeCost=0;
    if(itemSlots[slot].stack==0) itemSlots[slot]=new Item();
    }

    public bool DropSlot(int slot) {
    //Called when the interface is closed.
    //Return true to drop the item, false otherwise
    if(slot==2) return false;
    return true;
    }
    }

    public static string SetName() {
    if(Main.rand.Next(2)==0)
    return "Arthur";

    return "Lancelot";
    }

    public static bool TownSpawn() {
    if(Main.hardMode)
    return true;

    return false;
    }

    public static string Chat() {
    if(Main.rand.Next(2)==0)
    return "I eat Corruptors for breakfast.";
    return "The pixies around here have a serious temper.";
    }

    public static void SetupShop(Chest chest) {
    int index=0;
    chest.item[index].SetDefaults("Flame Beam");
    index++;
    chest.item[index].SetDefaults("Muramasa");
    index++;
    chest.item[index].SetDefaults("Red Hero's Shirt");
    index++;
    chest.item[index].SetDefaults("Red Hero's Pants");
    index++;
    chest.item[index].SetDefaults("Red Hero's Hat");
    index++;
    }
    public void AI()
    {
    NPC N = npc; //just naming npc as N
    N.ai(true); //make the NPC run a regular NPC's ai ,using the aistyle , and pretendType it might have been provided with.

    Rectangle NR = new Rectangle((int)N.position.X,(int)N.position.Y,N.width,N.height); //a collision box of the NPC
    foreach(NPC N2 in Main.npc)
    {
    if(N2.type==N.type || N2.townNPC) continue; //if the two are of the same type , or the checked one is a town npc , skip it
    Rectangle N2R = new Rectangle((int)N2.position.X,(int)N2.position.Y,N2.width,N2.height);
    if(!NR.Intersects(N2R)) continue; //if the boxes don't touch each other , skip

    int DMG = 9999; //damage
    float Knock = 100f; //knockback
    int direct = N.direction;

    N2.StrikeNPC(DMG, Knock, direct, false, false);
    }
    }
     
  9. guyguyga

    guyguyga Slimed Zombie

    When I do the code without the collision boxes, it compiles, but when i do it with the code, it fails to compile. Sigh. I wish I was better at this...
    Also, if you could help me make the blades follow a town npc, I'd really appreciate it! So far, I've tried to make npcs wear accessories so that it could protect them, but that failed miserably. Any help is appreciated. Thanks so much.
     
  10. Yoraiz0r

    Yoraiz0r The Wizard

    Please:
    -Don't double post , you can edit your post and people who watch the thread will be re-notified.
    -Provide the exact compilation error you are getting so I can laze around rather then copy pasting this code that is originally belonged to Surfpup.

    Also I am on a lazy mode the last few days so I shall not be rewriting the code to follow an NPC , should be simple though with my codes.
     
    TheUnrealMegashark likes this.
  11. guyguyga

    guyguyga Slimed Zombie

    My problem is in the thumbnail. Btw, thanks so much for your help and your EPIC mods. You said that it was simple to make the weapon follow the Town NPC with your code, but I don't know where to start. Could you tell me what to change in order to make it follow the npc? I've been trying for so long without any success. Please help whenever you can.
     

    Attached Files:

  12. Empio

    Empio Blazing Wheel

    npc.ai is an array of 3 floats. You want npc.AI(true) instead.
     
  13. Yoraiz0r

    Yoraiz0r The Wizard

    4* (there's index [3] and you start at 0 :p )

    also here's the issue , lol

    Code:
    //what you have
    npc.ai(true);
     
    //what you need
    npc.AI(true);
    
     
  14. guyguyga

    guyguyga Slimed Zombie

    OMG!!!!!! THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU! INFINITE THANKS TO ALL OF YOU!!!!!!!

    Also, whenever you have time, I'd really appreciate you helping me make the "pet blades" follow the town npc. What I'm trying to do as of now is define the owner as the Guard, and make the "pet" follow him. Please comment whenever you can.
     
  15. Empio

    Empio Blazing Wheel

    If you want the pet to follow him perfectly, you could create a Vector2 array that records the guard's positions and have the pet set to those positions with an offset each update.

    In Guard.cs

    Code:
    const int PosCount = 60;
     
    Vector2[] GuardPositions = new Vector2[PosCount];
     
    int CurPos = 0;
     
    public void AI()
    {
        for (int Pos = 0; Pos < PosCount - 1; Pos++)
            GuardPositions[Pos] = GuardPositions[Pos + 1];
     
        GuardPositions[PosCount - 1] = npc.position;
     
        if (CurPos < PosCount)
            CurPos++;
    }
     
    public Vector2 GetPos()
    {
        return GuardPositions[0];
    }
     
    public bool ArrayFull()
    {
        return CurPos >= PosCount;
    }
    In Pet.cs

    Code:
    // you will need to tell the pet what the guard's ID is
    int GuardID = -1;
     
    int GuardType = Config.npcDefs.byName["Guard"].type;
     
    // set this to position the pet properly
    Vector2 Offset;
     
    public void AI()
    {
        if (Guard != -1 && Main.npc[Guard] != null && Main.npc[Guard].active)
        {
            Main.npc[Guard].RunMethod("ArrayFull", null);
     
            if (!(bool)Codable.customMethodReturn)
                return;
     
            Main.npc[Guard].RunMethod("GetPos", null);
            npc.position = (Vector2)Codable.customMethodReturn + Offset;
        }
    }
     
  16. Yoraiz0r

    Yoraiz0r The Wizard

    This code in practice wouldn't work out too well , if you set the offest to anything different from 0,0 the pet might be placing itself inside solid tiles and the likes , and otherwise the pet will flop itself onto the npc after a while due to no offset.
    However the concept is proper for tracing the NPC's path (although that has nothing to do with how the blade cloaks work)
     
  17. Empio

    Empio Blazing Wheel

    An alternative is...

    In Pet.cs

    Code:
    // you will need to tell the pet what the guard's ID is
    int GuardID = -1;
     
    int GuardType = Config.npcDefs.byName["Guard"].type;
     
    // how fast the pet moves
    float MoveSpeed = 2.0f;
     
    public void AI()
    {
        if (Guard != -1 && Main.npc[Guard] != null && Main.npc[Guard].active)
        {
            Vector2 Distance;
            Distance.X = Main.npc[Guard].position.X - npc.position.X + (Main.npc[Guard].width - npc.width) * 0.5f;
            Distance.Y = Main.npc[Guard].position.Y - npc.position.Y + (Main.npc[Guard].height - npc.height) * 0.5f;
     
            if (Distance.LengthSquared() < Main.npc[Guard].width * Main.npc[Guard].width * 0.25f +
                npc.width * npc.width * 0.25f)
            {
                npc.velocity = Vector2.Zero;
                return;
            }
     
            npc.velocity = Vector2.Normalize(Distance) * MoveSpeed;
        }
    }
     
    Yoraiz0r likes this.
  18. guyguyga

    guyguyga Slimed Zombie

    I did this:
    Code:
    public static string ChatFuncName() {
        return "Enchant";
    }
    public void ChatFunc() {
        //Called when "Enchant" is clicked on
        //Setup the interface...
     
        EnchantInterface.Create();
        Main.npcChatText = "";
        Main.playerInventory = true;
        //By defining the npcInterface, it will be displayed appropriately...
    }
    public class EnchantInterface : Interfaceable {
        int upgradeCost=0;
        public static void Create() {
            Config.npcInterface = new InterfaceObj(new EnchantInterface(), 3, 3); //Pass along this code instance, and the # of item slots you want to use
            Config.npcInterface.AddItemSlot(150,250);
            Config.npcInterface.AddItemSlot(230,250);
            Config.npcInterface.AddItemSlot(190,300);
            Config.npcInterface.AddText("Enchant:", 130, 230, false, 0.9f);
            Config.npcInterface.AddText("Rune:", 225, 230, false, 0.9f);
            Config.npcInterface.AddText("Result:", 140, 310, false,0.9f);
        }
        public void ButtonClicked(int button) {
       
        }
        public bool CanPlaceSlot(int slot, Item mouseItem) {
            //The user shouldn't be able to place things in the output slot
            if(slot==2) {
                if(mouseItem.type==0 || mouseItem.stack==0) return true;
                else return false;
                //if(Config.npcInterface.itemSlots[2].type==0) return false;
            }
            return true;
        }
        public void PlaceSlot(int slot) {
            //This is called after an item is placed into the slot, or taken
                //(both happen on left-click)
            //Slot 0: Weapon/equipment to upgrade
            //Slot 1: Items used for upgrading
            //Slot 2: Result of upgrading
            Item[] itemSlots = Config.npcInterface.itemSlots;
            if(itemSlots[0] != null && itemSlots[1]!=null) {
                if(slot==2 && upgradeCost>0) {
                    itemSlots[0]=new Item();
                    itemSlots[1].stack-=upgradeCost;
                    upgradeCost=0;
                    return;
                }
                //Create a complete copy of the item, then call its upgrade() method
                itemSlots[2] = new Item();
                MemoryStream mstream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(mstream);
                Codable.SaveCustomData(itemSlots[0], writer);
                mstream.Position=0;
                itemSlots[2].SetDefaults(itemSlots[0].name);
                itemSlots[2].Prefix(itemSlots[0].prefix);
                Codable.LoadCustomData(itemSlots[2], new BinaryReader(mstream));
                Codable.globalRunKnowledge=true;
                if(itemSlots[2].RunMethod("Upgrade", itemSlots[1])) {
                    //Method ran, now let's get the return value
                    int amtUsed = (int)Codable.customMethodReturn;
                    if(amtUsed==0) itemSlots[2]=new Item();
                    else upgradeCost=amtUsed;
                }
                else {
                    upgradeCost=0;
                    itemSlots[2]=new Item();
                }
                Codable.globalRunKnowledge=false;
            }
            else upgradeCost=0;
            if(itemSlots[slot].stack==0) itemSlots[slot]=new Item();
        }
     
        public bool DropSlot(int slot) {
            //Called when the interface is closed.
            //Return true to drop the item, false otherwise
            if(slot==2) return false;
            return true;
        }
    }
     
    public static string SetName() {
        if(Main.rand.Next(2)==0)
            return "Arthur";
       
        return "Lancelot";
    }
     
    public static bool TownSpawn() {
        if(Main.hardMode)
            return true;
           
        return false;
    }
     
    public static string Chat() {
        if(Main.rand.Next(2)==0)
            return "I eat Corruptors for breakfast.";
        return "The pixies around here have a serious temper.";
    }
     
    public static void SetupShop(Chest chest) {
        int index=0;
        chest.item[index].SetDefaults("Flame Beam");
        index++;
        chest.item[index].SetDefaults("Muramasa");
        index++;
        chest.item[index].SetDefaults("Red Hero's Shirt");
        index++;
        chest.item[index].SetDefaults("Red Hero's Pants");
        index++;
        chest.item[index].SetDefaults("Red Hero's Hat");
        index++;
    }
     
    public void AI()
    {
          NPC N = npc; //just naming npc as N
          N.AI(true); //make the NPC run a regular NPC's ai ,using the aistyle , and pretendType it might have been provided with.
     
          Rectangle NR = new Rectangle((int)N.position.X,(int)N.position.Y,N.width,N.height); //a collision box of the NPC
          foreach(NPC N2 in Main.npc)
          {
                if(N2.type==N.type || N2.townNPC) continue; //if the two are of the same type , or the checked one is a town npc , skip it
                Rectangle N2R = new Rectangle((int)N2.position.X,(int)N2.position.Y,N2.width,N2.height);
                if(!NR.Intersects(N2R)) continue; //if the boxes don't touch each other , skip
     
                int DMG = 9999; //damage
                float Knock = 100f; //knockback
                int direct  = N.direction;
     
                N2.StrikeNPC(DMG, Knock, direct, false, false);
          }
          for (int Pos = 0; Pos < PosCount - 1; Pos++)
                GuardPositions[Pos] = GuardPositions[Pos + 1];
     
          GuardPositions[PosCount - 1] = npc.position;
     
          if (CurPos < PosCount)
                CurPos++;
          }
    }
     
    const int PosCount = 60;
     
    Vector2[] GuardPositions = new Vector2[PosCount];
     
    int CurPos = 0;
     
    public Vector2 GetPos()
    {
        return GuardPositions[0];
    }
     
    public bool ArrayFull()
    {
        return CurPos >= PosCount;
    }
    and got this problem
     

    Attached Files:

  19. Empio

    Empio Blazing Wheel

    Code:
    if (CurPos < PosCount)
          CurPos++;
    }
    Rogue closing bracket }
     
  20. guyguyga

    guyguyga Slimed Zombie

    got it. BTW, how do I get the projectile to spawn? I made an item to spawn the projectile, but it ends up crashing. I also want to make the projectile attack enemy npcs.
     

Share This Page