Advertisement
Guest User

Untitled

a guest
Jul 21st, 2014
1,741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.36 KB | None | 0 0
  1.  
  2. // new stuff
  3. #include <il/il.h>
  4. #include <il/ilu.h>
  5. #include "../../../Client/PRTerrainLib/Terrain.h"
  6. #include <vector>
  7. namespace atr{
  8. enum eAttrEnumRet{RET_OK=0, ATTR_NOT_READABLE=1, SERVERATTR_NOT_WRITABLE=2};
  9. typedef struct sAttrTypeRet_s
  10. {
  11.     int iType;
  12.     std::string szErrFile;
  13. } sAttrTypeRet_t;
  14.  
  15. #define M2_NEW new
  16. #define M2_DELETE(p) delete (p)
  17. #define M2_DELETE_ARRAY(p) delete[] (p)
  18. #define M2_PTR_REF(p) (p)
  19. #define M2_PTR_DEREF(p) (*(p))
  20.  
  21. #define IS_SET(flag, bit)                ((flag) & (bit))
  22. #define SET_BIT(var, bit)                ((var) |= (bit))
  23. #define REMOVE_BIT(var, bit)             ((var) &= ~(bit))
  24. #define TOGGLE_BIT(var, bit)             ((var) = (var) ^ (bit))
  25.  
  26. #define CELL_SIZE       50
  27. #define SECTREE_SIZE    6400
  28. #define SECMAP_SIZE     (SECTREE_SIZE / CELL_SIZE) * (SECTREE_SIZE / CELL_SIZE)                     // 16384
  29. #define SECMAP_SIZE2    sizeof(DWORD) * SECMAP_SIZE                                                 // 65536
  30. #define SECMAP_SIZE3    SECMAP_SIZE/sizeof(DWORD)                                                   // 4096
  31.  
  32. #define ATTR_SIZE       CTerrainImpl::ATTRMAP_XSIZE * CTerrainImpl::ATTRMAP_YSIZE * sizeof(BYTE)    // 65536
  33. #define ATTR_HEAD       sizeof(WORD) + sizeof(WORD) + sizeof(WORD)                                  // 6
  34. #define SECATTR_SIZE    TERRAIN_SIZE/2                                                              // 64
  35. #define SECATTR_SIZE2   TERRAIN_SIZE*2                                                              // 256
  36. #define SECATTR_SIZE3   TERRAIN_SIZE*3                                                              // 384
  37. #define SECATTR_SIZE4   TERRAIN_SIZE*4                                                              // 512
  38.  
  39. void GetLzoAttr(std::vector<std::vector<std::vector<DWORD> > > & attrMaps, DWORD iWidth, DWORD iHeight, FILE* wfp)
  40. {
  41.     assert(SECMAP_SIZE2==65536); // ==ATTR_SIZE
  42.     assert(SECMAP_SIZE==16384); // ==ATTR_SIZE/4
  43.     assert(TERRAIN_SIZE==128);
  44.     unsigned int uiSize;
  45.     unsigned int uiDestSize;
  46.  
  47.     size_t maxMemSize = LZOManager::instance().GetMaxCompressedSize(SECMAP_SIZE2);
  48.     Tracef("allocated %u\n", static_cast<unsigned int>(maxMemSize));
  49.  
  50.     BYTE * abComp = M2_NEW BYTE[maxMemSize];
  51.     DWORD * attr = M2_NEW DWORD[SECMAP_SIZE];
  52.  
  53.     // BYTE wReg;
  54.     int sCountX = iWidth/4;
  55.     int sCountY = iHeight/4;
  56.     Tracef("sCountX %d sCountY %d iWidth %d iHeight %d\n", sCountX, sCountY, iWidth, iHeight);
  57.  
  58.     for (DWORD ry = 0; ry < iHeight; ry++)
  59.     {
  60.     for (DWORD rx = 0; rx < iWidth; rx++)
  61.     {
  62.         // generate server_attr sector by attr.atr data
  63.         uiSize = SECMAP_SIZE2;
  64.         memset(attr, 0, SECMAP_SIZE2);
  65.  
  66.         std::copy(attrMaps[ry][rx].begin(), attrMaps[ry][rx].end(), attr);
  67.         // compress and finalize server_attr sector
  68.         Tracef("[%d x %d] orig_size %d\n", rx, ry, uiSize);
  69.         LZOManager::instance().Compress((unsigned char *) attr, uiSize, abComp, (lzo_uint*)&uiDestSize);
  70.         Tracef("[%d x %d] new_size %d\n", rx, ry, uiDestSize);
  71.  
  72.         fwrite(&uiDestSize, sizeof(int), 1, wfp);
  73.         fwrite(abComp, uiDestSize, 1, wfp);
  74.     }
  75.     }
  76.     // delete stuff
  77.     M2_DELETE_ARRAY(attr);
  78.     M2_DELETE_ARRAY(abComp);
  79. }
  80.  
  81. sAttrTypeRet_t __attrProcess(short sCountX, short sCountY, std::string csMapName)
  82. {
  83.     LZOManager pLZOManager;
  84.  
  85.     sAttrTypeRet_t retValue;
  86.     retValue.szErrFile=csMapName+"\\server_attr";
  87.     retValue.iType=RET_OK;
  88.     // attr.atr details
  89.     const WORD mapver = 2634;
  90.     const int iAttrSize = ATTR_SIZE;
  91.  
  92.     // server_attr details
  93.     int iWidth = sCountX*4;
  94.     int iHeight = sCountY*4;
  95.     std::vector<std::vector<std::vector<DWORD> > > attrMaps(iHeight, std::vector<std::vector<DWORD> >(iWidth, std::vector<DWORD>(SECMAP_SIZE, 0)));
  96.  
  97.     Tracef("ServerAttr size %d %d (Attr*4)\n", iWidth, iHeight);
  98.     // assert(iWidth==4 && iHeight==4);
  99.  
  100.     FILE * wfp = fopen(retValue.szErrFile.c_str(), "wb");
  101.     if (!wfp)
  102.     {
  103.         retValue.iType=SERVERATTR_NOT_WRITABLE;
  104.         return retValue;
  105.     }
  106.     fwrite(&iWidth, sizeof(int), 1, wfp);
  107.     fwrite(&iHeight, sizeof(int), 1, wfp);
  108.  
  109.     for (short sX = 0; sX < sCountX; ++sX)
  110.     {
  111.         for (short sY = 0; sY < sCountY; ++sY)
  112.         {
  113.             DWORD wID = (DWORD) (sX) * 1000L + (DWORD)(sY);
  114.             char szAttrFileName[MAX_PATH + 1];
  115.             _snprintf(szAttrFileName, MAX_PATH, "%s\\%06u\\attr.atr", csMapName.c_str(), wID);
  116.  
  117.             char szBufAttr[iAttrSize+6];
  118.             FILE * fp = fopen(szAttrFileName, "rb");
  119.             if (!fp)
  120.             {
  121.                 retValue.szErrFile=szAttrFileName;
  122.                 retValue.iType=ATTR_NOT_READABLE;
  123.                 return retValue;
  124.             }
  125.             fseek(fp, 0L, SEEK_END);
  126.             size_t sz = ftell(fp);
  127.             fseek(fp, 0L, SEEK_SET);
  128.             Tracef("[%s] size %u\n", szAttrFileName, static_cast<unsigned int>(sz));
  129.             // process attr.atr
  130.             fread(szBufAttr, iAttrSize+6, 1, fp);
  131.             fclose(fp);
  132.  
  133.             ilInit();
  134.             ilEnable(IL_FILE_OVERWRITE);
  135.  
  136.             ILuint image;
  137.             ilGenImages(1, &image);
  138.             ilBindImage(image);
  139.  
  140.             ilTexImage(256, 256, 1, 1, IL_RGBA, IL_BYTE, NULL);
  141.  
  142.             ILubyte * pRawData = ilGetData();
  143.             memcpy(pRawData, szBufAttr+6, ilGetInteger(IL_IMAGE_WIDTH)*ilGetInteger(IL_IMAGE_HEIGHT));
  144.  
  145.             ilSetData(pRawData);
  146.             iluScale(512, 512, 1);
  147.  
  148.             ilConvertImage(IL_RGBA, IL_INT); // do not try to save the image after this via DevIL
  149.             pRawData = ilGetData(); // do not try to use ilSetData (bpp>1; crash)
  150.             {
  151.                 int ilHeight = ilGetInteger(IL_IMAGE_HEIGHT);
  152.                 int ilWidth = ilGetInteger(IL_IMAGE_WIDTH);
  153.                 int ilBpp = ilGetInteger(IL_IMAGE_BPP);
  154.                 DWORD* pdwRawData = M2_NEW DWORD[ilWidth*ilHeight];
  155.                 memcpy(pdwRawData, pRawData, ilWidth*ilHeight*ilBpp);
  156.                 Tracef("RawAttrData width=%d, height=%d, bpp=%d\n", ilWidth, ilHeight, ilBpp);
  157.                 Tracef("allocated %u\n", ilWidth*ilHeight*ilBpp);
  158.                 DWORD* pdwRawData2 = pdwRawData;
  159.                 bool found = false;
  160.                 for (int h = 0; h < ilHeight; ++h)
  161.                 {
  162.                     for (int w = 0; w < ilWidth; ++w)
  163.                     {
  164.                         *pdwRawData2 >>= 24;
  165.                         if (IS_SET(*pdwRawData2, 0xFFFFFFF8))
  166.                         {
  167.                             if (!found)
  168.                             {
  169.                                 TraceError("!!! wrong attr flags in height %d width %d flag 0x%08x", h, w, *pdwRawData2);
  170.                                 found = true;
  171.                             }
  172.                             REMOVE_BIT(*pdwRawData2, 0xFFFFFFF8);
  173.                         }
  174.                         pdwRawData2++;
  175.                     }
  176.                 }
  177.                 for (DWORD rx = 0; rx < sizeof(DWORD); rx++)
  178.                 {
  179.                 for (DWORD ry = 0; ry < sizeof(DWORD); ry++)
  180.                 {
  181.                     for (int y = 0; y < TERRAIN_SIZE; ++y)
  182.                     {
  183.                         for (int x = 0; x < TERRAIN_SIZE; ++x)
  184.                         {
  185.                             attrMaps[(rx+sY*4)][(ry+sX*4)][x+(y*TERRAIN_SIZE)] = pdwRawData[(x+ry*TERRAIN_SIZE)+(y*SECATTR_SIZE4+rx*ATTR_SIZE)];
  186.                         }
  187.                     }
  188.                 }
  189.                 }
  190.                 // ilSetData(pdwRawData); // crash
  191.                 if (0)
  192.                 {
  193.                     char szAttrFileNameRaw[MAX_PATH + 1];
  194.                     _snprintf(szAttrFileNameRaw, MAX_PATH, "%s.raw", szAttrFileName);
  195.  
  196.                     FILE * wwfp = fopen(szAttrFileNameRaw, "wb");
  197.                     assert(wwfp);
  198.                     Tracef("RawAttrData width=%d, height=%d, bpp=%d\n", ilWidth, ilHeight, ilBpp);
  199.                     fwrite(pdwRawData, ilWidth*ilHeight*ilBpp, 1, wwfp);
  200.                     fclose(wwfp);
  201.                 }
  202.                 M2_DELETE_ARRAY(pdwRawData);
  203.             }
  204.  
  205.             ilDeleteImages(1, &image);
  206.  
  207.             ILenum Error;
  208.             while ((Error = ilGetError()) != IL_NO_ERROR)
  209.             TraceError("DevIL: %d: %s", Error, iluErrorString(Error));
  210.         }
  211.     }
  212.     GetLzoAttr(attrMaps, iWidth, iHeight, wfp);
  213.     fclose(wfp);
  214.     return retValue;
  215. }
  216. }
  217.  
  218. void CMapFilePage::OnButtonInitShadowMap() // generate server attr
  219. {
  220.     CWorldEditorApp * pApplication = (CWorldEditorApp *)AfxGetApp();
  221.     CMapManagerAccessor * pMapManagerAccessor = pApplication->GetMapManagerAccessor();
  222.     if (!pMapManagerAccessor->IsMapReady())
  223.     {
  224.         LogBox("Load a map before dumping a server_attr file.");
  225.         return;
  226.     }
  227.     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);
  228.     if (6 != iRet)
  229.         return;
  230.  
  231.     CMainFrame * pFrame = (CMainFrame*)AfxGetMainWnd();
  232.     CWorldEditorView * pView = (CWorldEditorView *)pFrame->GetActiveView();
  233.     pView->Lock();
  234.  
  235.     {
  236.         CMapOutdoorAccessor * pMapOutdoor = pMapManagerAccessor->GetMapOutdoorPtr();
  237.         // CMapOutdoor & rMapOutdoor = pMapManagerAccessor->GetMapOutdoorRef();
  238.  
  239.         Tracef("MapName %s\n", pMapOutdoor->GetName().c_str());
  240.  
  241.         short sCountX, sCountY;
  242.         pMapOutdoor->GetTerrainCount(&sCountX, &sCountY);
  243.         Tracef("AttrMap size %d %d\n", sCountX, sCountY);
  244.  
  245.         atr::sAttrTypeRet_t retValue = atr::__attrProcess(sCountX, sCountY, pMapOutdoor->GetName());
  246.  
  247.         switch (retValue.iType)
  248.         {
  249.             case atr::RET_OK:
  250.                 LogBoxf("server_attr successfully saved in [%s].", retValue.szErrFile.c_str());
  251.                 break;
  252.             case atr::ATTR_NOT_READABLE:
  253.                 LogBoxf("[%s] is not readable or missing.", retValue.szErrFile.c_str());
  254.                 break;
  255.             case atr::SERVERATTR_NOT_WRITABLE:
  256.                 LogBoxf("[%s] is not writable.", retValue.szErrFile.c_str());
  257.                 break;
  258.         }
  259.     }
  260.     pView->Unlock();
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement