示例#1
0
        // no storge sync

        private bool ClientClientMouseEvent(PacketObj data)
        {
            var packet      = data.Packet;
            var mousePacket = (InputPacket)packet;

            if (mousePacket.Data == null)
            {
                return(Error(data, Msg("Data")));
            }
            var cube = MyEntities.GetEntityByIdOrDefault(packet.EntityId) as MyCubeBlock;

            if (cube == null)
            {
                return(Error(data, Msg($"CubeId: {packet.EntityId}")));
            }

            GridAi ai;
            long   playerId;

            if (GridToMasterAi.TryGetValue(cube.CubeGrid, out ai) && SteamToPlayer.TryGetValue(packet.SenderId, out playerId))
            {
                PlayerMouseStates[playerId] = mousePacket.Data;

                data.Report.PacketValid = true;
            }
            else
            {
                return(Error(data, Msg("No PlayerId Found")));
            }

            return(true);
        }
示例#2
0
        private bool ClientConstructFoci(PacketObj data)
        {
            var packet     = data.Packet;
            var myGrid     = MyEntities.GetEntityByIdOrDefault(packet.EntityId) as MyCubeGrid;
            var fociPacket = (ConstructFociPacket)packet;

            if (myGrid == null)
            {
                return(Error(data, Msg($"Grid: {packet.EntityId}")));
            }

            GridAi ai;

            if (GridToMasterAi.TryGetValue(myGrid, out ai))
            {
                var rootConstruct = ai.Construct.RootAi.Construct;

                if (rootConstruct.Data.Repo.FocusData.Revision < fociPacket.Data.Revision)
                {
                    rootConstruct.Data.Repo.FocusData.Sync(ai, fociPacket.Data);
                }
                else
                {
                    Log.Line($"ClientConstructFoci Version failure - Version:{rootConstruct.Data.Repo.FocusData.Revision}({fociPacket.Data.Revision})");
                }

                data.Report.PacketValid = true;
            }
            else
            {
                return(Error(data, Msg($"GridAi not found, is marked:{myGrid.MarkedForClose}, has root:{GridToMasterAi.ContainsKey(myGrid)}")));
            }

            return(true);
        }
