Files
AVA2/tests/touch_game_test.cpp

275 lines
16 KiB
C++

// Touch mode in the original GameWindow: the mobile HUD must leave the joystick / attack zones and
// open world to the touch router, while real UI (HUD buttons) still takes the finger.
// The checks are assert()s; keep them in release builds.
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "python_runtime.h"
#include "python_gameplay_modules.h"
#include "python_touch.h"
#include "python_item.h"
#include "eter_render/Gr2ModelRenderer.h"
#include "app/input/touch_router.h"
#include "eter_ui/PythonWindowManager.h"
#include "eter_ui/PythonWindow.h"
#include "eter_image/GrpDevice.h"
#include "test_rundir.h"
#include "eter_net/PythonNetworkStream.h"
#include "eter_net/NetworkActorManager.h"
#include "python_system.h"
#include <Python.h>
#include <SDL3/SDL.h>
#include <array>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <iostream>
namespace {
struct GameSink final : ava2::input::TouchSink {
UI::CWindowManager* wm = nullptr;
int ui_downs = 0, taps = 0, skill_casts=0, skill_slot=-1;
int SkillAt(float x,float y) override {return ava2::TouchSkillAt(*wm,long(x),long(y));}
void Skill(int slot,float,float,bool) override {++skill_casts;skill_slot=slot;}
bool attack = false;
bool IsUiAt(float x, float y) override {
auto* win = wm->PickWindow(long(x), long(y));
return win && !ava2::IsPlayerGameWindow(win->GetHandler());
}
void UiMouseMove(float x, float y) override { wm->RunMouseMove(long(x), long(y)); }
void UiMouseDown(float x, float y) override { ++ui_downs; wm->RunMouseMove(long(x), long(y)); }
void UiMouseUp(float x, float y) override { wm->RunMouseMove(long(x), long(y)); }
void WorldTap(float, float) override { ++taps; }
void Joystick(bool active, float x, float y) override { ava2::SetPlayerJoystick(active, x, y); }
void Attack(bool pressed) override { attack = pressed; ava2::SetPlayerAttackKey(pressed); }
void CameraRotate(float, float) override {}
void CameraZoom(float) override {}
};
} // namespace
int main(int argc, char** argv) {
if (argc < 3) return 2;
if (!ava2_test::EnterTestRunDir()) return ava2_test::kSkip;
constexpr int W = 1024, H = 768;
// Must be on before the runtime creates the app module (app.ENABLE_MOBILE_HUD).
ava2::input::SetTouchMode(true);
auto& router = ava2::input::GetTouchRouter();
router.SetLayout(ava2::input::TouchLayout::ForScreen(W, H));
ava2::PythonRuntime runtime(argv[1], argv[2]);
auto* wm = runtime.window_manager();
wm->SetScreenSize(W, H); wm->SetResolution(W, H);
SDL_Surface* surface = SDL_CreateSurface(W, H, SDL_PIXELFORMAT_ARGB8888);
SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface);
assert(surface && renderer);
CGraphicDevice device;
device.Init(renderer);
runtime.execute(
"import app, touch, localeInfo, networkModule, introLoading, net, player\n"
"assert app.ENABLE_MOBILE_HUD == 1 and touch.IsEnabled()\n"
"net.SendSelectCharacterPacket=lambda slot: None\n"
"introLoading.__main__()\n"
"localeInfo.LoadLocaleData()\n"
"stream = networkModule.MainStream()\nstream.Create()\n"
"stream.SetLoadingPhase()\n"
"stream.SetGamePhase()\n"
"game = stream.curPhaseWindow\n"
"assert game.__class__.__name__ == 'GameWindow'\n"
"hud = game.interface.wndMobileHUD\n"
"assert hud and hud.IsShow()\n"
"game.OnUpdate()\n");
GameSink sink;
sink.wm = wm;
router.SetSink(&sink);
router.SetWorldEnabled(true);
const auto layout = router.GetLayout();
// The combat wheel moved the attack button onto its own art (bottom-right, radius 60).
assert(layout.attack_r == 60.0f);
assert(layout.attack_x == W - 120.0f && layout.attack_y == H - 120.0f);
// Open world, joystick zone and attack button are not UI: the HUD visuals are not_pick.
assert(!sink.IsUiAt(W * 0.6f, H * 0.4f));
assert(!sink.IsUiAt(160.0f, H - 180.0f));
assert(!sink.IsUiAt(layout.attack_x, layout.attack_y));
// Mobile mode hides the PC taskbar; the HUD buttons (here the combat wheel's page button)
// are real UI. The quick belt is quickslot page 4 and the minimap sits under the top-right row.
runtime.execute("assert not game.interface.wndTaskBar.IsShow()\n"
"assert [s.globalIndex for s in hud.quick_slots] == range(24, 32)\n"
"mm = game.interface.wndMiniMap\n"
"assert mm.GetGlobalPosition()[1] >= hud.drawer.GetGlobalPosition()[1] + hud.drawer.GetRowHeight()\n"
"pb = hud.combat_wheel.page_button\n"
"touch_tb = pb.GetGlobalPosition()\n"
"touch_tb = (touch_tb[0] + pb.GetWidth() / 2, touch_tb[1] + pb.GetHeight() / 2)\n"
);
PyObject* main = PyImport_AddModule("__main__");
PyObject* tb = PyDict_GetItemString(PyModule_GetDict(main), "touch_tb");
assert(tb && PyTuple_Check(tb));
const float tb_x = float(PyInt_AsLong(PyTuple_GetItem(tb, 0)));
const float tb_y = float(PyInt_AsLong(PyTuple_GetItem(tb, 1)));
assert(sink.IsUiAt(tb_x, tb_y));
// Joystick pushed to the screen right walks the player camera-relative right (yaw 90 at camera 0).
float yaw = -1.0f;
assert(!ava2::GetPlayerMovementYaw(0.0f, yaw));
router.FingerDown(1, 160.0f, H - 180.0f, 0);
router.FingerMove(1, 160.0f + layout.joy_radius, H - 180.0f, 16);
assert(router.GetJoystick().active);
assert(ava2::GetPlayerMovementYaw(0.0f, yaw) && std::fabs(yaw - 90.0f) < 0.5f);
// A second finger holds attack while walking; the HUD shows the pressed button.
router.FingerDown(2, layout.attack_x, layout.attack_y, 20);
assert(sink.attack && router.IsAttackPressed());
runtime.execute("game.interface.wndMobileHUD.combat_wheel.OnUpdate()\n"
"assert game.interface.wndMobileHUD.combat_wheel.attack_pressed\n"
"game.interface.wndMobileHUD.joystick.OnUpdate()\n");
router.Update(100); // a frame has seen the held key
router.FingerUp(2, layout.attack_x, layout.attack_y, 200);
router.FingerUp(1, 160.0f + layout.joy_radius, H - 180.0f, 220);
assert(!sink.attack && !ava2::GetPlayerMovementYaw(0.0f, yaw));
runtime.execute("hud.joystick.OnUpdate()\nassert not hud.joystick.base_bar.IsShow() and not hud.joystick.knob.IsShow()\n");
// Skill shortcut resolves the current slot through the real picked Python window, including rim.
runtime.execute("player.GetSkillIndex=lambda n: 1\nplayer.GetLocalQuickSlot=lambda n: (player.SLOT_TYPE_SKILL, 17)\n"
"shortcut=hud.combat_wheel.slots[0]\nshortcut_xy=shortcut.GetGlobalPosition()\n");
auto* xy=PyDict_GetItemString(PyModule_GetDict(main),"shortcut_xy");
const float sx=float(PyInt_AsLong(PyTuple_GetItem(xy,0)))+2,sy=float(PyInt_AsLong(PyTuple_GetItem(xy,1)))+2;
assert(sink.SkillAt(sx,sy)==17);router.FingerDown(8,sx,sy,230);router.FingerMove(8,sx+60,sy,240);router.Update(241);
assert(sink.skill_casts==1 && sink.skill_slot==17);
runtime.execute("hud.combat_wheel.OnUpdate()\nassert hud.combat_wheel.aim_marker.IsShow()\n");
router.FingerCancel(8,250);runtime.execute("hud.combat_wheel.OnUpdate()\nassert not hud.combat_wheel.aim_marker.IsShow()\n");
// Real actor/item tap adapter: approach, completion and stale item generation.
auto& manager=ava2::NetworkActorManager::Instance();auto& network=ava2::PythonNetworkStream::Instance();manager.Clear();
ava2::SNetworkActorData mainData{};mainData.m_dwVID=100;mainData.m_bType=6;manager.AppendActor(mainData);network.SetMainActorVID(100);
auto mainActor=manager.GetActorInstance(100);mainActor->SetPosition(-1000,0,0);
ava2::SNetworkActorData npcData{};npcData.m_dwVID=101;npcData.m_bType=1;manager.AppendActor(npcData);auto npc=manager.GetActorInstance(101);npc->SetPosition(0,0,0);npc->SetHP(100);
ava2::Gr2SceneRenderer scene(W,H);scene.SetCameraLookAt(0,100,-500,0,0,0,0,1,0);scene.SetPerspective(60,float(W)/H,1,3000);
float px,py;assert(scene.ProjectToScreen(0,0,0,px,py));network.ClearSendQueue();
assert(ava2::HandlePlayerTouchWorldTap(scene,px+20,py));assert(mainActor->HasMoveTarget());
mainActor->SetPosition(-400,0,0);ava2::ProcessPlayerTouchInteraction();assert(!mainActor->HasMoveTarget());
const auto click=network.GetSendQueue();assert(click==std::vector<uint8_t>({61,101,0,0,0,26,101,0,0,0}));
auto& ground=ava2::CPythonItem::Instance();ground.CreateItem(102,27001,0,0,0,false);mainActor->SetPosition(-1000,0,0);network.ClearSendQueue();
assert(ava2::HandlePlayerTouchWorldTap(scene,px,py) && mainActor->HasMoveTarget());
ground.DeleteItem(102);ground.CreateItem(102,27001,0,0,0,false);mainActor->SetPosition(0,0,0);ava2::ProcessPlayerTouchInteraction();
assert(network.GetSendQueue().empty() && !mainActor->HasMoveTarget());
assert(ava2::HandlePlayerTouchWorldTap(scene,px,py));assert(!network.GetSendQueue().empty());ground.DeleteAllItems();manager.Clear();
// HUD button taps go to the window manager, open world taps become world clicks.
router.FingerDown(3, tb_x, tb_y, 300);
router.FingerUp(3, tb_x, tb_y, 350);
assert(sink.ui_downs == 1);
router.FingerDown(4, W * 0.6f, H * 0.4f, 400);
router.FingerUp(4, W * 0.6f, H * 0.4f, 450);
assert(sink.taps == 1);
// Safe area (platform/): desktop has none; a HUD built with insets keeps the attack button inside.
runtime.execute("assert touch.GetSafeArea() == (0, 0, 0, 0)\n");
router.SetLayout(ava2::input::TouchLayout::ForScreen(W, H, 40.0f, 0.0f, 30.0f, 20.0f));
runtime.execute("assert touch.GetSafeArea() == (40, 0, 30, 20)\n"
"import ui_mobile_hud\n"
"safe_hud = ui_mobile_hud.MobileHUD(game.interface)\n"
"(ax, ay, ar), joy = touch.GetLayout()\n"
"assert (ax, ay, ar) == (1024 - 30 - 120, 768 - 20 - 120, 60), (ax, ay, ar)\n"
"assert safe_hud.player_hud.GetGlobalPosition()[0] == 40 + 12\n"
"safe_hud.Hide()\nsafe_hud = None\n"
// Live: the running HUD notices the new insets, rebuilds and moves the original windows.
"hud.OnUpdate()\n"
"assert hud.safe_area == (40, 0, 30, 20)\n"
"assert hud.player_hud.GetGlobalPosition()[0] == 40 + 12\n"
"assert touch.GetLayout()[0] == (1024 - 30 - 120, 768 - 20 - 120, 60)\n"
"assert mm.GetGlobalPosition()[0] + mm.GetWidth() == 1024 - 30\n"
"assert game.affectShower.GetGlobalPosition()[0] == 40 + 12 + 64 + 8\n"
"assert not game.interface.wndTaskBar.IsShow()\n");
router.SetLayout(layout);
runtime.execute("hud.OnUpdate()\n"
"assert hud.safe_area == (0, 0, 0, 0) and hud.player_hud.GetGlobalPosition()[0] == 12\n"
"assert mm.GetGlobalPosition()[0] + mm.GetWidth() == 1024\n"
"game.OnUpdate()\n");
// Auto hunt (mobile step 6): the AUTO button sits on the combat wheel and is real UI; with a
// main actor and a mob the native driver targets, walks, stops in reach and gives up on the dead.
ava2::CPythonSystem::SetConfigDirectory(std::filesystem::temp_directory_path().string());
std::filesystem::remove(std::filesystem::temp_directory_path() / "autohunt.cfg");
runtime.execute("ab = hud.combat_wheel.auto_button\n"
"assert ab.IsShow() and not player.IsAutoHunt()\n"
"assert player.SetAutoHunt(1) == 0\n" // no main actor yet
"touch_tb = ab.GetGlobalPosition()\n"
"touch_tb = (touch_tb[0] + ab.GetWidth() / 2, touch_tb[1] + ab.GetHeight() / 2)\n"
"assert player.GetAutoHuntOption('radius') == 1500\n"
"player.SetAutoHuntOption('radius', 2000)\nplayer.SetAutoHuntOption('stones', 1)\n"
"assert player.GetAutoHuntOption('radius') == 2000 and player.GetAutoHuntOption('stones') == 1\n"
"player.SetAutoHuntOption('stones', 0)\n");
tb = PyDict_GetItemString(PyModule_GetDict(main), "touch_tb");
const float ab_x = float(PyInt_AsLong(PyTuple_GetItem(tb, 0)));
const float ab_y = float(PyInt_AsLong(PyTuple_GetItem(tb, 1)));
assert(sink.IsUiAt(ab_x, ab_y));
{
std::ifstream cfg(std::filesystem::temp_directory_path() / "autohunt.cfg");
std::string text((std::istreambuf_iterator<char>(cfg)), std::istreambuf_iterator<char>());
assert(text.find("radius 2000\n") != std::string::npos && text.find("stones 0\n") != std::string::npos);
}
auto& actors = ava2::NetworkActorManager::Instance();
ava2::SNetworkActorData me{};
me.m_dwVID = 5001; me.m_bType = 6; me.m_stName = "AutoHunter"; me.m_lCurX = 10000; me.m_lCurY = 10000;
actors.AppendActor(me);
ava2::PythonNetworkStream::Instance().SetMainActorVID(5001);
ava2::SNetworkActorData mob{};
mob.m_dwVID = 5002; mob.m_bType = 0; mob.m_dwRace = 101; mob.m_lCurX = 10800; mob.m_lCurY = 10000;
actors.AppendActor(mob);
auto hunter = actors.GetActorInstance(5001);
auto prey = actors.GetActorInstance(5002);
assert(hunter && prey);
// Tap AUTO: on (button turns green, state text shows the fight).
wm->RunMouseMove(long(ab_x), long(ab_y));
runtime.execute("ab.OnMouseLeftButtonDown()\nab.OnMouseLeftButtonUp()\n"
"assert player.IsAutoHunt() and ab.isOn\n");
ava2::ProcessAutoHunt();
runtime.execute("assert player.GetAutoHuntState() == ('engage', 5002), player.GetAutoHuntState()\n"
"assert player.GetTargetVID() == 5002\n"
"ab.OnUpdate()\nassert ab.state_text.GetText() == 'Fight'\n");
assert(hunter->HasMoveTarget());
hunter->SetPosition(10700.0f, hunter->GetY(), 10000.0f);
ava2::ProcessAutoHunt();
assert(!hunter->HasMoveTarget());
// Steering pauses it; the mob dies; turning it off from the button.
ava2::SetPlayerJoystick(true, 1.0f, 0.0f);
ava2::ProcessAutoHunt();
runtime.execute("assert player.GetAutoHuntState()[0] == 'paused'\n");
ava2::SetPlayerJoystick(false, 0.0f, 0.0f);
prey->SetHP(0);
runtime.execute("ab.OnMouseLeftButtonDown()\nab.OnMouseLeftButtonUp()\n"
"assert not player.IsAutoHunt()\nab.OnUpdate()\nassert not ab.isOn\n"
// Long press opens the settings window instead of toggling.
"ab.OnMouseLeftButtonDown()\nab.pressTime -= 1000\nab.OnUpdate()\nab.OnMouseLeftButtonUp()\n"
"import ui_mobile_autohunt\n"
"assert not player.IsAutoHunt() and ui_mobile_autohunt._settings.IsShow()\n"
"ui_mobile_autohunt._settings.Close()\n");
actors.RemoveActor(5002);
actors.RemoveActor(5001);
ava2::PythonNetworkStream::Instance().SetMainActorVID(0);
std::filesystem::remove(std::filesystem::temp_directory_path() / "autohunt.cfg");
SDL_FillSurfaceRect(surface, nullptr, 0xff10141e);
wm->Render(); SDL_RenderPresent(renderer);
// CDisc geometry: the attack disc is dark red between the icon and the rim; empty screen is not.
auto pixel = [&](int x, int y) {
Uint8 r = 0, g = 0, b = 0, a = 0;
SDL_ReadSurfacePixel(surface, x, y, &r, &g, &b, &a);
return std::array<int, 3>{r, g, b};
};
const auto disc = pixel(int(layout.attack_x), int(layout.attack_y) + 45);
assert(disc[0] > 40 && disc[0] > disc[1] * 3 && disc[0] > disc[2] * 3);
const auto empty = pixel(int(W * 0.6f), int(H * 0.4f));
assert(empty[0] < 40);
if (argc > 3) SDL_SaveBMP(surface, argv[3]);
router.SetSink(nullptr);
runtime.execute("stream.Destroy()\n");
wm->Update();
device.Init(nullptr);
SDL_DestroyRenderer(renderer); SDL_DestroySurface(surface);
std::cout << "Touch router drives joystick, attack and world taps around the mobile HUD\n";
}