Skip to content

Commit

Permalink
Throw the bugs down the well, so our software can be fun.
Browse files Browse the repository at this point in the history
  • Loading branch information
godarklight committed Mar 16, 2018
1 parent 8d9c128 commit 89effa5
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -6,4 +6,4 @@
*.suo
*.csproj.user
saves/
Client/DarkMultiPlayer/
.vs/
2 changes: 1 addition & 1 deletion Client/Client.csproj
Expand Up @@ -101,7 +101,7 @@
<Compile Include="VesselRecorder.cs" />
<Compile Include="PosistionStatistics.cs" />
<Compile Include="VesselCtrlUpdate.cs" />
<Compile Include="VesselPackedUpdater.cs" />
<Compile Include="VesselInterFrameUpdater.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\DarkMultiPlayerCommon.csproj">
Expand Down
4 changes: 2 additions & 2 deletions Client/DMPGame.cs
Expand Up @@ -45,7 +45,7 @@ public class DMPGame
public readonly AsteroidWorker asteroidWorker;
public readonly VesselRecorder vesselRecorder;
public readonly PosistionStatistics posistionStatistics;
public readonly VesselPackedUpdater vesselPackedUpdater;
public readonly VesselInterFrameUpdater vesselPackedUpdater;
private DMPModInterface dmpModInterface;

