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

Various neckonings #77

Open
wants to merge 1 commit into
base: master
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
8 changes: 6 additions & 2 deletions source/GcLib/directx/DxObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ void DxScriptParticleListObject2D::SetRenderState() {
graphics->SetTextureFilter(filterMin_, filterMag_, filterMip_);
}
void DxScriptParticleListObject2D::CleanUp() {
GetParticlePointer()->ClearInstance();
ParticleRenderer2D* obj = GetParticlePointer();
if (obj->GetAutoClearInstance())
obj->ClearInstance();
}

//****************************************************************************
Expand Down Expand Up @@ -524,7 +526,9 @@ void DxScriptParticleListObject3D::SetRenderState() {
graphics->SetTextureFilter(filterMin_, filterMag_, filterMip_);
}
void DxScriptParticleListObject3D::CleanUp() {
GetParticlePointer()->ClearInstance();
ParticleRenderer3D* obj = GetParticlePointer();
if (obj->GetAutoClearInstance())
obj->ClearInstance();
}

//****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion source/GcLib/directx/DxText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ bool DxCharGlyph::Create(UINT code, const Font& winFont, const DxFont* dxFont) {
if (minAlphaEnableDist < widthBorder)
destAlpha = 255;
else if (minAlphaEnableDist == widthBorder)
destAlpha = count;
destAlpha = static_cast<short>(count);
else destAlpha = 0;

