Skip to content

Commit 4015bc5

Browse files
committed
First contract seems to work ok
1 parent c5ad72c commit 4015bc5

3 files changed

Lines changed: 125 additions & 110 deletions

File tree

7 KB
Binary file not shown.

KourageousTourists/KourageousTourists/KourageousContracts.cs renamed to KourageousTourists/KourageousTourists/Contracts/KourageousWalkContract.cs

Lines changed: 23 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ namespace KourageousTourists
88
public class KourageousWalkContract : Contract
99
{
1010
CelestialBody targetBody = null;
11-
private int numTourists = 0;
11+
private int numTourists;
1212
private List<ProtoCrewMember> tourists;
13+
private string hashString;
1314

1415
public KourageousWalkContract() {
15-
tourists = new List<ProtoCrewMember> ();
16+
this.tourists = new List<ProtoCrewMember> ();
17+
this.hashString = "";
1618
}
1719

1820
protected override bool Generate()
1921
//System.Type contractType, Contract.ContractPrestige difficulty, int seed, State state)
2022
{
2123
KourageousTouristsAddOn.printDebug ("entered");
24+
2225
targetBody = selectNextCelestialBody ();
2326

24-
numTourists = UnityEngine.Random.Range (1, 8);
27+
this.numTourists = UnityEngine.Random.Range (1, 8);
2528
KourageousTouristsAddOn.printDebug ("num tourists: " + numTourists);
26-
for (int i = 0; i < numTourists; i++) {
29+
for (int i = 0; i < this.numTourists; i++) {
2730
ProtoCrewMember tourist = CrewGenerator.RandomCrewMemberPrototype (ProtoCrewMember.KerbalType.Tourist);
2831

2932
tourists.Add (tourist);
@@ -75,6 +78,8 @@ protected override bool Generate()
7578

7679
}
7780

81+
GenerateHashString ();
82+
7883
base.SetExpiry ();
7984
base.SetScience (0.0f, targetBody);
8085
base.SetDeadlineYears (1, targetBody);
@@ -118,21 +123,24 @@ private string getProperForMultiples() {
118123
}
119124

120125
public override bool CanBeCancelled() {
121-
// Once accepted, we can't left tourists on their own
122-
return false;
126+
// TODO: Let's make that if any tourist is out of Kerbin,
127+
// the contract can't be cancelled
128+
return true;
123129
}
124130

125131
public override bool CanBeDeclined() {
126132
return true;
127133
}
128134

129135
protected override string GetHashString() {
130-
KourageousTouristsAddOn.printDebug ("generating hash");
131-
string hash = "walkctrct" + targetBody.bodyName;
132-
foreach (ProtoCrewMember tourist in tourists)
136+
return this.hashString;
137+
}
138+
139+
private void GenerateHashString() {
140+
string hash = "walkcntrct-" + targetBody.bodyName;
141+
foreach (ProtoCrewMember tourist in this.tourists)
133142
hash += tourist.name;
134-
KourageousTouristsAddOn.printDebug ("hash: " + hash);
135-
return hash;
143+
this.hashString = hash;
136144
}
137145

138146
protected override string GetTitle () {
@@ -201,109 +209,14 @@ public override bool MeetRequirements ()
201209

202210
private CelestialBody selectNextCelestialBody() {
203211

204-
List<CelestialBody> allBodies = new List<CelestialBody> ();
205-
getRelevantBodies(Planetarium.fetch.Sun, allBodies);
212+
List<CelestialBody> allBodies = Contract.GetBodies_Reached (false, false);
213+
foreach (CelestialBody body in allBodies)
214+
if (!body.hasSolidSurface)
215+
allBodies.Remove (body);
206216
return allBodies [UnityEngine.Random.Range (0, allBodies.Count - 1)];
207217
}
208-
209-
private void getRelevantBodies(CelestialBody body, List<CelestialBody> bodies) {
210-
211-
foreach (CelestialBody orbitingBody in body.orbitingBodies) {
212-
if (!orbitingBody.Equals(Planetarium.fetch.Home) && orbitingBody.hasSolidSurface)
213-
bodies.Add (orbitingBody);
214-
getRelevantBodies (orbitingBody, bodies);
215-
}
216-
}
217218
}
218219

219-
public class KourageousWalkParameter: ContractParameter
220-
{
221-
private CelestialBody targetBody;
222-
private String tourist;
223-
224-
public KourageousWalkParameter() {
225-
targetBody = Planetarium.fetch.Home;
226-
tourist = "Unknown";
227-
KourageousTouristsAddOn.printDebug ("default constructor");
228-
}
229-
230-
public KourageousWalkParameter(CelestialBody target, String kerbal) {
231-
this.targetBody = target;
232-
this.tourist = kerbal;
233-
KourageousTouristsAddOn.printDebug (String.Format("constructor: {0}, {1}",
234-
target.bodyName, kerbal
235-
));
236-
}
237-
238-
protected override string GetHashString() {
239-
return "walk" + targetBody.bodyName + tourist;
240-
}
241-
242-
protected override string GetTitle() {
243-
return String.Format ("Let {0} walk on the surface of {1}",
244-
tourist, targetBody.bodyName);
245-
}
246-
247-
protected override void OnRegister() {
248-
KourageousTouristsAddOn.printDebug ("setting event OnEva");
249-
GameEvents.onCrewOnEva.Add (OnEva);
250-
}
251220

252-
protected override void OnUnregister() {
253-
GameEvents.onCrewOnEva.Remove (OnEva);
254-
}
255-
256-
private void OnEva(GameEvents.FromToAction<Part, Part> action) {
257-
KourageousTouristsAddOn.printDebug (
258-
String.Format("triggered; vessel: {0}, {1}",action.to.vessel, action.from.vessel));
259-
Vessel v = action.to.vessel;
260-
if (
261-
v.mainBody == targetBody &&
262-
v.GetVesselCrew () [0].name.Equals(tourist) &&
263-
v.situation == Vessel.Situations.LANDED)
264-
base.SetComplete ();
265-
}
266-
267-
protected override void OnLoad (ConfigNode node)
268-
{
269-
int bodyID = int.Parse(node.GetValue ("targetBody"));
270-
foreach(var body in FlightGlobals.Bodies)
271-
if (body.flightGlobalsIndex == bodyID)
272-
targetBody = body;
273-
274-
tourist = node.GetValue ("name");
275-
}
276-
protected override void OnSave (ConfigNode node)
277-
{
278-
int bodyID = targetBody.flightGlobalsIndex;
279-
node.AddValue ("targetBody", bodyID);
280-
node.AddValue ("name", tourist);
281-
}
282-
}
283-
284-
public class KourageousKerbalDestinationParameter: KerbalDestinationParameter
285-
{
286-
287-
288-
public KourageousKerbalDestinationParameter(): base() {
289-
}
290-
291-
public KourageousKerbalDestinationParameter (
292-
CelestialBody targetBody, FlightLog.EntryType type, string name) :
293-
base(targetBody, type, name) {
294-
}
295-
296-
protected override string GetHashString() {
297-
string hash = "";
298-
try {
299-
hash = base.GetHashString();
300-
}
301-
catch (Exception e) {
302-
KourageousTouristsAddOn.printDebug ("Got exception in base class: " + e);
303-
hash = "kwdst" + targetBody.bodyName + kerbalName;
304-
}
305-
return hash;
306-
}
307-
}
308221
}
309222

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using Contracts;
3+
4+
namespace KourageousTourists
5+
{
6+
public class KourageousWalkParameter: ContractParameter
7+
{
8+
private CelestialBody targetBody;
9+
private string tourist;
10+
11+
public KourageousWalkParameter() {
12+
this.targetBody = Planetarium.fetch.Home;
13+
this.tourist = "Unknown";
14+
KourageousTouristsAddOn.printDebug ("default constructor");
15+
}
16+
17+
public KourageousWalkParameter(CelestialBody target, String kerbal) {
18+
this.targetBody = target;
19+
this.tourist = String.Copy(kerbal);
20+
KourageousTouristsAddOn.printDebug (String.Format("constructor: {0}, {1}",
21+
target.bodyName, this.tourist
22+
));
23+
}
24+
25+
protected override string GetHashString() {
26+
return "walk" + this.targetBody.bodyName + this.tourist;
27+
}
28+
29+
protected override string GetTitle() {
30+
return String.Format ("Let {0} walk on the surface of {1}",
31+
tourist, targetBody.bodyName);
32+
}
33+
34+
protected override void OnRegister() {
35+
KourageousTouristsAddOn.printDebug ("setting event OnEva");
36+
GameEvents.onCrewOnEva.Add (OnEva);
37+
GameEvents.onVesselSituationChange.Add (OnSituationChange);
38+
}
39+
40+
protected override void OnUnregister() {
41+
GameEvents.onCrewOnEva.Remove (OnEva);
42+
GameEvents.onVesselSituationChange.Remove (OnSituationChange);
43+
}
44+
45+
private void OnSituationChange(GameEvents.HostedFromToAction<Vessel, Vessel.Situations> data) {
46+
if (!data.host.isEVA)
47+
return;
48+
49+
checkCompletion (data.host);
50+
}
51+
52+
private void OnEva(GameEvents.FromToAction<Part, Part> action) {
53+
Vessel v = action.to.vessel;
54+
KourageousTouristsAddOn.printDebug (
55+
String.Format("triggered; vessel: {0}, {1}; param tourist: {2}; body: {3}; vessel situation: {4}; vessel body: {5}",
56+
action.to.vessel, action.from.vessel, this.tourist, this.targetBody.bodyName, v.situation, v.mainBody.bodyName));
57+
58+
checkCompletion (v);
59+
}
60+
61+
private void checkCompletion(Vessel v) {
62+
63+
foreach(ProtoCrewMember c in v.GetVesselCrew())
64+
KourageousTouristsAddOn.printDebug (
65+
String.Format("param vessel crew: {0}",c.name));
66+
if (
67+
v.mainBody == targetBody &&
68+
v.GetVesselCrew () [0].name.Equals(tourist) &&
69+
v.situation == Vessel.Situations.LANDED)
70+
base.SetComplete ();
71+
}
72+
73+
protected override void OnLoad (ConfigNode node)
74+
{
75+
KourageousTouristsAddOn.printDebug (
76+
String.Format("loading; node: {0}",node));
77+
78+
int bodyID = int.Parse(node.GetValue ("targetBody"));
79+
KourageousTouristsAddOn.printDebug (
80+
String.Format("loading; bodyID: {0}",bodyID));
81+
foreach (var body in FlightGlobals.Bodies)
82+
if (body.flightGlobalsIndex == bodyID) {
83+
targetBody = body;
84+
break;
85+
}
86+
KourageousTouristsAddOn.printDebug (
87+
String.Format("loading; body: {0}", targetBody.bodyName));
88+
89+
this.tourist = String.Copy(node.GetValue ("tourist"));
90+
KourageousTouristsAddOn.printDebug (
91+
String.Format("loading; tourist: {0}",tourist));
92+
}
93+
protected override void OnSave (ConfigNode node)
94+
{
95+
int bodyID = targetBody.flightGlobalsIndex;
96+
node.AddValue ("targetBody", bodyID);
97+
node.AddValue ("tourist", tourist);
98+
}
99+
}
100+
101+
}
102+

0 commit comments

Comments
 (0)