forked from scp-fs2open/fs2open.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.cpp
More file actions
2646 lines (2142 loc) · 66.8 KB
/
Copy pathmanagement.cpp
File metadata and controls
2646 lines (2142 loc) · 66.8 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
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "stdafx.h"
#include "FRED.h"
#include "MainFrm.h"
#include "FREDDoc.h"
#include "FREDView.h"
#include "FredRender.h"
#include "ai/aigoals.h"
#include "ship/ship.h"
#include "globalincs/linklist.h"
#include "globalincs/version.h"
#include "globalincs/alphacolors.h"
#include "math/curve.h"
#include "mission/missiongrid.h"
#include "mission/missionparse.h"
#include "mission/missionmessage.h"
#include "mission/missiongoals.h"
#include "mission/missionbriefcommon.h"
#include "missioneditor/common.h"
#include "model/modelreplace.h"
#include "Management.h"
#include "cfile/cfile.h"
#include "graphics/2d.h"
#include "render/3d.h"
#include "weapon/weapon.h"
#include "io/key.h"
#include "parse/parselo.h"
#include "math/fvi.h"
#include "starfield/starfield.h"
#include "parse/sexp.h"
#include "parse/sexp/sexp_lookup.h"
#include "io/mouse.h"
#include "mission/missioncampaign.h"
#include "wing.h"
#include "MessageEditorDlg.h"
#include "EventEditor.h"
#include "MissionGoalsDlg.h"
#include "MissionCutscenesDlg.h"
#include "ShieldSysDlg.h"
#include "gamesnd/eventmusic.h"
#include "DebriefingEditorDlg.h"
#include "starfield/nebula.h"
#include "asteroid/asteroid.h"
#include "hud/hudsquadmsg.h"
#include "jumpnode/jumpnode.h"
#include "stats/medals.h"
#include "localization/localize.h"
#include "osapi/osregistry.h"
#include "localization/fhash.h"
#include "io/timer.h"
#include "nebula/neb.h"
#include "nebula/neblightning.h"
#include "species_defs/species_defs.h"
#include "lighting/lighting_profiles.h"
#include "osapi/osapi.h"
#include "graphics/font.h"
#include "object/objectdock.h"
#include "gamesnd/gamesnd.h"
#include "iff_defs/iff_defs.h"
#include "menuui/techmenu.h"
#include "missionui/fictionviewer.h"
#include "mod_table/mod_table.h"
#include "libs/ffmpeg/FFmpeg.h"
#include "scripting/scripting.h"
#include "scripting/global_hooks.h"
#include "utils/Random.h"
#include "prop/prop.h"
#include <direct.h>
#include "cmdline/cmdline.h"
#define SDL_MAIN_HANDLED
#include <SDL_main.h>
#define MAX_DOCKS 1000
#define UNKNOWN_USER "Unknown"
extern void ssm_init(); // Need this to populate Ssm_info so OPF_SSM_CLASS does something. -MageKing17
int cur_wing = -1;
int cur_wing_index;
int cur_object_index = -1;
int cur_ship = -1;
int cur_ship_type_combo_index = 0;
waypoint *cur_waypoint = NULL;
waypoint_list *cur_waypoint_list = NULL;
int delete_flag;
int bypass_update = 0;
int Update_ship = 0;
int Update_wing = 0;
int Update_prop = 0;
char Fred_exe_dir[512] = "";
char Fred_base_dir[512] = "";
char Fred_alt_names[MAX_SHIPS][NAME_LENGTH+1];
char Fred_callsigns[MAX_SHIPS][NAME_LENGTH+1];
extern void allocate_parse_text(size_t size);
// object numbers for ships in a wing.
int wing_objects[MAX_WINGS][MAX_SHIPS_PER_WING];
char *Docking_bay_list[MAX_DOCKS];
// Goober5000
SCP_vector<bool> Show_iff;
CCriticalSection CS_cur_object_index;
// Used in the FRED drop-down menu and in error_check_initial_orders
// 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 },
{ "Attack ship type", AI_GOAL_CHASE_SHIP_TYPE, 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 }
};
int Ai_goal_list_size = sizeof(Ai_goal_list) / sizeof(ai_goal_list);
// internal function prototypes
void set_cur_indices(int obj);
int common_object_delete(int obj);
int create_waypoint(vec3d *pos, int waypoint_instance);
int create_ship(matrix *orient, vec3d *pos, int ship_type);
int query_ship_name_duplicate(int ship);
int query_prop_name_duplicate(int prop);
char* reg_read_string(char* section, char* name, char* default_value);
// Note that the parameter here is max *length*, not max buffer size. Leave room for the null-terminator!
void string_copy(char *dest, const CString &src, size_t max_len, bool modify)
{
if (modify)
if (strcmp(src, dest))
set_modified();
auto len = strlen(src);
if (len > max_len)
len = max_len;
strncpy(dest, src, len);
dest[len] = 0;
}
void string_copy(SCP_string &dest, const CString &src, bool modify)
{
if (modify)
if (strcmp(src, dest.c_str()))
set_modified();
dest = src;
}
// converts a multiline string (one with newlines in it) into a windows format multiline
// string (newlines changed to '\r\n').
void convert_multiline_string(CString &dest, const SCP_string &src)
{
dest = src.c_str();
dest.Replace("\n", "\r\n");
}
// converts a multiline string (one with newlines in it) into a windows format multiline
// string (newlines changed to '\r\n').
void convert_multiline_string(CString &dest, const char *src)
{
dest = src;
dest.Replace("\n", "\r\n");
}
// Converts a windows format multiline CString back into a normal multiline string.
void deconvert_multiline_string(char *dest, const CString &str, size_t max_len)
{
// leave room for the null terminator
memset(dest, 0, max_len + 1);
strncpy(dest, (LPCTSTR) str, max_len);
replace_all(dest, "\r\n", "\n", max_len);
}
// ditto for SCP_string
void deconvert_multiline_string(SCP_string &dest, const CString &str)
{
dest = str;
replace_all(dest, "\r\n", "\n");
}
void strip_quotation_marks(CString& str) { str.Remove('\"'); }
void pad_with_newline(CString& str, int max_size) {
int len = str.GetLength();
if (!len) {
len = 1;
}
if (str[len - 1] != '\n' && len < max_size) {
str += _T("\n");
}
}
void lcl_fred_replace_stuff(CString &text)
{
// this should be kept in sync with the function in localize.cpp
text.Replace("\"", "$quote");
text.Replace(";", "$semicolon");
text.Replace("/", "$slash");
text.Replace("\\", "$backslash");
}
CString get_display_name_for_text_box(const char *orig_name)
{
auto p = get_pointer_to_first_hash_symbol(orig_name);
if (p)
{
// use the same logic as in end_string_at_first_hash_symbol, but rewritten for CString
CString display_name(orig_name, static_cast<int>(p - orig_name));
display_name.TrimRight();
return display_name;
}
else
return "<none>";
}
void fred_preload_all_briefing_icons()
{
for (SCP_vector<briefing_icon_info>::iterator ii = Briefing_icon_info.begin(); ii != Briefing_icon_info.end(); ++ii)
{
generic_anim_load(&ii->regular);
hud_anim_load(&ii->fade);
hud_anim_load(&ii->highlight);
}
}
bool fred_init(std::unique_ptr<os::GraphicsOperations>&& graphicsOps)
{
int i;
SDL_SetMainReady();
Random::seed(static_cast<unsigned int>(time(nullptr)));
init_pending_messages();
os_init(Osreg_class_name, Osreg_app_name);
timer_init();
Assert(strlen(Fred_base_dir) > 0); //-V805
// sigh... this should enable proper reading of cmdline_fso.cfg - Goober5000
cfile_chdir(Fred_base_dir);
// this should enable mods - Kazan
if (!parse_cmdline(__argc, __argv)) {
// Command line contained an option that terminates the program immediately
exit(1);
}
#ifndef NDEBUG
#if FS_VERSION_REVIS == 0
mprintf(("Fred2 Open version: %i.%i.%i\n", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD));
#else
mprintf(("Fred2 Open version: %i.%i.%i.%i\n", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD, FS_VERSION_REVIS));
#endif
extern void cmdline_debug_print_cmdline();
cmdline_debug_print_cmdline();
#endif
// d'oh
if(cfile_init(Fred_exe_dir)){
exit(1);
}
// Load game_settings.tbl
mod_table_init();
// initialize localization module. Make sure this is done AFTER initializing OS.
// NOTE: Fred should ALWAYS run without localization. Otherwise it might swap in another language
// when saving - which would cause inconsistencies when externalizing to tstrings.tbl via Exstr
// trust me on this :)
lcl_init(LCL_UNTRANSLATED);
// Goober5000 - force init XSTRs (so they work, but only work untranslated, based on above comment)
extern bool Xstr_inited;
Xstr_inited = true;
#ifndef NDEBUG
load_filter_info();
#endif
//CFREDView *window = CFREDView::GetView();
//HWND hwndApp = window->GetSafeHwnd();
//os_set_window((uint) hwndApp);
snd_init();
// Not ready for this yet
// Cmdline_nospec = 1;
// Cmdline_noglow = 1;
Cmdline_window = 1;
gr_init(std::move(graphicsOps), GR_OPENGL, 640, 480, 32);
gr_set_gamma(3.0f);
io::mouse::CursorManager::get()->showCursor(false);
font::init(); // loads up all fonts
curves_init();
particle::ParticleManager::init();
gamesnd_parse_soundstbl(true);
iff_init(); // Goober5000
species_init(); // Kazan
gamesnd_parse_soundstbl(false);
brief_icons_init();
hud_init_comm_orders(); // Goober5000
// wookieejedi
// load in the controls and defaults including the controlconfigdefault.tbl
// this allows the sexp tree in key-pressed to actually match what the game will use
// especially useful when a custom Controlconfigdefaults.tbl is used
control_config_common_init();
rank_init();
traitor_init();
medals_init(); // get medal names for sexpression usage
key_init();
mouse_init();
model_init();
virtual_pof_init();
event_music_init();
alpha_colors_init();
// get fireball IDs for sexpression usage
// (we don't need to init the entire system via fireball_init, we just need the information)
fireball_parse_tbl();
animation::ModelAnimationParseHelper::parseTables();
sexp_startup(); // Must happen before ship init for LuaAI
obj_init();
armor_init();
ai_init();
ai_profiles_init();
weapon_init();
glowpoint_init();
ship_init();
prop_init();
techroom_intel_init();
hud_positions_init();
asteroid_init();
mission_brief_common_init();
neb2_init(); // fullneb stuff
nebl_init(); // neb lightning
stars_init();
ssm_init(); // The game calls this after stars_init(), and we need Ssm_info initialized for OPF_SSM_CLASS. -MageKing17
lighting_profiles::load_profiles();
// To avoid breaking current mods which do not support scripts in FRED we only initialize the scripting
// system if a special mod_table option is set
if (Enable_scripts_in_fred) {
// Note: Avoid calling any non-script functions after this line and before OnGameInit->run(), lest they run before scripting has completely initialized.
script_init(); //WMC
}
Script_system.RunInitFunctions();
Scripting_game_init_run = true; // set this immediately before OnGameInit so that OnGameInit *itself* will run
if (scripting::hooks::OnGameInit->isActive()) {
scripting::hooks::OnGameInit->run();
}
//Technically after the splash screen, but the best we can do these days. Since the override is hard-deprecated, we don't need to check it.
if (scripting::hooks::OnSplashScreen->isActive()) {
scripting::hooks::OnSplashScreen->run();
}
// A non-deprecated hook that runs after the splash screen has faded out.
if (scripting::hooks::OnSplashEnd->isActive()) {
scripting::hooks::OnSplashEnd->run();
}
libs::ffmpeg::initialize();
// for fred specific replacement texture stuff
Fred_texture_replacements.clear();
// Goober5000
Show_iff.clear();
for (i = 0; i < (int)Iff_info.size(); i++)
Show_iff.push_back(true);
// Goober5000
strcpy_s(Voice_abbrev_briefing, "");
strcpy_s(Voice_abbrev_campaign, "");
strcpy_s(Voice_abbrev_command_briefing, "");
strcpy_s(Voice_abbrev_debriefing, "");
strcpy_s(Voice_abbrev_message, "");
strcpy_s(Voice_abbrev_mission, "");
Voice_no_replace_filenames = false;
strcpy_s(Voice_script_entry_format, Voice_script_default_string.c_str());
Voice_export_selection = 0;
Show_waypoints = TRUE;
// initialize and activate external string hash table
// make sure to do here so that we don't parse the table files into the hash table - waste of space
fhash_init();
fhash_activate();
fred_preload_all_briefing_icons(); //phreak. This needs to be done or else the briefing icons won't show up
fiction_viewer_reset();
cmd_brief_reset();
// mission creation requires the existence of a timestamp snapshot
timer_start_frame();
mission_campaign_clear();
create_new_mission();
gr_reset_clip();
g3_start_frame(0);
g3_set_view_matrix(&eye_pos, &eye_orient, 0.5f);
Fred_main_wnd -> init_tools();
return true;
}
void set_physics_controls()
{
physics_init(&view_physics);
view_physics.max_vel.xyz.x *= physics_speed / 3.0f;
view_physics.max_vel.xyz.y *= physics_speed / 3.0f;
view_physics.max_vel.xyz.z *= physics_speed / 3.0f;
view_physics.max_rear_vel *= physics_speed / 3.0f;
view_physics.max_rotvel.xyz.x *= physics_rot / 30.0f;
view_physics.max_rotvel.xyz.y *= physics_rot / 30.0f;
view_physics.max_rotvel.xyz.z *= physics_rot / 30.0f;
view_physics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
theApp.write_ini_file(1);
}
int create_object_on_grid(int waypoint_instance, bool prop)
{
int obj = -1;
float rval;
vec3d dir,pos;
g3_point_to_vec_delayed(&dir, marking_box.x2, marking_box.y2);
rval = fvi_ray_plane(&pos, &The_grid->center, &The_grid->gmatrix.vec.uvec, &view_pos, &dir, 0.0f);
if (rval>=0.0f) {
unmark_all();
obj = create_object(&pos, waypoint_instance, prop);
if (obj >= 0) {
mark_object(obj);
FREDDoc_ptr->autosave("object create");
} else if (obj == -1) {
if (prop && Prop_info.empty()) {
Fred_main_wnd->MessageBox("No props defined. Can't create prop object!");
} else {
Fred_main_wnd->MessageBox("Maximum object limit reached. Can't add any more objects.");
}
}
}
return obj;
}
void fix_prop_name(int prop)
{
int i = 1;
do {
sprintf(prop_id_lookup(prop)->prop_name, "U.R.A. Dummy %d", i++);
} while (query_prop_name_duplicate(prop));
}
void 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));
}
int create_ship(matrix *orient, vec3d *pos, int ship_type)
{
int obj, z1, z2;
float temp_max_hull_strength;
ship_info *sip;
// Save the Current Working dir to restore in a minute - fred is being stupid
char pwd[MAX_PATH_LEN];
getcwd(pwd, MAX_PATH_LEN); // get the present working dir - probably <fs2path>[/modpath]/data/missions/
// "pop" and cfile_chdirs off the stack
chdir(Fred_base_dir);
obj = ship_create(orient, pos, ship_type);
if (obj == -1)
return -1;
// ok, done with file io, restore the pwd
chdir(pwd);
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;
z1 = Shield_sys_teams[shipp->team];
z2 = Shield_sys_types[ship_type];
if (((z1 == 1) && z2) || (z2 == 1))
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 = NULL;
// 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 == NULL || 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));
return obj;
}
int create_prop(matrix* orient, vec3d* pos, int prop_type)
{
// Save the Current Working dir to restore in a minute - fred is being stupid
char pwd[MAX_PATH_LEN];
getcwd(pwd, MAX_PATH_LEN); // get the present working dir - probably <fs2path>[/modpath]/data/missions/
// "pop" and cfile_chdirs off the stack
chdir(Fred_base_dir);
int obj = prop_create(orient, pos, prop_type);
if (obj == -1)
return -1;
// ok, done with file io, restore the pwd
chdir(pwd);
if (query_prop_name_duplicate(Objects[obj].instance))
fix_prop_name(Objects[obj].instance);
return obj;
}
int query_prop_name_duplicate(int prop)
{
const auto& target = prop_id_lookup(prop)->prop_name;
for (size_t i = 0; i < Props.size(); ++i) {
if (i == static_cast<size_t>(prop))
continue;
const auto& other = Props[i];
if (other.has_value() && other->objnum != -1) {
if (!stricmp(other->prop_name, target)) {
return 1;
}
}
}
return 0;
}
int 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 1;
return 0;
}
void copy_bits(int *dest, int src, int mask)
{
*dest &= ~mask;
*dest |= src & mask;
}
int 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;
}
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 create_object(vec3d *pos, int waypoint_instance, bool prop)
{
int obj, n;
if (prop) {
int prop_class = m_new_prop_type_combo_box.GetCurSel();
if (prop_class < 0 || prop_class >= prop_info_size())
return -1;
obj = create_prop(nullptr, pos, prop_class);
if (obj == -1)
return -1;
} else {
if (cur_ship_type_combo_index == static_cast<int>(Id_select_type_waypoint)) {
obj = create_waypoint(pos, waypoint_instance);
} else if (cur_ship_type_combo_index == static_cast<int>(Id_select_type_jump_node)) {
CJumpNode jnp(pos);
obj = jnp.GetSCPObjectNumber();
Jump_nodes.push_back(std::move(jnp));
} else { // creating a ship
int ship_class = m_new_ship_type_combo_box.GetShipClass(cur_ship_type_combo_index);
if (ship_class < 0 || ship_class >= ship_info_size())
return -1;
obj = create_ship(nullptr, pos, ship_class);
if (obj == -1)
return -1;
n = Objects[obj].instance;
Ships[n].arrival_cue = alloc_sexp("true", SEXP_ATOM, SEXP_ATOM_OPERATOR, -1, -1);
Ships[n].departure_cue = alloc_sexp("false", SEXP_ATOM, SEXP_ATOM_OPERATOR, -1, -1);
Ships[n].cargo1 = 0;
}
}
if (obj < 0)
return obj;
obj_merge_created_list();
set_modified();
Update_window = 1;
return obj;
}
int 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();
set_modified();
return obj;
}
int create_waypoint(vec3d *pos, int waypoint_instance)
{
int obj = waypoint_add(pos, waypoint_instance);
set_modified();
return obj;
}
void create_new_mission()
{
reset_mission();
*Mission_filename = 0;
FREDDoc_ptr->autosave("nothing");
Undo_count = 0;
}
void reset_mission()
{
clear_mission();
create_player(&vmd_zero_vector, &vmd_identity_matrix);
stars_post_level_init();
}
void clear_mission(bool fast_reload)
{
char *str;
int i, j, count;
CTime t;
// clean up everything we need to before we reset back to defaults.
clean_up_selections();
if (Briefing_dialog){
Briefing_dialog->reset_editor();
}
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(), 0);
for (i=0; i<MAX_SHIP_CLASSES; i++){
Shield_sys_types[i] = 0;
}
set_cur_indices(-1);
str = reg_read_string("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "RegisteredOwner", NULL);
if (!str) {
str = reg_read_string("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "RegisteredOwner", NULL);
if (!str) {
str = getenv("USERNAME");
if (!str){
str = UNKNOWN_USER;
}
}
}
time_t currentTime;
time(¤tTime);
auto timeinfo = localtime(¤tTime);
The_mission.name = "Untitled";
The_mission.author = str;
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(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 (i=0; i<MAX_TVT_TEAMS; i++) {
count = 0;
for ( j = 0; j < 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 ( j = 0; j < 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();
fred_render_init();
set_physics_controls();
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);
set_modified(FALSE);
Update_window = 1;
}
int query_valid_object(int index)
{
int obj_found = FALSE;
object *ptr;
if (index < 0 || index >= MAX_OBJECTS || Objects[index].type == OBJ_NONE)
return FALSE;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {
Assert(ptr->type != OBJ_NONE);
if (OBJ_INDEX(ptr) == index)
obj_found = TRUE;
ptr = GET_NEXT(ptr);
}
Assert(obj_found); // just to make sure it's in the list like it should be.
return TRUE;
}
int query_valid_ship(int index)
{
int obj_found = FALSE;
object *ptr;
if (index < 0 || index >= MAX_OBJECTS || Objects[index].type != OBJ_SHIP)
return FALSE;
ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list)) {