Skip to content

Commit 0a10c4a

Browse files
committed
qtfred support
1 parent 02f3060 commit 0a10c4a

5 files changed

Lines changed: 58 additions & 104 deletions

File tree

‎qtfred/src/mission/Editor.cpp‎

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "starfield/starfield.h" // stars_init, stars_pre_level_init, stars_post_level_init
3434
#include "hud/hudsquadmsg.h"
3535
#include "globalincs/linklist.h"
36+
#include "globalincs/utility.h"
3637

3738
#include "ui/QtGraphicsOperations.h"
3839

@@ -251,7 +252,7 @@ void Editor::maybeUseAutosave(std::string& filepath)
251252

252253
bool Editor::loadMission(const std::string& mission_name, int flags) {
253254
char name[512], * old_name;
254-
int i, j, k, ob;
255+
int i, j, ob;
255256
object* objp;
256257

257258
// activate the localizer hash table
@@ -387,11 +388,10 @@ bool Editor::loadMission(const std::string& mission_name, int flags) {
387388
update_sexp_references(old_name, name);
388389
ai_update_goal_references(sexp_ref_type::SHIP, old_name, name);
389390
update_texture_replacements(old_name, name);
390-
for (k = 0; k < Num_reinforcements; k++) {
391-
if (!strcmp(old_name, Reinforcements[k].name)) {
392-
Assert(strlen(name) < NAME_LENGTH);
393-
strcpy_s(Reinforcements[k].name, name);
394-
}
391+
int k = find_item_with_string(Reinforcements, &reinforcements::name, old_name);
392+
if (k >= 0) {
393+
Assert(strlen(name) < NAME_LENGTH);
394+
strcpy_s(Reinforcements[k].name, name);
395395
}
396396

397397
strcpy_s(Ships[Wings[i].ship_index[j]].ship_name, name);
@@ -956,7 +956,7 @@ int Editor::getNumMarked() {
956956
}
957957
int Editor::dup_object(object* objp) {
958958

959-
int i, j, n, inst, obj = -1;
959+
int i, n, inst, obj = -1;
960960
ai_info* aip1, * aip2;
961961
object* objp1, * objp2;
962962
ship_subsys* subp1, * subp2;
@@ -1018,17 +1018,10 @@ int Editor::dup_object(object* objp) {
10181018
subp2 = GET_NEXT(subp2);
10191019
}
10201020

1021-
for (i = 0; i < Num_reinforcements; i++) {
1022-
if (!stricmp(Reinforcements[i].name, Ships[inst].ship_name)) {
1023-
if (Num_reinforcements < MAX_REINFORCEMENTS) {
1024-
j = Num_reinforcements++;
1025-
strcpy_s(Reinforcements[j].name, Ships[n].ship_name);
1026-
Reinforcements[j].type = Reinforcements[i].type;
1027-
Reinforcements[j].uses = Reinforcements[i].uses;
1028-
}
1029-
1030-
break;
1031-
}
1021+
i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[inst].ship_name);
1022+
if (i >= 0) {
1023+
Reinforcements.push_back(Reinforcements[i]);
1024+
strcpy_s(Reinforcements.back().name, Ships[n].ship_name);
10321025
}
10331026

10341027
} else if (objp->type == OBJ_WAYPOINT) {
@@ -1184,12 +1177,7 @@ int Editor::common_object_delete(int obj) {
11841177
invalidate_references(name, sexp_ref_type::SHIP);
11851178
}
11861179

1187-
for (i = 0; i < Num_reinforcements; i++) {
1188-
if (!stricmp(name, Reinforcements[i].name)) {
1189-
delete_reinforcement(i);
1190-
break;
1191-
}
1192-
}
1180+
delete_reinforcement(name);
11931181

11941182
// check if any ship is docked with this ship and break dock if so
11951183
while (object_is_docked(&Objects[obj])) {
@@ -1396,13 +1384,8 @@ int Editor::reference_handler(const char* name, sexp_ref_type type, int obj) {
13961384
return 0;
13971385
}
13981386

1399-
for (n = 0; n < Num_reinforcements; n++) {
1400-
if (!stricmp(name, Reinforcements[n].name)) {
1401-
break;
1402-
}
1403-
}
1404-
1405-
if (n < Num_reinforcements) {
1387+
n = find_item_with_string(Reinforcements, &reinforcements::name, name);
1388+
if (n >= 0) {
14061389
sprintf(msg, "Ship \"%s\" is a reinforcement unit.\n"
14071390
"Do you want to delete it anyway?", name);
14081391

@@ -1639,11 +1622,9 @@ int Editor::invalidate_references(const char* name, sexp_ref_type type) {
16391622
update_sexp_references(name, new_name);
16401623
ai_update_goal_references(type, name, new_name);
16411624
update_texture_replacements(name, new_name);
1642-
for (i = 0; i < Num_reinforcements; i++) {
1643-
if (!stricmp(name, Reinforcements[i].name)) {
1644-
strcpy_s(Reinforcements[i].name, new_name);
1645-
}
1646-
}
1625+
i = find_item_with_string(Reinforcements, &reinforcements::name, name);
1626+
if (i >= 0)
1627+
strcpy_s(Reinforcements[i].name, new_name);
16471628

16481629
return 0;
16491630
}
@@ -1670,8 +1651,6 @@ void Editor::update_texture_replacements(const char* old_name, const char* new_n
16701651
}
16711652
}
16721653
int Editor::rename_ship(int ship, const char* name) {
1673-
int i;
1674-
16751654
Assert(ship >= 0);
16761655
Assert(strlen(name) < NAME_LENGTH);
16771656

@@ -1682,44 +1661,34 @@ int Editor::rename_ship(int ship, const char* name) {
16821661
update_sexp_references(Ships[ship].ship_name, name);
16831662
ai_update_goal_references(sexp_ref_type::SHIP, Ships[ship].ship_name, name);
16841663
update_texture_replacements(Ships[ship].ship_name, name);
1685-
for (i = 0; i < Num_reinforcements; i++) {
1686-
if (!stricmp(Ships[ship].ship_name, Reinforcements[i].name)) {
1687-
strcpy_s(Reinforcements[i].name, name);
1688-
}
1689-
}
1664+
int i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[ship].ship_name);
1665+
if (i >= 0)
1666+
strcpy_s(Reinforcements[i].name, name);
16901667

16911668
strcpy_s(Ships[ship].ship_name, name);
16921669

16931670
missionChanged();
16941671

16951672
return 0;
16961673
}
1697-
void Editor::delete_reinforcement(int num) {
1698-
int i;
1699-
1700-
for (i = num; i < Num_reinforcements - 1; i++) {
1701-
Reinforcements[i] = Reinforcements[i + 1];
1702-
}
1674+
void Editor::delete_reinforcement(const char* name) {
1675+
int i = find_item_with_string(Reinforcements, &reinforcements::name, name);
1676+
if (i < 0)
1677+
return;
17031678

1704-
Num_reinforcements--;
1679+
Reinforcements.erase(Reinforcements.begin() + i);
17051680
missionChanged();
17061681
}
17071682
int Editor::check_wing_dependencies(int wing_num) {
17081683
const char* name = Wings[wing_num].name;
17091684
return reference_handler(name, sexp_ref_type::WING, -1);
17101685
}
17111686
int Editor::set_reinforcement(const char* name, int state) {
1712-
int i, index, cur = -1;
1713-
1714-
for (i = 0; i < Num_reinforcements; i++) {
1715-
if (!stricmp(Reinforcements[i].name, name)) {
1716-
cur = i;
1717-
}
1718-
}
1687+
int index;
1688+
int cur = find_item_with_string(Reinforcements, &reinforcements::name, name);
17191689

17201690
if (!state && (cur != -1)) {
1721-
Num_reinforcements--;
1722-
Reinforcements[cur] = Reinforcements[Num_reinforcements];
1691+
Reinforcements.erase(Reinforcements.begin() + cur);
17231692

17241693
// clear the ship/wing flag for this reinforcement
17251694
index = ship_name_lookup(name);
@@ -1739,14 +1708,15 @@ int Editor::set_reinforcement(const char* name, int state) {
17391708
return -1;
17401709
}
17411710

1742-
if (state && (cur == -1) && (Num_reinforcements < MAX_REINFORCEMENTS)) {
1711+
if (state && (cur == -1)) {
17431712
Assert(strlen(name) < NAME_LENGTH);
1744-
strcpy_s(Reinforcements[Num_reinforcements].name, name);
1745-
Reinforcements[Num_reinforcements].uses = 1;
1746-
Reinforcements[Num_reinforcements].arrival_delay = 0;
1747-
memset(Reinforcements[Num_reinforcements].no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1748-
memset(Reinforcements[Num_reinforcements].yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1749-
Num_reinforcements++;
1713+
reinforcements reinforcement;
1714+
strcpy_s(reinforcement.name, name);
1715+
reinforcement.uses = 1;
1716+
reinforcement.arrival_delay = 0;
1717+
memset(reinforcement.no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1718+
memset(reinforcement.yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1719+
Reinforcements.push_back(std::move(reinforcement));
17501720

17511721
// set the reinforcement flag on the ship or wing
17521722
index = ship_name_lookup(name);
@@ -2652,21 +2622,17 @@ int Editor::global_error_check_impl() {
26522622
}
26532623
}
26542624

2655-
if (Num_reinforcements > MAX_REINFORCEMENTS) {
2656-
return internal_error("Number of reinforcements exceeds max limit");
2657-
}
2658-
2659-
for (i = 0; i < Num_reinforcements; i++) {
2625+
for (const auto &reinforcement : Reinforcements) {
26602626
z = 0;
26612627
for (ship = 0; ship < MAX_SHIPS; ship++) {
2662-
if ((Ships[ship].objnum >= 0) && !stricmp(Ships[ship].ship_name, Reinforcements[i].name)) {
2628+
if ((Ships[ship].objnum >= 0) && !stricmp(Ships[ship].ship_name, reinforcement.name)) {
26632629
z = 1;
26642630
break;
26652631
}
26662632
}
26672633

26682634
for (wing = 0; wing < MAX_WINGS; wing++) {
2669-
if (Wings[wing].wave_count && !stricmp(Wings[wing].name, Reinforcements[i].name)) {
2635+
if (Wings[wing].wave_count && !stricmp(Wings[wing].name, reinforcement.name)) {
26702636
z = 1;
26712637
break;
26722638
}

‎qtfred/src/mission/Editor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class Editor : public QObject {
330330

331331
int invalidate_references(const char* name, sexp_ref_type type);
332332

333-
void delete_reinforcement(int num);
333+
void delete_reinforcement(const char* name);
334334

335335
// changes the currently selected wing. It is assumed that cur_wing == cur_ship's wing
336336
// number. Don't call this if this won't be true, or else you'll screw things up.

‎qtfred/src/mission/EditorWing.cpp‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "mission/dialogs/FormWingDialogModel.h"
55

66
#include <globalincs/linklist.h>
7+
#include <globalincs/utility.h>
78
#include <ship/ship.h>
89

910
namespace {
@@ -29,12 +30,7 @@ int Editor::delete_wing(int wing_num, int bypass)
2930
}
3031

3132
already_deleting_wing = 1;
32-
for (i = 0; i < Num_reinforcements; i++) {
33-
if (!stricmp(Wings[wing_num].name, Reinforcements[i].name)) {
34-
delete_reinforcement(i);
35-
break;
36-
}
37-
}
33+
delete_reinforcement(Wings[wing_num].name);
3834

3935
invalidate_references(Wings[wing_num].name, sexp_ref_type::WING);
4036
if (!bypass) {

‎qtfred/src/mission/dialogs/ReinforcementsEditorDialogModel.cpp‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ ReinforcementsDialogModel::ReinforcementsDialogModel(QObject* parent, EditorView
1212

1313
void ReinforcementsDialogModel::initializeData()
1414
{
15-
for (int i = 0; i < Num_reinforcements; i++) {
16-
_reinforcementList.emplace_back(Reinforcements[i].name, Reinforcements[i].uses, Reinforcements[i].arrival_delay);
15+
for (const auto &r : Reinforcements) {
16+
_reinforcementList.emplace_back(r.name, r.uses, r.arrival_delay);
1717
}
1818

1919
// add the wings to the model's internal storage
@@ -81,20 +81,17 @@ void ReinforcementsDialogModel::initializeData()
8181

8282
bool ReinforcementsDialogModel::apply()
8383
{
84-
Num_reinforcements = static_cast<int>(_reinforcementList.size());
85-
86-
int i = 0;
87-
8884
// Properly set all reinforcement info.
85+
Reinforcements.clear();
8986
for (auto& modelReinforcement : _reinforcementList) {
90-
strcpy_s(Reinforcements[i].name, std::get<0>(modelReinforcement).c_str());
91-
Reinforcements[i].uses = std::get<1>(modelReinforcement);
92-
Reinforcements[i].arrival_delay = std::get<2>(modelReinforcement);
93-
Reinforcements[i].type = 0;
94-
memset( Reinforcements[i].no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
95-
memset( Reinforcements[i].yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
96-
97-
i++;
87+
reinforcements reinforcement;
88+
strcpy_s(reinforcement.name, std::get<0>(modelReinforcement).c_str());
89+
reinforcement.uses = std::get<1>(modelReinforcement);
90+
reinforcement.arrival_delay = std::get<2>(modelReinforcement);
91+
reinforcement.type = 0;
92+
memset( reinforcement.no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
93+
memset( reinforcement.yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH );
94+
Reinforcements.push_back(std::move(reinforcement));
9895
}
9996

10097
_shipWingPool.clear();
@@ -124,11 +121,6 @@ void ReinforcementsDialogModel::addToReinforcements(const SCP_vector<SCP_string>
124121
}
125122
}
126123

127-
while (_reinforcementList.size() > MAX_REINFORCEMENTS) {
128-
_shipWingPool.push_back(std::get<0>(_reinforcementList.back()));
129-
_reinforcementList.pop_back();
130-
}
131-
132124
set_modified();
133125
}
134126

‎qtfred/src/mission/dialogs/ShipEditor/ShipEditorDialogModel.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "missioneditor/common.h"
1717

1818
#include <globalincs/linklist.h>
19+
#include <globalincs/utility.h>
1920
#include <hud/hudsquadmsg.h>
2021
#include <localization/localize.h>
2122
#include <mission/object.h>
@@ -810,11 +811,10 @@ void ShipEditorDialogModel::setShipName(const SCP_string& m_ship_name)
810811
update_sexp_references(old_name, Ships[single_ship].ship_name);
811812
_editor->ai_update_goal_references(sexp_ref_type::SHIP, old_name, Ships[single_ship].ship_name);
812813
_editor->update_texture_replacements(old_name, Ships[single_ship].ship_name);
813-
for (int j = 0; j < Num_reinforcements; j++) {
814-
if (!strcmp(old_name, Reinforcements[j].name)) {
815-
Assert(strlen(Ships[single_ship].ship_name) < NAME_LENGTH);
816-
strcpy_s(Reinforcements[j].name, Ships[single_ship].ship_name);
817-
}
814+
int j = find_item_with_string(Reinforcements, &reinforcements::name, old_name);
815+
if (j >= 0) {
816+
Assert(strlen(Ships[single_ship].ship_name) < NAME_LENGTH);
817+
strcpy_s(Reinforcements[j].name, Ships[single_ship].ship_name);
818818
}
819819
}
820820

0 commit comments

Comments
 (0)