// chr / chrmgr against the original AVA2 PythonCharacterModule.cpp + PythonCharacterManagerModule.cpp: // the module surfaces, the no-instance getter answers, Py_BuildException (chr) vs Py_BadArgument // (chrmgr) argument errors, the rotation/direction conversions, CreateInstance's dict path (with the // "y" -> m_lPosX bug), GetVIDInfo, SetPathName's d:/ymir guard, SetRaceHeight and testSetRideMan. #include "python_gameplay_modules.h" #include "python_runtime.h" #include "eter_net/NetworkActorManager.h" #include "eter_render/RaceData.h" #include #include #include using namespace ava2; namespace fs = std::filesystem; void require(bool ok,const char* why) { if(!ok)throw std::runtime_error(why); } int main(int argc,char** argv) { if(argc!=3)return 2; try { const fs::path root=fs::temp_directory_path()/"ava2_chr_test"; fs::remove_all(root);fs::create_directories(root); fs::current_path(root); PythonRuntime python(argv[1],root); auto& mgr=NetworkActorManager::Instance(); mgr.Destroy(); python.execute( "import chr, chrmgr\n" "chr_names=('DismountHorse','MountHorse','SetSash','Destroy','Update','Deform','Render','RenderCollision'," "'CreateInstance','DeleteInstance','DeleteInstanceByFade','SelectInstance','HasInstance','IsEnemy','IsNPC'," "'IsGameMaster','IsPartyMember','Select','SetAddRenderMode','SetBlendRenderMode','Unselect','Hide','Show','Pick'," "'PickAll','SetArmor','SetWeapon','ChangeShape','SetRace','SetHair','ChangeHair','SetVirtualID','SetNameString'," "'SetInstanceType','SetPixelPosition','SetDirection','Refresh','Revive','Die','AttachEffectByID','AttachEffectByName'," "'LookAt','SetMotionMode','SetLoopMotion','BlendLoopMotion','PushOnceMotion','PushLoopMotion','GetPixelPosition'," "'SetRotation','SetRotationAll','BlendRotation','GetRotation','GetRace','GetName','GetNameByVID','GetGuildID'," "'GetProjectPosition','GetVirtualNumber','GetInstanceType','GetBoundBoxOnlyXY','RaceToJob','RaceToSex'," "'testGetPKData','FaintTest','SetMoveSpeed','SetAttackSpeed','WeaponTraceSetTexture','WeaponTraceUseAlpha'," "'WeaponTraceUseTexture','MoveToDestPosition','testSetComboType','testSetAddRenderMode','testSetModulateRenderMode'," "'testSetAddRenderModeRGB','testSetModulateRenderModeRGB','testSetSpecularRenderMode','testSetSpecularRenderMode2'," "'testRestoreRenderMode','testSetRideMan','IsStone')\n" "funcs=sorted(n for n in dir(chr) if callable(getattr(chr,n)))\n" "assert funcs==sorted(chr_names), set(funcs)^set(chr_names)\n" "assert not hasattr(chr,'RequestPetName')\n" "mgr_names=('SetRaceHeight','SetEmpireNameMode','GetVIDInfo','GetPickedVID','SetShapeModel','AppendShapeSkin'," "'SetPathName','LoadRaceData','LoadLocalRaceData','CreateRace','SelectRace','RegisterAttachingBoneName'," "'RegisterMotionMode','SetMotionRandomWeight','RegisterNormalAttack','ReserveComboAttack','RegisterComboAttack'," "'ReserveComboAttackNew','RegisterComboAttackNew','RegisterMotionData','RegisterRaceName','RegisterRaceSrcName'," "'RegisterCacheMotionData','SetAffect','SetEmoticon','IsPossibleEmoticon','RegisterEffect','RegisterCacheEffect'," "'RegisterPointEffect','ShowPointEffect','ToggleDirectionLine','SetMovingSpeed','SetDustGap','SetHorseDustGap'," "'RegisterTitleName','RegisterNameColor','RegisterTitleColor')\n" "funcs=sorted(n for n in dir(chrmgr) if callable(getattr(chrmgr,n)))\n" "assert funcs==sorted(mgr_names), set(funcs)^set(mgr_names)\n" "assert not hasattr(chrmgr,'RegisterMotion')\n" // No selected / unknown instance answers. "assert chr.GetRotation()==0.0 and chr.GetRace()==0 and chr.GetName()==0\n" "assert chr.GetNameByVID(5)=='None' and chr.GetGuildID(5)==0\n" "assert chr.GetProjectPosition(5,0)==(-100,-100)\n" "assert chr.GetVirtualNumber(5)==6 and chr.GetInstanceType(5)==6\n" "assert chr.GetBoundBoxOnlyXY()==(0.0,0.0,0.0,0.0)\n" "assert chr.testGetPKData()==(0,4)\n" "assert chr.Pick()==-1 and chr.PickAll()==-1 and chrmgr.GetPickedVID()==-1\n" "assert chr.GetPixelPosition(5) is None\n" "assert chr.IsEnemy(5)==0 and chr.IsNPC(5)==0 and chr.IsStone(5)==0 and chr.IsGameMaster(5)==0\n" "chr.SetDirection(1)\n" // chr argument errors: Py_BuildException() clears and answers None; chrmgr raises. "assert chr.HasInstance() is None and chr.SetRace('x') is None\n" "for f,a in ((chrmgr.CreateRace,()),(chrmgr.SetDustGap,('x',)),(chrmgr.GetVIDInfo,()),(chr.WeaponTraceSetTexture,())," "(chr.MoveToDestPosition,(1,2)),(chr.testSetRideMan,(1,))):\n" " try:\n" " f(*a)\n" " raise AssertionError('no error: '+f.__name__)\n" " except TypeError:\n" " pass\n" "assert (chr.RaceToJob(5),chr.RaceToSex(5),chr.RaceToSex(1),chr.RaceToSex(7))==(1,1,0,1)\n" // RegisterInstance (no dict) and the selected-instance setters. "chr.CreateInstance(10)\n" "assert chr.HasInstance(10)==1 and chr.GetInstanceType(10)==6\n" "chr.SelectInstance(10)\n" "chr.SetRotation(90.0)\n" "assert abs(chr.GetRotation()-90.0)<1e-3\n" "chr.SetDirection(2)\n" "assert abs(chr.GetRotation()-270.0)<1e-3\n" "chr.SetDirection(9)\n" "assert abs(chr.GetRotation()-360.0)<1e-3\n" "chr.BlendRotation(30.0, 0.1)\n" "assert abs(chr.GetRotation()-30.0)<1e-3\n" "chr.SetRotationAll(1.0, 2.0, 200.0)\n" "assert abs(chr.GetRotation()-160.0)<1e-3\n" "chr.SetPixelPosition(100, 200, 30)\n" "assert chr.GetPixelPosition(10)==(100.0,200.0,30.0)\n" "chr.SetPixelPosition(150, 250)\n" "assert chr.GetPixelPosition(10)==(150.0,250.0,0.0)\n" "chr.SetNameString('Hero')\n" "assert chr.GetName()=='Hero' and chr.GetNameByVID(10)=='Hero'\n" "chr.SetRace(3)\n" "assert chr.GetRace()==3\n" "chr.SetInstanceType(2)\n" "assert chr.IsStone(10)==1 and chr.GetInstanceType(10)==2\n" "chr.SetInstanceType(1)\n" "assert chr.IsNPC(10)==1\n" "chr.SetInstanceType(0)\n" "assert chr.IsEnemy(10)==1\n" "assert chr.testGetPKData()[0]==0\n" "chr.Hide()\n" "assert chrmgr.GetVIDInfo(10)=='VID 10 (isRegistered=1, isAlive=1, isDead=0) pos=(150, 250)'\n" "assert chrmgr.GetVIDInfo(99)=='VID 99 (isRegistered=0, isAlive=0, isDead=0) '\n" // CreateInstance's dict path: "y" lands in m_lPosX like Windows. "chr.CreateInstance(20, {'x':5, 'y':7, 'horse':0})\n" "assert chr.HasInstance(20)==1 and chr.GetInstanceType(20)==6 and chr.GetVirtualNumber(20)==0\n" "assert chr.GetPixelPosition(20)[0]==7.0 and chr.GetPixelPosition(20)[1]==0.0\n" "assert chr.GetNameByVID(20)=='NONAME'\n" "chr.CreateInstance(20, {'x':1})\n" "assert chr.GetPixelPosition(20)[0]==7.0\n" "assert chr.CreateInstance(30, 5) is None and chr.HasInstance(30)==0\n" "chr.testSetRideMan(40, 50)\n" "assert chr.HasInstance(1)==1 and chr.GetPixelPosition(1)[:2]==(40.0,50.0)\n" "chr.testSetRideMan(60, 70)\n" "assert chr.GetPixelPosition(1)[:2]==(40.0,50.0)\n" "chr.DeleteInstance(20)\n" "assert chr.HasInstance(20)==0\n" // chrmgr race manager calls. "chrmgr.SetPathName('c:/other/')\n" "chrmgr.SetRaceHeight(7, 1.5)\n" "chrmgr.ToggleDirectionLine()\n" "assert chrmgr.IsPossibleEmoticon(-1) in (0,1)\n" "chr.Update()\n" "print 'chr script ok'\n"); const auto beforeCompile=python.compilation_count(); const std::string cachedDispatch="__cache_result=__cache_callback()\n"; python.execute("__cache_callback=lambda: 17\n"); python.execute(cachedDispatch); const auto cachedCompile=python.compilation_count(); python.execute("assert __cache_result==17\n__cache_callback=lambda: 29\n"); const auto rebindCompile=python.compilation_count(); python.execute(cachedDispatch); require(python.compilation_count()==rebindCompile,"repeated dispatch reuses code but reads current globals"); python.execute("assert __cache_result==29\n"); require(cachedCompile>beforeCompile,"first dispatch compiles"); bool failed=false; try {python.execute("raise ValueError('cache regression')\n");}catch(const std::runtime_error&){failed=true;} require(failed,"cached runtime exceptions retain error behavior"); python.execute("assert 1+1==2\n"); for(int i=0;i<100;++i)python.execute("__cache_counter="+std::to_string(i)+"\n"); require(python.compiled_code_count()<=64 && python.compiled_code_bytes()<=128u*1024u,"code cache is bounded"); python.execute(cachedDispatch); python.execute("assert __cache_result==29\n"); const auto* hidden=mgr.GetActorData(10); require(hidden && hidden->m_isHidden,"chr.Hide marks the selected instance hidden"); require(RaceManager::Instance().GetFullPathFileName("a.msa")!="c:/other/a.msa","SetPathName ignores paths without d:/ymir"); require(RaceManager::Instance().GetRaceHeight(7)==1.5f,"SetRaceHeight stores the race height"); python.execute( "chrmgr.SetPathName('d:/ymir work/pc/warrior/')\n" "chr.SelectInstance(10)\n" "chr.Show()\n"); require(RaceManager::Instance().GetFullPathFileName("a.msa")=="d:/ymir work/pc/warrior/a.msa","SetPathName keeps d:/ymir paths"); require(!mgr.GetActorData(10)->m_isHidden,"chr.Show clears the hidden flag"); python.execute("chr.Destroy()\nassert chr.HasInstance(10)==0 and chr.HasInstance(1)==0\n"); std::cout<<"AVA2 chr parity passed"<