private void OnVesselSituationChange(GameEvents.HostedFromToAction <Vessel, Vessel.Situations> arg) { StnSciScenario.Log("OnVesselSituationChanged"); if (!((arg.from == Vessel.Situations.LANDED || arg.from == Vessel.Situations.PRELAUNCH) && (arg.to == Vessel.Situations.FLYING || arg.to == Vessel.Situations.SUB_ORBITAL))) { return; } if (arg.host.mainBody.name != "Kerbin") { return; } AvailablePart experimentType = StnSciParameter.getExperimentType(this); if (experimentType == null) { return; } foreach (Part part in arg.host.Parts) { if (part.name == experimentType.name) { StationExperiment e = part.FindModuleImplementing <StationExperiment>(); if (e != null && e.launched == 0) { e.launched = (float)Planetarium.GetUniversalTime(); } } } }
private int CompletedCount(String exp = null, CelestialBody body = null) { int ret = 0; if (ContractSystem.Instance == null) { StnSciScenario.Log("ContractSystem Instance is null"); return(0); } if (ContractSystem.Instance.ContractsFinished == null) { StnSciScenario.Log("ContractSystem ContratsFinished is null"); return(0); } foreach (Contract con in ContractSystem.Instance.ContractsFinished) { StnSciContract sscon = con as StnSciContract; if (sscon != null && sscon.ContractState == Contract.State.Completed && sscon.experimentType != null && sscon.targetBody != null && (exp == null || sscon.experimentType != null) && (body == null || sscon.targetBody != null) && ((exp == null || exp == sscon.experimentType.name) && (body == null || body.name == sscon.targetBody.name))) { ret += 1; } } return(ret); }
protected override void OnUpdate() { base.OnUpdate(); if (lastUpdate > UnityEngine.Time.realtimeSinceStartup + .1) { return; } CelestialBody targetBody = StnSciParameter.getTargetBody(this); AvailablePart experimentType = StnSciParameter.getExperimentType(this); if (targetBody == null || experimentType == null) { if (targetBody == null || experimentType == null) { StnSciScenario.Log("targetBody or experimentType is null"); return; } } lastUpdate = UnityEngine.Time.realtimeSinceStartup; Vessel vessel = FlightGlobals.ActiveVessel; if (vessel != null) { foreach (Part part in vessel.Parts) { if (part.name == experimentType.name) { StationExperiment e = part.FindModuleImplementing <StationExperiment>(); if (e != null) { if (e.completed >= this.Root.DateAccepted && e.completed > e.launched) { ScienceData[] data = e.GetData(); foreach (ScienceData datum in data) { if (datum.subjectID.ToLower().Contains("@" + targetBody.name.ToLower() + "inspace")) { SetComplete(); return; } } } } } } } SetIncomplete(); }
private void OnVesselCreate(Vessel vessel) { StnSciScenario.Log("OnVesselCreate"); AvailablePart experimentType = StnSciParameter.getExperimentType(this); if (experimentType == null) { return; } foreach (Part part in vessel.Parts) { if (part.name == experimentType.name) { StationExperiment e = part.FindModuleImplementing <StationExperiment>(); if (e != null) { e.launched = (float)Planetarium.GetUniversalTime(); } } } }
private void OnRecovery(Vessel vessel) { StnSciScenario.Log("Recovering " + vessel.vesselName); CelestialBody targetBody = StnSciParameter.getTargetBody(this); AvailablePart experimentType = StnSciParameter.getExperimentType(this); if (targetBody == null || experimentType == null) { StnSciScenario.Log("targetBody or experimentType is null"); return; } foreach (Part part in vessel.Parts) { if (part.name == experimentType.name) { StationExperiment e = part.FindModuleImplementing <StationExperiment>(); if (e != null) { if (e.launched >= this.Root.DateAccepted && e.completed >= e.launched) { ScienceData[] data = e.GetData(); foreach (ScienceData datum in data) { if (datum.subjectID.ToLower().Contains("@" + targetBody.name.ToLower() + "inspace")) { StnSciParameter parent = this.Parent as StnSciParameter; SetComplete(); if (parent != null) { parent.Complete(); } return; } } } } } } SetIncomplete(); }
private void OnRecovered(ProtoVessel pv, bool dummy) { StnSciScenario.Log("Recovered " + pv.vesselName); CelestialBody targetBody = StnSciParameter.getTargetBody(this); AvailablePart experimentType = StnSciParameter.getExperimentType(this); if (targetBody == null || experimentType == null) { StnSciScenario.Log("targetBody or experimentType is null"); return; } foreach (ProtoPartSnapshot part in pv.protoPartSnapshots) { if (part.partName == experimentType.name) { foreach (ProtoPartModuleSnapshot module in part.modules) { if (module.moduleName == "StationExperiment") { ConfigNode cn = module.moduleValues; if (!cn.HasValue("launched") || !cn.HasValue("completed")) { continue; } float launched, completed; try { launched = float.Parse(cn.GetValue("launched")); completed = float.Parse(cn.GetValue("completed")); } catch (Exception e) { StnSciScenario.LogError(e.ToString()); continue; } if (launched >= this.Root.DateAccepted && completed >= launched) { foreach (ConfigNode datum in cn.GetNodes("ScienceData")) { if (!datum.HasValue("subjectID")) { continue; } string subjectID = datum.GetValue("subjectID"); if (subjectID.ToLower().Contains("@" + targetBody.name.ToLower() + "inspace")) { StnSciParameter parent = this.Parent as StnSciParameter; SetComplete(); if (parent != null) { parent.Complete(); } return; } } } } } } } }
protected override bool Generate() { StnSciScenario.Log("Considering a StatSci contract"); if (ActiveCount() >= StnSciScenario.Instance.settings.maxContracts) { /*StnSciScenario.Log("StationScience contracts cap hit (" + * StnSciScenario.Instance.settings.maxContracts + ").");*/ return(false); } double xp = StnSciScenario.Instance.xp + Reputation.Instance.reputation * StnSciScenario.Instance.settings.reputationFactor; if (this.Prestige == ContractPrestige.Trivial) { xp *= StnSciScenario.Instance.settings.trivialMultiplier; } if (this.Prestige == ContractPrestige.Significant) { xp *= StnSciScenario.Instance.settings.significantMultiplier; } if (this.Prestige == ContractPrestige.Exceptional) { xp *= StnSciScenario.Instance.settings.exceptionalMultiplier; } if (xp <= 0.5) { xp = 0.5; } StnSciScenario.Log("checking unlocked experiements"); List <string> experiments = GetUnlockedExperiments(); StnSciScenario.Log("experiements = " + experiments + " checking unlocked bodies"); List <CelestialBody> bodies = GetBodies_Reached(true, false); StnSciScenario.Log("bodies = " + bodies); List <ContractCandidate> candidates = new List <ContractCandidate>(); double totalWeight = 0.0; //Get most difficult combination of planet and experiment that doesn't exceed random difficulty target foreach (var exp in experiments) { StnSciScenario.Log("Experiment: " + exp); double expValue; try { expValue = StnSciScenario.Instance.settings.experimentChallenge[exp]; } catch (KeyNotFoundException) { continue; } foreach (var body in bodies) { StnSciScenario.Log("Body: " + body.name); int acount = ActiveCount(exp, body); if (acount > 0) { StnSciScenario.Log("Contract already active!"); continue; } double plaValue; try { plaValue = StnSciScenario.Instance.settings.planetChallenge[body.name]; } catch (KeyNotFoundException) { continue; } ContractCandidate candidate = new ContractCandidate(); candidate.body = body; candidate.experiment = exp; candidate.value = expValue * plaValue; /* log-gaussian function; * when val equals xp, weight is 1 * when val is half of xp, weight is .5 */ candidate.weight = Math.Exp(-Math.Pow(Math.Log(candidate.value / xp, 2), 2) / (2 * Math.Pow(2 / 2.355, 2))); candidates.Add(candidate); totalWeight += candidate.weight; } } StnSciScenario.Log("Candidate List: " + candidates.Count); double rand = GetUniform() * totalWeight; ContractCandidate chosen = null; foreach (var cand in candidates) { if (rand <= cand.weight) { chosen = cand; break; } rand -= cand.weight; } if (chosen == null) { StnSciScenario.LogError("Couldn't find appropriate planet/experiment!"); return(false); } if (!SetExperiment(chosen.experiment)) { return(false); } targetBody = chosen.body; this.value = chosen.value; this.AddParameter(new Parameters.StnSciParameter(experimentType, targetBody), null); int ccount = CompletedCount(experimentType.name, targetBody); bool first_time = (ccount == 0); float v = (float)this.value; base.SetExpiry(); float sciReward = StnSciScenario.Instance.settings.contractScience.calcReward(v, first_time); StnSciScenario.Log("SciReward: " + sciReward); base.SetScience(sciReward, targetBody); base.SetDeadlineYears(StnSciScenario.Instance.settings.contractDeadline.calcReward(v, first_time), targetBody); base.SetReputation(StnSciScenario.Instance.settings.contractReputation.calcReward(v, first_time), StnSciScenario.Instance.settings.contractReputation.calcFailure(v, first_time), targetBody); base.SetFunds(StnSciScenario.Instance.settings.contractFunds.calcAdvance(v, first_time), StnSciScenario.Instance.settings.contractFunds.calcReward(v, first_time), StnSciScenario.Instance.settings.contractFunds.calcFailure(v, first_time), targetBody); return(true); }