Jump to content

Displaying Kerbals in IVA/EVA - Updated


Recommended Posts

I have a mod oriented around hollow parts which allow players to create enclosed bases or ships for their kerbals to move around in. I'm trying to alter the sfr code. Currently the IVA position and rotation is based off of the first part placed in the VAB. The code works perfectly if the command pod is the first thing used, however I would like to make it a bit more flexible. I've posted the relevant bits of code below.

/*
* sfr
* License: BY: Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0): http://creativecommons.org/licenses/by-sa/3.0/
*/
using System;
using System.Linq;
using System.Text;
using UnityEngine;
using HSHModules.Extensions;
using KSP.IO;
using System.Collections;
using System.Collections.Generic;
using KSP;

namespace HSHModules
{
public class FindTransform
{
private Transform f;

private Transform Find()
{
if (this.f)
return this.f;
else
return (Transform)null;
}
}

public class sfrUtility
{
public bool Found;

public static Camera FindCamera(string name)
{
foreach (Camera c in Camera.allCameras)
{
if (c.name == name)
return c;
}
return (Camera)null;
}

public static int Find(Transform part, string name, ref List<Transform> aTransform)
{
IEnumerator enumerator = part.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
Transform part1 = (Transform)enumerator.Current;
if (part1.name.StartsWith(name))
aTransform.Add(part1);
sfrUtility.Find(part1, name, ref aTransform);
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
disposable.Dispose();
}
return part.childCount;
}

public static void ChangeLayer(Transform part, int layer)
{
if (!part)
return;
if (!part.camera)
part.gameObject.layer = layer;
IEnumerator enumerator = part.GetEnumerator();
try
{
while (enumerator.MoveNext())
ChangeLayer((Transform)enumerator.Current, layer);
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
disposable.Dispose();
}
}

public static Transform FindTransform(Transform transform1, string name)
{
if (transform1.name == name)
return transform1;
IEnumerator enumerator = transform1.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
Transform transform = sfrUtility.FindTransform((Transform)enumerator.Current, name);
if (transform)
return transform;
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
disposable.Dispose();
}
return (Transform)null;
}
}

public class sfrInternal : PartModule
{

private Transform kspParent;
private Vector3 kspPosition;
private Quaternion kspRotation;
private bool isActive;
private bool packed;

public override void OnAwake()
{
base.OnAwake();
}

public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
}

public override void OnSave(ConfigNode node)
{
base.OnSave(node);
}

public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
}

