Files
AVA2/tests/speedtree_forest_test.cpp
T
shenleiandClaude Opus 5.5 9077aa389a AVA2 client Windows parity passes (gr2 BitKnit2/64-bit, GamePhase handlers, python modules)
- gr2: BitKnit2 decompression, 64-bit LE files, compressed curves
- net: remaining GamePhase handlers, generated GC packet table
- python: event/chat/chr/minimap/guild/quest/messenger/shop-search and
  other native modules ported against the AVA2 Windows client
- render: D3D8 fixed-function lighting in Gr2ModelRenderer, terrain
  without baked shade; cape/NPC equipment attachment
- tests + audits updated; docs/PORT-PLAN.md and WINDOWS-PARITY.md

Batch 4 (render pipeline: background.*, app.RenderGame/UpdateGame,
camera bindings, main.cpp fake-content removal) is still in progress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:43:01 +09:00

170 lines
7.6 KiB
C++

#include "src/eter_render/SpeedTreeRT.h"
#include "src/eter_render/SpeedTreeForest.h"
#include "src/eter_render/MapObjectManager.h"
#include <cassert>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
using namespace ava2;
void TestPrtParsing() {
std::cout << "[Test] SpeedTree Property (.prt) Parsing..." << std::endl;
// Test synthetic PRT
std::string test_prt = "/tmp/test_tree.prt";
{
std::ofstream out(test_prt);
out << "YPRT\n"
<< "4240221483\n"
<< "propertyname\t\t\"treewillowoakfreesu\"\n"
<< "propertytype\t\t\"Tree\"\n"
<< "treefile\t\t\"d:/ymir work/palnts_2/all/ot/treewillowoakfreesu.spt\"\n"
<< "treesize\t\t\"1200.000000\"\n"
<< "treevariance\t\t\"50.000000\"\n";
}
SpeedTreeProperty prop;
bool ok = SpeedTreeProperty::LoadFromFile(test_prt, prop);
assert(ok && "Failed to parse synthetic .prt file");
assert(prop.crc == 4240221483 && "Property CRC mismatch");
assert(prop.property_name == "treewillowoakfreesu" && "Property name mismatch");
assert(prop.tree_size == 1200.0f && "Tree size mismatch");
assert(prop.tree_variance == 50.0f && "Tree variance mismatch");
std::cout << " -> PRT parsed successfully! (CRC: " << prop.crc << ", size: " << prop.tree_size << ")" << std::endl;
}
void TestSptSniffing() {
std::cout << "[Test] SpeedTree Binary (.spt) Sniffing..." << std::endl;
std::string test_spt = "build/dev/ymir work/plants/grass.spt";
SpeedTreeSptInfo info;
bool ok = SpeedTreeSptInfo::SniffSpt(test_spt, info);
if (ok) {
std::cout << " -> Sniffed grass.spt: ok=" << info.ok
<< ", bark='" << info.bark_texture
<< "', leaf='" << info.leaf_texture << "'" << std::endl;
assert(!info.leaf_texture.empty() && "Leaf texture must not be empty");
} else {
// Fallback test with synthetic memory buffer
std::vector<uint8_t> syn(64, 0);
std::string magic = "__IdvSpt_02_";
std::memcpy(syn.data() + 8, magic.data(), magic.size());
uint32_t mlen = magic.size();
std::memcpy(syn.data() + 4, &mlen, 4);
bool syn_ok = SpeedTreeSptInfo::SniffMemory(syn.data(), syn.size(), info);
assert(syn_ok && "Synthetic SPT sniffing must succeed");
std::cout << " -> Synthetic SPT sniffed successfully!" << std::endl;
}
}
void TestSpeedTreeModelGeneration() {
std::cout << "[Test] SpeedTree Model Geometry Generation..." << std::endl;
// 1. Procedural Oak Tree
auto oak = SpeedTreeModel::CreateProcedural("WillowOak", 1000.0f, "bark.dds", "leaves.dds");
assert(oak != nullptr && "Failed to create procedural tree model");
assert(!oak->IsGrassOrShrub() && "Oak should be identified as tree");
assert(oak->GetHeight() == 1000.0f && "Height mismatch");
auto draws = oak->BuildDrawCommands(100.0f, 50.0f, 200.0f, 45.0f, 1.0f, 0.0f);
assert(draws.size() == 2 && "Tree model must generate trunk and canopy draws");
const auto& trunk = draws[0];
const auto& canopy = draws[1];
assert(!trunk.positions.empty() && !trunk.indices.empty() && "Trunk must have geometry");
assert(!canopy.positions.empty() && !canopy.indices.empty() && "Canopy must have geometry");
assert(canopy.alpha_test && canopy.alpha_ref == 84 && "Canopy must have alpha test 84");
assert(canopy.cull_mode == 1 && "Canopy must be double sided");
std::cout << " -> Oak Tree generated: Trunk verts=" << trunk.positions.size() / 3
<< ", tris=" << trunk.indices.size() / 3
<< " | Canopy verts=" << canopy.positions.size() / 3
<< ", tris=" << canopy.indices.size() / 3 << std::endl;
// 2. Procedural Grass / Bush
auto grass = SpeedTreeModel::CreateProcedural("WildGrass", 120.0f, "", "plant_grass.dds");
assert(grass->IsGrassOrShrub() && "WildGrass should be identified as shrub/grass");
auto gdraws = grass->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
assert(gdraws.size() == 1 && "Grass model should only generate foliage draw");
assert(gdraws[0].cull_mode == 1 && gdraws[0].alpha_test && "Grass must be double-sided alpha tested");
std::cout << " -> Grass cluster generated: verts=" << gdraws[0].positions.size() / 3
<< ", tris=" << gdraws[0].indices.size() / 3 << std::endl;
}
void TestWindMotionSimulation() {
std::cout << "[Test] Wind Motion Simulation & Anchoring..." << std::endl;
auto tree = SpeedTreeModel::CreateProcedural("BlowingPine", 1000.0f, "bark.dds", "leaves.dds");
auto draws_t0 = tree->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 25.0f);
auto draws_t1 = tree->BuildDrawCommands(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.57f, 25.0f);
// Check base vertex (y ~ 0) stays anchored
float base_x0 = draws_t0[0].positions[0];
float base_x1 = draws_t1[0].positions[0];
assert(std::abs(base_x0 - base_x1) < 0.01f && "Tree trunk base must be firmly anchored at ground");
// Check top canopy vertex sways with wind (flex grows with height², so pick the
// highest canopy vertex; vertex 0 is the bottom edge of the lowest cluster).
const auto& canopy0 = draws_t0[1].positions;
size_t top = 0;
for (size_t i = 0; i < canopy0.size() / 3; ++i) {
if (canopy0[i * 3 + 1] > canopy0[top * 3 + 1]) top = i;
}
float top_x0 = canopy0[top * 3 + 0];
float top_x1 = draws_t1[1].positions[top * 3 + 0];
float delta_sway = std::abs(top_x0 - top_x1);
assert(delta_sway > 0.5f && "Tree canopy must visibly sway with wind");
std::cout << " -> Wind displacement at canopy tip: " << delta_sway << " units (sway verified!)" << std::endl;
}
void TestSpeedTreeForestSystem() {
std::cout << "[Test] SpeedTreeForest Management & Frustum Culling..." << std::endl;
SpeedTreeForest forest;
forest.ClearMainTree();
SpeedTreeProperty prop_oak{1001, "Oak", "ymir work/palnts_2/all/ot/treewillowoakfreesu.spt", 1200.0f, 0.0f};
SpeedTreeProperty prop_grass{1002, "Grass", "ymir work/plants/grass.spt", 100.0f, 0.0f};
forest.RegisterPropertyData(prop_oak);
forest.RegisterPropertyData(prop_grass);
// Place trees in front of camera
forest.CreateInstance(0.0f, 0.0f, 300.0f, 1001, 0.0f, 1.0f);
forest.CreateInstance(50.0f, 0.0f, 350.0f, 1002, 30.0f, 1.0f);
// Place one tree behind camera (Z = -500)
forest.CreateInstance(0.0f, 0.0f, -500.0f, 1001, 0.0f, 1.0f);
assert(forest.GetInstanceCount() == 3 && "Instance count mismatch");
forest.UpdateSystem(0.033f);
// Standard camera looking down +Z
float view[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
float proj[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, -1.0f, 0.0f
};
auto culled_draws = forest.BuildDrawCommands(view, proj, 0.0f, 50.0f, 0.0f, 2000.0f);
assert(!culled_draws.empty() && "Visible trees must produce draw commands");
// Front trees (1 tree = 2 draws, 1 grass = 1 draw -> 3 draws total)
assert(culled_draws.size() == 3 && "Behind-camera tree must be culled (3 draws expected)");
std::cout << " -> Forest generated " << culled_draws.size()
<< " culled draw commands for 2 visible instances (1 culled behind camera)!" << std::endl;
}
int main() {
std::cout << "=== Running SpeedTree & Forest Engine Tests ===" << std::endl;
TestPrtParsing();
TestSptSniffing();
TestSpeedTreeModelGeneration();
TestWindMotionSimulation();
TestSpeedTreeForestSystem();
std::cout << "=== ALL SpeedTree & Forest Tests PASSED (100%) ===" << std::endl;
return 0;
}