public static void DrawBannerGame()
 {
     if (!ManagingGuardianHouses)
     {
         return;
     }
     try
     {
         string MouseText = "";
         for (int g = 0; g < WorldMod.MaxGuardianNpcsInWorld; g++)
         {
             WorldMod.GuardianTownNpcState townnpc = WorldMod.GuardianNPCsInWorld[g];
             if (townnpc != null && !townnpc.Homeless && WorldMod.IsGuardianNpcInWorld(townnpc.CharID))
             {
                 int BannerX = townnpc.HomeX, BannerY = townnpc.HomeY;
                 if (BannerX < 0 || BannerY < 0)
                 {
                     continue;
                 }
                 BannerY--;
                 if (Main.tile[BannerX, BannerY] == null)
                 {
                     continue;
                 }
                 bool EndsOnNullTile = false;
                 while (!Main.tile[BannerX, BannerY].active() || !Main.tileSolid[(int)Main.tile[BannerX, BannerY].type])
                 {
                     BannerY--;
                     if (BannerY < 10)
                     {
                         break;
                     }
                     if (Main.tile[BannerX, BannerY] == null)
                     {
                         EndsOnNullTile = true;
                         break;
                     }
                 }
                 if (EndsOnNullTile)
                 {
                     continue;
                 }
                 TerraGuardian guardian = null;
                 foreach (TerraGuardian tg in WorldMod.GuardianTownNPC)
                 {
                     if (townnpc.IsID(tg.ID, tg.ModID))
                     {
                         guardian = tg;
                         break;
                     }
                 }
                 if (guardian == null)
                 {
                     continue;
                 }
                 const int PaddingX = 8;
                 int       PaddingY = 18;
                 if (Main.tile[BannerX, BannerY].type == 19)
                 {
                     PaddingY -= 8;
                 }
                 BannerY++;
                 Vector2 BannerPosition = new Vector2(BannerX * 16 + PaddingX, BannerY * 16 + PaddingY) - Main.screenPosition;
                 DrawBanner(guardian, BannerPosition, Lighting.GetColor(BannerX, BannerY));
                 BannerPosition -= new Vector2(Main.HouseBannerTexture[1].Width * 0.5f, Main.HouseBannerTexture[1].Height * 0.5f);
                 if (Main.mouseX >= BannerPosition.X && Main.mouseX < BannerPosition.X + Main.HouseBannerTexture[1].Width &&
                     Main.mouseY >= BannerPosition.Y && Main.mouseY < BannerPosition.Y + Main.HouseBannerTexture[1].Height)
                 {
                     //MainPlayer.mouseInterface = true;
                     MouseText = guardian.Name;
                     if (Main.mouseRight && Main.mouseRightRelease)
                     {
                         if (!guardian.IsStarter && !guardian.IsPlayerBuddy(Main.LocalPlayer) && guardian.FriendshipLevel < guardian.Base.MoveInLevel)
                         {
                             Main.NewText(guardian.Name + (Main.rand.Next(2) == 0 ? " refuses to leave their house." : " doesn't want to be kicked out of their house."), Color.PaleVioletRed);
                         }
                         else
                         {
                             townnpc.Homeless  = true;
                             townnpc.HomeX     =
                                 townnpc.HomeY = -1;
                             townnpc.ValidateHouse();
                             Main.PlaySound(12, -1, -1, 1, 1f, 0f);
                         }
                     }
                 }
             }
         }
         if (MouseText != "")
         {
             Utils.DrawBorderString(Main.spriteBatch, MouseText, new Vector2(Main.mouseX, Main.mouseY + 16), Color.White, 1f, 0.5f);
         }
     }
     catch
     {
     }
 }
