Add logic for retrieving and mutating villager+house pairs

This commit is contained in:
Kurt 2021-02-06 15:10:41 -08:00
parent 6fc686c910
commit 9638ea6f45
5 changed files with 104 additions and 1 deletions

View File

@ -10,7 +10,11 @@ public class GSaveMemory : IVillagerOrigin
public GSaveMemory(byte[] data) => Data = data;
public GSavePlayerId PlayerId => Data.Slice(0, GSavePlayerId.SIZE).ToStructure<GSavePlayerId>();
public GSavePlayerId PlayerId
{
get => Data.Slice(0, GSavePlayerId.SIZE).ToStructure<GSavePlayerId>();
set => value.ToBytes().CopyTo(Data, 0);
}
public uint TownID
{

View File

@ -0,0 +1,14 @@
namespace NHSE.Villagers
{
public class VillagerData
{
public readonly byte[] Villager;
public readonly byte[] House;
public VillagerData(byte[] villager, byte[] house)
{
Villager = villager;
House = house;
}
}
}

View File

@ -0,0 +1,16 @@
using NHSE.Core;
namespace NHSE.Villagers
{
public class VillagerInfo
{
public readonly Villager2 Villager;
public readonly VillagerHouse House;
public VillagerInfo(Villager2 villager, VillagerHouse house)
{
Villager = villager;
House = house;
}
}
}

View File

@ -0,0 +1,36 @@
using System.Diagnostics;
using NHSE.Core;
using static NHSE.Villagers.Properties.Resources;
namespace NHSE.Villagers
{
public static class VillagerResources
{
private static string GetResourceNameVillager(string villagerName) => $"{villagerName}V";
private static string GetResourceNameHouse(string villagerName) => $"{villagerName}H";
/// <summary>
/// Checks if the villager name is in the database.
/// </summary>
/// <param name="villagerName">Internal villager name.</param>
/// <returns>True if exists in database</returns>
public static bool IsVillagerDataKnown(string villagerName) => ResourceManager.GetObject(GetResourceNameVillager(villagerName)) != null;
/// <summary>
/// Gets the raw Villager data and house data for the <see cref="villagerName"/>.
/// </summary>
/// <param name="villagerName">Internal villager name.</param>
public static VillagerData GetVillager(string villagerName)
{
var nv = GetResourceNameVillager(villagerName);
var nh = GetResourceNameHouse(villagerName);
var bv = (byte[])ResourceManager.GetObject(nv);
var bh = (byte[])ResourceManager.GetObject(nh);
Debug.Assert(bv.Length == Villager2.SIZE);
Debug.Assert(bh.Length == VillagerHouse.SIZE);
return new VillagerData(bv, bh);
}
}
}

View File

@ -0,0 +1,33 @@
using NHSE.Core;
namespace NHSE.Villagers
{
public static class VillagerSwap
{
public static VillagerData GetReplacementVillager(VillagerInfo exist, string newVillager, bool prepMoveOut = false)
{
var replace = VillagerResources.GetVillager(newVillager);
return AdaptVillager(exist, replace, prepMoveOut);
}
private static VillagerData AdaptVillager(VillagerInfo exist, VillagerData replace, bool prepMoveOut = false)
{
var ov = exist.Villager;
var oh = exist.House;
var nv = new Villager2(replace.Villager);
_ = new VillagerHouse(replace.House) {NPC1 = oh.NPC1, NPC2 = oh.NPC2, BuildPlayer = oh.BuildPlayer};
// Copy Memories
var om = nv.GetMemory(0);
var nm = ov.GetMemory(0);
nm.PlayerId = om.PlayerId;
nv.SetMemory(nm, 0);
if (!prepMoveOut)
return replace;
nv.MovingOut = true;
return replace;
}
}
}