1 #define _CRT_SECURE_NO_DEPRECATE
16 #include "mpq_libmpq.h"
18 //#include <windows.h>
19 unsigned int iRes
=256;
28 int holetab_h
[4] = {0x1111, 0x2222, 0x4444, 0x8888};
29 int holetab_v
[4] = {0x000F, 0x00F0, 0x0F00, 0xF000};
31 bool LoadADT(char* filename
)
38 //printf("No such file.\n");
46 size_t mcnk_offsets
[256], mcnk_sizes
[256];
50 //uint32 fs=mf.getSize()-3;
51 //while (mf.getPos()<fs)
58 size_t nextpos
= mf
.getPos() + size
;
61 case 0x4d43494e: // MCIN
63 //printf("Found chunks info\n");
64 // mapchunk offsets/sizes
65 for (int i
=0; i
<256; i
++)
67 mf
.read(&mcnk_offsets
[i
],4);
68 mf
.read(&mcnk_sizes
[i
],4);
73 case 0x4d4f4446: // MODF
78 //printf("\nwmo count %d\n",size/64);
80 for (int i=0; i<wmo_count; i++)
84 WMO *wmo = (WMO*)wmomanager.items[wmomanager.get(wmos[id])];
85 WMOInstance inst(wmo, mf);
86 wmois.push_back(inst);
91 case 0x4d574d4f: // MWMO
96 char *buf = new char[size];
105 wmomanager.add(path);
106 wmos.push_back(path);
112 case 0x4d564552: // MVER
113 case 0x4d484452: // MHDR header
114 case 0x4d434e4b: // MCNK
115 case 0x4d544558: // MTEX textures (strings)
116 case 0x4d4d4458: // MMDX m2 models (strings)
117 case 0x4d4d4944: // MMID offsets for strings in MMDX
118 case 0x4d574944: // MWID offsets for strings in MWMO
119 case 0x4d444446: // MDDF
120 case 0x4d46424f: // MFBO new in BC
121 case 0x4d48324f: // MH2O new in WotLK
122 case 0x4D545846: // MTXF new in WotLK
126 // mf.seekRelative(-3);
127 printf("Unhandled map chunk: %u\n",fourcc
);
134 //printf("Loading chunks info\n");
135 // read individual map chunks
136 for (int j
=0; j
<16; j
++)
137 for (int i
=0; i
<16; i
++)
139 mf
.seek((int)mcnk_offsets
[j
*16+i
]);
140 LoadMapChunk(mf
,&(mcells
->ch
[i
][j
]));
144 for(uint32 t=0;t<wmo_count ;t++)
153 struct MapChunkHeader
{
176 uint32 nEffectDoodad
;
177 uint32 ofsSndEmitters
;
189 bool isHole(int holes
, int i
, int j
)
193 if(testi
>3) testi
= 3;
194 if(testj
>3) testj
= 3;
195 return (holes
& holetab_h
[testi
] & holetab_v
[testj
])!=0;
199 void LoadMapChunk(MPQFile
& mf
, chunk
*_chunk
)
204 MapChunkHeader header
;
209 size_t lastpos
= mf
.getPos() + size
;
210 mf
.read(&header
, 0x80);
211 _chunk
->area_id
=header
.areaid
;
214 float xbase
= header
.xpos
;
215 float ybase
= header
.ypos
;
216 float zbase
= header
.zpos
;
217 zbase
= TILESIZE
*32-zbase
;
218 xbase
= TILESIZE
*32-xbase
;
219 if(wmoc
.x
>xbase
)wmoc
.x
=xbase
;
220 if(wmoc
.z
>zbase
)wmoc
.z
=zbase
;
221 int chunkflags
= header
.flags
;
222 float zmin
=999999999.0f
;
223 float zmax
=-999999999.0f
;
224 //must be there, bl!zz uses some crazy format
226 while (mf
.getPos() < lastpos
)
231 // printf("\n sz=%d",size);
232 size_t nextpos
= mf
.getPos() + size
;
233 if(fourcc
==0x4d435654) // MCVT
235 for (int j
=0; j
<17; j
++)
236 for (int i
=0; i
<((j
%2)?8:9); i
++)
242 if(isHole(header
.holes
,i
,j
))
243 _chunk
->v8
[i
][j
/2] = -1000;
245 _chunk
->v8
[i
][j
/2] = z
;
249 if(isHole(header
.holes
,i
,j
))
250 _chunk
->v9
[i
][j
/2] = -1000;
252 _chunk
->v9
[i
][j
/2] = z
;
259 else if(fourcc
==0x4d434e52) // MCNR
261 nextpos
= mf
.getPos() + 0x1C0; // size fix
263 else if(fourcc
==0x4d434c51) // MCLQ
265 // liquid / water level
272 if (!strcmp(fcc1
,"MCSE"))
276 _chunk
->waterlevel
[i
][j
]=-999999; // no liquid/water
281 mf
.read(&maxheight
, 4);
289 _chunk
->waterlevel
[i
][j
]=-999999;
291 _chunk
->waterlevel
[i
][j
]=h
;
294 if(chunkflags
& 4 || chunkflags
& 8)
301 else if (fourcc
==0x4d434c59) // MCLY
304 nTextures
= (int)size
;
306 else if (fourcc
==0x4d43414c) // MCAL
316 double solve (vec
*v
,vec
*p
)
318 double a
= v
[0].y
*(v
[1].z
- v
[2].z
) + v
[1].y
*(v
[2].z
- v
[0].z
) + v
[2].y
*(v
[0].z
- v
[1].z
);
319 double b
= v
[0].z
*(v
[1].x
- v
[2].x
) + v
[1].z
*(v
[2].x
- v
[0].x
) + v
[2].z
*(v
[0].x
- v
[1].x
);
320 double c
= v
[0].x
*(v
[1].y
- v
[2].y
) + v
[1].x
*(v
[2].y
- v
[0].y
) + v
[2].x
*(v
[0].y
- v
[1].y
);
321 double d
= v
[0].x
*(v
[1].y
*v
[2].z
- v
[2].y
*v
[1].z
) + v
[1].x
* (v
[2].y
*v
[0].z
- v
[0].y
*v
[2].z
) + v
[2].x
* (v
[0].y
*v
[1].z
- v
[1].y
*v
[0].z
);
324 //plane equation ax+by+cz+d=0
325 return ((a
*p
->x
+c
*p
->z
-d
)/b
);
329 double GetZ(double x
,double z
)
339 int xc
=(int)(x
/UNITSIZE
);
340 int zc
=(int)(z
/UNITSIZE
);
344 double lx
=x
-xc
*UNITSIZE
;
345 double lz
=z
-zc
*UNITSIZE
;
350 v
[0].y
=cell
->v8
[xc
][zc
];
356 v
[1].y
=cell
->v9
[xc
+1][zc
];
362 v
[1].y
=cell
->v9
[xc
][zc
+1];
369 v
[2].y
=cell
->v9
[xc
+1][zc
+1];
375 v
[2].y
=cell
->v9
[xc
][zc
];
384 void TransformWaterData()
388 for(int x
=0;x
<128;x
++)
389 for(int y
=0;y
<128;y
++)
390 cell
->v9
[x
][y
] = mcells
->ch
[x
/8][y
/8].waterlevel
[x
%8][y
%8];
393 cell
->v9
[128][128] = mcells
->ch
[15][15].waterlevel
[8][8];
401 for(int x
=0;x
<128;x
++)
403 for(int y
=0;y
<128;y
++)
405 cell
->v8
[x
][y
] = (float)mcells
->ch
[x
/8][y
/8].v8
[x
%8][y
%8];
406 cell
->v9
[x
][y
] = (float)mcells
->ch
[x
/8][y
/8].v9
[x
%8][y
%8];
409 //extra 1 point on bounds
410 cell
->v9
[x
][128] = (float)mcells
->ch
[x
/8][15].v9
[x
%8][8];
412 cell
->v9
[128][x
] = (float)mcells
->ch
[15][x
/8].v9
[8][x
%8];
417 cell
->v9
[128][128] = (float)mcells
->ch
[15][15].v9
[8][8];
422 const char MAP_MAGIC
[] = "MAP_2.00";
424 bool ConvertADT(char * filename
,char * filename2
)
426 //if(!strstr(filename,"oth_32_48"))return false;
427 if(!LoadADT(filename
))
430 FILE *output
=fopen(filename2
,"wb");
433 printf("Can't create the output file '%s'\n",filename2
);
437 // write magic header
438 fwrite(MAP_MAGIC
,1,8,output
);
440 for(unsigned int x
=0;x
<16;x
++)
442 for(unsigned int y
=0;y
<16;y
++)
444 if(mcells
->ch
[y
][x
].area_id
&& mcells
->ch
[y
][x
].area_id
< 0x102D)
446 if(areas
[mcells
->ch
[y
][x
].area_id
]==0xffff)
447 printf("\nCan't find area flag for areaid %u.\n",mcells
->ch
[y
][x
].area_id
);
449 fwrite(&areas
[mcells
->ch
[y
][x
].area_id
],1,2,output
);
454 fwrite(&flag
,1,2,output
);
459 for(unsigned int x
=0;x
<16;x
++)
460 for(unsigned int y
=0;y
<16;y
++)
461 fwrite(&mcells
->ch
[y
][x
].flag
,1,1,output
);
463 TransformWaterData();
465 for(unsigned int x
=0;x
<128;x
++)
466 for(unsigned int y
=0;y
<128;y
++)
467 fwrite(&cell
->v9
[y
][x
],1,sizeof(float),output
);
472 for(unsigned int x
=0;x
<iRes
;x
++)
473 for(unsigned int y
=0;y
<iRes
;y
++)
476 (((double)(y
))*TILESIZE
)/((double)(iRes
-1)),
477 (((double)(x
))*TILESIZE
)/((double)(iRes
-1)));
479 fwrite(&z
,1,sizeof(z
),output
);
485 for (std::vector<std::string>::iterator it = wmos.begin(); it != wmos.end(); ++it)
486 wmomanager.delbyname(*it);
491 for (std::vector<model>::iterator it = wmomodel.begin(); it != wmomodel.end(); ++it)
496 //printf("\n %d \n",in);
498 //polygons.clear();*/