@@ -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
0 commit comments