Skip to content

Commit

Permalink
Merge pull request #1 from NicholasLogan/delete-shot-return
Browse files Browse the repository at this point in the history
Meaningful return values for and expansion of DeleteShot family
  • Loading branch information
nazjun authored Aug 1, 2021
2 parents 61c48fb + 956e5f9 commit a3cfb0a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
56 changes: 55 additions & 1 deletion source/TouhouDanmakufu/Common/StgShot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ 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;

Expand All @@ -195,6 +195,8 @@ void StgShotManager::DeleteInCircle(int typeDelete, int typeTo, int typeOwner, i
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;
Expand All @@ -215,8 +217,60 @@ void StgShotManager::DeleteInCircle(int typeDelete, int typeTo, int typeOwner, i
obj->SetFadeDelete();
else if (typeTo == TO_TYPE_ITEM)
obj->ConvertToItem(false);
++res;
}
}

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 (typeTo == TO_TYPE_IMMEDIATE)
obj->DeleteImmediate();
else if (typeTo == TO_TYPE_FADE)
obj->SetFadeDelete();
else if (typeTo == TO_TYPE_ITEM)
obj->ConvertToItem(false);
++res;
}
}

return res;
}

std::vector<int> StgShotManager::GetShotIdInCircle(int typeOwner, int cx, int cy, int* radius) {
Expand Down
3 changes: 2 additions & 1 deletion source/TouhouDanmakufu/Common/StgShot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class StgShotManager {
void SetShotDeleteClip(const DxRect<LONG>& clip) { rcDeleteClip_ = clip; }
DxRect<LONG>* GetShotDeleteClip() { return &rcDeleteClip_; }

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);
size_t GetShotCount(int typeOwner);
size_t GetShotCountAll() { return listObj_.size(); }
Expand Down
37 changes: 33 additions & 4 deletions source/TouhouDanmakufu/Common/StgStageScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ static const std::vector<function> stgStageFunction = {
//STG共通関数:弾
{ "DeleteShotAll", StgStageScript::Func_DeleteShotAll, 2 },
{ "DeleteShotInCircle", StgStageScript::Func_DeleteShotInCircle, 5 },
{ "DeleteShotInRegularPolygon", StgStageScript::Func_DeleteShotInRegularPolygon, 7 },
{ "CreateShotA1", StgStageScript::Func_CreateShotA1, 6 },
{ "CreateShotPA1", StgStageScript::Func_CreateShotPA1, 8 },
{ "CreateShotA2", StgStageScript::Func_CreateShotA2, 8 }, //Deprecated, exists for compatibility
Expand Down Expand Up @@ -1439,9 +1440,9 @@ gstd::value StgStageScript::Func_DeleteShotAll(gstd::script_machine* machine, in
case TYPE_ITEM:typeTo = StgShotManager::TO_TYPE_ITEM; break;
}

stageController->GetShotManager()->DeleteInCircle(typeDel, typeTo, StgShotObject::OWNER_ENEMY, 0, 0, nullptr);
size_t res = stageController->GetShotManager()->DeleteInCircle(typeDel, typeTo, StgShotObject::OWNER_ENEMY, 0, 0, nullptr);

return value();
return script->CreateIntValue(res);
}
gstd::value StgStageScript::Func_DeleteShotInCircle(gstd::script_machine* machine, int argc, const gstd::value* argv) {
StgStageScript* script = (StgStageScript*)machine->data;
Expand All @@ -1465,9 +1466,37 @@ gstd::value StgStageScript::Func_DeleteShotInCircle(gstd::script_machine* machin
case TYPE_ITEM:typeTo = StgShotManager::TO_TYPE_ITEM; break;
}

stageController->GetShotManager()->DeleteInCircle(typeDel, typeTo, StgShotObject::OWNER_ENEMY, posX, posY, &radius);
size_t res = stageController->GetShotManager()->DeleteInCircle(typeDel, typeTo, StgShotObject::OWNER_ENEMY, posX, posY, &radius);

return value();
return script->CreateIntValue(res);
}
gstd::value StgStageScript::Func_DeleteShotInRegularPolygon(gstd::script_machine* machine, int argc, const gstd::value* argv) {
StgStageScript* script = (StgStageScript*)machine->data;
StgStageController* stageController = script->stageController_;

int typeDel = argv[0].as_int();
int typeTo = argv[1].as_int();
int posX = argv[2].as_real();
int posY = argv[3].as_real();
int radius = argv[4].as_real();
int edges = argv[5].as_int();
double angle = argv[6].as_real();

switch (typeDel) {
case TYPE_ALL:typeDel = StgShotManager::DEL_TYPE_ALL; break;
case TYPE_SHOT:typeDel = StgShotManager::DEL_TYPE_SHOT; break;
case TYPE_CHILD:typeDel = StgShotManager::DEL_TYPE_CHILD; break;
}

switch (typeTo) {
case TYPE_IMMEDIATE:typeTo = StgShotManager::TO_TYPE_IMMEDIATE; break;
case TYPE_FADE:typeTo = StgShotManager::TO_TYPE_FADE; break;
case TYPE_ITEM:typeTo = StgShotManager::TO_TYPE_ITEM; break;
}

size_t res = stageController->GetShotManager()->DeleteInRegularPolygon(typeDel, typeTo, StgShotObject::OWNER_ENEMY, posX, posY, &radius, edges, angle);

return script->CreateIntValue(res);
}
gstd::value StgStageScript::Func_CreateShotA1(gstd::script_machine* machine, int argc, const gstd::value* argv) {
StgStageScript* script = (StgStageScript*)machine->data;
Expand Down
1 change: 1 addition & 0 deletions source/TouhouDanmakufu/Common/StgStageScript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class StgStageScript : public StgControlScript {
//STG共通関数:弾
static gstd::value Func_DeleteShotAll(gstd::script_machine* machine, int argc, const gstd::value* argv);
static gstd::value Func_DeleteShotInCircle(gstd::script_machine* machine, int argc, const gstd::value* argv);
static gstd::value Func_DeleteShotInRegularPolygon(gstd::script_machine* machine, int argc, const gstd::value* argv);
static gstd::value Func_CreateShotA1(gstd::script_machine* machine, int argc, const gstd::value* argv);
static gstd::value Func_CreateShotPA1(gstd::script_machine* machine, int argc, const gstd::value* argv);
static gstd::value Func_CreateShotA2(gstd::script_machine* machine, int argc, const gstd::value* argv);
Expand Down
Binary file not shown.

0 comments on commit a3cfb0a

Please sign in to comment.