//color = ColorAccess::SetColorA(color, ColorAccess::GetColorA(colorBorder)*count/255);
Expand Down
1 change: 1 addition & 0 deletions source/GcLib/directx/RenderObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ void TrajectoryObject3D::AddPoint(const D3DXMATRIX& mat) {
ParticleRendererBase::ParticleRendererBase() {
countInstance_ = 0U;
countInstancePrev_ = 0U;
autoClearInstance_ = true;
instColor_ = 0xffffffff;
instPosition_ = D3DXVECTOR3(0, 0, 0);
instScale_ = D3DXVECTOR3(1, 1, 1);
Expand Down
4 changes: 4 additions & 0 deletions source/GcLib/directx/RenderObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ namespace directx {
void AddInstance();
void ClearInstance();

void SetAutoClearInstance(bool clear) { autoClearInstance_ = clear; }
bool GetAutoClearInstance() { return autoClearInstance_; }

void SetInstanceColor(D3DCOLOR color) { instColor_ = color; }
void SetInstanceColorRGB(int r, int g, int b);
void SetInstanceColorRGB(D3DCOLOR color);
Expand All @@ -388,6 +391,7 @@ namespace directx {
size_t countInstance_;
size_t countInstancePrev_;
std::vector<VERTEX_INSTANCE> instanceData_;
bool autoClearInstance_;

D3DCOLOR instColor_;
D3DXVECTOR3 instPosition_;
Expand Down
48 changes: 48 additions & 0 deletions source/GcLib/gstd/GstdUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,54 @@ namespace gstd {
return DifferentialLinear<T>;
}
};

class Kinematic {
public:
template<typename T>
static inline T U_VAD(T v, T a, T d) { return sqrt(v * v - 2 * a * d); }
template<typename T>
static inline T U_VAT(T v, T a, T t) { return v - a * t; }
template<typename T>
static inline T U_VDT(T v, T d, T t) { return 2 * d / t - v; }
template<typename T>
static inline T U_ADT(T a, T d, T t) { return d / t - a * t / 2; }

template<typename T>
static inline T V_UAD(T u, T a, T d) { return sqrt(u * u + 2 * a * d); }
template<typename T>
static inline T V_UAT(T u, T a, T t) { return u + a * t; }
template<typename T>
static inline T V_UDT(T u, T d, T t) { return 2 * d / t - u; }
template<typename T>
static inline T V_ADT(T a, T d, T t) { return d / t + a * t / 2; }

template<typename T>
static inline T A_UVD(T u, T v, T d) { return (v * v - u * u) / (2 * d); }
template<typename T>
static inline T A_UVT(T u, T v, T t) { return (v - u) / t; }
template<typename T>
static inline T A_UDT(T u, T d, T t) { return 2 * (d - u * t) / (t * t); }
template<typename T>
static inline T A_VDT(T v, T d, T t) { return 2 * (v * t - d) / (t * t); }

template<typename T>
static inline T D_UVA(T u, T v, T a) { return (v * v - u * u) / (2 * a); }
template<typename T>
static inline T D_UVT(T u, T v, T t) { return t / 2 * (u + v); }
template<typename T>
static inline T D_UAT(T u, T a, T t) { return u * t + a * t * t / 2; }
template<typename T>
static inline T D_VAT(T v, T a, T t) { return v * t - a * t * t / 2; }

template<typename T>
static inline T T_UVA(T u, T v, T a) { return (v - u) / a; }
template<typename T>
static inline T T_UVD(T u, T v, T d) { return 2 * d / (u + v); }
template<typename T>
static inline T T_UAD(T u, T a, T d) { return (-u + sqrt(u * u + 2 * a * d)) / a; }
template<typename T>
static inline T T_VAD(T v, T a, T d) { return (v - sqrt(v * v - 2 * a * d)) / a; }
};
};
#endif

Expand Down
27 changes: 27 additions & 0 deletions source/GcLib/gstd/ScriptClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ static const std::vector<function> commonFunction = {
{ "Interpolate_X_AngleR", ScriptClientBase::Func_Interpolate_X_Angle<true>, 4 },
{ "Interpolate_X_Array", ScriptClientBase::Func_Interpolate_X_Array, 3 },

//Kinematics
{ "Kinematic_U_VAD", ScriptClientBase::Func_Kinematic<Math::Kinematic::U_VAD>, 3 },
{ "Kinematic_U_VAT", ScriptClientBase::Func_Kinematic<Math::Kinematic::U_VAT>, 3 },
{ "Kinematic_U_VDT", ScriptClientBase::Func_Kinematic<Math::Kinematic::U_VDT>, 3 },
{ "Kinematic_U_ADT", ScriptClientBase::Func_Kinematic<Math::Kinematic::U_ADT>, 3 },
{ "Kinematic_V_UAD", ScriptClientBase::Func_Kinematic<Math::Kinematic::V_UAD>, 3 },
{ "Kinematic_V_UAT", ScriptClientBase::Func_Kinematic<Math::Kinematic::V_UAT>, 3 },
{ "Kinematic_V_UDT", ScriptClientBase::Func_Kinematic<Math::Kinematic::V_UDT>, 3 },
{ "Kinematic_V_ADT", ScriptClientBase::Func_Kinematic<Math::Kinematic::V_ADT>, 3 },
{ "Kinematic_A_UVD", ScriptClientBase::Func_Kinematic<Math::Kinematic::A_UVD>, 3 },
{ "Kinematic_A_UVT", ScriptClientBase::Func_Kinematic<Math::Kinematic::A_UVT>, 3 },
{ "Kinematic_A_UDT", ScriptClientBase::Func_Kinematic<Math::Kinematic::A_UDT>, 3 },
{ "Kinematic_A_VDT", ScriptClientBase::Func_Kinematic<Math::Kinematic::A_VDT>, 3 },
{ "Kinematic_D_UVA", ScriptClientBase::Func_Kinematic<Math::Kinematic::D_UVA>, 3 },
{ "Kinematic_D_UVT", ScriptClientBase::Func_Kinematic<Math::Kinematic::D_UVT>, 3 },
{ "Kinematic_D_UAT", ScriptClientBase::Func_Kinematic<Math::Kinematic::D_UAT>, 3 },
{ "Kinematic_D_VAT", ScriptClientBase::Func_Kinematic<Math::Kinematic::D_VAT>, 3 },
{ "Kinematic_T_UVA", ScriptClientBase::Func_Kinematic<Math::Kinematic::T_UVA>, 3 },
{ "Kinematic_T_UVD", ScriptClientBase::Func_Kinematic<Math::Kinematic::T_UVD>, 3 },
{ "Kinematic_T_UAD", ScriptClientBase::Func_Kinematic<Math::Kinematic::T_UAD>, 3 },
{ "Kinematic_T_VAD", ScriptClientBase::Func_Kinematic<Math::Kinematic::T_VAD>, 3 },

//Rotation
{ "Rotate2D", ScriptClientBase::Func_Rotate2D, 3 },
{ "Rotate2D", ScriptClientBase::Func_Rotate2D, 5 },
Expand Down Expand Up @@ -1040,6 +1062,11 @@ value ScriptClientBase::Func_Interpolate_X_Array(script_machine* machine, int ar
return _ScriptValueLerp(machine, &arr[from], &arr[to], x, lerpFunc);
}

template<double (*funcKinematic)(double, double, double)>
value ScriptClientBase::Func_Kinematic(script_machine* machine, int argc, const value* argv) {
return CreateFloatValue(funcKinematic(argv[0].as_float(), argv[1].as_float(), argv[2].as_float()));
}

value ScriptClientBase::Func_Rotate2D(script_machine* machine, int argc, const value* argv) {
double pos[2] = { argv[0].as_float(), argv[1].as_float() };
double ang = argv[2].as_float();
Expand Down
4 changes: 4 additions & 0 deletions source/GcLib/gstd/ScriptClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ namespace gstd {
DNH_FUNCAPI_DECL_(Func_Interpolate_X_Angle);
DNH_FUNCAPI_DECL_(Func_Interpolate_X_Array);

//Math functions; kinematics
template<double (*funcKinematic)(double, double, double)>
DNH_FUNCAPI_DECL_(Func_Kinematic);

//Math functions; rotation
DNH_FUNCAPI_DECL_(Func_Rotate2D);
DNH_FUNCAPI_DECL_(Func_Rotate3D);
Expand Down
20 changes: 17 additions & 3 deletions source/TouhouDanmakufu/Common/StgItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,29 @@ void StgItemManager::CancelCollectItems() {
bCancelToPlayer_ = true;
}

std::vector<int> StgItemManager::GetItemIdInCircle(int cx, int cy, int radius, int* itemType) {
int rr = radius * radius;
std::vector<int> StgItemManager::GetItemIdInCircle(int cx, int cy, int* radius, int* itemType) {
int r = radius ? *radius : 0;
int rr = r * r;

int rect_x1 = cx - r;
int rect_y1 = cy - r;
int rect_x2 = cx + r;
int rect_y2 = cy + r;

std::vector<int> res;
for (ref_unsync_ptr<StgItemObject>& obj : listObj_) {
if (obj->IsDeleted()) continue;
if (itemType != nullptr && (*itemType != obj->GetItemType())) continue;

if (Math::HypotSq<int>(cx - obj->GetPositionX(), cy - obj->GetPositionY()) <= rr)
int sx = obj->GetPositionX();
int sy = obj->GetPositionY();

bool bInCircle = radius == nullptr;
if (!bInCircle) {
bool bPassAABB = (sx > rect_x1 && sy > rect_y1) && (sx < rect_x2&& sy < rect_y2);
bInCircle = bPassAABB && Math::HypotSq<int64_t>(cx - sx, cy - sy) <= rr;
}
if (bInCircle)
res.push_back(obj->GetObjectID());
}

Expand Down
2 changes: 1 addition & 1 deletion source/TouhouDanmakufu/Common/StgItem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class StgItemManager {
void CollectItemsInCircle(const DxCircle& circle);
void CancelCollectItems();

std::vector<int> GetItemIdInCircle(int cx, int cy, int radius, int* itemType);
std::vector<int> GetItemIdInCircle(int cx, int cy, int* radius, int* itemType);

bool IsDefaultBonusItemEnable() { return bDefaultBonusItemEnable_; }
void SetDefaultBonusItemEnable(bool bEnable) { bDefaultBonusItemEnable_ = bEnable; }
Expand Down
109 changes: 108 additions & 1 deletion source/TouhouDanmakufu/Common/StgShot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ void StgShotManager::AddShot(ref_unsync_ptr<StgShotObject> obj) {
listObj_.push_back(obj);
}

void StgShotManager::DeleteInCircle(int typeDelete, int typeTo, int typeOwner, int cx, int cy, int* radius) {
size_t StgShotManager::DeleteInCircle(int typeDelete, int typeTo, int typeOwner, int cx, int cy, int* radius) {
int r = radius ? *radius : 0;
int rr = r * r;

DxRect<int> rcBox(cx - r, cy - r, cx + r, cy + r);

size_t res = 0;

for (ref_unsync_ptr<StgShotObject>& obj : listObj_) {
if (obj->IsDeleted()) continue;
if ((typeOwner != StgShotObject::OWNER_NULL) && (obj->GetOwnerType() != typeOwner)) continue;
Expand All @@ -179,6 +181,13 @@ void StgShotManager::DeleteInCircle(int typeDelete, int typeTo, int typeOwner, i
int sy = obj->GetPositionY();

if (radius == nullptr || (rcBox.IsPointIntersected(sx, sy) && Math::HypotSq<int64_t>(cx - sx, cy - sy) <= rr)) {
if (obj->GetObjectType() == TypeObject::Shot)
++res;
else {
StgLaserObject* laser = dynamic_cast<StgLaserObject*>(obj.get());
res += floor(laser->GetLength() / laser->GetItemDistance());
}

if (typeTo == TO_TYPE_IMMEDIATE)
obj->DeleteImmediate();
else if (typeTo == TO_TYPE_FADE)
Expand All @@ -187,6 +196,63 @@ void StgShotManager::DeleteInCircle(int typeDelete, int typeTo, int typeOwner, i
obj->ConvertToItem();
}
}

return res;
}
size_t StgShotManager::DeleteInRegularPolygon(int typeDelete, int typeTo, int typeOwner, int cx, int cy, int* radius, int edges, double angle) {
int r = radius ? *radius : 0;

int rect_x1 = cx - r;
int rect_y1 = cy - r;
int rect_x2 = cx + r;
int rect_y2 = cy + r;

size_t res = 0;

for (ref_unsync_ptr<StgShotObject>& obj : listObj_) {
if (obj->IsDeleted()) continue;
if ((typeOwner != StgShotObject::OWNER_NULL) && (obj->GetOwnerType() != typeOwner)) continue;
if (typeDelete == DEL_TYPE_SHOT && obj->IsSpellResist()) continue;

int sx = obj->GetPositionX();
int sy = obj->GetPositionY();

bool bInPolygon = radius == nullptr;
if (!bInPolygon && ((sx > rect_x1 && sy > rect_y1) && (sx < rect_x2 && sy < rect_y2))) {
float f = GM_PI / (float)edges;
float cf = cosf(f);
float dx = sx - cx;
float dy = sy - cy;
float dist = hypotf(dy, dx);

bInPolygon = dist <= r;
if (bInPolygon) {
double r_apothem = r * cf;
bInPolygon = dist <= r_apothem;
if (!bInPolygon) {
double ang = fmod(Math::NormalizeAngleRad(atan2(dy, dx)) - Math::DegreeToRadian(angle), 2 * f);
bInPolygon = dist <= (r_apothem / cos(ang - f));
}
}
}
if (bInPolygon) {
if (obj->GetObjectType() == TypeObject::Shot)
++res;
else {
StgLaserObject* laser = dynamic_cast<StgLaserObject*>(obj.get());
res += floor(laser->GetLength() / laser->GetItemDistance());
}

if (typeTo == TO_TYPE_IMMEDIATE)
obj->DeleteImmediate();
else if (typeTo == TO_TYPE_FADE)
obj->SetFadeDelete();
else if (typeTo == TO_TYPE_ITEM)
obj->ConvertToItem();
}
}

return res;
}

std::vector<int> StgShotManager::GetShotIdInCircle(int typeOwner, int cx, int cy, int* radius) {
Expand All @@ -210,6 +276,47 @@ std::vector<int> StgShotManager::GetShotIdInCircle(int typeOwner, int cx, int cy

return res;
}
std::vector<int> StgShotManager::GetShotIdInRegularPolygon(int typeOwner, int cx, int cy, int* radius, int edges, double angle) {
int r = radius ? *radius : 0;
int rr = r * r;

int rect_x1 = cx - r;
int rect_y1 = cy - r;
int rect_x2 = cx + r;
int rect_y2 = cy + r;

std::vector<int> res;
for (ref_unsync_ptr<StgShotObject>& obj : listObj_) {
if (obj->IsDeleted()) continue;
if ((typeOwner != StgShotObject::OWNER_NULL) && (obj->GetOwnerType() != typeOwner)) continue;

int sx = obj->GetPositionX();
int sy = obj->GetPositionY();

bool bInPolygon = radius == nullptr;
if (!bInPolygon && ((sx > rect_x1 && sy > rect_y1) && (sx < rect_x2 && sy < rect_y2))) {
float f = GM_PI / (float)edges;
float cf = cosf(f);
float dx = sx - cx;
float dy = sy - cy;
float dist = hypotf(dy, dx);

bInPolygon = dist <= r;
if (bInPolygon) {
double r_apothem = r * cf;
bInPolygon = dist <= r_apothem;
if (!bInPolygon) {
double ang = fmod(Math::NormalizeAngleRad(atan2(dy, dx)) - Math::DegreeToRadian(angle), 2 * f);
bInPolygon = dist <= (r_apothem / cos(ang - f));
}
}
}
if (bInPolygon)
res.push_back(obj->GetObjectID());
}

return res;
}
size_t StgShotManager::GetShotCount(int typeOwner) {
size_t res = 0;

Expand Down
5 changes: 4 additions & 1 deletion source/TouhouDanmakufu/Common/StgShot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ class StgShotManager {
filterMag_ = mag;
}

void DeleteInCircle(int typeDelete, int typeTo, int typeOwner, int cx, int cy, int* radius);
size_t DeleteInCircle(int typeDelete, int typeTo, int typeOwner, int cx, int cy, int* radius);
size_t DeleteInRegularPolygon(int typeDelete, int typeTo, int typeOwner, int cx, int cy, int* radius, int edges, double angle);
std::vector<int> GetShotIdInCircle(int typeOwner, int cx, int cy, int* radius);
std::vector<int> GetShotIdInRegularPolygon(int typeOwner, int cx, int cy, int* radius, int edges, double angle);
size_t GetShotCount(int typeOwner);
size_t GetShotCountAll() { return listObj_.size(); }

Expand Down Expand Up @@ -537,6 +539,7 @@ class StgLaserObject : public StgShotObject {
void SetInvalidLength(float start, float end) { invalidLengthStart_ = start; invalidLengthEnd_ = end; }

void SetItemDistance(float dist) { itemDistance_ = std::max(dist, 0.1f); }
float GetItemDistance() { return itemDistance_; }
};

//*******************************************************************
Expand Down
Loading