// ------------------------------------------------------------------------------------ public override void LoadData() { if (Static != null) { // take data from previous instance m_listenerThread = Static.m_listenerThread; m_listener = Static.m_listener; m_clients = Static.m_clients; m_active = Static.m_active; m_arrayBuffer = Static.m_arrayBuffer; m_tempBuffer = Static.m_tempBuffer; m_receivedMsgHandlers = Static.m_receivedMsgHandlers; MySessionComponentExtDebug.Static = this; base.LoadData(); return; } MySessionComponentExtDebug.Static = this; if (m_tempBuffer == IntPtr.Zero) { m_tempBuffer = Marshal.AllocHGlobal(MsgSizeLimit); } if (!ForceDisable) { StartServer(); } base.LoadData(); }
private void OnDepositQueryComplete(MyDepositQuery query, ConcurrentCachingList <MyEntityOreDeposit> deposits, List <Vector3I> emptyCells) { foreach (var deposit in deposits) { Vector3I depositCell = deposit.CellCoord; m_depositsByCellCoord[depositCell] = deposit; //RegisterMarker(deposit); //MyHud.OreMarkers.RegisterMarker(deposit); } foreach (var emptyCell in emptyCells) { m_emptyCellCoord.Add(emptyCell); } m_tasksRunning--; var bb = new BoundingBoxI(query.Min, query.Max); m_completed += bb.Size.Size; Logger.Instance.LogDebugOnGameThread($"Completed: {m_completed}"); if (m_tasksRunning == 0) { Scanning = false; } }
public SpraySoundEmitter(Func <Vector3D> positionGetter, Func <bool> realisticIsHoldingCondition) { if (positionGetter == null) { throw new ArgumentNullException("positionGetter"); } if (realisticIsHoldingCondition == null) { throw new ArgumentNullException("realisticIsHoldingCondition"); } SoundEmitter = new MyEntity3DSoundEmitter(null); PositionGetter = positionGetter; // remove all 2D forcing conditions ConcurrentCachingList <Delegate> shouldPlay2D = SoundEmitter.EmitterMethods[(int)MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D]; shouldPlay2D.ClearImmediate(); shouldPlay2D.Add(new Func <bool>(EmitterPlay2D)); // if no methods are declared, it defaults to always 2D if (MyAPIGateway.Session.SessionSettings.RealisticSound) { // remove some unnecessary conditions foreach (ConcurrentCachingList <Delegate> funcList in SoundEmitter.EmitterMethods.Values) { foreach (Delegate func in funcList) { switch (func.Method.Name) { case "IsCurrentWeapon": case "IsControlledEntity": case "IsOnSameGrid": funcList.Remove(func); break; } } } // custom IsCurrentWeapon because Entity is not set for this emitter to detect on its own SoundEmitter.EmitterMethods[(int)MyEntity3DSoundEmitter.MethodsEnum.CanHear].Add(realisticIsHoldingCondition); //foreach(KeyValuePair<int, ConcurrentCachingList<Delegate>> kv in SoundEmitter.EmitterMethods) //{ // kv.Value.ApplyChanges(); // foreach(Delegate func in kv.Value) // { // Log.Info($"{((MyEntity3DSoundEmitter.MethodsEnum)kv.Key)} {func.Method.Name}"); // } //} } }
internal static bool GetClosestHitableBlockOfType(ConcurrentCachingList <MyCubeBlock> cubes, GridAi ai, Target target, Vector3D currentPos, Vector3D targetLinVel, Vector3D targetAccel, ref BoundingSphereD waterSphere, Weapon w = null, bool checkPower = true) { var minValue = double.MaxValue; var minValue0 = double.MaxValue; var minValue1 = double.MaxValue; var minValue2 = double.MaxValue; var minValue3 = double.MaxValue; MyCubeBlock newEntity = null; MyCubeBlock newEntity0 = null; MyCubeBlock newEntity1 = null; MyCubeBlock newEntity2 = null; MyCubeBlock newEntity3 = null; var bestCubePos = Vector3D.Zero; var top5Count = target.Top5.Count; var testPos = currentPos; var top5 = target.Top5; IHitInfo hitInfo = null; for (int i = 0; i < cubes.Count + top5Count; i++) { ai.Session.BlockChecks++; var index = i < top5Count ? i : i - top5Count; var cube = i < top5Count ? top5[index] : cubes[index]; var grid = cube.CubeGrid; if (grid == null || grid.MarkedForClose) { continue; } if (!(cube is IMyTerminalBlock) || cube.MarkedForClose || cube == newEntity || cube == newEntity0 || cube == newEntity1 || cube == newEntity2 || cube == newEntity3 || checkPower && !cube.IsWorking) { continue; } var cubePos = grid.GridIntegerToWorld(cube.Position); var range = cubePos - testPos; var test = (range.X * range.X) + (range.Y * range.Y) + (range.Z * range.Z); if (ai.Session.WaterApiLoaded && waterSphere.Radius > 2 && waterSphere.Contains(cubePos) != ContainmentType.Disjoint) { continue; } if (test < minValue3) { IHitInfo hit = null; var best = test < minValue; var bestTest = false; if (best) { if (w != null && !(!w.IsTurret && w.ActiveAmmoDef.AmmoDef.Trajectory.Smarts.OverideTarget)) { ai.Session.CanShoot++; Vector3D predictedPos; if (Weapon.CanShootTarget(w, ref cubePos, targetLinVel, targetAccel, out predictedPos)) { ai.Session.ClosestRayCasts++; if (ai.Session.Physics.CastRay(testPos, cubePos, out hit, CollisionLayers.DefaultCollisionLayer)) { var hitEnt = hit.HitEntity?.GetTopMostParent() as MyEntity; var hitGrid = hitEnt as MyCubeGrid; if (hitGrid != null && grid == hitGrid || hit.HitEntity is MyFloatingObject || hit.HitEntity is IMyCharacter || hitEnt != null && w.Comp.Ai.Targets.ContainsKey(hitEnt)) { bestTest = true; } } } } else { bestTest = true; } } if (best && bestTest) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = minValue1; newEntity2 = newEntity1; minValue1 = minValue0; newEntity1 = newEntity0; minValue0 = minValue; newEntity0 = newEntity; minValue = test; newEntity = cube; bestCubePos = cubePos; hitInfo = hit; } else if (test < minValue0) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = minValue1; newEntity2 = newEntity1; minValue1 = minValue0; newEntity1 = newEntity0; minValue0 = test; newEntity0 = cube; } else if (test < minValue1) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = minValue1; newEntity2 = newEntity1; minValue1 = test; newEntity1 = cube; } else if (test < minValue2) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = test; newEntity2 = cube; } else { minValue3 = test; newEntity3 = cube; } } } top5.Clear(); if (newEntity != null && hitInfo != null) { double rayDist; Vector3D.Distance(ref testPos, ref bestCubePos, out rayDist); var shortDist = rayDist * (1 - hitInfo.Fraction); var origDist = rayDist * hitInfo.Fraction; var topEntId = newEntity.GetTopMostParent().EntityId; target.Set(newEntity, hitInfo.Position, shortDist, origDist, topEntId); top5.Add(newEntity); } else if (newEntity != null) { double rayDist; Vector3D.Distance(ref testPos, ref bestCubePos, out rayDist); var shortDist = rayDist; var origDist = rayDist; var topEntId = newEntity.GetTopMostParent().EntityId; target.Set(newEntity, bestCubePos, shortDist, origDist, topEntId); top5.Add(newEntity); } else { target.Reset(ai.Session.Tick, Target.States.NoTargetsSeen, w == null); } if (newEntity0 != null) { top5.Add(newEntity0); } if (newEntity1 != null) { top5.Add(newEntity1); } if (newEntity2 != null) { top5.Add(newEntity2); } if (newEntity3 != null) { top5.Add(newEntity3); } return(top5.Count > 0); }
private static bool FindRandomBlock(WeaponSystem system, GridAi ai, Target target, Vector3D weaponPos, TargetInfo info, ConcurrentCachingList <MyCubeBlock> subSystemList, Weapon w, WeaponRandomGenerator wRng, RandomType type, ref BoundingSphereD waterSphere, bool checkPower = true) { var totalBlocks = subSystemList.Count; var topEnt = info.Target.GetTopMostParent(); var entSphere = topEnt.PositionComp.WorldVolume; var distToEnt = MyUtils.GetSmallestDistanceToSphere(ref weaponPos, ref entSphere); var turretCheck = w != null; var topBlocks = system.Values.Targeting.TopBlocks; var lastBlocks = topBlocks > 10 && distToEnt < 1000 ? topBlocks : 10; var isPriroity = false; if (lastBlocks < 250) { TargetInfo priorityInfo; MyEntity fTarget; if (ai.Construct.Data.Repo.FocusData.Target[0] > 0 && MyEntities.TryGetEntityById(ai.Construct.Data.Repo.FocusData.Target[0], out fTarget) && ai.Targets.TryGetValue(fTarget, out priorityInfo) && priorityInfo.Target?.GetTopMostParent() == topEnt) { isPriroity = true; lastBlocks = totalBlocks < 250 ? totalBlocks : 250; } else if (ai.Construct.Data.Repo.FocusData.Target[1] > 0 && MyEntities.TryGetEntityById(ai.Construct.Data.Repo.FocusData.Target[1], out fTarget) && ai.Targets.TryGetValue(fTarget, out priorityInfo) && priorityInfo.Target?.GetTopMostParent() == topEnt) { isPriroity = true; lastBlocks = totalBlocks < 250 ? totalBlocks : 250; } } if (totalBlocks < lastBlocks) { lastBlocks = totalBlocks; } var deck = GetDeck(ref target.BlockDeck, ref target.BlockPrevDeckLen, 0, totalBlocks, topBlocks, wRng, type); var physics = system.Session.Physics; var iGrid = topEnt as IMyCubeGrid; var gridPhysics = iGrid?.Physics; Vector3D targetLinVel = gridPhysics?.LinearVelocity ?? Vector3D.Zero; Vector3D targetAccel = (int)system.Values.HardPoint.AimLeadingPrediction > 1 ? info.Target.Physics?.LinearAcceleration ?? Vector3D.Zero : Vector3.Zero; var foundBlock = false; var blocksChecked = 0; var blocksSighted = 0; for (int i = 0; i < totalBlocks; i++) { if (turretCheck && (blocksChecked > lastBlocks || isPriroity && (blocksSighted > 100 || blocksChecked > 50 && system.Session.RandomRayCasts > 500 || blocksChecked > 25 && system.Session.RandomRayCasts > 1000))) { break; } var card = deck[i]; var block = subSystemList[card]; if (!(block is IMyTerminalBlock) || block.MarkedForClose || checkPower && !block.IsWorking) { continue; } system.Session.BlockChecks++; var blockPos = block.CubeGrid.GridIntegerToWorld(block.Position); double rayDist; if (turretCheck) { double distSqr; Vector3D.DistanceSquared(ref blockPos, ref weaponPos, out distSqr); if (distSqr > w.MaxTargetDistanceSqr || distSqr < w.MinTargetDistanceSqr) { continue; } blocksChecked++; ai.Session.CanShoot++; Vector3D predictedPos; if (!Weapon.CanShootTarget(w, ref blockPos, targetLinVel, targetAccel, out predictedPos)) { continue; } if (system.Session.WaterApiLoaded && waterSphere.Radius > 2 && waterSphere.Contains(predictedPos) != ContainmentType.Disjoint) { continue; } blocksSighted++; system.Session.RandomRayCasts++; IHitInfo hitInfo; physics.CastRay(weaponPos, blockPos, out hitInfo, 15); if (hitInfo?.HitEntity == null || hitInfo.HitEntity is MyVoxelBase) { continue; } var hitGrid = hitInfo.HitEntity as MyCubeGrid; if (hitGrid != null) { if (hitGrid.MarkedForClose || hitGrid != block.CubeGrid && hitGrid.IsSameConstructAs(ai.MyGrid)) { continue; } bool enemy; var bigOwners = hitGrid.BigOwners; if (bigOwners.Count == 0) { enemy = true; } else { var relationship = target.FiringCube.GetUserRelationToOwner(hitGrid.BigOwners[0]); enemy = relationship != MyRelationsBetweenPlayerAndBlock.Owner && relationship != MyRelationsBetweenPlayerAndBlock.FactionShare; } if (!enemy) { continue; } } Vector3D.Distance(ref weaponPos, ref blockPos, out rayDist); var shortDist = rayDist * (1 - hitInfo.Fraction); var origDist = rayDist * hitInfo.Fraction; var topEntId = block.GetTopMostParent().EntityId; target.Set(block, hitInfo.Position, shortDist, origDist, topEntId); foundBlock = true; break; } Vector3D.Distance(ref weaponPos, ref blockPos, out rayDist); target.Set(block, block.PositionComp.WorldAABB.Center, rayDist, rayDist, block.GetTopMostParent().EntityId); foundBlock = true; break; } return(foundBlock); }
internal static bool GetClosestHitableBlockOfType(ConcurrentCachingList <MyCubeBlock> cubes, GridAi ai, Target target, Vector3D currentPos, Vector3D targetLinVel, Vector3D targetAccel, WeaponSystem system, Weapon w = null, bool checkPower = true) { var minValue = double.MaxValue; var minValue0 = double.MaxValue; var minValue1 = double.MaxValue; var minValue2 = double.MaxValue; var minValue3 = double.MaxValue; MyCubeBlock newEntity = null; MyCubeBlock newEntity0 = null; MyCubeBlock newEntity1 = null; MyCubeBlock newEntity2 = null; MyCubeBlock newEntity3 = null; var bestCubePos = Vector3D.Zero; var top5Count = target.Top5.Count; var testPos = currentPos; var top5 = target.Top5; IHitInfo hitInfo = null; var notSelfHit = false; for (int i = 0; i < cubes.Count + top5Count; i++) { ai.Session.BlockChecks++; var index = i < top5Count ? i : i - top5Count; var cube = i < top5Count ? top5[index] : cubes[index]; var grid = cube.CubeGrid; if (grid?.Physics == null || !grid.Physics.Enabled || grid.PositionComp == null) { continue; } if (cube.MarkedForClose || checkPower && !cube.IsWorking || !(cube is IMyTerminalBlock) || cube == newEntity || cube == newEntity0 || cube == newEntity1 || cube == newEntity2 || cube == newEntity3) { continue; } var cubePos = grid.GridIntegerToWorld(cube.Position); var range = cubePos - testPos; var test = (range.X * range.X) + (range.Y * range.Y) + (range.Z * range.Z); if (test < minValue3) { IHitInfo hit = null; var best = test < minValue; var bestTest = false; if (best) { if (w != null && !(!w.IsTurret && w.ActiveAmmoDef.AmmoDef.Trajectory.Smarts.OverideTarget)) { ai.Session.CanShoot++; var castRay = false; Vector3D predictedPos; Vector3D?hitPos; if (Weapon.CanShootTarget(w, cubePos, targetLinVel, targetAccel, out predictedPos)) { castRay = !w.HitOther || !GridIntersection.BresenhamGridIntersection(ai.MyGrid, ref testPos, ref cubePos, out hitPos, w.Comp.MyCube, w.Comp.Ai); } if (castRay) { ai.Session.ClosestRayCasts++; bestTest = MyAPIGateway.Physics.CastRay(testPos, cubePos, out hit, 15, true) && hit?.HitEntity == cube.CubeGrid; if (hit == null && (!w.System.Values.HardPoint.Other.MuzzleCheck || !w.MuzzleHitSelf()) || (hit.HitEntity != ai.MyGrid)) { notSelfHit = true; } } } else { bestTest = true; } } if (best && bestTest) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = minValue1; newEntity2 = newEntity1; minValue1 = minValue0; newEntity1 = newEntity0; minValue0 = minValue; newEntity0 = newEntity; minValue = test; newEntity = cube; bestCubePos = cubePos; hitInfo = hit; } else if (test < minValue0) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = minValue1; newEntity2 = newEntity1; minValue1 = minValue0; newEntity1 = newEntity0; minValue0 = test; newEntity0 = cube; } else if (test < minValue1) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = minValue1; newEntity2 = newEntity1; minValue1 = test; newEntity1 = cube; } else if (test < minValue2) { minValue3 = minValue2; newEntity3 = newEntity2; minValue2 = test; newEntity2 = cube; } else { minValue3 = test; newEntity3 = cube; } } } top5.Clear(); if (newEntity != null && hitInfo != null) { double rayDist; Vector3D.Distance(ref testPos, ref bestCubePos, out rayDist); var shortDist = rayDist * (1 - hitInfo.Fraction); var origDist = rayDist * hitInfo.Fraction; var topEntId = newEntity.GetTopMostParent().EntityId; target.Set(newEntity, hitInfo.Position, shortDist, origDist, topEntId); top5.Add(newEntity); } else if (newEntity != null) { double rayDist; Vector3D.Distance(ref testPos, ref bestCubePos, out rayDist); var shortDist = rayDist; var origDist = rayDist; var topEntId = newEntity.GetTopMostParent().EntityId; target.Set(newEntity, bestCubePos, shortDist, origDist, topEntId); top5.Add(newEntity); } else { target.Reset(ai.Session.Tick, Target.States.NoTargetsSeen, w == null); } if (newEntity0 != null) { top5.Add(newEntity0); } if (newEntity1 != null) { top5.Add(newEntity1); } if (newEntity2 != null) { top5.Add(newEntity2); } if (newEntity3 != null) { top5.Add(newEntity3); } if (!notSelfHit && w != null) { w.HitOther = true; } return(hitInfo != null); }
private static bool FindRandomBlock(WeaponSystem system, GridAi ai, Target target, Vector3D weaponPos, TargetInfo info, ConcurrentCachingList <MyCubeBlock> subSystemList, Weapon w, int seed, bool checkPower = true) { var totalBlocks = subSystemList.Count; var topEnt = info.Target.GetTopMostParent(); var entSphere = topEnt.PositionComp.WorldVolume; var distToEnt = MyUtils.GetSmallestDistanceToSphere(ref weaponPos, ref entSphere); var turretCheck = w != null; var topBlocks = system.Values.Targeting.TopBlocks; var lastBlocks = topBlocks > 10 && distToEnt < 1000 ? topBlocks : 10; var isPriroity = false; if (lastBlocks < 250) { TargetInfo priorityInfo; if (ai.Focus.Target[0] != null && ai.Targets.TryGetValue(ai.Focus.Target[0], out priorityInfo) && priorityInfo.Target?.GetTopMostParent() == topEnt) { isPriroity = true; lastBlocks = totalBlocks < 250 ? totalBlocks : 250; } else if (ai.Focus.Target[1] != null && ai.Targets.TryGetValue(ai.Focus.Target[1], out priorityInfo) && priorityInfo.Target?.GetTopMostParent() == topEnt) { isPriroity = true; lastBlocks = totalBlocks < 250 ? totalBlocks : 250; } } if (totalBlocks < lastBlocks) { lastBlocks = totalBlocks; } var deck = GetDeck(ref target.BlockDeck, ref target.BlockPrevDeckLen, 0, totalBlocks, topBlocks, seed); var physics = ai.Session.Physics; var iGrid = topEnt as IMyCubeGrid; var gridPhysics = iGrid?.Physics; Vector3D targetLinVel = gridPhysics?.LinearVelocity ?? Vector3D.Zero; Vector3D targetAccel = (int)system.Values.HardPoint.AimLeadingPrediction > 1 ? info.Target.Physics?.LinearAcceleration ?? Vector3D.Zero : Vector3.Zero; var notSelfHit = false; var foundBlock = false; var blocksChecked = 0; var blocksSighted = 0; var weaponRangeSqr = turretCheck ? w.MaxTargetDistanceSqr : 0; for (int i = 0; i < totalBlocks; i++) { if (turretCheck && (blocksChecked > lastBlocks || isPriroity && (blocksSighted > 100 || blocksChecked > 50 && ai.Session.RandomRayCasts > 500 || blocksChecked > 25 && ai.Session.RandomRayCasts > 1000))) { break; } var card = deck[i]; var block = subSystemList[card]; if (!(block is IMyTerminalBlock) || block.MarkedForClose || checkPower && !block.IsWorking) { continue; } ai.Session.BlockChecks++; var blockPos = block.CubeGrid.GridIntegerToWorld(block.Position); double rayDist; if (turretCheck) { double distSqr; Vector3D.DistanceSquared(ref blockPos, ref weaponPos, out distSqr); if (distSqr > weaponRangeSqr) { continue; } blocksChecked++; ai.Session.CanShoot++; Vector3D predictedPos; if (!Weapon.CanShootTarget(w, blockPos, targetLinVel, targetAccel, out predictedPos)) { continue; } blocksSighted++; Vector3D?hitPos; if (!w.HitOther && GridIntersection.BresenhamGridIntersection(ai.MyGrid, ref weaponPos, ref blockPos, out hitPos, w.Comp.MyCube, w.Comp.Ai)) { continue; } ai.Session.RandomRayCasts++; IHitInfo hitInfo; physics.CastRay(weaponPos, blockPos, out hitInfo, 15, true); if (hitInfo == null || hitInfo.HitEntity != ai.MyGrid && (!w.System.Values.HardPoint.Other.MuzzleCheck || !w.MuzzleHitSelf())) { notSelfHit = true; } if (hitInfo?.HitEntity == null || hitInfo.HitEntity is MyVoxelBase || hitInfo.HitEntity == ai.MyGrid) { continue; } var hitGrid = hitInfo.HitEntity as MyCubeGrid; if (hitGrid != null) { if (hitGrid.MarkedForClose) { continue; } bool enemy; var bigOwners = hitGrid.BigOwners; if (bigOwners.Count == 0) { enemy = true; } else { var relationship = target.FiringCube.GetUserRelationToOwner(hitGrid.BigOwners[0]); enemy = relationship != MyRelationsBetweenPlayerAndBlock.Owner && relationship != MyRelationsBetweenPlayerAndBlock.FactionShare; } if (!enemy) { continue; } } Vector3D.Distance(ref weaponPos, ref blockPos, out rayDist); var shortDist = rayDist * (1 - hitInfo.Fraction); var origDist = rayDist * hitInfo.Fraction; var topEntId = block.GetTopMostParent().EntityId; target.Set(block, hitInfo.Position, shortDist, origDist, topEntId); foundBlock = true; break; } Vector3D.Distance(ref weaponPos, ref blockPos, out rayDist); target.Set(block, block.PositionComp.WorldAABB.Center, rayDist, rayDist, block.GetTopMostParent().EntityId); foundBlock = true; break; } if (turretCheck && !notSelfHit) { w.HitOther = true; } return(foundBlock); }
public static void GetBlocksInsideSphereFast(MyCubeGrid grid, ref BoundingSphereD sphere, bool checkDestroyed, ConcurrentCachingList <CubeAccel> blocks) { var radius = sphere.Radius; radius *= grid.GridSizeR; var center = grid.WorldToGridInteger(sphere.Center); var gridMin = grid.Min; var gridMax = grid.Max; double radiusSq = radius * radius; int radiusCeil = (int)Math.Ceiling(radius); int i, j, k; Vector3I max2 = Vector3I.Min(Vector3I.One * radiusCeil, gridMax - center); Vector3I min2 = Vector3I.Max(Vector3I.One * -radiusCeil, gridMin - center); for (i = min2.X; i <= max2.X; ++i) { for (j = min2.Y; j <= max2.Y; ++j) { for (k = min2.Z; k <= max2.Z; ++k) { if (i * i + j * j + k * k < radiusSq) { MyCube cube; var vector3I = center + new Vector3I(i, j, k); if (grid.TryGetCube(vector3I, out cube)) { var slim = (IMySlimBlock)cube.CubeBlock; if (slim.Position == vector3I) { if (checkDestroyed && slim.IsDestroyed) { continue; } blocks.Add(new CubeAccel { Block = slim, CubeExists = slim.FatBlock != null, Grid = (MyCubeGrid)slim.CubeGrid }); } } } } } } blocks.ApplyAdditions(); }
static ProfilerResultQueue() { _profilerResults = new ConcurrentQueue <ProfilerResult>(); _profilers = new ConcurrentCachingList <IProfiler>(); }
public MyDepositQuery() { m_onComplete = OnComplete; m_result = new ConcurrentCachingList <MyEntityOreDeposit>(); }