Pol  Revision:cb584c9
uofile08.cpp
Go to the documentation of this file.
1 
9 #include <cstdio>
10 #include <map>
11 #include <stdexcept>
12 #include <stdlib.h>
13 
14 #include "../clib/passert.h"
15 #include "../clib/rawtypes.h"
16 #include "clidata.h"
17 #include "poltype.h"
18 #include "uofile.h"
19 #include "uofilei.h"
20 #include "ustruct.h"
21 
22 #include "../plib/RawMap.h"
23 
24 namespace Pol
25 {
26 namespace Plib
27 {
28 unsigned int num_map_patches = 0;
29 }
30 namespace Core
31 {
32 
34 static bool rawmap_ready = false;
35 
37 {
39 }
40 
41 static signed char rawmapinfo( unsigned short x, unsigned short y, USTRUCT_MAPINFO* gi )
42 {
43  // UoTool has a serious problem of not loading data before using this function...
44  if ( !rawmap_ready )
46 
47  return rawmap.rawinfo( x, y, gi );
48 }
49 
51 {
53 
54  unsigned int blocks = 0;
55  if ( mapfile == nullptr )
56  blocks = rawmap.load_full_map( uo_mapid, uopmapfile );
57  else
58  blocks = rawmap.load_full_map( mapfile, mapdif_file );
59 
60  if ( blocks )
61  rawmap_ready = true;
62 }
63 
64 /*
65 
66  MAP(x,y) MAP
67  ^
68  \--CHARACTER(x,y)
69 
70  MAP MAP
71 
72  We use the diagonal with the smallest differential..
73 
74  Simple averaging doesn't seem to work near cliffs and such.
75  poltest has a unit test for this.
76  */
77 bool groundheight( unsigned short x, unsigned short y, short* z )
78 {
79  USTRUCT_MAPINFO md, mi;
80  short z1, z2, z3, z4; // quadrants
81 
82  z1 = rawmapinfo( x + 1, y, &md );
83  z2 = rawmapinfo( x, y, &mi );
84  z3 = rawmapinfo( x, y + 1, &md );
85  z4 = rawmapinfo( x + 1, y + 1, &md );
86 
87  if ( abs( z1 - z3 ) < abs( z2 - z4 ) )
88  *z = ( z1 + z3 ) / 2;
89  else
90  *z = ( z2 + z4 ) / 2;
91 
92  if ( mi.landtile == 0x2 ) // it's a nodraw tile
93  *z = Core::ZCOORD_MIN;
94 
95  return ( ( mi.landtile < 0x4000 ) &&
96  ( ( landtile_uoflags( mi.landtile ) & USTRUCT_TILE::FLAG_BLOCKING ) == 0 ) );
97 }
98 
99 void getmapinfo( unsigned short x, unsigned short y, short* z, USTRUCT_MAPINFO* mi )
100 {
101  USTRUCT_MAPINFO md;
102  short z1, z2, z3, z4; // quadrants
103 
104  z1 = rawmapinfo( x + 1, y, &md );
105  z2 = rawmapinfo( x, y, mi );
106  z3 = rawmapinfo( x, y + 1, &md );
107  z4 = rawmapinfo( x + 1, y + 1, &md );
108 
109  short zsum;
110 
111  if ( abs( z1 - z3 ) < abs( z2 - z4 ) )
112  {
113  zsum = ( z1 + z3 );
114  }
115  else
116  {
117  zsum = ( z2 + z4 );
118  }
119  if ( zsum >= 0 )
120  {
121  *z = zsum / 2;
122  }
123  else
124  {
125  *z = ( zsum - 1 ) / 2;
126  }
127 }
128 
129 void safe_getmapinfo( unsigned short x, unsigned short y, short* z, USTRUCT_MAPINFO* mi )
130 {
131  USTRUCT_MAPINFO md;
132  short z1, z2, z3, z4; // quadrants
133 
134  if ( x >= uo_map_width )
135  x = uo_map_width - 1;
136  if ( y >= uo_map_height )
137  y = uo_map_height - 1;
138  unsigned short xp = x + 1, yp = y + 1;
139  if ( xp >= uo_map_width )
140  xp = uo_map_width - 1;
141  if ( yp >= uo_map_height )
142  yp = uo_map_height - 1;
143  z1 = rawmapinfo( xp, y, &md );
144  z2 = rawmapinfo( x, y, mi );
145  z3 = rawmapinfo( x, yp, &md );
146  z4 = rawmapinfo( xp, yp, &md );
147 
148  short zsum;
149 
150  if ( abs( z1 - z3 ) < abs( z2 - z4 ) )
151  {
152  zsum = ( z1 + z3 );
153  }
154  else
155  {
156  zsum = ( z2 + z4 );
157  }
158  if ( zsum >= 0 )
159  {
160  *z = zsum / 2;
161  }
162  else
163  {
164  *z = ( zsum - 1 ) / 2;
165  }
166 }
167 }
168 }
unsigned int load_full_map(FILE *mapfile, FILE *mapdif_file)
Definition: RawMap.cpp:67
FILE * mapdif_file
Definition: uofile00.cpp:30
void getmapinfo(unsigned short x, unsigned short y, short *z, USTRUCT_MAPINFO *mi)
Definition: uofile08.cpp:99
unsigned int load_map_difflist(FILE *mapdifl_file)
Definition: RawMap.cpp:52
static signed char rawmapinfo(unsigned short x, unsigned short y, USTRUCT_MAPINFO *gi)
Definition: uofile08.cpp:41
unsigned int num_map_patches
Definition: uofile08.cpp:28
int uo_mapid
Definition: uofile00.cpp:85
unsigned int landtile_uoflags(unsigned short landtile)
Definition: landtile.cpp:41
void safe_getmapinfo(unsigned short x, unsigned short y, short *z, USTRUCT_MAPINFO *mi)
Definition: uofile08.cpp:129
const short ZCOORD_MIN
Definition: poltype.h:15
signed char rawinfo(unsigned short x, unsigned short y, Pol::Core::USTRUCT_MAPINFO *gi)
Definition: RawMap.cpp:25
FILE * mapdifl_file
Definition: uofile00.cpp:29
void set_bounds(unsigned short width, unsigned short height)
Definition: RawMap.cpp:46
FILE * mapfile
Definition: uofile00.cpp:21
void read_map_difs()
Definition: uofile08.cpp:36
bool groundheight(unsigned short x, unsigned short y, short *z)
Definition: uofile08.cpp:77
std::ifstream uopmapfile
Definition: uofile00.cpp:32
unsigned short uo_map_height
Definition: uofile00.cpp:90
void rawmapfullread()
Definition: uofile08.cpp:50
unsigned short uo_map_width
Definition: uofile00.cpp:89
static Pol::Plib::RawMap rawmap
Definition: uofile08.cpp:33
static bool rawmap_ready
Definition: uofile08.cpp:34
Definition: berror.cpp:12