From 3ed0e82aba4b4a2b65f7195bc4446106ff6a0bb9 Mon Sep 17 00:00:00 2001 From: Johnny Cai Date: Sun, 1 Aug 2021 19:10:09 -0400 Subject: [PATCH] Check against nil; More friendly to new users --- changelog.txt | 10 +++++++ control.lua | 4 ++- info.json | 2 +- logic/guiController.lua | 18 +++++++++---- logic/planeCollisions.lua | 3 +-- logic/planeManager.lua | 2 +- logic/planePollution.lua | 11 +++----- logic/planeRunway.lua | 4 +-- logic/planeTakeoffLanding.lua | 51 ++++++++++++++++++----------------- logic/planeUtility.lua | 17 ++++++++---- logic/utility.lua | 13 +++++---- settings.lua | 6 ++--- 12 files changed, 81 insertions(+), 60 deletions(-) diff --git a/changelog.txt b/changelog.txt index 54a9a6e..3cd668f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,14 @@ --------------------------------------------------------------------------------------------------- +Version: 1.3.1 +Date: 2021-08-01 + Bugfixes: + - Fixed crash when trying to kill player in sandbox mode + Changes: + - Guard against modded fuels changing, resulting in current burn time > fuel burn time + - Handle planes with no burner + - Asserts fail on incorrect creation of grounded and airborne plane prototypes + - Only realistic turn radius is on by default (Less harsh for new users) +--------------------------------------------------------------------------------------------------- Version: 1.3.0 Date: 2021-06-24 Features: diff --git a/control.lua b/control.lua index 0499157..ace7ea8 100755 --- a/control.lua +++ b/control.lua @@ -29,7 +29,7 @@ function OnPlayerDrivingChangedState(e) -- If driver bailed, passenger become the pilot if passenger and not driver then e.entity.set_driver(passenger) - -- If passenger and driver jumps out, nothing hapens + -- If passenger and driver jumps out, plane crashes elseif not driver and not passenger then e.entity.die() end @@ -46,6 +46,7 @@ function OnPlayerDrivingChangedState(e) end -- Destroy gauges upon leaving a plane + -- The gauges are recreated later if the player is still in plane guiController.deleteGauges(player) end end @@ -60,6 +61,7 @@ end -- Special function for the helicopter mod function CheckHelicopterMod(player) + assert(player.vehicle) if player.vehicle.name == "heli-entity-_-" then planePollution.createPollution(settings, player.surface, player.vehicle) end diff --git a/info.json b/info.json index 204d4e4..11aa942 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "AircraftRealism", - "version": "1.3.0", + "version": "1.3.1", "title": "Aircraft Realism", "author": "haih_ys", "homepage": "https://forums.factorio.com/viewtopic.php?f=190&t=73964", diff --git a/logic/guiController.lua b/logic/guiController.lua index f971f76..28c4b91 100755 --- a/logic/guiController.lua +++ b/logic/guiController.lua @@ -27,7 +27,7 @@ end local function getGaugeGui(player) local gui_elements = mod_gui.get_frame_flow(player) - -- Find the aircraft gauge elemen + -- Find the aircraft gauge element -- gui_elements["aircraft-realism-gauge-frame"] for i,element in pairs(gui_elements.children) do @@ -47,6 +47,7 @@ end -------------------- -- Fuel gauge local function getFuelPercentage(player, game) + assert(player.vehicle) local emptySlots = 0 -- Empty fuel inventory slots local totalSlots = player.vehicle.prototype.burner_prototype.fuel_inventory_size -- Total fuel inventory slots @@ -90,13 +91,21 @@ local function getFuelGaugeLeftIndex(fuelPercentage) end local function getFuelGaugeRightIndex(player) - if not player.vehicle.burner.currently_burning then + assert(player.vehicle) + if not player.vehicle.burner or not player.vehicle.burner.currently_burning then return 0 end --Remaining energy of burning fuel compared to the full energy of the burning fuel local remainingBurningFuel = player.vehicle.burner.remaining_burning_fuel / player.vehicle.burner.currently_burning["fuel_value"] * 100 - return utils.roundNumber(remainingBurningFuel * 30 / 100) + local index = utils.roundNumber(remainingBurningFuel * 30 / 100) + + -- Guard against modded fuel values changing + if index > 30 then + index = 30 + end + + return index end -------------------- @@ -128,7 +137,7 @@ end -- Gets the takeoff speed if the plane is grounded, landing speed if plane is airborne -> km/h or mph speed local function getTakeoffLandingSpeed(player, settings) - + assert(player.vehicle) if planeUtils.isGroundedPlane(player.vehicle.prototype.order) then return settings.global["aircraft-takeoff-speed-" .. player.vehicle.name].value @@ -221,7 +230,6 @@ local function updateGaugeArrows(tick, player, settings, game) end end --- Return all the functions for gui back to control functions.deleteGauges = deleteGauges functions.updateGaugeArrows = updateGaugeArrows diff --git a/logic/planeCollisions.lua b/logic/planeCollisions.lua index 700a062..027bc4e 100755 --- a/logic/planeCollisions.lua +++ b/logic/planeCollisions.lua @@ -1,4 +1,4 @@ --- Handles environmental collisions of the plane (cliffs, water) +-- Handles environmental collisions of the plane WITH DRIVER (cliffs, water) local planeUtils = require("logic.planeUtility") local function obstacleCollision(settings, surface, player, plane) @@ -43,7 +43,6 @@ local function obstacleCollision(settings, surface, player, plane) end end --- Makes these functions available to the lua script which requires this file local functions = {} functions.obstacleCollision = obstacleCollision diff --git a/logic/planeManager.lua b/logic/planeManager.lua index 4ae1642..ab39a86 100755 --- a/logic/planeManager.lua +++ b/logic/planeManager.lua @@ -16,6 +16,7 @@ end -- Checks the planes and performs all the functions a plane should do local function checkPlanes(e, player, game, defines, settings) + assert(player.vehicle) local quarterSecond = e.tick % 15 == 0 --15 ticks, 1/4 of a second if quarterSecond then @@ -51,7 +52,6 @@ local function checkPlanes(e, player, game, defines, settings) end --- Makes these functions available to the lua script which requires this file local functions = {} functions.checkPlanes = checkPlanes diff --git a/logic/planePollution.lua b/logic/planePollution.lua index 2364c72..a9436cc 100755 --- a/logic/planePollution.lua +++ b/logic/planePollution.lua @@ -1,18 +1,15 @@ -- Handles polluting the surface local function createPollution(settings, surface, plane) if settings.global["aircraft-emit-pollution"].value then - - -- More pollution is emitted at higher speeds, also depending on the fuel - local emissions = settings.global["aircraft-pollution-amount"].value - if plane.burner.currently_burning then + if plane.burner and plane.burner.currently_burning then + -- More pollution is emitted at higher speeds, also depending on the fuel + local emissions = settings.global["aircraft-pollution-amount"].value emissions = emissions * plane.burner.currently_burning.fuel_emissions_multiplier + surface.pollute(plane.position, emissions * math.abs(plane.speed)) end - - surface.pollute(plane.position, emissions * math.abs(plane.speed)) end end --- Makes these functions available to the lua script which requires this file local functions = {} functions.createPollution = createPollution diff --git a/logic/planeRunway.lua b/logic/planeRunway.lua index 97603e7..f6755cc 100755 --- a/logic/planeRunway.lua +++ b/logic/planeRunway.lua @@ -11,7 +11,6 @@ local function validateRunwayTile(settings, surface, plane) -- Cap the max speed to the max taxi speed when not on a runway if tile.prototype.vehicle_friction_modifier > settings.global["aircraft-realism-strict-runway-checking-maximum-tile-vehicle-friction"].value then - --Take off 10% of speed and damage the plane if past the max taxi speed by 2x if plane.speed > utils.toFactorioUnit(settings, settings.global["aircraft-realism-strict-runway-max-taxi-speed"].value) or plane.speed < -1 * utils.toFactorioUnit(settings, settings.global["aircraft-realism-strict-runway-max-taxi-speed"].value) then if plane.speed > 0 then @@ -20,7 +19,7 @@ local function validateRunwayTile(settings, surface, plane) plane.speed = plane.speed + 0.00925 end - -- Damage the plane if past the max taxi speed, this should only apply on landing, I added a margin of 20km/h so one should not be able to accelerate faster than it unless in jet or has afterburners + -- Damage the plane if past the max taxi speed, margin of 20km/h so less easy to accidently damage plane if plane.speed > utils.toFactorioUnit(settings, settings.global["aircraft-realism-strict-runway-max-taxi-speed"].value) + 0.09259 or plane.speed < -1 * utils.toFactorioUnit(settings, settings.global["aircraft-realism-strict-runway-max-taxi-speed"].value) - 0.009259 then plane.health = plane.health - 1 @@ -37,7 +36,6 @@ local function validateRunwayTile(settings, surface, plane) return true end --- Makes these functions available to the lua script which requires this file local functions = {} functions.validateRunwayTile = validateRunwayTile diff --git a/logic/planeTakeoffLanding.lua b/logic/planeTakeoffLanding.lua index 373017d..6e28f55 100755 --- a/logic/planeTakeoffLanding.lua +++ b/logic/planeTakeoffLanding.lua @@ -3,7 +3,8 @@ local utils = require("logic.utility") local planeUtils = require("logic.planeUtility") local function insertItems(oldInventory, newInventory) - if oldInventory and newInventory then + if oldInventory then + assert(newInventory, "Old plane has inventory, new plane does not. Check plane prototypes") -- With this method, inventory items stay in the same place for i = 1, #oldInventory, 1 do if i <= #newInventory then @@ -19,9 +20,13 @@ local function transitionPlane(oldPlane, newPlane, game, defines, takingOff) newPlane.copy_settings(oldPlane) -- Set Fuel bar - newPlane.burner.currently_burning = oldPlane.burner.currently_burning - newPlane.burner.remaining_burning_fuel = oldPlane.burner.remaining_burning_fuel + if oldPlane.burner then + assert(newPlane.burner, "Old plane has burner, new plane does not. Check plane prototypes") + newPlane.burner.currently_burning = oldPlane.burner.currently_burning + newPlane.burner.remaining_burning_fuel = oldPlane.burner.remaining_burning_fuel + end + -- The inventories cannot differ, or else items disappear -- Set fuel inventory insertItems(oldPlane.get_fuel_inventory(), newPlane.get_fuel_inventory()) @@ -32,34 +37,30 @@ local function transitionPlane(oldPlane, newPlane, game, defines, takingOff) insertItems(oldPlane.get_inventory(defines.inventory.car_ammo), newPlane.get_inventory(defines.inventory.car_ammo)) -- Select the last weapon - if oldPlane.selected_gun_index and newPlane.prototype.guns then - -- Validate that the 2 planes both have the same weapons - if newPlane.selected_gun_index and - utils.getTableLength(newPlane.prototype.guns) >= oldPlane.selected_gun_index then - newPlane.selected_gun_index = oldPlane.selected_gun_index - end + if oldPlane.selected_gun_index then + assert(utils.getTableLength(oldPlane.prototype.guns) == utils.getTableLength(newPlane.prototype.guns), "Old plane does not have same number of guns as new plane. Check plane prototypes") + newPlane.selected_gun_index = oldPlane.selected_gun_index end -- Transfer over equipment grid if oldPlane.grid then + assert(newPlane.grid, "Old plane has grid, new plane does not. Check plane prototypes") for index,item in pairs(oldPlane.grid.equipment) do local addedEquipment = newPlane.grid.put{name=item.name, position=item.position} + assert(addedEquipment, "Could not insert old plane equipment into new plane. Check plane prototypes") - if addedEquipment then - -- Transfer over charge and shield capacity - if item.energy ~= 0 then addedEquipment.energy = item.energy end - if item.shield ~= 0 then addedEquipment.shield = item.shield end + -- Transfer over charge and shield capacity + addedEquipment.energy = item.energy + addedEquipment.shield = item.shield - if item.burner ~= nil then - local burnerInv = item.burner.inventory + if item.burner then + assert(addedEquipment.burner, "Old plane equipment has burner, new plane equipment does not. Check plane prototypes") + -- Transfer burner contents + insertItems(item.burner.inventory, addedEquipment.burner.inventory) - -- Transfer burner contents - insertItems(burnerInv, addedEquipment.burner.inventory) - - addedEquipment.burner.currently_burning = item.burner.currently_burning - addedEquipment.burner.heat = item.burner.heat - addedEquipment.burner.remaining_burning_fuel = item.burner.remaining_burning_fuel - end + addedEquipment.burner.currently_burning = item.burner.currently_burning + addedEquipment.burner.heat = item.burner.heat + addedEquipment.burner.remaining_burning_fuel = item.burner.remaining_burning_fuel end end end @@ -94,6 +95,7 @@ local function transitionPlane(oldPlane, newPlane, game, defines, takingOff) end local function planeTakeoff(player, game, defines, settings) + assert(player.vehicle) -- If player is grounded and plane is greater than the specified takeoff speed if planeUtils.isGroundedPlane(player.vehicle.prototype.order) and player.vehicle.speed > utils.toFactorioUnit(settings, settings.global["aircraft-takeoff-speed-" .. player.vehicle.name].value) then @@ -123,6 +125,7 @@ local function planeTakeoff(player, game, defines, settings) end local function planeLand(player, game, defines, settings) + assert(player.vehicle) local groundedName = string.sub(player.vehicle.name, 0, string.len(player.vehicle.name) - string.len("-airborne")) -- If player is airborne and plane is less than the specified landing speed @@ -160,11 +163,9 @@ local function planeLand(player, game, defines, settings) end end --- Makes these functions available to the lua script which requires this file local functions = {} functions.planeTakeoff = planeTakeoff functions.planeLand = planeLand - -return functions \ No newline at end of file +return functions diff --git a/logic/planeUtility.lua b/logic/planeUtility.lua index b8b08e1..a8b6708 100755 --- a/logic/planeUtility.lua +++ b/logic/planeUtility.lua @@ -1,5 +1,3 @@ --- Test for plane type for plane name in the array of recognisedPlanes - -- Information is stored in the prototype order on whether or not it is a plane local function isGroundedPlane(order) local suffix = "-__Z9ZC_G" @@ -12,13 +10,22 @@ local function isAirbornePlane(order) end local function killDriverAndPassenger(plane, player) - plane.get_driver().die(player.force, plane) + local driver = plane.get_driver() + + -- get_driver() and get_passenger() returns LuaPlayer OR LuaEntity + -- die() only exists for LuaEntity + if driver and not driver.is_player() then + driver.die(player.force, plane) + end + local passenger = plane.get_passenger() - if passenger then passenger.die(player.force, plane) end + if passenger and not passenger.is_player() then + passenger.die(player.force, plane) + end + plane.die() end --- Makes these functions available to the lua script which requires this file local functions = {} functions.isGroundedPlane = isGroundedPlane diff --git a/logic/utility.lua b/logic/utility.lua index 70e8d4c..3ac9d11 100755 --- a/logic/utility.lua +++ b/logic/utility.lua @@ -2,7 +2,7 @@ local function toFactorioUnit(settings, kmH) if settings.global["aircraft-speed-unit"].value == "imperial" then - kmH = kmH * 1.609 --Thanks google! + kmH = kmH * 1.609 end -- Convert the lua speed into km/h with * 60 * 3.6 @@ -41,20 +41,19 @@ end local function roundNumber(number) if (number - (number % 0.1)) - (number - (number % 1)) < 0.5 then - number = number - (number % 1) + number = number - (number % 1) else - number = (number - (number % 1)) + 1 + number = (number - (number % 1)) + 1 end - return number - end + return number +end --- You have no idea how scared I am writing a new function in Lua after seeing nils from all sorts of places local function playSound(settings, player, soundName) if settings.get_player_settings(player)["aircraft-realism-sounds-enabled"].value then player.play_sound({path=soundName}) end end --- Makes these functions available to the lua script which requires this file + local functions = {} functions.toFactorioUnit = toFactorioUnit diff --git a/settings.lua b/settings.lua index 32ae511..e6a138d 100644 --- a/settings.lua +++ b/settings.lua @@ -12,21 +12,21 @@ data:extend({ type = "bool-setting", name = "aircraft-realism-acceleration", setting_type = "startup", - default_value = true, + default_value = false, order="aab" }, { type = "bool-setting", name = "aircraft-realism-braking-speed", setting_type = "startup", - default_value = true, + default_value = false, order="aac" }, { type = "bool-setting", name = "aircraft-realism-takeoff-health", setting_type = "startup", - default_value = true, + default_value = false, order="aad" }, {