Files

252 lines
7.4 KiB
C++

#include "effect/effect.h"
#include <iostream>
#include <cassert>
#include <cmath>
#include <filesystem>
namespace fs = std::filesystem;
static int g_failures = 0;
#define TEST_CHECK(cond) \
do { \
if (!(cond)) { \
std::cerr << "FAIL [" << __FILE__ << ":" << __LINE__ << "]: " #cond << std::endl; \
++g_failures; \
} \
} while(0)
void test_math_and_time_events() {
std::cout << "[Test] Math & TimeEvent interpolation..." << std::endl;
effect::Vec3 v1{1.0f, 2.0f, 3.0f};
effect::Vec3 v2{4.0f, 5.0f, 6.0f};
effect::Vec3 v3 = v1 + v2;
TEST_CHECK(v3.x == 5.0f && v3.y == 7.0f && v3.z == 9.0f);
float d = effect::Vec3::dot({1, 0, 0}, {0, 1, 0});
TEST_CHECK(std::abs(d) < 1e-5f);
effect::Vec3 c = effect::Vec3::cross({1, 0, 0}, {0, 1, 0});
TEST_CHECK(std::abs(c.x) < 1e-5f && std::abs(c.y) < 1e-5f && std::abs(c.z - 1.0f) < 1e-5f);
std::vector<effect::TimeEvent> events = {
{0.0f, 10.0f},
{1.0f, 20.0f},
{3.0f, 40.0f}
};
TEST_CHECK(effect::interpolate_time_event(events, -0.5f) == 10.0f);
TEST_CHECK(effect::interpolate_time_event(events, 0.0f) == 10.0f);
TEST_CHECK(effect::interpolate_time_event(events, 0.5f) == 15.0f);
TEST_CHECK(effect::interpolate_time_event(events, 1.0f) == 20.0f);
TEST_CHECK(effect::interpolate_time_event(events, 2.0f) == 30.0f);
TEST_CHECK(effect::interpolate_time_event(events, 3.0f) == 40.0f);
TEST_CHECK(effect::interpolate_time_event(events, 5.0f) == 40.0f);
}
void test_effect_data_and_simulation() {
std::cout << "[Test] Effect script parsing & particle simulation..." << std::endl;
std::string script = R"(
BoundingSphereRadius 120.000000
BoundingSpherePosition 10.000000 20.000000 30.000000
Group Particle
{
StartTime 0.000000
Position 0.000000 0.000000 50.000000
Group EmitterProperty
{
MaxEmissionCount 10
CycleLength 1.000000
CycleLoopEnable 1
EmittingDirection 0.000000 0.000000 1.000000
List TimeEventEmittingVelocity
{
0.000000 100.000000
}
List TimeEventEmissionCountPerSecond
{
0.000000 50.000000
}
List TimeEventLifeTime
{
0.000000 1.000000
}
List TimeEventSizeX
{
0.000000 20.000000
}
List TimeEventSizeY
{
0.000000 20.000000
}
}
Group ParticleProperty
{
SrcBlendType 5
DestBlendType 2
BillboardType 1
RotationSpeed 2.000000
List TimeEventGravity
{
0.000000 9.800000
}
List TimeEventAlpha
{
0.000000 0.000000
0.500000 1.000000
1.000000 0.000000
}
List TextureFiles
{
"d:/ymir work/effect/flare.dds"
}
}
}
)";
auto eff_data = std::make_shared<effect::EffectData>();
TEST_CHECK(eff_data->load_from_string(script));
TEST_CHECK(eff_data->get_bounding_sphere_radius() == 120.0f);
TEST_CHECK(eff_data->get_bounding_sphere_position().x == 10.0f);
TEST_CHECK(eff_data->get_particles().size() == 1);
const auto& p0 = eff_data->get_particles()[0];
TEST_CHECK(p0.particle.texture_files.size() == 1);
TEST_CHECK(p0.particle.texture_files[0] == "d:/ymir work/effect/flare.dds");
TEST_CHECK(p0.particle.billboard_type == 1);
TEST_CHECK(p0.particle.rotation_speed == 2.0f);
// Test simulation
effect::EffectInstance instance(eff_data);
instance.set_position(0.0f, 0.0f, 0.0f);
TEST_CHECK(instance.is_alive());
TEST_CHECK(instance.get_active_particle_count() == 0);
// Step simulation by 0.1s -> should emit particles
instance.update(0.1f);
TEST_CHECK(instance.get_active_particle_count() > 0);
// Verify drawing commands / billboards
std::vector<effect::ParticleDrawBatch> batches;
instance.build_draw_batches({0.0f, -100.0f, 50.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, batches);
TEST_CHECK(!batches.empty());
if (!batches.empty()) {
TEST_CHECK(batches[0].texture == "d:/ymir work/effect/flare.dds");
TEST_CHECK(batches[0].vertices.size() >= 4);
TEST_CHECK(batches[0].indices.size() >= 6);
}
}
void test_flying_homing_missile() {
std::cout << "[Test] Flying homing missile physics & hit..." << std::endl;
std::string fly_script = R"(
InitialVelocity 1000.000000
Gravity 0.000000
BombRange 15.000000
HomingFlag 1
HomingMaxAngle 10.000000
Range 5000.000000
Group AttachData
{
Type 1
AttachFile "arrow.mse"
TailFlag 1
TailLength 0.200000
}
)";
auto fly_data = std::make_shared<effect::FlyingData>();
TEST_CHECK(fly_data->load_from_string(fly_script));
TEST_CHECK(fly_data->initial_velocity == 1000.0f);
TEST_CHECK(fly_data->bomb_range == 15.0f);
TEST_CHECK(fly_data->homing_flag == true);
TEST_CHECK(fly_data->attach.has_tail == true);
effect::Vec3 start{0.0f, 0.0f, 0.0f};
effect::Vec3 target{100.0f, 20.0f, 0.0f};
bool hit_called = false;
effect::Vec3 hit_location;
effect::FlyingInstance fly_inst(fly_data, start, target);
fly_inst.set_hit_callback([&](const effect::Vec3& loc) {
hit_called = true;
hit_location = loc;
});
// Step physics towards target
float dt = 0.02f;
for (int step = 0; step < 50; ++step) {
if (!fly_inst.is_alive()) break;
fly_inst.update(dt);
}
TEST_CHECK(hit_called);
TEST_CHECK((hit_location - target).length() <= fly_data->bomb_range);
}
void test_real_effect_files_if_present() {
std::cout << "[Test] Real effect files check..." << std::endl;
std::vector<std::string> mse_paths = {
"metin2-client/build/zh-locale/locale-src/zh/effect/gm.mse",
"../../metin2-client/build/zh-locale/locale-src/zh/effect/gm.mse",
"ava2-client/ymir work/pc/warrior/effect/geom_4_badak.mse",
"../../ava2-client/ymir work/pc/warrior/effect/geom_4_badak.mse"
};
for (const auto& path : mse_paths) {
if (fs::exists(path)) {
effect::EffectData data;
bool ok = data.load_file(path);
TEST_CHECK(ok);
if (ok) {
std::cout << " -> Loaded real MSE (" << path << "), particles count: "
<< data.get_particles().size() << std::endl;
}
break;
}
}
std::vector<std::string> msf_paths = {
"ava2-client/ymir work/pc/warrior/effect/geompung.msf",
"../../ava2-client/ymir work/pc/warrior/effect/geompung.msf"
};
for (const auto& path : msf_paths) {
if (fs::exists(path)) {
effect::FlyingData data;
bool ok = data.load_file(path);
TEST_CHECK(ok);
if (ok) {
std::cout << " -> Loaded real MSF (" << path << "), init velocity: "
<< data.initial_velocity << ", homing: "
<< data.homing_flag << std::endl;
}
break;
}
}
}
int main() {
std::cout << "=== Running effect Unit Tests ===" << std::endl;
test_math_and_time_events();
test_effect_data_and_simulation();
test_flying_homing_missile();
test_real_effect_files_if_present();
if (g_failures == 0) {
std::cout << "=== ALL effect TESTS PASSED ===" << std::endl;
return 0;
} else {
std::cerr << "=== " << g_failures << " effect TESTS FAILED ===" << std::endl;
return 1;
}
}