Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // new stuff
- #include <il/il.h>
- #include <il/ilu.h>
- #include "../../../Client/PRTerrainLib/Terrain.h"
- #include <vector>
- namespace atr{
- enum eAttrEnumRet{RET_OK=0, ATTR_NOT_READABLE=1, SERVERATTR_NOT_WRITABLE=2};
- typedef struct sAttrTypeRet_s
- {
- int iType;
- std::string szErrFile;
- } sAttrTypeRet_t;
- #define M2_NEW new
- #define M2_DELETE(p) delete (p)
- #define M2_DELETE_ARRAY(p) delete[] (p)
- #define M2_PTR_REF(p) (p)
- #define M2_PTR_DEREF(p) (*(p))
- #define IS_SET(flag, bit) ((flag) & (bit))
- #define SET_BIT(var, bit) ((var) |= (bit))
- #define REMOVE_BIT(var, bit) ((var) &= ~(bit))
- #define TOGGLE_BIT(var, bit) ((var) = (var) ^ (bit))
- #define CELL_SIZE 50
- #define SECTREE_SIZE 6400
- #define SECMAP_SIZE (SECTREE_SIZE / CELL_SIZE) * (SECTREE_SIZE / CELL_SIZE) // 16384
- #define SECMAP_SIZE2 sizeof(DWORD) * SECMAP_SIZE // 65536
- #define SECMAP_SIZE3 SECMAP_SIZE/sizeof(DWORD) // 4096
- #define ATTR_SIZE CTerrainImpl::ATTRMAP_XSIZE * CTerrainImpl::ATTRMAP_YSIZE * sizeof(BYTE) // 65536
- #define ATTR_HEAD sizeof(WORD) + sizeof(WORD) + sizeof(WORD) // 6
- #define SECATTR_SIZE TERRAIN_SIZE/2 // 64
- #define SECATTR_SIZE2 TERRAIN_SIZE*2 // 256
- #define SECATTR_SIZE3 TERRAIN_SIZE*3 // 384
- #define SECATTR_SIZE4 TERRAIN_SIZE*4 // 512
- void GetLzoAttr(std::vector<std::vector<std::vector<DWORD> > > & attrMaps, DWORD iWidth, DWORD iHeight, FILE* wfp)
- {
- assert(SECMAP_SIZE2==65536); // ==ATTR_SIZE
- assert(SECMAP_SIZE==16384); // ==ATTR_SIZE/4
- assert(TERRAIN_SIZE==128);
- unsigned int uiSize;
- unsigned int uiDestSize;
- size_t maxMemSize = LZOManager::instance().GetMaxCompressedSize(SECMAP_SIZE2);
- Tracef("allocated %u\n", static_cast<unsigned int>(maxMemSize));
- BYTE * abComp = M2_NEW BYTE[maxMemSize];
- DWORD * attr = M2_NEW DWORD[SECMAP_SIZE];
- // BYTE wReg;
- int sCountX = iWidth/4;
- int sCountY = iHeight/4;
- Tracef("sCountX %d sCountY %d iWidth %d iHeight %d\n", sCountX, sCountY, iWidth, iHeight);
- for (DWORD ry = 0; ry < iHeight; ry++)
- {
- for (DWORD rx = 0; rx < iWidth; rx++)
- {
- // generate server_attr sector by attr.atr data
- uiSize = SECMAP_SIZE2;
- memset(attr, 0, SECMAP_SIZE2);
- std::copy(attrMaps[ry][rx].begin(), attrMaps[ry][rx].end(), attr);
- // compress and finalize server_attr sector
- Tracef("[%d x %d] orig_size %d\n", rx, ry, uiSize);
- LZOManager::instance().Compress((unsigned char *) attr, uiSize, abComp, (lzo_uint*)&uiDestSize);
- Tracef("[%d x %d] new_size %d\n", rx, ry, uiDestSize);
- fwrite(&uiDestSize, sizeof(int), 1, wfp);
- fwrite(abComp, uiDestSize, 1, wfp);
- }
- }
- // delete stuff
- M2_DELETE_ARRAY(attr);
- M2_DELETE_ARRAY(abComp);
- }
- sAttrTypeRet_t __attrProcess(short sCountX, short sCountY, std::string csMapName)
- {
- LZOManager pLZOManager;
- sAttrTypeRet_t retValue;
- retValue.szErrFile=csMapName+"\\server_attr";
- retValue.iType=RET_OK;
- // attr.atr details
- const WORD mapver = 2634;
- const int iAttrSize = ATTR_SIZE;
- // server_attr details
- int iWidth = sCountX*4;
- int iHeight = sCountY*4;
- std::vector<std::vector<std::vector<DWORD> > > attrMaps(iHeight, std::vector<std::vector<DWORD> >(iWidth, std::vector<DWORD>(SECMAP_SIZE, 0)));
- Tracef("ServerAttr size %d %d (Attr*4)\n", iWidth, iHeight);
- // assert(iWidth==4 && iHeight==4);
- FILE * wfp = fopen(retValue.szErrFile.c_str(), "wb");
- if (!wfp)
- {
- retValue.iType=SERVERATTR_NOT_WRITABLE;
- return retValue;
- }
- fwrite(&iWidth, sizeof(int), 1, wfp);
- fwrite(&iHeight, sizeof(int), 1, wfp);
- for (short sX = 0; sX < sCountX; ++sX)
- {
- for (short sY = 0; sY < sCountY; ++sY)
- {
- DWORD wID = (DWORD) (sX) * 1000L + (DWORD)(sY);
- char szAttrFileName[MAX_PATH + 1];
- _snprintf(szAttrFileName, MAX_PATH, "%s\\%06u\\attr.atr", csMapName.c_str(), wID);
- char szBufAttr[iAttrSize+6];
- FILE * fp = fopen(szAttrFileName, "rb");
- if (!fp)
- {
- retValue.szErrFile=szAttrFileName;
- retValue.iType=ATTR_NOT_READABLE;
- return retValue;
- }
- fseek(fp, 0L, SEEK_END);
- size_t sz = ftell(fp);
- fseek(fp, 0L, SEEK_SET);
- Tracef("[%s] size %u\n", szAttrFileName, static_cast<unsigned int>(sz));
- // process attr.atr
- fread(szBufAttr, iAttrSize+6, 1, fp);
- fclose(fp);
- ilInit();
- ilEnable(IL_FILE_OVERWRITE);
- ILuint image;
- ilGenImages(1, &image);
- ilBindImage(image);
- ilTexImage(256, 256, 1, 1, IL_RGBA, IL_BYTE, NULL);
- ILubyte * pRawData = ilGetData();
- memcpy(pRawData, szBufAttr+6, ilGetInteger(IL_IMAGE_WIDTH)*ilGetInteger(IL_IMAGE_HEIGHT));
- ilSetData(pRawData);
- iluScale(512, 512, 1);
- ilConvertImage(IL_RGBA, IL_INT); // do not try to save the image after this via DevIL
- pRawData = ilGetData(); // do not try to use ilSetData (bpp>1; crash)
- {
- int ilHeight = ilGetInteger(IL_IMAGE_HEIGHT);
- int ilWidth = ilGetInteger(IL_IMAGE_WIDTH);
- int ilBpp = ilGetInteger(IL_IMAGE_BPP);
- DWORD* pdwRawData = M2_NEW DWORD[ilWidth*ilHeight];
- memcpy(pdwRawData, pRawData, ilWidth*ilHeight*ilBpp);
- Tracef("RawAttrData width=%d, height=%d, bpp=%d\n", ilWidth, ilHeight, ilBpp);
- Tracef("allocated %u\n", ilWidth*ilHeight*ilBpp);
- DWORD* pdwRawData2 = pdwRawData;
- bool found = false;
- for (int h = 0; h < ilHeight; ++h)
- {
- for (int w = 0; w < ilWidth; ++w)
- {
- *pdwRawData2 >>= 24;
- if (IS_SET(*pdwRawData2, 0xFFFFFFF8))
- {
- if (!found)
- {
- TraceError("!!! wrong attr flags in height %d width %d flag 0x%08x", h, w, *pdwRawData2);
- found = true;
- }
- REMOVE_BIT(*pdwRawData2, 0xFFFFFFF8);
- }
- pdwRawData2++;
- }
- }
- for (DWORD rx = 0; rx < sizeof(DWORD); rx++)
- {
- for (DWORD ry = 0; ry < sizeof(DWORD); ry++)
- {
- for (int y = 0; y < TERRAIN_SIZE; ++y)
- {
- for (int x = 0; x < TERRAIN_SIZE; ++x)
- {
- attrMaps[(rx+sY*4)][(ry+sX*4)][x+(y*TERRAIN_SIZE)] = pdwRawData[(x+ry*TERRAIN_SIZE)+(y*SECATTR_SIZE4+rx*ATTR_SIZE)];
- }
- }
- }
- }
- // ilSetData(pdwRawData); // crash
- if (0)
- {
- char szAttrFileNameRaw[MAX_PATH + 1];
- _snprintf(szAttrFileNameRaw, MAX_PATH, "%s.raw", szAttrFileName);
- FILE * wwfp = fopen(szAttrFileNameRaw, "wb");
- assert(wwfp);
- Tracef("RawAttrData width=%d, height=%d, bpp=%d\n", ilWidth, ilHeight, ilBpp);
- fwrite(pdwRawData, ilWidth*ilHeight*ilBpp, 1, wwfp);
- fclose(wwfp);
- }
- M2_DELETE_ARRAY(pdwRawData);
- }
- ilDeleteImages(1, &image);
- ILenum Error;
- while ((Error = ilGetError()) != IL_NO_ERROR)
- TraceError("DevIL: %d: %s", Error, iluErrorString(Error));
- }
- }
- GetLzoAttr(attrMaps, iWidth, iHeight, wfp);
- fclose(wfp);
- return retValue;
- }
- }
- void CMapFilePage::OnButtonInitShadowMap() // generate server attr
- {
- CWorldEditorApp * pApplication = (CWorldEditorApp *)AfxGetApp();
- CMapManagerAccessor * pMapManagerAccessor = pApplication->GetMapManagerAccessor();
- if (!pMapManagerAccessor->IsMapReady())
- {
- LogBox("Load a map before dumping a server_attr file.");
- return;
- }
- int iRet = ::MessageBox(NULL, "This will generate/overwrite a server_attr inside the map folder.\nNote: The file will be created using all the map's attr.atr and not from memory.\nSo save the map before proceeding!", "Info", MB_YESNO);
- if (6 != iRet)
- return;
- CMainFrame * pFrame = (CMainFrame*)AfxGetMainWnd();
- CWorldEditorView * pView = (CWorldEditorView *)pFrame->GetActiveView();
- pView->Lock();
- {
- CMapOutdoorAccessor * pMapOutdoor = pMapManagerAccessor->GetMapOutdoorPtr();
- // CMapOutdoor & rMapOutdoor = pMapManagerAccessor->GetMapOutdoorRef();
- Tracef("MapName %s\n", pMapOutdoor->GetName().c_str());
- short sCountX, sCountY;
- pMapOutdoor->GetTerrainCount(&sCountX, &sCountY);
- Tracef("AttrMap size %d %d\n", sCountX, sCountY);
- atr::sAttrTypeRet_t retValue = atr::__attrProcess(sCountX, sCountY, pMapOutdoor->GetName());
- switch (retValue.iType)
- {
- case atr::RET_OK:
- LogBoxf("server_attr successfully saved in [%s].", retValue.szErrFile.c_str());
- break;
- case atr::ATTR_NOT_READABLE:
- LogBoxf("[%s] is not readable or missing.", retValue.szErrFile.c_str());
- break;
- case atr::SERVERATTR_NOT_WRITABLE:
- LogBoxf("[%s] is not writable.", retValue.szErrFile.c_str());
- break;
- }
- }
- pView->Unlock();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement