From cc7b7a8f9087f662f08024c57ea7ce2c5ba2a899 Mon Sep 17 00:00:00 2001 From: dj803 Date: Thu, 5 Nov 2026 11:23:07 +0200 Subject: [PATCH] Scenarios: skip destroyed or missing entities in 03, 06, 07, 08, 31, 44, 50, 55, 57 Destroyed entities lose all their methods (only isValid() works), so a script that keeps a reference and calls a method on it later raises a Lua error. GameGlobalInfo::update stops calling update() after 5 errors in a row, so a mission can freeze with no win or loss. Each hunk guards one such call; no behaviour changes while the entity is alive. - 03_waves: reward the first friendly station still alive, not friendlyList[1] - 06_edgeofspace: orderRoaming only on Kraylor ships that still exist - 07_gftp: return after victory("Ghosts"); the rest of update() uses stakhanov - 08_atlantis: target dummies may be destroyed before the scan phase ends; a destroyed dummy counts as scanned - 31_payload: PayloadShip checked in CheckWinCondition, DeterminePayloadTarget, HandlePayloadMovement; SafeAttackRandom returns false for an empty list (irandom(1, 0)) - 44_outpost: enemy_reverts created before the first table.insert - 50_gaps: nearStations with an empty list, areEnemiesInRange on a destroyed homeStation, "Where are you headed?" when the freighter's target is gone - 55_defenderHunter, 57_shoreline: same "Where are you headed?" guard; 55 endStatistics checks station:isValid(); 57 friendlyFreighterComms defines the local shipType its branches use Co-Authored-By: Claude Sonnet 5.5 --- scripts/scenario_03_waves.lua | 8 +++++- scripts/scenario_06_edgeofspace.lua | 8 +++--- scripts/scenario_07_gftp.lua | 1 + scripts/scenario_08_atlantis.lua | 15 ++++++----- scripts/scenario_31_payload.lua | 25 ++++++++++++++--- scripts/scenario_44_outpost.lua | 2 ++ scripts/scenario_50_gaps.lua | 18 +++++++++---- scripts/scenario_55_defenderHunter.lua | 14 +++++++--- scripts/scenario_57_shoreline.lua | 37 +++++++++++++++++++++----- 9 files changed, 98 insertions(+), 30 deletions(-) diff --git a/scripts/scenario_03_waves.lua b/scripts/scenario_03_waves.lua index 7c8ed757e5..361bd8fac2 100644 --- a/scripts/scenario_03_waves.lua +++ b/scripts/scenario_03_waves.lua @@ -125,7 +125,13 @@ end function spawnWave() waveNumber = waveNumber + 1 getPlayerShip(-1):addToShipLog(string.format(_("shipLog", "Wave %d"), waveNumber), "red") - friendlyList[1]:addReputationPoints(150 + waveNumber * 15) + -- friendlyList[1] may have been destroyed by an earlier wave; reward the first station still alive + for _, friendly in ipairs(friendlyList) do + if friendly:isValid() then + friendly:addReputationPoints(150 + waveNumber * 15) + break + end + end enemyList = {} diff --git a/scripts/scenario_06_edgeofspace.lua b/scripts/scenario_06_edgeofspace.lua index 35707ad158..e6bdf9ca4a 100644 --- a/scripts/scenario_06_edgeofspace.lua +++ b/scripts/scenario_06_edgeofspace.lua @@ -438,10 +438,10 @@ Thank you for defending our station. Please dock with us, and we'll analyze the string.format(_("centralcommand-incCall", "Apollo, come in!\n\nLeave the E.O.S. data with Galileo for now, we've confirmed reports that Kraylor are brazen enough to attack our E.O.S. scope directly! All available ships should converge on E.O.S. territory in sector %s!\n\nThat means you, Apollo!"),EOS_Station:getSectorName()) ) - kraylor_e1:orderRoaming() - kraylor_e2:orderRoaming() - kraylor_e3:orderRoaming() - kraylor_e4:orderRoaming() + if kraylor_e1:isValid() then kraylor_e1:orderRoaming() end + if kraylor_e2:isValid() then kraylor_e2:orderRoaming() end + if kraylor_e3:isValid() then kraylor_e3:orderRoaming() end + if kraylor_e4:isValid() then kraylor_e4:orderRoaming() end Human_m1:orderDefendLocation(60500, 42100) Human_m2:orderDefendLocation(60500, 42100) diff --git a/scripts/scenario_07_gftp.lua b/scripts/scenario_07_gftp.lua index ab83d727c9..dd6f415db8 100644 --- a/scripts/scenario_07_gftp.lua +++ b/scripts/scenario_07_gftp.lua @@ -259,6 +259,7 @@ function update(delta) -- Stakhanov must survive if not stakhanov:isValid() then victory("Ghosts") + return -- the rest of update() calls into stakhanov end -- The player must survive diff --git a/scripts/scenario_08_atlantis.lua b/scripts/scenario_08_atlantis.lua index 826091591d..3ec762dc88 100644 --- a/scripts/scenario_08_atlantis.lua +++ b/scripts/scenario_08_atlantis.lua @@ -352,8 +352,8 @@ end function phase1WaitForSupplyPickup(delta) -- Keep the shields of the dummies charged. - target_dummy_1:setShields(300) - target_dummy_2:setShields(300) + if target_dummy_1:isValid() then target_dummy_1:setShields(300) end + if target_dummy_2:isValid() then target_dummy_2:setShields(300) end if not supply_drop:isValid() then shipyard_gamma:sendCommsMessage( @@ -371,10 +371,11 @@ end function phase1ScanDummyShips(delta) -- Keep the shields of the dummies charged. (Note, at this point, you could destroy them with nukes, which is why we keep the shields at 300) - target_dummy_1:setShields(300) - target_dummy_2:setShields(300) + if target_dummy_1:isValid() then target_dummy_1:setShields(300) end + if target_dummy_2:isValid() then target_dummy_2:setShields(300) end - if target_dummy_1:isScannedBy(player) and target_dummy_2:isScannedBy(player) then + if (not target_dummy_1:isValid() or target_dummy_1:isScannedBy(player)) + and (not target_dummy_2:isValid() or target_dummy_2:isScannedBy(player)) then shipyard_gamma:sendCommsMessage( player, _("station-incCall", [[Perfect, Atlantis-1. They identify as Kraylor ships because we put fake IDs in them. @@ -384,8 +385,8 @@ Destroy Dummy-1 with your beam weapons, and use a homing missile to take out Dum The shields of Dummy-2 are configured so that your beam weapons will not penetrate them.]]) ) mission_state = phase1DestroyDummyShips - target_dummy_1:setShieldsMax(30) - target_dummy_2:setShieldsMax(30) + if target_dummy_1:isValid() then target_dummy_1:setShieldsMax(30) end + if target_dummy_2:isValid() then target_dummy_2:setShieldsMax(30) end player:addReputationPoints(5) end end diff --git a/scripts/scenario_31_payload.lua b/scripts/scenario_31_payload.lua index 84e3dfbe71..22ddf58a96 100755 --- a/scripts/scenario_31_payload.lua +++ b/scripts/scenario_31_payload.lua @@ -572,6 +572,11 @@ end function SafeAttackRandom(ship, shipList, changeTarget) local changeTarget = changeTarget or false + -- An empty list made shipList[irandom(1, 0)] nil and SafeOrder raised. + -- Callers read false as "keep formation". + if #shipList == 0 then + return false + end local randomEnemy = shipList[irandom(1, #shipList)] return SafeOrder(ship, randomEnemy, "Attack", changeTarget) end @@ -692,10 +697,14 @@ end -- function CheckWinCondition() - if PayloadShip:isDocked(FactionStation) then - victory("Kraylor") - elseif PayloadShip:isDocked(KraylorStation) then - victory("Human Navy") + -- A destroyed PayloadShip has no methods: isDocked below raised "attempt to + -- call a nil value" and stopped update(). Test validity first. + if PayloadShip:isValid() then + if PayloadShip:isDocked(FactionStation) then + victory("Kraylor") + elseif PayloadShip:isDocked(KraylorStation) then + victory("Human Navy") + end end if not (Player:isValid() and PayloadShip:isValid() @@ -713,6 +722,10 @@ end -- Update the Payload's target based on proximities and scan status function DeterminePayloadTarget() + -- A destroyed PayloadShip has no getPosition: CheckPayloadControl raised. + if PayloadShip == nil or not PayloadShip:isValid() then + return + end local oldTarget = PayloadShip.target local inControl, ourCount, oppositionCount = CheckPayloadControl(PayloadShip, "Human Navy", "Kraylor") @@ -831,6 +844,10 @@ end -- Function to handle Payload movement function HandlePayloadMovement() + -- A destroyed PayloadShip has no getOrder. + if PayloadShip == nil or not PayloadShip:isValid() then + return + end local currentOrder = PayloadShip:getOrder() if PayloadShip.target == nil then if currentOrder ~= "Idle" then diff --git a/scripts/scenario_44_outpost.lua b/scripts/scenario_44_outpost.lua index 94a6026098..4b295dad51 100644 --- a/scripts/scenario_44_outpost.lua +++ b/scripts/scenario_44_outpost.lua @@ -6186,6 +6186,7 @@ function enemyComms(comms_data) comms_target.original_target = original_target end comms_target.taunt_may_expire = true --change to conditional in future refactoring + if enemy_reverts == nil then enemy_reverts = {} end table.insert(enemy_reverts,comms_target) end comms_target:orderAttack(comms_source) --consider alternative options besides attack in future refactoring @@ -6230,6 +6231,7 @@ function enemyComms(comms_data) --print(original_target:getCallSign()) comms_target.original_target = original_target end + if enemy_reverts == nil then enemy_reverts = {} end table.insert(enemy_reverts,comms_target) end comms_target.amenability_may_expire = true --set up conditional in future refactoring diff --git a/scripts/scenario_50_gaps.lua b/scripts/scenario_50_gaps.lua index 24c1aa22f5..7083905fbb 100644 --- a/scripts/scenario_50_gaps.lua +++ b/scripts/scenario_50_gaps.lua @@ -424,11 +424,11 @@ end -- Transport ship generation and handling function nearStations(station, compareStationList) remainingStations = {} - if compareStationList[1]:isValid() then + if compareStationList[1] ~= nil and compareStationList[1]:isValid() then if station:getCallSign() ~= compareStationList[1]:getCallSign() then closest = compareStationList[1] else - if compareStationList[2]:isValid() then + if compareStationList[2] ~= nil and compareStationList[2]:isValid() then closest = compareStationList[2] end end @@ -1586,7 +1586,11 @@ function neutralComms() setCommsMessage(_("trade-comms", "Yes?")) -- Offer destination information addCommsReply(_("trade-comms", "Where are you headed?"), function() - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end addCommsReply(_("Back"), commsShip) end) -- Offer to trade goods if goods or equipment freighter @@ -1686,7 +1690,11 @@ function neutralComms() if not comms_source:takeReputationPoints(destRep) then setCommsMessage(_("needRep-comms", "Insufficient reputation")) else - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end end addCommsReply(_("Back"), commsShip) end) @@ -3157,7 +3165,7 @@ function waves(delta) end end end - if homeStation:areEnemiesInRange(2000) then + if homeStation:isValid() and homeStation:areEnemiesInRange(2000) then wakeEnemyFleet = homeStation:getObjectsInRange(2000) for _, enemy in ipairs(wakeEnemyFleet) do if enemy:isEnemy(homeStation) then diff --git a/scripts/scenario_55_defenderHunter.lua b/scripts/scenario_55_defenderHunter.lua index 41f5ad57c1..75d28965d0 100644 --- a/scripts/scenario_55_defenderHunter.lua +++ b/scripts/scenario_55_defenderHunter.lua @@ -3375,7 +3375,11 @@ function neutralComms() setCommsMessage(_("trade-comms", "Yes?")) -- Offer destination information addCommsReply(_("trade-comms", "Where are you headed?"), function() - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end addCommsReply(_("Back"), commsShip) end) -- Offer to trade goods if goods or equipment freighter @@ -3452,7 +3456,11 @@ function neutralComms() if not comms_source:takeReputationPoints(destRep) then setCommsMessage(_("needRep-comms", "Insufficient reputation")) else - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end end addCommsReply(_("Back"), commsShip) end) @@ -6226,7 +6234,7 @@ function endStatistics() survivedNeutralStations = 0 for idx, station in pairs(originalStationList) do tp = getPlayerShip(-1) - if tp ~= nil and tp:isValid() then + if tp ~= nil and tp:isValid() and station:isValid() then if station:isFriendly(tp) then if station:isValid() then survivedStations = survivedStations + 1 diff --git a/scripts/scenario_57_shoreline.lua b/scripts/scenario_57_shoreline.lua index 057dff540c..18671d5c84 100644 --- a/scripts/scenario_57_shoreline.lua +++ b/scripts/scenario_57_shoreline.lua @@ -8348,6 +8348,7 @@ function neutralFriendlyFreighterComms() end end function friendlyFreighterComms() + local shipType = comms_target:getTypeName() if comms_source.interaction == nil then comms_source.interaction = {} end @@ -8400,7 +8401,11 @@ function friendlyFreighterComms() end) -- Offer destination information addCommsReply(_("trade-comms", "Where are you headed?"), function() - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end addCommsReply(_("Back"), altShipComms) end) -- Offer to trade goods if goods or equipment freighter @@ -8480,7 +8485,11 @@ function friendlyFreighterComms() if not comms_source:takeReputationPoints(destRep) then setCommsMessage(_("needRep-comms", "Insufficient reputation")) else - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end end addCommsReply(_("Back"), altShipComms) end) @@ -8717,7 +8726,11 @@ function friendlyComms(comms_data) end) -- Offer destination information addCommsReply(_("trade-comms", "Where are you headed?"), function() - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end addCommsReply(_("Back"), altShipComms) end) -- Offer to trade goods if goods or equipment freighter @@ -8797,7 +8810,11 @@ function friendlyComms(comms_data) if not comms_source:takeReputationPoints(destRep) then setCommsMessage(_("needRep-comms", "Insufficient reputation")) else - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end end addCommsReply(_("Back"), altShipComms) end) @@ -9214,7 +9231,11 @@ function neutralComms(comms_data) end) -- Offer destination information addCommsReply(_("trade-comms", "Where are you headed?"), function() - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end addCommsReply(_("Back"), altShipComms) end) -- Offer to trade goods if goods or equipment freighter @@ -9294,7 +9315,11 @@ function neutralComms(comms_data) if not comms_source:takeReputationPoints(destRep) then setCommsMessage(_("needRep-comms", "Insufficient reputation")) else - setCommsMessage(comms_target.target:getCallSign()) + if comms_target.target ~= nil and comms_target.target:isValid() then + setCommsMessage(comms_target.target:getCallSign()) + else + setCommsMessage(_("shipAssist-comms", "I no longer have a destination.")) + end end addCommsReply(_("Back"), altShipComms) end)