From bca90893d27423e176c9a6136c354a2e1faffdb5 Mon Sep 17 00:00:00 2001 From: Zwip-Zwap Zapony Date: Sun, 20 Mar 2022 09:52:47 +0100 Subject: [PATCH 1/2] Fix Ideya Capture sometimes stealing extra spheres Also clean up redundant "startingspheres" variable --- src/p_user.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/p_user.c b/src/p_user.c index 1f14d96c1..1b16064ca 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -6695,7 +6695,7 @@ static void P_NightsTransferPoints(player_t *player, fixed_t xspeed, fixed_t rad // static void P_DoNiGHTSCapsule(player_t *player) { - INT32 i, spherecount, totalduration, popduration, deductinterval, deductquantity, sphereresult, firstpoptic, startingspheres; + INT32 i, spherecount, totalduration, popduration, deductinterval, deductquantity, sphereresult, firstpoptic; INT32 tictimer = ++player->capsule->extravalue2; if (abs(player->mo->x-player->capsule->x) <= 3*FRACUNIT) @@ -6820,13 +6820,19 @@ static void P_DoNiGHTSCapsule(player_t *player) { player->spheres -= deductquantity; player->capsule->health -= deductquantity; + + // If spherecount isn't a multiple of deductquantity, the final deduction might steal too many spheres from the player + // E.g. with 80 capsule health, deductquantity is 3, 3*26 is 78, 78+3=81, and then it'll have stolen more than the 80 that it was meant to! + // So let's check for that and unsteal the extra ones ~Zwip-Zwap Zapony, 2022-20-03 + if (player->capsule->health < sphereresult) + { + player->spheres += sphereresult - player->capsule->health; // Give the player the "stolen" spheres back + player->capsule->health = sphereresult; // Un-deduct the capsule health by the "stolen" spheres. Often, this just sets it to 0 + } + + if (player->spheres < 0) // This probably can't happen, since we give the "stolen" spheres back, but better safe than sorry + player->spheres = 0; } - - if (player->spheres < 0) - player->spheres = 0; - - if (player->capsule->health < sphereresult) - player->capsule->health = sphereresult; } // Spawn a 'pop' for every 2 tics @@ -6847,9 +6853,8 @@ static void P_DoNiGHTSCapsule(player_t *player) } else { - startingspheres = player->spheres - player->capsule->health; + player->spheres -= player->capsule->health; player->capsule->health = 0; - player->spheres = startingspheres; } } From 7fdebdcdb9fb0cf2378543f8e8f54ae4714756a3 Mon Sep 17 00:00:00 2001 From: Zwip-Zwap Zapony Date: Sun, 20 Mar 2022 13:18:22 +0100 Subject: [PATCH 2/2] Suggested optimization --- src/p_user.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/p_user.c b/src/p_user.c index 1b16064ca..f2ff7efc6 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -6818,20 +6818,19 @@ static void P_DoNiGHTSCapsule(player_t *player) if (player->capsule->health > sphereresult && player->spheres > 0) { + // If spherecount isn't a multiple of deductquantity, the final deduction might steal too many spheres from the player + // E.g. with 80 capsule health, deductquantity is 3, 3*26 is 78, 78+3=81, and then it'll have stolen more than the 80 that it was meant to! + // So let's adjust deductquantity accordingly for the final deduction + deductquantity = min(deductquantity, player->capsule->health - sphereresult); + player->spheres -= deductquantity; player->capsule->health -= deductquantity; - // If spherecount isn't a multiple of deductquantity, the final deduction might steal too many spheres from the player - // E.g. with 80 capsule health, deductquantity is 3, 3*26 is 78, 78+3=81, and then it'll have stolen more than the 80 that it was meant to! - // So let's check for that and unsteal the extra ones ~Zwip-Zwap Zapony, 2022-20-03 - if (player->capsule->health < sphereresult) - { - player->spheres += sphereresult - player->capsule->health; // Give the player the "stolen" spheres back - player->capsule->health = sphereresult; // Un-deduct the capsule health by the "stolen" spheres. Often, this just sets it to 0 - } - - if (player->spheres < 0) // This probably can't happen, since we give the "stolen" spheres back, but better safe than sorry + if (player->spheres < 0) // This can't happen... without Lua, setrings, et cetera player->spheres = 0; + + //if (player->capsule->health < sphereresult) // This can't happen + //player->capsule->health = sphereresult; } }