Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d5a223d
Editors: fix overlapping selection groups
The-E Nov 11, 2026
e79ef17
QtFRED: keep the wing arrival distance when the target changes
The-E Nov 11, 2026
44361fb
Editors: share one minimum arrival distance rule
The-E Nov 11, 2026
0ea2871
QtFRED: fix mixed-value spin boxes in multi-edit dialogs
The-E Nov 11, 2026
5583eae
QtFRED: fix Initial Status with several marked ships
The-E Nov 11, 2026
147d9fd
AI: free Lua AI goal arguments before the Lua state closes
The-E Nov 11, 2026
66e6ca8
Editors: fix crashes at exit and the shutdown order
The-E Nov 11, 2026
82babca
QtFRED: fix the action leak in the recent-files menu
The-E Nov 11, 2026
36a4912
QtFRED: fix the save path after Save As and template loads
The-E Nov 11, 2026
9b71d97
QtFRED: do not make the FS1 source file the save target
The-E Nov 11, 2026
77defb7
QtFRED: fix autosave recovery
The-E Nov 11, 2026
50e5816
QtFRED: report a failed mission save
The-E Nov 11, 2026
4f2dfc2
QtFRED: make setSpinBoxValueOrBlank static
The-E Nov 14, 2026
6fa834b
FRED2: use arrival_min_distance in the ship editor
The-E Nov 14, 2026
a0eced8
QtFRED: write the autosave at each interval again
The-E Nov 16, 2026
78ad158
QtFRED: keep the mixed state of a spin box on Enter
The-E Nov 16, 2026
d2ab216
Editors: apply the arrival distance minimum to each ship
The-E Nov 16, 2026
e9f96b9
Editors: give -1 to a ship that leaves its last group
The-E Nov 16, 2026
44ebebf
Tests: add tests for the editor rules and the Lua goal cleanup
The-E Nov 16, 2026
e4a2bae
Apply the smaller review notes
The-E Nov 16, 2026
f6ac060
Editors: report a failed rename at the end of a mission save
The-E Nov 17, 2026
881ca58
QtFRED: stop the autosave timer after Revert and Discard
The-E Nov 17, 2026
92e33fd
Editors: apply the arrival distance notes from the third review
The-E Nov 17, 2026
32d51e3
Tests: add a rounding case and add the Lua AI handler one time
The-E Nov 17, 2026
f81ec0e
QtFRED: refresh the ship editor distance field after a change
The-E Nov 18, 2026
179b098
Editors: replace the mission file in one step on save
The-E Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions code/ai/aicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,8 @@ LOCAL int ai_inited = 0;
void ai_init()
{
if ( !ai_inited ) {
ai_lua_init();

// Do the first time initialization stuff here
Ai_classes.clear();
Ai_class_names.clear();
Expand Down
22 changes: 22 additions & 0 deletions code/ai/ailua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
static std::unordered_map<int, ai_mode_lua> Lua_ai_modes;
static std::unordered_map<int, player_order_lua> Lua_player_orders;

// The goals of Lua AI modes keep Lua values. Free them while the Lua state still exists. Otherwise their
// destructors run later (for example at program exit) and release references in a closed Lua state.
static void ai_lua_free_goal_values(lua_State*)
{
// Only the arguments are Lua values. The target is plain data.
for (int i = 0; i < MAX_AI_INFO; ++i) {
Ai_info[i].lua_ai_target.arguments.clear();
for (auto& goal : Ai_info[i].goals)
goal.lua_ai_target.arguments.clear();
}

for (auto& wingp : Wings) {
for (auto& goal : wingp.ai_goals)
goal.lua_ai_target.arguments.clear();
}
}

void ai_lua_init()
{
Script_system.OnStateDestroy.add(ai_lua_free_goal_values);
}

void ai_lua_add_mode(int sexp_op, const ai_mode_lua& mode) {
Lua_ai_modes.emplace(sexp_op, mode);
}
Expand Down
3 changes: 3 additions & 0 deletions code/ai/ailua.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ struct player_order_lua {
};


// One-time setup of the Lua AI code. ai_init() calls it.
void ai_lua_init();

void ai_lua_add_mode(int sexp_op, const ai_mode_lua& mode);
bool ai_lua_add_order(int sexp_op, player_order_lua order);
bool ai_lua_has_mode(int sexp_op);
Expand Down
16 changes: 15 additions & 1 deletion code/cfile/cfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ void cf_attrib(const char *filename, int set, int clear, int dir_type)
}
#endif

int cf_rename(const char *old_name, const char *name, int dir_type)
int cf_rename(const char *old_name, const char *name, int dir_type, bool replace_existing)
{
Assert( CF_TYPE_SPECIFIED(dir_type) );

Expand All @@ -555,6 +555,20 @@ int cf_rename(const char *old_name, const char *name, int dir_type)
cf_create_default_path_string(old_longname, dir_type, old_name);
cf_create_default_path_string(new_longname, dir_type, name);

#ifdef _WIN32
if (replace_existing) {
if (MoveFileExA(old_longname.c_str(), new_longname.c_str(), MOVEFILE_REPLACE_EXISTING)) {
return CF_RENAME_SUCCESS;
}
const auto error = GetLastError();
return (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) ? CF_RENAME_FAIL_EXIST
: CF_RENAME_FAIL_ACCESS;
}
#else
// POSIX rename() always replaces a file that exists.
SCP_UNUSED(replace_existing);
#endif

ret_code = rename(old_longname.c_str(), new_longname.c_str());
if(ret_code != 0){
switch(errno){
Expand Down
4 changes: 3 additions & 1 deletion code/cfile/cfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ uint cf_add_chksum_long(uint seed, ubyte *buffer, size_t size);
#define CF_RENAME_SUCCESS 0 // successfully renamed the file
#define CF_RENAME_FAIL_ACCESS 1 // new name could not be created
#define CF_RENAME_FAIL_EXIST 2 // old name does not exist
int cf_rename(const char *old_name, const char *name, int type = CF_TYPE_ANY );
// If replace_existing is true and the new name already exists, the rename replaces that file in one step. Without
// it, rename() on Windows fails when the new name exists, but POSIX rename() replaces the file.
int cf_rename(const char *old_name, const char *name, int type = CF_TYPE_ANY, bool replace_existing = false);

// Creates the directory path if it doesn't exist. Even creates all its
// parent paths.
Expand Down
30 changes: 20 additions & 10 deletions code/mission/missionparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3493,11 +3493,7 @@ int parse_object(mission *pm, int /*flag*/, p_object *p_objp)
stuff_int(&p_objp->arrival_distance);

// Goober5000
if ((p_objp->arrival_distance <= 0) && (
(p_objp->arrival_location == ArrivalLocation::NEAR_SHIP)
|| (p_objp->arrival_location == ArrivalLocation::IN_FRONT_OF_SHIP) || (p_objp->arrival_location == ArrivalLocation::IN_BACK_OF_SHIP)
|| (p_objp->arrival_location == ArrivalLocation::ABOVE_SHIP) || (p_objp->arrival_location == ArrivalLocation::BELOW_SHIP)
|| (p_objp->arrival_location == ArrivalLocation::TO_LEFT_OF_SHIP) || (p_objp->arrival_location == ArrivalLocation::TO_RIGHT_OF_SHIP) ))
if ((p_objp->arrival_distance <= 0) && arrival_location_uses_distance(p_objp->arrival_location))
{
parse_warning_or_record("Arrival distance for ship %s cannot be %d — corrected to 1.", p_objp->name, p_objp->arrival_distance);
p_objp->arrival_distance = 1;
Expand Down Expand Up @@ -4956,11 +4952,7 @@ void parse_wing(mission *pm)
stuff_int( &wingp->arrival_distance );

// Goober5000
if ((wingp->arrival_distance <= 0) && (
(wingp->arrival_location == ArrivalLocation::NEAR_SHIP)
|| (wingp->arrival_location == ArrivalLocation::IN_FRONT_OF_SHIP) || (wingp->arrival_location == ArrivalLocation::IN_BACK_OF_SHIP)
|| (wingp->arrival_location == ArrivalLocation::ABOVE_SHIP) || (wingp->arrival_location == ArrivalLocation::BELOW_SHIP)
|| (wingp->arrival_location == ArrivalLocation::TO_LEFT_OF_SHIP) || (wingp->arrival_location == ArrivalLocation::TO_RIGHT_OF_SHIP) ))
if ((wingp->arrival_distance <= 0) && arrival_location_uses_distance(wingp->arrival_location))
{
parse_warning_or_record("Arrival distance for wing %s cannot be %d — corrected to 1.", wingp->name, wingp->arrival_distance);
wingp->arrival_distance = 1;
Expand Down Expand Up @@ -8980,6 +8972,24 @@ int add_path_restriction()
return index;
}

bool arrival_location_uses_distance(ArrivalLocation location)
{
switch (location) {
case ArrivalLocation::NEAR_SHIP:
case ArrivalLocation::IN_FRONT_OF_SHIP:
case ArrivalLocation::IN_BACK_OF_SHIP:
case ArrivalLocation::ABOVE_SHIP:
case ArrivalLocation::BELOW_SHIP:
case ArrivalLocation::TO_LEFT_OF_SHIP:
case ArrivalLocation::TO_RIGHT_OF_SHIP:
return true;
case ArrivalLocation::AT_LOCATION:
case ArrivalLocation::FROM_DOCK_BAY:
return false;
}
return false;
}

/**
* Look for \<any friendly\>, \<any hostile player\>, etc.
*/
Expand Down
5 changes: 4 additions & 1 deletion code/mission/missionparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum class DepartureLocation;
#define DEFAULT_AMBIENT_LIGHT_LEVEL 0x00787878

#define MIN_TARGET_ARRIVAL_DISTANCE 500.0f // float because that's how FRED does the math
#define MIN_TARGET_ARRIVAL_MULTIPLIER 2.0f // minimum distance is 2 * target radius, but at least 500
#define MIN_TARGET_ARRIVAL_MULTIPLIER 2.0f // minimum distance is 2 * target radius, but not more than MIN_TARGET_ARRIVAL_DISTANCE

anchor_t get_special_anchor(const char *name);
void check_anchor_for_hangar_bay(SCP_string &message, SCP_set<anchor_t> &anchors_checked, anchor_t anchor, const char *other_name, bool other_is_ship, bool is_arrival);
Expand Down Expand Up @@ -290,6 +290,9 @@ enum class ArrivalLocation : int
FROM_DOCK_BAY = 8
};

// True for each arrival location near a target ship. Such a location needs an arrival distance greater than 0.
bool arrival_location_uses_distance(ArrivalLocation location);

// enums for departure locations. The integer values of these enums should match their counterparts in the departure location array
#define MAX_DEPARTURE_NAMES 2
enum class DepartureLocation : int
Expand Down
64 changes: 63 additions & 1 deletion code/missioneditor/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
#include "parse/sexp.h"
#include "prop/prop.h"
#include "render/batching.h"
#include "scripting/scripting.h"
#include "sound/audiostr.h"
#include "sound/sound.h"
#include "graphics/2d.h"
#include "osapi/osapi.h"
#include "ship/ship.h"
#include "weapon/weapon.h"

#include <algorithm>
#include <cmath>

// to keep track of data
char Voice_abbrev_briefing[NAME_LENGTH];
Expand Down Expand Up @@ -110,6 +116,42 @@ anchor_t target_to_anchor(int target)
return anchor_t(target);
}

int arrival_min_distance(ArrivalLocation location, anchor_t anchor)
{
if (!arrival_location_uses_distance(location))
return 0;

if (!anchor.isValid() || (anchor.value() & ANCHOR_SPECIAL_ARRIVAL))
return 1;

const int shipnum = anchor_to_target(anchor);
if (shipnum < 0 || shipnum >= MAX_SHIPS || Ships[shipnum].objnum < 0)
return 1;

// The minimum is 2 * the target radius, but not more than MIN_TARGET_ARRIVAL_DISTANCE. Thus that constant
// is the largest minimum, not a lower limit. A large target does not need an arrival distance above it.
const float radius_distance = std::round(MIN_TARGET_ARRIVAL_MULTIPLIER * Objects[Ships[shipnum].objnum].radius);
return std::max(1, static_cast<int>(std::min(MIN_TARGET_ARRIVAL_DISTANCE, radius_distance)));
Comment thread
The-E marked this conversation as resolved.
Resolved
}

bool ship_in_editor_group(const ship& shipp, int group_bit)
{
// -1 has all bits set, so test it first.
return shipp.group >= 0 && (shipp.group & group_bit) != 0;
}

void ship_set_editor_group(ship& shipp, int group_bit, bool in_group)
{
if (in_group) {
shipp.group = (shipp.group < 0 ? 0 : shipp.group) | group_bit;
} else if (shipp.group >= 0) {
shipp.group &= ~group_bit;
// A ship in no group has -1, so that the mission save writes no group data for it.
if (shipp.group == 0)
shipp.group = -1;
}
}

void update_custom_wing_indexes()
{
int i;
Expand Down Expand Up @@ -355,7 +397,9 @@ SCP_string check_name_conflict(const char *entity_type, const char *name, int ex
return ""; // no error
}

void editor_free_engine_data()
// Free the engine data that the editors keep for their whole session. Call it while the graphics system is
// still up, because models own GPU buffers and cursors own bitmaps.
static void editor_free_engine_data()
{
// models, model instances, and collision trees; done before ship_close() because model_unload() walks Ship_info
model_free_all();
Expand All @@ -373,6 +417,11 @@ void editor_free_engine_data()
fhash_flush();
control_config_common_close();
sexp_shutdown();

// Use the same order as game_shutdown(). The SEXP data (Lua SEXPs) holds Lua references, so free it first.
// After this call, scripting is not available.
Script_system.Clear();

batching_shutdown();

// the cursors hold bitmaps, so this must happen before the graphics system shuts down
Expand All @@ -385,6 +434,19 @@ void editor_free_engine_data()
}
}

void editor_shutdown()
{
// Lua objects can hold sound handles, so free the Lua state before the sound system.
editor_free_engine_data();

audiostream_close();
snd_close();

gr_close();

os_cleanup();
}

int load_and_find_campaign_mission(const char *mission_filename)
{
if (!mission_filename || !mission_filename[0])
Expand Down
21 changes: 18 additions & 3 deletions code/missioneditor/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "mission/missionmessage.h"
#include "ship/anchor_t.h"

enum class ArrivalLocation;

// Default AWACS range applied when the nebula intensity is unset or invalid
constexpr float DEFAULT_NEBULA_RANGE = 3000.0f;

Expand Down Expand Up @@ -45,6 +47,17 @@ int anchor_to_target(anchor_t anchor);

anchor_t target_to_anchor(int target);

// The smallest arrival distance that the editors accept for an arrival location and anchor.
// It is 0 for a location that uses no distance. Near a ship it is min(500, 2 * radius of the target ship).
// For a special anchor (for example "<any friendly>") it is 1, because the distance must be greater than 0.
int arrival_min_distance(ArrivalLocation location, anchor_t anchor);

// ship::group holds the editor selection groups, one bit for each group. The value -1 means "in no group":
// a mission without +Group gives -1, and the mission save writes +Group only for a value of 0 or more.
class ship;
bool ship_in_editor_group(const ship& shipp, int group_bit);
void ship_set_editor_group(ship& shipp, int group_bit, bool in_group);

// Rebuild Starting_wings[], Squadron_wings[], TVT_wings[] from their parallel
// name arrays via wing_name_lookup.
void update_custom_wing_indexes();
Expand Down Expand Up @@ -73,9 +86,11 @@ bool set_single_player_start(int objnum);
// against the entity currently being renamed.
SCP_string check_name_conflict(const char *entity_type, const char *name, int exclude_ship = -1, int exclude_wing = -1, int exclude_waypoint_list = -1, int exclude_jump_node = -1);

// Free the engine data that the editors keep for their whole session. Call at shutdown, while the graphics
// system is still up, since models own GPU buffers and cursors own bitmaps.
void editor_free_engine_data();

// The shutdown of the engine systems, for FRED2 and QtFRED. The order is the same as in game_shutdown():
// first the engine data, the SEXP data, and the Lua state, then sound, then graphics.
// Call it after you close the editor windows, because they can use mission and SEXP data.
void editor_shutdown();

// Bounding-box midpoint of all (or only marked) objects and the farthest distance from it; false if there are none.
bool get_object_bounds(bool marked_only, vec3d* center, float* radius);
Expand Down
17 changes: 14 additions & 3 deletions code/missioneditor/missionsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2454,10 +2454,21 @@ int Fred_mission_save::save_mission_file(const char* pathname)
cf_attrib(pathname, 0, FILE_ATTRIBUTE_READONLY, CF_TYPE_MISSIONS);
#endif
cf_delete(backup_name, CF_TYPE_MISSIONS);
// This rename fails if the mission file does not exist yet. That is correct for a new file.
cf_rename(pathname, backup_name, CF_TYPE_MISSIONS);
cf_rename(savepath, pathname, CF_TYPE_MISSIONS);
} else {
cf_rename(savepath, pathname, CF_TYPE_MISSIONS);
}
#ifdef _WIN32
else {
// A replace fails on a read-only file.
cf_attrib(pathname, 0, FILE_ATTRIBUTE_READONLY, CF_TYPE_MISSIONS);
}
#endif

// Replace the old mission file in one step. If the rename fails, the old file stays and the new content is
// only in the temporary file. Report an error.
if (cf_rename(savepath, pathname, CF_TYPE_MISSIONS, true) != CF_RENAME_SUCCESS) {
mprintf(("Could not rename '%s' to '%s'. The mission is not saved.\n", savepath, pathname));
err = -19;
}
}

Expand Down
10 changes: 4 additions & 6 deletions fred2/fredview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4494,7 +4494,7 @@ void CFREDView::OnGroup(UINT nID)
objp = GET_FIRST(&obj_used_list);
while (objp != END_OF_LIST(&obj_used_list)) {
if (objp->type == OBJ_SHIP) {
if (Ships[objp->instance].group & n)
if (ship_in_editor_group(Ships[objp->instance], n))
mark_object(OBJ_INDEX(objp));
}

Expand All @@ -4510,13 +4510,13 @@ void CFREDView::OnSetGroup(UINT nID)
object *objp;

for (i=0; i<MAX_SHIPS; i++)
Ships[i].group &= ~n;
ship_set_editor_group(Ships[i], n, false);

objp = GET_FIRST(&obj_used_list);
while (objp != END_OF_LIST(&obj_used_list)) {
if (objp->flags[Object::Object_Flags::Marked]) {
if (objp->type == OBJ_SHIP) {
Ships[objp->instance].group |= n;
ship_set_editor_group(Ships[objp->instance], n, true);

} else
err = 1;
Expand Down Expand Up @@ -5082,9 +5082,7 @@ void CFREDView::OnEditorsFiction()

void CFREDView::OnDestroy()
{
audiostream_close();
snd_close();

// fred_shutdown() closes the sound system after the Lua state, the same as QtFRED and the game.
CView::OnDestroy();
}

Expand Down
6 changes: 1 addition & 5 deletions fred2/management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,7 @@ bool fred_init(std::unique_ptr<os::GraphicsOperations>&& graphicsOps)
// called when FRED closes, after the document has been closed
void fred_shutdown()
{
editor_free_engine_data();

gr_close();

os_cleanup();
editor_shutdown();
}

void set_physics_controls()
Expand Down
Loading
Loading