Pol  Revision:cb584c9
polfile1.cpp
Go to the documentation of this file.
1 
10 #include <cstdio>
11 #include <string>
12 
13 #include "../clib/fileutil.h"
14 #include "../clib/logfacility.h"
15 #include "../clib/passert.h"
16 #include "../clib/rawtypes.h"
17 #include "../plib/realmdescriptor.h"
18 #include "../plib/staticblock.h"
19 #include "../plib/systemstate.h"
20 #include "clidata.h"
21 #include "uofile.h"
22 #include "ustruct.h"
23 
24 namespace Pol
25 {
26 namespace Core
27 {
29 
30 bool newstat_dont_add( std::vector<Plib::STATIC_ENTRY>& vec, USTRUCT_STATIC* pstat )
31 {
32  char pheight = tileheight( pstat->graphic );
33 
34  for ( unsigned i = 0; i < vec.size(); ++i )
35  {
36  Plib::STATIC_ENTRY& prec = vec[i];
38  char height = tileheight( prec.objtype ); // TODO read from itemdesc?
39  unsigned char xy = ( pstat->x_offset << 4 ) | pstat->y_offset;
40  if ( // flags == pflags &&
41  prec.objtype == pstat->graphic && // TODO map objtype->graphic from itemdesc
42  height == pheight && prec.xy == xy && prec.z == pstat->z && prec.hue == pstat->hue )
43  {
44  return true;
45  }
46  }
47  return false;
48 }
49 
50 int write_pol_static_files( const std::string& realm )
51 {
52  unsigned int duplicates = 0;
53  unsigned int illegales = 0;
54  unsigned int statics = 0;
55  unsigned int empties = 0;
56  unsigned int nonempties = 0;
57  unsigned int maxcount = 0;
58 
59  std::string directory = "realm/" + realm + "/";
60 
61  std::string statidx_dat = directory + "statidx.dat";
62  std::string statics_dat = directory + "statics.dat";
63  std::string statidx_tmp = directory + "statidx.tmp";
64  std::string statics_tmp = directory + "statics.tmp";
65  Clib::RemoveFile( statidx_dat );
66  Clib::RemoveFile( statics_dat );
67  Clib::RemoveFile( statidx_tmp );
68  Clib::RemoveFile( statics_tmp );
69 
70  FILE* fidx = fopen( statidx_tmp.c_str(), "wb" );
71  FILE* fdat = fopen( statics_tmp.c_str(), "wb" );
72 
74 
75  int lastprogress = -1;
76  unsigned int index = 0;
77  for ( u16 y = 0; y < descriptor.height; y += Plib::STATICBLOCK_CHUNK )
78  {
79  int progress = y * 100L / descriptor.height;
80  if ( progress != lastprogress )
81  {
82  INFO_PRINT << "\rCreating POL statics files: " << progress << "%";
83  lastprogress = progress;
84  }
85  for ( u16 x = 0; x < descriptor.width; x += Plib::STATICBLOCK_CHUNK )
86  {
88  idx.index = index;
89  fwrite( &idx, sizeof idx, 1, fidx );
90 
91  std::vector<USTRUCT_STATIC> pstat;
92  int num;
93  std::vector<Plib::STATIC_ENTRY> vec;
94  readstaticblock( &pstat, &num, x, y );
95  for ( int i = 0; i < num; ++i )
96  {
97  if ( pstat[i].graphic <= Plib::systemstate.config.max_tile_id )
98  {
99  if ( !newstat_dont_add( vec, &pstat[i] ) )
100  {
101  Plib::STATIC_ENTRY nrec;
102 
103  nrec.objtype = pstat[i].graphic; // TODO map these?
104  nrec.xy = ( pstat[i].x_offset << 4 ) | pstat[i].y_offset;
105  nrec.z = pstat[i].z;
106  nrec.hue = pstat[i].hue;
107  vec.push_back( nrec );
108  ++statics;
109  }
110  else
111  {
112  ++duplicates;
113  }
114  }
115  else
116  {
117  ++illegales;
118 
119  if ( cfg_show_illegal_graphic_warning )
120  INFO_PRINT << " Warning: Item with illegal Graphic 0x" << fmt::hexu( pstat[i].graphic )
121  << " in Area " << x << " " << y << " " << ( x + Plib::STATICBLOCK_CHUNK - 1 )
122  << " " << ( y + Plib::STATICBLOCK_CHUNK - 1 ) << "\n";
123  }
124  }
125  for ( unsigned i = 0; i < vec.size(); ++i )
126  {
127  fwrite( &vec[i], sizeof( Plib::STATIC_ENTRY ), 1, fdat );
128  ++index;
129  }
130  if ( vec.empty() )
131  ++empties;
132  else
133  ++nonempties;
134  if ( vec.size() > maxcount )
135  maxcount = static_cast<unsigned int>( vec.size() );
136  }
137  }
138  Plib::STATIC_INDEX idx;
139  idx.index = index;
140  fwrite( &idx, sizeof idx, 1, fidx );
141 
142  int errors = ferror( fdat ) || ferror( fidx );
143  fclose( fdat );
144  fclose( fidx );
145  if ( !errors )
146  {
147  INFO_PRINT << "\rCreating POL statics files: Complete\n";
148  rename( statidx_tmp.c_str(), statidx_dat.c_str() );
149  rename( statics_tmp.c_str(), statics_dat.c_str() );
150  }
151  else
152  {
153  INFO_PRINT << "\rCreating POL statics files: Error\n";
154  }
155 
156 
157 #ifndef NDEBUG
158  INFO_PRINT << statics << " statics written\n"
159  << duplicates << " duplicates eliminated\n"
160  << illegales << " illegales eliminated\n"
161  << empties << " empties\n"
162  << nonempties << " nonempties\n"
163  << maxcount << " was the highest count\n";
164 #endif
165  return 0;
166 }
167 }
168 }
static RealmDescriptor Load(const std::string &realm_name, const std::string &realm_path="")
SystemState systemstate
Definition: systemstate.cpp:12
Core::PolConfig config
Definition: systemstate.h:43
bool cfg_show_illegal_graphic_warning
Definition: polfile1.cpp:28
const unsigned STATICBLOCK_CHUNK
Definition: staticblock.h:37
u16 hue
Definition: staticblock.h:30
u16 objtype
Definition: staticblock.h:27
unsigned int max_tile_id
Definition: polcfg.h:61
u8 xy
Definition: staticblock.h:28
unsigned short u16
Definition: rawtypes.h:26
void readstaticblock(std::vector< USTRUCT_STATIC > *ppst, int *pnum, unsigned short x, unsigned short y)
Definition: uofile02.cpp:68
char tileheight(unsigned short tilenum)
Definition: polfile2.cpp:34
s8 z
Definition: staticblock.h:29
Definition: staticblock.h:25
void RemoveFile(const std::string &fname)
Definition: fileutil.cpp:143
bool newstat_dont_add(std::vector< Plib::STATIC_ENTRY > &vec, USTRUCT_STATIC *pstat)
Definition: polfile1.cpp:30
#define passert_always(exp)
Definition: passert.h:80
#define INFO_PRINT
Definition: logfacility.h:223
Definition: berror.cpp:12
int write_pol_static_files(const std::string &realm)
Definition: polfile1.cpp:50