public void Init(MyObjectBuilder_GunBase objectBuilder, MyDefinitionId weaponDefinitionId, IMyGunBaseUser gunBaseUser) { m_user = gunBaseUser; m_weaponProperties = new MyWeaponPropertiesWrapper(weaponDefinitionId); //MyDebug.AssertDebug(m_weaponProperties.AmmoMagazinesCount > 0, "Weapon definition has no ammo magazines attached."); // object builder area - Start m_remainingAmmos = new Dictionary <MyDefinitionId, int>(WeaponProperties.AmmoMagazinesCount); if (objectBuilder != null) { MyDefinitionId ammoMagazineDef = new MyDefinitionId(typeof(MyObjectBuilder_AmmoMagazine), objectBuilder.CurrentAmmoMagazineName); if (m_weaponProperties.CanChangeAmmoMagazine(ammoMagazineDef)) { CurrentAmmo = objectBuilder.RemainingAmmo; m_weaponProperties.ChangeAmmoMagazine(ammoMagazineDef); } else { if (WeaponProperties.WeaponDefinition.HasAmmoMagazines()) { m_weaponProperties.ChangeAmmoMagazine(m_weaponProperties.WeaponDefinition.AmmoMagazinesId[0]); } } foreach (var remainingAmmo in objectBuilder.RemainingAmmosList) { m_remainingAmmos.Add(new MyDefinitionId(typeof(MyObjectBuilder_AmmoMagazine), remainingAmmo.SubtypeName), remainingAmmo.Amount); } LastShootTime = new DateTime(objectBuilder.LastShootTime); } else { if (WeaponProperties.WeaponDefinition.HasAmmoMagazines()) { m_weaponProperties.ChangeAmmoMagazine(m_weaponProperties.WeaponDefinition.AmmoMagazinesId[0]); } LastShootTime = new DateTime(0); } // object builder area - END if (m_user.AmmoInventory != null) { if (m_user.PutConstraint()) { MyDebug.AssertDebug(!String.IsNullOrEmpty(m_user.ConstraintDisplayName), "Display name of weapon is empty."); m_user.AmmoInventory.Constraint = CreateAmmoInventoryConstraints(m_user.ConstraintDisplayName); } RefreshAmmunitionAmount(); } if (m_user.Weapon != null) { m_user.Weapon.OnClosing += Weapon_OnClosing; MySyncGunBase.AmmoCountChanged += MySyncGunBase_AmmoCountChanged; } }
/** * Get the owner of a player Weapon */ private static bool GetPlayerByWeapon( IMyGunBaseUser gun, out IMyPlayer owner, out IMyFaction owningFaction) { owner = MyAPIGateway.Players.GetPlayerByID(gun.OwnerId); owningFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(gun.OwnerId); return owner != null || owningFaction != null; }
private void Unregister(IMyGunBaseUser gun, bool remove = true) { var block = (IMyCubeBlock)gun; block.IsWorkingChanged -= Block_IsWorkingChanged; if (remove) { _turretsForUpdate.Remove(gun); _turretsSleeping.Remove(gun); } }
private void Register(IMyGunBaseUser gun) { var block = (IMyCubeBlock)gun; var weapon = WeaponShortcuts.GetWeaponDefinition(block); if (weapon == null || !weapon.HasAmmoMagazines()) { return; } block.IsWorkingChanged += Block_IsWorkingChanged; Block_IsWorkingChanged(block); }
static bool IsDamagedByPlayer(IMyGunBaseUser Gun, out IMyPlayer Damager) { Damager = null; try { Damager = MyAPIGateway.Players.GetPlayerByID(Gun.OwnerId); return(Damager != null ? !Damager.IsBot : false); } catch (Exception Scrap) { AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check gun owner crashed", Scrap)); return(false); } }
private static bool IsDamagedByPlayer(IMyGunBaseUser gun, out IMyPlayer damager) { damager = null; try { damager = MyAPIGateway.Players.GetPlayerById(gun.OwnerId); //AISessionCore.DebugWrite($"GunDamage.IsDamagedByPlayer", $"Getting player from gun. ID: {Gun.OwnerId}, player: {(Damager != null ? Damager.DisplayName : "null")}", false); return(!damager?.IsBot ?? false); } catch (Exception scrap) { AiSessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check gun owner crashed", scrap)); return(false); } }
// Add new projectile to the list public static void Add(MyProjectileAmmoDefinition ammoDefinition, Vector3D origin, Vector3 initialVelocity, Vector3 directionNormalized, IMyGunBaseUser user) { //MyProjectile newProjectile = m_projectiles.Allocate(); MyProjectile newProjectile; m_projectiles.AllocateOrCreate(out newProjectile); newProjectile.Start( ammoDefinition, user.IgnoreEntity, origin, initialVelocity, directionNormalized, user.Weapon ); newProjectile.OwnerEntity = user.Owner != null ? user.Owner : user.IgnoreEntity; }
public static MyPlayer GetPlayerFromWeapon(IMyGunBaseUser gunUser) { if (gunUser == null) { Debug.Fail("Invalid argument"); return(null); } MyCharacter gunHolder = gunUser.Owner as MyCharacter; if (gunHolder != null) { return(GetPlayerFromCharacter(gunHolder)); } return(null); }
private void InitUpdate() { tool = (IMyGunBaseUser)Entity; owner = tool.Owner as IMyCharacter; if (Mod == null || owner == null || owner.Physics == null) { return; } var rifle = (IMyAutomaticRifleGun)Entity; if (rifle.GunBase == null) { throw new NullReferenceException($"{GetType().Name} :: Rifle.GunBase == null; ent={rifle}; owner={rifle.Owner}/{rifle.OwnerIdentityId.ToString()}"); } // HACK: prevent tool from ever reloading, which breaks animations for other things if (rifle.GunBase.CurrentAmmo <= 0) { rifle.GunBase.CurrentAmmo = 1; } first = false; // don't move this up because it needs to repeat until mod and character are available for a valid check // checking if local player is holding it if (owner != null && MyAPIGateway.Session.Player != null && tool.OwnerId == MyAPIGateway.Session.Player.IdentityId) { Mod.EquipTool((IMyAutomaticRifleGun)Entity); } if (!Entity.TryGetSubpart(SUBPART_NAME, out subpart)) { NeedsUpdate = MyEntityUpdateEnum.NONE; return; } prevMatrixInv = Entity.WorldMatrix; }
public void Init(MyObjectBuilder_GunBase objectBuilder, MyCubeBlockDefinition cubeBlockDefinition, IMyGunBaseUser gunBaseUser) { if (cubeBlockDefinition is MyWeaponBlockDefinition) { MyWeaponBlockDefinition weaponBlockDefinition = cubeBlockDefinition as MyWeaponBlockDefinition; Init(objectBuilder, weaponBlockDefinition.WeaponDefinitionId, gunBaseUser); } else { // Backward compatibility MyDefinitionId weaponDefinitionId = GetBackwardCompatibleDefinitionId(cubeBlockDefinition.Id.TypeId); Init(objectBuilder, weaponDefinitionId, gunBaseUser); } }
public void Init(MyObjectBuilder_GunBase objectBuilder, MyDefinitionId weaponDefinitionId, IMyGunBaseUser gunBaseUser) { m_user = gunBaseUser; m_weaponProperties = new MyWeaponPropertiesWrapper(weaponDefinitionId); //MyDebug.AssertDebug(m_weaponProperties.AmmoMagazinesCount > 0, "Weapon definition has no ammo magazines attached."); // object builder area - Start m_remainingAmmos = new Dictionary<MyDefinitionId, int>(WeaponProperties.AmmoMagazinesCount); if (objectBuilder != null) { MyDefinitionId ammoMagazineDef = new MyDefinitionId(typeof(MyObjectBuilder_AmmoMagazine), objectBuilder.CurrentAmmoMagazineName); if (m_weaponProperties.CanChangeAmmoMagazine(ammoMagazineDef)) { CurrentAmmo = objectBuilder.RemainingAmmo; m_weaponProperties.ChangeAmmoMagazine(ammoMagazineDef); } else { if (WeaponProperties.WeaponDefinition.HasAmmoMagazines()) m_weaponProperties.ChangeAmmoMagazine(m_weaponProperties.WeaponDefinition.AmmoMagazinesId[0]); } foreach (var remainingAmmo in objectBuilder.RemainingAmmosList) { m_remainingAmmos.Add(new MyDefinitionId(typeof(MyObjectBuilder_AmmoMagazine), remainingAmmo.SubtypeName), remainingAmmo.Amount); } LastShootTime = new DateTime(objectBuilder.LastShootTime); } else { if (WeaponProperties.WeaponDefinition.HasAmmoMagazines()) m_weaponProperties.ChangeAmmoMagazine(m_weaponProperties.WeaponDefinition.AmmoMagazinesId[0]); LastShootTime = new DateTime(0); } // object builder area - END if (m_user.AmmoInventory != null) { if (m_user.PutConstraint()) { MyDebug.AssertDebug(!String.IsNullOrEmpty(m_user.ConstraintDisplayName), "Display name of weapon is empty."); m_user.AmmoInventory.Constraint = CreateAmmoInventoryConstraints(m_user.ConstraintDisplayName); } RefreshAmmunitionAmount(); } if (m_user.Weapon != null) { m_user.Weapon.OnClosing += Weapon_OnClosing; MySyncGunBase.AmmoCountChanged += MySyncGunBase_AmmoCountChanged; } }
public static bool PutConstraint(this IMyGunBaseUser obj) { return(!string.IsNullOrEmpty(obj.ConstraintDisplayName)); }
private void CombatDamageHandler(object target, MyDamageInformation info) { if (info.Amount == 0) { return; } CombatDescription log = new CombatDescription { Damage = info.Amount, Type = info.Type.String, Timestamp = Tools.DateTime }; if (target is IMySlimBlock) { IMySlimBlock slim = target as IMySlimBlock; log.Integrity = slim.Integrity; log.VictimGridId = slim.CubeGrid.EntityId; log.VictimGridBlockId = Tools.GetBlockId(slim.Position); } else if (target is IMyCharacter) { IMyCharacter character = target as IMyCharacter; // characters keep getting hit after death we dont want to log that if (character.Name == AlreadyLogged) { return; } if (character.Integrity <= 0) { character.Name = AlreadyLogged; } log.VictimEntityId = character.EntityId; log.Integrity = character.Integrity; } else if (target is IMyFloatingObject) { IMyFloatingObject obj = (target as IMyFloatingObject); log.VictimEntityId = obj.EntityId; log.Integrity = obj.Integrity; } else { ActivityCollector.Log.Error($"Unrecognised Victim {target.GetType()}"); } IMyEntity entity = MyAPIGateway.Entities.GetEntityById(info.AttackerId); if (entity == null) { } else if (entity is IMyCubeBlock) { IMyCubeBlock cube = entity as IMyCubeBlock; log.AttackerGridId = cube.CubeGrid.EntityId; log.AttackerEntityId = cube.EntityId; } else if (entity is IMyCharacter) { try { IMyCharacter character = entity as IMyCharacter; if (character.Name != null) // hacks continued { long missileId = -1; long.TryParse(entity.Name, out missileId); if (missileId != -1) { IMyEntity missileEntity = MyAPIGateway.Entities.GetEntityById(missileId); if (missileEntity != null) { if (missileEntity is IMyCubeGrid) { IMyCubeGrid grid = missileEntity as IMyCubeGrid; log.AttackerGridId = grid.EntityId; } else { IMyCubeBlock cube = missileEntity as IMyCubeBlock; log.AttackerGridId = cube.CubeGrid.EntityId; log.AttackerGridBlockId = Tools.GetBlockId(cube.Position); } } else { ActivityCollector.Log.Error($"missiles parent grid was not found!"); } } else { ActivityCollector.Log.Error($"Entity of type {entity.GetType()} failed to parse id {entity.Name}"); } } else { log.AttackerEntityId = character.EntityId; } } catch (Exception e) { ActivityCollector.Log.Error(e); } } else if (entity is IMyGunBaseUser) // player tools { IMyGunBaseUser gun = entity as IMyGunBaseUser; log.AttackerEntityId = gun.Weapon.EntityId; } else if (entity is IMyEngineerToolBase) { IMyEngineerToolBase toolbase = entity as IMyEngineerToolBase; log.AttackerEntityId = toolbase.EntityId; } else if (entity is IMySlimBlock) { IMySlimBlock slim = entity as IMySlimBlock; log.AttackerGridId = slim.CubeGrid.EntityId; if (slim.FatBlock != null) { log.AttackerEntityId = slim.FatBlock.EntityId; } else { } } else if (entity is IMyCubeGrid) { IMyCubeGrid grid = entity as IMyCubeGrid; log.AttackerGridId = grid.EntityId; } else if (entity is MyVoxelBase) { MyVoxelBase voxel = entity as MyVoxelBase; log.AttackerEntityId = entity.EntityId; } else if (entity.GetType().Name == "MyMissile") { long missileId = -1; long.TryParse(entity.Name, out missileId); if (missileId != -1) { IMyEntity missileEntity = MyAPIGateway.Entities.GetEntityById(missileId); if (missileEntity != null) { if (missileEntity is IMyCubeGrid) { IMyCubeGrid grid = missileEntity as IMyCubeGrid; log.AttackerGridId = grid.EntityId; } else { IMyCubeBlock cube = missileEntity as IMyCubeBlock; log.AttackerGridId = cube.CubeGrid.EntityId; log.AttackerEntityId = cube.EntityId; } } else { ActivityCollector.Log.Error($"Missles parent grid was not found!"); } } else { ActivityCollector.Log.Error($"Entity of type {entity.GetType()} failed to parse id {entity.Name}"); } } else { ActivityCollector.Log.Error($"Unknown attacker entity of type: {entity.GetType()}"); } SQLQueryData.WriteToDatabase(log); }
// Add new projectile to the list public static void Add(MyProjectileAmmoDefinition ammoDefinition, Vector3D origin, Vector3 initialVelocity, Vector3 directionNormalized, IMyGunBaseUser user, MyEntity owner) { //MyProjectile newProjectile = m_projectiles.Allocate(); MyProjectile newProjectile; m_projectiles.AllocateOrCreate(out newProjectile); newProjectile.Start( ammoDefinition, user.IgnoreEntities, origin, initialVelocity, directionNormalized, user.Weapon ); newProjectile.OwnerEntity = user.Owner ?? (user.IgnoreEntities != null && user.IgnoreEntities.Length > 0 ? user.IgnoreEntities[0] : null); newProjectile.OwnerEntityAbsolute = owner; }