示例#3
0
        internal bool UpdateLocalAiAndCockpit()
        {
            InGridAiBlock      = false;
            ActiveControlBlock = ControlledEntity as MyCubeBlock;

            var cockPit = ControlledEntity as MyCockpit;

            if (cockPit != null && cockPit.EnableShipControl)
            {
                ActiveCockPit = cockPit;
            }
            else
            {
                ActiveCockPit = null;
            }

            long oldBlockId;
            var  activeBlock = ActiveCockPit ?? ActiveControlBlock;

            if (activeBlock != null && ActiveControlBlock != null && GridToMasterAi.TryGetValue(activeBlock.CubeGrid, out TrackingAi))
            {
                InGridAiBlock = true;
                TrackingAi.Data.Repo.ControllingPlayers.TryGetValue(PlayerId, out oldBlockId);

                if (IsServer)
                {
                    TrackingAi.Construct.UpdateConstructsPlayers(ActiveControlBlock, PlayerId, true);
                }
                if (HandlesInput && oldBlockId != ActiveControlBlock.EntityId)
                {
                    SendActiveControlUpdate(TrackingAi, activeBlock, true);
                }
            }
            else
            {
                if (TrackingAi != null)
                {
                    TrackingAi.Construct.Focus.ClientIsFocused(TrackingAi);

                    MyCubeBlock oldBlock;
                    if (TrackingAi.Data.Repo.ControllingPlayers.TryGetValue(PlayerId, out oldBlockId) && MyEntities.TryGetEntityById(oldBlockId, out oldBlock, true))
                    {
                        if (IsServer)
                        {
                            TrackingAi.Construct.UpdateConstructsPlayers(ActiveControlBlock, PlayerId, false);
                        }
                        if (HandlesInput)
                        {
                            SendActiveControlUpdate(TrackingAi, oldBlock, false);
                        }
                    }
                }

                TrackingAi         = null;
                ActiveCockPit      = null;
                ActiveControlBlock = null;
            }
            return(InGridAiBlock);
        }
        internal void ProccessServerPacketsForClients()
        {
            if ((!IsServer || !MpActive))
            {
                Log.Line($"trying to process server packets on a non-server");
                return;
            }

            PacketsToClient.AddRange(PrunedPacketsToClient.Values);
            for (int i = 0; i < PacketsToClient.Count; i++)
            {
                var packetInfo = PacketsToClient[i];
                var bytes      = MyAPIGateway.Utilities.SerializeToBinary(packetInfo.Packet);
                if (packetInfo.SingleClient)
                {
                    MyModAPIHelper.MyMultiplayer.Static.SendMessageTo(ClientPacketId, bytes, packetInfo.Packet.SenderId, true);
                }
                else
                {
                    long entityId = packetInfo.Entity?.GetTopMostParent().EntityId ?? -1;

                    foreach (var p in Players.Values)
                    {
                        var notSender  = p.SteamUserId != packetInfo.Packet.SenderId;
                        var sendPacket = notSender && packetInfo.Entity == null;
                        if (!sendPacket && notSender)
                        {
                            if (PlayerEntityIdInRange.ContainsKey(p.SteamUserId))
                            {
                                if (PlayerEntityIdInRange[p.SteamUserId].Contains(entityId))
                                {
                                    sendPacket = true;
                                }
                                else
                                {
                                    GridAi rootAi;
                                    var    grid = packetInfo.Entity.GetTopMostParent() as MyCubeGrid;
                                    if (grid != null && GridToMasterAi.TryGetValue(grid, out rootAi) && PlayerEntityIdInRange[p.SteamUserId].Contains(rootAi.MyGrid.EntityId))
                                    {
                                        sendPacket = true;
                                    }
                                }
                            }
                        }

                        if (sendPacket)
                        {
                            MyModAPIHelper.MyMultiplayer.Static.SendMessageTo(ClientPacketId, bytes, p.SteamUserId, true);
                        }
                    }
                }
            }

            ServerPacketsForClientsClean();
        }
示例#5
0
 private void MenuOpened(object obj)
 {
     try
     {
         InMenu = true;
         GridAi ai;
         if (ActiveControlBlock != null && GridToMasterAi.TryGetValue(ActiveControlBlock.CubeGrid, out ai))
         {
             //Send updates?
         }
     }
     catch (Exception ex) { Log.Line($"Exception in MenuOpened: {ex}"); }
 }
示例#6
0
        private bool ServerFocusUpdate(PacketObj data)
        {
            var packet      = data.Packet;
            var focusPacket = (FocusPacket)packet;
            var myGrid      = MyEntities.GetEntityByIdOrDefault(packet.EntityId) as MyCubeGrid;

            if (myGrid == null)
            {
                return(Error(data, Msg("Grid")));
            }

            GridAi ai;

            uint[] mIds;
            if (GridToMasterAi.TryGetValue(myGrid, out ai) && PlayerMIds.TryGetValue(packet.SenderId, out mIds) && mIds[(int)packet.PType] < packet.MId)
            {
                mIds[(int)packet.PType] = packet.MId;

                var targetGrid = MyEntities.GetEntityByIdOrDefault(focusPacket.TargetId) as MyCubeGrid;

                switch (packet.PType)
                {
                case PacketType.FocusUpdate:
                    if (targetGrid != null)
                    {
                        ai.Construct.Focus.ServerAddFocus(targetGrid, ai);
                    }
                    break;

                case PacketType.NextActiveUpdate:
                    ai.Construct.Focus.ServerNextActive(focusPacket.AddSecondary, ai);
                    break;

                case PacketType.ReleaseActiveUpdate:
                    ai.Construct.Focus.RequestReleaseActive(ai);
                    break;

                case PacketType.FocusLockUpdate:
                    ai.Construct.Focus.ServerCycleLock(ai);
                    break;
                }

                data.Report.PacketValid = true;
            }
            else
            {
                return(Error(data, Msg($"GridAi not found or mid failure: ai:{ai != null}, is marked:{myGrid.MarkedForClose}, has root:{GridToMasterAi.ContainsKey(myGrid)}")));
            }

            return(true);
        }
