Skip to content

Commit

Permalink
Merge pull request #24 from ManlyMarco/kks-preg
Browse files Browse the repository at this point in the history
KKS preggers port
  • Loading branch information
ManlyMarco authored Nov 5, 2021
2 parents 9ebe2b0 + 4072bad commit 296aee9
Show file tree
Hide file tree
Showing 58 changed files with 2,840 additions and 1,344 deletions.
39 changes: 18 additions & 21 deletions src/AI_Pregnancy/AI_Pregnancy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,30 @@
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\Core_Pregnancy\Data\HeroineStatus.cs" />
<!-- <Compile Include="..\Core_Pregnancy\LactationController.cs" /> -->
<Compile Include="..\Core_Pregnancy\Data\MenstruationSchedule.cs" />
<Compile Include="..\Core_Pregnancy\PregnancyBoneEffect.cs" />
<Compile Include="..\Core_Pregnancy\PregnancyCharaController.cs" />
<Compile Include="..\Core_Pregnancy\Data\PregnancyData.cs" />
<Compile Include="..\Core_Pregnancy\Data\PregnancyDataUtils.cs" />
<Compile Include="..\Core_Pregnancy\PregnancyGameController.cs" />
<Compile Include="..\Core_Pregnancy\GUI\PregnancyGui.cs" />
<!-- <Compile Include="..\Core_Pregnancy\GUI\PregnancyGui.HSceneMenstrIconOverride.cs" /> -->
<Compile Include="..\Core_Pregnancy\GUI\PregnancyGui.StatusIcons.cs" />
<Compile Include="..\Core_Pregnancy\PregnancyPlugin.cs" />
<Compile Include="..\Core_Pregnancy\PregnancyPlugin.Hooks.cs" />
<Compile Include="..\Core_Pregnancy\Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Resources\preg_icon.xcf" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\Core_Pregnancy\Resources\leave.png" />
<Content Include="Resources\leave.png" />
<Content Include="Resources\pregnant.png" />
<Content Include="Resources\risky.png" />
<Content Include="Resources\safe.png" />
<Content Include="Resources\unknown.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\Core_Pregnancy\Resources\pregnant.png" />
<EmbeddedResource Include="..\Core_Pregnancy\Resources\risky.png" />
<EmbeddedResource Include="..\Core_Pregnancy\Resources\safe.png" />
<EmbeddedResource Include="..\Core_Pregnancy\Resources\unknown.png" />
<Compile Include="Data\HeroineStatus.cs" />
<Compile Include="Data\MenstruationSchedule.cs" />
<Compile Include="Data\PregnancyData.cs" />
<Compile Include="Data\PregnancyDataUtils.cs" />
<Compile Include="GUI\PregnancyGui.cs" />
<Compile Include="GUI\PregnancyGui.StatusIcons.cs" />
<Compile Include="PregnancyBoneEffect.cs" />
<Compile Include="PregnancyCharaController.cs" />
<Compile Include="PregnancyGameController.cs" />
<Compile Include="PregnancyPlugin.cs" />
<Compile Include="PregnancyPlugin.Hooks.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
File renamed without changes.
140 changes: 140 additions & 0 deletions src/AI_Pregnancy/Data/PregnancyData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Reflection;
using ExtensibleSaveFormat;

