From c6659ef656ea4540d25134e64ac6508b17c4ea8d Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 28 Apr 2020 16:17:10 -0700 Subject: [PATCH] Initialize oldxp/oldyp in createentity() This prevents undefined behavior because we use oldxp/oldyp to do linear interpolation. It's also initialized in entclass::entclass(), just to be sure. And I've deduplicated the regular xp/yp initialization in createentity(), too. --- desktop_version/src/Ent.cpp | 5 +++ desktop_version/src/Entity.cpp | 74 ++++------------------------------ 2 files changed, 13 insertions(+), 66 deletions(-) diff --git a/desktop_version/src/Ent.cpp b/desktop_version/src/Ent.cpp index 5e8a1bfd..42628646 100644 --- a/desktop_version/src/Ent.cpp +++ b/desktop_version/src/Ent.cpp @@ -27,6 +27,8 @@ entclass::entclass() cy = 0; newxp = 0; newyp = 0; + oldxp = 0; + oldyp = 0; x1 = 0; y1 = 0; @@ -99,6 +101,7 @@ void entclass::setenemy( int t ) else if ( (para) == 1) { yp += 10; + oldyp += 10; tile = 63; animate = 100; //LIES colour = 6; @@ -138,7 +141,9 @@ void entclass::setenemy( int t ) else if ( (para) == 1) { xp += 4; + oldxp += 4; yp -= 4; + oldyp -= 4; tile = 76; animate = 100; // Clouds colour = 6; diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index 4cdbf91b..21dfb953 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -1163,6 +1163,10 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo //Rule 6 is a crew member entclass entity; + entity.xp = xp; + entity.yp = yp; + entity.oldxp = xp; + entity.oldyp = yp; entity.type = t; switch(t) { @@ -1170,8 +1174,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.rule = 0; //Playable character entity.tile = 0; entity.colour = 0; - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1184,8 +1186,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo break; case 1: //Simple enemy, bouncing off the walls entity.rule = 1; - entity.xp = xp; - entity.yp = yp; entity.behave = vx; entity.para = vy; entity.w = 16; @@ -1248,8 +1248,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo if (gridmatch(p1, p2, p3, p4, -32, -40, 352, 264)) entity.tile = 27; } - entity.xp = xp; - entity.yp = yp; entity.w = 32; entity.h = 8; @@ -1318,8 +1316,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo if (game.roomx == 50 && game.roomy == 52) entity.tile = 22; } - entity.xp = xp; - entity.yp = yp; entity.cy = -1; entity.w = 32; entity.h = 10; @@ -1335,8 +1331,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 3; entity.size = 1; entity.tile = 10; - entity.xp = xp; - entity.yp = yp; entity.cy = -1; entity.w = 8; entity.h = 10; @@ -1352,8 +1346,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 4; entity.size = 0; entity.tile = 11; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.behave = vx; @@ -1366,8 +1358,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 5; //Particles entity.colour = 1; entity.size = 3; - entity.xp = xp; - entity.yp = yp; entity.vx = vx; entity.vy = vy; @@ -1378,8 +1368,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 5; //Particles entity.colour = 2; entity.size = 3; - entity.xp = xp; - entity.yp = yp; entity.vx = vx; entity.vy = vy; @@ -1390,8 +1378,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 6; entity.size = 4; entity.tile = 48; - entity.xp = xp; - entity.yp = yp; entity.w = 8; entity.h = 8; entity.onentity = 1; @@ -1406,8 +1392,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 7; entity.size = 0; entity.tile = 22; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 3; @@ -1423,8 +1407,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 8; entity.size = 0; entity.tile = 20 + vx; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 4; @@ -1448,8 +1430,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 9; entity.size = 5; entity.life = 0; - entity.xp = xp; - entity.yp = yp; entity.w = vx; entity.h = 1; entity.onentity = 1; @@ -1459,8 +1439,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 10; entity.size = 6; entity.life = 0; - entity.xp = xp; - entity.yp = yp; entity.w = 1; entity.h = vx; //entity.colour = 0; @@ -1471,8 +1449,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 11; entity.size = 0; entity.tile = 18; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 10; @@ -1487,8 +1463,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 100; entity.size = 7; entity.tile = 1; //inactive - entity.xp = xp; - entity.yp = yp; entity.w = 96; entity.h = 96; entity.colour = 100; @@ -1501,8 +1475,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 12; //A special case! entity.tile = 144; entity.colour = 13; //144 for sad :( - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1518,8 +1490,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 12; //A special case! entity.tile = 144+6; entity.colour = 14; //144 for sad (upside down+12):( - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1535,8 +1505,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 12; //A special case! entity.tile = 144; entity.colour = 16; //144 for sad :( - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1560,8 +1528,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo { entity.tile = 144; } - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1583,8 +1549,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 12; //A special case! entity.tile = 0; entity.colour = 6; //54 for sad :( - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1600,8 +1564,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 13; entity.size = 0; entity.tile = 16 + vx; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 4; @@ -1614,8 +1576,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 13; entity.size = 0; entity.tile = 16 + vx; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 4; @@ -1628,8 +1588,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 7; entity.size = 0; entity.tile = 22; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 3; @@ -1644,8 +1602,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo //Given a different behavior, these enemies are especially for SWN mode and disappear outside the screen. entity.rule = 1; entity.type = 23; - entity.xp = xp; - entity.yp = yp; entity.behave = vx; entity.para = vy; entity.w = 16; @@ -1693,8 +1649,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo { entity.tile = 144; } - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1720,8 +1674,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.rule = 3; entity.type = 15; entity.size = 0; - entity.xp = xp; - entity.yp = yp; entity.w = 16; entity.h = 16; entity.colour = 4; @@ -1897,8 +1849,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 11; entity.size = 0; entity.tile = 18; - entity.xp = (xp); - entity.yp = (yp); entity.w = 16; entity.h = 16; entity.colour = 3; @@ -1913,8 +1863,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 51; entity.size = 6; entity.life = 0; - entity.xp = xp; - entity.yp = yp; entity.w = 1; entity.h = vx; //entity.colour = 0; @@ -1927,8 +1875,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 52; entity.size = 6; entity.life = 0; - entity.xp = xp; - entity.yp = yp; entity.w = 1; entity.h = vx; //entity.colour = 0; @@ -1941,8 +1887,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 53; entity.size = 5; entity.life = 0; - entity.xp = xp; - entity.yp = yp; entity.w = vx; entity.h = 1; entity.onentity = 1; @@ -1954,8 +1898,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.type = 54; entity.size = 5; entity.life = 0; - entity.xp = xp; - entity.yp = yp; entity.w = vx; entity.h = 1; entity.onentity = 1; @@ -1973,8 +1915,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo entity.tile = 0; } entity.colour = crewcolour(int(vy)); - entity.xp = xp; - entity.yp = yp; entity.cx = 6; entity.cy = 2; entity.w = 12; @@ -1994,8 +1934,6 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo case 56: //Custom enemy entity.rule = 1; entity.type = 1; - entity.xp = xp; - entity.yp = yp; entity.behave = vx; entity.para = vy; entity.w = 16; @@ -2380,17 +2318,20 @@ bool entityclass::updateentities( int i ) { entities[i].tile = 120; entities[i].yp = (28*8)-62; + entities[i].oldyp = (28*8)-62; } else { entities[i].tile = 96; entities[i].yp = 24; + entities[i].oldyp = 24; } //now, x position if (player > -1 && entities[player].xp > 20 * 8) { //approach from the left entities[i].xp = -64; + entities[i].oldxp = -64; entities[i].state = 2; bool entitygone = updateentities(i); //right if (entitygone) return true; @@ -2399,6 +2340,7 @@ bool entityclass::updateentities( int i ) { //approach from the left entities[i].xp = 320; + entities[i].oldxp = 320; entities[i].state = 3; bool entitygone = updateentities(i); //left if (entitygone) return true;