Advertisement
Dimitri_UA

Untitled

Jan 18th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.26 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Extension.h"
  4. #include "Stream.h"
  5. #include "RwTypes.h"
  6. #include "StdInc.h"
  7.  
  8. struct gtaCollisionSphere_COLL
  9. {
  10.     gtaRwReal radius;
  11.     gtaRwV3d center;
  12.     gtaRwUInt8 material;
  13.     gtaRwUInt8 piece;
  14.     gtaRwUInt8 _pad[2];
  15. };
  16.  
  17. struct gtaCollisionSphere_COL2
  18. {
  19.     gtaRwV3d center;
  20.     gtaRwReal radius;
  21.     gtaRwUInt8 material;
  22.     gtaRwUInt8 piece;
  23.     gtaRwUInt8 lighting;
  24.     gtaRwUInt8 _pad;
  25. };
  26.  
  27. struct gtaCollisionSphere
  28. {
  29.     gtaRwV3d center;
  30.     gtaRwReal radius;
  31.     gtaRwUInt8 material;
  32.     gtaRwUInt8 piece;
  33.     gtaRwUInt8 lighting;
  34. };
  35.  
  36. struct gtaCollisionBox_COLL
  37. {
  38.     gtaRwV3d min;
  39.     gtaRwV3d max;
  40.     gtaRwUInt8 material;
  41.     gtaRwUInt8 piece;
  42.     gtaRwUInt8 _pad[2];
  43. };
  44.  
  45. struct gtaCollisionBox_COL2
  46. {
  47.     gtaRwV3d min;
  48.     gtaRwV3d max;
  49.     gtaRwUInt8 material;
  50.     gtaRwUInt8 piece;
  51.     gtaRwUInt8 lighting;
  52.     gtaRwUInt8 _pad;
  53. };
  54.  
  55. struct gtaCollisionBox
  56. {
  57.     gtaRwV3d min;
  58.     gtaRwV3d max;
  59.     gtaRwUInt8 material;
  60.     gtaRwUInt8 piece;
  61.     gtaRwUInt8 lighting;
  62. };
  63.  
  64. struct gtaCollisionFaceGroup
  65. {
  66.     gtaRwV3d min;
  67.     gtaRwV3d max;
  68.     gtaRwUInt16 startFace;
  69.     gtaRwUInt16 endFace;
  70. };
  71.  
  72. struct gtaCollisionLine_COLL
  73. {
  74.     gtaRwV3d start;
  75.     gtaRwV3d end;
  76. };
  77.  
  78. struct gtaCollisionLine_COL2
  79. {
  80.     gtaRwV3d start;
  81.     gtaRwReal startRadius; // unused
  82.     gtaRwV3d end;
  83.     gtaRwReal endRadius; // unused
  84. };
  85.  
  86. struct gtaCollisionLine
  87. {
  88.     gtaRwV3d start;
  89.     gtaRwReal startRadius; // unused
  90.     gtaRwV3d end;
  91.     gtaRwReal endRadius; // unused
  92. };
  93.  
  94. struct gtaCollisionDisk
  95. {
  96.     gtaRwV3d start;
  97.     gtaRwReal startRadius;
  98.     gtaRwUInt8 material;
  99.     gtaRwUInt8 piece;
  100.     gtaRwUInt8 lighting;
  101.     gtaRwUInt8 _pad;
  102.     gtaRwV3d end;
  103.     gtaRwReal endRadius;
  104. };
  105.  
  106. struct gtaCollisionTriangle_COLL
  107. {
  108.     gtaRwUInt32 a, b, c;
  109.     gtaRwUInt8 material;
  110.     gtaRwUInt8 unused;
  111.     gtaRwUInt8 _pad[2];
  112. };
  113.  
  114. struct gtaCollisionTriangle_COL2
  115. {
  116.     gtaRwUInt16 a, b, c;
  117.     gtaRwUInt8 material;
  118.     gtaRwUInt8 light;
  119. };
  120.  
  121. struct gtaCollisionTriangle
  122. {
  123.     gtaRwUInt16 a, b, c;
  124.     gtaRwUInt8 material;
  125.     gtaRwUInt8 light;
  126. };
  127.  
  128. struct gtaCollisionVertex_COLL
  129. {
  130.     gtaRwReal x, y, z;
  131. };
  132.  
  133. struct gtaCollisionVertex_COL2
  134. {
  135.     gtaRwInt16 x, y, z;
  136. };
  137.  
  138. struct gtaCollisionVertex
  139. {
  140.     gtaRwReal x, y, z;
  141. };
  142.  
  143. enum gtaCollisionVersion
  144. {
  145.     GTA_COLL = 0,
  146.     GTA_COL2 = 1,
  147.     GTA_COL3 = 2,
  148.     GTA_COL4 = 3
  149. };
  150.  
  151. struct gtaCollisionFile_COLL
  152. {
  153.     gtaRwReal radius;
  154.     gtaRwV3d center;
  155.     gtaRwV3d min;
  156.     gtaRwV3d max;
  157.     gtaRwUInt32 numSpheres;
  158. };
  159.  
  160. struct gtaCollisionFile_COL2
  161. {
  162.     gtaRwV3d min;
  163.     gtaRwV3d max;
  164.     gtaRwV3d center;
  165.     gtaRwReal radius;
  166.     gtaRwUInt16 numSpheres;
  167.     gtaRwUInt16 numBoxes;
  168.     gtaRwUInt16 numFaces;
  169.     gtaRwUInt8 numLines;
  170.     gtaRwUInt8 _pad;
  171.     union{
  172.         gtaRwUInt32 flags;
  173.         struct{
  174.             gtaRwUInt32 usesDisks : 1;
  175.             gtaRwUInt32 isNotEmpty : 1;
  176.             gtaRwUInt32 flags_b3 : 1;
  177.             gtaRwUInt32 hasFaceGroups : 1;
  178.             gtaRwUInt32 hasShadowMesh : 1;
  179.         };
  180.     };
  181.     gtaCollisionSphere_COL2 *spheres;
  182.     gtaCollisionBox_COL2 *boxes;
  183.     union
  184.     {
  185.         gtaCollisionLine_COL2 *lines;
  186.         gtaCollisionDisk *disks;
  187.     };
  188.     gtaCollisionVertex_COL2 *vertices;
  189.     gtaCollisionTriangle_COL2 *faces;
  190.     void *facePlanes; // not read
  191. };
  192.  
  193. struct gtaCollisionFile_COL3 : public gtaCollisionFile_COL2
  194. {
  195.     gtaRwUInt32 numShadowFaces;
  196.     gtaCollisionVertex_COL2 *shadowVertices;
  197.     gtaCollisionTriangle_COL2 *shadowFaces;
  198. };
  199.  
  200. struct gtaCollisionFile_COL4 : public gtaCollisionFile_COL3
  201. {
  202.     gtaRwUInt32 unknown; // not used
  203. };
  204.  
  205. struct gtaCollisionFile
  206. {
  207.     gtaRwChar modelName[20];
  208.     gtaRwInt8 _pad[2];
  209.     gtaRwInt16 modelId;
  210.     union
  211.     {
  212.         gtaCollisionFile_COLL coll;
  213.         gtaCollisionFile_COL2 col2;
  214.         gtaCollisionFile_COL3 col3;
  215.         gtaCollisionFile_COL4 col4;
  216.     };
  217. };
  218.  
  219. struct gtaClumpCollisionPlugin : public gtaRwExtension
  220. {
  221.     gtaRwUInt32 fourcc;
  222.     gtaRwUInt32 size;
  223.     gtaRwChar modelName[20];
  224.     gtaRwInt16 modelId;
  225.     gtaRwV3d min;
  226.     gtaRwV3d max;
  227.     gtaRwV3d center;
  228.     gtaRwReal radius;
  229.     gtaRwUInt16 numSpheres;
  230.     gtaRwUInt16 numBoxes;
  231.     gtaRwUInt16 numFaces;
  232.     gtaRwUInt32 numVertices;
  233.     union
  234.     {
  235.         gtaRwUInt8 numLines;
  236.         gtaRwUInt8 numDisks;
  237.     };
  238.     gtaRwBool usesDisks;
  239.     gtaCollisionSphere *spheres;
  240.     gtaCollisionBox *boxes;
  241.     union
  242.     {
  243.         gtaCollisionLine *lines;
  244.         gtaCollisionDisk *disks;
  245.     };
  246.     gtaCollisionVertex *vertices;
  247.     gtaCollisionTriangle *faces;
  248.     gtaRwUInt32 numFaceGroups;
  249.     gtaCollisionFaceGroup *faceGroups;
  250.     gtaRwUInt32 numShadowFaces;
  251.     gtaRwUInt32 numShadowVertices;
  252.     gtaCollisionVertex *shadowVertices;
  253.     gtaCollisionTriangle *shadowFaces;
  254.  
  255.     gtaClumpCollisionPlugin()
  256.     {
  257.         memset(this, 0, sizeof(gtaClumpCollisionPlugin));
  258.     }
  259.  
  260.     void Initialise(gtaCollisionVersion Version, gtaRwChar *Name, gtaRwInt32 ModelId, gtaRwUInt32 NumSpheres, gtaRwUInt32 NumBoxes,
  261.         gtaRwUInt32 NumLines, gtaRwUInt32 NumVertices, gtaRwUInt32 NumFaces, gtaRwUInt32 NumShadowVertices, gtaRwUInt32 NumShadowFaces)
  262.     {
  263.         Destroy();
  264.         switch(Version)
  265.         {
  266.         case GTA_COL3:
  267.             fourcc = '3LOC';
  268.         default:
  269.             gtaRwErrorSet("Not supported version (%d) for Collision plugin", fourcc);
  270.             return;
  271.         }
  272.         enabled = true;
  273.         modelId = ModelId;
  274.         strcpy(modelName, Name);
  275.         numSpheres = NumSpheres;
  276.         numBoxes = NumBoxes;
  277.         numLines = NumLines;
  278.         numVertices = NumVertices;
  279.         numFaces = NumFaces;
  280.         numShadowFaces = NumShadowFaces;
  281.         numShadowVertices = NumShadowVertices;
  282.         if(NumSpheres > 0)
  283.             spheres = (gtaCollisionSphere *)gtaMemAlloc(NumSpheres * sizeof(gtaCollisionSphere));
  284.         if(NumBoxes > 0)
  285.             boxes = (gtaCollisionBox *)gtaMemAlloc(NumBoxes * sizeof(gtaCollisionBox));
  286.         if(NumLines > 0)
  287.             lines = (gtaCollisionLine *)gtaMemAlloc(NumLines * sizeof(gtaCollisionLine));
  288.         if(NumVertices > 0)
  289.             vertices = (gtaCollisionVertex *)gtaMemAlloc(NumVertices * sizeof(gtaCollisionVertex));
  290.         if(NumFaces > 0)
  291.             faces = (gtaCollisionTriangle *)gtaMemAlloc(NumFaces * sizeof(gtaCollisionTriangle));
  292.         if(NumShadowFaces > 0)
  293.             shadowFaces = (gtaCollisionTriangle *)gtaMemAlloc(NumShadowFaces * sizeof(gtaCollisionTriangle));
  294.         if(NumShadowVertices > 0)
  295.             shadowVertices = (gtaCollisionVertex *)gtaMemAlloc(NumSpheres * sizeof(gtaCollisionVertex));
  296.     }
  297.  
  298.     void Destroy()
  299.     {
  300.         if(spheres)
  301.             free(spheres);
  302.         if(boxes)
  303.             free(boxes);
  304.         if(lines)
  305.             free(lines);
  306.         if(vertices)
  307.             free(vertices);
  308.         if(faces)
  309.             free(faces);
  310.         if(faceGroups)
  311.             free(faceGroups);
  312.         if(shadowVertices)
  313.             free(shadowVertices);
  314.         if(shadowFaces)
  315.             free(shadowFaces);
  316.         memset(this, 0, sizeof(gtaClumpCollisionPlugin));
  317.     }
  318.  
  319.     bool StreamWrite(gtaRwStream *stream)
  320.     {
  321.         if(enabled)
  322.         {
  323.             if(!gtaRwStreamWriteVersionedChunkHeader(stream, gtaID_COLLISIONPLUGIN, 0, gtaRwVersion, gtaRwBuild))
  324.                 return false;
  325.         }
  326.         return true;
  327.     }
  328.  
  329.     bool StreamRead(gtaRwStream *stream)
  330.     {
  331.         Destroy();
  332.         if(gtaRwStreamRead(stream, &fourcc, 8) != 8)
  333.             return false;
  334.         gtaCollisionFile *data = (gtaCollisionFile*)gtaMemAlloc(size);
  335.         if(gtaRwStreamRead(stream, data, size) == size)
  336.         {
  337.             strcpy(modelName, data->modelName);
  338.             modelId = data->modelId;
  339.             switch(fourcc)
  340.             {
  341.             case '3LOC':
  342.                 enabled = true;
  343.                 min = data->col3.min;
  344.                 max = data->col3.max;
  345.                 center = data->col3.center;
  346.                 radius = data->col3.radius;
  347.                 numSpheres = data->col3.numSpheres;
  348.                 numBoxes = data->col3.numBoxes;
  349.                 numLines = data->col3.numLines;
  350.                 numFaces = data->col3.numFaces;
  351.  
  352.                 // read spheres
  353.                 if(numSpheres > 0 && data->col3.spheres)
  354.                 {
  355.                     data->col3.spheres = (gtaCollisionSphere_COL2 *)((gtaRwUInt32)data->col3.spheres + (gtaRwUInt32)data - 4);
  356.                     spheres = (gtaCollisionSphere *)gtaMemAlloc(sizeof(gtaCollisionSphere) * numSpheres);
  357.                     for(int i = 0; i < numSpheres; i++)
  358.                     {
  359.                         spheres[i].center = data->col3.spheres[i].center;
  360.                         spheres[i].radius = data->col3.spheres[i].radius;
  361.                         spheres[i].material = data->col3.spheres[i].material;
  362.                         spheres[i].piece = data->col3.spheres[i].piece;
  363.                         spheres[i].lighting = data->col3.spheres[i].lighting;
  364.                     }
  365.                 }
  366.                 // read boxes
  367.                 if(numBoxes > 0 && data->col3.boxes)
  368.                 {
  369.                     data->col3.boxes = (gtaCollisionBox_COL2 *)((gtaRwUInt32)data->col3.boxes + (gtaRwUInt32)data - 4);
  370.                     boxes = (gtaCollisionBox *)gtaMemAlloc(sizeof(gtaCollisionBox) * numBoxes);
  371.                     for(int i = 0; i < numBoxes; i++)
  372.                     {
  373.                         boxes[i].min = data->col3.boxes[i].min;
  374.                         boxes[i].max = data->col3.boxes[i].max;
  375.                         boxes[i].material = data->col3.boxes[i].material;
  376.                         boxes[i].piece = data->col3.boxes[i].piece;
  377.                         boxes[i].lighting = data->col3.boxes[i].lighting;
  378.                     }
  379.                 }
  380.                 // read lines or disks
  381.                 if(data->col3.usesDisks)
  382.                 {
  383.                     if(numDisks > 0 && data->col3.disks)
  384.                     {
  385.                         data->col3.disks = (gtaCollisionDisk *)((gtaRwUInt32)data->col3.disks + (gtaRwUInt32)data - 4);
  386.                         disks = (gtaCollisionDisk *)gtaMemAlloc(sizeof(gtaCollisionBox) * numDisks);
  387.                         for(int i = 0; i < numDisks; i++)
  388.                         {
  389.                             disks[i].start = data->col3.disks[i].start;
  390.                             disks[i].end = data->col3.disks[i].end;
  391.                             disks[i].startRadius = data->col3.disks[i].startRadius;
  392.                             disks[i].endRadius = data->col3.disks[i].endRadius;
  393.                             disks[i].material = data->col3.disks[i].material;
  394.                             disks[i].piece = data->col3.disks[i].piece;
  395.                             disks[i].lighting = data->col3.disks[i].lighting;
  396.                         }
  397.                     }
  398.                 }
  399.                 else
  400.                 {
  401.                     if(numLines > 0 && data->col3.lines)
  402.                     {
  403.                         data->col3.lines = (gtaCollisionLine_COL2 *)((gtaRwUInt32)data->col3.lines + (gtaRwUInt32)data - 4);
  404.                         lines = (gtaCollisionLine *)gtaMemAlloc(sizeof(gtaCollisionLine) * numLines);
  405.                         for(int i = 0; i < numLines; i++)
  406.                         {
  407.                             lines[i].start = data->col3.lines[i].start;
  408.                             lines[i].end = data->col3.lines[i].end;
  409.                             lines[i].startRadius = data->col3.lines[i].startRadius;
  410.                             lines[i].endRadius = data->col3.lines[i].endRadius;
  411.                         }
  412.                     }
  413.                 }
  414.                
  415.  
  416.                 /*if(numFaces > 0)
  417.                 {
  418.                     faces = (gtaCollisionTriangle *)gtaMemAlloc(sizeof(gtaCollisionTriangle) * numFaces);
  419.                     for(int i = 0; i < numFaces; i++)
  420.                     {
  421.                         faces[i].a = data->col3.
  422.                     }
  423.                 }
  424.                 if(data->col3.hasFaceGroups)
  425.                 {
  426.                     numFaceGroups;
  427.                 }
  428.                 if(data->col3.hasShadowMesh)
  429.                 {
  430.                     numShadowFaces = data->col3.numShadowFaces;
  431.                 }*/
  432.                 return true;
  433.             default:
  434.                 gtaRwErrorSet("Not supported version (%d) for Collision plugin", fourcc);
  435.                 break;
  436.             }
  437.         }
  438.         gtaMemFree(data);
  439.         Destroy();
  440.         return false;
  441.     }
  442.    
  443.     unsigned int GetStreamSize()
  444.     {
  445.         if(enabled)
  446.         {
  447.             unsigned int size = 12;
  448.             switch(fourcc)
  449.             {
  450.             case '3LOC':
  451.                 size += 120;
  452.                 if(vertices && numVertices > 0)
  453.                 {
  454.                     size += numVertices * 6;
  455.                     if(!((numVertices * 6) % 4))
  456.                         size += 2;
  457.                 }
  458.                 if(faces && numFaces > 0)
  459.                     size += numFaces * 8;
  460.                 if(numFaceGroups > 0 && faceGroups)
  461.                     size += 4 + numFaceGroups * 28;
  462.                 if(numSpheres > 0 && spheres)
  463.                     size += numSpheres * 20;
  464.                 if(numBoxes > 0 && boxes)
  465.                     size += numBoxes * 28;
  466.                 if(usesDisks)
  467.                 {
  468.                     if(numDisks && disks)
  469.                         size += numDisks * 36;
  470.                 }
  471.                 else
  472.                 {
  473.                     if(numLines && lines)
  474.                         size += numLines * 32;
  475.                 }
  476.                 if(numShadowVertices > 0 && shadowVertices)
  477.                 {
  478.                     size += numShadowVertices * 6;
  479.                     if(!((numShadowVertices * 6) % 4))
  480.                         size += 2;
  481.                 }
  482.                 if(shadowFaces && numShadowFaces > 0)
  483.                     size += numShadowFaces * 8;
  484.                 return size;
  485.             default:
  486.                 gtaRwErrorSet("Not supported version (%d) for Collision plugin", fourcc);
  487.                 return 0;
  488.             }
  489.         }
  490.         return 0;
  491.     }
  492. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement