Files
metin2-SDK/examples/tree_and_character_demo.cpp
T

240 lines
12 KiB
C++

#include <speedtree/speedtree.h>
#include <speedtree/foliage_shaders.h>
#include <gr2_render/gr2_render.h>
#include <eterpack/eterpack.h>
#include <proto/proto.h>
#include <effect/effect.h>
#include <terrain/terrain.h>
#include <character/character.h>
#include <property/property.h>
#include <property/property_manager.h>
#include <texture/texture.h>
#include <network/network.h>
#include <spatial/spatial.h>
#include <ui_core/ui_core.h>
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
std::cout << "====================================================================\n";
std::cout << " Metin2 Cross-Platform Independent SDK Suite (Full Verification)\n";
std::cout << "====================================================================\n\n";
// 1. SpeedTree High-Fidelity Tree & Wind Dynamics
std::cout << "[1] SpeedTree Procedural Foliage Generation & Wind Physics\n";
auto tree = speedtree::SpeedTreeModel::CreateProcedural(
"AncientOak", 1200.0f, "ymir work/tree/oak_bark.dds", "ymir work/tree/oak_leaf.dds"
);
if (tree) {
std::cout << " Tree Created: " << tree->GetName() << "\n";
std::cout << " Height: " << tree->GetHeight() << " units, Radius: " << tree->GetRadius() << " units\n";
const auto& trunk = tree->GetTrunkMesh();
const auto& canopy = tree->GetLeafMesh();
std::cout << " Trunk Geometry: " << trunk.vertices.size() << " vertices, "
<< trunk.indices.size() / 3 << " triangles\n";
std::cout << " Canopy Foliage: " << canopy.vertices.size() << " vertices, "
<< canopy.indices.size() / 3 << " triangles\n";
speedtree::WindParameters wind;
wind.strength = 1.2f; // Gusty breeze
wind.direction[0] = 1.0f;
wind.direction[1] = 0.0f;
wind.direction[2] = 0.5f;
std::vector<float> anim_pos;
std::vector<float> anim_norm;
tree->EvaluateCpuTransformedVertices(canopy, anim_pos, anim_norm, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, wind);
if (!anim_pos.empty()) {
std::cout << " CPU Evaluated Transformed Canopy Vertices: " << anim_pos.size() / 3 << " points\n";
}
std::string shader_code = speedtree::GetFoliageVertexShaderGLSL();
std::cout << " Shader Code Definition: " << shader_code.substr(0, 42) << "...\n\n";
}
// 2. Granny2 Mesh & Skeleton Render Instance SDK
std::cout << "[2] Granny2 (GR2) Skeletal Animation & Attachment SDK\n";
gr2_render::Gr2MeshDraw draw_cmd;
draw_cmd.world[0] = 1.0f; draw_cmd.world[5] = 1.0f; draw_cmd.world[10] = 1.0f; draw_cmd.world[15] = 1.0f;
draw_cmd.texture0 = "d:/ymir work/pc/warrior/warrior_novice.dds";
std::cout << " Gr2MeshDraw Standard Pipeline Unit: AlphaTest=" << draw_cmd.alpha_test
<< ", DoubleSided=" << draw_cmd.double_sided << ", Tex=" << draw_cmd.texture0 << "\n\n";
// 3. EterPack VFS
std::cout << "[3] EterPack Virtual File System (TEA + MiniLZO)\n";
uint32_t tea_key[4] = { 0x12345678, 0x9abcdef0, 0x0fedcba9, 0x87654321 };
std::string test_data = "SpeedTree & Metin2 CrossPlatform VFS TEA Test Data!!";
std::span<const uint8_t> data_span(reinterpret_cast<const uint8_t*>(test_data.data()), test_data.size());
auto encrypted = eterpack::tea_encrypt_bytes(data_span, tea_key);
auto decrypted = eterpack::tea_decrypt_bytes(encrypted, tea_key);
std::string recovered(reinterpret_cast<const char*>(decrypted.data()), test_data.size());
std::cout << " EterPack TEA Encrypt/Decrypt Verified: " << recovered << "\n\n";
// 4. Proto SDK
std::cout << "[4] Proto SDK (Item/Mob Binary & Alignment)\n";
proto::ItemProtoRecord item_sword{};
item_sword.vnum = 10;
item_sword.name = "Sword+0";
item_sword.type = 1;
item_sword.size = 2;
std::cout << " Item Proto Loaded: Vnum=" << item_sword.vnum << ", Name=" << item_sword.name
<< ", Inventory Size=" << (int)item_sword.size << " grid slots.\n\n";
// 5. Effect & Time Events SDK
std::cout << "[5] Effect & Dynamic Particle SDK (MSE / Time Curve Interpolation)\n";
std::vector<effect::TimeEvent> events = { {0.0f, 10.0f}, {1.0f, 20.0f}, {3.0f, 40.0f} };
float val = effect::interpolate_time_event(events, 0.5f);
std::cout << " Time Event Keyframe Interpolated at t=0.5s: " << val << " (Expected 15.0)\n\n";
// 6. Terrain & Water SDK
std::cout << "[6] Terrain & Water SDK (Heightmap Sampling & Water Simulation)\n";
metin2::terrain::TerrainHeightmap terrain_map;
std::vector<uint16_t> fake_heights(131 * 131, 2000); // flat 20.00m
fake_heights[65 * 131 + 65] = 5000; // hill peak 50.00m
terrain_map.LoadFromBuffer(fake_heights.data(), fake_heights.size());
float sample_h = terrain_map.GetHeightAtWorld(12800.0f * 0.5f, 12800.0f * 0.5f);
std::cout << " - Sampled World Center Height: " << sample_h << " meters (Hill Peak=50m)\n";
metin2::terrain::WaterManager water;
water.GenerateProceduralWater(10.0f, -1000.0f, 1000.0f, -1000.0f, 1000.0f, 60.0f);
auto water_cmd = water.BuildDrawCommand(0.5f);
std::cout << " - Water Dynamic Mesh: " << water_cmd.positions.size() / 3 << " vertices, "
<< water_cmd.indices.size() / 3 << " triangles\n\n";
// 7. Character & MSM SDK
std::cout << "[7] Character & MSM SDK (Actor Assembly & Socket Calculation)\n";
const std::string msm_text =
"ScriptType RaceDataFile\n"
"BaseActorData\n"
"{\n"
" BaseModelFileName \"d:/ymir work/pc/warrior/warrior_novice.gr2\"\n"
"}\n"
"Group ShapeData\n"
"{\n"
" ShapeDataCount 1\n"
" Group ShapeData00\n"
" {\n"
" ShapeIndex 0\n"
" Model \"warrior_novice.gr2\"\n"
" SourceSkin \"warrior_novice.dds\"\n"
" TargetSkin \"warrior_novice_blue.dds\"\n"
" }\n"
"}\n";
auto race = std::make_shared<metin2::character::MsmData>(0);
race->LoadMsmFromString(msm_text);
metin2::character::CharacterAssembly actor(race);
actor.SetArmorShape(0);
actor.SetWeapon(metin2::character::PART_WEAPON, "d:/ymir work/weapon/sword.gr2");
std::cout << " - Actor Assembled: Parts=" << actor.GetAssembledParts().size() << ", ArmorShape=0\n\n";
std::cout << "[8] Property & Object SDK (YPRT & OBB SAT Collision)\n";
const std::string prb_text =
"YPRT\n"
"12345678\n"
"buildingfile \"d:/ymir work/zone/temple.gr2\"\n"
"propertyname \"ancient_temple\"\n"
"propertytype \"Building\"\n";
metin2::property::PropertyManager prop_mgr;
prop_mgr.RegisterFromMemory("zone/temple.prb", prb_text.data(), prb_text.size());
auto temple_prop = prop_mgr.GetByCRC(12345678);
std::cout << " - YPRT Property Registered: " << (temple_prop ? temple_prop->GetData().property_name : "None") << "\n";
metin2::property::OrientedBoundingBox temple_obb;
temple_obb.center[0] = 500.0f; temple_obb.center[2] = 500.0f;
temple_obb.half_extents[0] = 100.0f; temple_obb.half_extents[2] = 100.0f;
float push_nx = 0.0f, push_nz = 0.0f, pen_dist = 0.0f;
temple_obb.GetPenetrationAndNormal(590.0f, 500.0f, 20.0f, push_nx, push_nz, pen_dist);
std::cout << " - OBB Collision Pushout: Penetration=" << pen_dist << " units, Normal=("
<< push_nx << ", " << push_nz << ")\n\n";
// 9. Texture & SubImage SDK
std::cout << "[9] Texture & SubImage SDK (DXT Soft-Decoder & 9-Slice Mesh)\n";
const std::string sub_text =
"title subImage\n"
"image \"d:/ymir work/ui/public.dds\"\n"
"left 0\n"
"top 0\n"
"right 64\n"
"bottom 64\n";
metin2::texture::SubImage sub_img;
if (sub_img.LoadFromString(sub_text)) {
auto uv = sub_img.ComputeUV(256, 256);
auto mesh = sub_img.BuildNineSliceMesh(0.0f, 0.0f, 300.0f, 150.0f, 16.0f, 16.0f, 16.0f, 16.0f, 256, 256);
std::cout << " - SubImage UV rect: (" << uv.u0 << ", " << uv.v0 << ") to (" << uv.u1 << ", " << uv.v1 << ")\n";
std::cout << " - 9-Slice UI Window Mesh: " << mesh.vertices.size() << " vertices, "
<< mesh.indices.size() / 3 << " triangles\n\n";
}
// 10. Network Protocol & FSM SDK
std::cout << "[10] Network Protocol & State Machine SDK (Phase FSM + Rolling Key + Event Dispatch)\n";
metin2::network::NetworkClient net_client;
net_client.SetPhase(metin2::network::ConnectionPhase::Handshake);
std::cout << " - Connection State: " << net_client.GetPhaseName() << "\n";
// Simulate server Handshake
std::vector<uint8_t> hs_packet = { metin2::network::HEADER_GC_HANDSHAKE, 0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0 };
net_client.SetOnHandshake([](uint32_t s_time) {
std::cout << " - Handshake Synchronized! ServerTime=" << s_time << "\n";
});
net_client.PushReceivedData(hs_packet.data(), hs_packet.size());
net_client.SetPhase(metin2::network::ConnectionPhase::Auth);
net_client.SendLogin("HeroPlayer", "Password123");
auto login_bytes = net_client.PopSendData();
std::cout << " - Sent Encrypted Login Packet: " << login_bytes.size() << " bytes\n\n";
// 11. Spatial Loose Octree & Frustum Culling SDK
std::cout << "[11] Spatial Loose Octree & Frustum Culling SDK\n";
metin2::spatial::AABB scene_bounds(metin2::spatial::Vector3(-50000, -5000, -50000), metin2::spatial::Vector3(50000, 10000, 50000));
metin2::spatial::LooseOctree octree(scene_bounds, 2.0f);
// Register World Objects
octree.Insert(1001, metin2::spatial::SpatialObjectType::Building, metin2::spatial::AABB(metin2::spatial::Vector3(-200, 0, 500), metin2::spatial::Vector3(200, 400, 900)));
octree.Insert(2001, metin2::spatial::SpatialObjectType::Tree, metin2::spatial::AABB(metin2::spatial::Vector3(-50, 0, 300), metin2::spatial::Vector3(50, 600, 400)));
octree.Insert(3001, metin2::spatial::SpatialObjectType::Monster, metin2::spatial::AABB(metin2::spatial::Vector3(-30, 0, 150), metin2::spatial::Vector3(30, 100, 210)));
octree.Insert(4001, metin2::spatial::SpatialObjectType::Tree, metin2::spatial::AABB(metin2::spatial::Vector3(0, 0, -500), metin2::spatial::Vector3(50, 600, -400))); // Behind camera
// Camera at (0, 50, 0) looking along +Z
metin2::spatial::Vector3 cam_eye(0.0f, 50.0f, 0.0f);
auto frustum = metin2::spatial::Frustum::CreatePerspective(cam_eye, metin2::spatial::Vector3(0, 0, 1), metin2::spatial::Vector3(0, 1, 0), 60.0f, 16.0f / 9.0f, 1.0f, 3000.0f);
auto visible_objs = octree.QueryFrustum(frustum, cam_eye, true);
std::cout << " - Registered Scene Objects: " << octree.Size() << "\n";
std::cout << " - Frustum Query Visible Objects (Sorted by Distance): " << visible_objs.size() << "\n";
for (const auto& vis : visible_objs) {
std::cout << " * Visible ID=" << vis.id << " (Type=" << (int)vis.type << "), Distance=" << (int)vis.distance << " units\n";
}
std::cout << "\n";
// 12. UI Core Reactive Node Tree & Grid SDK
std::cout << "[12] UI Core Reactive Node Tree & Inventory Grid SDK (No Python Dependency)\n";
metin2::ui_core::UIManager ui_mgr(1920.0f, 1080.0f);
auto inv_window = std::make_shared<metin2::ui_core::UIWindow>();
inv_window->SetName("Inventory");
inv_window->SetSize(220.0f, 340.0f);
inv_window->SetAnchor(metin2::ui_core::UIAnchor::Center);
auto grid = std::make_shared<metin2::ui_core::UIGrid>(5, 9, 32.0f, 2.0f);
grid->SetPosition(10.0f, 30.0f);
grid->SetItemAt(0, 0, 10, 1); // Sword in (0, 0)
grid->SetItemAt(2, 4, 50300, 5); // Skill book in (2, 4)
inv_window->AddChild(grid);
ui_mgr.AddElement(inv_window);
ui_mgr.UpdateLayout();
auto ui_batches = ui_mgr.GenerateRenderBatches();
std::cout << " - Root UI Hierarchy Layout Updated (Window Global Rect: "
<< (int)inv_window->GetGlobalRect().x << ", " << (int)inv_window->GetGlobalRect().y << ")\n";
std::cout << " - Inventory Slot (0,0) HasItem: " << grid->GetSlot(0, 0)->HasItem()
<< ", Vnum=" << grid->GetSlot(0, 0)->GetItemVnum() << "\n";
std::cout << " - UI Render Batches Built: " << ui_batches.size() << " draw calls\n\n";
std::cout << "====================================================================\n";
std::cout << " All 12 Standalone Metin2 C++20 SDKs Operational and Verified!\n";
std::cout << "====================================================================\n";
return 0;
}