mirror of
https://github.com/kwsch/NHSE.git
synced 2026-04-21 07:57:25 -05:00
Add logic for retrieving and mutating villager+house pairs
This commit is contained in:
parent
6fc686c910
commit
9638ea6f45
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
14
NHSE.Villagers/VillagerData.cs
Normal file
14
NHSE.Villagers/VillagerData.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
NHSE.Villagers/VillagerInfo.cs
Normal file
16
NHSE.Villagers/VillagerInfo.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
NHSE.Villagers/VillagerResources.cs
Normal file
36
NHSE.Villagers/VillagerResources.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
NHSE.Villagers/VillagerSwap.cs
Normal file
33
NHSE.Villagers/VillagerSwap.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user