Skip to content

Commit

Permalink
Merge branch '1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaihysc committed Aug 1, 2021
2 parents 14a3faf + 3ed0e82 commit c17a087
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 60 deletions.
10 changes: 10 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 3 additions & 1 deletion control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 13 additions & 5 deletions logic/guiController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

--------------------
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions logic/planeCollisions.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion logic/planeManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions logic/planePollution.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 1 addition & 3 deletions logic/planeRunway.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
51 changes: 26 additions & 25 deletions logic/planeTakeoffLanding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
return functions
17 changes: 12 additions & 5 deletions logic/planeUtility.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions logic/utility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand Down

0 comments on commit c17a087

Please sign in to comment.