public DMPGame(Settings dmpSettings, UniverseSyncCache universeSyncCache, ModWorker modWorker, ConnectionWindow connectionWindow, DMPModInterface dmpModInterface, ToolbarSupport toolbarSupport, OptionsWindow optionsWindow)
Expand All @@ -64,7 +64,7 @@ public DMPGame(Settings dmpSettings, UniverseSyncCache universeSyncCache, ModWor
this.partKiller = new PartKiller(lockSystem);
this.dynamicTickWorker = new DynamicTickWorker(this, networkWorker);
this.kerbalReassigner = new KerbalReassigner();
this.vesselPackedUpdater = new VesselPackedUpdater(lockSystem, posistionStatistics);
this.vesselPackedUpdater = new VesselInterFrameUpdater(lockSystem, posistionStatistics);
this.vesselWorker = new VesselWorker(this, dmpSettings, modWorker, lockSystem, networkWorker, configNodeSerializer, dynamicTickWorker, kerbalReassigner, partKiller, posistionStatistics, vesselPackedUpdater);
this.scenarioWorker = new ScenarioWorker(this, vesselWorker, configNodeSerializer, networkWorker);
this.playerStatusWorker = new PlayerStatusWorker(this, dmpSettings, vesselWorker, lockSystem, networkWorker);
Expand Down
91 changes: 42 additions & 49 deletions Client/KerbalReassigner.cs
Expand Up @@ -7,9 +7,10 @@ namespace DarkMultiPlayer
public class KerbalReassigner
{
private bool registered = false;
private static string[] femaleNames;
private static string[] femaleNamesPrefix;
private static string[] femaleNamesPostfix;
private static bool inited = false;
private static bool broken = false;
private static HashSet<string> femaleNames = new HashSet<string>();
private static HashSet<string> femaleNamesSpecial = new HashSet<string>();
private Dictionary<Guid, List<string>> vesselToKerbal = new Dictionary<Guid, List<string>>();
private Dictionary<string, Guid> kerbalToVessel = new Dictionary<string, Guid>();

Expand Down Expand Up @@ -183,82 +184,74 @@ public void CreateKerbalIfMissing(string kerbalName, Guid vesselID)
if (!HighLogic.CurrentGame.CrewRoster.Exists(kerbalName))
{
ProtoCrewMember pcm = CrewGenerator.RandomCrewMemberPrototype(ProtoCrewMember.KerbalType.Crew);
HighLogic.CurrentGame.CrewRoster.AddCrewMember(pcm);
pcm.ChangeName(kerbalName);
pcm.rosterStatus = ProtoCrewMember.RosterStatus.Assigned;
HighLogic.CurrentGame.CrewRoster.AddCrewMember(pcm);
DarkLog.Debug("Created kerbal " + pcm.name + " for vessel " + vesselID + ", Kerbal was missing");
}
}

//Better not use a bool for this and enforce the gender binary on xir!
public static ProtoCrewMember.Gender GetKerbalGender(string kerbalName)
{
string trimmedName = kerbalName;
if (kerbalName.Contains(" Kerman"))
if (broken)
{
trimmedName = kerbalName.Substring(0, kerbalName.IndexOf(" Kerman"));
DarkLog.Debug("(KerbalReassigner) Trimming name to '" + trimmedName + "'");
return ProtoCrewMember.Gender.Male;
}
foreach (FieldInfo fi in typeof(CrewGenerator).GetFields(BindingFlags.Static | BindingFlags.NonPublic))

if (!inited)
{
if (fi.FieldType == typeof(string[]))
string[] femaleNamesArray = null;
string[] femaleNamesSpecialArray = null;
FieldInfo femaleNamesArrayFI = typeof(CrewGenerator).GetField("kerbalNamesFemale", BindingFlags.NonPublic | BindingFlags.Static);
FieldInfo femaleNamesSpecialFI = typeof(CrewGenerator).GetField("specialNamesFemale", BindingFlags.NonPublic | BindingFlags.Static);
if (femaleNamesArrayFI != null && femaleNamesSpecialFI != null)
{
femaleNamesArray = (string[])femaleNamesArrayFI.GetValue(null);
femaleNamesSpecialArray = (string[])femaleNamesSpecialFI.GetValue(null);
}
inited = true;
if (femaleNamesArray == null || femaleNamesSpecialArray == null)
{
broken = true;
DarkLog.Debug("Kerbal Gender Assigner is BROKEN!");
return ProtoCrewMember.Gender.Male;
}
else
{
string[] fieldValue = (string[])fi.GetValue(null);
foreach (string entry in fieldValue)
foreach (string name in femaleNamesArray)
{
if (entry == "Alice")
{
DarkLog.Debug("Found female single names!");
femaleNames = fieldValue;
break;
}
if (entry == "Aga")
{
DarkLog.Debug("Found female prefixes!");
femaleNamesPrefix = fieldValue;
break;
}
if (entry == "alla")
{
DarkLog.Debug("Found female postfixes!");
femaleNamesPostfix = fieldValue;
break;
}
femaleNames.Add(name);
}

foreach (string name in femaleNamesSpecialArray)
{
femaleNamesSpecial.Add(name);
}
}
}
if (femaleNames == null || femaleNamesPrefix == null || femaleNamesPostfix == null)

string trimmedName = kerbalName;
if (kerbalName.Contains(" "))
{
DarkLog.Debug("Kerbal Gender Assigner is BROKEN!");
return ProtoCrewMember.Gender.Male;
trimmedName = kerbalName.Substring(0, kerbalName.LastIndexOf(" "));
}

if (trimmedName == "Valentina")
{
return ProtoCrewMember.Gender.Female;
}

foreach (string name in femaleNames)
if (femaleNames.Contains(trimmedName))
{
if (name == trimmedName)
{
return ProtoCrewMember.Gender.Female;
}
return ProtoCrewMember.Gender.Female;
}

foreach (string prefixName in femaleNamesPrefix)
if (femaleNamesSpecial.Contains(trimmedName))
{
if (trimmedName.StartsWith(prefixName))
{
foreach (string postfixName in femaleNamesPostfix)
{
if (trimmedName == prefixName + postfixName)
{
return ProtoCrewMember.Gender.Female;
}
}
}
return ProtoCrewMember.Gender.Female;
}

return ProtoCrewMember.Gender.Male;
}

Expand Down
Expand Up @@ -3,14 +3,14 @@
using DarkMultiPlayer;
namespace DarkMultiPlayer
{
public class VesselPackedUpdater
public class VesselInterFrameUpdater
{
private PosistionStatistics posistionStatistics;
private LockSystem lockSystem;
private Dictionary<Guid, VesselUpdate> currentVesselUpdates = new Dictionary<Guid, VesselUpdate>();
private Dictionary<Guid, VesselUpdate> previousVesselUpdates = new Dictionary<Guid, VesselUpdate>();

public VesselPackedUpdater (LockSystem lockSystem, PosistionStatistics posistionStatistics)
public VesselInterFrameUpdater (LockSystem lockSystem, PosistionStatistics posistionStatistics)
{
this.lockSystem = lockSystem;
this.posistionStatistics = posistionStatistics;
Expand Down
2 changes: 0 additions & 2 deletions Client/VesselRecorder.cs
Expand Up @@ -87,8 +87,6 @@ public void StartPlayback()
{
while (fs.Position < fs.Length)
{
DarkLog.Debug(fs.Position.ToString());
DarkLog.Debug(fs.Length.ToString());
messagesLoaded++;
byte[] headerBytes = new byte[8];
fs.Read(headerBytes, 0, 8);
Expand Down
94 changes: 33 additions & 61 deletions Client/VesselUpdate.cs
Expand Up @@ -144,15 +144,10 @@ public void Apply(PosistionStatistics posistionStatistics, Dictionary<Guid, Vess
//DarkLog.Debug("ApplyVesselUpdate - updateBody not found");
return;
}

Quaternion normalRotate = Quaternion.identity;
Vector3 oldVelocity = Vector3.zero;
if (!updateVessel.packed)
{
oldVelocity = updateVessel.rootPart.rb.velocity;
}
double distanceError = 0;
double velocityError = 0;
double rotationalError = 0;
Vector3 oldPos = updateVessel.GetWorldPos3D();
Vector3 oldVelocity = updateVessel.orbitDriver.orbit.GetVel();
//Position/Velocity
if (isSurfaceUpdate)
{
Expand Down Expand Up @@ -191,47 +186,20 @@ public void Apply(PosistionStatistics posistionStatistics, Dictionary<Guid, Vess
//Displacement = v0*t + 1/2at^2.
positionFudge = (updateVelocity * planetariumDifference) + (0.5d * updateAcceleration * planetariumDifference * planetariumDifference);
}

Vector3d updatePostion = updateBody.GetWorldSurfacePosition(position[0], position[1], position[2] + altitudeFudge) + positionFudge;

//Posisional Error Tracking
distanceError = Vector3d.Distance(updatePostion, updateVessel.GetWorldPos3D());

double latitude = updateBody.GetLatitude(updatePostion);
double longitude = updateBody.GetLongitude(updatePostion);
double altitude = updateBody.GetAltitude(updatePostion);
updateVessel.latitude = latitude;
updateVessel.longitude = longitude;
updateVessel.altitude = altitude;
updateVessel.protoVessel.latitude = latitude;
updateVessel.protoVessel.longitude = longitude;
updateVessel.protoVessel.altitude = altitude;

Vector3d orbitalPos = updatePostion - updateBody.position;
Vector3d surfaceOrbitVelDiff = updateBody.getRFrmVel(updatePostion);
Vector3d orbitalVel = updateVelocity + surfaceOrbitVelDiff;
updateVessel.orbitDriver.orbit.UpdateFromStateVectors(orbitalPos.xzy, orbitalVel.xzy, updateBody, Planetarium.GetUniversalTime());
}
else
{
Orbit updateOrbit = new Orbit(orbit[0], orbit[1], orbit[2], orbit[3], orbit[4], orbit[5], orbit[6], updateBody);
updateOrbit.Init();
updateOrbit.UpdateFromUT(Planetarium.GetUniversalTime());

//Positional Error Tracking
distanceError = Vector3d.Distance(updateVessel.GetWorldPos3D(), updateOrbit.pos);
double latitude = updateBody.GetLatitude(updateOrbit.pos);
double longitude = updateBody.GetLongitude(updateOrbit.pos);
double altitude = updateBody.GetAltitude(updateOrbit.pos);
updateVessel.latitude = latitude;
updateVessel.longitude = longitude;
updateVessel.altitude = altitude;
updateVessel.protoVessel.latitude = latitude;
updateVessel.protoVessel.longitude = longitude;
updateVessel.protoVessel.altitude = altitude;
VesselUtil.CopyOrbit(updateOrbit, updateVessel.orbitDriver.orbit);
updateVessel.orbit.SetOrbit(orbit[0], orbit[1], orbit[2], orbit[3], orbit[4], orbit[5], orbit[6], updateBody);
}
//Updates orbit.pos/vel
updateVessel.orbitDriver.orbit.UpdateFromOrbitAtUT(updateVessel.orbitDriver.orbit, Planetarium.GetUniversalTime(), updateBody);

//Updates vessel pos from the orbit, as if on rails
updateVessel.orbitDriver.updateFromParameters();

//Rotation
Expand All @@ -240,8 +208,8 @@ public void Apply(PosistionStatistics posistionStatistics, Dictionary<Guid, Vess
if (previousUpdate != null)
{
double deltaUpdateT = planetTime - previousUpdate.planetTime;
double deltaRealT = Planetarium.GetUniversalTime() - planetTime;
float scaling = 1f + (float)(deltaRealT / deltaUpdateT);
double deltaRealT = Planetarium.GetUniversalTime() - previousUpdate.planetTime;
float scaling = (float)(deltaRealT / deltaUpdateT);
if (Math.Abs(deltaRealT) < 3f)
{
Quaternion previousRotation = new Quaternion(previousUpdate.rotation[0], previousUpdate.rotation[1], previousUpdate.rotation[2], previousUpdate.rotation[3]);
Expand All @@ -259,43 +227,47 @@ public void Apply(PosistionStatistics posistionStatistics, Dictionary<Guid, Vess
}
Quaternion updateRotation = normalRotate * unfudgedRotation;
//Rotational error tracking
rotationalError = Quaternion.Angle(updateVessel.srfRelRotation, updateRotation);
updateVessel.SetRotation(updateVessel.mainBody.bodyTransform.rotation * updateRotation);
double rotationalError = Quaternion.Angle(updateVessel.srfRelRotation, updateRotation);
//updateVessel.SetRotation(updateVessel.mainBody.bodyTransform.rotation * updateRotation);
updateVessel.srfRelRotation = updateRotation;
updateVessel.protoVessel.rotation = updateVessel.srfRelRotation;

//Velocity set
if (updateVessel.parts != null && !updateVessel.packed)
{
for (int i = 0; i < updateVessel.parts.Count; i++)
{
updateVessel.parts[i].ResumeVelocity();
}
}

//Angular velocity
Vector3 rootVel = FlightGlobals.ActiveVessel.rootPart.rb.velocity;
Vector3 angularVel = updateVessel.ReferenceTransform.rotation * new Vector3(angularVelocity[0], angularVelocity[1], angularVelocity[2]);
if (updateVessel.parts != null && !updateVessel.packed)
{
for (int i = 0; i < updateVessel.parts.Count; i++)
{
Part vesselPart = updateVessel.parts[i];
if (vesselPart.rb != null && vesselPart.State != PartStates.DEAD)
Part thisPart = updateVessel.parts[i];
thisPart.vel = updateVessel.orbit.GetVel() - Krakensbane.GetFrameVelocity();
if (thisPart.orbit.referenceBody.inverseRotation)
{
thisPart.vel -= updateBody.getRFrmVel(thisPart.partTransform.position);
}
if (thisPart.rb != null && thisPart.State != PartStates.DEAD)
{
vesselPart.rb.angularVelocity = angularVel;
if (vesselPart != updateVessel.rootPart)
thisPart.rb.velocity = thisPart.vel;
//Angular Vel
thisPart.rb.angularVelocity = angularVel;
if (thisPart != updateVessel.rootPart)
{
Vector3 diffPos = vesselPart.rb.position - updateVessel.CoM;
Vector3 diffPos = thisPart.rb.position - updateVessel.CoM;
Vector3 partVelDifference = Vector3.Cross(angularVel, diffPos);
vesselPart.rb.velocity = rootVel + partVelDifference;
thisPart.rb.velocity = thisPart.rb.velocity + partVelDifference;
}
}
}
}

//Updates Vessel.CoMD, which is used for GetWorldPos3D
updateVessel.precalc.CalculatePhysicsStats();

updateVessel.latitude = updateBody.GetLatitude(updateVessel.GetWorldPos3D());
updateVessel.longitude = updateBody.GetLongitude(updateVessel.GetWorldPos3D());
updateVessel.altitude = updateBody.GetAltitude(updateVessel.GetWorldPos3D());
updateVessel.protoVessel.latitude = updateVessel.latitude;
updateVessel.protoVessel.longitude = updateVessel.longitude;
updateVessel.protoVessel.altitude = updateVessel.altitude;
double distanceError = Vector3d.Distance(oldPos, updateVessel.GetWorldPos3D());
double velocityError = Vector3d.Distance(oldVelocity, updateVessel.orbitDriver.orbit.GetVel());
if (ctrlUpdate != null)
{
if (ctrlUpdate.ContainsKey(updateVessel.id))
Expand Down
15 changes: 0 additions & 15 deletions Client/VesselUtil.cs
Expand Up @@ -5,21 +5,6 @@ namespace DarkMultiPlayer
{
public class VesselUtil
{
//Credit where credit is due, Thanks hyperedit.
public static void CopyOrbit(Orbit sourceOrbit, Orbit destinationOrbit)
{
destinationOrbit.inclination = sourceOrbit.inclination;
destinationOrbit.eccentricity = sourceOrbit.eccentricity;
destinationOrbit.semiMajorAxis = sourceOrbit.semiMajorAxis;
destinationOrbit.LAN = sourceOrbit.LAN;
destinationOrbit.argumentOfPeriapsis = sourceOrbit.argumentOfPeriapsis;
destinationOrbit.meanAnomalyAtEpoch = sourceOrbit.meanAnomalyAtEpoch;
destinationOrbit.epoch = sourceOrbit.epoch;
destinationOrbit.referenceBody = sourceOrbit.referenceBody;
destinationOrbit.Init();
destinationOrbit.UpdateFromUT(Planetarium.GetUniversalTime());
}

public static DMPRaycastPair RaycastGround(double latitude, double longitude, CelestialBody body)
{
//We can only find the ground on bodies that actually *have* ground and if we are in flight near the origin
Expand Down

0 comments on commit 89effa5

Please sign in to comment.