public override void UpdateBeforeSimulation() { if (MyAPIGateway.Utilities.IsDedicated) { return; } CurrentData = LeadingData.GetLeadingData(MyAPIGateway.Session?.Player?.Controller?.ControlledEntity?.Entity, CurrentData); if (CurrentData == null) { ClearGPS(); return; } _wasInTurretLastFrame = true; if (CurrentData.Ammo == null) { ClearGPS(); return; } float projectileSpeed = CurrentData.Ammo.DesiredSpeed; float projectileRange = CurrentData.Ammo.MaxTrajectory + 100; float projectileRangeSquared = projectileRange * projectileRange; foreach (IMyCubeGrid grid in _grids) { if (grid.Physics == null) { RemoveGPS(grid.EntityId); continue; } Vector3D gridLoc = grid.WorldAABB.Center; if (grid.EntityId == CurrentData.EntityId || Vector3D.DistanceSquared(gridLoc, CurrentData.Position) > projectileRangeSquared || !GridHasHostileOwners(grid)) { RemoveGPS(grid.EntityId); continue; } Vector3D interceptPoint = CalculateProjectileInterceptPosition( projectileSpeed, CurrentData.Velocity, CurrentData.Position, grid.Physics.LinearVelocity, gridLoc); AddGPS(grid.EntityId, interceptPoint); } }
public static LeadingData GetLeadingData(IMyEntity block, LeadingData current) { try { if (block is IMyLargeTurretBase) { IMyLargeTurretBase turret = block as IMyLargeTurretBase; return(new LeadingData() { EntityId = turret.EntityId, Position = turret.GetPosition(), Velocity = turret.CubeGrid.Physics.LinearVelocity, Ammo = (turret as IMyGunObject <MyGunBase>).GunBase.CurrentAmmoDefinition }); } else if (block is IMyShipController) { IMyShipController cockpit = block as IMyShipController; LeadingData data = new LeadingData { EntityId = block.EntityId, Velocity = cockpit.CubeGrid.Physics.LinearVelocity, Position = cockpit.CubeGrid.WorldAABB.Center }; if (current != null) { SerializableDefinitionId definition = GetWeaponDef(cockpit, ref data.toolbarIndex); if (current.toolbarIndex != data.toolbarIndex) { List <IMySlimBlock> blocks = new List <IMySlimBlock>(); cockpit.CubeGrid.GetBlocks(blocks, (b) => { return(b.BlockDefinition.Id == definition && b.FatBlock != null && b.FatBlock.Orientation.Forward == cockpit.Orientation.Forward); }); foreach (IMySlimBlock b in blocks) { data.offset += data.Position - b.FatBlock.PositionComp.WorldAABB.Center; if (data.Ammo == null) { data.Ammo = (b.FatBlock as IMyGunObject <MyGunBase>).GunBase.CurrentAmmoDefinition; } } data.offset = data.offset / blocks.Count; } else { data.offset = current.offset; data.Ammo = current.Ammo; } } data.Position = data.Position - data.offset; return(data); } } catch (Exception e) { // bad fix i know //MyLog.Default.Info(e.ToString()); } return(null); }