示例#7
0
        internal void SetTarget(MyEntity entity, GridAi ai, Dictionary <MyEntity, float> masterTargets)
        {
            TrackingAi = ai;
            ai.Construct.Focus.RequestAddFocus(entity, ai);

            GridAi gridAi;

            TargetArmed = false;
            var grid = entity as MyCubeGrid;

            if (grid != null && GridToMasterAi.TryGetValue(grid, out gridAi))
            {
                TargetArmed = true;
            }
            else
            {
                float offenseRating;
                if (!masterTargets.TryGetValue(entity, out offenseRating))
                {
                    return;
                }
                ConcurrentDictionary <BlockTypes, ConcurrentCachingList <MyCubeBlock> > typeDict;

                var tGrid = entity as MyCubeGrid;
                if (tGrid != null && GridToBlockTypeMap.TryGetValue(tGrid, out typeDict))
                {
                    ConcurrentCachingList <MyCubeBlock> fatList;
                    if (typeDict.TryGetValue(Offense, out fatList))
                    {
                        TargetArmed = fatList.Count > 0;
                    }
                    else
                    {
                        TargetArmed = false;
                    }
                }
                else
                {
                    TargetArmed = false;
                }
            }
        }
        private bool ClientConstruct(PacketObj data)
        {
            var packet   = data.Packet;
            var myGrid   = MyEntities.GetEntityByIdOrDefault(packet.EntityId) as MyCubeGrid;
            var cgPacket = (ConstructPacket)packet;

            if (myGrid == null)
            {
                return(Error(data, Msg($"Grid: {packet.EntityId}")));
            }

            GridAi ai;

            if (GridToMasterAi.TryGetValue(myGrid, out ai))
            {
                if (ai.MIds[(int)packet.PType] < packet.MId)
                {
                    ai.MIds[(int)packet.PType] = packet.MId;

                    var rootConstruct = ai.Construct.RootAi.Construct;

                    rootConstruct.Data.Repo.Sync(rootConstruct, cgPacket.Data);
                    rootConstruct.UpdateLeafs();
                }
                else
                {
                    Log.Line($"ClientConstructGroups MID failure - mId:{packet.MId}");
                }

                data.Report.PacketValid = true;
            }
            else
            {
                return(Error(data, Msg($"GridAi not found, is marked:{myGrid.MarkedForClose}, has root:{GridToMasterAi.ContainsKey(myGrid)}")));
            }

            return(true);
        }
示例#9
0
        private bool ServerActiveControlUpdate(PacketObj data)
        {
            var packet  = data.Packet;
            var dPacket = (BoolUpdatePacket)packet;
            var cube    = MyEntities.GetEntityByIdOrDefault(packet.EntityId) as MyCubeBlock;

            if (cube == null)
            {
                return(Error(data, Msg("Cube")));
            }

            GridAi ai;
            long   playerId = 0;

            if (GridToMasterAi.TryGetValue(cube.CubeGrid, out ai) && SteamToPlayer.TryGetValue(packet.SenderId, out playerId))
            {
                uint[] mIds;
                if (PlayerMIds.TryGetValue(packet.SenderId, out mIds) && mIds[(int)packet.PType] < packet.MId)
                {
                    mIds[(int)packet.PType] = packet.MId;

                    ai.Construct.UpdateConstructsPlayers(cube, playerId, dPacket.Data);
                    data.Report.PacketValid = true;
                }
                else
                {
                    Log.Line($"ServerActiveControlUpdate: MidsHasSenderId:{PlayerMIds.ContainsKey(packet.SenderId)} - midsNull:{mIds == null} - senderId:{packet.SenderId}");
                }
            }
            else
            {
                Log.Line($"ServerActiveControlUpdate: ai:{ai != null} - targetingAi:{GridTargetingAIs.ContainsKey(cube.CubeGrid)} - masterAi:{GridToMasterAi.ContainsKey(cube.CubeGrid)} - IdToComp:{IdToCompMap.ContainsKey(cube.EntityId)} - {cube.BlockDefinition.Id.SubtypeName} - playerId:{playerId}({packet.SenderId}) - marked:{cube.MarkedForClose}({cube.CubeGrid.MarkedForClose}) - active:{dPacket.Data}");
            }

            return(true);
        }
