Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "Extension.h"
- #include "Stream.h"
- #include "RwTypes.h"
- #include "StdInc.h"
- struct gtaCollisionSphere_COLL
- {
- gtaRwReal radius;
- gtaRwV3d center;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 _pad[2];
- };
- struct gtaCollisionSphere_COL2
- {
- gtaRwV3d center;
- gtaRwReal radius;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 lighting;
- gtaRwUInt8 _pad;
- };
- struct gtaCollisionSphere
- {
- gtaRwV3d center;
- gtaRwReal radius;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 lighting;
- };
- struct gtaCollisionBox_COLL
- {
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 _pad[2];
- };
- struct gtaCollisionBox_COL2
- {
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 lighting;
- gtaRwUInt8 _pad;
- };
- struct gtaCollisionBox
- {
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 lighting;
- };
- struct gtaCollisionFaceGroup
- {
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwUInt16 startFace;
- gtaRwUInt16 endFace;
- };
- struct gtaCollisionLine_COLL
- {
- gtaRwV3d start;
- gtaRwV3d end;
- };
- struct gtaCollisionLine_COL2
- {
- gtaRwV3d start;
- gtaRwReal startRadius; // unused
- gtaRwV3d end;
- gtaRwReal endRadius; // unused
- };
- struct gtaCollisionLine
- {
- gtaRwV3d start;
- gtaRwReal startRadius; // unused
- gtaRwV3d end;
- gtaRwReal endRadius; // unused
- };
- struct gtaCollisionDisk
- {
- gtaRwV3d start;
- gtaRwReal startRadius;
- gtaRwUInt8 material;
- gtaRwUInt8 piece;
- gtaRwUInt8 lighting;
- gtaRwUInt8 _pad;
- gtaRwV3d end;
- gtaRwReal endRadius;
- };
- struct gtaCollisionTriangle_COLL
- {
- gtaRwUInt32 a, b, c;
- gtaRwUInt8 material;
- gtaRwUInt8 unused;
- gtaRwUInt8 _pad[2];
- };
- struct gtaCollisionTriangle_COL2
- {
- gtaRwUInt16 a, b, c;
- gtaRwUInt8 material;
- gtaRwUInt8 light;
- };
- struct gtaCollisionTriangle
- {
- gtaRwUInt16 a, b, c;
- gtaRwUInt8 material;
- gtaRwUInt8 light;
- };
- struct gtaCollisionVertex_COLL
- {
- gtaRwReal x, y, z;
- };
- struct gtaCollisionVertex_COL2
- {
- gtaRwInt16 x, y, z;
- };
- struct gtaCollisionVertex
- {
- gtaRwReal x, y, z;
- };
- enum gtaCollisionVersion
- {
- GTA_COLL = 0,
- GTA_COL2 = 1,
- GTA_COL3 = 2,
- GTA_COL4 = 3
- };
- struct gtaCollisionFile_COLL
- {
- gtaRwReal radius;
- gtaRwV3d center;
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwUInt32 numSpheres;
- };
- struct gtaCollisionFile_COL2
- {
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwV3d center;
- gtaRwReal radius;
- gtaRwUInt16 numSpheres;
- gtaRwUInt16 numBoxes;
- gtaRwUInt16 numFaces;
- gtaRwUInt8 numLines;
- gtaRwUInt8 _pad;
- union{
- gtaRwUInt32 flags;
- struct{
- gtaRwUInt32 usesDisks : 1;
- gtaRwUInt32 isNotEmpty : 1;
- gtaRwUInt32 flags_b3 : 1;
- gtaRwUInt32 hasFaceGroups : 1;
- gtaRwUInt32 hasShadowMesh : 1;
- };
- };
- gtaCollisionSphere_COL2 *spheres;
- gtaCollisionBox_COL2 *boxes;
- union
- {
- gtaCollisionLine_COL2 *lines;
- gtaCollisionDisk *disks;
- };
- gtaCollisionVertex_COL2 *vertices;
- gtaCollisionTriangle_COL2 *faces;
- void *facePlanes; // not read
- };
- struct gtaCollisionFile_COL3 : public gtaCollisionFile_COL2
- {
- gtaRwUInt32 numShadowFaces;
- gtaCollisionVertex_COL2 *shadowVertices;
- gtaCollisionTriangle_COL2 *shadowFaces;
- };
- struct gtaCollisionFile_COL4 : public gtaCollisionFile_COL3
- {
- gtaRwUInt32 unknown; // not used
- };
- struct gtaCollisionFile
- {
- gtaRwChar modelName[20];
- gtaRwInt8 _pad[2];
- gtaRwInt16 modelId;
- union
- {
- gtaCollisionFile_COLL coll;
- gtaCollisionFile_COL2 col2;
- gtaCollisionFile_COL3 col3;
- gtaCollisionFile_COL4 col4;
- };
- };
- struct gtaClumpCollisionPlugin : public gtaRwExtension
- {
- gtaRwUInt32 fourcc;
- gtaRwUInt32 size;
- gtaRwChar modelName[20];
- gtaRwInt16 modelId;
- gtaRwV3d min;
- gtaRwV3d max;
- gtaRwV3d center;
- gtaRwReal radius;
- gtaRwUInt16 numSpheres;
- gtaRwUInt16 numBoxes;
- gtaRwUInt16 numFaces;
- gtaRwUInt32 numVertices;
- union
- {
- gtaRwUInt8 numLines;
- gtaRwUInt8 numDisks;
- };
- gtaRwBool usesDisks;
- gtaCollisionSphere *spheres;
- gtaCollisionBox *boxes;
- union
- {
- gtaCollisionLine *lines;
- gtaCollisionDisk *disks;
- };
- gtaCollisionVertex *vertices;
- gtaCollisionTriangle *faces;
- gtaRwUInt32 numFaceGroups;
- gtaCollisionFaceGroup *faceGroups;
- gtaRwUInt32 numShadowFaces;
- gtaRwUInt32 numShadowVertices;
- gtaCollisionVertex *shadowVertices;
- gtaCollisionTriangle *shadowFaces;
- gtaClumpCollisionPlugin()
- {
- memset(this, 0, sizeof(gtaClumpCollisionPlugin));
- }
- void Initialise(gtaCollisionVersion Version, gtaRwChar *Name, gtaRwInt32 ModelId, gtaRwUInt32 NumSpheres, gtaRwUInt32 NumBoxes,
- gtaRwUInt32 NumLines, gtaRwUInt32 NumVertices, gtaRwUInt32 NumFaces, gtaRwUInt32 NumShadowVertices, gtaRwUInt32 NumShadowFaces)
- {
- Destroy();
- switch(Version)
- {
- case GTA_COL3:
- fourcc = '3LOC';
- default:
- gtaRwErrorSet("Not supported version (%d) for Collision plugin", fourcc);
- return;
- }
- enabled = true;
- modelId = ModelId;
- strcpy(modelName, Name);
- numSpheres = NumSpheres;
- numBoxes = NumBoxes;
- numLines = NumLines;
- numVertices = NumVertices;
- numFaces = NumFaces;
- numShadowFaces = NumShadowFaces;
- numShadowVertices = NumShadowVertices;
- if(NumSpheres > 0)
- spheres = (gtaCollisionSphere *)gtaMemAlloc(NumSpheres * sizeof(gtaCollisionSphere));
- if(NumBoxes > 0)
- boxes = (gtaCollisionBox *)gtaMemAlloc(NumBoxes * sizeof(gtaCollisionBox));
- if(NumLines > 0)
- lines = (gtaCollisionLine *)gtaMemAlloc(NumLines * sizeof(gtaCollisionLine));
- if(NumVertices > 0)
- vertices = (gtaCollisionVertex *)gtaMemAlloc(NumVertices * sizeof(gtaCollisionVertex));
- if(NumFaces > 0)
- faces = (gtaCollisionTriangle *)gtaMemAlloc(NumFaces * sizeof(gtaCollisionTriangle));
- if(NumShadowFaces > 0)
- shadowFaces = (gtaCollisionTriangle *)gtaMemAlloc(NumShadowFaces * sizeof(gtaCollisionTriangle));
- if(NumShadowVertices > 0)
- shadowVertices = (gtaCollisionVertex *)gtaMemAlloc(NumSpheres * sizeof(gtaCollisionVertex));
- }
- void Destroy()
- {
- if(spheres)
- free(spheres);
- if(boxes)
- free(boxes);
- if(lines)
- free(lines);
- if(vertices)
- free(vertices);
- if(faces)
- free(faces);
- if(faceGroups)
- free(faceGroups);
- if(shadowVertices)
- free(shadowVertices);
- if(shadowFaces)
- free(shadowFaces);
- memset(this, 0, sizeof(gtaClumpCollisionPlugin));
- }
- bool StreamWrite(gtaRwStream *stream)
- {
- if(enabled)
- {
- if(!gtaRwStreamWriteVersionedChunkHeader(stream, gtaID_COLLISIONPLUGIN, 0, gtaRwVersion, gtaRwBuild))
- return false;
- }
- return true;
- }
- bool StreamRead(gtaRwStream *stream)
- {
- Destroy();
- if(gtaRwStreamRead(stream, &fourcc, 8) != 8)
- return false;
- gtaCollisionFile *data = (gtaCollisionFile*)gtaMemAlloc(size);
- if(gtaRwStreamRead(stream, data, size) == size)
- {
- strcpy(modelName, data->modelName);
- modelId = data->modelId;
- switch(fourcc)
- {
- case '3LOC':
- enabled = true;
- min = data->col3.min;
- max = data->col3.max;
- center = data->col3.center;
- radius = data->col3.radius;
- numSpheres = data->col3.numSpheres;
- numBoxes = data->col3.numBoxes;
- numLines = data->col3.numLines;
- numFaces = data->col3.numFaces;
- // read spheres
- if(numSpheres > 0 && data->col3.spheres)
- {
- data->col3.spheres = (gtaCollisionSphere_COL2 *)((gtaRwUInt32)data->col3.spheres + (gtaRwUInt32)data - 4);
- spheres = (gtaCollisionSphere *)gtaMemAlloc(sizeof(gtaCollisionSphere) * numSpheres);
- for(int i = 0; i < numSpheres; i++)
- {
- spheres[i].center = data->col3.spheres[i].center;
- spheres[i].radius = data->col3.spheres[i].radius;
- spheres[i].material = data->col3.spheres[i].material;
- spheres[i].piece = data->col3.spheres[i].piece;
- spheres[i].lighting = data->col3.spheres[i].lighting;
- }
- }
- // read boxes
- if(numBoxes > 0 && data->col3.boxes)
- {
- data->col3.boxes = (gtaCollisionBox_COL2 *)((gtaRwUInt32)data->col3.boxes + (gtaRwUInt32)data - 4);
- boxes = (gtaCollisionBox *)gtaMemAlloc(sizeof(gtaCollisionBox) * numBoxes);
- for(int i = 0; i < numBoxes; i++)
- {
- boxes[i].min = data->col3.boxes[i].min;
- boxes[i].max = data->col3.boxes[i].max;
- boxes[i].material = data->col3.boxes[i].material;
- boxes[i].piece = data->col3.boxes[i].piece;
- boxes[i].lighting = data->col3.boxes[i].lighting;
- }
- }
- // read lines or disks
- if(data->col3.usesDisks)
- {
- if(numDisks > 0 && data->col3.disks)
- {
- data->col3.disks = (gtaCollisionDisk *)((gtaRwUInt32)data->col3.disks + (gtaRwUInt32)data - 4);
- disks = (gtaCollisionDisk *)gtaMemAlloc(sizeof(gtaCollisionBox) * numDisks);
- for(int i = 0; i < numDisks; i++)
- {
- disks[i].start = data->col3.disks[i].start;
- disks[i].end = data->col3.disks[i].end;
- disks[i].startRadius = data->col3.disks[i].startRadius;
- disks[i].endRadius = data->col3.disks[i].endRadius;
- disks[i].material = data->col3.disks[i].material;
- disks[i].piece = data->col3.disks[i].piece;
- disks[i].lighting = data->col3.disks[i].lighting;
- }
- }
- }
- else
- {
- if(numLines > 0 && data->col3.lines)
- {
- data->col3.lines = (gtaCollisionLine_COL2 *)((gtaRwUInt32)data->col3.lines + (gtaRwUInt32)data - 4);
- lines = (gtaCollisionLine *)gtaMemAlloc(sizeof(gtaCollisionLine) * numLines);
- for(int i = 0; i < numLines; i++)
- {
- lines[i].start = data->col3.lines[i].start;
- lines[i].end = data->col3.lines[i].end;
- lines[i].startRadius = data->col3.lines[i].startRadius;
- lines[i].endRadius = data->col3.lines[i].endRadius;
- }
- }
- }
- /*if(numFaces > 0)
- {
- faces = (gtaCollisionTriangle *)gtaMemAlloc(sizeof(gtaCollisionTriangle) * numFaces);
- for(int i = 0; i < numFaces; i++)
- {
- faces[i].a = data->col3.
- }
- }
- if(data->col3.hasFaceGroups)
- {
- numFaceGroups;
- }
- if(data->col3.hasShadowMesh)
- {
- numShadowFaces = data->col3.numShadowFaces;
- }*/
- return true;
- default:
- gtaRwErrorSet("Not supported version (%d) for Collision plugin", fourcc);
- break;
- }
- }
- gtaMemFree(data);
- Destroy();
- return false;
- }
- unsigned int GetStreamSize()
- {
- if(enabled)
- {
- unsigned int size = 12;
- switch(fourcc)
- {
- case '3LOC':
- size += 120;
- if(vertices && numVertices > 0)
- {
- size += numVertices * 6;
- if(!((numVertices * 6) % 4))
- size += 2;
- }
- if(faces && numFaces > 0)
- size += numFaces * 8;
- if(numFaceGroups > 0 && faceGroups)
- size += 4 + numFaceGroups * 28;
- if(numSpheres > 0 && spheres)
- size += numSpheres * 20;
- if(numBoxes > 0 && boxes)
- size += numBoxes * 28;
- if(usesDisks)
- {
- if(numDisks && disks)
- size += numDisks * 36;
- }
- else
- {
- if(numLines && lines)
- size += numLines * 32;
- }
- if(numShadowVertices > 0 && shadowVertices)
- {
- size += numShadowVertices * 6;
- if(!((numShadowVertices * 6) % 4))
- size += 2;
- }
- if(shadowFaces && numShadowFaces > 0)
- size += numShadowFaces * 8;
- return size;
- default:
- gtaRwErrorSet("Not supported version (%d) for Collision plugin", fourcc);
- return 0;
- }
- }
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement