From 0b2a27544211b6a6a7eda56fe9d343f4f4ed1348 Mon Sep 17 00:00:00 2001 From: JaXt0r <120568393+JaXt0r@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:08:27 +0100 Subject: [PATCH] Added all C_NPC properties. --- PxCs.Tests/PxVmTest.cs | 19 +++++++- PxCs/Data/Vm/PxVmNpcData.cs | 29 ++++++++++- PxCs/Interface/PxVm.cs | 95 +++++++++++++++++++++++++++++++++---- 3 files changed, 133 insertions(+), 10 deletions(-) diff --git a/PxCs.Tests/PxVmTest.cs b/PxCs.Tests/PxVmTest.cs index 6ef3bd7..d4f456b 100644 --- a/PxCs.Tests/PxVmTest.cs +++ b/PxCs.Tests/PxVmTest.cs @@ -163,10 +163,27 @@ public void Test_instantiate_Npc_by_index() var vmPtr = LoadVm(VmGothicPath); PxVm.pxVmRegisterExternalDefault(vmPtr, PxVmExternalDefaultCallbackFunction); - // FIXME: I need to check what's a real index of an NPC. var npc = PxVm.InitializeNpc(vmPtr, 7656); Assert.NotEqual(npc!.instancePtr, IntPtr.Zero); + Assert.True(npc.level != 0, "NPC has no level set."); + + PxVm.pxVmDestroy(vmPtr); + } + + [Fact] + public void Test_change_Npc_value() + { + var vmPtr = LoadVm(VmGothicPath); + PxVm.pxVmRegisterExternalDefault(vmPtr, PxVmExternalDefaultCallbackFunction); + + var npc = PxVm.InitializeNpc(vmPtr, 7656); + var valueToTest = "SomeDebugValue"; + + Assert.True(npc.wp != valueToTest); + PxVm.pxVmInstanceNpcSetWP(npc.instancePtr, valueToTest); + + // There is no way to get the information back as of now. We can therefore check for an exception only. PxVm.pxVmDestroy(vmPtr); } diff --git a/PxCs/Data/Vm/PxVmNpcData.cs b/PxCs/Data/Vm/PxVmNpcData.cs index e395992..d5c5197 100644 --- a/PxCs/Data/Vm/PxVmNpcData.cs +++ b/PxCs/Data/Vm/PxVmNpcData.cs @@ -6,7 +6,34 @@ namespace PxCs.Data.Vm public class PxVmNpcData : PxVmData { public int id; - public string[] names = new string[0]; + public string slot = default!; + public uint npcType; + public uint flags; + public int startAiState; + public string spawnPoint = default!; + public int spawnDelay; + public int damageType; + public int guild; + public int level; + public int fightTactic; + public int weapon; + public int voice; + public int voicePitch; + public int bodyMass; public int routine; + public int aiState; + public int senses; + public int sensesRange; + public string wp = default!; + public int exp; + public int expNext; + public int lp; + + public string[] names = default!; + public int[] attribute = default!; + public int[] protection = default!; + public int[] damage = default!; + public int[] mission = default!; + public int[] aiVar = default!; } } diff --git a/PxCs/Interface/PxVm.cs b/PxCs/Interface/PxVm.cs index bb6ad6a..f0697b2 100644 --- a/PxCs/Interface/PxVm.cs +++ b/PxCs/Interface/PxVm.cs @@ -164,11 +164,41 @@ public enum PxVmCMenuItemSelectAction // C_Npc public delegate void PxVmEnumerateInstancesCallback(string itemName); [DllImport(DLLNAME)] public static extern void pxVmEnumerateInstancesByClassName(IntPtr vm, string name, PxVmEnumerateInstancesCallback cb); - [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetId(IntPtr instance); [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetNameLength(IntPtr instance); [DllImport(DLLNAME)] public static extern IntPtr pxVmInstanceNpcGetName(IntPtr instance, uint i); + [DllImport(DLLNAME)] public static extern IntPtr pxVmInstanceNpcGetSlot(IntPtr instance); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetNpcType(IntPtr instance); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetFlags(IntPtr instance); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetAttributeLength(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetAttribute(IntPtr instance, uint i); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetProtectionLength(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetProtection(IntPtr instance, uint i); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetDamageLength(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetDamage(IntPtr instance, uint i); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetDamageType(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetGuild(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetLevel(IntPtr instance); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetMissionLength(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetMission(IntPtr instance, uint i); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetFightTactic(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetWeapon(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetVoice(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetVoicePitch(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetBodyMass(IntPtr instance); [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetRoutine(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetStartAiState(IntPtr instance); + [DllImport(DLLNAME)] public static extern IntPtr pxVmInstanceNpcGetSpawnPoint(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetSpawnDelay(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetSenses(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetSensesRange(IntPtr instance); + [DllImport(DLLNAME)] public static extern uint pxVmInstanceNpcGetAiLength(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetAiVar(IntPtr instance, uint i); + [DllImport(DLLNAME)] public static extern IntPtr pxVmInstanceNpcGetWP(IntPtr instance); + [DllImport(DLLNAME)] public static extern void pxVmInstanceNpcSetWP(IntPtr instance, string wpName); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetExp(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetExpNext(IntPtr instance); + [DllImport(DLLNAME)] public static extern int pxVmInstanceNpcGetLp(IntPtr instance); // C_Item [DllImport(DLLNAME)] public static extern int pxVmInstanceItemGetId(IntPtr instance); @@ -349,7 +379,7 @@ public enum PxVmCMenuItemSelectAction [DllImport(DLLNAME)] public static extern IntPtr pxVmInstancePfxGetMrkTexture(IntPtr instance); [DllImport(DLLNAME)] public static extern float pxVmInstancePfxGetMrkSize(IntPtr instance); - + public static bool CallFunction(IntPtr vmPtr, string methodName, params object[] parameters) { StackPushParameters(vmPtr, parameters); @@ -525,15 +555,64 @@ private static PxVmNpcData GetNpcByInstancePtr(IntPtr instancePtr) var npc = new PxVmNpcData(); AddInstanceData(npc, instancePtr); - var nameCount = pxVmInstanceNpcGetNameLength(instancePtr); - string[] names = new string[nameCount]; - for (var i = 0u; i < nameCount; i++) - names[i] = pxVmInstanceNpcGetName(instancePtr, i).MarshalAsString(); - npc.id = pxVmInstanceNpcGetId(instancePtr); npc.symbolIndex = pxVmInstanceGetSymbolIndex(instancePtr); - npc.names = names; + + npc.slot = pxVmInstanceNpcGetSlot(instancePtr).MarshalAsString(); + npc.npcType = pxVmInstanceNpcGetNpcType(instancePtr); + npc.flags = pxVmInstanceNpcGetFlags(instancePtr); + npc.startAiState = pxVmInstanceNpcGetStartAiState(instancePtr); + npc.spawnPoint = pxVmInstanceNpcGetSpawnPoint(instancePtr).MarshalAsString(); + npc.spawnDelay = pxVmInstanceNpcGetSpawnDelay(instancePtr); + npc.damageType = pxVmInstanceNpcGetDamageType(instancePtr); + npc.guild = pxVmInstanceNpcGetGuild(instancePtr); + npc.level = pxVmInstanceNpcGetLevel(instancePtr); + npc.fightTactic = pxVmInstanceNpcGetFightTactic(instancePtr); + npc.weapon = pxVmInstanceNpcGetWeapon(instancePtr); + npc.voice = pxVmInstanceNpcGetVoice(instancePtr); + npc.voicePitch = pxVmInstanceNpcGetVoicePitch(instancePtr); + npc.bodyMass = pxVmInstanceNpcGetBodyMass(instancePtr); npc.routine = pxVmInstanceNpcGetRoutine(instancePtr); + npc.aiState = pxVmInstanceNpcGetStartAiState(instancePtr); + npc.spawnPoint = pxVmInstanceNpcGetSpawnPoint(instancePtr).MarshalAsString(); + npc.spawnDelay = pxVmInstanceNpcGetSpawnDelay(instancePtr); + npc.senses = pxVmInstanceNpcGetSenses(instancePtr); + npc.sensesRange = pxVmInstanceNpcGetSensesRange(instancePtr); + npc.wp = pxVmInstanceNpcGetWP(instancePtr).MarshalAsString(); + npc.exp = pxVmInstanceNpcGetExp(instancePtr); + npc.expNext = pxVmInstanceNpcGetExpNext(instancePtr); + npc.lp = pxVmInstanceNpcGetLp(instancePtr); + + var nameLength = pxVmInstanceNpcGetNameLength(instancePtr); + var attributeLength = pxVmInstanceNpcGetAttributeLength(instancePtr); + var protectionLength = pxVmInstanceNpcGetProtectionLength(instancePtr); + var damageLength = pxVmInstanceNpcGetDamageLength(instancePtr); + var missionLength = pxVmInstanceNpcGetMissionLength(instancePtr); + var aiLength = pxVmInstanceNpcGetAiLength(instancePtr); + + npc.names = new string[nameLength]; + for (var i = 0u; i < nameLength; i++) + npc.names[i] = pxVmInstanceNpcGetName(instancePtr, i).MarshalAsString(); + + npc.attribute = new int[attributeLength]; + for (var i = 0u; i < attributeLength; i++) + npc.attribute[i] = pxVmInstanceNpcGetAttribute(instancePtr, i); + + npc.protection = new int[protectionLength]; + for (var i = 0u; i < protectionLength; i++) + npc.protection[i] = pxVmInstanceNpcGetProtection(instancePtr, i); + + npc.damage = new int[damageLength]; + for (var i = 0u; i < damageLength; i++) + npc.damage[i] = pxVmInstanceNpcGetDamage(instancePtr, i); + + npc.mission = new int[missionLength]; + for (var i = 0u; i < missionLength; i++) + npc.mission[i] = pxVmInstanceNpcGetMission(instancePtr, i); + + npc.aiVar = new int[aiLength]; + for (var i = 0u; i < aiLength; i++) + npc.aiVar[i] = pxVmInstanceNpcGetAiVar(instancePtr, i); return npc; }