示例#10
0
        private void UpdatePlacer()
        {
            if (!Placer.Visible)
            {
                Placer = null;
            }
            if (!MyCubeBuilder.Static.DynamicMode && MyCubeBuilder.Static.HitInfo.HasValue)
            {
                var    hit  = MyCubeBuilder.Static.HitInfo.Value as IHitInfo;
                var    grid = hit.HitEntity as MyCubeGrid;
                GridAi gridAi;
                if (grid != null && GridToMasterAi.TryGetValue(grid, out gridAi))
                {
                    if (MyCubeBuilder.Static.CurrentBlockDefinition != null)
                    {
                        var         subtypeIdHash = MyCubeBuilder.Static.CurrentBlockDefinition.Id.SubtypeId;
                        WeaponCount weaponCount;
                        if (gridAi.WeaponCounter.TryGetValue(subtypeIdHash, out weaponCount))
                        {
                            if (weaponCount.Max > 0 && gridAi.Construct.GetWeaponCount(subtypeIdHash) >= weaponCount.Max)
                            {
                                MyCubeBuilder.Static.NotifyPlacementUnable();
                                MyCubeBuilder.Static.Deactivate();
                                return;
                            }
                        }

                        if (WeaponAreaRestrictions.ContainsKey(subtypeIdHash))
                        {
                            MyOrientedBoundingBoxD restrictedBox;
                            MyOrientedBoundingBoxD buildBox = MyCubeBuilder.Static.GetBuildBoundingBox();
                            BoundingSphereD        restrictedSphere;
                            if (IsWeaponAreaRestricted(subtypeIdHash, buildBox, grid, 0, null, out restrictedBox, out restrictedSphere))
                            {
                                DsDebugDraw.DrawBox(buildBox, _uninitializedColor);
                            }

                            if (MyAPIGateway.Session.Config.HudState == 1)
                            {
                                if (restrictedBox.HalfExtent.AbsMax() > 0)
                                {
                                    DsDebugDraw.DrawBox(restrictedBox, _restrictionAreaColor);
                                }
                                if (restrictedSphere.Radius > 0)
                                {
                                    DsDebugDraw.DrawSphere(restrictedSphere, _restrictionAreaColor);
                                }
                                for (int i = 0; i < gridAi.Weapons.Count; i++)
                                {
                                    MyOrientedBoundingBoxD b;
                                    BoundingSphereD        s;
                                    CoreComponent          Comp = gridAi.Weapons[i];
                                    MyOrientedBoundingBoxD blockBox;
                                    SUtils.GetBlockOrientedBoundingBox(Comp.MyCube, out blockBox);

                                    CalculateRestrictedShapes(Comp.MyCube.BlockDefinition.Id.SubtypeId, blockBox, out b, out s);

                                    if (s.Radius > 0)
                                    {
                                        DsDebugDraw.DrawSphere(s, _restrictionAreaColor);
                                    }
                                    if (b.HalfExtent.AbsMax() > 0)
                                    {
                                        DsDebugDraw.DrawBox(b, _restrictionAreaColor);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        internal bool UpdateLocalAiAndCockpit()
        {
            InGridAiBlock      = false;
            ActiveControlBlock = ControlledEntity as MyCubeBlock;
            ActiveCockPit      = ActiveControlBlock as MyCockpit;

            var activeBlock = ActiveCockPit ?? ActiveControlBlock;

            if (activeBlock != null && (GridTargetingAIs.TryGetValue(activeBlock.CubeGrid, out TrackingAi) || GridToMasterAi.TryGetValue(activeBlock.CubeGrid, out TrackingAi)))
            {
                InGridAiBlock = true;
                MyCubeBlock oldBlock;
                TrackingAi.ControllingPlayers.TryGetValue(PlayerId, out oldBlock);
                TrackingAi.ControllingPlayers[PlayerId] = ActiveControlBlock;
                TrackingAi.ControllingPlayers.ApplyAdditionsAndModifications();

                if (HandlesInput && oldBlock != ActiveControlBlock)
                {
                    SendActiveControlUpdate(activeBlock, true);
                }
            }
            else
            {
                if (TrackingAi != null)
                {
                    TrackingAi.Focus.IsFocused(TrackingAi);

                    MyCubeBlock oldBlock;
                    if (HandlesInput && TrackingAi.ControllingPlayers.TryGetValue(PlayerId, out oldBlock))
                    {
                        SendActiveControlUpdate(oldBlock, false);
                    }

                    TrackingAi.ControllingPlayers.Remove(PlayerId, true);
                    TrackingAi.ControllingPlayers.ApplyRemovals();
                }

                TrackingAi         = null;
                ActiveCockPit      = null;
                ActiveControlBlock = null;
            }
            return(InGridAiBlock);
        }
示例#12
0
        internal bool UpdateLocalAiAndCockpit()
        {
            InGridAiBlock      = false;
            ActiveControlBlock = ControlledEntity as MyCubeBlock;

            var cockPit = ControlledEntity as MyCockpit;

            if (cockPit != null && cockPit.EnableShipControl)
            {
                ActiveCockPit = cockPit;
            }
            else
            {
                ActiveCockPit = null;
            }

            long oldBlockId;
            var  activeBlock = ActiveCockPit ?? ActiveControlBlock;

            if (activeBlock != null && ActiveControlBlock != null && GridToMasterAi.TryGetValue(activeBlock.CubeGrid, out TrackingAi))
            {
                var camera = Session.CameraController?.Entity as MyCameraBlock;
                if (camera == null || !GroupedCamera(camera))
                {
                    ActiveCameraBlock = null;
                }

                InGridAiBlock = true;
                TrackingAi.Data.Repo.ControllingPlayers.TryGetValue(PlayerId, out oldBlockId);

                if (IsServer)
                {
                    TrackingAi.Construct.UpdateConstructsPlayers(ActiveControlBlock, PlayerId, true);
                }
                if (oldBlockId != ActiveControlBlock.EntityId)
                {
                    SendActiveControlUpdate(TrackingAi, activeBlock, true);
                    TargetLeadUpdate();
                }
                else if (LeadGroupsDirty || !MyUtils.IsEqual(LastOptimalDps, TrackingAi.Construct.OptimalDps))
                {
                    TargetLeadUpdate();
                }
            }
            else
            {
                if (TrackingAi != null)
                {
                    TrackingAi.Construct.Focus.ClientIsFocused(TrackingAi);

                    MyCubeBlock oldBlock;
                    if (TrackingAi.Data.Repo.ControllingPlayers.TryGetValue(PlayerId, out oldBlockId) && MyEntities.TryGetEntityById(oldBlockId, out oldBlock, true))
                    {
                        if (IsServer)
                        {
                            TrackingAi.Construct.UpdateConstructsPlayers(ActiveControlBlock, PlayerId, false);
                        }

                        SendActiveControlUpdate(TrackingAi, oldBlock, false);
                        foreach (var list in LeadGroups)
                        {
                            list.Clear();
                        }
                        LeadGroupActive = false;
                    }
                }

                TrackingAi         = null;
                ActiveCockPit      = null;
                ActiveControlBlock = null;
                ActiveCameraBlock  = null;
            }
            return(InGridAiBlock);
        }