public override void OnUpdate()
{
base.OnUpdate();

if (!part.internalModel)
{
part.CreateInternalModel();
part.vessel.SetActiveInternalPart(part);
kspParent = part.transform;//InternalCamera.Instance.transform.parent;
if (!part.internalModel)
part.internalModel.SetVisible(true);
part.internalModel.transform.parent = part.transform;
part.internalModel.transform.localRotation = new Quaternion(0.0f, 0.7f, -0.7f, 0.0f);
part.internalModel.transform.localPosition = Vector3.zero;
if (part.protoModuleCrew.Count > 0)
{
part.internalModel.Initialize(part);
part.internalModel.enabled = true;
using (List<ProtoCrewMember>.Enumerator enumerator = ((List<ProtoCrewMember>)part.protoModuleCrew).GetEnumerator())
{
while (enumerator.MoveNext())
{
ProtoCrewMember current = enumerator.Current;
if (current.seat && !part.vessel.packed)
{
current.seat.enabled = true;
current.seat.SpawnCrew();
}
if (current.KerbalRef)
{
current.KerbalRef.enabled = true;
current.KerbalRef.kerbalCam.cullingMask = 1;
}
}
}
using (List<InternalSeat>.Enumerator enumerator = part.internalModel.seats.GetEnumerator())
{
while (enumerator.MoveNext())
{
InternalSeat current = enumerator.Current;
if (current.kerbalRef)
current.SpawnCrew();
if (current.portraitCamera != null)
current.portraitCamera.cullingMask = 65537;
}
}
}
}
Exception exception;
if (isActive != part.vessel.isActiveVessel && !vessel.packed)
{
isActive = part.vessel.isActiveVessel;
if (part.internalModel && part.protoModuleCrew.Count > 0)
{
part.vessel.SetActiveInternalPart(part);
part.internalModel.Initialize(part);
using (List<InternalSeat>.Enumerator enumerator = part.internalModel.seats.GetEnumerator())
{
while (enumerator.MoveNext())
{
InternalSeat current = enumerator.Current;
current.enabled = true;
if (current.portraitCamera)
current.portraitCamera.cullingMask = 1;
}
}
try
{
using (List<ProtoCrewMember>.Enumerator enumerator = part.protoModuleCrew.GetEnumerator())
{
while (enumerator.MoveNext())
{
ProtoCrewMember current = enumerator.Current;
if (current.seat && !part.vessel.packed)
{
current.seat.enabled = true;
current.seat.SpawnCrew();
}
if (current.KerbalRef)
{
current.KerbalRef.enabled = true;
current.KerbalRef.kerbalCam.cullingMask = 1;
}
}
}
}
catch (Exception ex)
{
exception = ex;
}
}
}
try
{
if (sfrUtility.FindCamera("InternalCamera") && part.vessel.isActiveVessel)
{
sfrUtility.ChangeLayer(part.internalModel.transform, 16);
part.internalModel.transform.parent = kspParent;
part.internalModel.transform.localRotation = new Quaternion(0.0f, 0.7f, -0.7f, 0.0f); //Working at root
print(this.part.transform.localPosition.ToString());
kspPosition = this.part.transform.position - this.vessel.rootPart.transform.position;
part.internalModel.transform.position = new Vector3(kspPosition.x, kspPosition.y, kspPosition.z); // Fairly certain these need to be reordered.
//part.internalModel.transform.localPosition = Vector3.zero; //Working at root
print("1");
}
else
{
sfrUtility.ChangeLayer(part.internalModel.transform, 0);
part.internalModel.SetVisible(true);
part.internalModel.gameObject.SetActive(true);
part.internalModel.transform.parent = part.transform;
part.internalModel.transform.localRotation = new Quaternion(0.0f, 0.7f, -0.7f, 0.0f);
part.internalModel.transform.localPosition = Vector3.zero;
print("2");
}
}
catch (Exception ex)
{
exception = ex;
}
try
{
using (List<ProtoCrewMember>.Enumerator enumerator = part.protoModuleCrew.GetEnumerator())
{
while (enumerator.MoveNext())
{
ProtoCrewMember current = enumerator.Current;
if (current.seat)
{
current.seat.enabled = true;
if (current.seat.portraitCamera)
current.seat.portraitCamera.cullingMask = (65537);
}
if (current.KerbalRef)
{
current.KerbalRef.enabled = true;
current.KerbalRef.kerbalCam.cullingMask = (65537);
if (current.KerbalRef.kerbalCamOverlay)
current.KerbalRef.kerbalCamOverlay.layer = 16;
}
}
}
}
catch (Exception ex)
{
exception = ex;
}
}
}
}

Edited by Alskari
Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...
Additionally exiting a part via the KerbalSeat module results in the kerbal being placed on the exterior of the vessel, which rules it out as a stopgap option.

Sorry if I'm missing something apparent, but can't you put the hatch transform position on the inside of the module/model to get the kerbal to be placed where its needed?

Link to comment
Share on other sites

  • 2 weeks later...

When seated the kerbal can be placed just fine. It's when the kerbal exits that the issue occurs. The stock code apparently determines the exit point for a kerbal based on the exterior most collision mesh of the entire vessel. I'm trying to build hollow structures, so this results in kerbals teleporting outside after leaving a chair.

Link to comment
Share on other sites

You'll get a quicker answer in the #KSPModders IRC. Lots of nice and smart people.

They seem nice and smart, but I have yet to ever get an answer relating to code. I try for several consecutive days, get frustrated, leave and then repeat the cycle about every month.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...