272 lines
9.5 KiB
C++
272 lines
9.5 KiB
C++
#include "eter_net/PythonExchange.h"
|
|
#include "eter_net/PythonSafeBox.h"
|
|
#include "eter_render/ActorInstance.h"
|
|
#include "eter_render/MapObjectManager.h"
|
|
#include "eter_render/CRenderTarget.h"
|
|
#include <Python.h>
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <cmath>
|
|
|
|
using namespace ava2;
|
|
|
|
void TestExchangeCpp() {
|
|
std::cout << "[Test 1] Testing CPythonExchange state machine..." << std::endl;
|
|
auto& ex = CPythonExchange::Instance();
|
|
ex.Clear();
|
|
assert(!ex.isTrading());
|
|
|
|
ex.Start();
|
|
assert(ex.isTrading());
|
|
|
|
ex.SetSelfName("HeroWarrior");
|
|
ex.SetTargetName("MerchantNPC");
|
|
assert(std::string(ex.GetNameFromSelf()) == "HeroWarrior");
|
|
assert(std::string(ex.GetNameFromTarget()) == "MerchantNPC");
|
|
|
|
ex.SetSelfLevel(99);
|
|
ex.SetTargetLevel(105);
|
|
assert(ex.GetLevelFromSelf() == 99);
|
|
assert(ex.GetLevelFromTarget() == 105);
|
|
|
|
ex.SetElkToSelf(50000000);
|
|
ex.SetElkToTarget(12000000);
|
|
assert(ex.GetElkFromSelf() == 50000000);
|
|
assert(ex.GetElkFromTarget() == 12000000);
|
|
|
|
// Offer item in slot 0
|
|
ex.SetItemToSelf(0, 169, 1); // Battle Sword+9
|
|
assert(ex.GetItemVnumFromSelf(0) == 169);
|
|
assert(ex.GetItemCountFromSelf(0) == 1);
|
|
|
|
ex.SetItemMetinSocketToSelf(0, 0, 28430); // Stone of Monster+4
|
|
assert(ex.GetItemMetinSocketFromSelf(0, 0) == 28430);
|
|
|
|
ex.SetItemAttributeToSelf(0, 0, 1, 2000); // Max HP +2000
|
|
uint8_t aType = 0; int16_t aVal = 0;
|
|
ex.GetItemAttributeFromSelf(0, 0, &aType, &aVal);
|
|
assert(aType == 1 && aVal == 2000);
|
|
|
|
ex.SetItemRandomAttributeToSelf(0, 0, 71, 10); // Attack value +10
|
|
ex.GetItemRandomAttributeFromSelf(0, 0, &aType, &aVal);
|
|
assert(aType == 71 && aVal == 10);
|
|
|
|
ex.SetItemNewName(0, "Legendary Blade", true);
|
|
assert(std::string(ex.GetItemNewName(0, true)) == "Legendary Blade");
|
|
|
|
// Offer target item in slot 1
|
|
ex.SetItemToTarget(1, 11299, 1); // Black Steel Armor+9
|
|
assert(ex.GetItemVnumFromTarget(1) == 11299);
|
|
assert(ex.GetItemCountFromTarget(1) == 1);
|
|
|
|
// Accept toggle
|
|
assert(!ex.GetAcceptFromSelf());
|
|
ex.SetAcceptToSelf(1);
|
|
assert(ex.GetAcceptFromSelf());
|
|
|
|
assert(!ex.GetAcceptFromTarget());
|
|
ex.SetAcceptToTarget(1);
|
|
assert(ex.GetAcceptFromTarget());
|
|
|
|
ex.End();
|
|
assert(!ex.isTrading());
|
|
std::cout << " -> CPythonExchange test PASSED!" << std::endl;
|
|
}
|
|
|
|
void TestSafeBoxCpp() {
|
|
std::cout << "[Test 2] Testing CPythonSafeBox storeroom..." << std::endl;
|
|
auto& sb = CPythonSafeBox::Instance();
|
|
|
|
// Windows OpenSafeBox takes rows: three rows * five columns, not three pages.
|
|
sb.OpenSafeBox(3);
|
|
assert(sb.GetCurrentSafeBoxSize() == 15);
|
|
|
|
sb.SetMoney(9876543);
|
|
assert(sb.GetMoney() == 9876543);
|
|
|
|
TItemData swordItem;
|
|
swordItem.vnum = 299; // FMS+9 (Full Moon Sword)
|
|
swordItem.count = 1;
|
|
swordItem.alSockets[0] = 28430; // Stone of Monster
|
|
swordItem.aAttr[0].bType = 72; // Avg Damage
|
|
swordItem.aAttr[0].sValue = 45; // 45% Avg
|
|
sb.SetItemData(5, swordItem);
|
|
|
|
uint32_t queriedVnum = 0;
|
|
assert(sb.GetSlotItemID(5, &queriedVnum) && queriedVnum == 299);
|
|
|
|
const TItemData* ptr = nullptr;
|
|
assert(sb.GetItemDataPtr(5, &ptr) && ptr != nullptr);
|
|
assert(ptr->vnum == 299);
|
|
assert(ptr->alSockets[0] == 28430);
|
|
assert(ptr->aAttr[0].bType == 72 && ptr->aAttr[0].sValue == 45);
|
|
|
|
// DelItemData
|
|
sb.DelItemData(5);
|
|
assert(sb.GetSlotItemID(5, &queriedVnum) && queriedVnum == 0);
|
|
|
|
// Mall
|
|
sb.OpenMall(2); // Two rows * five columns.
|
|
assert(sb.GetMallSize() == 10);
|
|
|
|
TItemData mallItem;
|
|
mallItem.vnum = 70005; // Teleportation Ring
|
|
mallItem.count = 50;
|
|
sb.SetMallItemData(0, mallItem);
|
|
|
|
assert(sb.GetSlotMallItemID(0, &queriedVnum) && queriedVnum == 70005);
|
|
std::cout << " -> CPythonSafeBox test PASSED!" << std::endl;
|
|
}
|
|
|
|
void TestActorPolymorphAndShining() {
|
|
std::cout << "[Test 3] Testing ActorInstance Polymorph & Shining system..." << std::endl;
|
|
ActorInstance actor;
|
|
std::string warriorModel = "ymir work/pc/warrior/warrior_novice.gr2";
|
|
if (!std::filesystem::exists(warriorModel)) {
|
|
warriorModel = "ymir work/pc/warrior/warrior_assasin1.gr2";
|
|
}
|
|
|
|
if (std::filesystem::exists(warriorModel)) {
|
|
actor.SetModel(warriorModel);
|
|
assert(!actor.IsPolymorph());
|
|
|
|
// Test Polymorph into mob vnum 101 (stray dog) or 102 (wolf)
|
|
if (actor.SetPolymorph(101)) {
|
|
assert(actor.IsPolymorph());
|
|
assert(actor.GetPolymorphVnum() == 101);
|
|
std::cout << " -> Polymorph to Stray Dog (101) successful!" << std::endl;
|
|
|
|
// Clear Polymorph
|
|
actor.ClearPolymorph();
|
|
assert(!actor.IsPolymorph());
|
|
assert(actor.GetPolymorphVnum() == 0);
|
|
std::cout << " -> Clear Polymorph successful, restored base model!" << std::endl;
|
|
}
|
|
|
|
// Test Shining system
|
|
actor.SetWeaponShining(9);
|
|
actor.SetArmorShining(9);
|
|
actor.SetAcceShining(18);
|
|
assert(actor.GetWeaponShining() == 9);
|
|
assert(actor.GetArmorShining() == 9);
|
|
assert(actor.GetAcceShining() == 18);
|
|
|
|
auto draws = actor.BuildDrawCommands();
|
|
// Should contain mesh draw commands plus shining additive billboard draws
|
|
assert(!draws.empty());
|
|
bool foundAdditive = false;
|
|
for (const auto& d : draws) {
|
|
if (d.alpha_blend && d.blend_mode == 1) {
|
|
foundAdditive = true;
|
|
break;
|
|
}
|
|
}
|
|
assert(foundAdditive);
|
|
std::cout << " -> Shining system additive aura draw commands generated!" << std::endl;
|
|
|
|
actor.ClearShining();
|
|
assert(actor.GetWeaponShining() == 0);
|
|
}
|
|
std::cout << " -> ActorInstance Polymorph & Shining test PASSED!" << std::endl;
|
|
}
|
|
|
|
void TestBuildingOBBCollision() {
|
|
std::cout << "[Test 4] Testing MapObjectManager Building OBB Collision..." << std::endl;
|
|
MapObjectManager mgr;
|
|
|
|
std::string gatePath = "ymir work/zone/b/building/b1-034-stonedoor.gr2";
|
|
if (std::filesystem::exists(gatePath)) {
|
|
bool added = mgr.AddObject(gatePath, 1000.0f, 0.0f, 1000.0f, 0.0f, 0.0f, 0.0f, 1.0f);
|
|
assert(added);
|
|
|
|
// Point very far away: no collision
|
|
assert(!mgr.CheckCollision(5000.0f, 5000.0f, 30.0f));
|
|
|
|
// Point near center of building: collision detected!
|
|
bool collides = mgr.CheckCollision(1000.0f, 1000.0f, 30.0f);
|
|
assert(collides);
|
|
|
|
float nx = 0.0f, nz = 0.0f;
|
|
bool hasNormal = mgr.CheckCollisionNormal(1000.0f, 1000.0f, 30.0f, nx, nz);
|
|
assert(hasNormal);
|
|
float len = std::sqrt(nx * nx + nz * nz);
|
|
assert(std::abs(len - 1.0f) < 0.01f);
|
|
std::cout << " -> Building OBB collision & sliding normal (" << nx << ", " << nz << ") verified!" << std::endl;
|
|
|
|
// Test Actor sliding into building
|
|
ActorInstance player;
|
|
player.SetPosition(900.0f, 0.0f, 1000.0f);
|
|
// Move towards building
|
|
player.MoveInDirection(90.0f, 0.5f, nullptr, &mgr);
|
|
// Actor should be prevented from penetrating building center
|
|
assert(player.GetX() < 1000.0f);
|
|
std::cout << " -> Actor movement sliding against building OBB verified!" << std::endl;
|
|
} else {
|
|
std::cout << " -> Skipped building gate test (file not in repo)" << std::endl;
|
|
}
|
|
std::cout << " -> Building OBB Collision test PASSED!" << std::endl;
|
|
}
|
|
|
|
#include "python_native_modules.h"
|
|
|
|
void TestPythonModules() {
|
|
std::cout << "[Test 5] Testing Python exchange & safebox module bindings..." << std::endl;
|
|
CPythonExchange::Instance().Clear();
|
|
CPythonExchange::Instance().End();
|
|
CPythonSafeBox::Instance().OpenSafeBox(0);
|
|
CPythonSafeBox::Instance().OpenMall(0);
|
|
CPythonSafeBox::Instance().SetMoney(0);
|
|
|
|
Py_NoSiteFlag = 1;
|
|
Py_IgnoreEnvironmentFlag = 1;
|
|
PyImport_AppendInittab("exchange", initTrade);
|
|
PyImport_AppendInittab("safebox", initsafebox);
|
|
Py_Initialize();
|
|
|
|
int res = PyRun_SimpleString(
|
|
"import exchange\n"
|
|
"assert exchange.EXCHANGE_ITEM_MAX_NUM == 12\n"
|
|
"assert exchange.isTrading() == 0\n"
|
|
"exchange.InitTrading()\n"
|
|
"assert exchange.GetElkFromSelf() == 0\n"
|
|
"assert exchange.GetItemVnumFromSelf(0) == 0\n"
|
|
"assert exchange.GetAcceptFromSelf() == 0\n"
|
|
"assert int(exchange.GetElkMode()) == 0\n"
|
|
"exchange.SetElkMode(1)\n"
|
|
"assert int(exchange.GetElkMode()) == 1\n"
|
|
);
|
|
assert(res == 0);
|
|
|
|
res = PyRun_SimpleString(
|
|
"import safebox\n"
|
|
"assert safebox.SAFEBOX_SLOT_X_COUNT == 5\n"
|
|
"assert safebox.SAFEBOX_SLOT_Y_COUNT == 9\n"
|
|
"assert safebox.SAFEBOX_PAGE_SIZE == 45\n"
|
|
"assert safebox.GetCurrentSafeboxSize() == 0\n"
|
|
"assert safebox.GetMoney() == 0\n"
|
|
"assert safebox.GetItemID(0) == 0\n"
|
|
"assert safebox.GetMallSize() == 0\n"
|
|
);
|
|
assert(res == 0);
|
|
|
|
Py_Finalize();
|
|
std::cout << " -> Python exchange & safebox modules verified successfully!" << std::endl;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
std::cout << "========================================" << std::endl;
|
|
std::cout << " AVA2 Exchange, SafeBox & OBB Unit Test" << std::endl;
|
|
std::cout << "========================================" << std::endl;
|
|
|
|
TestExchangeCpp();
|
|
TestSafeBoxCpp();
|
|
TestActorPolymorphAndShining();
|
|
TestBuildingOBBCollision();
|
|
TestPythonModules();
|
|
|
|
std::cout << "========================================" << std::endl;
|
|
std::cout << " ALL TESTS PASSED SUCCESSFULLY (100%) " << std::endl;
|
|
std::cout << "========================================" << std::endl;
|
|
return 0;
|
|
}
|