Skip to content

Commit

Permalink
Fixed movtex and macro factories
Browse files Browse the repository at this point in the history
  • Loading branch information
KiritoDv committed Sep 1, 2024
1 parent b956033 commit 2985786
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 47 deletions.
65 changes: 32 additions & 33 deletions src/factories/sm64/CollisionFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,26 @@ ExportResult SM64::CollisionHeaderExporter::Export(std::ostream &write, std::sha
ExportResult SM64::CollisionBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> data, std::string& entryName, YAML::Node &node, std::string* replacement ) {
const auto collision = std::static_pointer_cast<Collision>(data).get();

auto writer = LUS::BinaryWriter();
std::vector<int16_t> commands;

writer.Write((int16_t)COL_INIT());
commands.push_back((int16_t)COL_INIT());

// Vertices
if (collision->mVertices.size() > 0) {
writer.Write((int16_t)COL_VERTEX_INIT(collision->mVertices.size()));
commands.push_back((int16_t)COL_VERTEX_INIT(collision->mVertices.size()));
}
for (auto &vertex : collision->mVertices) {
writer.Write(vertex.x);
writer.Write(vertex.y);
writer.Write(vertex.z);
commands.push_back(vertex.x);
commands.push_back(vertex.y);
commands.push_back(vertex.z);
}

// Surfaces
for (auto &surface : collision->mSurfaces) {
// size check is probably not necessary here
if (surface.tris.size() > 0) {
writer.Write((int16_t)surface.surfaceType);
writer.Write((int16_t)surface.tris.size());
commands.push_back((int16_t)surface.surfaceType);
commands.push_back((int16_t)surface.tris.size());
}
bool hasForce = false;
switch (surface.surfaceType) {
Expand All @@ -294,55 +294,54 @@ ExportResult SM64::CollisionBinaryExporter::Export(std::ostream &write, std::sha
break;
}
for (auto &tri : surface.tris) {
writer.Write(tri.x);
writer.Write(tri.y);
writer.Write(tri.z);
commands.push_back(tri.x);
commands.push_back(tri.y);
commands.push_back(tri.z);
if (hasForce) {
writer.Write(tri.force);
commands.push_back(tri.force);
}
}
}
if (collision->mSurfaces.size()) {
writer.Write((int16_t)COL_TRI_STOP());
if (!collision->mSurfaces.empty()) {
commands.push_back((int16_t)COL_TRI_STOP());
}

// Special Objects
if (collision->mSpecialObjects.size() > 0) {
writer.Write((int16_t)TERRAIN_LOAD_OBJECTS);
writer.Write((int16_t)collision->mSpecialObjects.size());
commands.push_back((int16_t)TERRAIN_LOAD_OBJECTS);
commands.push_back((int16_t)collision->mSpecialObjects.size());
}
for (auto &specialObject : collision->mSpecialObjects) {
writer.Write((int16_t)specialObject.presetId);
writer.Write(specialObject.x);
writer.Write(specialObject.y);
writer.Write(specialObject.z);
commands.push_back((int16_t)specialObject.presetId);
commands.push_back(specialObject.x);
commands.push_back(specialObject.y);
commands.push_back(specialObject.z);
for (int16_t extraParam : specialObject.extraParams) {
writer.Write(extraParam);
commands.push_back(extraParam);
}
}

// Environment Region Boxes
if (collision->mEnvRegionBoxes.size() > 0) {
writer.Write((int16_t)TERRAIN_LOAD_ENVIRONMENT);
writer.Write((int16_t)collision->mEnvRegionBoxes.size());
commands.push_back((int16_t)TERRAIN_LOAD_ENVIRONMENT);
commands.push_back((int16_t)collision->mEnvRegionBoxes.size());
}
for (auto &envRegionBox : collision->mEnvRegionBoxes) {
writer.Write(envRegionBox.id);
writer.Write(envRegionBox.x1);
writer.Write(envRegionBox.z1);
writer.Write(envRegionBox.x2);
writer.Write(envRegionBox.z2);
writer.Write(envRegionBox.height);
commands.push_back(envRegionBox.id);
commands.push_back(envRegionBox.x1);
commands.push_back(envRegionBox.z1);
commands.push_back(envRegionBox.x2);
commands.push_back(envRegionBox.z2);
commands.push_back(envRegionBox.height);
}

std::vector<char> buffer = writer.ToVector();
writer.Close();
commands.push_back((int16_t) COL_END());

LUS::BinaryWriter output = LUS::BinaryWriter();
WriteHeader(output, Torch::ResourceType::Collision, 0);

output.Write(static_cast<uint32_t>(buffer.size()));
output.Write(buffer.data(), buffer.size());
output.Write(static_cast<uint32_t>(commands.size()));
output.Write((char*) commands.data(), commands.size() * sizeof(int16_t));
output.Finish(write);
output.Close();
return std::nullopt;
Expand Down
4 changes: 2 additions & 2 deletions src/factories/sm64/MacroFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ ExportResult SM64::MacroBinaryExporter::Export(std::ostream &write, std::shared_

WriteHeader(writer, Torch::ResourceType::MacroObject, 0);

writer.Write((uint32_t)macro->mMacroData.size());
writer.Write((uint32_t) macro->mMacroData.size());

for (auto &object : macro->mMacroData) {
writer.Write(static_cast<int16_t>(object.preset));
writer.Write(object.yaw);
writer.Write((int16_t) std::round(object.yaw * 1.41));
writer.Write(object.posX);
writer.Write(object.posY);
writer.Write(object.posZ);
Expand Down
47 changes: 36 additions & 11 deletions src/factories/sm64/MovtexFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,44 @@ ExportResult SM64::MovtexBinaryExporter::Export(std::ostream &write, std::shared

WriteHeader(writer, Torch::ResourceType::Movtex, 0);

// May want extra info, but importer should just be mMovtexData with an extra 0 (or 2) at the end
// if (movtex->mIsQuad) {
// writer.Write(0);
// writer.Write(0);
// } else {
// writer.Write(movtex->mVertexCount);
// writer.Write((uint32_t)(movtex->mHasColor ? 1 : 0));
// }

for (auto datum : movtex->mMovtexData) {
writer.Write(datum);
std::vector<int16_t> buffer;

if (movtex->mIsQuad) {
auto numLists = movtex->mMovtexData.at(0);

buffer.push_back(numLists);
buffer.push_back(0);

for (uint32_t i = 0; i < numLists; ++i) {
for(size_t j = 0; j < 14; j++){
buffer.push_back(movtex->mMovtexData.at(i * 14 + (j + 1)));
}

buffer.push_back(0);
}
} else {
buffer.push_back(movtex->mMovtexData.at(0));

if (movtex->mHasColor) {
for (uint32_t i = 0; i < movtex->mVertexCount; ++i) {
for(size_t j = 0; j < 8; j++){
buffer.push_back(movtex->mMovtexData.at(i * 8 + (j + 1)));
}
}
} else {
for (uint32_t i = 0; i < movtex->mVertexCount; ++i) {
for(size_t j = 0; j < 5; j++){
buffer.push_back(movtex->mMovtexData.at(i * 5 + (j + 1)));
}
}
}

buffer.push_back(0);
}

writer.Write((uint32_t) buffer.size());
writer.Write((char*) buffer.data(), buffer.size() * sizeof(int16_t));

writer.Finish(write);
return std::nullopt;
}
Expand Down
3 changes: 2 additions & 1 deletion src/factories/sm64/MovtexQuadFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ ExportResult SM64::MovtexQuadBinaryExporter::Export(std::ostream &write, std::sh
writer.Write(hash);
} else {
SPDLOG_WARN("Could not find movtex at 0x{:X}", quad.second);
writer.Write(0);
throw std::runtime_error("PANIC");
}
}
write << "},\n";
}

writer.Finish(write);
Expand Down

0 comments on commit 2985786

Please sign in to comment.