From 9e6412a937ccf811d893f39e0ad4184889e1e73f Mon Sep 17 00:00:00 2001 From: Sevenfm Date: Wed, 8 Jan 2020 17:17:59 +0000 Subject: [PATCH] Lua: initialize return values to prevent returning garbage if soldier is not found. Improved r8104 fix to allow Fatima quest (civilians and NPCs with profile can go off screen). git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@8725 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- Strategic/LuaInitNPCs.cpp | 176 ++++++++++++++++++-------------------- Tactical/Overhead.cpp | 9 +- 2 files changed, 90 insertions(+), 95 deletions(-) diff --git a/Strategic/LuaInitNPCs.cpp b/Strategic/LuaInitNPCs.cpp index 51634477f..f7bbfcc40 100644 --- a/Strategic/LuaInitNPCs.cpp +++ b/Strategic/LuaInitNPCs.cpp @@ -5938,32 +5938,32 @@ static int l_usStrategicInsertionDataProfileID(lua_State *L) static int l_bNeutral(lua_State *L) { -BOOLEAN Bool; -SOLDIERTYPE * pSoldier; + BOOLEAN Bool = FALSE; + SOLDIERTYPE * pSoldier; - if ( lua_gettop(L) >= 1 ) + if (lua_gettop(L) >= 1) { - UINT8 ID = lua_tointeger(L,1); + UINT8 ID = lua_tointeger(L, 1); - pSoldier = FindSoldierByProfileID( ID, FALSE ); + pSoldier = FindSoldierByProfileID(ID, FALSE); if (pSoldier) - { - if ( pSoldier->aiData.bNeutral ) - Bool = TRUE; - else - Bool = FALSE; - } + { + if (pSoldier->aiData.bNeutral) + Bool = TRUE; + else + Bool = FALSE; + } lua_pushboolean(L, Bool); - } - -return 1; + } + + return 1; } static int l_CheckSoldierAlertStatus(lua_State *L) { -UINT8 Status_alert; -SOLDIERTYPE * pSoldier; + UINT8 Status_alert = NUM_STATUS_STATES; + SOLDIERTYPE * pSoldier; if ( lua_gettop(L) >= 1 ) { @@ -5974,10 +5974,8 @@ SOLDIERTYPE * pSoldier; Status_alert = pSoldier->aiData.bAlertStatus; lua_pushinteger(L, Status_alert); - } - -return 1; + return 1; } static int l_InitFace(lua_State *L) @@ -6081,8 +6079,8 @@ return 0; static int l_CheckSoldierNoiseVolume(lua_State *L) { -UINT8 NoiseVolume; -SOLDIERTYPE * pSoldier; + UINT8 NoiseVolume = NOWHERE; + SOLDIERTYPE * pSoldier; if ( lua_gettop(L) >= 1 ) { @@ -6090,14 +6088,13 @@ SOLDIERTYPE * pSoldier; pSoldier = FindSoldierByProfileID( ID, FALSE ); if (pSoldier) - { - + { NoiseVolume = pSoldier->aiData.ubNoiseVolume; - } + } lua_pushinteger(L, NoiseVolume); } -return 1; + return 1; } @@ -6246,9 +6243,8 @@ return 0; static int l_CheckAction(lua_State *L) { -UINT32 Action; - -SOLDIERTYPE * pSoldier; + UINT32 Action = AI_ACTION_NONE; + SOLDIERTYPE * pSoldier; if ( lua_gettop(L) >= 1 ) { @@ -6259,7 +6255,6 @@ SOLDIERTYPE * pSoldier; { Action = pSoldier->aiData.bAction; } - lua_pushinteger(L, Action); } @@ -6507,30 +6502,29 @@ return 0; static int l_SoldierTo3DLocationLineOfSightTest(lua_State *L) { -BOOLEAN Bool; -SOLDIERTYPE * pSoldier; + BOOLEAN Bool = FALSE; + SOLDIERTYPE * pSoldier; - if ( lua_gettop(L) >= 4 ) + if (lua_gettop(L) >= 4) { - UINT8 ID = lua_tointeger(L,1); - UINT32 sGridNo = lua_tointeger(L,2); - INT8 bLevel = lua_tointeger(L,3); - INT8 bCubeLevel = lua_tointeger(L,4); + UINT8 ID = lua_tointeger(L, 1); + UINT32 sGridNo = lua_tointeger(L, 2); + INT8 bLevel = lua_tointeger(L, 3); + INT8 bCubeLevel = lua_tointeger(L, 4); - - pSoldier = FindSoldierByProfileID( ID, FALSE ); + + pSoldier = FindSoldierByProfileID(ID, FALSE); if (pSoldier) { - if ( SoldierTo3DLocationLineOfSightTest( pSoldier, sGridNo, bLevel, bCubeLevel, TRUE ) ) + if (SoldierTo3DLocationLineOfSightTest(pSoldier, sGridNo, bLevel, bCubeLevel, TRUE)) Bool = TRUE; else - Bool = FALSE; + Bool = FALSE; } - - lua_pushboolean(L, Bool); - } - -return 1; + lua_pushboolean(L, Bool); + } + + return 1; } //------------ @@ -7998,15 +7992,15 @@ static int l_RecalculateOppCntsDueToNoLongerNeutral (lua_State *L) static int l_CheckSoldierActive (lua_State *L) { -SOLDIERTYPE *pSoldier; -BOOLEAN Bool; + SOLDIERTYPE *pSoldier; + BOOLEAN Bool = FALSE; if ( lua_gettop(L) >= 1 ) { UINT8 UID = lua_tointeger(L,1); pSoldier = FindSoldierByProfileID( UID, FALSE ); - if (pSoldier) + if (pSoldier) { if ( pSoldier->bActive ) Bool = TRUE; @@ -8017,7 +8011,7 @@ BOOLEAN Bool; lua_pushboolean(L, Bool); } -return 1; + return 1; } static int l_CheckSoldierInSector (lua_State *L) @@ -12198,79 +12192,79 @@ UINT8 UID; return 1; } -static int l_CheckSoldierSectorY (lua_State *L) +static int l_CheckSoldierSectorY(lua_State *L) { -UINT8 n = lua_gettop(L); -int i; -INT16 SectorY; -SOLDIERTYPE *pSoldier; -UINT8 UID; + UINT8 n = lua_gettop(L); + int i; + INT16 SectorY = 0; + SOLDIERTYPE *pSoldier; + UINT8 UID; - for (i= 1; i<=n; i++ ) + for (i = 1; i <= n; i++) { - if (i == 1 ) UID = lua_tointeger(L,i); + if (i == 1) UID = lua_tointeger(L, i); } - pSoldier = FindSoldierByProfileID( UID, FALSE ); - + pSoldier = FindSoldierByProfileID(UID, FALSE); + if (pSoldier) { - SectorY = pSoldier->sSectorY; + SectorY = pSoldier->sSectorY; } - + lua_pushinteger(L, SectorY); - -return 1; + + return 1; } -static int l_CheckSoldierSectorX (lua_State *L) +static int l_CheckSoldierSectorX(lua_State *L) { -UINT8 n = lua_gettop(L); -int i; -INT16 SectorX; -SOLDIERTYPE *pSoldier; -UINT8 UID; + UINT8 n = lua_gettop(L); + int i; + INT16 SectorX = 0; + SOLDIERTYPE *pSoldier; + UINT8 UID; - for (i= 1; i<=n; i++ ) + for (i = 1; i <= n; i++) { - if (i == 1 ) UID = lua_tointeger(L,i); + if (i == 1) UID = lua_tointeger(L, i); } - pSoldier = FindSoldierByProfileID( UID, FALSE ); - + pSoldier = FindSoldierByProfileID(UID, FALSE); + if (pSoldier) { - SectorX = pSoldier->sSectorX; + SectorX = pSoldier->sSectorX; } - + lua_pushinteger(L, SectorX); - -return 1; + + return 1; } -static int l_CheckSoldierSectorZ (lua_State *L) +static int l_CheckSoldierSectorZ(lua_State *L) { -UINT8 n = lua_gettop(L); -int i; -INT8 SectorZ; -SOLDIERTYPE *pSoldier; -UINT8 UID; + UINT8 n = lua_gettop(L); + int i; + INT8 SectorZ = 0; + SOLDIERTYPE *pSoldier; + UINT8 UID; - for (i= 1; i<=n; i++ ) + for (i = 1; i <= n; i++) { - if (i == 1 ) UID = lua_tointeger(L,i); + if (i == 1) UID = lua_tointeger(L, i); } - pSoldier = FindSoldierByProfileID( UID, FALSE ); - + pSoldier = FindSoldierByProfileID(UID, FALSE); + if (pSoldier) { - SectorZ = pSoldier->bSectorZ; + SectorZ = pSoldier->bSectorZ; } - + lua_pushinteger(L, SectorZ); - -return 1; + + return 1; } static int l_SetSoldierSectorY (lua_State *L) diff --git a/Tactical/Overhead.cpp b/Tactical/Overhead.cpp index 9195e1f9a..a2c914dd3 100644 --- a/Tactical/Overhead.cpp +++ b/Tactical/Overhead.cpp @@ -4863,10 +4863,11 @@ BOOLEAN NewOKDestination( SOLDIERTYPE * pCurrSoldier, INT32 sGridNo, BOOLEAN fPe INT16 sDesiredLevel; BOOLEAN fOKCheckStruct; - if ( !GridNoOnVisibleWorldTile( sGridNo ) ) - { - return( FALSE ); - } + // sevenfm: allow civilians and NPCs with profile to go off screen + if (!GridNoOnVisibleWorldTile(sGridNo) && (pCurrSoldier->bTeam != CIV_TEAM || pCurrSoldier->ubProfile == NO_PROFILE)) + { + return(FALSE); + } if (fPeopleToo && ( bPerson = WhoIsThere2( sGridNo, bLevel ) ) != NOBODY ) {