Skip to content

Commit 81695b3

Browse files
committed
qtfred support
1 parent 7545e5d commit 81695b3

6 files changed

Lines changed: 61 additions & 107 deletions

File tree

qtfred/src/mission/Editor.cpp

Lines changed: 36 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "starfield/starfield.h" // stars_init, stars_pre_level_init, stars_post_level_init
3232
#include "hud/hudsquadmsg.h"
3333
#include "globalincs/linklist.h"
34+
#include "globalincs/utility.h"
3435

3536
#include "ui/QtGraphicsOperations.h"
3637

@@ -249,7 +250,7 @@ void Editor::maybeUseAutosave(std::string& filepath)
249250

250251
bool Editor::loadMission(const std::string& mission_name, int flags) {
251252
char name[512], * old_name;
252-
int i, j, k, ob;
253+
int i, j, ob;
253254
object* objp;
254255

255256
// 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
// bash it again so that we handle display names if needed
@@ -917,7 +917,7 @@ int Editor::getNumMarked() {
917917
}
918918
int Editor::dup_object(object* objp) {
919919

920-
int i, j, n, inst, obj = -1;
920+
int i, n, inst, obj = -1;
921921
ai_info* aip1, * aip2;
922922
object* objp1, * objp2;
923923
ship_subsys* subp1, * subp2;
@@ -979,17 +979,10 @@ int Editor::dup_object(object* objp) {
979979
subp2 = GET_NEXT(subp2);
980980
}
981981

982-
for (i = 0; i < Num_reinforcements; i++) {
983-
if (!stricmp(Reinforcements[i].name, Ships[inst].ship_name)) {
984-
if (Num_reinforcements < MAX_REINFORCEMENTS) {
985-
j = Num_reinforcements++;
986-
strcpy_s(Reinforcements[j].name, Ships[n].ship_name);
987-
Reinforcements[j].type = Reinforcements[i].type;
988-
Reinforcements[j].uses = Reinforcements[i].uses;
989-
}
990-
991-
break;
992-
}
982+
i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[inst].ship_name);
983+
if (i >= 0) {
984+
Reinforcements.push_back(Reinforcements[i]);
985+
strcpy_s(Reinforcements.back().name, Ships[n].ship_name);
993986
}
994987

995988
} else if (objp->type == OBJ_WAYPOINT) {
@@ -1145,12 +1138,7 @@ int Editor::common_object_delete(int obj) {
11451138
invalidate_references(name, sexp_ref_type::SHIP);
11461139
}
11471140

1148-
for (i = 0; i < Num_reinforcements; i++) {
1149-
if (!stricmp(name, Reinforcements[i].name)) {
1150-
delete_reinforcement(i);
1151-
break;
1152-
}
1153-
}
1141+
delete_reinforcement(name);
11541142

11551143
// check if any ship is docked with this ship and break dock if so
11561144
while (object_is_docked(&Objects[obj])) {
@@ -1344,13 +1332,8 @@ int Editor::reference_handler(const char* name, sexp_ref_type type, int obj) {
13441332
return 0;
13451333
}
13461334

1347-
for (n = 0; n < Num_reinforcements; n++) {
1348-
if (!stricmp(name, Reinforcements[n].name)) {
1349-
break;
1350-
}
1351-
}
1352-
1353-
if (n < Num_reinforcements) {
1335+
n = find_item_with_string(Reinforcements, &reinforcements::name, name);
1336+
if (n >= 0) {
13541337
sprintf(msg, "Ship \"%s\" is a reinforcement unit.\n"
13551338
"Do you want to delete it anyway?", name);
13561339

@@ -1468,11 +1451,9 @@ int Editor::invalidate_references(const char* name, sexp_ref_type type) {
14681451
update_sexp_references(name, new_name);
14691452
ai_update_goal_references(type, name, new_name);
14701453
update_texture_replacements(name, new_name);
1471-
for (i = 0; i < Num_reinforcements; i++) {
1472-
if (!stricmp(name, Reinforcements[i].name)) {
1473-
strcpy_s(Reinforcements[i].name, new_name);
1474-
}
1475-
}
1454+
i = find_item_with_string(Reinforcements, &reinforcements::name, name);
1455+
if (i >= 0)
1456+
strcpy_s(Reinforcements[i].name, new_name);
14761457

14771458
return 0;
14781459
}
@@ -1499,8 +1480,6 @@ void Editor::update_texture_replacements(const char* old_name, const char* new_n
14991480
}
15001481
}
15011482
int Editor::rename_ship(int ship, const char* name) {
1502-
int i;
1503-
15041483
Assert(ship >= 0);
15051484
Assert(strlen(name) < NAME_LENGTH);
15061485

@@ -1511,11 +1490,9 @@ int Editor::rename_ship(int ship, const char* name) {
15111490
update_sexp_references(Ships[ship].ship_name, name);
15121491
ai_update_goal_references(sexp_ref_type::SHIP, Ships[ship].ship_name, name);
15131492
update_texture_replacements(Ships[ship].ship_name, name);
1514-
for (i = 0; i < Num_reinforcements; i++) {
1515-
if (!stricmp(Ships[ship].ship_name, Reinforcements[i].name)) {
1516-
strcpy_s(Reinforcements[i].name, name);
1517-
}
1518-
}
1493+
int i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[ship].ship_name);
1494+
if (i >= 0)
1495+
strcpy_s(Reinforcements[i].name, name);
15191496

15201497
strcpy_s(Ships[ship].ship_name, name);
15211498

@@ -1537,32 +1514,24 @@ int Editor::rename_ship(int ship, const char* name) {
15371514

15381515
return 0;
15391516
}
1540-
void Editor::delete_reinforcement(int num) {
1541-
int i;
1542-
1543-
for (i = num; i < Num_reinforcements - 1; i++) {
1544-
Reinforcements[i] = Reinforcements[i + 1];
1545-
}
1517+
void Editor::delete_reinforcement(const char* name) {
1518+
int i = find_item_with_string(Reinforcements, &reinforcements::name, name);
1519+
if (i < 0)
1520+
return;
15461521

1547-
Num_reinforcements--;
1522+
Reinforcements.erase(Reinforcements.begin() + i);
15481523
missionChanged();
15491524
}
15501525
int Editor::check_wing_dependencies(int wing_num) {
15511526
const char* name = Wings[wing_num].name;
15521527
return reference_handler(name, sexp_ref_type::WING, -1);
15531528
}
15541529
int Editor::set_reinforcement(const char* name, int state) {
1555-
int i, index, cur = -1;
1556-
1557-
for (i = 0; i < Num_reinforcements; i++) {
1558-
if (!stricmp(Reinforcements[i].name, name)) {
1559-
cur = i;
1560-
}
1561-
}
1530+
int index;
1531+
int cur = find_item_with_string(Reinforcements, &reinforcements::name, name);
15621532

15631533
if (!state && (cur != -1)) {
1564-
Num_reinforcements--;
1565-
Reinforcements[cur] = Reinforcements[Num_reinforcements];
1534+
Reinforcements.erase(Reinforcements.begin() + cur);
15661535

15671536
// clear the ship/wing flag for this reinforcement
15681537
index = ship_name_lookup(name);
@@ -1582,14 +1551,15 @@ int Editor::set_reinforcement(const char* name, int state) {
15821551
return -1;
15831552
}
15841553

1585-
if (state && (cur == -1) && (Num_reinforcements < MAX_REINFORCEMENTS)) {
1554+
if (state && (cur == -1)) {
15861555
Assert(strlen(name) < NAME_LENGTH);
1587-
strcpy_s(Reinforcements[Num_reinforcements].name, name);
1588-
Reinforcements[Num_reinforcements].uses = 1;
1589-
Reinforcements[Num_reinforcements].arrival_delay = 0;
1590-
memset(Reinforcements[Num_reinforcements].no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1591-
memset(Reinforcements[Num_reinforcements].yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1592-
Num_reinforcements++;
1556+
reinforcements reinforcement;
1557+
strcpy_s(reinforcement.name, name);
1558+
reinforcement.uses = 1;
1559+
reinforcement.arrival_delay = 0;
1560+
memset(reinforcement.no_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1561+
memset(reinforcement.yes_messages, 0, MAX_REINFORCEMENT_MESSAGES * NAME_LENGTH);
1562+
Reinforcements.push_back(std::move(reinforcement));
15931563

15941564
// set the reinforcement flag on the ship or wing
15951565
index = ship_name_lookup(name);

qtfred/src/mission/Editor.h

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

315315
int invalidate_references(const char* name, sexp_ref_type type);
316316

317-
void delete_reinforcement(int num);
317+
void delete_reinforcement(const char* name);
318318

319319
// changes the currently selected wing. It is assumed that cur_wing == cur_ship's wing
320320
// 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[_singleShip].ship_name);
811812
_editor->ai_update_goal_references(sexp_ref_type::SHIP, old_name, Ships[_singleShip].ship_name);
812813
_editor->update_texture_replacements(old_name, Ships[_singleShip].ship_name);
813-
for (int j = 0; j < Num_reinforcements; j++) {
814-
if (!strcmp(old_name, Reinforcements[j].name)) {
815-
Assert(strlen(Ships[_singleShip].ship_name) < NAME_LENGTH);
816-
strcpy_s(Reinforcements[j].name, Ships[_singleShip].ship_name);
817-
}
814+
int j = find_item_with_string(Reinforcements, &reinforcements::name, old_name);
815+
if (j >= 0) {
816+
Assert(strlen(Ships[_singleShip].ship_name) < NAME_LENGTH);
817+
strcpy_s(Reinforcements[j].name, Ships[_singleShip].ship_name);
818818
}
819819
}
820820

qtfred/src/ui/util/ErrorChecker.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -784,27 +784,23 @@ int ErrorChecker::checkPlayerStarts() {
784784

785785
int ErrorChecker::checkReinforcements() {
786786

787-
if (Num_reinforcements > MAX_REINFORCEMENTS) {
788-
return internal_error("Number of reinforcements exceeds max limit");
789-
}
790-
791-
for (int i = 0; i < Num_reinforcements; i++) {
792-
if (Reinforcements[i].arrival_delay < 0) {
793-
error("Reinforcement \"%s\" has a negative arrival delay", Reinforcements[i].name);
787+
for (const auto& reinforcement : Reinforcements) {
788+
if (reinforcement.arrival_delay < 0) {
789+
error("Reinforcement \"%s\" has a negative arrival delay", reinforcement.name);
794790
}
795791

796792
int z = 0;
797793
int ship_wingnum = -1;
798794
for (const auto& ship : Ships) {
799-
if ((ship.objnum >= 0) && !stricmp(ship.ship_name, Reinforcements[i].name)) {
795+
if ((ship.objnum >= 0) && !stricmp(ship.ship_name, reinforcement.name)) {
800796
z = 1;
801797
ship_wingnum = ship.wingnum;
802798
break;
803799
}
804800
}
805801

806802
for (const auto& wing : Wings) {
807-
if (wing.wave_count && !stricmp(wing.name, Reinforcements[i].name)) {
803+
if (wing.wave_count && !stricmp(wing.name, reinforcement.name)) {
808804
z = 1;
809805
break;
810806
}
@@ -816,7 +812,7 @@ int ErrorChecker::checkReinforcements() {
816812

817813
if (ship_wingnum >= 0) {
818814
potential("Reinforcement \"%s\" is a ship that belongs to a wing; the reinforcement flag will be ignored",
819-
Reinforcements[i].name);
815+
reinforcement.name);
820816
}
821817
}
822818

0 commit comments

Comments
 (0)