11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics . CodeAnalysis ;
43using System . Reflection ;
54using BattleTech ;
65using Harmony ;
7- using HBS . Logging ;
86using Newtonsoft . Json ;
97
8+ // ReSharper disable CollectionNeverUpdated.Global
9+ // ReSharper disable FieldCanBeMadeReadOnly.Global
10+ // ReSharper disable ConvertToConstant.Global
11+ // ReSharper disable InconsistentNaming
12+ // ReSharper disable UnusedMember.Global
13+
1014namespace RandomCampaignStart
1115{
12- [ SuppressMessage ( "ReSharper" , "InconsistentNaming" ) ]
1316 [ HarmonyPatch ( typeof ( SimGameState ) , "FirstTimeInitializeDataFromDefs" ) ]
1417 public static class SimGameState_FirstTimeInitializeDataFromDefs_Patch
1518 {
1619 // from https://stackoverflow.com/questions/273313/randomize-a-listt
1720 private static readonly Random rng = new Random ( ) ;
21+
1822 private static void RNGShuffle < T > ( this IList < T > list )
1923 {
20- int n = list . Count ;
24+ var n = list . Count ;
2125 while ( n > 1 )
2226 {
2327 n -- ;
24- int k = rng . Next ( n + 1 ) ;
25- T value = list [ k ] ;
28+ var k = rng . Next ( n + 1 ) ;
29+ var value = list [ k ] ;
2630 list [ k ] = list [ n ] ;
2731 list [ n ] = value ;
2832 }
2933 }
3034
35+ private static List < T > GetRandomSubList < T > ( List < T > list , int number )
36+ {
37+ var subList = new List < T > ( ) ;
38+
39+ if ( list . Count <= 0 || number <= 0 )
40+ return subList ;
41+
42+ var randomizeMe = new List < T > ( list ) ;
43+
44+ // add enough duplicates of the list to satisfy the number specified
45+ while ( randomizeMe . Count < number )
46+ randomizeMe . AddRange ( list ) ;
47+
48+ randomizeMe . RNGShuffle ( ) ;
49+ for ( var i = 0 ; i < number ; i ++ )
50+ subList . Add ( randomizeMe [ i ] ) ;
51+
52+ return subList ;
53+ }
54+
3155 public static void Postfix ( SimGameState __instance )
3256 {
33- // clear roster
3457 if ( RngStart . Settings . NumberRandomRonin + RngStart . Settings . NumberProceduralPilots > 0 )
3558 {
59+ // clear roster
3660 while ( __instance . PilotRoster . Count > 0 )
37- {
3861 __instance . PilotRoster . RemoveAt ( 0 ) ;
39- }
4062
4163 // pilotgenerator seems to give me the same exact results for ronin
4264 // every time, and can push out duplicates, which is odd?
4365 // just do our own thing
4466 var pilots = new List < PilotDef > ( ) ;
45-
46- if ( RngStart . Settings . NumberRandomRonin > 0 )
67+
68+ // add our guarenteed starting ronin
69+ foreach ( var roninID in RngStart . Settings . StartingRonin )
4770 {
48- var roninPilots = new List < PilotDef > ( __instance . RoninPilots ) ;
49- roninPilots . RNGShuffle ( ) ;
71+ var pilotDef = __instance . DataManager . PilotDefs . Get ( roninID ) ;
5072
51- for ( int i = 0 ; i < RngStart . Settings . NumberRandomRonin ; i ++ )
52- {
53- pilots . Add ( roninPilots [ i ] ) ;
54- }
73+ // add directly to roster, don't want to get duplicate ronin from random ronin
74+ if ( pilotDef != null )
75+ __instance . AddPilotToRoster ( pilotDef , true ) ;
5576 }
5677
78+ pilots . AddRange ( GetRandomSubList ( __instance . RoninPilots , RngStart . Settings . NumberRandomRonin ) ) ;
79+
5780 // pilot generator works fine for non-ronin =/
5881 if ( RngStart . Settings . NumberProceduralPilots > 0 )
59- {
60- var randomPilots = __instance . PilotGenerator . GeneratePilots ( RngStart . Settings . NumberProceduralPilots , 1 , 0 , out var notUsed ) ;
61- pilots . AddRange ( randomPilots ) ;
62- }
82+ pilots . AddRange ( __instance . PilotGenerator . GeneratePilots ( RngStart . Settings . NumberProceduralPilots , 1 , 0 , out _ ) ) ;
6383
84+ // actually add the pilots to the SimGameState
6485 foreach ( var pilotDef in pilots )
65- {
6686 __instance . AddPilotToRoster ( pilotDef , true ) ;
67- }
6887 }
69-
88+
7089 // mechs
71- if ( RngStart . Settings . NumberLightMechs + RngStart . Settings . NumberMediumMechs > 0 )
90+ if ( RngStart . Settings . NumberLightMechs + RngStart . Settings . NumberMediumMechs + RngStart . Settings . NumberHeavyMechs + RngStart . Settings . NumberAssaultMechs > 0 )
7291 {
73- int baySlot = 1 ;
92+ var baySlot = 1 ;
7493 var mechIds = new List < string > ( ) ;
75-
76- // remove the mechs added by the startinglance
77- for ( int i = 1 ; i < __instance . Constants . Story . StartingLance . Length + 1 ; i ++ )
78- {
94+
95+ // clear the initial lance
96+ for ( var i = 1 ; i < __instance . Constants . Story . StartingLance . Length + 1 ; i ++ )
7997 __instance . ActiveMechs . Remove ( i ) ;
80- }
8198
8299 // remove ancestral mech if specified
83100 if ( RngStart . Settings . RemoveAncestralMech )
@@ -86,58 +103,51 @@ public static void Postfix(SimGameState __instance)
86103 baySlot = 0 ;
87104 }
88105
89- // add the random medium mechs
90- var mediumMechIds = new List < string > ( RngStart . Settings . MediumMechsPossible ) ;
91- while ( mediumMechIds . Count < RngStart . Settings . NumberMediumMechs )
92- {
93- mediumMechIds . AddRange ( RngStart . Settings . MediumMechsPossible ) ;
94- }
95-
96- mediumMechIds . RNGShuffle ( ) ;
97- for ( int i = 0 ; i < RngStart . Settings . NumberMediumMechs ; i ++ )
98- {
99- mechIds . Add ( mediumMechIds [ i ] ) ;
100- }
101-
102- // add the random light mechs
103- var lightMechIds = new List < string > ( RngStart . Settings . LightMechsPossible ) ;
104- while ( lightMechIds . Count < RngStart . Settings . NumberLightMechs )
105- {
106- lightMechIds . AddRange ( RngStart . Settings . LightMechsPossible ) ;
107- }
108-
109- lightMechIds . RNGShuffle ( ) ;
110- for ( int i = 0 ; i < RngStart . Settings . NumberLightMechs ; i ++ )
111- {
112- mechIds . Add ( lightMechIds [ i ] ) ;
113- }
106+ // add the random mechs to mechIds
107+ mechIds . AddRange ( GetRandomSubList ( RngStart . Settings . AssaultMechsPossible , RngStart . Settings . NumberAssaultMechs ) ) ;
108+ mechIds . AddRange ( GetRandomSubList ( RngStart . Settings . HeavyMechsPossible , RngStart . Settings . NumberHeavyMechs ) ) ;
109+ mechIds . AddRange ( GetRandomSubList ( RngStart . Settings . MediumMechsPossible , RngStart . Settings . NumberMediumMechs ) ) ;
110+ mechIds . AddRange ( GetRandomSubList ( RngStart . Settings . LightMechsPossible , RngStart . Settings . NumberLightMechs ) ) ;
114111
115112 // actually add the mechs to the game
116- foreach ( var mechId in mechIds )
113+ for ( var i = 0 ; i < mechIds . Count ; i ++ )
117114 {
118- var mechDef = new MechDef ( __instance . DataManager . MechDefs . Get ( mechId ) , __instance . GenerateSimGameUID ( ) ) ;
115+ var mechDef = new MechDef ( __instance . DataManager . MechDefs . Get ( mechIds [ i ] ) , __instance . GenerateSimGameUID ( ) ) ;
119116 __instance . AddMech ( baySlot , mechDef , true , true , false ) ;
120- baySlot ++ ;
117+
118+ // check to see if we're on the last mechbay and if we have more mechs to add
119+ // if so, store the mech at index 5 before next iteration.
120+ if ( baySlot == 5 && i + 1 < mechIds . Count )
121+ __instance . UnreadyMech ( 5 , mechDef ) ;
122+ else
123+ baySlot ++ ;
121124 }
122125 }
123126 }
124127 }
125128
126129 internal class ModSettings
127130 {
128- public bool RemoveAncestralMech = false ;
129- public int NumberRandomRonin = 4 ;
130- public int NumberProceduralPilots = 0 ;
131+ public List < string > AssaultMechsPossible = new List < string > ( ) ;
132+ public List < string > HeavyMechsPossible = new List < string > ( ) ;
133+ public List < string > LightMechsPossible = new List < string > ( ) ;
134+ public List < string > MediumMechsPossible = new List < string > ( ) ;
135+
136+ public int NumberAssaultMechs = 0 ;
137+ public int NumberHeavyMechs = 0 ;
131138 public int NumberLightMechs = 3 ;
132139 public int NumberMediumMechs = 1 ;
133140
134- public List < string > LightMechsPossible = new List < string > ( ) ;
135- public List < string > MediumMechsPossible = new List < string > ( ) ;
141+ public List < string > StartingRonin = new List < string > ( ) ;
142+
143+ public int NumberProceduralPilots = 0 ;
144+ public int NumberRandomRonin = 4 ;
145+
146+ public bool RemoveAncestralMech = false ;
136147 }
137148
138149 public static class RngStart
139150 {
140- internal static ILog Logger = HBS . Logging . Logger . GetLogger ( "RandomCampaignStart" ) ;
141151 internal static ModSettings Settings ;
142152
143153 public static void Init ( string modDir , string modSettings )
0 commit comments