{"id":26,"date":"2021-06-27T20:04:17","date_gmt":"2021-06-27T20:04:17","guid":{"rendered":"https:\/\/blogs.scummvm.org\/ayyg\/?p=26"},"modified":"2022-05-26T15:18:45","modified_gmt":"2022-05-26T06:18:45","slug":"week-%e2%88%9e","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/ayyg\/2021\/06\/27\/week-%e2%88%9e\/","title":{"rendered":"Week \u221e"},"content":{"rendered":"\r\n<p>This time&#8217;s post will be a big summary of the events up until the present point. I feel like this is necessary so we can get into cool in-depth stuff soon!<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Tile Drawing<\/h2>\r\n\r\n\r\n\r\n<p>Similar to the debug images we tested last time, we first get clean up <code>tile.cpp<\/code> by getting rid of any remaining traces of their memory manager <code>rmem<\/code>. Then we adopt some debug methods and use them inside of the main run function.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>void testTileRendering() {\r\n\ttileRes = resFile-&gt;newContext(MKTAG('T', 'I', 'L', 'E'), \"tile resources\");\r\n\tlistRes = objResFile-&gt;newContext(MKTAG('L', 'I', 'S', 'T'), \"list resources\");\r\n\tresImports = (ResImportTable *)LoadResource(listRes, MKTAG('I', 'M', 'P', 'O'), \"res imports\");\r\n\r\n\tinitResourceHandles();\r\n\tmainPort.setDisplayPage(&amp;protoPage);\r\n\tinitPanelSystem();\r\n\tinitDisplayPort();\r\n\tinitDisplay();\r\n\tinitGameMaps();\r\n\ttestTiles();\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Along with the actual tile debug blitting function <code>testTiles<\/code>, I also call a lot of initialization methods. These methods are supposed to be called the game&#8217;s own initialization tower procedure, located in a file called <code>towerfta.cpp<\/code>. However, since we&#8217;re trying to blit tiles without going through the initialization, we have to call these methods manually.<sup>1<\/sup><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>void testTiles() {\r\n\tinitTileCyclingStates();\r\n...\r\n\tsetCurrentMap(0);\r\n\tPlayModeSetup();\r\n\r\n\t\/\/ draws tiles to tileDrawMap.data\r\n\tdrawMetaTiles();\r\n\r\n\tuint8 *img = tileDrawMap.data;\r\n\tPoint16 size = tileDrawMap.size;\r\n\tdebugC(3, kDebugTiles, \"img = %p, size = %d,%d\", (void *)img, size.x, size.y);\r\n\r\n\tGraphics::Surface sur;\r\n\tsur.create(size.x, size.y, Graphics::PixelFormat::createFormatCLUT8());\r\n\tsur.setPixels(img);\r\n\tsur.debugPrint();\r\n\tg_system-&gt;copyRectToScreen(sur.getPixels(), sur.pitch, 0, 0, sur.w, sur.h);\r\n...\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>The <code>testTiles<\/code> method has quite some stuff going on, but the important parts here are <code>setCurrentMap(0)<\/code> and <code>drawMetaTiles()<\/code>. <code>drawMetaTiles<\/code> takes in the current map, which we set with <code>setCurrentMap<\/code>, and then blits it into <code>gPixelMap tileDrawMap<\/code>. It is analogous to OSystem&#8217;s <code>Graphics::Surface<\/code>.<\/p>\r\n\r\n\r\n\r\n<p>About 3 or 4 hierarchies below the <code>drawMetaTiles<\/code> call, we have a call to <code>drawTile<\/code>, which is defined in assembly. Thus, this is probably the method we need to implement.<\/p>\r\n\r\n\r\n\r\n<p>Thankfully, SAGA engine has a method of the same name implemented in <code>saga\/isomap.cpp<\/code>. After implementing it inside of SAGA2, commenting out irrelevant blocks and changing values to FTA2 standards, we get the following result:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\">\r\n<figcaption>\r\n<figure id=\"attachment_107\" aria-describedby=\"caption-attachment-107\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-107\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/1.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/1.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/1-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/1-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-107\" class=\"wp-caption-text\">Never did squares look so beautiful.<\/figcaption><\/figure>\r\n<\/figcaption>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>It blits <em>mostly<\/em> correctly. Here we see horizontal stripes throughout the image, which we later learn is the result of the tile width constant. <em>Spoilers.<\/em><\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Scripts<\/h2>\r\n\r\n\r\n\r\n<p>We run the same methodology on scripts:<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li>Clean up <code>interp.cpp<\/code><\/li>\r\n<li>Find a way to test it out.<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<p>For item 1 however, we have to be careful of these kinds of expressions too:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>return strSeg + ((uint16 *)strSeg)[strNum];<\/code><\/pre>\r\n\r\n\r\n\r\n<p>The problem with this expression is that we&#8217;re trying to read a WORD (2 bytes) from a byte array that is loaded from an external resource (i.e. files). The result will change depending on whether the machine is Big Endian or Little Endian. For this reason, we use <code>READ_LE_16<\/code> to specify 16 bits to be read in Little Endian:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>return strSeg + READ_LE_INT16(strSeg + 2 * strNum);<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Viva OSystem! Portable code has never been so easy.<\/p>\r\n\r\n\r\n\r\n<p>For item 2 we do the following:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>void testScripts() {\r\n\tscriptCallFrame scf;\r\n\t\/\/for (int i = 1; i &lt; 100; ++i)\r\n\t\/\/\trunScript(i, scf);\r\n\trunScript(1, scf);\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>We also confirm it is trying to run scripts through debug statements:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Scripts: op_enter: [SagaObject].WriteName \r\nScripts: op_return<\/code><\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Initialization<\/h2>\r\n\r\n\r\n\r\n<p>I will mostly just skim over the procedure, but many of initialization steps deserve their own posting.<\/p>\r\n\r\n\r\n\r\n<p>Now that we have the basic resource loading and image displaying working, it is time to the proper tower initialization I mentioned before. The entire structure is as follows<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>TowerLayer tower[fullyInitialized] = {\r\n\t{ nothingInitialized,        &amp;initTowerBase,        &amp;termTowerBase },\r\n\t{ errHandlersInitialized,    &amp;initErrorManagers,    &amp;termErrorManagers },\r\n\t{ delayedErrInitialized,     &amp;initDelayedErrors,    &amp;termDelayedErrors },\r\n\t{ activeErrInitialized,      &amp;initActiveErrors,     &amp;termActiveErrors },\r\n\t{ configTestInitialized,     &amp;initSystemConfig,     &amp;termTowerBase },\r\n\t{ memoryInitialized,         &amp;initMemPool,          &amp;termMemPool },\r\n\t{ introInitialized,          &amp;initPlayIntro,        &amp;termPlayOutro },\r\n\t{ timerInitialized,          &amp;initSystemTimer,      &amp;termSystemTimer },\r\n\t{ resourcesInitialized,      &amp;initResourceFiles,    &amp;termResourceFiles },\r\n\t{ serversInitialized,        &amp;initResourceServers,  &amp;termResourceServers },\r\n\t{ pathFinderInitialized,     &amp;initPathFinders,      &amp;termPathFinders },\r\n\t{ scriptsInitialized,        &amp;initSAGAInterpreter,  &amp;termSAGAInterpreter },\r\n\t{ audStartInitialized,       &amp;initAudioChannels,    &amp;termAudioChannels },\r\n\t{ tileResInitialized,        &amp;initResourceHandles,  &amp;termResourceHandles },\r\n\t{ palettesInitialized,       &amp;initPalettes,         &amp;termPalettes },\r\n\t{ mainWindowInitialized,     &amp;initDisplayPort,      &amp;termDisplayPort },\r\n\t{ panelsInitialized,         &amp;initPanelSystem,      &amp;termPanelSystem },\r\n\t{ mainWindowOpenInitialized, &amp;initMainWindow,       &amp;termMainWindow },\r\n\t{ guiMessInitialized,        &amp;initGUIMessagers,     &amp;termGUIMessagers },\r\n\t{ mouseImageInitialized,     &amp;initMousePointer,     &amp;termMousePointer },\r\n\t{ displayInitialized,        &amp;initDisplay,          &amp;termDisplay },\r\n\t{ mapsInitialized,           &amp;initGameMaps,         &amp;termGameMaps },\r\n\t{ patrolsInitialized,        &amp;initRouteData,        &amp;termRouteData },\r\n\t{ spritesInitialized,        &amp;initActorSprites,     &amp;termActorSprites },\r\n\t{ weaponsInitialized,        &amp;initWeaponData,       &amp;termWeaponData },\r\n\t{ magicInitialized,          &amp;initSpellData,        &amp;termSpellData },\r\n\t{ objectSoundFXInitialized,  &amp;initObjectSoundFX,    &amp;termObjectSoundFX },\r\n\t{ prototypesInitialized,     &amp;initObjectPrototypes, &amp;termObjectPrototypes },\r\n\t{ gameStateInitialized,      &amp;initDynamicGameData,  &amp;termDynamicGameData },\r\n\t{ gameModeInitialized,       &amp;initGameMode,         &amp;termGameMode },\r\n\t{ gameDisplayEnabled,        &amp;initTop,              &amp;termTop },\r\n\t{ procResEnabled,            &amp;initProcessResources, &amp;termProcessResources }\r\n};<\/code><\/pre>\r\n\r\n\r\n\r\n<p>While TowerLayer is a struct defined the following way:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>struct TowerLayer {\r\n\tint \t\t\tord;\r\n\tpPROGRAM_INITIALIZER\tinit;\r\n\tpPROGRAM_TERMINATOR\tterm;\r\n\tconfigProblem\t\tonFail;\r\n};<\/code><\/pre>\r\n\r\n\r\n\r\n<p><code>pPROGRAM_INITIALIZER<\/code> and <code>pPROGRAM_TERMINATOR<\/code> are simply typedefs for function pointers. We don&#8217;t need to care about <code>configProblem<\/code> right now.<\/p>\r\n\r\n\r\n\r\n<p>You may be able to guess how the initialization works now. We go through each step in the <code>tower<\/code> table, calling the appropriate initialization functions (and vice-versa when terminating). So our plan of attack is to simply connect to the game&#8217;s main loop, reach the tower initialization process and fix the errors that pop up (and boy, will many errors pop up).<\/p>\r\n\r\n\r\n\r\n<p>These errors are mostly memory management ones related to <code>rmem<\/code> (such as the use of <code>RHANDLE<\/code>). Some others arise from the difference in architecture (such as different pointer sizes) or from writing into &#8220;packed&#8221; structs. Yet more errors result from their custom timer system.<\/p>\r\n\r\n\r\n\r\n<p><code>rmem<\/code> related errors are easy to fix, but resource loading-related ones require direct writing into each resource struct, like this:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>static void readGameObject(hResContext *con, ResourceGameObject &amp;obj) {\r\n\tobj.protoIndex = con-&gt;readS16LE();\r\n\tobj.location.u = con-&gt;readS16LE();\r\n\tobj.location.v = con-&gt;readS16LE();\r\n\tobj.location.z = con-&gt;readS16LE();\r\n\tobj.nameIndex = con-&gt;readU16LE();\r\n\tobj.parentID = con-&gt;readU16LE();\r\n\tobj.script = con-&gt;readU16LE();\r\n\tobj.objectFlags = con-&gt;readU16LE();\r\n\tobj.hitPoints = con-&gt;readByte();\r\n\tobj.misc = con-&gt;readU16LE();\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p><em>We end up scraping this design later on though, opting instead to load resources in each struct\/class&#8217;s constructor or member functions.<\/em><\/p>\r\n\r\n\r\n\r\n<p>Finally, the timer related errors caused headache for some time because it caused the game to stop progressing without us knowing. This particular example doesn&#8217;t happen <em>at this moment in time<\/em>, but I&#8217;m gonna explain it here in order to not break the flow. I was running around in circles trying to figure out just <em>why was the screen not updating<\/em>*, until I noticed there was a check like this in a routine that updates the screen:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/  If it's time to do a new frame.\r\nif (frameAlarm.check()\r\n\t&amp;&amp;  tileLockFlag == 0) {<\/code><\/pre>\r\n\r\n\r\n\r\n<p>We hadn&#8217;t implemented timers yet, so this check never passed. After that, sev (my mentor) rewrote the timers so that they made use of OSystem&#8217;s TimerManager.<\/p>\r\n\r\n\r\n\r\n<p>After fixing some more initialization steps, enabling the cursor and some other stuff, we eventually were into the event loop looking at this:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-108\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/2.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/2.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/2-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/2-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Drawing UI<\/h2>\r\n\r\n\r\n\r\n<p>We knew some things were being drawn on the screen. We also knew we could draw images properly due to our previous test run. So why were we getting garbage?<\/p>\r\n\r\n\r\n\r\n<p>Well, spoilers ahead.<\/p>\r\n\r\n\r\n\r\n<p>1. The main surface on which the UI is drawn on was not initialized<\/p>\r\n\r\n\r\n\r\n<p>2. Our blitting functions were being ignored by a display check that returned false due to Windows DD not being present.<\/p>\r\n\r\n\r\n\r\n<p>After fixing both of those issues, things were properly drawing<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\">\r\n<figure id=\"attachment_110\" aria-describedby=\"caption-attachment-110\" style=\"width: 1914px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-110\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3.png\" alt=\"\" width=\"1914\" height=\"1560\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3.png 1914w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3-300x245.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3-1024x835.png 1024w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3-768x626.png 768w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3-1536x1252.png 1536w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3-480x391.png 480w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/3-960x782.png 960w\" sizes=\"auto, (max-width: 1914px) 100vw, 1914px\" \/><\/a><figcaption id=\"caption-attachment-110\" class=\"wp-caption-text\">Would you look at the progress.<\/figcaption><\/figure>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>By clicking on the UI and fixing stuff that crashed, we eventually got other parts of the UI showing up too.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\">\r\n<figure id=\"attachment_111\" aria-describedby=\"caption-attachment-111\" style=\"width: 1914px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-111\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4.png\" alt=\"\" width=\"1914\" height=\"1560\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4.png 1914w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4-300x245.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4-1024x835.png 1024w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4-768x626.png 768w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4-1536x1252.png 1536w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4-480x391.png 480w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/4-960x782.png 960w\" sizes=\"auto, (max-width: 1914px) 100vw, 1914px\" \/><\/a><figcaption id=\"caption-attachment-111\" class=\"wp-caption-text\">Tasty.<\/figcaption><\/figure>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>I went to sleep and left it to sev to implement the rest of the sprite related routines. He also fixed the tile width constant to the proper value.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-112\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5.png\" alt=\"\" width=\"1914\" height=\"1560\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5.png 1914w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5-300x245.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5-1024x835.png 1024w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5-768x626.png 768w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5-1536x1252.png 1536w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5-480x391.png 480w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/5-960x782.png 960w\" sizes=\"auto, (max-width: 1914px) 100vw, 1914px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Drawing Platforms<\/h2>\r\n\r\n\r\n\r\n<p>So our next step was to figure out what&#8217;s going on with the game map. Cutting to the chase, something called <code>DList<\/code> was the problem. I haven&#8217;t mentioned it much, but removing it completely is also one of our goals.<br \/>In FTA2, platform (map) fetching makes use of a <code>cache<\/code> design, and after (haphazardly) getting rid of it I was greeted with this sight.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/6.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/6.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/6-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/6-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>This sure seems like a lot of progress, but I later learned that the window was not updating properly when we fixed the problem mentioned above*.<\/p>\r\n\r\n\r\n\r\n<p>After that was fixed, we started seeing the map flickering:<\/p>\r\n\r\n\r\n\r\n<p><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/fta2.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-114\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/fta2.gif\" alt=\"\" width=\"631\" height=\"469\" \/><\/a><\/p>\r\n\r\n\r\n\r\n<p>I&#8217;m gonna spoil once again, but this was the result of not properly porting the cacheing in the platform fetching.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Headache-inducing Scripts<\/h2>\r\n\r\n\r\n\r\n<p>This problem took a particularly long time to figure out. It deserves its own post, so I&#8217;m not going to explain the details just yet.<\/p>\r\n\r\n\r\n\r\n<p>Basically, we were crashing at a script execution. We thought of many possibilities, such as the wrong script being called. However sev checked against the original and confirmed that we were in the correct script call. The script call in question was this one:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>int16 scriptSelectNearbySite(int16 *args) {\r\n\tMONOLOG(SelectNearbySite);\r\n\tTilePoint       tp;\r\n\r\n\ttp = selectNearbySite(args[3],\r\n\t                      TilePoint(args[0], args[1], args[2]),\r\n\t                      args[4],\r\n\t                      args[5],\r\n\t                      args[6]);\r\n\r\n\tif (tp == Nowhere) return 0;\r\n\tscriptCallFrame     &amp;scf = thisThread-&gt;threadArgs;\r\n\r\n\tscf.coords = tp;\r\n\treturn true;\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Particularly, we crashed because the first argument, <code>args[3]<\/code>, didn&#8217;t represent a valid <code>WorldID<\/code>.<\/p>\r\n\r\n\r\n\r\n<p>Cutting to the chase, the problem was that their scripts access memory according to a 32-bit layout. In my 64-bit machine, the structs are different. In particular, pointer sizes are different. This difference caused the script to grab the wrong address.<\/p>\r\n\r\n\r\n\r\n<p>After noticing that (and feeling very smart about it), we fixed it by implementing a packed struct object containing all data in the expected address.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Motion At Last<\/h2>\r\n\r\n\r\n\r\n<p>And with that, we were able to walk around.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-115\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/7.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/7.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/7-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/7-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>You may see the tiles not loaded in the left side. The reason for that is because we are drawing at the wrong offset. After correcting for that and removing <code>rmem<\/code> from <code>speech.cpp<\/code>, we are able to see something resembling gameplay.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-116\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/8.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/8.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/8-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/8-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/figure>\r\n\r\n\r\n\r\n<p>We also had our fair share of weird bugs.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\">\r\n<figure id=\"attachment_117\" aria-describedby=\"caption-attachment-117\" style=\"width: 1914px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-117\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9.png\" alt=\"\" width=\"1914\" height=\"1560\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9.png 1914w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9-300x245.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9-1024x835.png 1024w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9-768x626.png 768w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9-1536x1252.png 1536w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9-480x391.png 480w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/9-960x782.png 960w\" sizes=\"auto, (max-width: 1914px) 100vw, 1914px\" \/><\/a><figcaption id=\"caption-attachment-117\" class=\"wp-caption-text\">This is&#8230; requiem.<\/figcaption><\/figure>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>The above was the result of, <em>again<\/em>, improper platform fetching. More specifically, I had removed some lines of code and forgotten about them. <em>Oops<\/em>.<\/p>\r\n\r\n\r\n\r\n<p>We (aka sev) fixed the buckets on the inventory by extracting the method codes from the disassembly. After removing some more <code>rmem<\/code> calls, we were able to learn skills, open our sack, open the map and much more.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\">\r\n<figure id=\"attachment_118\" aria-describedby=\"caption-attachment-118\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-118\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/10.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/10.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/10-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/10-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-118\" class=\"wp-caption-text\">Was this a game all along?<\/figcaption><\/figure>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>One problem though, is that actors were either not spawning or not moving. The later was fixed by setting proper flags in assignment creation.<sup>2<\/sup> The former was fixed by fixing a typo. Cheers.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>if (sect) {\r\n\tif (sect-&gt;isActivated())\r\n\t\tsect-&gt;activate(); \/\/ should be simply activate()\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Tasks<sup>3<\/sup> not being cleared was also another problem. That one was fixed by comparing the code with the original and putting <code>delete<\/code> expressions where they were missing. <em>Code refactoring is dangerous sometimes<\/em>.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Present Day, Present Time<\/h2>\r\n\r\n\r\n\r\n<p>The most recent (and also yet unsolved) bug is the one that relates to NPC Kwarrel.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\">\r\n<figure id=\"attachment_119\" aria-describedby=\"caption-attachment-119\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-119\" src=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/11.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/11.png 640w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/11-300x225.png 300w, https:\/\/blogs.scummvm.org\/ayyg\/wp-content\/uploads\/sites\/7\/2021\/06\/11-480x360.png 480w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-119\" class=\"wp-caption-text\">FTA2: Halls of the Talking Corpses<\/figcaption><\/figure>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>He dies while talking to you. <em>I wonder if that could be considered murder.<\/em><\/p>\r\n\r\n\r\n\r\n<p>We have a few leads, such as multiple speech tasks being created. It probably relates to timers, but we have no clue about what exactly is wrong yet.<\/p>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<p>Alright, it was a bit long, but that&#8217;s mostly it. Quite a few details are missing, but we&#8217;ll fill them up as we go!<\/p>\r\n\r\n\r\n<hr class=\"wp-block-separator\" \/>\r\n\r\n\r\n<p>1: I spent <em>a lot<\/em> of time to get the correct procedure here.<\/p>\r\n\r\n\r\n\r\n<p>2, 3: Assignments? Assignment creation? Tasks? We&#8217;ll talk about those later.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>This time&#8217;s post will be a big summary of the events up until the present point. I feel like this is necessary so we can get into cool in-depth stuff soon! Tile Drawing Similar to the debug images we tested last time, we first get clean up tile.cpp by getting rid of any remaining traces [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":27,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":7,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/posts\/26\/revisions\/120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/media\/27"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/ayyg\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}