Files
AVA2/tests/background_test.cpp
T
shenandshen 281418f351 Fix cross-platform rendering and optimize frame performance
Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase.

Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements.

Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
2026-10-11 17:55:53 -07:00

226 lines
11 KiB
C++

// background / app camera module parity tests against AVA2 PythonBackgroundModule.cpp + PythonApplicationModule.cpp:
// - All 54 background module functions and constants
// - View distance settings and queries
// - Visibility flags and splat limits
// - GlobalPositionToLocalPosition and GlobalPositionToMapInfo conversions
// - Map loading, Warping (WarpTest / Warp), GetCurrentMapName
// - Height and attribute querying (GetHeight, isAttrOn, IsAttrOn)
// - app module camera bindings: SetCamera, GetCamera, GetCameraPitch, GetCameraRotation, RotateCamera,
// PitchCamera, ZoomCamera, MovieCamera functions, SetCameraSpeed, SetCameraMaxDistance,
// SetDefaultCamera, SetCameraSetting, Save/LoadCameraSetting, EnableSpecialCameraMode,
// GetRotatingDirection, GetDegreeDifference, GetCursorPosition, UpdateGame, RenderGame,
// and camera constants.
#include "python_background.h"
#include "python_gameplay_modules.h"
#include "python_runtime.h"
#include <cmath>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace ava2;
namespace fs = std::filesystem;
static void require(bool ok, const char* why) {
if (!ok) throw std::runtime_error(why);
}
static void spit(const fs::path& p, const std::string& s) {
fs::create_directories(p.parent_path());
std::ofstream f(p, std::ios::binary);
f << s;
}
int main(int argc, char** argv) {
if (argc != 3) return 2;
try {
const fs::path stdlib = fs::absolute(argv[1]);
const fs::path root = fs::temp_directory_path() / "ava2_background_test";
fs::remove_all(root);
fs::create_directories(root);
fs::current_path(root);
// Map and atlas setup
spit(root / "locale/en/atlasinfo.txt", "test_bg_map 102400 204800 2 2\n");
spit(root / "map/test_bg_map/setting.txt",
"ScriptType MapSetting\nMapSize 2 2\nBasePosition 102400 204800\n");
// Prepare dummy heightmap: 131x131 uint16_t filled with 200 (height 200 * 0.5 = 100.0)
std::vector<uint16_t> heights(131 * 131, 200);
std::string raw_heights(reinterpret_cast<const char*>(heights.data()), heights.size() * sizeof(uint16_t));
spit(root / "map/test_bg_map/000000/height.raw", raw_heights);
for (int row = 0; row < 131; ++row)
for (int col = 0; col < 131; ++col) heights[row * 131 + col] = 400 + row * 2 + col * 4;
spit(root / "map/test_bg_map/001001/height.raw",
std::string(reinterpret_cast<const char*>(heights.data()), heights.size() * sizeof(uint16_t)));
// Prepare dummy attr: 256x256 uint8_t with ATTR_BLOCK (1)
std::vector<uint8_t> attrs(256 * 256, 1);
std::string raw_attrs(reinterpret_cast<const char*>(attrs.data()), attrs.size());
spit(root / "map/test_bg_map/000000/attr.atr", raw_attrs);
PythonRuntime python(stdlib, root);
std::cout << "[TEST] Running block 1 (exports & constants)..." << std::endl;
python.execute(
"import background as bg\n"
"bg_names = ('IsSoftwareTiling', 'EnableSoftwareTiling', 'TextureChange', 'EnableSnow',\n"
" 'GlobalPositionToLocalPosition', 'GlobalPositionToMapInfo', 'GetRenderShadowTime',\n"
" 'LoadMap', 'Destroy', 'RegisterEnvironmentData', 'SetEnvironmentData',\n"
" 'GetCurrentMapName', 'GetPickingPoint', 'BeginEnvironment', 'EndEnvironment',\n"
" 'SetCharacterDirLight', 'SetBackgroundDirLight', 'Initialize', 'Update',\n"
" 'Render', 'RenderPCBlocker', 'RenderCollision', 'RenderSky', 'RenderCloud',\n"
" 'RenderWater', 'RenderEffect', 'RenderBeforeLensFlare', 'RenderAfterLensFlare',\n"
" 'RenderCharacterShadowToTexture', 'RenderDungeon', 'GetHeight', 'isAttrOn',\n"
" 'IsAttrOn', 'SetShadowLevel', 'IsVisiblePart', 'SetVisiblePart',\n"
" 'GetShadowMapColor', 'SetSplatLimit', 'GetRenderedSplatNum',\n"
" 'GetRenderedGraphicThingInstanceNum', 'SelectViewDistanceNum',\n"
" 'SetViewDistanceSet', 'GetFarClip', 'GetDistanceSetInfo', 'SetBGLoading',\n"
" 'SetRenderSort', 'SetTransparentTree', 'SetXMasTree', 'RegisterDungeonMapName',\n"
" 'VisibleGuildArea', 'DisableGuildArea', 'WarpTest', 'CreatePrivateShopPos',\n"
" 'DeletePrivateShopPos')\n"
"funcs = sorted(n for n in dir(bg) if callable(getattr(bg, n)))\n"
"assert funcs == sorted(bg_names), set(funcs) ^ set(bg_names)\n"
"\n"
"assert (bg.PART_TERRAIN, bg.PART_OBJECT, bg.PART_CLOUD, bg.PART_WATER, bg.PART_TREE, bg.PART_SKY, bg.PART_SHOP) == (0, 1, 2, 3, 4, 5, 6)\n"
"assert (bg.SKY_RENDER_MODE_DEFAULT, bg.SKY_RENDER_MODE_DIFFUSE, bg.SKY_RENDER_MODE_TEXTURE, bg.SKY_RENDER_MODE_MODULATE, bg.SKY_RENDER_MODE_MODULATE2X, bg.SKY_RENDER_MODE_MODULATE4X) == (0, 1, 2, 3, 4, 5)\n"
"assert (bg.SHADOW_NONE, bg.SHADOW_GROUND, bg.SHADOW_GROUND_AND_SOLO, bg.SHADOW_ALL, bg.SHADOW_ALL_HIGH, bg.SHADOW_ALL_MAX) == (0, 1, 2, 3, 4, 5)\n"
"assert (bg.DISTANCE0, bg.DISTANCE1, bg.DISTANCE2, bg.DISTANCE3, bg.DISTANCE4) == (0, 1, 2, 3, 4)\n"
"assert (bg.DISTANCE_SORT, bg.TEXTURE_SORT) == (0, 1)\n"
"assert (bg.ATTR_BLOCK, bg.ATTR_WATER, bg.ATTR_BANPK) == (1, 2, 4)\n"
"assert (bg.ATTRIBUTE_BLOCK, bg.ATTRIBUTE_WATER, bg.ATTRIBUTE_BANPK) == (1, 2, 4)\n"
);
std::cout << "[TEST] Running block 2 (basic calls & state)..." << std::endl;
python.execute(
"import background as bg\n"
"assert bg.GetCurrentMapName() == ''\n"
"assert bg.GetHeight(0.0, 0.0) == 0.0\n"
"assert bg.isAttrOn(0.0, 0.0, bg.ATTR_BLOCK) == 0\n"
"assert bg.IsAttrOn(0, 0, bg.ATTR_BLOCK) == 0\n"
"assert bg.GetFarClip() > 0.0\n"
"assert bg.IsSoftwareTiling() in (0, 1)\n"
"bg.EnableSoftwareTiling(1)\n"
"bg.EnableSnow(1)\n"
"bg.EnableSnow(0)\n"
"bg.SetShadowLevel(bg.SHADOW_ALL)\n"
"assert bg.IsVisiblePart(bg.PART_TERRAIN) == 1\n"
"bg.SetVisiblePart(bg.PART_TERRAIN, 0)\n"
"assert bg.IsVisiblePart(bg.PART_TERRAIN) == 0\n"
"bg.SetVisiblePart(bg.PART_TERRAIN, 1)\n"
"assert bg.IsVisiblePart(bg.PART_TERRAIN) == 1\n"
"bg.SetSplatLimit(100)\n"
"splat_info = bg.GetRenderedSplatNum()\n"
"assert len(splat_info) == 4\n"
"assert bg.GetRenderedGraphicThingInstanceNum() == (0, 0)\n"
"bg.SelectViewDistanceNum(0)\n"
"bg.SetViewDistanceSet(0, 25000.0)\n"
"dist_info = bg.GetDistanceSetInfo()\n"
"assert len(dist_info) == 4\n"
"bg.SetBGLoading(True)\n"
"bg.SetRenderSort(bg.DISTANCE_SORT)\n"
"bg.SetTransparentTree(True)\n"
"bg.SetXMasTree(1)\n"
"bg.RegisterDungeonMapName('dungeon_map')\n"
"bg.VisibleGuildArea()\n"
"bg.DisableGuildArea()\n"
"bg.CreatePrivateShopPos(1234)\n"
"bg.DeletePrivateShopPos()\n"
"bg.TextureChange('some_texture.dds')\n"
"assert isinstance(bg.GetRenderShadowTime(), int)\n"
"assert len(bg.GetPickingPoint()) == 3\n"
"bg.BeginEnvironment()\n"
"bg.SetCharacterDirLight()\n"
"bg.SetBackgroundDirLight()\n"
"bg.EndEnvironment()\n"
"bg.Update(0.0, 0.0, 0.0)\n"
"bg.Render()\n"
"bg.RenderPCBlocker()\n"
"bg.RenderCollision()\n"
"bg.RenderSky()\n"
"bg.RenderCloud()\n"
"bg.RenderWater()\n"
"bg.RenderEffect()\n"
"bg.RenderBeforeLensFlare()\n"
"bg.RenderAfterLensFlare()\n"
"bg.RenderCharacterShadowToTexture()\n"
"bg.RenderDungeon()\n"
);
std::cout << "[TEST] Running block 3 (map coords & warp)..." << std::endl;
python.execute(
"import background as bg\n"
"map_info = bg.GlobalPositionToMapInfo(102400, 204800)\n"
"assert map_info == ('test_bg_map', 102400, 204800), map_info\n"
"assert bg.GlobalPositionToMapInfo(10, 10) == ('', 0, 0)\n"
"\n"
"bg.WarpTest(1024, 2048)\n"
"assert bg.GetCurrentMapName() == 'test_bg_map', bg.GetCurrentMapName()\n"
"assert bg.GlobalPositionToLocalPosition(102400 + 400, 204800 + 600) == (400, 600)\n"
"assert bg.GetHeight(0.0, 0.0) == 100.0, bg.GetHeight(0.0, 0.0)\n"
"assert bg.isAttrOn(0.0, 0.0, bg.ATTR_BLOCK) == 1\n"
"assert bg.IsAttrOn(0, 0, bg.ATTR_BLOCK) == 1\n"
"bg.WarpTest(1284, 2308)\n"
"assert bg.GetHeight(26000.0, 26000.0) == 209.0, bg.GetHeight(26000.0, 26000.0)\n"
"\n"
"bg.Destroy()\n"
"assert bg.GetCurrentMapName() == ''\n"
);
std::cout << "[TEST] Running block 4 (app camera bindings)..." << std::endl;
python.execute(
"import app\n"
"assert app.DEGREE_DIRECTION_SAME == 0\n"
"assert app.DEGREE_DIRECTION_RIGHT == 1\n"
"assert app.DEGREE_DIRECTION_LEFT == 2\n"
"assert app.CAMERA_TO_POSITIVE == 1\n"
"assert app.CAMERA_TO_NEGATIVE == -1\n"
"assert app.CAMERA_STOP == 0\n"
"\n"
"app.SetCamera(1200.0, 15.0, 90.0, 45.0)\n"
"cam = app.GetCamera()\n"
"assert cam == (1200.0, 15.0, 90.0, 45.0), cam\n"
"assert app.GetCameraPitch() == 15.0\n"
"assert app.GetCameraRotation() == 90.0\n"
"\n"
"app.RotateCamera(app.CAMERA_TO_POSITIVE)\n"
"app.PitchCamera(app.CAMERA_TO_POSITIVE)\n"
"app.ZoomCamera(app.CAMERA_TO_POSITIVE)\n"
"app.MovieRotateCamera(1)\n"
"app.MoviePitchCamera(1)\n"
"app.MovieZoomCamera(1)\n"
"app.MovieResetCamera()\n"
"app.SetCameraSpeed(150)\n"
"app.SetCameraMaxDistance(4000.0)\n"
"app.SetDefaultCamera()\n"
"app.SetCameraSetting(0, 0, 100, 1200, 45, 20)\n"
"app.SaveCameraSetting('cam_test.txt')\n"
"app.LoadCameraSetting('cam_test.txt')\n"
"app.EnableSpecialCameraMode()\n"
"\n"
"assert app.GetRotatingDirection(0.0, 0.0) == app.DEGREE_DIRECTION_LEFT\n"
"assert app.GetRotatingDirection(20.0, 10.0) == app.DEGREE_DIRECTION_RIGHT\n"
"assert app.GetRotatingDirection(10.0, 20.0) == app.DEGREE_DIRECTION_LEFT\n"
"diff = app.GetDegreeDifference(10.0, 20.0)\n"
"assert isinstance(diff, float)\n"
"\n"
"cur_pos = app.GetCursorPosition()\n"
"assert len(cur_pos) == 2\n"
"\n"
"app.UpdateGame()\n"
"app.RenderGame()\n"
);
std::cout << "AVA2 background parity passed" << std::endl;
return 0;
} catch (const std::exception& e) {
std::cerr << "background_test failed: " << e.what() << std::endl;
return 1;
}
}