From 9ac1ce04d6e24bbcbeb6eb3ea658b947b4614a04 Mon Sep 17 00:00:00 2001 From: terminx Date: Sun, 19 May 2019 03:53:46 +0000 Subject: [PATCH] Simplify CON_FINDNEARACTOR family of functions and restore the original behavior of returning the nearest sprite instead of any sprite that satisfies the condition git-svn-id: https://svn.eduke32.com/eduke32@7641 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/duke3d/src/gameexec.cpp | 105 ++++++++++++++------------------- 1 file changed, 43 insertions(+), 62 deletions(-) diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index 76c123035..fe16b2a0a 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -5261,60 +5261,41 @@ badindex: // that is of into // -1 for none found // - int const findPicnum = *insptr++; - int const maxDist = Gv_GetVarX(*insptr++); - int const returnVar = *insptr++; + bool const actorsOnly = (tw == CON_FINDNEARACTOR || tw == CON_FINDNEARACTOR3D); + auto const dist_funcptr = (tw == CON_FINDNEARACTOR || tw == CON_FINDNEARSPRITE) ? &ldist : &dist; + int const findTile = *insptr++; + int maxDist = Gv_GetVarX(*insptr++); + int const returnVar = *insptr++; + + int findStatnum = actorsOnly ? STAT_ACTOR : MAXSTATUS - 1; int foundSprite = -1; - int findStatnum = STAT_ACTOR; - int spriteNum; - - if (tw == CON_FINDNEARSPRITE || tw == CON_FINDNEARSPRITE3D) - findStatnum = MAXSTATUS - 1; - - if (tw == CON_FINDNEARACTOR3D || tw == CON_FINDNEARSPRITE3D) - { - do - { - spriteNum = headspritestat[findStatnum]; // all sprites - - while (spriteNum >= 0) - { - if (sprite[spriteNum].picnum == findPicnum && spriteNum != vm.spriteNum - && dist(vm.pSprite, &sprite[spriteNum]) < maxDist) - { - foundSprite = spriteNum; - spriteNum = MAXSPRITES; - break; - } - spriteNum = nextspritestat[spriteNum]; - } - if (spriteNum == MAXSPRITES || tw == CON_FINDNEARACTOR3D) - break; - } while (findStatnum--); - Gv_SetVarX(returnVar, foundSprite); - dispatch(); - } do { - spriteNum = headspritestat[findStatnum]; // all sprites + int spriteNum = headspritestat[findStatnum]; // all sprites - while (spriteNum >= 0) + while ((unsigned)spriteNum < MAXSPRITES) { - if (sprite[spriteNum].picnum == findPicnum && spriteNum != vm.spriteNum - && ldist(vm.pSprite, &sprite[spriteNum]) < maxDist) + if (sprite[spriteNum].picnum == findTile && spriteNum != vm.spriteNum) { - foundSprite = spriteNum; - spriteNum = MAXSPRITES; - break; + int const foundDist = dist_funcptr(vm.pSprite, &sprite[spriteNum]); + + if (foundDist < maxDist) + { + maxDist = foundDist; + foundSprite = spriteNum; + } } + spriteNum = nextspritestat[spriteNum]; } - if (spriteNum == MAXSPRITES || tw == CON_FINDNEARACTOR) + if (actorsOnly) break; - } while (findStatnum--); + } + while (findStatnum--); + Gv_SetVarX(returnVar, foundSprite); dispatch(); } @@ -5328,42 +5309,42 @@ badindex: // that is of into // -1 for none found // - int const findPicnum = *insptr++; - int const maxDist = Gv_GetVarX(*insptr++); - int const maxZDist = Gv_GetVarX(*insptr++); - int const returnVar = *insptr++; + bool const actorsOnly = (tw == CON_FINDNEARACTORZ); + int const findTile = *insptr++; + int maxDist = Gv_GetVarX(*insptr++); + int const maxZDist = Gv_GetVarX(*insptr++); + int const returnVar = *insptr++; + + int findStatnum = actorsOnly ? STAT_ACTOR : MAXSTATUS - 1; int foundSprite = -1; - int findStatnum = MAXSTATUS - 1; do { - int spriteNum = headspritestat[tw == CON_FINDNEARACTORZ ? STAT_ACTOR : findStatnum]; // all sprites + int spriteNum = headspritestat[findStatnum]; // all sprites - if (spriteNum == -1) - continue; - do + while ((unsigned)spriteNum < MAXSPRITES) { - if (sprite[spriteNum].picnum == findPicnum && spriteNum != vm.spriteNum) + if (sprite[spriteNum].picnum == findTile && spriteNum != vm.spriteNum) { - if (ldist(vm.pSprite, &sprite[spriteNum]) < maxDist) + int const foundDist = ldist(vm.pSprite, &sprite[spriteNum]); + + if (foundDist < maxDist && klabs(vm.pSprite->z - sprite[spriteNum].z) < maxZDist) { - if (klabs(vm.pSprite->z - sprite[spriteNum].z) < maxZDist) - { - foundSprite = spriteNum; - spriteNum = MAXSPRITES; - break; - } + maxDist = foundDist; + foundSprite = spriteNum; } } + spriteNum = nextspritestat[spriteNum]; - } while (spriteNum >= 0); + } - if (tw == CON_FINDNEARACTORZ || spriteNum == MAXSPRITES) + if (actorsOnly) break; - } while (findStatnum--); - Gv_SetVarX(returnVar, foundSprite); + } + while (findStatnum--); + Gv_SetVarX(returnVar, foundSprite); dispatch(); }