forked from scp-fs2open/fs2open.github.com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditor.cpp
More file actions
2022 lines (1674 loc) · 56.6 KB
/
Copy pathEditor.cpp
File metadata and controls
2022 lines (1674 loc) · 56.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Editor.h"
#include <array>
#include <vector>
#include <stdexcept>
#include <clocale>
#include <SDL.h>
#include <ai/ai.h>
#include <parse/parselo.h>
#include <mission/missiongoals.h>
#include <mission/missionparse.h>
#include <asteroid/asteroid.h>
#include <jumpnode/jumpnode.h>
#include <prop/prop.h>
#include <util.h>
#include <mission/missionmessage.h>
#include <missioneditor/common.h>
#include <missioneditor/missionsave.h>
#include <gamesnd/eventmusic.h>
#include <starfield/nebula.h>
#include <object/objectdock.h>
#include <localization/fhash.h>
#include <scripting/global_hooks.h>
#include "iff_defs/iff_defs.h" // iff_init
#include "object/object.h" // obj_init
#include "species_defs/species_defs.h" // species_init
#include "weapon/weapon.h" // weapon_init
#include "nebula/neb.h" // neb2_init
#include "starfield/starfield.h" // stars_init, stars_pre_level_init, stars_post_level_init
#include "hud/hudsquadmsg.h"
#include "globalincs/linklist.h"
#include "globalincs/utility.h"
#include "ui/QtGraphicsOperations.h"
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
#include "object.h"
#include "management.h"
#include "util.h"
#include "FredApplication.h"
namespace {
std::pair<int, sexp_src> query_referenced_in_ai_goals(sexp_ref_type type, const char* name) {
int i;
for (i = 0; i < MAX_AI_INFO; i++) { // loop through all Ai_info entries
if (Ai_info[i].shipnum >= 0) { // skip if unused
if (query_referenced_in_ai_goals(Ai_info[i].goals, type, name)) {
return std::make_pair(Ai_info[i].shipnum, sexp_src::SHIP_ORDER);
}
}
}
for (i = 0; i < MAX_WINGS; i++) {
if (Wings[i].wave_count) {
if (query_referenced_in_ai_goals(Wings[i].ai_goals, type, name)) {
return std::make_pair(i, sexp_src::WING_ORDER);
}
}
}
return std::make_pair(-1, sexp_src::NONE);
}
// Used in the FRED drop-down menu and in ErrorChecker::checkInitialOrders
// NOTE: Certain goals (Form On Wing, Rearm, Chase Weapon, Fly To Ship) aren't listed here. This may or may not be intentional,
// but if they are added in the future, it will be necessary to verify correct functionality in the various FRED dialog functions.
ai_goal_list Ai_goal_list[] = {
{ "Waypoints", AI_GOAL_WAYPOINTS, 0 },
{ "Waypoints once", AI_GOAL_WAYPOINTS_ONCE, 0 },
{ "Attack", AI_GOAL_CHASE, 0 },
{ "Attack", AI_GOAL_CHASE_WING, 0 }, // duplicate needed because we can no longer use bitwise operators
{ "Attack any ship", AI_GOAL_CHASE_ANY, 0 },
{ "Attack ship class", AI_GOAL_CHASE_SHIP_CLASS, 0 },
{ "Guard", AI_GOAL_GUARD, 0 },
{ "Guard", AI_GOAL_GUARD_WING, 0 }, // duplicate needed because we can no longer use bitwise operators
{ "Disable ship", AI_GOAL_DISABLE_SHIP, 0 },
{ "Disable ship (tactical)",AI_GOAL_DISABLE_SHIP_TACTICAL, 0 },
{ "Disarm ship", AI_GOAL_DISARM_SHIP, 0 },
{ "Disarm ship (tactical)", AI_GOAL_DISARM_SHIP_TACTICAL, 0 },
{ "Destroy subsystem", AI_GOAL_DESTROY_SUBSYSTEM, 0 },
{ "Dock", AI_GOAL_DOCK, 0 },
{ "Undock", AI_GOAL_UNDOCK, 0 },
{ "Warp", AI_GOAL_WARP, 0 },
{ "Evade ship", AI_GOAL_EVADE_SHIP, 0 },
{ "Ignore ship", AI_GOAL_IGNORE, 0 },
{ "Ignore ship (new)", AI_GOAL_IGNORE_NEW, 0 },
{ "Stay near ship", AI_GOAL_STAY_NEAR_SHIP, 0 },
{ "Keep safe distance", AI_GOAL_KEEP_SAFE_DISTANCE, 0 },
{ "Stay still", AI_GOAL_STAY_STILL, 0 },
{ "Play dead", AI_GOAL_PLAY_DEAD, 0 },
{ "Play dead (persistent)", AI_GOAL_PLAY_DEAD_PERSISTENT, 0 }
};
}
char Fred_callsigns[MAX_SHIPS][NAME_LENGTH + 1];
char Fred_alt_names[MAX_SHIPS][NAME_LENGTH + 1];
extern void allocate_parse_text(size_t size);
namespace fso {
namespace fred {
Editor::Editor() : currentObject{ -1 }, Shield_sys_teams(Iff_info.size(), GlobalShieldStatus::HasShields), Shield_sys_types(MAX_SHIP_CLASSES, GlobalShieldStatus::HasShields) {
connect(fredApp, &FredApplication::onIdle, this, &Editor::update);
// When the mission changes we need to update all renderers
connect(this, &Editor::missionChanged, this, &Editor::updateAllViewports);
// When a mission was loaded we need to notify everyone that the mission has changed
connect(this, &Editor::missionLoaded, this, [this](const std::string&) { missionChanged(); });
_autosaveDirectory = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/autosave/";
QDir().mkpath(_autosaveDirectory);
_autosaveTimer = new QTimer(this);
_autosaveTimer->setSingleShot(false);
connect(_autosaveTimer, &QTimer::timeout, this, &Editor::performTimedAutosave);
fredApp->runAfterInit([this]() { initialSetup(); });
}
EditorViewport* Editor::createEditorViewport(os::Viewport* renderView) {
std::unique_ptr<FredRenderer> renderer(new FredRenderer(renderView));
std::unique_ptr<EditorViewport> viewport(new EditorViewport(this, std::move(renderer)));
auto ptr = viewport.get();
_viewports.push_back(std::move(viewport));
return ptr;
}
void Editor::update() {
// Do updates for all renderers
for (auto& viewport : _viewports) {
viewport->game_do_frame(currentObject);
}
}
void Editor::maybeUseAutosave(std::string& filepath)
{
const QString qpath = QString::fromStdString(filepath);
const QString basename = QFileInfo(qpath).fileName();
const QString autosavePath = _autosaveDirectory + basename;
const QFileInfo autosaveInfo(autosavePath);
if (!autosaveInfo.exists())
return;
const QFileInfo originalInfo(qpath);
if (autosaveInfo.lastModified() <= originalInfo.lastModified())
return;
if (_lastActiveViewport == nullptr || _lastActiveViewport->dialogProvider == nullptr)
return;
const auto result = _lastActiveViewport->dialogProvider->showButtonDialog(
DialogType::Question,
"Autosave Recovery",
"An autosave file for this mission is newer than the original. Load the autosave instead?",
{ DialogButton::Yes, DialogButton::No });
if (result == DialogButton::Yes) {
filepath = autosavePath.toStdString();
}
}
void Editor::startAutosaveTimer(int intervalSeconds) {
_autosaveTimer->stop();
if (intervalSeconds > 0)
_autosaveTimer->start(intervalSeconds * 1000);
}
void Editor::stopAutosaveTimer() {
_autosaveTimer->stop();
}
void Editor::setCurrentMissionPath(const QString& path) {
_currentMissionPath = path;
}
void Editor::performTimedAutosave() {
QString savePath;
if (_currentMissionPath.isEmpty()) {
savePath = _autosaveDirectory + "untitled_autosave.fs2";
} else {
savePath = _autosaveDirectory + QFileInfo(_currentMissionPath).fileName();
}
autosaveDue(savePath);
}
bool Editor::loadMission(const std::string& mission_name, int flags) {
char name[512], * old_name;
int i, j, ob;
object* objp;
// activate the localizer hash table
fhash_flush();
clearMission(flags & MPF_FAST_RELOAD);
std::string filepath = mission_name;
auto res = cf_find_file_location(filepath.c_str(), CF_TYPE_MISSIONS);
if (res.found) {
// We found this file in the CFile system
if (res.offset == 0) {
// This is a "real" file. Since we now have an absolute path we can use that to make sure we only deal with
// absolute paths which makes sure that the "recent mission" menu has correct file names
filepath = res.full_name;
}
}
// message 1: required version
if (!parse_main(filepath.c_str(), flags)) {
auto term = (flags & MPF_IMPORT_FSM) ? "import" : "load";
SCP_string msg;
// the version will have been assigned before loading was aborted
if (!gameversion::check_at_least(The_mission.required_fso_version)) {
msg += "The file \"";
msg += filepath;
msg += "\" cannot be ";
msg += term;
msg += "ed because it requires FSO version ";
msg += format_version(The_mission.required_fso_version, true);
msg += ".";
}
else {
msg += "Unable to ";
msg += term;
msg += " the file \"";
msg += filepath;
msg += "\".";
}
_lastActiveViewport->dialogProvider->showButtonDialog(DialogType::Error,
"Load Error",
msg,
{ DialogButton::Ok });
createNewMission();
return false;
}
// message 2: unknown classes
if ((Num_unknown_ship_classes > 0) || (Num_unknown_prop_classes > 0) || (Num_unknown_weapon_classes > 0) || (Num_unknown_loadout_classes > 0)) {
if (flags & MPF_IMPORT_FSM) {
SCP_string msg;
sprintf(msg,
"Fred encountered unknown ship/prop/weapon classes when importing \"%s\" (path \"%s\"). You will have to manually edit the converted mission to correct this.",
The_mission.name.c_str(),
filepath.c_str());
_lastActiveViewport->dialogProvider->showButtonDialog(DialogType::Warning,
"Unknown object classes",
msg,
{ DialogButton::Ok });
} else {
_lastActiveViewport->dialogProvider->showButtonDialog(DialogType::Warning,
"Unknown object classes",
"Fred encountered unknown ship/prop/weapon classes when parsing the mission file. This may be due to mission disk data you do not have.",
{ DialogButton::Ok });
}
}
// message 3: warning about saving under a new version
if (!(flags & MPF_IMPORT_FSM) && (The_mission.required_fso_version != LEGACY_MISSION_VERSION) && (MISSION_VERSION > The_mission.required_fso_version)) {
SCP_string msg = "This mission's file format is ";
msg += format_version(The_mission.required_fso_version, true);
msg += ". When you save this mission, the file format will be migrated to ";
msg += format_version(MISSION_VERSION, true);
msg += ". FSO versions earlier than this will not be able to load the mission.";
_lastActiveViewport->dialogProvider->showButtonDialog(DialogType::Information,
"Mission File Format",
msg,
{ DialogButton::Ok });
}
// message 4: check for "immobile" flag migration
if (!Fred_migrated_immobile_ships.empty()) {
SCP_string msg = "The \"immobile\" ship flag has been superseded by the \"don't-change-position\", and \"don't-change-orientation\" flags. "
"All ships which previously had \"Does Not Move\" checked in the ship flags editor will now have both \"Does Not Change Position\" and "
"\"Does Not Change Orientation\" checked.\n\nWould you like to open the error checker now to review any potential issues "
"involving these flags?\n\nThe following ships have been migrated:";
for (int shipnum : Fred_migrated_immobile_ships) {
msg += "\n\t";
msg += Ships[shipnum].ship_name;
}
truncate_message_lines(msg, 30);
auto z = _lastActiveViewport->dialogProvider->showButtonDialog(DialogType::Question,
"Ship Flag Migration",
msg,
{ DialogButton::Yes, DialogButton::No });
if (z == DialogButton::Yes) {
// Consumed by FredView::autoRunErrorChecker after loadMission returns.
_lastActiveViewport->Error_checker_force_display_potentials_once = true;
}
}
obj_merge_created_list();
objp = GET_FIRST(&obj_used_list);
while (objp != END_OF_LIST(&obj_used_list)) {
if (objp->flags[Object::Object_Flags::Player_ship]) {
Assert(objp->type == OBJ_SHIP);
objp->type = OBJ_START;
// Player_starts++;
}
objp = GET_NEXT(objp);
}
for (i = 0; i < Num_wings; i++) {
for (j = 0; j < Wings[i].wave_count; j++) {
ob = Ships[Wings[i].ship_index[j]].objnum;
wing_objects[i][j] = ob;
Ships[Wings[i].ship_index[j]].wingnum = i;
Ships[Wings[i].ship_index[j]].arrival_cue = Locked_sexp_false;
}
// fix old ship names for ships in wings if needed
while (j--) {
if ((Objects[wing_objects[i][j]].type == OBJ_SHIP) || (Objects[wing_objects[i][j]].type == OBJ_START)) { // don't change player ship names
wing_bash_ship_name(name, Wings[i].name, j + 1);
old_name = Ships[Wings[i].ship_index[j]].ship_name;
if (stricmp(name, old_name) != 0) { // need to fix name
update_sexp_references(old_name, name);
ai_update_goal_references(sexp_ref_type::SHIP, old_name, name);
update_texture_replacements(old_name, name);
int k = find_item_with_string(Reinforcements, &reinforcements::name, old_name);
if (k >= 0) {
Assert(strlen(name) < NAME_LENGTH);
strcpy_s(Reinforcements[k].name, name);
}
// bash it again so that we handle display names if needed
wing_bash_ship_name(&Ships[Wings[i].ship_index[j]], &Wings[i], j + 1, true);
}
}
}
}
for (i = 0; i < Num_teams; i++) {
generate_team_weaponry_usage_list(i, _weapon_usage[i]);
for (j = 0; j < Team_data[i].num_weapon_choices; j++) {
// The amount used in wings is always set by a static loadout entry so skip any that were set by Sexp variables
if ((!strlen(Team_data[i].weaponry_pool_variable[j]))
&& (!strlen(Team_data[i].weaponry_amount_variable[j]))) {
// convert weaponry_pool to be extras available beyond the current ships weapons
Team_data[i].weaponry_count[j] -= _weapon_usage[i][Team_data[i].weaponry_pool[j]];
if (Team_data[i].weaponry_count[j] < 0) {
Team_data[i].weaponry_count[j] = 0;
}
// zero the used pool entry
_weapon_usage[i][Team_data[i].weaponry_pool[j]] = 0;
}
}
// Weapons used in wings but missing from the loadout pool are flagged by the error checker.
}
Assert(Mission_palette >= 0);
Assert(Mission_palette <= 98);
// go through all ships and translate their callsign and alternate name indices
objp = GET_FIRST(&obj_used_list);
while (objp != END_OF_LIST(&obj_used_list)) {
// if this is a ship, check it, and mark its possible alternate name down in the auxiliary array
if (((objp->type == OBJ_SHIP) || (objp->type == OBJ_START)) && (objp->instance >= 0)) {
if (Ships[objp->instance].alt_type_index >= 0) {
strcpy_s(Fred_alt_names[objp->instance], mission_parse_lookup_alt_index(Ships[objp->instance].alt_type_index));
// also zero it
Ships[objp->instance].alt_type_index = -1;
}
if (Ships[objp->instance].callsign_index >= 0) {
strcpy_s(Fred_callsigns[objp->instance], mission_parse_lookup_callsign_index(Ships[objp->instance].callsign_index));
// also zero it
Ships[objp->instance].callsign_index = -1;
}
}
objp = GET_NEXT(objp);
}
for (auto& viewport : _viewports) {
viewport->camera.view_orient = Parse_viewer_orient;
viewport->camera.view_pos = Parse_viewer_pos;
}
if (flags & MPF_IS_TEMPLATE) {
// reset fields that should not carry over from the template source
The_mission.name = "Untitled";
The_mission.author = getUsername();
time_t currentTime;
time(¤tTime);
auto timeinfo = localtime(¤tTime);
time_to_mission_info_string(timeinfo, The_mission.created, DATE_TIME_LENGTH - 1);
strcpy_s(The_mission.modified, The_mission.created);
strcpy_s(The_mission.notes, "This is a FRED2_OPEN created mission.");
strcpy_s(The_mission.mission_desc, "Put mission description here");
for (auto& viewport : _viewports) {
viewport->reset();
}
}
stars_post_level_init();
missionLoaded(filepath);
// This hook will allow for scripts to know when a mission has been loaded
// which will then allow them to update any LuaEnums that may be related to sexps
if (scripting::hooks::FredOnMissionLoad->isActive()) {
scripting::hooks::FredOnMissionLoad->run();
}
if (!(flags & MPF_FAST_RELOAD)) {
// TODO(Phase 3): _undoStack->clear()
}
return true;
}
void Editor::clean_up_selections() {
unmark_all();
}
void Editor::unmark_all() {
if (numMarked > 0) {
for (auto i = 0; i < MAX_OBJECTS; i++) {
Objects[i].flags.remove(Object::Object_Flags::Marked);
if (Objects[i].type != OBJ_NONE) {
// Only emit signals for valid objects
objectMarkingChanged(i, false);
}
}
numMarked = 0;
setupCurrentObjectIndices(-1);
updateAllViewports();
}
}
void Editor::markObject(int obj) {
Assert(query_valid_object(obj));
if (!(Objects[obj].flags[Object::Object_Flags::Marked])) {
Objects[obj].flags.set(Object::Object_Flags::Marked); // set as marked
objectMarkingChanged(obj, true);
numMarked++;
if (currentObject == -1) {
setupCurrentObjectIndices(obj);
}
updateAllViewports();
}
}
void Editor::unmarkObject(int obj) {
Assert(query_valid_object(obj));
if (Objects[obj].flags[Object::Object_Flags::Marked]) {
Objects[obj].flags.remove(Object::Object_Flags::Marked);
objectMarkingChanged(obj, false);
numMarked--;
if (obj == currentObject) { // need to find a new index
object* ptr;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {
if (ptr->flags[Object::Object_Flags::Marked]) {
setupCurrentObjectIndices(OBJ_INDEX(ptr)); // found one
return;
}
ptr = GET_NEXT(ptr);
}
setupCurrentObjectIndices(-1); // can't find one; nothing is marked.
}
updateAllViewports();
}
}
void Editor::clearMission(bool fast_reload) {
// clean up everything we need to before we reset back to defaults.
clean_up_selections();
allocate_parse_text(PARSE_TEXT_SIZE);
mission_init(&The_mission);
obj_init();
if (!fast_reload)
model_free_all(); // Free all existing models
ai_init();
props_level_init();
asteroid_level_init();
ship_level_init();
nebula_init(Nebula_index, Nebula_pitch, Nebula_bank, Nebula_heading);
Shield_sys_teams.clear();
Shield_sys_teams.resize(Iff_info.size(), GlobalShieldStatus::HasShields);
for (int i = 0; i < MAX_SHIP_CLASSES; i++) {
Shield_sys_types[i] = GlobalShieldStatus::HasShields;
}
setupCurrentObjectIndices(-1);
auto userName = getUsername();
time_t currentTime;
time(¤tTime);
auto timeinfo = localtime(¤tTime);
The_mission.name = "Untitled";
The_mission.author = userName;
time_to_mission_info_string(timeinfo, The_mission.created, DATE_TIME_LENGTH - 1);
strcpy_s(The_mission.modified, The_mission.created);
strcpy_s(The_mission.notes, "This is a FRED2_OPEN created mission.");
strcpy_s(The_mission.mission_desc, "Put mission description here");
apply_default_custom_data(&The_mission);
// reset alternate name & callsign stuff
for (auto i = 0; i < MAX_SHIPS; i++) {
strcpy_s(Fred_alt_names[i], "");
strcpy_s(Fred_callsigns[i], "");
}
// set up the default ship types for all teams. For now, this is the same class
// of ships for all teams
for (auto i = 0; i < MAX_TVT_TEAMS; i++) {
auto count = 0;
for (auto j = 0; j < static_cast<int>(Ship_info.size()); j++) {
if (Ship_info[j].flags[Ship::Info_Flags::Default_player_ship]) {
Team_data[i].ship_list[count] = j;
strcpy_s(Team_data[i].ship_list_variables[count], "");
Team_data[i].ship_count[count] = 5;
strcpy_s(Team_data[i].ship_count_variables[count], "");
count++;
}
}
Team_data[i].num_ship_choices = count;
count = 0;
for (auto j = 0; j < static_cast<int>(Weapon_info.size()); j++) {
if (Weapon_info[j].wi_flags[Weapon::Info_Flags::Default_player_weapon]) {
if (Weapon_info[j].subtype == WP_LASER) {
Team_data[i].weaponry_count[count] = 16;
} else {
Team_data[i].weaponry_count[count] = 500;
}
Team_data[i].weaponry_pool[count] = j;
strcpy_s(Team_data[i].weaponry_pool_variable[count], "");
strcpy_s(Team_data[i].weaponry_amount_variable[count], "");
count++;
}
Team_data[i].weapon_required[j] = false;
}
Team_data[i].num_weapon_choices = count;
}
unmark_all();
for (auto& viewport : _viewports) {
viewport->reset();
}
Event_annotations.clear();
Fred_migrated_immobile_ships.clear();
// free memory from all parsing so far -- see also the stop_parse() in player_select_close() which frees all tbls found during game_init()
stop_parse();
// however, FRED expects to parse comments from the raw buffer, so we need a nominal string for that
allocate_parse_text(1);
}
void Editor::initialSetup() {
}
void Editor::setupCurrentObjectIndices(int selectedObj) {
if (query_valid_object(selectedObj)) {
currentObject = selectedObj;
cur_ship = cur_wing = -1;
cur_waypoint_list = nullptr;
cur_waypoint = nullptr;
if ((Objects[selectedObj].type == OBJ_SHIP) || (Objects[selectedObj].type == OBJ_START)) {
cur_ship = Objects[selectedObj].instance;
cur_wing = Ships[cur_ship].wingnum;
} else if (Objects[selectedObj].type == OBJ_WAYPOINT) {
cur_waypoint = find_waypoint_with_instance(Objects[selectedObj].instance);
Assert(cur_waypoint != nullptr);
cur_waypoint_list = cur_waypoint->get_parent_list();
}
currentObjectChanged(currentObject);
return;
}
if (selectedObj == -1 || !Num_objects) {
currentObject = cur_ship = cur_wing = -1;
cur_waypoint_list = nullptr;
cur_waypoint = nullptr;
currentObjectChanged(currentObject);
return;
}
object* ptr;
if (query_valid_object(currentObject)) {
ptr = Objects[currentObject].next;
} else {
ptr = GET_FIRST(&obj_used_list);
}
if (ptr == END_OF_LIST(&obj_used_list)) {
ptr = ptr->next;
}
Assert(ptr != END_OF_LIST(&obj_used_list));
currentObject = OBJ_INDEX(ptr);
Assert(ptr->type != OBJ_NONE);
cur_ship = cur_wing = -1;
cur_waypoint_list = nullptr;
cur_waypoint = nullptr;
if (ptr->type == OBJ_SHIP) {
cur_ship = ptr->instance;
cur_wing = Ships[cur_ship].wingnum;
} else if (ptr->type == OBJ_WAYPOINT) {
cur_waypoint = find_waypoint_with_instance(ptr->instance);
Assert(cur_waypoint != nullptr);
cur_waypoint_list = cur_waypoint->get_parent_list();
}
currentObjectChanged(currentObject);
}
void Editor::selectObject(int objId) {
if (objId < 0) {
unmark_all();
} else {
markObject(objId);
}
setupCurrentObjectIndices(objId); // select the new object
}
void Editor::updateAllViewports() {
// This takes all renderers and issues an update request for each of them. For now that is only one but this allows
// us to expand FRED to multiple view ports in the future.
for (auto& viewport : _viewports) {
viewport->needsUpdate();
}
}
int Editor::create_player(vec3d* pos, matrix* orient, int type) {
int obj;
if (type == -1) {
type = get_default_player_ship_index();
}
Assert(type >= 0);
obj = create_ship(orient, pos, type);
Objects[obj].type = OBJ_START;
Assert(Player_starts < MAX_PLAYERS);
Player_starts++;
if (Player_start_shipnum < 0) {
Player_start_shipnum = Objects[obj].instance;
}
// be sure arrival/departure cues are set
Ships[Objects[obj].instance].arrival_cue = Locked_sexp_true;
Ships[Objects[obj].instance].departure_cue = Locked_sexp_false;
obj_merge_created_list();
missionChanged();
return obj;
}
int Editor::create_ship(matrix* orient, vec3d* pos, int ship_type) {
int obj;
float temp_max_hull_strength;
ship_info* sip;
obj = ship_create(orient, pos, ship_type);
if (obj == -1) {
return -1;
}
Objects[obj].phys_info.speed = 33.0f;
ship* shipp = &Ships[Objects[obj].instance];
sip = &Ship_info[shipp->ship_info_index];
if (query_ship_name_duplicate(Objects[obj].instance)) {
fix_ship_name(Objects[obj].instance);
}
// default stuff according to species and IFF
shipp->team = Species_info[Ship_info[shipp->ship_info_index].species].default_iff;
resolve_parse_flags(&Objects[obj], Iff_info[shipp->team].default_parse_flags);
// default shield setting
shipp->special_shield = -1;
auto z1 = Shield_sys_teams[shipp->team];
auto z2 = Shield_sys_types[ship_type];
if (((z1 == GlobalShieldStatus::NoShields) && z2 != GlobalShieldStatus::HasShields) || (z2 == GlobalShieldStatus::NoShields)) {
Objects[obj].flags.set(Object::Object_Flags::No_shields);
}
// set orders according to whether the ship is on the player ship's team
{
object* temp_objp;
ship* temp_shipp = nullptr;
// find the first player ship
for (temp_objp = GET_FIRST(&obj_used_list); temp_objp != END_OF_LIST(&obj_used_list);
temp_objp = GET_NEXT(temp_objp)) {
if (temp_objp->type == OBJ_START) {
temp_shipp = &Ships[temp_objp->instance];
break;
}
}
// set orders if teams match, or if player couldn't be found
if (temp_shipp == nullptr || shipp->team == temp_shipp->team) {
// if this ship is not a small ship, then make the orders be the default orders without
// the depart item
if (!(sip->is_small_ship())) {
shipp->orders_accepted = ship_get_default_orders_accepted(sip);
shipp->orders_accepted.erase(DEPART_ITEM);
}
} else {
shipp->orders_accepted.clear();
}
}
// calc kamikaze stuff
if (shipp->use_special_explosion) {
temp_max_hull_strength = (float) shipp->special_exp_blast;
} else {
temp_max_hull_strength = sip->max_hull_strength;
}
Ai_info[shipp->ai_index].kamikaze_damage = (int) std::min(1000.0f, 200.0f + (temp_max_hull_strength / 4.0f));
missionChanged();
return obj;
}
bool Editor::query_ship_name_duplicate(int ship) {
int i;
for (i = 0; i < MAX_SHIPS; i++) {
if ((i != ship) && (Ships[i].objnum != -1)) {
if (!stricmp(Ships[i].ship_name, Ships[ship].ship_name)) {
return true;
}
}
}
return false;
}
void Editor::fix_ship_name(int ship) {
int i = 1;
do {
sprintf(Ships[ship].ship_name, "U.R.A. Moron %d", i++);
} while (query_ship_name_duplicate(ship));
}
void Editor::createNewMission() {
clearMission();
create_player(&vmd_zero_vector, &vmd_identity_matrix);
stars_post_level_init();
// TODO(Phase 3): _undoStack->clear()
missionLoaded("");
}
void Editor::hideMarkedObjects() {
object* ptr;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {
if (ptr->flags[Object::Object_Flags::Marked]) {
ptr->flags.set(Object::Object_Flags::Hidden);
unmarkObject(OBJ_INDEX(ptr));
}
ptr = GET_NEXT(ptr);
}
updateAllViewports();
}
void Editor::showHiddenObjects() {
object* ptr;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {
ptr->flags.remove(Object::Object_Flags::Hidden);
ptr = GET_NEXT(ptr);
}
updateAllViewports();
}
void Editor::lockMarkedObjects() {
object* ptr;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {
if (ptr->flags[Object::Object_Flags::Marked]) {
ptr->flags.set(Object::Object_Flags::Locked_from_editing);
unmarkObject(OBJ_INDEX(ptr));
}
ptr = GET_NEXT(ptr);
}
updateAllViewports();
}
void Editor::unlockAllObjects() {
object* ptr;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {
ptr->flags.remove(Object::Object_Flags::Locked_from_editing);
ptr = GET_NEXT(ptr);
}
updateAllViewports();
}
int Editor::create_waypoint(vec3d* pos, int waypoint_instance) {
int obj = waypoint_add(pos, waypoint_instance);
missionChanged();
return obj;
}
int Editor::getNumMarked() {
return numMarked;
}
int Editor::dup_object(object* objp) {
int i, n, inst, obj = -1;
ai_info* aip1, * aip2;
object* objp1, * objp2;
ship_subsys* subp1, * subp2;
static int waypoint_instance(-1);
if (!objp) {
waypoint_instance = -1;
return 0;
}
inst = objp->instance;
if ((objp->type == OBJ_SHIP) || (objp->type == OBJ_START)) {
obj = create_ship(&objp->orient, &objp->pos, Ships[inst].ship_info_index);
if (obj == -1) {
return -1;
}
n = Objects[obj].instance;
Ships[n].team = Ships[inst].team;
Ships[n].arrival_cue = dup_sexp_chain(Ships[inst].arrival_cue);
Ships[n].departure_cue = dup_sexp_chain(Ships[inst].departure_cue);
Ships[n].cargo1 = Ships[inst].cargo1;
Ships[n].arrival_location = Ships[inst].arrival_location;
Ships[n].departure_location = Ships[inst].departure_location;
Ships[n].arrival_delay = Ships[inst].arrival_delay;
Ships[n].departure_delay = Ships[inst].departure_delay;
Ships[n].weapons = Ships[inst].weapons;
Ships[n].hotkey = Ships[inst].hotkey;
aip1 = &Ai_info[Ships[n].ai_index];
aip2 = &Ai_info[Ships[inst].ai_index];
aip1->ai_class = aip2->ai_class;
for (i = 0; i < MAX_AI_GOALS; i++) {
aip1->goals[i] = aip2->goals[i];
}
if (aip2->ai_flags[AI::AI_Flags::Kamikaze]) {
aip1->ai_flags.set(AI::AI_Flags::Kamikaze);
}
if (aip2->ai_flags[AI::AI_Flags::No_dynamic]) {
aip2->ai_flags.set(AI::AI_Flags::No_dynamic);
}
aip1->kamikaze_damage = aip2->kamikaze_damage;
objp1 = &Objects[obj];
objp2 = &Objects[Ships[inst].objnum];
objp1->phys_info.speed = objp2->phys_info.speed;
objp1->phys_info.fspeed = objp2->phys_info.fspeed;
objp1->hull_strength = objp2->hull_strength;
objp1->shield_quadrant[0] = objp2->shield_quadrant[0];
subp1 = GET_FIRST(&Ships[n].subsys_list);
subp2 = GET_FIRST(&Ships[inst].subsys_list);
while (subp1 != END_OF_LIST(&Ships[n].subsys_list)) {
Assert(subp2 != END_OF_LIST(&Ships[inst].subsys_list));
subp1->current_hits = subp2->current_hits;
subp1 = GET_NEXT(subp1);
subp2 = GET_NEXT(subp2);
}
i = find_item_with_string(Reinforcements, &reinforcements::name, Ships[inst].ship_name);
if (i >= 0) {
Reinforcements.push_back(Reinforcements[i]);
strcpy_s(Reinforcements.back().name, Ships[n].ship_name);
}
} else if (objp->type == OBJ_WAYPOINT) {
obj = create_waypoint(&objp->pos, waypoint_instance);
waypoint_instance = Objects[obj].instance;
} else if (objp->type == OBJ_JUMP_NODE) {
CJumpNode jnp(&objp->pos);
obj = jnp.GetSCPObjectNumber();
Jump_nodes.push_back(std::move(jnp));
} else if (objp->type == OBJ_PROP) {
prop* propp = prop_id_lookup(inst);
if (propp != nullptr) {
obj = prop_create(&objp->orient, &objp->pos, propp->prop_info_index);
}
}
if (obj == -1) {
return -1;
}
Objects[obj].pos = objp->pos;
Objects[obj].orient = objp->orient;
Objects[obj].flags.set(Object::Object_Flags::Temp_marked);
return obj;
}
int Editor::common_object_delete(int obj) {
char msg[255];
const char *name;
int i, z, r, type;
object* objp;
SCP_list<CJumpNode>::iterator jnp;
type = Objects[obj].type;
if (type == OBJ_START) {
i = Objects[obj].instance;
if (Player_starts < 2) { // player 1 start
_lastActiveViewport->dialogProvider->showButtonDialog(DialogType::Error,
"Error",
"Must have at least 1 player starting point!",
{ DialogButton::Ok });
unmarkObject(obj);
return 1;
}
Assert((i >= 0) && (i < MAX_SHIPS));
sprintf(msg, "Player %d", i + 1);
name = msg;
r = reference_handler(name, sexp_ref_type::PLAYER, obj);
if (r) {
return r;
}
if (Ships[i].wingnum >= 0) {
r = delete_ship_from_wing(i);
if (r) {
return r;
}
}
Objects[obj].type = OBJ_SHIP; // was allocated as a ship originally, so remove as such.
invalidate_references(name, sexp_ref_type::PLAYER);
// check if any ship is docked with this ship and break dock if so
while (object_is_docked(&Objects[obj])) {
ai_do_objects_undocked_stuff(&Objects[obj], dock_get_first_docked_object(&Objects[obj]));
}