Example #1
0
 void CastSpell(Spell spell)
 {
     if (CurSpell.Execute())
     {
         vec2 Pos = ScreenToGridCoord(Input.CurMousePos);
         Networking.ToServer(new MessageCastSpell(Spells.SpellList.IndexOf(spell), Pos));
     }
 }
Example #2
0
        public void OnChatEnter(string message)
        {
            if (message != null && message.Length > 0)
            {
                Console.WriteLine("ui chat message: " + message);
                Networking.ToServer(new MessageChat(ChatGlobal, Game.PlayerName(), message));
            }

            ToggleChatViaFlag(Toggle.Off);
        }
Example #3
0
        public static void LeaveGameNetwork()
        {
            if (Program.Server)
            {
                Networking.ToClients(new Message(MessageType.ServerLeft));
            }
            else
            {
                Networking.ToServer(new Message(MessageType.LeaveGame));
            }

            Networking.FinalSend();
        }
Example #4
0
        void AttackMove()
        {
            DataGroup.SelectedUnitsBounds();

            if (DataGroup.SelectedUnits == 0)
            {
                return;
            }

            vec2 Pos = ScreenToGridCoord(Input.CurMousePos);

            if (MouseOverMinimap)
            {
                Pos = MinimapGridPos();
            }

            vec2 Selected_BL   = DataGroup.SelectedBound_BL;
            vec2 Selected_Size = DataGroup.SelectedBound_TR - DataGroup.SelectedBound_BL;

            if (Selected_Size.x < 1)
            {
                Selected_Size.x = 1;
            }
            if (Selected_Size.y < 1)
            {
                Selected_Size.y = 1;
            }

            float SquareWidth = (float)Math.Sqrt(DataGroup.SelectedUnits);

            if (SquareWidth < 2)
            {
                SquareWidth = 0;
            }
            Pos = floor(Pos);

            vec2 Destination_Size = new vec2(SquareWidth, SquareWidth) * .8f;
            vec2 Destination_BL   = Pos - Destination_Size / 2;

            Destination_Size = floor(Destination_Size);
            Destination_BL   = floor(Destination_BL);
            Destination_BL   = max(Destination_BL, vec2.Zero);

            Networking.ToServer(new MessageAttackMove(Pos, Selected_BL, Selected_Size, Destination_BL, Destination_Size, CurSelectionFilter));

            AddAttackMarker();
        }