namespace KK_Pregnancy
{
public sealed class PregnancyData
{
public static readonly float DefaultFertility = 0.3f;

/// <summary>
/// Week at which pegnancy reaches max level and the character leaves school
/// </summary>
public static readonly int LeaveSchoolWeek = 41;

/// <summary>
/// Week at which pegracy ends and the character returns to school
/// </summary>
public static readonly int ReturnToSchoolWeek = LeaveSchoolWeek + 7;

#region Names of these are important, used as dictionary keys

/// <summary>
/// The character is harder to get pregananant.
/// </summary>
public float Fertility = 0.3f;

/// <summary>
/// Should any gameplay code be executed for this character.
/// If false the current pregancy week doesn't change and the character can't get pegnant.
/// </summary>
public bool GameplayEnabled = true;

public MenstruationSchedule MenstruationSchedule = MenstruationSchedule.Default;

/// <summary>
/// The first day that menstraion started in AI
/// </summary>
public int MenstrationStartDay = -1;

/// <summary>
/// If 0 or negative, the character is not pregant.
/// If between 0 and <see cref="LeaveSchoolWeek"/> the character is pregant and the belly is proportionately sized.
/// If equal or above <see cref="LeaveSchoolWeek"/> the character is on a maternal leave until <see cref="ReturnToSchoolWeek"/>.
/// </summary>
public int Week;

/// <summary>
/// How many times the character was pergant, including the current one.
/// </summary>
public int PregnancyCount;

public int WeeksSinceLastPregnancy;

/// <summary>
/// Always have milk, even if not pergenant
/// </summary>
public bool AlwaysLactates;

#endregion

#region Save/Load

private static readonly PregnancyData _default = new PregnancyData();
private static readonly FieldInfo[] _serializedFields = typeof(PregnancyData).GetFields(BindingFlags.Public | BindingFlags.Instance);

public static PregnancyData Load(PluginData data)
{
if (data?.data == null) return null;

var result = new PregnancyData();
foreach (var fieldInfo in _serializedFields)
{
if (data.data.TryGetValue(fieldInfo.Name, out var val))
{
try
{
if (fieldInfo.FieldType.IsEnum) val = (int)val;
fieldInfo.SetValue(result, val);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

if (result.IsPregnant)
{
result.WeeksSinceLastPregnancy = 0;
if (result.PregnancyCount == 0) result.PregnancyCount = 1;
}

return result;
}

public PluginData Save()
{
var result = new PluginData { version = 1 };
foreach (var fieldInfo in _serializedFields)
{
var value = fieldInfo.GetValue(this);
// Check if any value is different than default, if not then don't save any data
var defaultValue = fieldInfo.GetValue(_default);
if (!Equals(defaultValue, value))
result.data.Add(fieldInfo.Name, value);
}

return result.data.Count > 0 ? result : null;
}

#endregion

// If week is 0 the character is not peregenent
public bool IsPregnant => Week > 0;

public void StartPregnancy()
{
if (GameplayEnabled && !IsPregnant)
{
Week = 1;
PregnancyCount++;
WeeksSinceLastPregnancy = 0;
}
}

public void StartMenstration(int currentDay)
{
if (!IsPregnant)
{
MenstrationStartDay = currentDay;
}
}

public void StopMenstration()
{
MenstrationStartDay = -1;
}
}
}
92 changes: 92 additions & 0 deletions src/AI_Pregnancy/Data/PregnancyDataUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ExtensibleSaveFormat;
using KKAPI.MainGame;
using AIChara;
using AIProject;
using AIProject.SaveData;
using AIProject.Definitions;

namespace KK_Pregnancy
{
public static class PregnancyDataUtils
{
private static readonly int[] _earlyDetectPersonalities = { 00, 11, 12, 13, 19, 24, 31, 33 };
private static readonly int[] _lateDetectPersonalities = { 03, 05, 08, 20, 25, 26, 37 };

/// <param name="c">ChaFile to test</param>
///// <param name="afterWasDiscovered">The girl knows about it / tested it</param>
public static PregnancyData GetPregnancyData(this ChaFileControl c)
{
if (c == null) return null;

var d = ExtendedSave.GetExtendedDataById(c, PregnancyPlugin.GUID);
if (d == null) return null;

return PregnancyData.Load(d);
}

public static PregnancyData GetPregnancyData(this AgentData heroine)
{
if (heroine == null) return new PregnancyData();

// Figure out which data to take if there are multiple
// probably not necessary? null check should be enough?
return heroine.GetRelatedChaFiles()
.Select(GetPregnancyData)
.Where(x => x != null)
.OrderByDescending(x => x.PregnancyCount)
.ThenByDescending(x => x.WeeksSinceLastPregnancy)
.ThenByDescending(x => x.Week)
.ThenByDescending(x => x.GameplayEnabled)
.FirstOrDefault() ?? new PregnancyData();
}

public static HeroineStatus GetHeroineStatus(this AgentData heroine, PregnancyData pregData = null)
{
if (heroine == null) return HeroineStatus.Unknown;
if (pregData == null) pregData = heroine.GetPregnancyData();

var chaControl = heroine.GetNPC()?.ChaControl;
if (chaControl == null) return HeroineStatus.Unknown;

// Check if she wants to tell
if (heroine.SickState.ID == AIProject.Definitions.Sickness.GoodHealthID && !heroine.IsWet &&
(chaControl.fileGameInfo.phase > 2
|| heroine.StatsTable[(int)Status.Type.Mood] > 95
|| heroine.StatsTable[(int)Status.Type.Immoral] > 95
|| heroine.StatsTable[(int)Status.Type.Motivation] > 140))
{

var pregnancyWeek = pregData.Week;
if (pregnancyWeek > 0)
{
if (PregnancyPlugin.ShowPregnancyIconEarly.Value) return HeroineStatus.Pregnant;
// Different personalities notice at different times
if (_earlyDetectPersonalities.Contains(chaControl.fileParam.personality))
{
if (pregnancyWeek > 1) return HeroineStatus.Pregnant;
}
else if (_lateDetectPersonalities.Contains(chaControl.fileParam.personality))
{
if (pregnancyWeek > 11) return HeroineStatus.Pregnant;
}
else
{
if (pregnancyWeek > 5) return HeroineStatus.Pregnant;
}
}

var pregCharCtrl = chaControl.GetComponent<PregnancyCharaController>();
return !pregCharCtrl.isDangerousDay
? HeroineStatus.Safe
: HeroineStatus.Risky;
}

return HeroineStatus.Unknown;

}
}
}
Loading

0 comments on commit 296aee9

Please sign in to comment.