Example #1
0
        private static bool ProcessMessage(byte[] msgData, Farmer sourceFarmer)
        {
            var subMessageType = (NetworkUtility.MessageTypes)BitConverter.ToInt32(msgData, 0);

            Console.WriteLine($"Receiving special message, type = {subMessageType}. Message length = {msgData.Length}");

            switch (subMessageType)
            {
            case (NetworkUtility.MessageTypes.TAKE_DAMAGE):
                int damage = BitConverter.ToInt32(msgData, 4);

                long?damagerID = null;
                if (msgData.Length > 8)
                {
                    damagerID = BitConverter.ToInt64(msgData, 8);
                }

                Console.WriteLine($"Taking {damage} damage from another player");

                ModEntry.BRGame.TakeDamage(damage, damagerID);
                return(false);

            case (NetworkUtility.MessageTypes.SERVER_BROADCAST_GAME_START):
                if (!Game1.IsServer)
                {
                    int  numberOfPlayers     = BitConverter.ToInt32(msgData, 4);
                    int  stormIndex          = BitConverter.ToInt32(msgData, 8);
                    bool isHostParticipating = BitConverter.ToBoolean(msgData, 12);

                    ModEntry.BRGame.ClientStartGame(numberOfPlayers, isHostParticipating, stormIndex);
                }
                return(false);

            case NetworkUtility.MessageTypes.WARP:
                int tileX = BitConverter.ToInt32(msgData, 4);
                int tileY = BitConverter.ToInt32(msgData, 8);

                StringBuilder sb = new StringBuilder();
                for (int i = 12; i < msgData.Length; i++)
                {
                    sb.Append((char)msgData[i]);
                }

                string locationName = sb.ToString().Substring(1);

                Console.WriteLine($"I was told by the server to move to {locationName} @ ({tileX},{tileY})");

                //Face downwards & Warp
                Game1.player.FacingDirection = 2;
                Game1.player.warpFarmer(new TileLocation(locationName, tileX, tileY).CreateWarp());
                return(false);

            case (NetworkUtility.MessageTypes.SERVER_BROADCAST_GAME_END):
                if (!Game1.IsServer)
                {
                    ModEntry.BRGame.ClientEndGame();
                }
                return(false);

            case (NetworkUtility.MessageTypes.RELAY_MESSAGE_TO_ANOTHER_PLAYER):
                if (Game1.IsServer)
                {
                    long targetUniqueID = BitConverter.ToInt64(msgData, 4);

                    byte[] messageData = new byte[msgData.Length - 12];
                    Array.Copy(msgData, 12, messageData, 0, msgData.Length - 12);

                    if (targetUniqueID == Game1.player.UniqueMultiplayerID)
                    {
                        Console.WriteLine("Received relay instruction addressed to myself");
                        ProcessMessage(messageData, sourceFarmer);
                        return(false);
                    }
                    else
                    {
                        Console.WriteLine($"Relaying a message to {targetUniqueID}...");
                        Game1.server.sendMessage(targetUniqueID, new OutgoingMessage(NetworkUtility.uniqueMessageType, Game1.player, (object)messageData));
                        return(false);
                    }
                }
                else
                {
                    Console.WriteLine("Accidentally received relay instruction as a client");
                    return(false);
                }

            case (NetworkUtility.MessageTypes.TELL_SERVER_I_DIED):
                if (msgData.Length > 12)
                {
                    ModEntry.BRGame.HandleDeath(sourceFarmer, BitConverter.ToInt64(msgData, 12));
                }
                else
                {
                    ModEntry.BRGame.HandleDeath(sourceFarmer);
                }
                return(false);

            case NetworkUtility.MessageTypes.SERVER_BROADCAST_CHAT_MESSAGE:
                string message   = Encoding.UTF8.GetString(msgData.Skip(4).ToArray()).Substring(1);
                string colorName = "white";
                try
                {
                    colorName = message.Substring(1, message.Substring(1).IndexOf(']'));
                    Console.WriteLine($"color = {colorName}");
                }
                catch (Exception) { }

                var color = StardewValley.Menus.ChatMessage.getColorFromName(colorName);
                if (color == Microsoft.Xna.Framework.Color.White)
                {
                    color = Microsoft.Xna.Framework.Color.Gold;
                }

                Game1.chatBox.addMessage(message, color);
                return(false);

            case (NetworkUtility.MessageTypes.TELL_PLAYER_HIT_SHAKE_TIMER):
                int howLongMilliseconds = BitConverter.ToInt32(msgData, 4);
                HitShaker.SetHitShakeTimer(sourceFarmer.UniqueMultiplayerID, howLongMilliseconds);
                return(false);

            case NetworkUtility.MessageTypes.BROADCAST_ALIVE_COUNT:
                int howManyPlayersAlive = BitConverter.ToInt32(msgData, 4);

                if (ModEntry.BRGame.OverlayUI != null)
                {
                    ModEntry.BRGame.OverlayUI.AlivePlayers = howManyPlayersAlive;
                }
                return(false);

            case NetworkUtility.MessageTypes.SEND_PHASE_DATA:
                try
                {
                    var b = new BinaryFormatter();

                    var phases = (List <Phase>[])b.Deserialize(new MemoryStream(msgData.Skip(4).ToArray()));

                    Console.WriteLine($"Received phases data from server, info: Length={phases.Length}");

                    Storm.Phases = phases;
                }catch (Exception)
                {
                    Console.WriteLine("Unable to process phases data. Kicking to prevent glitches...");
                    long id = sourceFarmer.UniqueMultiplayerID;
                    NetworkUtility.SendChatMessageToPlayerWithoutMod(id, "Unable to process phases data. Kicking to prevent glitches...");
                    Game1.server.sendMessage(id, new OutgoingMessage((byte)19, id, new object[0]));
                    Game1.server.playerDisconnected(id);
                    Game1.otherFarmers.Remove(id);
                }
                return(false);

            case NetworkUtility.MessageTypes.KICK_PLAYER:
                StringBuilder sb2 = new StringBuilder();
                for (int i = 4; i < msgData.Length; i++)
                {
                    sb2.Append((char)msgData[i]);
                }

                string kickMsg = sb2.ToString().Substring(1);
                Console.WriteLine($"Kicked. Reason: \"{kickMsg}\"");

                Game1.client.disconnect();

                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    System.Threading.Thread.Sleep(300);
                    Game1.activeClickableMenu = new StardewValley.Menus.DialogueBox($"Kicked. Reason: \"{kickMsg}\"");
                });

                return(false);

            case NetworkUtility.MessageTypes.SEND_MY_VERSION_TO_SERVER:
                if (Game1.IsServer)
                {
                    int    major = BitConverter.ToInt32(msgData, 4);
                    int    minor = BitConverter.ToInt32(msgData, 8);
                    byte[] sha   = msgData.Skip(12).ToArray();

                    Console.WriteLine($"Received version from client {sourceFarmer.Name}/{sourceFarmer.UniqueMultiplayerID}: {major}.{minor}");
                    new AutoKicker().AcknowledgeClientVersion(sourceFarmer.UniqueMultiplayerID, major, minor, sha);

                    if (ModEntry.BRGame.IsGameInProgress)
                    {
                        System.Threading.Tasks.Task.Factory.StartNew(() =>
                        {
                            System.Threading.Thread.Sleep(1000);
                            Game1.server.sendMessage(sourceFarmer.UniqueMultiplayerID,
                                                     new OutgoingMessage(NetworkUtility.uniqueMessageType, Game1.player, (int)NetworkUtility.MessageTypes.TELL_NEW_PLAYER_TO_SPECTATE));
                        });
                    }
                }
                return(false);

            case NetworkUtility.MessageTypes.TELL_NEW_PLAYER_TO_SPECTATE:
                if (!Game1.IsServer)
                {
                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        System.Threading.Thread.Sleep(1000);

                        if (!ModEntry.BRGame.alivePlayers.Contains(Game1.player.UniqueMultiplayerID))
                        {
                            var oldLocation = Game1.player.currentLocation;
                            var oldPosition = new xTile.Dimensions.Location(
                                (int)Game1.player.Position.X - Game1.viewport.Width / 2,
                                (int)Game1.player.Position.Y - Game1.viewport.Height / 2);

                            SpectatorMode.EnterSpectatorMode(oldLocation, oldPosition);
                        }
                    });
                }
                return(false);

            case NetworkUtility.MessageTypes.GIVE_HAT:
                int hatID = 10;                        //Chicken hat
                Game1.player?.addItemToInventory(new StardewValley.Objects.Hat(hatID));
                return(false);

            default:
                return(true);
            }
        }
        public void _draw(Game1 tthis, GameTime gameTime, RenderTarget2D target_screen)
        {
            Game1.showingHealthBar = false;
            Color bgColor = Color.Black;
            {
                if (target_screen != null)
                {
                    tthis.GraphicsDevice.SetRenderTarget(target_screen);
                }
                {
                    tthis.GraphicsDevice.Clear(bgColor);

                    if (Game1.gameMode == (byte)11)
                    {
                        Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                        Game1.spriteBatch.DrawString(Game1.dialogueFont, Game1.content.LoadString("Strings\\StringsFromCSFiles:Game1.cs.3685"), new Vector2(16f, 16f), Microsoft.Xna.Framework.Color.HotPink);
                        Game1.spriteBatch.DrawString(Game1.dialogueFont, Game1.content.LoadString("Strings\\StringsFromCSFiles:Game1.cs.3686"), new Vector2(16f, 32f), new Microsoft.Xna.Framework.Color(0, (int)byte.MaxValue, 0));
                        Game1.spriteBatch.DrawString(Game1.dialogueFont, Game1.parseText(Game1.errorMessage, Game1.dialogueFont, Game1.graphics.GraphicsDevice.Viewport.Width), new Vector2(16f, 48f), Microsoft.Xna.Framework.Color.White);
                        Game1.spriteBatch.End();
                    }
                    else if (Game1.gameMode == (byte)6 || Game1.gameMode == (byte)3 && Game1.currentLocation == null)
                    {
                        Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                        string str1 = "";
                        for (int index = 0; (double)index < gameTime.TotalGameTime.TotalMilliseconds % 999.0 / 333.0; ++index)
                        {
                            str1 += ".";
                        }
                        string str2          = Game1.content.LoadString("Strings\\StringsFromCSFiles:Game1.cs.3688");
                        string s             = str2 + str1;
                        string str3          = str2 + "... ";
                        int    widthOfString = SpriteText.getWidthOfString(str3, 999999);
                        int    height        = 64;
                        int    x             = 64;
                        int    y             = Game1.graphics.GraphicsDevice.Viewport.GetTitleSafeArea().Bottom - height;
                        SpriteText.drawString(Game1.spriteBatch, s, x, y, 999999, widthOfString, height, 1f, 0.88f, false, 0, str3, -1, SpriteText.ScrollTextAlignment.Left);
                        Game1.spriteBatch.End();
                        if (target_screen != null)
                        {
                            tthis.GraphicsDevice.SetRenderTarget((RenderTarget2D)null);
                            tthis.GraphicsDevice.Clear(bgColor);
                            Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone);
                            Game1.spriteBatch.Draw((Texture2D)target_screen, Vector2.Zero, new Microsoft.Xna.Framework.Rectangle?(target_screen.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, Game1.options.zoomLevel, SpriteEffects.None, 1f);
                            Game1.spriteBatch.End();
                        }
                        if (Game1.overlayMenu != null)
                        {
                            Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                            Game1.overlayMenu.draw(Game1.spriteBatch);
                            Game1.spriteBatch.End();
                        }
                    }
                    else
                    {
                        Microsoft.Xna.Framework.Rectangle rectangle;
                        Viewport viewport;
                        if (Game1.gameMode == (byte)0)
                        {
                            Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                        }
                        else
                        {
                            if (Game1.drawLighting)
                            {
                                tthis.GraphicsDevice.SetRenderTarget(Game1.lightmap);
                                tthis.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White * 0.0f);
                                Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                                Microsoft.Xna.Framework.Color color = !Game1.currentLocation.Name.StartsWith("UndergroundMine") || !(Game1.currentLocation is MineShaft) ? (Game1.ambientLight.Equals(Microsoft.Xna.Framework.Color.White) || Game1.isRaining && (bool)(NetFieldBase <bool, NetBool>)Game1.currentLocation.isOutdoors ? Game1.outdoorLight : Game1.ambientLight) : (Game1.currentLocation as MineShaft).getLightingColor(gameTime);
                                Game1.spriteBatch.Draw(Game1.staminaRect, Game1.lightmap.Bounds, color);
                                foreach (LightSource currentLightSource in Game1.currentLightSources)
                                {
                                    if (!Game1.isRaining && !Game1.isDarkOut() || currentLightSource.lightContext.Value != LightSource.LightContext.WindowLight)
                                    {
                                        if (currentLightSource.PlayerID != 0L && currentLightSource.PlayerID != Game1.player.UniqueMultiplayerID)
                                        {
                                            Farmer farmerMaybeOffline = Game1.getFarmerMaybeOffline(currentLightSource.PlayerID);
                                            if (farmerMaybeOffline == null || farmerMaybeOffline.currentLocation != null && farmerMaybeOffline.currentLocation.Name != Game1.currentLocation.Name || (bool)(NetFieldBase <bool, NetBool>)farmerMaybeOffline.hidden)
                                            {
                                                continue;
                                            }
                                        }
                                        if (Utility.isOnScreen((Vector2)(NetFieldBase <Vector2, NetVector2>)currentLightSource.position, (int)((double)(float)(NetFieldBase <float, NetFloat>)currentLightSource.radius * 64.0 * 4.0)))
                                        {
                                            Game1.spriteBatch.Draw(currentLightSource.lightTexture, Game1.GlobalToLocal(Game1.viewport, (Vector2)(NetFieldBase <Vector2, NetVector2>)currentLightSource.position) / (float)(Game1.options.lightingQuality / 2), new Microsoft.Xna.Framework.Rectangle?(currentLightSource.lightTexture.Bounds), (Microsoft.Xna.Framework.Color)(NetFieldBase <Microsoft.Xna.Framework.Color, NetColor>) currentLightSource.color, 0.0f, new Vector2((float)currentLightSource.lightTexture.Bounds.Center.X, (float)currentLightSource.lightTexture.Bounds.Center.Y), (float)(NetFieldBase <float, NetFloat>)currentLightSource.radius / (float)(Game1.options.lightingQuality / 2), SpriteEffects.None, 0.9f);
                                        }
                                    }
                                }
                                Game1.spriteBatch.End();
                                tthis.GraphicsDevice.SetRenderTarget(target_screen);
                            }
                            if (Game1.bloomDay && Game1.bloom != null)
                            {
                                Game1.bloom.BeginDraw();
                            }
                            tthis.GraphicsDevice.Clear(bgColor);
                            Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                            if (Game1.background != null)
                            {
                                Game1.background.draw(Game1.spriteBatch);
                            }
                            Game1.mapDisplayDevice.BeginScene(Game1.spriteBatch);
                            Game1.currentLocation.Map.GetLayer("Back").Draw(Game1.mapDisplayDevice, Game1.viewport, xTile.Dimensions.Location.Origin, false, 4);
                            Game1.currentLocation.drawWater(Game1.spriteBatch);

                            if (!Game1.currentLocation.shouldHideCharacters())
                            {
                                if (Game1.CurrentEvent == null)
                                {
                                    foreach (NPC character in Game1.currentLocation.characters)
                                    {
                                        if (!(bool)(NetFieldBase <bool, NetBool>)character.swimming && !character.HideShadow && (!character.IsInvisible && !Game1.currentLocation.shouldShadowBeDrawnAboveBuildingsLayer(character.getTileLocation())))
                                        {
                                            Game1.spriteBatch.Draw(Game1.shadowTexture, Game1.GlobalToLocal(Game1.viewport, character.Position + new Vector2((float)(character.Sprite.SpriteWidth * 4) / 2f, (float)(character.GetBoundingBox().Height + (character.IsMonster ? 0 : 12)))), new Microsoft.Xna.Framework.Rectangle?(Game1.shadowTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, new Vector2((float)Game1.shadowTexture.Bounds.Center.X, (float)Game1.shadowTexture.Bounds.Center.Y), (float)(4.0 + (double)character.yJumpOffset / 40.0) * (float)(NetFieldBase <float, NetFloat>)character.scale, SpriteEffects.None, Math.Max(0.0f, (float)character.getStandingY() / 10000f) - 1E-06f);
                                        }
                                    }
                                }
                                else
                                {
                                    foreach (NPC actor in Game1.CurrentEvent.actors)
                                    {
                                        if (!(bool)(NetFieldBase <bool, NetBool>)actor.swimming && !actor.HideShadow && !Game1.currentLocation.shouldShadowBeDrawnAboveBuildingsLayer(actor.getTileLocation()))
                                        {
                                            Game1.spriteBatch.Draw(Game1.shadowTexture, Game1.GlobalToLocal(Game1.viewport, actor.Position + new Vector2((float)(actor.Sprite.SpriteWidth * 4) / 2f, (float)(actor.GetBoundingBox().Height + (actor.IsMonster ? 0 : (actor.Sprite.SpriteHeight <= 16 ? -4 : 12))))), new Microsoft.Xna.Framework.Rectangle?(Game1.shadowTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, new Vector2((float)Game1.shadowTexture.Bounds.Center.X, (float)Game1.shadowTexture.Bounds.Center.Y), (float)(4.0 + (double)actor.yJumpOffset / 40.0) * (float)(NetFieldBase <float, NetFloat>)actor.scale, SpriteEffects.None, Math.Max(0.0f, (float)actor.getStandingY() / 10000f) - 1E-06f);
                                        }
                                    }
                                }
                            }
                            xTile.Layers.Layer layer1 = Game1.currentLocation.Map.GetLayer("Buildings");
                            layer1.Draw(Game1.mapDisplayDevice, Game1.viewport, xTile.Dimensions.Location.Origin, false, 4);
                            Game1.mapDisplayDevice.EndScene();
                            Game1.spriteBatch.End();
                            Game1.spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                            if (!Game1.currentLocation.shouldHideCharacters())
                            {
                                if (Game1.CurrentEvent == null)
                                {
                                    foreach (NPC character in Game1.currentLocation.characters)
                                    {
                                        if (!(bool)(NetFieldBase <bool, NetBool>)character.swimming && !character.HideShadow && (!(bool)(NetFieldBase <bool, NetBool>)character.isInvisible && Game1.currentLocation.shouldShadowBeDrawnAboveBuildingsLayer(character.getTileLocation())))
                                        {
                                            Game1.spriteBatch.Draw(Game1.shadowTexture, Game1.GlobalToLocal(Game1.viewport, character.Position + new Vector2((float)(character.Sprite.SpriteWidth * 4) / 2f, (float)(character.GetBoundingBox().Height + (character.IsMonster ? 0 : 12)))), new Microsoft.Xna.Framework.Rectangle?(Game1.shadowTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, new Vector2((float)Game1.shadowTexture.Bounds.Center.X, (float)Game1.shadowTexture.Bounds.Center.Y), (float)(4.0 + (double)character.yJumpOffset / 40.0) * (float)(NetFieldBase <float, NetFloat>)character.scale, SpriteEffects.None, Math.Max(0.0f, (float)character.getStandingY() / 10000f) - 1E-06f);
                                        }
                                    }
                                }
                                else
                                {
                                    foreach (NPC actor in Game1.CurrentEvent.actors)
                                    {
                                        if (!(bool)(NetFieldBase <bool, NetBool>)actor.swimming && !actor.HideShadow && Game1.currentLocation.shouldShadowBeDrawnAboveBuildingsLayer(actor.getTileLocation()))
                                        {
                                            Game1.spriteBatch.Draw(Game1.shadowTexture, Game1.GlobalToLocal(Game1.viewport, actor.Position + new Vector2((float)(actor.Sprite.SpriteWidth * 4) / 2f, (float)(actor.GetBoundingBox().Height + (actor.IsMonster ? 0 : 12)))), new Microsoft.Xna.Framework.Rectangle?(Game1.shadowTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, new Vector2((float)Game1.shadowTexture.Bounds.Center.X, (float)Game1.shadowTexture.Bounds.Center.Y), (float)(4.0 + (double)actor.yJumpOffset / 40.0) * (float)(NetFieldBase <float, NetFloat>)actor.scale, SpriteEffects.None, Math.Max(0.0f, (float)actor.getStandingY() / 10000f) - 1E-06f);
                                        }
                                    }
                                }
                            }
                            if ((Game1.eventUp || Game1.killScreen) && (!Game1.killScreen && Game1.currentLocation.currentEvent != null))
                            {
                                Game1.currentLocation.currentEvent.draw(Game1.spriteBatch);
                            }
                            if (Game1.player.currentUpgrade != null && Game1.player.currentUpgrade.daysLeftTillUpgradeDone <= 3 && Game1.currentLocation.Name.Equals("Farm"))
                            {
                                Game1.spriteBatch.Draw(Game1.player.currentUpgrade.workerTexture, Game1.GlobalToLocal(Game1.viewport, Game1.player.currentUpgrade.positionOfCarpenter), new Microsoft.Xna.Framework.Rectangle?(Game1.player.currentUpgrade.getSourceRectangle()), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, (float)(((double)Game1.player.currentUpgrade.positionOfCarpenter.Y + 48.0) / 10000.0));
                            }
                            Game1.currentLocation.draw(Game1.spriteBatch);
                            foreach (Vector2 key in Game1.crabPotOverlayTiles.Keys)
                            {
                                xTile.Tiles.Tile tile = layer1.Tiles[(int)key.X, (int)key.Y];
                                if (tile != null)
                                {
                                    Vector2 local = Game1.GlobalToLocal(Game1.viewport, key * 64f);
                                    xTile.Dimensions.Location location = new xTile.Dimensions.Location((int)local.X, (int)local.Y);
                                    Game1.mapDisplayDevice.DrawTile(tile, location, (float)(((double)key.Y * 64.0 - 1.0) / 10000.0));
                                }
                            }
                            if (Game1.eventUp && Game1.currentLocation.currentEvent != null)
                            {
                                string messageToScreen = Game1.currentLocation.currentEvent.messageToScreen;
                            }

                            if (Game1.currentLocation.Name.Equals("Farm"))
                            {
                                if (Game1.player.CoopUpgradeLevel > 0)
                                {
                                    Game1.spriteBatch.Draw(Game1.currentCoopTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2(1280f, 320f)), new Microsoft.Xna.Framework.Rectangle?(Game1.currentCoopTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, Math.Max(0.0f, 0.0576f));
                                }
                                switch (Game1.player.BarnUpgradeLevel)
                                {
                                case 1:
                                    Game1.spriteBatch.Draw(Game1.currentBarnTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2(768f, 320f)), new Microsoft.Xna.Framework.Rectangle?(Game1.currentBarnTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, Math.Max(0.0f, 0.0576f));
                                    break;

                                case 2:
                                    Game1.spriteBatch.Draw(Game1.currentBarnTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2(640f, 256f)), new Microsoft.Xna.Framework.Rectangle?(Game1.currentBarnTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, Math.Max(0.0f, 0.0576f));
                                    break;
                                }
                                if (Game1.player.hasGreenhouse)
                                {
                                    Game1.spriteBatch.Draw(Game1.greenhouseTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2(64f, 320f)), new Microsoft.Xna.Framework.Rectangle?(Game1.greenhouseTexture.Bounds), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, 1f, SpriteEffects.None, Math.Max(0.0f, 0.0576f));
                                }
                            }

                            Game1.mapDisplayDevice.BeginScene(Game1.spriteBatch);
                            Game1.currentLocation.Map.GetLayer("Front").Draw(Game1.mapDisplayDevice, Game1.viewport, xTile.Dimensions.Location.Origin, false, 4);
                            Game1.mapDisplayDevice.EndScene();
                            Game1.currentLocation.drawAboveFrontLayer(Game1.spriteBatch);
                            Game1.spriteBatch.End();
                            Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                            if (Game1.displayFarmer && Game1.player.ActiveObject != null && ((bool)(NetFieldBase <bool, NetBool>)Game1.player.ActiveObject.bigCraftable && tthis.checkBigCraftableBoundariesForFrontLayer()) && Game1.currentLocation.Map.GetLayer("Front").PickTile(new xTile.Dimensions.Location(Game1.player.getStandingX(), Game1.player.getStandingY()), Game1.viewport.Size) == null)
                            {
                                Game1.drawPlayerHeldObject(Game1.player);
                            }
                            else if (Game1.displayFarmer && Game1.player.ActiveObject != null)
                            {
                                if (Game1.currentLocation.Map.GetLayer("Front").PickTile(new xTile.Dimensions.Location((int)Game1.player.Position.X, (int)Game1.player.Position.Y - 38), Game1.viewport.Size) == null || Game1.currentLocation.Map.GetLayer("Front").PickTile(new xTile.Dimensions.Location((int)Game1.player.Position.X, (int)Game1.player.Position.Y - 38), Game1.viewport.Size).TileIndexProperties.ContainsKey("FrontAlways"))
                                {
                                    xTile.Layers.Layer layer2 = Game1.currentLocation.Map.GetLayer("Front");
                                    rectangle = Game1.player.GetBoundingBox();
                                    xTile.Dimensions.Location mapDisplayLocation1 = new xTile.Dimensions.Location(rectangle.Right, (int)Game1.player.Position.Y - 38);
                                    xTile.Dimensions.Size     size1 = Game1.viewport.Size;
                                    if (layer2.PickTile(mapDisplayLocation1, size1) != null)
                                    {
                                        xTile.Layers.Layer layer3 = Game1.currentLocation.Map.GetLayer("Front");
                                        rectangle = Game1.player.GetBoundingBox();
                                        xTile.Dimensions.Location mapDisplayLocation2 = new xTile.Dimensions.Location(rectangle.Right, (int)Game1.player.Position.Y - 38);
                                        xTile.Dimensions.Size     size2 = Game1.viewport.Size;
                                        if (layer3.PickTile(mapDisplayLocation2, size2).TileIndexProperties.ContainsKey("FrontAlways"))
                                        {
                                            goto label_139;
                                        }
                                    }
                                    else
                                    {
                                        goto label_139;
                                    }
                                }
                                Game1.drawPlayerHeldObject(Game1.player);
                            }
label_139:
                            if ((Game1.player.UsingTool || Game1.pickingTool) && Game1.player.CurrentTool != null && ((!Game1.player.CurrentTool.Name.Equals("Seeds") || Game1.pickingTool) && (Game1.currentLocation.Map.GetLayer("Front").PickTile(new xTile.Dimensions.Location(Game1.player.getStandingX(), (int)Game1.player.Position.Y - 38), Game1.viewport.Size) != null && Game1.currentLocation.Map.GetLayer("Front").PickTile(new xTile.Dimensions.Location(Game1.player.getStandingX(), Game1.player.getStandingY()), Game1.viewport.Size) == null)))
                            {
                                Game1.drawTool(Game1.player);
                            }
                            if (Game1.currentLocation.Map.GetLayer("AlwaysFront") != null)
                            {
                                Game1.mapDisplayDevice.BeginScene(Game1.spriteBatch);
                                Game1.currentLocation.Map.GetLayer("AlwaysFront").Draw(Game1.mapDisplayDevice, Game1.viewport, xTile.Dimensions.Location.Origin, false, 4);
                                Game1.mapDisplayDevice.EndScene();
                            }
                            if ((double)Game1.toolHold > 400.0 && Game1.player.CurrentTool.UpgradeLevel >= 1 && Game1.player.canReleaseTool)
                            {
                                Microsoft.Xna.Framework.Color color = Microsoft.Xna.Framework.Color.White;
                                switch ((int)((double)Game1.toolHold / 600.0) + 2)
                                {
                                case 1:
                                    color = Tool.copperColor;
                                    break;

                                case 2:
                                    color = Tool.steelColor;
                                    break;

                                case 3:
                                    color = Tool.goldColor;
                                    break;

                                case 4:
                                    color = Tool.iridiumColor;
                                    break;
                                }
                                Game1.spriteBatch.Draw(Game1.littleEffect, new Microsoft.Xna.Framework.Rectangle((int)Game1.player.getLocalPosition(Game1.viewport).X - 2, (int)Game1.player.getLocalPosition(Game1.viewport).Y - (Game1.player.CurrentTool.Name.Equals("Watering Can") ? 0 : 64) - 2, (int)((double)Game1.toolHold % 600.0 * 0.0799999982118607) + 4, 12), Microsoft.Xna.Framework.Color.Black);
                                Game1.spriteBatch.Draw(Game1.littleEffect, new Microsoft.Xna.Framework.Rectangle((int)Game1.player.getLocalPosition(Game1.viewport).X, (int)Game1.player.getLocalPosition(Game1.viewport).Y - (Game1.player.CurrentTool.Name.Equals("Watering Can") ? 0 : 64), (int)((double)Game1.toolHold % 600.0 * 0.0799999982118607), 8), color);
                            }

                            if ((double)Game1.currentLocation.LightLevel > 0.0 && Game1.timeOfDay < 2000)
                            {
                                Game1.spriteBatch.Draw(Game1.fadeToBlackRect, Game1.graphics.GraphicsDevice.Viewport.Bounds, Microsoft.Xna.Framework.Color.Black * Game1.currentLocation.LightLevel);
                            }
                            if (Game1.screenGlow)
                            {
                                Game1.spriteBatch.Draw(Game1.fadeToBlackRect, Game1.graphics.GraphicsDevice.Viewport.Bounds, Game1.screenGlowColor * Game1.screenGlowAlpha);
                            }
                            Game1.currentLocation.drawAboveAlwaysFrontLayer(Game1.spriteBatch);

                            Game1.spriteBatch.End();
                            Game1.spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                            if (Game1.eventUp && Game1.currentLocation.currentEvent != null)
                            {
                                foreach (NPC actor in Game1.currentLocation.currentEvent.actors)
                                {
                                    if (actor.isEmoting)
                                    {
                                        Vector2 localPosition = actor.getLocalPosition(Game1.viewport);
                                        localPosition.Y -= 140f;
                                        if (actor.Age == 2)
                                        {
                                            localPosition.Y += 32f;
                                        }
                                        else if (actor.Gender == 1)
                                        {
                                            localPosition.Y += 10f;
                                        }
                                        Game1.spriteBatch.Draw(Game1.emoteSpriteSheet, localPosition, new Microsoft.Xna.Framework.Rectangle?(new Microsoft.Xna.Framework.Rectangle(actor.CurrentEmoteIndex * 16 % Game1.emoteSpriteSheet.Width, actor.CurrentEmoteIndex * 16 / Game1.emoteSpriteSheet.Width * 16, 16, 16)), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, 4f, SpriteEffects.None, (float)actor.getStandingY() / 10000f);
                                    }
                                }
                            }
                            Game1.spriteBatch.End();

                            Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, (DepthStencilState)null, (RasterizerState)null);
                            if (Game1.drawGrid)
                            {
                                int   num1 = -Game1.viewport.X % 64;
                                float num2 = (float)(-Game1.viewport.Y % 64);
                                int   num3 = num1;
                                while (true)
                                {
                                    int num4 = num3;
                                    viewport = Game1.graphics.GraphicsDevice.Viewport;
                                    int width = viewport.Width;
                                    if (num4 < width)
                                    {
                                        SpriteBatch spriteBatch = Game1.spriteBatch;
                                        Texture2D   staminaRect = Game1.staminaRect;
                                        int         x           = num3;
                                        int         y           = (int)num2;
                                        viewport = Game1.graphics.GraphicsDevice.Viewport;
                                        int height = viewport.Height;
                                        Microsoft.Xna.Framework.Rectangle destinationRectangle = new Microsoft.Xna.Framework.Rectangle(x, y, 1, height);
                                        Microsoft.Xna.Framework.Color     color = Microsoft.Xna.Framework.Color.Red * 0.5f;
                                        spriteBatch.Draw(staminaRect, destinationRectangle, color);
                                        num3 += 64;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                float num5 = num2;
                                while (true)
                                {
                                    double num4 = (double)num5;
                                    viewport = Game1.graphics.GraphicsDevice.Viewport;
                                    double height = (double)viewport.Height;
                                    if (num4 < height)
                                    {
                                        SpriteBatch spriteBatch = Game1.spriteBatch;
                                        Texture2D   staminaRect = Game1.staminaRect;
                                        int         x           = num1;
                                        int         y           = (int)num5;
                                        viewport = Game1.graphics.GraphicsDevice.Viewport;
                                        int width = viewport.Width;
                                        Microsoft.Xna.Framework.Rectangle destinationRectangle = new Microsoft.Xna.Framework.Rectangle(x, y, width, 1);
                                        Microsoft.Xna.Framework.Color     color = Microsoft.Xna.Framework.Color.Red * 0.5f;
                                        spriteBatch.Draw(staminaRect, destinationRectangle, color);
                                        num5 += 64f;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        Game1.spriteBatch.End();
                        tthis.CallAction("renderScreenBuffer", target_screen);
                    }
                }
            }
        }
        /*********
        ** Private methods
        *********/
        /// <summary>The method to call after <see cref="Farmer.moveRaft"/>.</summary>
        public static void After_MoveRaft(Farmer __instance, GameLocation currentLocation, GameTime time)
        {
            __instance.position.X += __instance.xVelocity;
            __instance.position.Y += __instance.yVelocity;
            Rectangle r = new Rectangle((int)(__instance.Position.X), (int)(__instance.Position.Y + 16), 64, 64);

            // 2 - down, 0 - up, 3 - left, 1 - right
            //Log.trace( "meow: " + ( __instance.movementDirections.Count > 0 ?__instance.movementDirections[0]:-1));
            if (__instance.movementDirections.Contains(2) || __instance.movementDirections.Contains(0))
            {
                Vector2 pos  = new Vector2(r.X + r.Width / 2, r.Y + r.Height);
                var     xPos = new xTile.Dimensions.Location((int)pos.X, (int)pos.Y);
                xTile.ObjectModel.PropertyValue propVal = null;
                currentLocation.map.GetLayer("Back").PickTile(xPos, Game1.viewport.Size)?.TileIndexProperties.TryGetValue("Water", out propVal);
                if (propVal == null)
                {
                    if (currentLocation.isTileLocationOpen(xPos))
                    {
                        Game1.player.isRafting = false;
                        Game1.player.Position  = pos;
                        //Game1.player.position.Y -= 64 + 16;
                        Game1.player.setTrajectory(0, 0);
                    }
                    return;
                }

                pos     = new Vector2(r.X + r.Width / 2, r.Y);
                xPos    = new xTile.Dimensions.Location((int)pos.X, (int)pos.Y);
                propVal = null;
                currentLocation.map.GetLayer("Back").PickTile(xPos, Game1.viewport.Size)?.TileIndexProperties.TryGetValue("Water", out propVal);
                if (propVal == null)
                {
                    if (currentLocation.isTileLocationOpen(xPos))
                    {
                        Game1.player.isRafting   = false;
                        Game1.player.Position    = pos;
                        Game1.player.position.Y -= 64 + 16;
                        Game1.player.setTrajectory(0, 0);
                    }
                    return;
                }
            }

            if (__instance.movementDirections.Contains(3))
            {
                Vector2 pos  = new Vector2(r.X, r.Y + r.Height / 2);
                var     xPos = new xTile.Dimensions.Location((int)pos.X, (int)pos.Y);
                xTile.ObjectModel.PropertyValue propVal = null;
                currentLocation.map.GetLayer("Back").PickTile(xPos, Game1.viewport.Size)?.TileIndexProperties.TryGetValue("Water", out propVal);
                if (propVal == null)
                {
                    if (currentLocation.isTileLocationOpen(xPos))
                    {
                        Game1.player.isRafting   = false;
                        Game1.player.Position    = pos;
                        Game1.player.position.Y -= 64 + 16;
                        Game1.player.setTrajectory(0, 0);
                    }
                    return;
                }

                pos     = new Vector2(r.X + r.Width, r.Y + r.Height / 2);
                xPos    = new xTile.Dimensions.Location((int)pos.X, (int)pos.Y);
                propVal = null;
                currentLocation.map.GetLayer("Back").PickTile(xPos, Game1.viewport.Size)?.TileIndexProperties.TryGetValue("Water", out propVal);
                if (propVal == null)
                {
                    if (currentLocation.isTileLocationOpen(xPos))
                    {
                        Game1.player.isRafting   = false;
                        Game1.player.Position    = pos;
                        Game1.player.position.Y -= 64 + 16;
                        Game1.player.setTrajectory(0, 0);
                    }
                    return;
                }
            }
        }