forked from exult/exult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemap.cc
1829 lines (1689 loc) · 52.2 KB
/
gamemap.cc
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
/**
** Gamemap.cc - X-windows Ultima7 map browser.
**/
/*
*
* Copyright (C) 1998-1999 Jeffrey S. Freedman
* Copyright (C) 2000-2013 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <cstdio>
#include "gamemap.h"
#include "objs.h"
#include "chunks.h"
#include "mappatch.h"
#include "fnames.h"
#include "utils.h"
#include "shapeinf.h"
#include "objiter.h"
#include "Flex.h"
#include "exceptions.h"
#include "animate.h"
#include "barge.h"
#include "spellbook.h"
#include "virstone.h"
#include "egg.h"
#include "jawbone.h"
#include "actors.h" /* For Dead_body, which should be moved. */
#include "ucsched.h"
#include "gamewin.h" /* With some work, could get rid of this. */
#include "game.h"
#include "effects.h"
#include "objiter.cc" /* Yes we #include the .cc here on purpose! Please don't "fix" this */
#include "databuf.h"
#include "weaponinf.h"
#include <fstream>
#include <sstream>
#ifndef UNDER_EMBEDDED_CE
using std::cerr;
using std::cout;
using std::endl;
using std::istream;
using std::ifstream;
using std::istringstream;
using std::ios;
using std::memcpy;
using std::memset;
using std::ofstream;
using std::ostringstream;
using std::rand;
using std::strcmp;
using std::strcpy;
using std::string;
using std::strlen;
using std::srand;
using std::vector;
using std::pair;
#endif
vector<Chunk_terrain *> *Game_map::chunk_terrains = 0;
std::ifstream *Game_map::chunks = 0;
bool Game_map::v2_chunks = false;
bool Game_map::read_all_terrain = false;
bool Game_map::chunk_terrains_modified = false;
const int V2_CHUNK_HDR_SIZE = 4 + 4 + 2; // 0xffff, "exlt", vers.
static char v2hdr[] = {static_cast<char>(0xff), static_cast<char>(0xff),
static_cast<char>(0xff), static_cast<char>(0xff),
'e', 'x', 'l', 't', 0, 0
};
/*
* Create a chunk.
*/
Map_chunk *Game_map::create_chunk(
int cx, int cy
) {
return (objects[cx][cy] = new Map_chunk(this, cx, cy));
}
/*
* Read in a terrain chunk.
*/
Chunk_terrain *Game_map::read_terrain(
int chunk_num // Want this one from u7chunks.
) {
const int ntiles = c_tiles_per_chunk * c_tiles_per_chunk;
assert(chunk_num >= 0 && static_cast<unsigned>(chunk_num) < chunk_terrains->size());
unsigned char buf[ntiles * 3];
if (v2_chunks) {
chunks->seekg(V2_CHUNK_HDR_SIZE + chunk_num * ntiles * 3);
chunks->read(reinterpret_cast<char *>(buf), ntiles * 3);
} else {
chunks->seekg(chunk_num * ntiles * 2);
chunks->read(reinterpret_cast<char *>(buf), ntiles * 2);
}
Chunk_terrain *ter = new Chunk_terrain(&buf[0], v2_chunks);
if (static_cast<unsigned>(chunk_num) >= chunk_terrains->size())
chunk_terrains->resize(chunk_num + 1);
(*chunk_terrains)[chunk_num] = ter;
return ter;
}
/*
* Create game window.
*/
Game_map::Game_map(
int n
) :
num(n), didinit(false),
map_modified(false), caching_out(0),
map_patches(new Map_patch_collection) {
}
/*
* Deleting map.
*/
Game_map::~Game_map(
) {
clear(); // Delete all objects, chunks.
delete chunks;
delete map_patches;
}
/*
* Init. the static data.
*/
void Game_map::init_chunks(
) {
delete chunks;
chunks = new ifstream;
int num_chunk_terrains;
bool patch_exists = is_system_path_defined("<PATCH>");
if (patch_exists && U7exists(PATCH_U7CHUNKS))
U7open(*chunks, PATCH_U7CHUNKS);
else try {
U7open(*chunks, U7CHUNKS);
} catch (const file_exception &f) {
if (!Game::is_editing() || // Ok if map-editing.
!patch_exists) // But only if patch exists.
throw;
ofstream ochunks; // Create one in 'patch'.
U7open(ochunks, PATCH_U7CHUNKS);
unsigned char buf[16 * 16 * 3];
ochunks.write(v2hdr, sizeof(v2hdr));
memset(&buf[0], 0, sizeof(buf));
ochunks.write(reinterpret_cast<char *>(buf), sizeof(buf));
ochunks.close();
U7open(*chunks, PATCH_U7CHUNKS);
}
char v2buf[V2_CHUNK_HDR_SIZE]; // Check for V2.
chunks->read(v2buf, sizeof(v2buf));
int hdrsize = 0, chunksz = c_tiles_per_chunk * c_tiles_per_chunk * 2;
if (memcmp(v2hdr, v2buf, sizeof(v2buf)) == 0) {
v2_chunks = true;
hdrsize = V2_CHUNK_HDR_SIZE;
chunksz = c_tiles_per_chunk * c_tiles_per_chunk * 3;
}
// Get to end so we can get length.
chunks->seekg(0, ios::end);
// 2 bytes/tile.
num_chunk_terrains = (static_cast<int>(chunks->tellg()) - hdrsize) / chunksz;
if (!chunk_terrains)
chunk_terrains = new vector<Chunk_terrain *>();
// Resize list to hold all.
chunk_terrains->resize(num_chunk_terrains);
read_all_terrain = false;
}
/*
* Initialize for new/restored game.
*/
void Game_map::init(
) {
char fname[128];
if (num == 0)
init_chunks();
map_modified = false;
std::ifstream u7map; // Read in map.
bool nomap = false;
if (is_system_path_defined("<PATCH>") &&
U7exists(get_mapped_name(PATCH_U7MAP, fname)))
U7open(u7map, fname);
else try {
U7open(u7map, get_mapped_name(U7MAP, fname));
} catch (const file_exception & /*f*/) {
if (!Game::is_editing()) // Ok if map-editing.
cerr << "Map file '" << fname << "' not found." <<
endl;
nomap = true;
}
for (int schunk = 0; schunk < c_num_schunks * c_num_schunks; schunk++) {
// Read in the chunk #'s.
unsigned char buf[16 * 16 * 2];
if (nomap)
memset(&buf[0], 0, sizeof(buf));
else
u7map.read(reinterpret_cast<char *>(buf), sizeof(buf));
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
uint8 *mapdata = buf;
// Go through chunks.
for (int cy = 0; cy < 16; cy++)
for (int cx = 0; cx < 16; cx++)
terrain_map[scx + cx][scy + cy] = Read2(mapdata);
}
u7map.close();
// Clear object lists, flags.
memset(objects, 0, sizeof(objects));
memset(schunk_read, 0, sizeof(schunk_read));
memset(schunk_modified, 0, sizeof(schunk_modified));
memset(schunk_cache, 0, sizeof(schunk_cache));
memset(schunk_cache_sizes, -1, sizeof(schunk_cache_sizes));
didinit = true;
}
/*
* Clear the static data.
*/
void Game_map::clear_chunks(
) {
if (chunk_terrains) {
int cnt = chunk_terrains->size();
for (int i = 0; i < cnt; i++)
delete(*chunk_terrains)[i];
delete chunk_terrains;
chunk_terrains = 0;
}
delete chunks; // Close 'u7chunks'.
chunks = 0;
read_all_terrain = false;
}
/*
* Clear out world's contents. Should be used during a 'restore'.
*/
void Game_map::clear(
) {
if (num == 0)
clear_chunks();
if (didinit) {
// Delete all chunks (& their objs).
for (int y = 0; y < c_num_chunks; y++)
for (int x = 0; x < c_num_chunks; x++) {
delete objects[x][y];
objects[x][y] = 0;
}
for (int i = 0; i < 144; i++) delete [] schunk_cache[i];
} else
memset(objects, 0, sizeof(objects));
didinit = false;
map_modified = false;
// Clear 'read' flags.
memset(schunk_read, 0, sizeof(schunk_read));
memset(schunk_modified, 0, sizeof(schunk_modified));
memset(schunk_cache, 0, sizeof(schunk_cache));
memset(schunk_cache_sizes, -1, sizeof(schunk_cache_sizes));
}
/*
* Read in superchunk data to cover the screen.
*/
void Game_map::read_map_data(
) {
Game_window *gwin = Game_window::get_instance();
int scrolltx = gwin->get_scrolltx(), scrollty = gwin->get_scrollty();
int w = gwin->get_width(), h = gwin->get_height();
// Start one tile to left.
int firstsx = (scrolltx - 1) / c_tiles_per_schunk,
firstsy = (scrollty - 1) / c_tiles_per_schunk;
// End 8 tiles to right.
int lastsx = (scrolltx + (w + c_tilesize - 2) / c_tilesize +
c_tiles_per_chunk / 2) / c_tiles_per_schunk;
int lastsy = (scrollty + (h + c_tilesize - 2) / c_tilesize +
c_tiles_per_chunk / 2) / c_tiles_per_schunk;
// Watch for wrapping.
int stopsx = (lastsx + 1) % c_num_schunks,
stopsy = (lastsy + 1) % c_num_schunks;
// Read in "map", "ifix" objects for
// all visible superchunks.
for (int sy = firstsy; sy != stopsy; sy = (sy + 1) % c_num_schunks)
for (int sx = firstsx; sx != stopsx;
sx = (sx + 1) % c_num_schunks) {
// Figure superchunk #.
int schunk = 12 * sy + sx;
// Read it if necessary.
if (!schunk_read[schunk])
get_superchunk_objects(schunk);
}
}
/*
* Get the map objects and scenery for a superchunk.
*/
void Game_map::get_map_objects(
int schunk // Superchunk # (0-143).
) {
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
// Go through chunks.
for (int cy = 0; cy < 16; cy++)
for (int cx = 0; cx < 16; cx++)
get_chunk_objects(scx + cx, scy + cy);
}
/*
* Read in terrain graphics data into window's image. (May also be
* called during map-editing if the chunknum changes.)
*/
void Game_map::get_chunk_objects(
int cx, int cy // Chunk index within map.
) {
// Get list we'll store into.
Map_chunk *chunk = get_chunk(cx, cy);
int chunk_num = terrain_map[cx][cy];
Chunk_terrain *ter = get_terrain(chunk_num);
chunk->set_terrain(ter);
}
/*
* Read in all terrain chunks (for editing).
*/
void Game_map::get_all_terrain(
) {
if (read_all_terrain)
return; // Already done.
int num_chunk_terrains = chunk_terrains->size();
for (int i = 0; i < num_chunk_terrains; i++)
if (!(*chunk_terrains)[i])
read_terrain(i);
read_all_terrain = true;
}
/*
* Set a chunk to a new terrain (during map-editing).
*/
void Game_map::set_chunk_terrain(
int cx, int cy, // Coords. of chunk to change.
int chunknum // New chunk #.
) {
terrain_map[cx][cy] = chunknum; // Set map.
get_chunk_objects(cx, cy); // Set chunk to it.
map_modified = true;
}
/*
* Build a file name with the map directory before it; ie,
* get_mapped_name("<GAMEDAT>/ireg, 3, to) will store
* "<GAMEDAT>/map03/ireg".
*/
char *Game_map::get_mapped_name(
const char *from,
char *to
) {
return Get_mapped_name(from, num, to);
}
/*
* Get the name of an ireg or ifix file.
*
* Output: ->fname, where name is stored.
*/
char *Game_map::get_schunk_file_name(
const char *prefix, // "ireg" or "ifix".
int schunk, // Superchunk # (0-143).
char *fname // Name is stored here.
) {
get_mapped_name(prefix, fname);
int len = strlen(fname);
fname[len] = '0' + schunk / 16;
int lb = schunk % 16;
fname[len + 1] = lb < 10 ? ('0' + lb) : ('a' + (lb - 10));
fname[len + 2] = 0;
return (fname);
}
/*
* Have shapes been added?
*/
static bool New_shapes() {
int u7nshapes = GAME_SI ? 1036 : 1024;
int nshapes =
Shape_manager::get_instance()->get_shapes().get_num_shapes();
return nshapes > u7nshapes;
}
/*
* Write out the chunks descriptions.
*/
void Game_map::write_chunk_terrains(
) {
const int ntiles = c_tiles_per_chunk * c_tiles_per_chunk;
int cnt = chunk_terrains->size();
int i; // Any terrains modified?
for (i = 0; i < cnt; i++)
if ((*chunk_terrains)[i] &&
(*chunk_terrains)[i]->is_modified())
break;
if (i < cnt) { // Got to update.
get_all_terrain(); // IMPORTANT: Get all in memory.
ofstream ochunks; // Open file for chunks data.
// This truncates the file.
U7open(ochunks, PATCH_U7CHUNKS);
v2_chunks = New_shapes();
int nbytes = v2_chunks ? 3 : 2;
if (v2_chunks)
ochunks.write(v2hdr, sizeof(v2hdr));
for (i = 0; i < cnt; i++) {
Chunk_terrain *ter = (*chunk_terrains)[i];
unsigned char data[ntiles * 3];
if (ter) {
ter->write_flats(data, v2_chunks);
ter->set_modified(false);
} else {
memset(&data[0], 0, ntiles * nbytes);
cerr << "NULL terrain. U7chunks may be bad."
<< endl;
}
ochunks.write(reinterpret_cast<char *>(data),
ntiles * nbytes);
}
if (!ochunks.good())
throw file_write_exception(U7CHUNKS);
ochunks.close();
}
chunk_terrains_modified = false;
}
/*
* Write out the 'static' map files.
*/
void Game_map::write_static(
) {
char fname[128];
U7mkdir("<PATCH>", 0755); // Create dir if not already there.
// Don't use PATCHDAT define cause
// it has a trailing slash
int schunk; // Write each superchunk to 'static'.
for (schunk = 0; schunk < c_num_schunks * c_num_schunks; schunk++)
// Only write what we've modified.
if (schunk_modified[schunk])
write_ifix_objects(schunk);
if (chunk_terrains_modified)
write_chunk_terrains();
std::ofstream u7map; // Write out map.
U7open(u7map, get_mapped_name(PATCH_U7MAP, fname));
for (schunk = 0; schunk < c_num_schunks * c_num_schunks; schunk++) {
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
uint8 buf[16 * 16 * 2];
uint8 *mapdata = buf;
// Go through chunks.
for (int cy = 0; cy < 16; cy++)
for (int cx = 0; cx < 16; cx++)
Write2(mapdata, terrain_map[scx + cx][scy + cy]);
u7map.write(reinterpret_cast<char *>(buf), sizeof(buf));
}
if (!u7map.good())
throw file_write_exception(U7MAP);
u7map.close();
map_modified = false;
}
/*
* Write out one of the "u7ifix" files.
*
* Output: Errors reported.
*/
void Game_map::write_ifix_objects(
int schunk // Superchunk # (0-143).
) {
char fname[128]; // Set up name.
ofstream ifix_stream; // There it is.
U7open(ifix_stream, get_schunk_file_name(PATCH_U7IFIX, schunk, fname));
StreamDataSource ifix(&ifix_stream);
// +++++Use game title.
const int count = c_chunks_per_schunk * c_chunks_per_schunk;
Flex::Flex_vers vers = !New_shapes() ? Flex::orig : Flex::exult_v2;
bool v2 = vers == Flex::exult_v2;
Flex_writer writer(&ifix, "Exult", count, vers);
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
// Go through chunks.
for (int cy = 0; cy < 16; cy++)
for (int cx = 0; cx < 16; cx++) {
Map_chunk *chunk = get_chunk(scx + cx,
scy + cy);
// Restore original order (sort of).
Object_iterator_backwards next(chunk);
Game_object *obj;
while ((obj = next.get_next()) != 0)
obj->write_ifix(&ifix, v2);
writer.mark_section_done();
}
if (!writer.close())
throw file_write_exception(fname);
schunk_modified[schunk] = false;
return;
}
/*
* Read in the objects for a superchunk from one of the "u7ifix" files.
*/
void Game_map::get_ifix_objects(
int schunk // Superchunk # (0-143).
) {
char fname[128]; // Set up name.
ifstream ifix_stream; // There it is.
if (is_system_path_defined("<PATCH>") &&
// First check for patch.
U7exists(get_schunk_file_name(PATCH_U7IFIX, schunk, fname)))
U7open(ifix_stream, fname);
else try {
U7open(ifix_stream, get_schunk_file_name(U7IFIX, schunk, fname));
} catch (const file_exception & /*f*/) {
if (!Game::is_editing()) // Ok if map-editing.
cerr << "Ifix file '" << fname << "' not found." <<
endl;
return;
}
FlexFile flex(fname);
int vers = static_cast<int>(flex.get_vers());
StreamDataSource ifix(&ifix_stream);
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
// Go through chunks.
for (int cy = 0; cy < 16; cy++)
for (int cx = 0; cx < 16; cx++) {
// Get to index entry for chunk.
int chunk_num = cy * 16 + cx;
size_t len;
uint32 offset = flex.get_entry_info(chunk_num, len);
if (len)
get_ifix_chunk_objects(&ifix, vers, offset,
len, scx + cx, scy + cy);
}
}
/*
* Get the objects from one ifix chunk entry onto the screen.
*/
void Game_map::get_ifix_chunk_objects(
DataSource *ifix,
int vers, // Flex file vers.
long filepos, // Offset in file.
int len, // Length of data.
int cx, int cy // Absolute chunk #'s.
) {
Ifix_game_object *obj;
ifix->seek(filepos); // Get to actual shape.
// Get buffer to hold entries' indices.
unsigned char *entries = new unsigned char[len];
unsigned char *ent = entries; // Read them in.
ifix->read(reinterpret_cast<char *>(entries), len);
// Get object list for chunk.
Map_chunk *olist = get_chunk(cx, cy);
if (static_cast<Flex::Flex_vers>(vers) == Flex::orig) {
int cnt = len / 4;
for (int i = 0; i < cnt; i++, ent += 4) {
int tx = (ent[0] >> 4) & 0xf, ty = ent[0] & 0xf,
tz = ent[1] & 0xf;
int shnum = ent[2] + 256 * (ent[3] & 3), frnum = ent[3] >> 2;
Shape_info &info = ShapeID::get_info(shnum);
obj = (info.is_animated() || info.has_sfx()) ?
new Animated_ifix_object(shnum, frnum, tx, ty, tz)
: new Ifix_game_object(shnum, frnum, tx, ty, tz);
olist->add(obj);
}
} else if (static_cast<Flex::Flex_vers>(vers) == Flex::exult_v2) {
// b0 = tx,ty, b1 = lift, b2-3 = shnum, b4=frnum
int cnt = len / 5;
for (int i = 0; i < cnt; i++, ent += 5) {
int tx = (ent[0] >> 4) & 0xf, ty = ent[0] & 0xf,
tz = ent[1] & 0xf;
int shnum = ent[2] + 256 * ent[3], frnum = ent[4];
Shape_info &info = ShapeID::get_info(shnum);
obj = (info.is_animated() || info.has_sfx()) ?
new Animated_ifix_object(shnum, frnum, tx, ty, tz)
: new Ifix_game_object(shnum, frnum, tx, ty, tz);
olist->add(obj);
}
} else
assert(0);
delete[] entries; // Done with buffer.
olist->setup_dungeon_levels(); // Should have all dungeon pieces now.
}
/*
* Constants for IREG files:
*/
#define IREG_SPECIAL 255 // Precedes special entries.
#define IREG_UCSCRIPT 1 // Saved Usecode_script for object.
#define IREG_ENDMARK 2 // Just an 'end' mark.
#define IREG_ATTS 3 // Attribute/value pairs.
#define IREG_STRING 4 // A string; ie, function name.
/*
* Write out attributes for an object.
*/
void Game_map::write_attributes(
DataSource *ireg,
vector<pair<const char *, int> > &attlist
) {
int len = 0; // Figure total length.
int i, cnt = attlist.size();
if (!cnt)
return;
for (i = 0; i < cnt; ++i) {
const char *att = attlist[i].first;
len += strlen(att) + 1 + 2; // Name, NULL, val.
}
ireg->write1(IREG_SPECIAL);
ireg->write1(IREG_ATTS);
ireg->write2(len);
for (i = 0; i < cnt; ++i) {
const char *att = attlist[i].first;
int val = attlist[i].second;
ireg->write(att, strlen(att) + 1);
ireg->write2(val);
}
}
/*
* Write out scheduled usecode for an object.
*/
void Game_map::write_scheduled(
DataSource *ireg,
Game_object *obj,
bool write_mark // Write an IREG_ENDMARK if true.
) {
for (Usecode_script *scr = Usecode_script::find(obj); scr;
scr = Usecode_script::find(obj, scr)) {
ostringstream outbuf(ios::out);
StreamDataSource nbuf(&outbuf);
int len = scr->save(&nbuf);
if (len < 0)
cerr << "Error saving Usecode script" << endl;
else if (len > 0) {
ireg->write1(IREG_SPECIAL);
ireg->write1(IREG_UCSCRIPT);
ireg->write2(len); // Store length.
ireg->write(outbuf.str());
}
}
if (write_mark) {
ireg->write1(IREG_SPECIAL);
ireg->write1(IREG_ENDMARK);
}
}
/*
* Write string entry and/or return length of what's written.
*/
int Game_map::write_string(
DataSource *ireg, // Null if we just want length.
const char *str
) {
int len = 1 + strlen(str);
if (ireg) {
ireg->write1(IREG_SPECIAL);
ireg->write1(IREG_STRING);
ireg->write2(len);
ireg->write(str, len);
}
return len + 4;
}
/*
* Write modified 'u7ireg' files.
*/
void Game_map::write_ireg(
) {
// Write each superchunk to Iregxx.
for (int schunk = 0; schunk < c_num_schunks * c_num_schunks; schunk++)
// Only write what we've read.
if (schunk_cache[schunk] && schunk_cache_sizes[schunk] >= 0) {
// It's loaded in a memory buffer
char fname[128]; // Set up name.
ofstream ireg_stream;
U7open(ireg_stream, get_schunk_file_name(U7IREG, schunk, fname));
ireg_stream.write(schunk_cache[schunk], schunk_cache_sizes[schunk]);
} else if (schunk_read[schunk]) {
// It's active
write_ireg_objects(schunk);
}
}
/*
* Write out one of the "u7ireg" files.
*
* Output: 0 if error, which is reported.
*/
void Game_map::write_ireg_objects(
int schunk // Superchunk # (0-143).
) {
char fname[128]; // Set up name.
ofstream ireg_stream; // There it is.
U7open(ireg_stream, get_schunk_file_name(U7IREG, schunk, fname));
StreamDataSource ireg(&ireg_stream);
write_ireg_objects(schunk, &ireg);
ireg_stream.flush();
int result = ireg_stream.good();
if (!result) throw file_write_exception(fname);
return;
}
/*
* Write out one of the "u7ireg" files.
*
* Output: 0 if error, which is reported.
*/
void Game_map::write_ireg_objects(
int schunk, // Superchunk # (0-143).
DataSource *ireg
) {
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
// Go through chunks.
for (int cy = 0; cy < 16; cy++)
for (int cx = 0; cx < 16; cx++) {
Map_chunk *chunk = get_chunk(scx + cx,
scy + cy);
Game_object *obj;
// Restore original order (sort of).
Object_iterator_backwards next(chunk);
while ((obj = next.get_next()) != 0)
obj->write_ireg(ireg);
ireg->write2(0);// End with 2 0's.
}
}
/*
* Read in the objects for a superchunk from one of the "u7ireg" files.
* (These are the moveable objects.)
*/
void Game_map::get_ireg_objects(
int schunk // Superchunk # (0-143).
) {
char fname[128]; // Set up name.
ifstream ireg_stream; // There it is.
DataSource *ireg = 0;
if (schunk_cache[schunk] && schunk_cache_sizes[schunk] >= 0) {
// No items
if (schunk_cache_sizes[schunk] == 0) return;
ireg = new BufferDataSource(schunk_cache[schunk], schunk_cache_sizes[schunk]);
#ifdef DEBUG
std::cout << "Reading " << get_schunk_file_name(U7IREG, schunk, fname) << " from memory" << std::endl;
#endif
} else {
try {
U7open(ireg_stream, get_schunk_file_name(U7IREG, schunk, fname));
} catch (const file_exception & /*f*/) {
return; // Just don't show them.
}
ireg = new StreamDataSource(&ireg_stream);
}
int scy = 16 * (schunk / 12); // Get abs. chunk coords.
int scx = 16 * (schunk % 12);
read_ireg_objects(ireg, scx, scy);
// A fixup:
if (schunk == 10 * 12 + 11 && Game::get_game_type() == SERPENT_ISLE) {
// Lever in SilverSeed:
Game_object_vector vec;
if (Game_object::find_nearby(vec, Tile_coord(2936, 2726, 0),
787, 0, 0, c_any_qual, 5))
vec[0]->move(2937, 2727, 2);
}
delete ireg;
if (schunk_cache[schunk]) {
delete [] schunk_cache[schunk];
schunk_cache[schunk] = 0;
schunk_cache_sizes[schunk] = -1;
}
}
/*
* Read in a 'special' IREG entry (one starting with 255).
*/
void Read_special_ireg(
DataSource *ireg,
Game_object *obj // Last object read.
) {
int type = ireg->read1(); // Get type.
int len = ireg->read2(); // Length of rest.
unsigned char *buf = new unsigned char[len];
ireg->read(reinterpret_cast<char *>(buf), len);
if (type == IREG_UCSCRIPT) { // Usecode script?
BufferDataSource nbuf(buf, len);
Usecode_script *scr = Usecode_script::restore(obj, &nbuf);
if (scr) {
scr->start(scr->get_delay());
#if 0
COUT("Restored script for '" <<
get_item_name(obj->get_shapenum())
<< "'" << endl);
scr->print(cout);
cout << endl;
#endif
}
} else if (type == IREG_ATTS) // Attribute/value pairs?
obj->read_attributes(buf, len);
else if (type == IREG_STRING) { // IE, Usecode egg function name?
if (obj->is_egg())
static_cast<Egg_object *>(obj)->set_str1(
reinterpret_cast<char *>(buf));
} else
cerr << "Unknown special IREG entry: " << type << endl;
delete [] buf;
}
/*
* Read in a 'special' IREG entry (one starting with 255).
*/
void Game_map::read_special_ireg(
DataSource *ireg,
Game_object *obj // Last object read.
) {
unsigned char entlen;
while ((entlen = ireg->peek()) == IREG_SPECIAL && !ireg->eof()) {
ireg->read1(); // Eat the IREG_SPECIAL.
unsigned char type = ireg->peek();
if (type == IREG_ENDMARK) {
// End of list.
ireg->read1();
return;
}
Read_special_ireg(ireg, obj);
}
}
/*
* Containers and items classed as 'quality_flags' have a byte of flags.
* This routine returns them converted into Object_flags.
*/
inline unsigned long Get_quality_flags(
unsigned char qualbyte // Quality byte containing flags.
) {
return ((qualbyte & 1) << Obj_flags::invisible) |
(((qualbyte >> 3) & 1) << Obj_flags::okay_to_take);
}
/*
* Read a list of ireg objects. They are either placed in the desired
* game chunk, or added to their container.
*/
void Game_map::read_ireg_objects(
DataSource *ireg, // File to read from.
int scx, int scy, // Abs. chunk coords. of superchunk.
Game_object *container, // Container, or null.
unsigned long flags // Usecode item flags.
) {
unsigned char entbuf[20];
int entlen; // Gets entry length.
sint8 index_id = -1;
Game_object *last_obj = 0; // Last one read in this call.
Game_window *gwin = Game_window::get_instance();
// Go through entries.
while (((entlen = ireg->read1(), !ireg->eof()))) {
int extended = 0; // 1 for 2-byte shape #'s.
// Skip 0's & ends of containers.
if (!entlen || entlen == 1) {
if (container)
return; // Skip 0's & ends of containers.
else
continue;
}
// Detect the 2 byte index id
else if (entlen == 2) {
index_id = static_cast<sint8>(ireg->read2());
continue;
} else if (entlen == IREG_SPECIAL) {
Read_special_ireg(ireg, last_obj);
continue;
} else if (entlen == IREG_EXTENDED) {
extended = 1;
entlen = ireg->read1();
}
// Get copy of flags.
unsigned long oflags = flags & ~(1 << Obj_flags::is_temporary);
int testlen = entlen - extended;
if (testlen != 6 && testlen != 10 && testlen != 12 &&
testlen != 13 && testlen != 14 && testlen != 18) {
long pos = ireg->getPos();
cout << "Unknown entlen " << testlen << " at pos. " <<
pos << endl;
ireg->seek(pos + entlen);
continue; // Only know these two types.
}
unsigned char *entry = &entbuf[0]; // Get entry.
ireg->read(reinterpret_cast<char *>(entry), entlen);
int cx = entry[0] >> 4; // Get chunk indices within schunk.
int cy = entry[1] >> 4;
// Get coord. #'s where shape goes.
int tilex, tiley;
if (container) { // In container? Get gump coords.
tilex = entry[0];
tiley = entry[1];
} else {
tilex = entry[0] & 0xf;
tiley = entry[1] & 0xf;
}
int shnum, frnum; // Get shape #, frame #.
if (extended) {
shnum = entry[2] + 256 * entry[3];
frnum = entry[4];
++entry; // So the rest is in the right place.
} else {
shnum = entry[2] + 256 * (entry[3] & 3);
frnum = entry[3] >> 2;
}
Shape_info &info = ShapeID::get_info(shnum);
unsigned int lift, quality, type;
Ireg_game_object *obj;
int is_egg = 0; // Fields are eggs.
// Has flag byte(s)
if (testlen == 10) {
// Temporary
if (entry[6] & 1) oflags |= 1 << Obj_flags::is_temporary;
}
// An "egg"?
if (info.get_shape_class() == Shape_info::hatchable) {
bool anim = info.is_animated() || info.has_sfx();
lift = entry[9] >> 4;
Egg_object *egg = Egg_object::create_egg(entry, entlen,
anim, shnum, frnum, tilex, tiley, lift);
get_chunk(scx + cx, scy + cy)->add_egg(egg);
last_obj = egg;
continue;
} else if (testlen == 6 || testlen == 10) { // Simple entry?
type = 0;
lift = entry[4] >> 4;
quality = entry[5];
obj = create_ireg_object(info, shnum, frnum,
tilex, tiley, lift);
is_egg = obj->is_egg();