Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added actorSit actorGetUp actorRaycast calculateAnticipatedLocation #2016

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release/dev/sp-somefunctions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added some useful functions: ActorSit/GetUp, Raycast, CalculateAnticipatedLocation.
Original file line number Diff line number Diff line change
Expand Up @@ -1586,3 +1586,14 @@ export interface Inventory {
}

export declare function setInventory(formId: number, inventory: Inventory): void;

export interface RayCastResult
{
pos: number[];
normal: number[];
};

export declare function actorSit(formId: number): void;
export declare function actorGetUp(actorId: number): void;
export declare function actorRaycast(actorId: number, r: number): RayCastResult;
export declare function calculateAnticipatedLocation(actorId: number): number[];
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,17 @@ export interface Inventory {

export declare function setInventory(formId: number, inventory: Inventory): void;

export interface RayCastResult
{
pos: number[];
normal: number[];
};

export declare function actorSit(formId: number): void;
export declare function actorGetUp(actorId: number): void;
export declare function actorRaycast(actorId: number, r: number): RayCastResult;
export declare function calculateAnticipatedLocation(actorId: number): number[];

// Based on Form.pex
export declare class Form extends PapyrusObject {
static from(papyrusObject: PapyrusObject | null): Form | null
Expand Down
160 changes: 160 additions & 0 deletions skyrim-platform/src/platform_se/skyrim_platform/FenixFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include "NullPointerException.h"

namespace FenixFunctions {

namespace Impl {

void Sit(RE::Actor* a)
{
a->actorState1.sitSleepState = RE::SIT_SLEEP_STATE::kIsSitting;
}

void GetUp(RE::Actor* a)
{
a->actorState1.sitSleepState = RE::SIT_SLEEP_STATE::kNormal;
}

enum class LineOfSightLocation : uint32_t
{
kNone,
kEyes,
kHead,
kTorso,
kFeet
};

RE::NiPoint3 CalculateLOSLocation(RE::TESObjectREFR* refr,
LineOfSightLocation los_loc)
{
using func_t = decltype(CalculateLOSLocation);
REL::Relocation<func_t> func{ RELOCATION_ID(46021, 44877) };
return func(refr, los_loc);
}

RE::NiPoint3 ConvertAnglesToDir(const RE::NiPoint3& angles)
{
RE::NiPoint3 ans;

float sinx = sinf(angles.x);
float cosx = cosf(angles.x);
float sinz = sinf(angles.z);
float cosz = cosf(angles.z);

ans.x = cosx * sinz;
ans.y = cosx * cosz;
ans.z = -sinx;

return ans;
}

RE::NiPoint3 Rotate(float r, const RE::NiPoint3& angles)
{
return ConvertAnglesToDir(angles) * r;
}

std::pair<RE::NiPoint3, RE::NiPoint3> RaycastActor(RE::Actor* caster, float R)
{
auto havokWorldScale = RE::bhkWorld::GetWorldScale();
RE::bhkPickData pickData;
RE::NiPoint3 rayStart, rayEnd;

rayStart = CalculateLOSLocation(caster, LineOfSightLocation::kHead);
rayEnd = rayStart + Rotate(R, caster->data.angle);
pickData.rayInput.from = rayStart * havokWorldScale;
pickData.rayInput.to = rayEnd * havokWorldScale;

uint32_t collisionFilterInfo = 0;
caster->GetCollisionFilterInfo(collisionFilterInfo);
pickData.rayInput.filterInfo =
(static_cast<uint32_t>(collisionFilterInfo >> 16) << 16) |
static_cast<uint32_t>(RE::COL_LAYER::kCharController);

caster->GetParentCell()->GetbhkWorld()->PickObject(pickData);
RE::NiPoint3 hitPos;
if (pickData.rayOutput.HasHit()) {
hitPos = rayStart + (rayEnd - rayStart) * pickData.rayOutput.hitFraction;
// pick_data.rayOutput.normal;
Pospelove marked this conversation as resolved.
Show resolved Hide resolved
RE::NiPoint3 normal = { pickData.rayOutput.normal.quad.m128_f32[0],
pickData.rayOutput.normal.quad.m128_f32[1],
pickData.rayOutput.normal.quad.m128_f32[2] };
return { hitPos, normal };
} else {
return { rayEnd, {} };
}
}

void CalculateAnticipatedLocation(RE::TESObjectREFR* refr, float dtime,
RE::NiPoint3& ans)
{
using func_t = decltype(CalculateAnticipatedLocation);
REL::Relocation<func_t> func{ RELOCATION_ID(46045, 47309) };
return func(refr, dtime, ans);
}

}

RE::Actor* GetArgActor(const JsValue& arg)
{
auto formId = static_cast<uint32_t>(static_cast<double>(arg));
auto a = RE::TESForm::LookupByID<RE::Actor>(formId);

if (!a) {
throw NullPointerException("pActor");
}

return a;
}

JsValue ConvertPointToJS(const RE::NiPoint3& P)
{
std::vector<JsValue> p = { P.x, P.y, P.z };
return p;
}

JsValue ActorSit(const JsFunctionArguments& args)
{
auto a = GetArgActor(args[1]);
Impl::Sit(a);
return JsValue::Undefined();
}

JsValue ActorGetUp(const JsFunctionArguments& args)
{
auto a = GetArgActor(args[1]);
Impl::GetUp(a);
return JsValue::Undefined();
}

JsValue ActorRaycast(const JsFunctionArguments& args)
{
auto a = GetArgActor(args[1]);
float R = static_cast<float>(static_cast<double>(args[2]));
auto [P, N] = Impl::RaycastActor(a, R);

auto ans = JsValue::Object();
ans.SetProperty("pos", ConvertPointToJS(P));
ans.SetProperty("normal", ConvertPointToJS(N));
return ans;
}

JsValue CalculateAnticipatedLocation(const JsFunctionArguments& args)
{
auto a = GetArgActor(args[1]);
float dTime = static_cast<float>(static_cast<double>(args[2]));

RE::NiPoint3 ans;
Impl::CalculateAnticipatedLocation(a, dTime, ans);

return ConvertPointToJS(ans);
}

void Register(JsValue& exports)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
exports.SetProperty("actorSit", JsValue::Function(ActorSit));
exports.SetProperty("actorGetUp", JsValue::Function(ActorGetUp));
exports.SetProperty("actorRaycast", JsValue::Function(ActorRaycast));
exports.SetProperty("calculateAnticipatedLocation",
JsValue::Function(CalculateAnticipatedLocation));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace FenixFunctions {
JsValue ActorSit(const JsFunctionArguments& args);
JsValue ActorGetUp(const JsFunctionArguments& args);

void Register(JsValue& exports);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "HttpClient.h"
#include "HttpClientApi.h"
#include "InventoryApi.h"
#include "FenixFunctions.h"
#include "LoadGameApi.h"
#include "MpClientPluginApi.h"
#include "SkyrimPlatformProxy.h"
Expand Down Expand Up @@ -230,6 +231,7 @@ class CommonExecutionListener : public TickListener
FileInfoApi::Register(e);
TextApi::Register(e);
InventoryApi::Register(e);
FenixFunctions::Register(e);
ConstEnumApi::Register(e, engine);
CallNativeApi::Register(
e, [this] { return nativeCallRequirements; });
Expand Down
Loading