Skip to content
This repository was archived by the owner on Aug 23, 2025. It is now read-only.

Commit 8dec17f

Browse files
author
Michael P. Starkweather
committed
First working prototype
1 parent 9e03035 commit 8dec17f

3 files changed

Lines changed: 186 additions & 26 deletions

File tree

RandomCampaignStart/Class1.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

RandomCampaignStart/RNGStart.cs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Reflection;
5+
using BattleTech;
6+
using Harmony;
7+
using HBS.Logging;
8+
using Newtonsoft.Json;
9+
10+
namespace RandomCampaignStart
11+
{
12+
[SuppressMessage("ReSharper", "InconsistentNaming")]
13+
[HarmonyPatch(typeof(SimGameState), "FirstTimeInitializeDataFromDefs")]
14+
public static class SimGameState_FirstTimeInitializeDataFromDefs_Patch
15+
{
16+
// from https://stackoverflow.com/questions/273313/randomize-a-listt
17+
private static readonly Random rng = new Random();
18+
private static void RNGShuffle<T>(this IList<T> list)
19+
{
20+
int n = list.Count;
21+
while (n > 1)
22+
{
23+
n--;
24+
int k = rng.Next(n + 1);
25+
T value = list[k];
26+
list[k] = list[n];
27+
list[n] = value;
28+
}
29+
}
30+
31+
public static void Postfix(SimGameState __instance)
32+
{
33+
// clear roster
34+
if (RngStart.Settings.NumberRandomRonin + RngStart.Settings.NumberProceduralPilots > 0)
35+
{
36+
while (__instance.PilotRoster.Count > 0)
37+
{
38+
__instance.PilotRoster.RemoveAt(0);
39+
}
40+
41+
// pilotgenerator seems to give me the same exact results for ronin
42+
// every time, and can push out duplicates, which is odd?
43+
// just do our own thing
44+
var pilots = new List<PilotDef>();
45+
46+
if (RngStart.Settings.NumberRandomRonin > 0)
47+
{
48+
var roninPilots = new List<PilotDef>(__instance.RoninPilots);
49+
roninPilots.RNGShuffle();
50+
51+
for (int i = 0; i < RngStart.Settings.NumberRandomRonin; i++)
52+
{
53+
pilots.Add(roninPilots[i]);
54+
}
55+
}
56+
57+
// pilot generator works fine for non-ronin =/
58+
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+
}
63+
64+
foreach (var pilotDef in pilots)
65+
{
66+
__instance.AddPilotToRoster(pilotDef, true);
67+
}
68+
}
69+
70+
// mechs
71+
if (RngStart.Settings.NumberLightMechs + RngStart.Settings.NumberMediumMechs > 0)
72+
{
73+
int baySlot = 1;
74+
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+
{
79+
__instance.ActiveMechs.Remove(i);
80+
}
81+
82+
// remove ancestral mech if specified
83+
if (RngStart.Settings.RemoveAncestralMech)
84+
{
85+
__instance.ActiveMechs.Remove(0);
86+
baySlot = 0;
87+
}
88+
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+
}
114+
115+
// actually add the mechs to the game
116+
foreach (var mechId in mechIds)
117+
{
118+
var mechDef = new MechDef(__instance.DataManager.MechDefs.Get(mechId), __instance.GenerateSimGameUID());
119+
__instance.AddMech(baySlot, mechDef, true, true, false);
120+
baySlot++;
121+
}
122+
}
123+
}
124+
}
125+
126+
internal class ModSettings
127+
{
128+
public bool RemoveAncestralMech = false;
129+
public int NumberRandomRonin = 4;
130+
public int NumberProceduralPilots = 0;
131+
public int NumberLightMechs = 3;
132+
public int NumberMediumMechs = 1;
133+
134+
public List<string> LightMechsPossible = new List<string>();
135+
public List<string> MediumMechsPossible = new List<string>();
136+
}
137+
138+
public static class RngStart
139+
{
140+
internal static ILog Logger = HBS.Logging.Logger.GetLogger("RandomCampaignStart");
141+
internal static ModSettings Settings;
142+
143+
public static void Init(string modDir, string modSettings)
144+
{
145+
var harmony = HarmonyInstance.Create("io.github.mpstark.RandomCampaignStart");
146+
harmony.PatchAll(Assembly.GetExecutingAssembly());
147+
148+
// read settings
149+
try
150+
{
151+
Settings = JsonConvert.DeserializeObject<ModSettings>(modSettings);
152+
}
153+
catch (Exception)
154+
{
155+
Settings = new ModSettings();
156+
}
157+
}
158+
}
159+
}
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>20cef650-c940-4b2a-bb30-f382b3778c6e</ProjectGuid>
7+
<ProjectGuid>{20CEF650-C940-4B2A-BB30-F382B3778C6E}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>RandomCampaignStart</RootNamespace>
@@ -22,28 +22,40 @@
2222
<WarningLevel>4</WarningLevel>
2323
</PropertyGroup>
2424
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25-
<DebugType>pdbonly</DebugType>
25+
<DebugType>none</DebugType>
2626
<Optimize>true</Optimize>
2727
<OutputPath>bin\Release\</OutputPath>
2828
<DefineConstants>TRACE</DefineConstants>
2929
<ErrorReport>prompt</ErrorReport>
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
3232
<ItemGroup>
33-
<Reference Include="System"/>
34-
35-
<Reference Include="System.Core"/>
36-
<Reference Include="System.Xml.Linq"/>
37-
<Reference Include="System.Data.DataSetExtensions"/>
38-
39-
40-
<Reference Include="System.Data"/>
41-
42-
<Reference Include="System.Xml"/>
33+
<Reference Include="0Harmony">
34+
<HintPath>D:\Games\GOG Galaxy\Games\BATTLETECH\BattleTech_Data\Managed\0Harmony.dll</HintPath>
35+
<Private>False</Private>
36+
</Reference>
37+
<Reference Include="Assembly-CSharp">
38+
<HintPath>D:\Games\GOG Galaxy\Games\BATTLETECH\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
39+
<Private>False</Private>
40+
</Reference>
41+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
42+
<SpecificVersion>False</SpecificVersion>
43+
<HintPath>D:\Games\GOG Galaxy\Games\BATTLETECH\BattleTech_Data\Managed\Newtonsoft.Json.dll</HintPath>
44+
<Private>False</Private>
45+
</Reference>
46+
<Reference Include="System" />
47+
<Reference Include="System.Core" />
48+
<Reference Include="System.Xml.Linq" />
49+
<Reference Include="System.Data.DataSetExtensions" />
50+
<Reference Include="System.Data" />
51+
<Reference Include="System.Xml" />
4352
</ItemGroup>
4453
<ItemGroup>
45-
<Compile Include="Class1.cs" />
54+
<Compile Include="RNGStart.cs" />
4655
<Compile Include="Properties\AssemblyInfo.cs" />
4756
</ItemGroup>
4857
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
49-
</Project>
58+
<PropertyGroup>
59+
<PostBuildEvent>xcopy "$(TargetDir)$(TargetFileName)" "$(BattleTechGame)\Mods\RandomCampaignStart\" /y</PostBuildEvent>
60+
</PropertyGroup>
61+
</Project>

0 commit comments

Comments
 (0)