Skip to content

Commit

Permalink
Enable locking to other ship
Browse files Browse the repository at this point in the history
Fix loose offset
Fix in editor buttons
Don't allow locking between locks of different scales
  • Loading branch information
whale2 committed Jul 25, 2019
1 parent de47180 commit 196fb26
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 21 deletions.
2 changes: 1 addition & 1 deletion GameData/InnerLock/InnerLock.version
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"VERSION":{
"MAJOR":0,
"MINOR":1,
"PATCH":0
"PATCH":1
},
"KSP_VERSION":{
"MAJOR":1,
Expand Down
Binary file modified GameData/InnerLock/Plugins/InnerLock.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ PART
lockingTo = IR_LockConnector,IR_LockConnector_LA
allowedRolls = 0, 1, -1
lockStrength = 145

canLockToOtherShip = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ PART
lockingTo = IR_LockConnector_LA,IR_LockConnector
allowedRolls = 0, 1, -1
lockStrength = 145
canLockToOtherShip = true
}
}
7 changes: 5 additions & 2 deletions InnerLock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
KSP-1.7|Any CPU = KSP-1.7|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.Release|Any CPU.Build.0 = Release|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.KSP-1.7|Any CPU.ActiveCfg = KSP-1.7|Any CPU
{59577D4A-2D90-4CF8-B640-720093EC7E3F}.KSP-1.7|Any CPU.Build.0 = KSP-1.7|Any CPU
EndGlobalSection
EndGlobal
15 changes: 12 additions & 3 deletions InnerLock/InnerLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ public void enableIntraCollisions ()
public override void OnStart (StartState state)
{
base.OnStart (state);
if (state == StartState.Editor && isSwitchable) {
Events ["enableIntraCollisions"].active = !isActiveForAll;
Events ["disableIntraCollisions"].active = isActiveForAll;
if (state == StartState.Editor)
{
if (isSwitchable)
{
Events["enableIntraCollisions"].active = !isActiveForAll;
Events["disableIntraCollisions"].active = isActiveForAll;
}
else
{
Events["enableIntraCollisions"].active = false;
Events["disableIntraCollisions"].active = false;
}
}
else {
GameEvents.onVesselCreate.Add (EnqueueVessel);
Expand Down
21 changes: 15 additions & 6 deletions InnerLock/InnerLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,33 @@
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'KSP-1.7|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\KSP-1.7</OutputPath>
<DefineConstants>DEBUG;KSP_171;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Compile Include="LockFSM.cs" />
<Compile Include="LockMechanism.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="InnerLock.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\ksp-versions\ksp-1.3.1-dev\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>..\..\..\ksp-versions\ksp-1.7.1-innerlock\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\ksp-versions\ksp-1.3.1-dev\KSP_Data\Managed\UnityEngine.dll</HintPath>
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\ksp-versions\ksp-1.7.1-innerlock\KSP_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>..\..\..\ksp-versions\ksp-1.3.1-dev\KSP_Data\Managed\UnityEngine.UI.dll</HintPath>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\ksp-versions\ksp-1.7.1-innerlock\KSP_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>cp "$(TargetPath)" "$(SolutionDir)/GameData/InnerLock/Plugins"</PostBuildEvent>
</PropertyGroup>
Expand Down
45 changes: 38 additions & 7 deletions InnerLock/LockMechanism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public class LockMechanism : PartModule
public float lockStrength = 50f;

[KSPField]
public float maxOffset = 0.05f;
public float maxOffset = 0.01f;

[KSPField]
public float maxGap = 0.11f;

[KSPField]
public float maxRollDeviation = 0.01f;
Expand Down Expand Up @@ -372,7 +375,8 @@ public Part findCounterpartOnLaunch()
}
printDebug("Part could be locked to");
// Ok, we can lock to it. Check alignment
if (!checkRollAndDot(hitPart, upperBound))

if (!checkRollAndDot(hitPart))
{
printDebug("Part not aligned");
return null;
Expand All @@ -386,7 +390,29 @@ public Part findCounterpartOnLaunch()

private bool isLockablePart(Part otherPart)
{
return lockingTo.Any(otherPart.partInfo.name.Replace(".", "_").Contains);
if (! lockingTo.Any(otherPart.partInfo.name.Replace(".", "_").Contains))
{
return false;
}

// Check if both halves have same scale
if (!part.Modules.Contains("TweakScale"))
{
return true;
}

if (!otherPart.Modules.Contains("TweakScale"))
{
return false;
}

PartModule tweakScale = part.Modules["TweakScale"];
float scale = tweakScale.Fields["currentScale"].GetValue<float>(tweakScale);

PartModule otherTweakScale = otherPart.Modules["TweakScale"];
float otherScale = otherTweakScale.Fields["currentScale"].GetValue<float>(tweakScale);

return Math.Abs(scale - otherScale) < 0.1f;
}

// Collision processing
Expand Down Expand Up @@ -428,7 +454,7 @@ private bool checkCollision(Collision c)
continue;

// Check if it is properly aligned
if (!checkRollAndDot(otherLockPart, maxOffset))
if (!checkRollAndDot(otherLockPart))
continue;

return true;
Expand Down Expand Up @@ -457,26 +483,30 @@ private bool checkOtherHalf(Part otherPart) {
}

// Check if other half is properly aligned
private bool checkRollAndDot(Part otherPart, float maxDistance) {
private bool checkRollAndDot(Part otherPart) {

float dotup = Vector3.Dot (otherPart.transform.up, transform.up);
float dotfwd = Vector3.Dot (otherPart.transform.forward, transform.forward);
float offset = Vector3.Distance (
Vector3.ProjectOnPlane (transform.position, transform.up),
Vector3.ProjectOnPlane (otherPart.transform.position, transform.up));
float gap = Vector3.Distance(
Vector3.ProjectOnPlane (transform.position, transform.forward),
Vector3.ProjectOnPlane (otherPart.transform.position, transform.forward));

bool aligned = !(-dotup < maxRollDeviation || offset > maxDistance);
bool aligned = !(-dotup < maxRollDeviation || offset > maxOffset || gap > maxGap);

foreach (float roll in allowedRolls) {
if (Math.Abs (dotfwd - roll) < maxRollDeviation) {
aligned = true;
break;
}
}
printDebug ($"dotup = {dotup}; dotfwd = {dotfwd}; offset = {offset}; gap = {gap}");
printDebug($"maxOffset = {maxOffset}; maxGap = {maxGap}; rollDeviation (dotfwd/dotup) = {maxRollDeviation}");

if (!aligned) {
if (!msgPosted) {
printDebug ("dotup = " + dotup + "; dotfwd = " + dotfwd + "; offset = " + offset);
ScreenMessages.PostScreenMessage ("Latch not aligned - can't lock");
msgPosted = true;
}
Expand Down Expand Up @@ -506,6 +536,7 @@ public void lockHasp (Part latch)
#endif
if (num < ecConsumption) {
ScreenMessages.PostScreenMessage ("Not enough electric charge to lock the hasp!");
printDebug($"request ec res = {num}");
return;
}
isSlave = false;
Expand Down
2 changes: 1 addition & 1 deletion multibuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
PROJECT="InnerLock"
KSP_BASE="${HOME}/ksp-versions"

TARGETS="1.3.1 1.5.1 1.6.1 1.7.1"
TARGETS="1.3.1 1.5.1 1.6.1 1.7.3"

PROJECT_VERSION=$(cat GameData/${PROJECT}/$PROJECT.version|jq '.VERSION.MAJOR,.VERSION.MINOR,.VERSION.PATCH'|tr '\n' '.'|sed -e s'/\.$//')

Expand Down

0 comments on commit 196fb26

Please sign in to comment.