Skip to content

Commit

Permalink
turret fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rynchodon committed Apr 15, 2017
1 parent 602c948 commit 8660ec7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 111 deletions.
44 changes: 1 addition & 43 deletions Scripts/Utility/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ private static void appendWithBrackets(string append)
[System.Diagnostics.Conditional("DEBUG")]
public static void DebugNotify(string message, int disappearTimeMs = 2000, severity level = severity.TRACE)
{ Notify(message, disappearTimeMs, level); }
#if UNSTABLE

/// <summary>
/// For a safe way to display a message as a notification, not conditional.
/// </summary>
Expand Down Expand Up @@ -567,49 +567,7 @@ private static string fontForSeverity(severity level = severity.TRACE)
return MyFontEnum.White;
}
}
#else
/// <summary>
/// For a safe way to display a message as a notification, not conditional.
/// </summary>
/// <param name="message">the notification message</param>
/// <param name="disappearTimeMs">time on screen, in milliseconds</param>
/// <param name="level">severity level</param>
/// <returns>true iff the message was displayed</returns>
public static void Notify(string message, int disappearTimeMs = 2000, severity level = severity.TRACE)
{
if (Globals.WorldClosed)
return;

MyFontEnum font = fontForSeverity(level);
if (MyAPIGateway.Utilities != null)
MyAPIGateway.Utilities.TryInvokeOnGameThread(() => MyAPIGateway.Utilities.ShowNotification(message, disappearTimeMs, font));
//else
// log(severity.WARNING, "ShowNotificationDebug()", "MyAPIGateway.Utilities == null");
}

public enum severity : byte { OFF, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE, ALL }

