Files
AVA2/tests/background_test.cpp
T

304 lines
18 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 "python_app.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"
"assert bg.GetHeight(0.0, 0.0) == 100.0, bg.GetHeight(0.0, 0.0)\n"
"assert bg.IsAttrOn(0, 0, bg.ATTR_BLOCK) == 1\n"
"assert bg.IsAttrOn(26000, 26000, bg.ATTR_BLOCK) == 0\n"
"bg.Update(400.0, 400.0, 0.0)\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] Map height scale, visibility, streaming and water parity..." << std::endl;
// MapOutdoorLoad::LoadSetting -> AreaTerrain scaled raw heights and water vertices.
spit(root/"map/scaled_map/setting.txt","ScriptType MapSetting\nMapSize 4 1\nBasePosition 50000 60000\nHeightScale 2\nTerrainVisible 0\nTextureSet textureset/scaled.txt\n");
spit(root/"textureset/scaled.txt","Start Texture004\n\"scaled.dds\"\n2\n3\n0\n0\n1\n0\n0\nEnd Texture004\n");
std::string water(7+128*128+8,char(0xff));
auto word=[&](size_t offset,uint16_t value) {water[offset]=char(value);water[offset+1]=char(value>>8);};
word(0,5426);word(2,128);word(4,128);water[6]=2;
water[7+128+1]=0;water[7+128+2]=1; // Odd row/column cell; second layer is -1 (no water).
const size_t waterHeightOffset=7+128*128;
for(int byte=0;byte<4;++byte)water[waterHeightOffset+byte]=char(uint32_t(1000)>>(8*byte));
for(int block=0;block<4;++block) {
char name[16];std::snprintf(name,sizeof(name),"%03d000",block);
std::vector<uint16_t> scaledRaw(131*131,uint16_t(200+block*100));
spit(root/"map/scaled_map"/name/"height.raw",std::string(reinterpret_cast<const char*>(scaledRaw.data()),scaledRaw.size()*2));
spit(root/"map/scaled_map"/name/"water.wtr",water);
}
auto& background=CPythonBackground::Instance();
for(int part=0;part<CPythonBackground::PART_NUM;++part)background.SetVisiblePart(part,false);
background.SetVisiblePart(CPythonBackground::PART_TERRAIN,true);
require(background.LoadMap("scaled_map"),"scaled map load");
require(background.GetHeight(100,100)==400 && background.GetTerrain()->GetHeightScale()==2,"map HeightScale ignored");
require(background.GetTerrain()->GetCellScale()==200,"original fixed CellScale changed");
std::vector<Render3DDraw> draws;background.BuildDrawCommands(nullptr,nullptr,nullptr,draws);
require(draws.empty() && background.IsVisiblePart(CPythonBackground::PART_TERRAIN),"map TerrainVisible must independently suppress draws");
auto* firstTerrain=background.GetTerrain();background.Update(3*25600+100,100,0);
require(background.GetTerrain()==firstTerrain && background.GetHeight(3*25600+100,100)==1000,"streamed blocks lost HeightScale or replaced active terrain");
auto* loadedWater=background.GetWater();require(loadedWater && loadedWater->GetWaterLevel()==2000,"water scale/bias diverged from original");
require(loadedWater->GetWaterFaceCount()==2,"invalid -1 water layer counted");
auto waterDraw=loadedWater->BuildDrawCommand(0);
require(waterDraw.positions.size()==12 && waterDraw.indices.size()==6,"odd water cell skipped or expanded across dry cells");
require(waterDraw.positions[0]==2*25600+200 && waterDraw.positions[1]==2000 && waterDraw.positions[2]==200,"water uses bordered terrain origin or fixed height offset");
background.SetVisiblePart(CPythonBackground::PART_WATER,true);
draws.clear();background.BuildDrawCommands(nullptr,nullptr,nullptr,draws);
require(draws.size()==2,"TerrainVisible disabled water or emitted hidden terrain");
// Missing optional settings in existing minimal fixtures must reset to defaults, not leak.
spit(root/"map/reset_map/setting.txt","ScriptType MapSetting\nMapSize 1 1\n");
std::vector<uint16_t> resetRaw(131*131,200);
spit(root/"map/reset_map/000000/height.raw",std::string(reinterpret_cast<const char*>(resetRaw.data()),resetRaw.size()*2));
require(background.LoadMap("reset_map"),"default map load");
require(background.GetHeight(100,100)==100 && background.GetOutdoorMap()->baseX==0 && background.GetTerrain()->GetTextureSet().empty(),"map settings/textures inherited");
draws.clear();background.BuildDrawCommands(nullptr,nullptr,nullptr,draws);
require(!draws.empty(),"default TerrainVisible not restored");
background.SetVisiblePart(CPythonBackground::PART_TERRAIN,false);draws.clear();
background.BuildDrawCommands(nullptr,nullptr,nullptr,draws);require(draws.empty(),"runtime terrain visibility ignored");
background.SetVisiblePart(CPythonBackground::PART_TERRAIN,true);
// A valid camera whose entire map is beyond its draw distance must not trigger an uncullable full-map fallback.
const float view[16]={1,0,0,0,0,1,0,0,0,0,1,0,0,0,1000000,1};
const float proj[16]={1,0,0,0,0,1,0,0,0,0,1,1,0,0,-1,0};
const float eye[3]={0,0,-1000000};
std::vector<Render3DDrawRef> refs;draws.clear();background.BuildDrawCommands(view,proj,eye,draws,&refs);
require(draws.empty() && refs.empty(),"culled terrain reappeared through full-map fallback");
spit(root/"map/reset_map/setting.txt","ScriptType MapSetting\nMapSize 1 1\nHeightScale 1.25\n");
require(background.LoadMap("reset_map",100000,0),"fallback terrain load");
require(background.GetHeight(100,100)==250,"fallback terrain ignored scale");
spit(root/"map/reset_map/setting.txt","ScriptType MapSetting\nMapSize 1 1\nHeightScale nan\n");
require(!background.LoadMap("reset_map") && !background.IsMapReady(),"non-finite terrain heights accepted");
background.Destroy();
require(background.LoadMap("test_bg_map"),"forest clock map load");
SpeedTreeForest reference;
reference.CreateInstance(0,0,300,191919,0,1,"clock");
background.GetForest()->CreateInstance(0,0,300,191919,0,1,"clock");
const float treeView[16]={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
const float treeProj[16]={1,0,0,0,0,1,0,0,0,0,1,1,0,0,-1,0};
for(float elapsed:{0.125f,0.375f,0.0f}) {
SetGameFrameDelta(elapsed);background.Update(0,0,0);reference.UpdateSystem(GetGameFrameDelta());
auto expected=reference.BuildDrawCommands(treeView,treeProj,0,50,0);
auto actual=background.GetForest()->BuildDrawCommands(treeView,treeProj,0,50,0);
require(!actual.empty() && actual.size()==expected.size(),"forest clock draw fixture");
for(size_t i=0;i<actual.size();++i)require(actual[i].positions==expected[i].positions,"background wind uses fixed 0.033 instead of frame delta");
}
SetGameFrameDelta(0.033f);background.Destroy();
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;
}
}