-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bl A Fileio.cpp
executable file
·5427 lines (4849 loc) · 229 KB
/
Bl A Fileio.cpp
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
// Note about windows and Mac file formats.
// Windows and Mac write short (2 byte) integers differently. One does
// high byte first, one does low byte first. In the Blades of Exile days, there
// were two scenario formats, one Mac and one Windows, and the first four bytes
// in a scenario file identified which was which.
// Of course, the game was savvy enough to tell which sort of scenario it was loading and
// adjust the value accordingly.
// But this was stupid. In BoA, it is done a much smarter way. Every file is
// written in Mac format, and only the Windows version has to flip the formats.
// Just FYI.
// messing with these caused a real stuff up!
#include "stdafx.h"
#include <cctype>
#include <commdlg.h>
#include "Global.h"
#include "CMemStream.h"
#include "Bl A Fileio.h"
#define DONE_BUTTON_ITEM 1
#define NIL 0L
#define kWARNING_BEEP 0x010000 // flag bit to beep when display warning
// Global variables
char scenario_path[_MAX_PATH] = ""; // just the path to the scenario file
char store_editor_path[_MAX_PATH + 1];
char appl_path[_MAX_PATH + 1];
// external global variables
extern HWND mainPtr;
extern scenario_data_type scenario;
extern town_record_type town;
extern big_tr_type t_d;
extern outdoor_record_type current_terrain;
extern scen_item_data_type scen_data;
extern zone_names_data_type zone_names;
// extern short borders[4][50];
// extern unsigned char border_floor[4][50];
// extern unsigned char border_height[4][50];
extern outdoor_record_type border_terrains[3][3];
extern short cur_town;
extern short town_type ;
extern location cur_out;
extern Boolean editing_town;
extern short overall_mode;
extern short max_dim[3];
extern Boolean file_is_loaded;
extern Boolean showed_graphics_error;
extern short cen_x, cen_y;
extern Boolean change_made_town, change_made_outdoors;
extern location selected_square;
// local variables
short data_dump_file_id;
char start_name[256];
short start_volume;
long start_dir;
ave_tr_type ave_t;
tiny_tr_type tiny_t;
scenario_data_type temp_scenario;
town_record_type warrior_grove_town;
ave_tr_type warrior_grove_terrain;
outdoor_record_type warrior_grove_out;
// data for Blades of Exile porting
old_blades_scenario_data_type boe_scenario;
old_blades_scen_item_data_type boe_scen_data;
old_blades_piles_of_stuff_dumping_type boe_scen_text;
old_blades_big_tr_type boe_big_town;
old_blades_ave_tr_type boe_ave_town;
old_blades_tiny_tr_type boe_tiny_town;
old_blades_town_record_type boe_town;
old_blades_outdoor_record_type boe_outdoor;
old_blades_talking_record_type boe_talk_data;
Boolean currently_editing_windows_scenario = FALSE;
// Big waste!
// char last_load_file[_MAX_PATH] = "";
char szFileName [_MAX_PATH] = ""; // full path and file name
char szFileName2 [_MAX_PATH] = "tempscen.bas";
char szTitleName [_MAX_FNAME + _MAX_EXT] = ""; // just file name
OPENFILENAME ofn;
OPENFILENAME ofn_import_boe;
OFSTRUCT store;
char *old_blades_button_strs[150] = {
"Done ","Ask"," "," ","Keep", "Cancel",
"+","-","Buy","Leave","Get","1","2","3","4","5","6","Cast"," "," "," "," "," ",
"Buy","Sell","Other Spells","Buy x10"," "," ","Save","Race","Train","Items",
"Spells","Heal Party","1","2","3","4","5","6","7","8","9","10","11","12","13",
"14","15","16","Take","Create","Delete","Race/Special","Skill","Name","Graphic",
"Bash Door","Pick Lock","Leave","Steal","Attack","OK","Yes","No","Step In"," ",
"Record","Climb","Flee","Onward","Answer","Drink","Approach","Mage Spells",
"Priest Spells","Advantages","New Game","Land","Under","Restore","Restart",
"Quit","Save First","Just Quit","Rest","Read","Pull","Alchemy","17","Push",
"Pray","Wait","","","Delete","Graphic","Create","Give","Destroy","Pay","Free",
"Next Tip","Touch", "Select Icon","Create/Edit","Clear Special","Edit Abilities",
"Choose","Go Back","Create New","General","One Shots","Affect PCs","If-Thens",
"Town Specs","Out Specs","Advanced","Weapon Abil","General Abil.","NonSpell Use",
"Spell Usable","Reagents","Missiles","Abilities","Pick Picture","Animated",
"Enter","Burn","Insert","Remove","Accept","Refuse","Open","Close","Sit","Stand",
"","","18","19","20","Invisible!","","","","","",""};
char *old_blades_node_names[256] = {
"No Special","Set Flag","Increment Flag","Display Message","Secret Passage","Display Small Message","Flip Flag","Out Block","Town Block","Combat Block",
"Looking Block","Can't Enter","Change Time","Start General Timer","Play a Sound","Change Horse Possession","Change Boat Possession","Show/Hide Town","Major Event Has Occured","Forced Give",
"Buy Items of Type","Call Global Special","Set Many Flags","Copy Flag","Ritual of Sanct. Block","Have a Rest","Wandering will fight","End Scenario","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Give Item","Give Special Item","One-Time Do Nothing","One-Time and Set","One-Time Text Message","Display Dialog (Dialog pic)","Display Dialog (Terrain pic)","Display Dialog (Monster pic)","Give Item (Dialog pic)","Give Item (Terrain pic)",
"Give Item (Monster pic)","One-Time Place Outdoor Enc.","One-Time Place Town Enc.","Trap","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Select a PC","Do Damage","Affect Health","Affect Spell Points","Affect Experience","Affect Skill Points","Kill/Raise Dead","Affect Poison","Affect Slow/Haste","Affect Invulnerability",
"Affect Magic Resistance","Affect Webs","Affect Disease","Affect Sanctuary","Affect Curse/Bless","Affect Dumbfounding","Affect Sleep","Affect Paralysis","Affect Statistic","Give Mage Spell",
"Give Priest Spell","Affect Gold","Affect Food","Affect Alchemy","Affect Stealth","Affect Firewalk","Affect Flying","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Stuff Done Flag?","Town Number?","Random Number?","Have Special Item?","Stuff Done Compare?","Terrain this type? (town)","Terrain this type? (out)","Has gold?","Has food?","Item Class on Space?",
"Have Item With Class?","Equipped Item With Class?","Has Gold? (+ take)","Has Food? (+ take)","Item Class on Space? (+ take)","Have Item W. Class? (+ take)","Equip Item W. Class? (+ take)","Day Reached?","Any Barrels?","Any Crates?",
"Special Thing Happened?","Has Cave Lore?","Has Woodsman?","Has Enough Mage Lore?","Text Response?","Stuff Done Equal?","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Town Hostile","Change Terrain","Swap Terrain","Transform Terrain","Move Party","Hit Space","Explosion on Space","Lock Space","Unlock Space","Do sfx Burst",
"Make Wandering Monster","Place a Monster","Destroy Monster","Destroy All Monsters","Generic Lever","Generic Portal","Generic Button","Generic Stairway","Lever (Dialog Display)","Portal (Dialog Display)",
"Stairway (Dialog Display)","Relocate Outdoors","Place Item","Split Party","Reunite Party","Start General Timer (Town)","Unused","Unused","Unused","Unused",
"Place Fire Wall","Place Force Wall","Place Ice Wall","Place Blade Wall","Place Stinking Cloud","Place Sleep Field","Place Quickfire","Place Fire Barrier","Place Force Barrier","Cleanse Rectangle",
"Place SFX","Place Barrels, Etc.","Move Items","Destroy Items","Change Rectange Terrain","Swap Rectangle Terrain","Transform Rectangle Terrain","Lock Rectangle","Unlock Rectangle","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Make Outdoor Wandering","Change Out Terrain","Place Outdoor Encounter","Outdoor Move Party","Outdoor Store",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused",
"Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused","Currently Unused"
};
short old_blades_available_dlog_buttons[NUM_DLOG_B] = {
0,63,64,65,1,4,5,8,128,9,10,11,12,13,14,15,16,17,29,51,60,61,62,
66,69,70,71,72,73,74,79,80,83,86,87,88,91,92,93,99,100,101,102,104,
129,130,131,132,133,134,135,136,137};
char *BOAFieldnames[22] = {
"Unknown","Blocked","Oblique Mirror","Force Barrier","Fire Barrier","Web",
"Crate","Barrel","Facing Mirror","Unknown","Unknown","Unknown","Unknown",
"Unknown","Small Blood Stain","Medium Blood Stain","Large Blood Stain",
"Small Slime Pool","Large Slime Pool","Dried Blood","Bones","Rocks"};
// function prototype
bool read_BoAFilesFolder_from_Pref( char * boaFolder );
void write_BoAFilesFolder_to_Pref( char * boaFolder );
bool check_BoAFilesFolder( char * boaFolder );
bool init_directories_with_pref( char * boaFolder );
bool init_directories_with_appl_folder( char * boaFolder );
bool init_directories_with_user_input( char * boaFolder );
bool init_directories( void );
void print_write_position ();
int make_backup_file( char * filePath );
int do_save_change_to_outdoor_size(short plus_north,short plus_west,short plus_south,short plus_east,short on_surface);
void load_outdoor( location which_out, outdoor_record_type & to_where );
void oops_error(short error);
// void extract_old_scen_text();
Boolean copy_script(char *script_source_name,char *script_dest_name,char *dest_directory);
// short str_to_num(char *str) ;
void port_boe_scenario_data();
void port_boe_out_data() ;
Boolean is_old_road(short i,short j);
Boolean is_old_wall(short ter);
void port_boe_town_data(short which_town,Boolean is_mac_scen) ;
void boe_port_talk_nodes();
void boe_port_town();
void boe_port_t_d();
void boe_port_scenario();
void boe_port_item_list();
void boe_port_out(old_blades_outdoor_record_type *out);
void boe_flip_spec_node(old_blades_special_node_type *spec);
void port_scenario_script(char *script_name,char *directory_id);
void port_a_special_node(old_blades_special_node_type *node,short node_num,FILE *file_id,short node_type);
void get_bl_str(char *str,short str_type,short str_num);
void get_all_bl_str(char *str,short str_type,short str_num);
void add_ishtrs_string_to_file(FILE *file_id,char *str1,char *str2,char *str3);
void add_short_string_to_file(FILE *file_id,char *str1,short num,char *str2);
void add_ish_string_to_file(FILE *file_id,char *str1,short num1,char *str2,short num2,char *str3);
void add_big_string_to_file(FILE *file_id,char *str1,short num1,char *str2,short num2,char *str3,short num3,char *str4);
void add_ishqns_string_to_file(FILE *file_id,char *str1,char *str2,char *str3,char *str4,char *str5);
void add_string_to_file(FILE *file_id,char *str);
void add_cr(FILE *file_id);
void add_string(FILE *file_id,char *str);
void handle_messages(FILE *file_id,short node_type,short message_1,short message_2);
void port_town_script(char *script_name,char *directory_id,short which_town);
void trunc_str(char *str);
void port_outdoor_script(char *script_name,char *directory_id,short sector_x,short sector_y);
void port_town_dialogue_script(char *script_name,char *directory_id,short which_town);
void port_dialogue_intro_text(short *current_dialogue_node,short which_slot,FILE *file_id,short town_being_ported);
void port_dialogue_node(short *current_dialogue_node,short which_slot,FILE *file_id,short which_node,short town_being_ported);
void clean_str(char *str);
// void open_current_scenario_resources();
// void close_current_scenario_resources();
void kludge_correct_old_bad_data();
short SetFPos(FILE *file, short mode, long len);
short FSWrite(FILE *file_id, long *len, char *data);
Boolean use_custom_name = 0;
// registry constant
const char* kRegistryKey = "Software\\Spiderweb Software\\BoA 3D Editor";
const char* kRegistryName = "BoA DATA Dirctory";
bool read_BoAFilesFolder_from_Pref( char * boaFolder )
{
HKEY hKey;
if (RegOpenKey(HKEY_CURRENT_USER, kRegistryKey, &hKey) != ERROR_SUCCESS)
return false;
LONG len = _MAX_PATH;
if( RegQueryValue( hKey, kRegistryName, (LPTSTR)boaFolder, &len ) != ERROR_SUCCESS)
return false;
return (len != 0);
}
void write_BoAFilesFolder_to_Pref( char * boaFolder )
{
HKEY hKey;
if (RegCreateKey(HKEY_CURRENT_USER, kRegistryKey, &hKey) != ERROR_SUCCESS)
return;
RegSetValue( hKey, kRegistryName, REG_SZ, boaFolder, (DWORD)strlen( boaFolder ) + 1 );
RegCloseKey( hKey );
}
bool check_BoAFilesFolder( char * boaFolder )
{
char path[ _MAX_PATH ];
FILE * fp;
strcpy( path, boaFolder );
strcat( path, "\\Editor Graphics\\G4900.bmp" );
if ( (fp = fopen( path, "rb" ) ) == NULL )
return false;
fclose( fp );
return true;
}
bool init_directories_with_pref( char * boaFolder )
{
if ( read_BoAFilesFolder_from_Pref( boaFolder ) )
return check_BoAFilesFolder( boaFolder );
return false;
}
bool init_directories_with_appl_folder( char * boaFolder )
{
if ( GetCurrentDirectory( _MAX_PATH, boaFolder ) != 0 )
return check_BoAFilesFolder( boaFolder );
return false;
}
bool init_directories_with_user_input( char * boaFolder )
{
char anAppPath[ _MAX_PATH ];
OPENFILENAME anOfn;
*anAppPath = '\0';
ZeroMemory( &anOfn, sizeof(anOfn) );
anOfn.lStructSize = sizeof(OPENFILENAME);
anOfn.lpstrFilter = "Application (*.EXE)\0" "*.EXE\0";
anOfn.lpstrFile = anAppPath;
anOfn.nMaxFile = _MAX_PATH;
anOfn.lpstrInitialDir = "C:\\Program Files\\Blades of Avernum";
anOfn.lpstrTitle = "Select \"Blades of Avernum.exe\" application under \"Blades of Avernum\" folder";
anOfn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_NONETWORKBUTTON;
if ( GetOpenFileName( &anOfn ) == 0 ) //according to MSDN function returns 0, not NULL if user doesn't click OK
return false;
char anDrv[ 8 ];
char anDir[_MAX_PATH];
char anFile[_MAX_FNAME];
char anExt[_MAX_EXT];
_splitpath( anAppPath, anDrv, anDir, anFile, anExt );
_makepath( boaFolder, anDrv, anDir, "Data", NULL );
return check_BoAFilesFolder( boaFolder );
}
bool init_directories( void )
{
bool update_pref = false;
char boaFolder[ _MAX_PATH ];
if ( GetCurrentDirectory( _MAX_PATH, appl_path ) == 0 )
return false;
if ( !init_directories_with_pref( boaFolder ) ) { // first, check preference
update_pref = true;
if ( !init_directories_with_appl_folder( boaFolder ) ) // next, check application folder
if ( !init_directories_with_user_input( boaFolder ) ) // then, ask to user
return false;
}
if ( update_pref )
write_BoAFilesFolder_to_Pref( boaFolder );
strcpy( store_editor_path, boaFolder );
return true;
}
bool file_initialize()
{
// file extention filter - beware string concatenation
const char *szFilter = "Blades of Avernum Scenarios (*.BAS)\0" "*.bas\0"
"All Files (*.*)\0" "*.*\0"
"";
const char *szFilter2 = "Blades of Exile Scenarios (*.EXS)\0" "*.exs\0"
"All Files (*.*)\0" "*.*\0"
"";
if ( !init_directories() )
return false;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = mainPtr;
ofn.hInstance = NULL;
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = _MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = "txt";
ofn.lCustData = 0L;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
ofn_import_boe = ofn;
ofn_import_boe.lpstrFilter = szFilter2;
return true;
}
// Here we go. this is going to hurt.
// Note no save as is available for scenarios.
//At this point, szFileName MUST contain the path and filename for the currently edited scen.
// Strategy ... assemble a big Dummy file containing the whole scenario
//chunk by chunk, copy the dummy over the original, and delete the dummy
// the whole scenario is too big be be shifted around at once
void save_campaign()
{
short i,j,k,num_outdoors;
FILE *dummy_f,*scen_f;
char *buffer = NULL;
DWORD buf_len = 100000;
short error;
short out_num;
long len,scen_ptr_move = 0,save_town_size = 0,save_out_size = 0;
outdoor_record_type *dummy_out_ptr;
HGLOBAL temp_buffer;
// before saving, do all the final processing that needs to be done (liek readjusting lights)
set_up_lights();
//OK. FIrst find out what file name we're working with, and make the dummy file
// which we'll build the new scenario in
// first, create the dummy file name and then aler it slightly so it different from original
strcpy(szFileName2,szFileName);
if (szFileName2[strlen(szFileName2) - 1] == 's') // this shoult be true, should end in .bas
szFileName2[strlen(szFileName2) - 1] = 't';
else szFileName2[strlen(szFileName2) - 1] = 's';
if (NULL == (dummy_f = fopen(szFileName2, "wb"))) {
oops_error(11);
return;
}
if (NULL == (scen_f = fopen(szFileName, "rb"))) {
oops_error(12);
return;
}
// Now we need to set up a buffer for moving the data over to the dummy
temp_buffer = GlobalAlloc(GMEM_FIXED,buf_len);
if (temp_buffer == NULL) {
FSClose(scen_f); FSClose(dummy_f); oops_error(14);
return;
}
buffer = (char *) GlobalLock(temp_buffer);
if (buffer == NULL) {
FSClose(scen_f); FSClose(dummy_f); oops_error(14);
return;
}
scenario.prog_make_ver[0] = 2;
scenario.prog_make_ver[1] = 0;
scenario.prog_make_ver[2] = 0;
// Now, the pointer in scen_f needs to move along, so that the correct towns are sucked in.
// To do so, we'll remember the size of the saved town and out now.
// this is much simple thabn it was in Blades of Exile, since chunks have a constant length now
out_num = cur_out.y * scenario.out_width + cur_out.x;
save_out_size = (long) (sizeof (outdoor_record_type));
save_town_size = (long) (sizeof (town_record_type));
if (scenario.town_size[cur_town] == 0)
save_town_size += (long) (sizeof (big_tr_type));
else if (scenario.town_size[cur_town] == 1)
save_town_size += (long) (sizeof (ave_tr_type));
else save_town_size += (long) (sizeof (tiny_tr_type));
scen_ptr_move = sizeof(scenario_data_type);
scenario.last_town_edited = cur_town;
scenario.last_out_edited = cur_out;
// now, if editing windows scenario, we need to write it in a windows friendly
// way.
if (currently_editing_windows_scenario == FALSE)
scenario.port();
len = sizeof(scenario_data_type); // scenario data
if ((error = FSWrite(dummy_f, &len, (char *) &scenario)) != 0) {
EdSysBeep( /* 2 */ ); FSClose(scen_f); FSClose(dummy_f);oops_error(62);
return;
}
if (currently_editing_windows_scenario == FALSE)
scenario.port();
SetFPos(scen_f,1,scen_ptr_move);
// OK ... scenario written. Now outdoors.
num_outdoors = scenario.out_width * scenario.out_height;
for (i = 0; i < num_outdoors; i++)
if (i == out_num) {
if (currently_editing_windows_scenario == FALSE)
current_terrain.port();
len = sizeof(outdoor_record_type);
error = FSWrite(dummy_f, &len, (char *) ¤t_terrain);
if (currently_editing_windows_scenario == FALSE)
current_terrain.port();
if (error != 0) {FSClose(scen_f); FSClose(dummy_f);oops_error(63);}
// Now skip ahead scen_f position to match
// Kludge ... should be using SetFPos, but for some reason it now working
//SetFPos(scen_f,3,sizeof (outdoor_record_type));
len = (long) (sizeof (outdoor_record_type));
error = FSRead(scen_f, &len, buffer);
}
else {
len = (long) (sizeof (outdoor_record_type));
error = FSRead(scen_f, &len, buffer);
dummy_out_ptr = (outdoor_record_type *) buffer;
if (error != 0) {FSClose(scen_f); FSClose(dummy_f);oops_error(64);}
if ((error = FSWrite(dummy_f, &len, buffer)) != 0) {
EdSysBeep(/* 2 */); FSClose(scen_f); FSClose(dummy_f);oops_error(65);
return;
}
}
// now, finally, write towns.
for (k = 0; k < scenario.num_towns; k++)
if (k == cur_town) {
// write towns
if (currently_editing_windows_scenario == FALSE)
town.port();
len = sizeof(town_record_type);
error = FSWrite(dummy_f, &len, (char *) &town);
if (currently_editing_windows_scenario == FALSE)
town.port();
if (error != 0) {FSClose(scen_f); FSClose(dummy_f);oops_error(66);}
if (currently_editing_windows_scenario == FALSE)
t_d.port();
switch (scenario.town_size[cur_town]) {
case 0:
len = sizeof(big_tr_type);
FSWrite(dummy_f, &len, (char *) &t_d);
break;
case 1:
for (i = 0; i < 48; i++)
for (j = 0; j < 48; j++) {
ave_t.terrain[i][j] = t_d.terrain[i][j];
ave_t.floor[i][j] = t_d.floor[i][j];
ave_t.height[i][j] = t_d.height[i][j];
ave_t.lighting[i][j] = t_d.lighting[i][j];
}
len = sizeof(ave_tr_type);
FSWrite(dummy_f, &len, (char *) &ave_t);
break;
case 2:
for (i = 0; i < 32; i++)
for (j = 0; j < 32; j++) {
tiny_t.terrain[i][j] = t_d.terrain[i][j];
tiny_t.floor[i][j] = t_d.floor[i][j];
tiny_t.height[i][j] = t_d.height[i][j];
tiny_t.lighting[i][j] = t_d.lighting[i][j];
}
len = sizeof(tiny_tr_type);
FSWrite(dummy_f, &len, (char *) &tiny_t);
break;
}
if (currently_editing_windows_scenario == FALSE)
t_d.port();
// Now skip ahead scen_f position to match
//SetFPos(scen_f,3,save_town_size);
len = (long) (sizeof(town_record_type));
error = FSRead(scen_f, &len, buffer);
switch (scenario.town_size[k]) {
case 0: len = (long) ( sizeof(big_tr_type)); break;
case 1: len = (long) ( sizeof(ave_tr_type)); break;
case 2: len = (long) ( sizeof(tiny_tr_type)); break;
}
error = FSRead(scen_f, &len, buffer);
}
else { /// load unedited town into buffer and save, doing translataions when necessary
len = (long) (sizeof(town_record_type));
switch (scenario.town_size[k]) {
case 0: len += (long) ( sizeof(big_tr_type)); break;
case 1: len += (long) ( sizeof(ave_tr_type)); break;
case 2: len += (long) ( sizeof(tiny_tr_type)); break;
}
//for (long count = 0; count < len; count++) {
// int next_char = fgetc(scen_f);
// if (count < 100)
// dbug_first_chars[count] = (char) next_char;
// fputc(next_char,dummy_f);
// }
error = FSRead(scen_f, &len, buffer);
if (error != 0) {
FSClose(scen_f);
FSClose(dummy_f);
oops_error(67);
}
if ((error = FSWrite(dummy_f, &len, buffer)) != 0) {FSClose(scen_f); FSClose(dummy_f);oops_error(68);return;}
}
change_made_town = change_made_outdoors = FALSE;
// now, everything is moved over. Delete the original, and rename the dummy
// first close everything up
error = FSClose(scen_f);
if (error != 0) {FSClose(scen_f); FSClose(dummy_f);oops_error(71);}
error = FSClose(dummy_f);
if (error != 0) {FSClose(scen_f); FSClose(dummy_f);oops_error(72);}
// delete original
if (OpenFile(szFileName,&store,OF_DELETE) == HFILE_ERROR) {
if (remove(szFileName) != 0) {
GlobalUnlock(temp_buffer);
GlobalFree(temp_buffer);
oops_error(101);
give_error("File name that could not be deleted:",szFileName,0);
return;
}
}
// rename copy to be original
rename(szFileName2,szFileName);
GlobalUnlock(temp_buffer);
GlobalFree(temp_buffer);
}
int make_backup_file( char * filePath )
{
char backupFile[_MAX_PATH];
strcpy( backupFile, filePath );
strcat( backupFile, ".bak" );
// if the another backup file exists, delete it
FILE* fp;
if ( (fp = fopen( backupFile, "rb" )) != NULL ){
fclose( fp );
if ( remove( backupFile ) != 0 ) {
give_error("File name that could not be deleted:",backupFile,0);
return -1;
}
}
// rename the original
if ( rename( filePath, backupFile ) != 0 ) {
give_error("File name that could not be renamed:", filePath,0);
return -1;
}
return 0;
}
#define SetLocation( loc, x0, y0 ) loc.x = x0; loc.y = y0
// q_3DModStart
//assumes scen has already been saved
void save_change_to_outdoor_size(short plus_north,short plus_west,short plus_south,short plus_east,short on_surface)
{
int err = do_save_change_to_outdoor_size( plus_north, plus_west, plus_south, plus_east, on_surface );
if (err != 0) {
if ( kWARNING_BEEP & err )
EdSysBeep( /* 2 */ );
oops_error( (short)err );
}
}
int do_save_change_to_outdoor_size(short plus_north,short plus_west,short plus_south,short plus_east,short on_surface)
{
int i;
bool needPort = (currently_editing_windows_scenario == FALSE);
RECT oldRECT; // the previous outdoor area, in the coordinate after conversion
RECT newRECT; // the new outdoor area, actually, (0,0)-(out_width,out_hight)
RECT sumRECT; // minimum RECT that includes both oldRect and newRect
SetRECT( newRECT,
0, 0,
scenario.out_width + plus_west + plus_east,
scenario.out_height + plus_north + plus_south );
SetRECT( oldRECT,
plus_west, plus_north,
scenario.out_width + plus_west,
scenario.out_height + plus_north );
SetRECT( sumRECT,
(plus_west > 0) ? newRECT.left : oldRECT.left,
(plus_north > 0) ? newRECT.top : oldRECT.top,
(plus_east > 0) ? newRECT.right : oldRECT.right,
(plus_south > 0) ? newRECT.bottom : oldRECT.bottom );
scenario.out_width = (short)newRECT.right;
scenario.out_height = (short)newRECT.bottom;
//keep the "same" sector selected, if it still exists
OffsetLocation( cur_out, plus_west, plus_north );
if( !LocationInRECT( cur_out, newRECT ) ) {
SetLocation( cur_out, 0, 0 );
if(editing_town == FALSE)
cen_x = cen_y = 24;
}
//and the same starting place
OffsetLocation( scenario.what_outdoor_section_start_in, plus_west, plus_north );
if( !LocationInRECT( scenario.what_outdoor_section_start_in, newRECT ) ) {
SetLocation( scenario.what_outdoor_section_start_in, 0, 0 );
SetLocation( scenario.start_where_in_outdoor_section, 24, 24 );
}
scenario.prog_make_ver[0] = 2;
scenario.prog_make_ver[1] = 0;
scenario.prog_make_ver[2] = 0;
scenario.last_town_edited = cur_town;
scenario.last_out_edited = cur_out;
// first, create the in/out stream on memory and read in the original file on the buffer
CMemStream in_f;
CMemStream out_f;
if ( out_f.SetLength() ) return 60;
if ( in_f.ReadFromFile( szFileName ) ) return 61; // 62
// write scenario data to stream and skip it on the original
if ( needPort ) scenario.port();
out_f.PutBytes( kSizeOfScenario_data_type, &scenario ); // return (kWARNING_BEEP | 62);
if ( needPort ) scenario.port();
in_f.SetMarker( kSizeOfScenario_data_type, CMemStream::eMsMarker );
// OK ... scenario written. Now outdoors.
outdoor_record_type empty_terrain;
empty_terrain.SetSurface( on_surface );
// check through sumRECT: this algorithm should be most comprehensive, though it isn't most efficient.
char * outPtr;
POINT here;
for ( here.y = sumRECT.top; here.y < sumRECT.bottom; here.y++ ) {
for ( here.x = sumRECT.left; here.x < sumRECT.right; here.x++ ) {
// First, determine what to be written if needed
outPtr = (char *)&empty_terrain; // as the default, select the empty data
if ( POINTInRECT( here, oldRECT ) ) { // if here is within the old area,
outPtr = in_f.GetCurrentPtr(); // select the area on the original file
in_f.SetMarker( kSizeOfOutdoor_record_type, CMemStream::eMsMarker ); // and move the read marker
}
if ( (here.x == cur_out.x) && (here.y == cur_out.y) ) // if here is the outdoor currently editing,
outPtr = (char *)¤t_terrain; // select the current outdoor data
// Next, determine to write or not.
if ( POINTInRECT( here, newRECT ) ) // if here is within the new area,
out_f.PutBytes( kSizeOfOutdoor_record_type, outPtr ); // output selected data to the streem
} // otherwise nothing to do
}
// now, finally, write towns.
ave_tr_type* anAve_town_rec;
tiny_tr_type* aTny_town_rec;
for (i = 0; i < scenario.num_towns; i++) {
// if the index matches to the current town,
int anTownSize = scenario.town_size[cur_town];
if (i == cur_town) {
// write current town and read off the original
if ( needPort ) town.port();
out_f.PutBytes( kSizeOfTown_record_type, &town ); // return 68; //66, 67
if ( needPort ) town.port();
in_f.SetMarker( kSizeOfTown_record_type, CMemStream::eMsMarker );
// resize map to the appropriate size by copying it
// write town map and read off the original
switch (anTownSize) {
case 0:
if ( needPort ) t_d.port();
out_f.PutBytes( kSizeOfBig_tr_type, &t_d );
if ( needPort ) t_d.port();
in_f.SetMarker( kSizeOfBig_tr_type, CMemStream::eMsMarker );
break;
case 1:
anAve_town_rec = new ave_tr_type( t_d );
if ( needPort ) anAve_town_rec->port();
out_f.PutBytes( kSizeOfAve_tr_type, anAve_town_rec );
delete anAve_town_rec;
in_f.SetMarker( kSizeOfAve_tr_type, CMemStream::eMsMarker );
break;
case 2:
aTny_town_rec = new tiny_tr_type( t_d );
if ( needPort ) aTny_town_rec->port();
out_f.PutBytes( kSizeOfTiny_tr_type, aTny_town_rec );
delete aTny_town_rec;
in_f.SetMarker( kSizeOfTiny_tr_type, CMemStream::eMsMarker );
break;
default:
break;
}
} else { // transfer unedited town
if ( in_f.TransferBytes( (kSizeOfTown_record_type + max_dim[ anTownSize ]), &out_f ) != 0)
return 68; //66, 67
}
}
// rename the original to the backup and save new file
if ( make_backup_file( szFileName ) != 0 )
return 73;
if ( out_f.WriteToFile( szFileName ) != 0 )
return 73; // 71, 72, need other error dialog ?
//need to reload current outdoor sector, including bordering sectors
location spot_hit = {cur_out.x,cur_out.y};
//load_outdoors(spot_hit,0);
load_outdoor_and_borders(spot_hit);
set_up_terrain_buttons();
load_all_outdoor_names(NULL);
return 0;
}
// Loads a given campaign. Loads the last zone edited into memory for immediate editing.
void load_campaign()
{
short i;
FILE *file_id;
short error;
long len;
ofn.hwndOwner = mainPtr;
ofn.lpstrFile = szFileName; // full path and file name
ofn.lpstrFileTitle = szTitleName; // just file name
ofn.Flags = 0;
if (GetOpenFileName(&ofn) == 0) {
CommDlgExtendedError(); // for debug
return;
}
if (NULL == (file_id = fopen(szFileName, "rb"))) {
oops_error(28);
SysBeep(/* 2 */);
return;
}
// generate scenario_path, which means chopping the file name
// out of szFileName
strcpy(scenario_path,szFileName);
for (i = 0; i < _MAX_PATH - 1; i++)
if (same_string((char *) (szFileName + i),szTitleName)) {
scenario_path[i] = 0;
break;
}
len = (long) sizeof(scenario_data_type);
if ((error = FSRead(file_id, &len, (char *) &scenario)) != 0){
FSClose(file_id); oops_error(75); return;
}
if (scenario.scenario_platform() < 0) {
give_error("This file is not a legitimate Blades of Avernum scenario.","",0);
file_is_loaded = FALSE;
return;
}
currently_editing_windows_scenario = (Boolean)scenario.scenario_platform();
FSClose(file_id);
if (currently_editing_windows_scenario == FALSE)
scenario.port();
refresh_graphics_library();
overall_mode = 0;
change_made_town = change_made_outdoors = FALSE;
load_town(scenario.last_town_edited);
//load_town(0);
// load_outdoors(scenario.last_out_edited,0);
load_outdoor_and_borders(scenario.last_out_edited);
load_all_outdoor_names(NULL);
load_all_town_names(NULL);
file_is_loaded = TRUE;
clear_selected_copied_objects();
//load_spec_graphics();
// load in all the scenario data
// First, initialize all scen data
scen_data.clear_scen_item_data_type();
if (load_core_scenario_data() == FALSE) {
file_is_loaded = FALSE;
return;
}
char scen_file_name [_MAX_FNAME + _MAX_EXT] = ""; // just file name
get_name_of_current_scenario(scen_file_name);
if (load_individual_scenario_data(scen_file_name) == FALSE) {
file_is_loaded = FALSE;
return;
}
set_up_terrain_buttons();
for (short i = 0; i < 30; i++) {
if ((scenario.scen_boats[i].boat_loc.x == 0) && (scenario.scen_boats[i].boat_loc.y == 0) && (scenario.scen_boats[i].which_town == 0))
scenario.scen_boats[i].which_town = -1;
}
for (short i = 0; i < 30; i++) {
if ((scenario.scen_horses[i].horse_loc.x == 0) && (scenario.scen_horses[i].horse_loc.y == 0) && (scenario.scen_horses[i].which_town == 0))
scenario.scen_horses[i].which_town = -1;
}
}
// returns the name fo the currently edited scenario, with the extension stripped off
void get_name_of_current_scenario(char *name)
{
char file_name[256];
strcpy(file_name,szTitleName);
for (short i = 0; i < 252; i++)
if ((file_name[i] == '.') && (file_name[i + 1] == 'b')
&& (file_name[i + 2] == 'a') && (file_name[i + 3] == 's'))
file_name[i] = 0;
strcpy(name,(char *) file_name);
}
void load_outdoor_and_borders(location which_out)
{
short i,j;
location temp_current_out;
if (overall_mode == 61)
return;
for(j = -1; j <= 1; j++) {
for(i = -1; i <= 1; i++) {
temp_current_out.x = which_out.x + (char)i;
temp_current_out.y = which_out.y + (char)j;
if(temp_current_out.x < 0 || temp_current_out.y < 0 ||
temp_current_out.x >= scenario.out_width || temp_current_out.y >= scenario.out_height)
continue;
load_outdoor( temp_current_out, border_terrains[i + 1][j + 1] );
}
}
current_terrain = border_terrains[1][1];
cur_out = which_out;
showed_graphics_error = FALSE;
kludge_correct_old_bad_data();
}
void load_outdoor( location which_out, outdoor_record_type & to_where )
{
FILE *file_id;
long len,len_to_jump;
short out_sec_num;
short error;
if (overall_mode == 61)
return;
if (NULL == (file_id = fopen(szFileName, "rb"))) {
oops_error(28);
SysBeep(/* 2 */);
return;
}
out_sec_num = scenario.out_width * which_out.y + which_out.x;
len_to_jump = sizeof(scenario_data_type)
+ (long) (out_sec_num) * (long) (sizeof(outdoor_record_type));
error = SetFPos (file_id, 1, len_to_jump);
if (error != 0) {FSClose(file_id);oops_error(77);return;}
len = sizeof(outdoor_record_type);
error = FSRead(file_id, &len, (char *) &to_where);
if (error != 0) {FSClose(file_id);oops_error(78);return;}
if (currently_editing_windows_scenario == FALSE)
to_where.port();
error = FSClose(file_id);
if (error != 0) {FSClose(file_id);oops_error(79);return;}
}
void load_all_outdoor_names(const char* to_open)
{
short x,y;
FILE* file_id;
long len=sizeof(outdoor_record_type),len_to_jump;
short out_sec_num=0;
outdoor_record_type store_out;
short error;
len_to_jump = sizeof(scenario_data_type);
if(to_open==NULL){
if ((file_id = fopen(szFileName, "rb")) == NULL) {
oops_error(76); return;
}
zone_names.out_width=scenario.out_width;
zone_names.out_height=scenario.out_height;
}
else{
if ((file_id = fopen(to_open, "rb")) == NULL) {
oops_error(76); return;
}
scenario_data_type temp_scen;
if ((error = FSRead(file_id, &len_to_jump, (char *) &temp_scen)) != 0){
FSClose(file_id); oops_error(75); return;
}
if (temp_scen.scenario_platform() < 0) {
give_error("This file is not a legitimate Blades of Avernum scenario.","",0);
return;
}
if (!temp_scen.scenario_platform()) //is this correct?
temp_scen.port();
zone_names.out_width=temp_scen.out_width;
zone_names.out_height=temp_scen.out_height;
}
for(y=0; y<zone_names.out_height; y++){
for(x=0; x<zone_names.out_width; x++){
error = SetFPos (file_id, 1, len_to_jump);
if (error != 0) {FSClose(file_id);oops_error(77);return;}
out_sec_num = zone_names.out_width * y + x;
error = FSRead(file_id, &len, (char *) &store_out);
if (error != 0) {FSClose(file_id);oops_error(78);return;}
strcpy(&zone_names.section_names[out_sec_num][0],&store_out.name[0]);
len_to_jump+=sizeof(outdoor_record_type);;
}
}
error = FSClose(file_id);
if (error != 0) {FSClose(file_id);oops_error(79);return;}
}
void load_town(short which_town)
{
short i,j;
FILE *file_id;
long len,len_to_jump = 0,store;
short error;
if (NULL == (file_id = fopen(szFileName, "rb"))) {
oops_error(28);
SysBeep(/* 2 */);