Пример #2
0
        public override void Update(TerraGuardian guardian)
        {
            if (guardian.OwnerPos > -1 || Main.invasionType <= 0)
            {
                InUse = false;
                return;
            }
            if (guardian.UsingFurniture)
            {
                guardian.LeaveFurniture();
            }
            switch (Step)
            {
            case 0:
            {
                int DefendX = 0, DefendY = 0;
                WorldMod.GuardianTownNpcState townnpc = guardian.GetTownNpcInfo;
                bool MoveLeft = Main.rand.Next(2) == 0;
                if (townnpc != null && !townnpc.Homeless)
                {
                    DefendX = townnpc.HomeX;
                    DefendY = townnpc.HomeY;
                    WorldMod.GuardianBuildingInfo house = townnpc.HouseInfo;
                    if (house != null)
                    {
                        float HouseXStack = 0;
                        byte  Count       = 0;
                        foreach (WorldMod.GuardianTownNpcState tgh in house.GuardiansLivingHere)
                        {
                            if (!tgh.Homeless)
                            {
                                HouseXStack += tgh.HomeX;
                                Count++;
                            }
                        }
                        if (Count > 0)
                        {
                            HouseXStack /= Count;
                            MoveLeft     = (DefendX >= HouseXStack);
                        }
                    }
                }
                else
                {
                    NPC   NearestNpc      = null;
                    float NearestDistance = 1000;
                    for (int i = 0; i < 200; i++)
                    {
                        if (Main.npc[i].active && Main.npc[i].townNPC && Main.npc[i].type != Terraria.ID.NPCID.OldMan && !Main.npc[i].homeless)
                        {
                            float Distance = (Main.npc[i].Center - guardian.CenterPosition).Length();
                            if (Distance < NearestDistance)
                            {
                                NearestNpc      = Main.npc[i];
                                NearestDistance = Distance;
                            }
                        }
                    }
                    if (NearestNpc != null)
                    {
                        DefendX = NearestNpc.homeTileX;
                        DefendY = NearestNpc.homeTileY;
                    }
                    else
                    {
                        DefendX = (int)(guardian.Position.X * (1f / 16));
                        DefendX = (int)(guardian.Position.Y * (1f / 16));
                    }
                }
                bool FoundDefendPosition = false;
                byte AttemptTime         = 40;
                while (!FoundDefendPosition)
                {
                    AttemptTime--;
                    if (AttemptTime == 0)
                    {
                        break;
                    }
                    Tile tile = Framing.GetTileSafely(DefendX, DefendY);
                    if (tile != null && tile.active() && Main.tileSolid[tile.type])
                    {
                        DefendY--;
                        continue;
                    }
                    if (Main.wallHouse[tile.wall])
                    {
                        DefendX += (MoveLeft ? -1 : 1);
                    }
                    else
                    {
                        tile = Framing.GetTileSafely(DefendX, DefendY + 1);
                        if (tile != null && (!tile.active() || !Main.tileSolid[tile.type]))
                        {
                            DefendY++;
                            continue;
                        }
                        bool Failed = false;
                        for (int x = -4; x < 5; x++)
                        {
                            for (int y = -1; y < 2; y++)
                            {
                                if (Failed)
                                {
                                    break;
                                }
                                if (x != 0 || y != 0)
                                {
                                    tile = Framing.GetTileSafely(DefendX + x, DefendY + y);
                                    if (tile != null && Main.wallHouse[tile.wall])
                                    {
                                        DefendX += (MoveLeft ? -1 : 1);
                                        Failed   = true;
                                    }
                                }
                            }
                        }
                        if (!Failed)
                        {
                            FoundDefendPosition = true;
                        }
                    }
                }
                if (AttemptTime == 0)
                {
                    DefendX = (int)(guardian.Position.X * (1f / 16));
                    DefendY = (int)(guardian.Position.Y * (1f / 16));
                }
                GuardPosition = new Vector2(DefendX, DefendY) * 16;
                ChangeStep();
            }
            break;

            case 1:
            {
                if (guardian.TargetID == -1 && guardian.TalkPlayerID == -1)
                {
                    guardian.MoveLeft = guardian.MoveRight = false;
                    guardian.WalkMode = false;
                    if (Math.Abs(guardian.Position.X - GuardPosition.X) > 20)
                    {
                        if (GuardPosition.X < guardian.Position.X)
                        {
                            guardian.MoveLeft = true;
                        }
                        else
                        {
                            guardian.MoveRight = true;
                        }
                    }
                    else
                    {
                    }
                }
            }
            break;
            }
        }