private static MyFontEnum fontForSeverity(severity level = severity.TRACE)
{
switch (level)
{
case severity.TRACE:
return MyFontEnum.Blue;
case severity.DEBUG:
return MyFontEnum.DarkBlue;
case severity.INFO:
return MyFontEnum.Green;
case severity.WARNING:
return MyFontEnum.Red;
case severity.ERROR:
return MyFontEnum.Red;
case severity.FATAL:
return MyFontEnum.Red;
default:
return MyFontEnum.White;
}
}
#endif
/// <summary>
/// Append the relevant portion of the stack to a StringBuilder.
/// </summary>
Expand Down
30 changes: 28 additions & 2 deletions Scripts/Weapons/Guided/GuidedMissileLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using Rynchodon.AntennaRelay;
using Rynchodon.Utility.Network;
using Sandbox.Definitions;
Expand All @@ -27,8 +28,18 @@ private enum InitialTargetStatus : byte { NotReady, Golis, FromWeapon, NoStorage

#region Static

private static readonly FieldInfo MyMissile__m_missileAmmoDefinition;

private static ValueSync<bool, GuidedMissileLauncher> termControl_shoot;

static GuidedMissileLauncher()
{
Type MyMissileType = typeof(MyAmmoBase).Assembly.GetType("Sandbox.Game.Weapons.MyMissile", true);
MyMissile__m_missileAmmoDefinition = MyMissileType.GetField("m_missileAmmoDefinition", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (MyMissile__m_missileAmmoDefinition == null)
throw new NullReferenceException("MyMissile__m_missileAmmoDefinition");
}

[OnWorldLoad]
private static void Init()
{
Expand All @@ -51,8 +62,10 @@ private static void Entities_OnEntityAdd(IMyEntity obj)
{
if (obj.IsMissile())
{
MyDefinitionId definition = ((MyMissileAmmoDefinition)MyMissile__m_missileAmmoDefinition.GetValue(obj)).Id;
Logger.TraceLog("missile definition: " + definition);
foreach (GuidedMissileLauncher launcher in Registrar.Scripts<GuidedMissileLauncher>())
if (launcher.MissileBelongsTo(obj))
if (launcher.MissileBelongsTo(obj, ref definition))
return;
Logger.TraceLog("No launcher for: " + obj.nameWithId());
}
Expand Down Expand Up @@ -94,7 +107,10 @@ private static void HijackShoot()
};
shoot.Setter = (block, value) => {
if (IsGuidedMissileLauncher(block))
{
Logger.TraceLog("Set shoot: " + value);
termControl_shoot.SetValue(block, value);
}
else
originalSetter(block, value);
};
Expand Down Expand Up @@ -187,13 +203,20 @@ public void Update1()
LockAndShoot();
}

private bool MissileBelongsTo(IMyEntity missile)
private bool MissileBelongsTo(IMyEntity missile, ref MyDefinitionId definition)
{
if (!_isShooting)
{
myLogger.traceLog("Not mine, not shooting");
return false;
}
if (definition != loadedAmmo.MissileDefinition.Id)
{
myLogger.traceLog("Not mine, not my loaded ammo definition");
myLogger.traceLog("defn: " + definition);
myLogger.traceLog("loaded: " + loadedAmmo.MissileDefinition.Id);
return false;
}
Vector3D local = Vector3D.Transform(missile.GetPosition(), CubeBlock.WorldMatrixNormalizedInv);
if (MissileSpawnBox.Contains(local) != ContainmentType.Contains)
{
Expand Down Expand Up @@ -321,7 +344,10 @@ private void CheckCooldown()
private void LockAndShoot()
{
if (m_onCooldown || m_onGameCooldown)
{
myLogger.traceLog("on cooldown");
return;
}

if (!_shootCluster)
{
Expand Down
41 changes: 12 additions & 29 deletions Scripts/Weapons/Turret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Collections.Generic;
using Sandbox.Definitions;
using Sandbox.Game.Entities;
using Sandbox.Game.Weapons;
using Sandbox.ModAPI;
using Sandbox.ModAPI.Interfaces;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.ModAPI;
Expand All @@ -16,9 +16,6 @@ namespace Rynchodon.Weapons
public sealed class Turret : WeaponTargeting
{

/// <summary>vanilla property</summary>
private static ITerminalProperty<bool> TP_TargetMissiles, TP_TargetMeteors, TP_TargetCharacters, TP_TargetMoving, TP_TargetLargeGrids, TP_TargetSmallGrids, TP_TargetStations, TP_TargetNeutrals;

private readonly MyEntitySubpart m_barrel;
/// <summary>limits to determine whether or not a turret can face a target</summary>
private readonly float minElevation, maxElevation, minAzimuth, maxAzimuth;
Expand All @@ -36,21 +33,6 @@ public Turret(IMyCubeBlock block)
: base(block)
{
myLogger = new Logger(block);
//Registrar.Add(CubeBlock, this);

if (TP_TargetMissiles == null)
{
myLogger.debugLog("Filling Terminal Properties", Logger.severity.INFO);
IMyTerminalBlock term = CubeBlock as IMyTerminalBlock;
TP_TargetMissiles = term.GetProperty("TargetMissiles").AsBool();
TP_TargetMeteors = term.GetProperty("TargetMeteors").AsBool();
TP_TargetCharacters = term.GetProperty("TargetCharacters").AsBool();
TP_TargetMoving = term.GetProperty("TargetMoving").AsBool();
TP_TargetLargeGrids = term.GetProperty("TargetLargeShips").AsBool();
TP_TargetSmallGrids = term.GetProperty("TargetSmallShips").AsBool();
TP_TargetStations = term.GetProperty("TargetStations").AsBool();
TP_TargetNeutrals = term.GetProperty("TargetNeutrals").AsBool();
}

// definition limits
MyLargeTurretBaseDefinition definition = CubeBlock.GetCubeBlockDefinition() as MyLargeTurretBaseDefinition;
Expand Down Expand Up @@ -89,14 +71,15 @@ public Turret(IMyCubeBlock block)
/// </summary>
protected override void Update100_Options_TargetingThread(TargetingOptions Options)
{
SetFlag(TP_TargetMissiles, TargetType.Missile);
SetFlag(TP_TargetMeteors, TargetType.Meteor);
SetFlag(TP_TargetCharacters, TargetType.Character);
SetFlag(TP_TargetMoving, TargetType.Moving);
SetFlag(TP_TargetLargeGrids, TargetType.LargeGrid);
SetFlag(TP_TargetSmallGrids, TargetType.SmallGrid);
SetFlag(TP_TargetStations, TargetType.Station);
if (TP_TargetNeutrals.GetValue(CubeBlock))
MyLargeTurretBase turret = (MyLargeTurretBase)CubeBlock;
SetFlag(turret.TargetMissiles, TargetType.Missile);
SetFlag(turret.TargetMeteors, TargetType.Meteor);
SetFlag(turret.TargetCharacters, TargetType.Character);
SetFlag(turret.TargetLargeGrids, TargetType.LargeGrid);
SetFlag(turret.TargetSmallGrids, TargetType.SmallGrid);
SetFlag(turret.TargetStations, TargetType.Station);

if (turret.TargetNeutrals)
Options.Flags &= ~TargetingFlags.IgnoreOwnerless;
else
Options.Flags |= TargetingFlags.IgnoreOwnerless;
Expand All @@ -106,9 +89,9 @@ protected override void Update100_Options_TargetingThread(TargetingOptions Optio
//myLogger.debugLog("CanTarget = " + Options.CanTarget, "TargetOptionsFromTurret()");
}

private void SetFlag(ITerminalProperty<bool> prop, TargetType typeFlag)
private void SetFlag(bool enable, TargetType typeFlag)
{
if (prop.GetValue(CubeBlock))
if (enable)
Options.CanTarget |= typeFlag;
else
Options.CanTarget &= ~typeFlag;
Expand Down
73 changes: 36 additions & 37 deletions Scripts/Weapons/WeaponTargeting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Sandbox.Game.Weapons;
using Sandbox.Graphics.GUI;
using Sandbox.ModAPI;
using Sandbox.ModAPI.Interfaces;
using Sandbox.ModAPI.Interfaces.Terminal;
using SpaceEngineers.Game.Weapons.Guns;
using VRage.Game;
Expand Down Expand Up @@ -83,20 +84,16 @@ public StaticVariables()
TerminalControlHelper.EnsureTerminalControlCreated<MySmallMissileLauncher>();
TerminalControlHelper.EnsureTerminalControlCreated<MySmallMissileLauncherReload>();

//// find the current position of shoot On/Off
//foreach (ITerminalControl control in MyTerminalControlFactory.GetControls(typeof(MyUserControllableGun)))
//{
// ++ControlsIndex;
// if (control.Id == "Shoot")
// break;
//}

//Logger.TraceLog("controls index: " + ControlsIndex);

termControl_targetFlag = new FlagsValueSync<TargetingFlags, WeaponTargeting>("TargetFlag", "value_termControl_targetFlag");
termControl_targetType = new FlagsValueSync<TargetType, WeaponTargeting>("TargetType", "value_termControl_targetType");
termControl_weaponFlags = new FlagsValueSync<WeaponFlags, WeaponTargeting>("WeaponFlags", "value_termControl_weaponFlags");

{
MyTerminalControlOnOffSwitch<MyUserControllableGun> targetMoving = new MyTerminalControlOnOffSwitch<MyUserControllableGun>("ArmsTargetMoving", MyStringId.GetOrCompute("Target moving"), MyStringId.GetOrCompute("ARMS will target fast approaching objects"));
termControl_targetType.AddControl(targetMoving, TargetType.Moving);
AddControl(targetMoving, Visibility.Turret);
}

AddControl(new MyTerminalControlSeparator<MyUserControllableGun>());

{
Expand Down Expand Up @@ -199,7 +196,6 @@ public StaticVariables()
}

CloneTurretControl_OnOff("TargetMeteors", TargetType.Meteor);
CloneTurretControl_OnOff("TargetMoving", TargetType.Moving);
CloneTurretControl_OnOff("TargetMissiles", TargetType.Missile);
CloneTurretControl_OnOff("TargetSmallShips", TargetType.SmallGrid);
CloneTurretControl_OnOff("TargetLargeShips", TargetType.LargeGrid);
Expand All @@ -218,6 +214,12 @@ public StaticVariables()
}
}

{
MyTerminalControlOnOffSwitch<MyUserControllableGun> targetMoving = new MyTerminalControlOnOffSwitch<MyUserControllableGun>("ArmsTargetMoving", MyStringId.GetOrCompute("Target moving"), MyStringId.GetOrCompute("ARMS will target fast approaching objects"));
termControl_targetType.AddControl(targetMoving, TargetType.Moving);
AddControl(targetMoving, Visibility.Fixed);
}

Logger.TraceLog("initialized");
//Controls.TrimExcess();
//MyAPIGateway.TerminalControls.CustomControlGetter += CustomControlGetter;
Expand Down Expand Up @@ -472,7 +474,7 @@ private static void OnGpsListItemSelected(IMyTerminalBlock block, List<MyGuiCont
private InterpreterWeapon Interpreter;

private bool FireWeapon;
private bool IsFiringWeapon { get { return CubeBlock.IsShooting; } }
//private bool IsFiringWeapon { get { return CubeBlock.IsShooting; } }
private Control value_currentControl;

/// <summary>First item is target, second is the weapon, followed by custom items.</summary>
Expand Down Expand Up @@ -572,7 +574,6 @@ public Control CurrentControl
}
}

#if UNSTABLE
/*
* Bug in Space Engineers breaks Shoot_On, Shoot_Off, SetShooting, SetTarget, and TrackTarget for turrets.
*/
Expand Down Expand Up @@ -601,42 +602,40 @@ private void RestoreDefaultTargeting()
}
}

private static ITerminalProperty<bool> m_shootProperty;

private bool GetShootProp()
{
if (m_shootProperty == null)
m_shootProperty = CubeBlock.GetProperty("Shoot").AsBool();
return m_shootProperty.GetValue(CubeBlock);
}

private void ShootOn()
{
if (GetShootProp())
return;

myLogger.traceLog("Opening fire");
if (myTurret != null)
{
TurretNeedsUpdate(true);
myTurret.SetTarget(ProjectilePosition() + CurrentTarget.FiringDirection.Value * 1000f);
}
((MyUserControllableGun)CubeBlock).SetShooting(true);
m_shootProperty.SetValue(CubeBlock, true);
}

private void ShootOff()
{
if (!GetShootProp())
return;

myLogger.traceLog("Holding fire");
if (myTurret != null)
TurretNeedsUpdate(false);
((MyUserControllableGun)CubeBlock).SetShooting(false);
}
#else
private void RestoreDefaultTargeting()
{
if (myTurret != null)
myTurret.ResetTargetingToDefault();
m_shootProperty.SetValue(CubeBlock, false);
}

private void ShootOn()
{
// has to be this way for guided missiles
((MyUserControllableGun)CubeBlock).SetShooting(true);
}

private void ShootOff()
{
// has to be this way for guided missiles
((MyUserControllableGun)CubeBlock).SetShooting(false);
}
#endif

/// <summary>Checks that it is possible to control the weapon: working, not in use, etc.</summary>
public bool CanControl
{
Expand Down Expand Up @@ -721,16 +720,16 @@ public void Update_Targeting()
try
{
GameThreadActions.DequeueAll(action => action.Invoke());
if (CurrentControl != Control.Off && FireWeapon != IsFiringWeapon && MyAPIGateway.Multiplayer.IsServer)
if (CurrentControl != Control.Off && MyAPIGateway.Multiplayer.IsServer)
{
if (FireWeapon)
{
myLogger.traceLog("Opening fire");
//myLogger.traceLog("Opening fire");
ShootOn();
}
else
{
myLogger.traceLog("Holding fire");
//myLogger.traceLog("Holding fire");
ShootOff();
}
}
Expand Down

0 comments on commit 8660ec7

Please sign in to comment.