Example #5
0
        void Update_BuildingPlacing()
        {
            vec2 GridCoord = ScreenToGridCoord(Input.CurMousePos) - new vec2(1, 1);

            try
            {
                CanPlaceItem = CheckBuildingAvailability(GridCoord, MyPlayerNumber, MyTeamNumber, BuildingUserIsPlacing, CanPlace);
            }
            catch
            {
                CanPlaceItem = false;
            }

            if (LeftMousePressed && MyPlayerInfo != null)
            {
                if (!MyPlayerInfo.DragonLordAlive && !MapEditorActive)
                {
                    Message_NoDragonLordConstruction();
                }
                else if (!CanPlaceItem)
                {
                    Message_CanNotPlaceHere();
                }
                else if (!MapEditorActive && !MyPlayerInfo.CanAffordBuilding(BuildingUserIsPlacing))
                {
                    Message_InsufficientGold();
                }
                else
                {
                    try
                    {
                        Networking.ToServer(new MessagePlaceBuilding(GridCoord, Int(BuildingUserIsPlacing)));

                        if (!MapEditorActive)
                        {
                            EndPlaceMode();
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
Example #6
0
        private void HashCheck(bool Send = false)
        {
            string hash_string = "";
            int    hash        = 0;

            //string curdata_hash = DataGroup.DoHash(DataGroup.CurrentData, DataHash.Apply);
            //string prevdata_hash = DataGroup.DoHash(DataGroup.PreviousData, DataHash.Apply);
            //string curunit_hash = DataGroup.DoHash(DataGroup.CurrentUnits);
            //string prevunit_hash = DataGroup.DoHash(DataGroup.PreviousUnits);
            //string target_hash = DataGroup.DoHash(DataGroup.TargetData);
            //string extra_hash = DataGroup.DoHash(DataGroup.Extra);
            //hash_string = string.Format("Hash = {0} {1} {2} {3} {4} {5}", curdata_hash, prevdata_hash, curunit_hash, prevunit_hash, target_hash, extra_hash, target_hash, extra_hash);
            //hash_string = curdata_hash;
            //hash_string = curunit_hash;
            //for (int i = 1; i <= 4; i++) hash_string += PlayerInfo[i].ToString();
            for (int i = 1; i <= 4; i++)
            {
                hash_string += PlayerInfo[i].Units.ToString() + '|';
            }

            hash = hash_string.GetHashCode();

            if (Log.Hash)
            {
                Console.WriteLine("Hash = {0}", hash_string);
                Console.WriteLine("Hash code = {0}", hash);
            }

            if (Send)
            {
                //Hashes[SimStep] = hash;
                //Networking.ToServer(new MessageHash(SimStep, hash));

                StringHashes[SimStep] = hash_string;
                Networking.ToServer(new MessageStringHash(SimStep, hash_string));
            }
        }
Example #7
0
 public void RequestUnpause()
 {
     Networking.ToServer(new Message(MessageType.RequestUnpause));
 }
Example #8
0
        public void Draw()
        {
            ProcessInbox();

            DrawCount++;
            Render.StandardRenderSetup();

            double PreviousSecondsSinceLastUpdate = SecondsSinceLastUpdate;

            if (!DesyncPause)
            {
                CheckIfShouldPause();
                CheckIfShouldShowWaiting();
            }

            if (GameClass.GameActive && !ServerPaused && !DesyncPause)
            {
                if (NotPaused_SimulationUpdate)
                {
                    double Elapsed = GameClass.DeltaT; //GameClass.ElapsedSeconds;

                    if (SimStep + SecondsSinceLastUpdate / DelayBetweenUpdates < ServerSimStep - .25f)
                    {
                        Elapsed *= 1.15f;
                        if (Log.SpeedMods)
                        {
                            Console.WriteLine("            -- Speed up please, Elasped = {3}  # {0}/{1} :{2}", Elapsed, SimStep, ServerSimStep, SecondsSinceLastUpdate / DelayBetweenUpdates);
                        }
                    }

                    SecondsSinceLastUpdate += Elapsed;
                    T += (float)Elapsed;
                }
                else
                {
                    DataGroup.PausedSimulationUpdate();

                    if (MapEditorActive)
                    {
                        SecondsSinceLastUpdate += DelayBetweenUpdates;
                        T += (float)DelayBetweenUpdates;
                    }
                }

                if (GameClass.HasFocus)
                {
                    switch (CurUserMode)
                    {
                    case UserMode.PlaceBuilding:
                        if (UnselectAll)
                        {
                            SelectionUpdate(SelectSize);
                            UnselectAll = false;
                        }

                        Update_BuildingPlacing();
                        break;

                    case UserMode.Painting:
                        if (UnselectAll || MapEditorActive)
                        {
                            SelectionUpdate(SelectSize);
                            UnselectAll = false;
                        }

                        Update_Painting();
                        break;

                    case UserMode.Select:
                        SelectionUpdate(SelectSize, LineSelect: LineSelect);
                        break;

                    case UserMode.CastSpell:
                        if (LeftMousePressed && MyPlayerInfo != null)
                        {
                            if (!MyPlayerInfo.DragonLordAlive)
                            {
                                Message_NoDragonLordMagic();
                            }
                            else if (MyPlayerInfo.CanAffordSpell(CurSpell))
                            {
                                CastSpell(CurSpell);
                            }
                            else
                            {
                                Message_InsufficientJade();
                            }
                        }

                        break;
                    }
                }

                SkipNextSelectionUpdate = false;

                if (Program.Server)
                {
                    if (SecondsSinceLastUpdate / DelayBetweenUpdates > .75f && SimStep == ServerSimStep && !SentBookend)
                    {
                        if (Log.UpdateSim)
                        {
                            Console.WriteLine("Ready for bookend. {0}/{1} : {2}", SimStep, ServerSimStep, SecondsSinceLastUpdate / DelayBetweenUpdates);
                        }

                        SentBookend = true;

                        AckSimStep = ServerSimStep + 2;
                        Networking.ToClients(new MessageBookend(ServerSimStep + 1));
                    }
                }

                // Check if post-upate calculation still need to be done
                if (!PostUpdateFinished)
                {
                    PostSimulationUpdate();
                }

                // Check if we need to do a simulation update
                //if (true)
                //Console.WriteLine(ServerSimStep);
                if (GameClass.UnlimitedSpeed || SecondsSinceLastUpdate > DelayBetweenUpdates || SimStep + 2 < ServerSimStep)
                {
                    if (SimStep < ServerSimStep && !(Program.Server && MinClientSimStep + 2 < ServerSimStep))
                    {
                        WaitingTime = 0;

                        if (!PostUpdateFinished)
                        {
                            PostSimulationUpdate(); // If we are behind do another post-sim update to help catchup.
                        }
                        else
                        {
                            SecondsSinceLastUpdate -= DelayBetweenUpdates;
                            if (SecondsSinceLastUpdate < 0)
                            {
                                SecondsSinceLastUpdate = 0;
                            }

                            HashCheck();

                            DeququeActions(SimStep + 1);

                            HashCheck();

                            SimulationUpdate();

                            if (!Program.Spectate || Program.Spectate && SimStep % 15 == 0)
                            {
                                HashCheck(Send: true);
                            }

                            SentBookend = false;
                            Networking.ToServer(new MessageStartingStep(SimStep));

                            if (Log.UpdateSim)
                            {
                                Console.WriteLine("Just updated sim # {0}/{1} : {2}      min={3}", SimStep, ServerSimStep, SecondsSinceLastUpdate / DelayBetweenUpdates, MinClientSimStep);
                            }
                        }
                    }
                    else
                    {
                        WaitingTime += GameClass.ElapsedSeconds;

                        if (Log.Delays)
                        {
                            Console.WriteLine("-Reverting from # {0}/{1} : {2}", SimStep, ServerSimStep, SecondsSinceLastUpdate / DelayBetweenUpdates);
                        }
                        SecondsSinceLastUpdate = DelayBetweenUpdates;
                        T -= (float)GameClass.ElapsedSeconds;
                        if (Log.Delays)
                        {
                            Console.WriteLine("-Reverting to # {0}/{1} : {2}", SimStep, ServerSimStep, SecondsSinceLastUpdate / DelayBetweenUpdates);
                        }
                    }
                }
                else
                {
                    if (Program.Server)
                    {
                        if (Log.Draws)
                        {
                            Console.WriteLine("Draw step {0},  {1}", DrawCount, SecondsSinceLastUpdate / DelayBetweenUpdates);
                        }
                    }
                }
            }

            BenchmarkTests.Run(DataGroup.CurrentData, DataGroup.PreviousData);

            if (!Program.Headless)
            {
                try
                {
                    DrawWorld();
                }
                catch (Exception e)
                {
                }
            }
            else
            {
                GridHelper.GraphicsDevice.SetRenderTarget(null);
            }
        }
Example #9
0
        public void SelectionUpdate(vec2 Size, bool EffectSelection = true, bool LineSelect = true)
        {
            if (!GameClass.HasFocus)
            {
                return;
            }
            if (SkipNextSelectionUpdate)
            {
                return;
            }

            vec2 WorldCoord     = ScreenToWorldCoord(Input.CurMousePos);
            vec2 WorldCoordPrev = ScreenToWorldCoord(Input.PrevMousePos);

            bool Deselect =
                (LineSelect && LeftMouseDown || !LineSelect && Input.LeftMouseReleased) &&
                !Keys.LeftShift.Down() && !Keys.RightShift.Down() ||
                CurUserMode != UserMode.Select ||
                Keys.Back.Down() || Keys.Escape.Down();
            bool Selecting =
                (LineSelect && LeftMouseDown || !LineSelect && Input.LeftMouseReleased) &&
                (CurUserMode == UserMode.Select || CurUserMode == UserMode.CastSpell || CurUserMode == UserMode.Painting);

            if (SkipDeselect)
            {
                Deselect     = false;
                SkipDeselect = false;
            }

            if (SkipSelect)
            {
                Selecting = false;
                if (CurUserMode != UserMode.Select || !LeftMouseDown)
                {
                    SkipSelect = false;
                }
            }

            if (LineSelect)
            {
                bool DoSelect = false;
                if (MapEditorActive && CurUserMode == UserMode.Painting)
                {
                    // Continuous selection
                    DoSelect = LeftMouseDown && Selecting && EffectSelection;
                }
                else
                {
                    // Discrete selection
                    DoSelect = LeftMousePressed && Selecting && EffectSelection;
                }

                if (DoSelect)
                {
                    Networking.ToServer(new MessageSelectAlongLine(Size, Deselect, WorldCoord, WorldCoordPrev));
                }
            }
            else
            {
                if (Selecting && EffectSelection && BoxSelecting)
                {
                    BoxSelecting = false;
                    vec2 bl = floor(min(BoxSelectGridStart, BoxSelectGridEnd) - vec(1f, 1f));
                    vec2 tr = ceiling(max(BoxSelectGridStart, BoxSelectGridEnd));

                    Networking.ToServer(new MessageSelectInBox(Deselect, bl, tr));
                }
            }

            if (MapEditorActive && !GameClass.Game.ShowChat)
            {
                if (Keys.D5.Pressed())
                {
                    FinalizeGeodesics();
                }

                if (Keys.OemOpenBrackets.Pressed())
                {
                    MakeMapSymmetric(SymmetryType.Octo, true);
                }

                if (Keys.O.Pressed())
                {
                    MakeMapSymmetric(SymmetryType.Quad, false);
                }

                if (Keys.Delete.Down() || Keys.Back.Down())
                {
                    DeleteUnits();
                }
            }

            if (CurUserMode != UserMode.Select)
            {
                return;
            }

            if (NotPaused_UnitOrders)
            {
                if (Input.RightMousePressed)
                {
                    AttackMove();
                }

                if (Deselect)
                {
                    CurSelectionFilter = SelectionFilter.All;
                }

                if (Keys.Tab.Pressed())
                {
                    // Cycle through and find the next selection filter that results in something different than the current filter.
                    // Skip filters that result in nothing selected.
                    // Keep the same filter if no other valid filter can be found.
                    float  PrevSelectionFilter     = CurSelectionFilter;
                    bool[] PreviousFilteredSummary = FilteredSummary(PrevSelectionFilter);

                    do
                    {
                        ChangeSelectionFilter();
                    }while ((PreviousFilteredSummary.SequenceEqual(FilteredSummary(CurSelectionFilter)) || !FilteredSummary(CurSelectionFilter).Contains(true)) &&
                            CurSelectionFilter != PrevSelectionFilter);
                }
            }
        }
Example #10
0
        void GameLogic(GameTime gameTime)
        {
            //Send("setMode", "in-game");
            //Send("setScreen", "in-game-ui");

            switch (State)
            {
            case GameState.ToEditor:
                NewWorldEditor();
                State = GameState.Game;

                Send("setMode", "in-game");
                Send("setScreen", "editor-ui");

                break;

            case GameState.ToMap:
                Render.StandardRenderSetup();
                DrawFullScreen(Assets.ScreenLoading);

                if (Program.StartupMap == null)
                {
                    Program.StartupMap = "Beset.m3n";
                }
                SetScenarioToLoad(Program.StartupMap);

                break;

            case GameState.TitleScreen:
                StartMenuMusicIfNeeded();
                AmbientSounds.EndAll();

                // No mouse input to web browser.
                SteamWrapper.SteamHtml.AllowMouseEvents = false;

                Render.StandardRenderSetup();

                DrawFullScreen(Assets.ScreenTitle);

                if (gameTime.TotalGameTime.Seconds < .005f)
                {
                    break;
                }

                if (MouseMovedSome)
                {
                    World.DrawArrowCursor();
                }
                else
                {
                    if (Input.DeltaMousPos.Length() > 40)
                    {
                        MouseMovedSome = true;
                    }
                }

                if (InputHelper.SomethingPressed())
                {
                    State = GameState.MainMenu;
                    Send("setMode", "main-menu");
                    Send("setScreen", "game-menu");
                }

                break;

            case GameState.MainMenu:
                StartMenuMusicIfNeeded();
                AmbientSounds.EndAll();

                if (!InputHelper.SomethingDown())
                {
                    SteamWrapper.SteamHtml.AllowMouseEvents = true;
                }

                if (_MapLoading != MapLoading)
                {
                    _MapLoading = MapLoading;
                    SetMapLoading();
                }

                if (MapLoading && NewMap != null)
                {
                    World      = NewMap;
                    MapLoading = false;

                    SetMapLoading();
                }

                Render.StandardRenderSetup();
                if (DrawMapPreview && World != null && World.DataGroup != null)
                {
                    Render.UnsetDevice();
                    try
                    {
                        World.DataGroup.UpdateGradient_ToPlayers();
                        World.DataGroup.UpdateGradient_ToBuildings();
                        World.DataGroup.UpdateGradient_ToPlayers();
                        World.DataGroup.UpdateGradient_ToBuildings();
                    }
                    catch
                    {
                    }

                    World.UpdateMinimap();

                    GridHelper.GraphicsDevice.SetRenderTarget(null);
                }

                Render.StandardRenderSetup();
                DrawFullScreen(Assets.ScreenDark);
                DrawWebView();

                if (DrawMapPreview && World != null && World.DataGroup != null)
                {
                    MapPreviewPos  = new vec2(0.76f, 0.32f);
                    MapPreviewSize = new vec2(.4f, .4f);

                    bool UseSolidColor = MapLoading || World == BlankWorld;
                    World.DrawMinimap(MapPreviewPos, MapPreviewSize, ShowCameraBox: false, SolidColor: UseSolidColor);
                }

                World.DrawArrowCursor();

                break;

            case GameState.Loading:
                FadeOutMenuMusicIfNeeded();

                PreGame();

                Render.StandardRenderSetup();
                DrawFullScreen(Assets.ScreenLoading);

                if (ScenarioToLoad != null)
                {
                    Send("setMode", "in-game");
                    Send("setScreen", "in-game-ui");

                    World = new World(
                        GameParams: Program.StartupGameParams,
                        RemoveComputerDragonLords: Program.RemoveComputerDragonLords);

                    TimeLoading          = 0;
                    World.LoadPlayerInfo = false;
                    World.Load(Path.Combine(MapDirectory, ScenarioToLoad));

                    Program.WorldLoaded = true;
                    Networking.ToServer(new Message(MessageType.DoneLoading));

                    ScenarioToLoad = null;
                    TimeSinceLoad  = 0;
                    DrawFullScreen(Assets.ScreenLoading);

                    GetNames();


                    //World.SaveCurrentStateInBuffer();
                    ////var m = new MessageGameState(World.SimStep, World.WorldBytes);
                    //var m = new Message(MessageType.DoneLoading);
                    //var s = m.Encode();

                    //Networking.SendString(new SteamPlayer(SteamCore.PlayerId()), s);
                    //var t = Networking.ReceiveString();
                    //var _s = t.Item2;

                    //Console.WriteLine("!");
                }

                if (Program.GameStarted)
                {
                    if (Program.Spectate)
                    {
                        State = GameState.Game;
                    }
                }
                else
                {
                    TimeLoading += DeltaT;

                    if (TimeLoading > 25)
                    {
                        OnFailedToJoinGame();
                    }

                    TimeSinceLoad = 0;
                    break;
                }

                FadeOutLoading();

                break;

            case GameState.Game:
                StartGameMusicIfNeeded();

                CalculateMouseDownOverUi();

                DrawGame(gameTime);

                if (Program.Spectate && ShouldDrawFading())
                {
                    Render.StandardRenderSetup();
                    DrawFullScreen(Assets.ScreenLoading);

                    FadeOutLoading();
                }
                else
                {
                    DrawWebView();

                    World.DrawUi();

                    if (TimeSinceLoad < 1.5f)
                    {
                        BlackOverlay(1f - (float)(TimeSinceLoad - 1.3f) / .2f);
                    }
                }

                break;
            }
        }