Pol  Revision:cb584c9
uofile01.cpp
Go to the documentation of this file.
1 
8 #include <cstdio>
9 #include <cstring>
10 
11 #include "../clib/passert.h"
12 #include "../clib/rawtypes.h"
13 #include "../plib/systemstate.h"
14 #include "uofile.h"
15 #include "uofilei.h"
16 #include "ustruct.h"
17 
18 namespace Pol
19 {
20 namespace Core
21 {
22 #define VERFILE_TILEDATA 0x1E
23 #define TILEDATA_TILES 0x68800
24 #define TILEDATA_TILES_HSA 0x78800
25 
26 struct TileData
27 {
31 };
33 
34 const unsigned N_LANDTILEDATA = 0x4000;
36 
37 unsigned int landtile_uoflags( unsigned short landtile )
38 {
39  passert_always( landtile < N_LANDTILEDATA );
40  return landtile_flags_arr[landtile];
41 }
42 
43 
45 {
46  typedef std::map<unsigned int, Core::USTRUCT_VERSION> VRecList;
47  VRecList vrecs; // key is the block
48 
49  void insert( const Core::USTRUCT_VERSION& vrec );
50  bool find( unsigned int block, const Core::USTRUCT_VERSION*& vrec );
51 };
53 {
54  vrecs.insert( VRecList::value_type( vrec.block, vrec ) );
55 }
56 bool VerdataIndexes::find( unsigned int block, const Core::USTRUCT_VERSION*& vrec )
57 {
58  VRecList::const_iterator itr = vrecs.find( block );
59  if ( itr == vrecs.end() )
60  return false;
61 
62  vrec = &( ( *itr ).second );
63  return true;
64 }
65 
66 
68 const unsigned int vidx_count = 32;
69 
70 bool check_verdata( unsigned int file, unsigned int block, const Core::USTRUCT_VERSION*& vrec )
71 {
72  return vidx[file].find( block, vrec );
73 }
74 
75 static bool seekto_newer_version( unsigned int file, unsigned int block )
76 {
77  const Core::USTRUCT_VERSION* vrec;
78  if ( vidx[file].find( block, vrec ) )
79  {
80  fseek( Core::verfile, vrec->filepos, SEEK_SET );
81  return true;
82  }
83  else
84  {
85  return false;
86  }
87 }
88 
89 void readtile( unsigned short tilenum, Core::USTRUCT_TILE* tile )
90 {
91  memset( tile, 0, sizeof *tile );
92 
93  if ( tilenum > Plib::systemstate.config.max_tile_id )
94  {
95  sprintf( tile->name, "multi" );
96  tile->weight = 0xFF;
97  }
98  else
99  {
100  int block;
101  block = ( tilenum / 32 );
102  if ( seekto_newer_version( VERFILE_TILEDATA, block + 0x200 ) )
103  {
104  int filepos;
105  filepos = 4 + ( sizeof *tile ) * ( tilenum & 0x1F );
106  fseek( Core::verfile, filepos, SEEK_CUR );
107  fread( tile, sizeof *tile, 1, Core::verfile );
108  }
109  else
110  {
111  // 512 blocks
112  // header 4
113  // 32*size (30)
114  int filepos;
115  filepos = TILEDATA_TILES + ( block * 4 ) + // skip headers of all previous blocks
116  4 + // skip my header
117  ( sizeof( Core::USTRUCT_TILE ) * tilenum );
118  fseek( Core::tilefile, filepos, SEEK_SET );
119  fread( tile, sizeof *tile, 1, Core::tilefile );
120  }
121  }
122 
123  // ensure string is null-terminated
124  tile->name[sizeof( tile->name ) - 1] = '\0';
125 }
126 
127 void readtile( unsigned short tilenum, Core::USTRUCT_TILE_HSA* tile )
128 {
129  memset( tile, 0, sizeof *tile );
130 
131  if ( tilenum > Plib::systemstate.config.max_tile_id )
132  {
133  sprintf( tile->name, "multi" );
134  tile->weight = 0xFF;
135  }
136  else
137  {
138  int block;
139  block = ( tilenum / 32 );
140  if ( seekto_newer_version( VERFILE_TILEDATA, block + 0x200 ) )
141  {
142  int filepos;
143  filepos = 4 + ( sizeof *tile ) * ( tilenum & 0x1F );
144  fseek( Core::verfile, filepos, SEEK_CUR );
145  fread( tile, sizeof *tile, 1, Core::verfile );
146  }
147  else
148  {
149  int filepos;
150  filepos = TILEDATA_TILES_HSA + ( block * 4 ) + // skip headers of all previous blocks
151  4 + // skip my header
152  ( sizeof( Core::USTRUCT_TILE_HSA ) * tilenum );
153  fseek( Core::tilefile, filepos, SEEK_SET );
154  fread( tile, sizeof *tile, 1, Core::tilefile );
155  }
156  }
157 
158  // ensure string is null-terminated
159  tile->name[sizeof( tile->name ) - 1] = '\0';
160 }
161 
162 
163 void readlandtile( unsigned short tilenum, Core::USTRUCT_LAND_TILE* landtile )
164 {
165  memset( landtile, 0, sizeof( *landtile ) );
166 
167  if ( tilenum <= 0x3FFF )
168  {
169  int block;
170  block = ( tilenum / 32 );
171  if ( seekto_newer_version( VERFILE_TILEDATA, block ) )
172  {
173  int filepos;
174  filepos = 4 + ( sizeof *landtile ) * ( tilenum & 0x1F );
175  fseek( Core::verfile, filepos, SEEK_CUR );
176  fread( landtile, sizeof *landtile, 1, Core::verfile );
177  }
178  else
179  {
180  int filepos;
181  filepos = ( block * 4 ) + // skip headers of all previous blocks
182  4 + // skip my header
183  ( sizeof( Core::USTRUCT_LAND_TILE ) * tilenum );
184  fseek( Core::tilefile, filepos, SEEK_SET );
185  fread( landtile, sizeof *landtile, 1, Core::tilefile );
186  }
187  }
188 
189  // ensure string is null-terminated
190  landtile->name[sizeof( landtile->name ) - 1] = '\0';
191 }
192 
193 void readlandtile( unsigned short tilenum, Core::USTRUCT_LAND_TILE_HSA* landtile )
194 {
195  memset( landtile, 0, sizeof( *landtile ) );
196 
197  if ( tilenum <= 0x3FFF )
198  {
199  int block;
200  block = ( tilenum / 32 );
201  if ( seekto_newer_version( VERFILE_TILEDATA, block ) )
202  {
203  int filepos;
204  filepos = 4 + ( sizeof *landtile ) * ( tilenum & 0x1F );
205  fseek( Core::verfile, filepos, SEEK_CUR );
206  fread( landtile, sizeof *landtile, 1, Core::verfile );
207  }
208  else
209  {
210  int filepos;
211  filepos = ( block * 4 ) + // skip headers of all previous blocks
212  4 + // skip my header
213  ( sizeof( Core::USTRUCT_LAND_TILE_HSA ) * tilenum );
214  fseek( Core::tilefile, filepos, SEEK_SET );
215  fread( landtile, sizeof *landtile, 1, Core::tilefile );
216  }
217  }
218 
219  // ensure string is null-terminated
220  landtile->name[sizeof( landtile->name ) - 1] = '\0';
221 }
222 
223 void read_objinfo( u16 graphic, Core::USTRUCT_TILE& objinfo )
224 {
225  readtile( graphic, &objinfo );
226 }
227 
228 void read_objinfo( u16 graphic, Core::USTRUCT_TILE_HSA& objinfo )
229 {
230  readtile( graphic, &objinfo );
231 }
232 
233 char tileheight( unsigned short tilenum )
234 {
235  u8 height;
236  u32 flags;
237 
238  if ( tilenum <= Plib::systemstate.config.max_tile_id )
239  {
240  height = tiledata[tilenum].height;
241  flags = tiledata[tilenum].flags;
242  }
243  else
244  {
246  {
248  readtile( tilenum, &tile );
249  height = tile.height;
250  flags = tile.flags;
251  }
252  else
253  {
254  Core::USTRUCT_TILE tile;
255  readtile( tilenum, &tile );
256  height = tile.height;
257  flags = tile.flags;
258  }
259  }
260 
262  return ( height / 2 );
263  else
264  return height;
265 }
266 
267 unsigned char tilelayer( unsigned short tilenum )
268 {
269  if ( tilenum <= Plib::systemstate.config.max_tile_id )
270  {
271  return tiledata[tilenum].layer;
272  }
273  else
274  {
276  {
278  tile.layer = 0;
279  readtile( tilenum, &tile );
280  return tile.layer;
281  }
282  else
283  {
284  Core::USTRUCT_TILE tile;
285  tile.layer = 0;
286  readtile( tilenum, &tile );
287  return tile.layer;
288  }
289  }
290 }
291 
292 u16 tileweight( unsigned short tilenum )
293 {
295  {
297  tile.weight = 1;
298  readtile( tilenum, &tile );
299  return tile.weight;
300  }
301  else
302  {
303  Core::USTRUCT_TILE tile;
304  tile.weight = 1;
305  readtile( tilenum, &tile );
306  return tile.weight;
307  }
308 }
309 
310 u32 tile_uoflags( unsigned short tilenum )
311 {
312  if ( tilenum <= Plib::systemstate.config.max_tile_id )
313  {
314  return tiledata[tilenum].flags;
315  }
316  else
317  {
319  {
321  tile.flags = 0;
322  readtile( tilenum, &tile );
323  return tile.flags;
324  }
325  else
326  {
327  Core::USTRUCT_TILE tile;
328  tile.flags = 0;
329  readtile( tilenum, &tile );
330  return tile.flags;
331  }
332  }
333 }
334 
335 
336 static void read_veridx()
337 {
338  int num_version_records;
340 
341  if ( Core::verfile != NULL )
342  {
343  // FIXME: should read this once per run, per file.
344  fseek( verfile, 0, SEEK_SET );
345  fread( &num_version_records, sizeof num_version_records, 1, verfile ); // ENDIAN-BROKEN
346 
347  for ( int i = 0; i < num_version_records; i++ )
348  {
349  fread( &vrec, sizeof vrec, 1, verfile );
350 
351  if ( vrec.file < vidx_count )
352  {
353  vidx[vrec.file].insert( vrec );
354  }
355  }
356  }
357 }
358 
359 static void read_tiledata()
360 {
361  tiledata = new TileData[Plib::systemstate.config.max_tile_id + 1];
362 
363  for ( u32 graphic_i = 0; graphic_i <= Plib::systemstate.config.max_tile_id; ++graphic_i )
364  {
365  u16 graphic = static_cast<u16>( graphic_i );
367  {
368  Core::USTRUCT_TILE_HSA objinfo;
369  memset( &objinfo, 0, sizeof objinfo );
370  readtile( (u16)graphic, &objinfo );
371 
372  tiledata[graphic].height = objinfo.height;
373  tiledata[graphic].layer = objinfo.layer;
374  tiledata[graphic].flags = objinfo.flags;
375  }
376  else
377  {
378  Core::USTRUCT_TILE objinfo;
379  memset( &objinfo, 0, sizeof objinfo );
380  readtile( graphic, &objinfo );
381 
382  tiledata[graphic].height = objinfo.height;
383  tiledata[graphic].layer = objinfo.layer;
384  tiledata[graphic].flags = objinfo.flags;
385  }
386  }
387 }
388 
390 {
391  delete[] tiledata;
392 }
393 
394 static void read_landtiledata()
395 {
396  for ( u16 objtype = 0; objtype < N_LANDTILEDATA; ++objtype )
397  {
399  {
401  readlandtile( objtype, &landtile );
402 
403  landtile_flags_arr[objtype] = landtile.flags;
404  }
405  else
406  {
407  Core::USTRUCT_LAND_TILE landtile;
408  readlandtile( objtype, &landtile );
409 
410  landtile_flags_arr[objtype] = landtile.flags;
411  }
412  }
413 }
414 void read_uo_data( void )
415 {
416  read_veridx();
417  read_tiledata();
421 }
422 }
423 }
unsigned char u8
Definition: rawtypes.h:25
void readtile(unsigned short tilenum, USTRUCT_TILE *tile)
Definition: uofile01.cpp:89
static void read_veridx()
Definition: uofile01.cpp:336
std::map< unsigned int, Core::USTRUCT_VERSION > VRecList
Definition: uofile01.cpp:46
#define TILEDATA_TILES
Definition: uofile01.cpp:23
SystemState systemstate
Definition: systemstate.cpp:12
TileData * tiledata
Definition: uofile01.cpp:32
Core::PolConfig config
Definition: systemstate.h:43
unsigned int tile_uoflags(unsigned short tilenum)
Definition: polfile2.cpp:61
unsigned char tilelayer(unsigned short tilenum)
Definition: polfile2.cpp:22
void clear_tiledata()
Definition: uofile01.cpp:389
const unsigned int vidx_count
Definition: uofile01.cpp:68
void read_objinfo(u16 graphic, struct USTRUCT_TILE &objinfo)
Definition: uofile01.cpp:223
#define VERFILE_TILEDATA
Definition: uofile01.cpp:22
unsigned int max_tile_id
Definition: polcfg.h:61
bool cfg_use_new_hsa_format
Definition: uofile02.cpp:40
void readlandtile(unsigned short tilenum, USTRUCT_LAND_TILE *landtile)
Definition: uofile01.cpp:163
bool check_verdata(unsigned int file, unsigned int block, const Core::USTRUCT_VERSION *&vrec)
Definition: uofile01.cpp:70
unsigned short u16
Definition: rawtypes.h:26
unsigned int u32
Definition: rawtypes.h:27
unsigned int landtile_uoflags(unsigned short landtile)
Definition: landtile.cpp:41
void read_static_diffs()
Definition: uofile02.cpp:53
static void read_tiledata()
Definition: uofile01.cpp:359
#define TILEDATA_TILES_HSA
Definition: uofile01.cpp:24
char tileheight(unsigned short tilenum)
Definition: polfile2.cpp:34
FILE * tilefile
Definition: uofile00.cpp:25
static bool seekto_newer_version(unsigned int file, unsigned int block)
Definition: uofile01.cpp:75
void read_map_difs()
Definition: uofile08.cpp:36
FILE * verfile
Definition: uofile00.cpp:24
VerdataIndexes vidx[32]
Definition: uofile01.cpp:67
unsigned int landtile_flags_arr[N_LANDTILEDATA]
Definition: uofile01.cpp:35
const unsigned N_LANDTILEDATA
Definition: uofile01.cpp:34
void read_uo_data(void)
Definition: uofile01.cpp:414
static void read_landtiledata()
Definition: uofile01.cpp:394
bool find(unsigned int block, const Core::USTRUCT_VERSION *&vrec)
Definition: uofile01.cpp:56
unsigned short tileweight(unsigned short tilenum)
Definition: polfile2.cpp:85
#define passert_always(exp)
Definition: passert.h:80
void insert(const Core::USTRUCT_VERSION &vrec)
Definition: uofile01.cpp:52
Definition: berror.cpp:12