{"id":694,"date":"2024-05-22T12:35:23","date_gmt":"2024-05-22T11:35:23","guid":{"rendered":"https:\/\/www.knightsgame.org.uk\/blog\/?p=694"},"modified":"2024-05-22T12:54:02","modified_gmt":"2024-05-22T11:54:02","slug":"lua-and-enet-upgrades","status":"publish","type":"post","link":"https:\/\/www.knightsgame.org.uk\/blog\/2024\/05\/22\/lua-and-enet-upgrades\/","title":{"rendered":"Lua and ENet upgrades"},"content":{"rendered":"\n<p><strong>Summary: <\/strong>In the next release of Knights I will be upgrading the versions of ENet (a networking library) and Lua that are used by Knights. I will also be removing some custom patches to Lua that were being used previously. This will have the following effects:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Users will have to upgrade their game client (because the new ENet version is incompatible with the old one).<\/li>\n\n\n\n<li>The Lua code for mods might need to be updated.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Lua Change<\/h2>\n\n\n\n<p>I have updated the version of Lua used to the latest version, 5.4.6 (previously it was 5.1.4).<\/p>\n\n\n\n<p>The previous versions of Knights also included a custom Lua patch (written by me) which added a new &#8220;&amp;&#8221; operator to the language. This was used for merging tables, for example one could write code like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i_wand_of_destruction = kts.ItemType(\n  basic_wand &amp; {\n    melee_damage = 1000,\n    melee_stun_time = rng_time_range(2, 3),\n    melee_tile_damage = 1000\n  }\n)<\/code><\/pre>\n\n\n\n<p>This would make an ItemType that had all of the properties from the &#8220;basic_wand&#8221; table, together with the new properties shown.<\/p>\n\n\n\n<p>Adding the &#8220;&amp;&#8221; operator to Lua seemed like a good idea at the time &#8212; but it does cause problems, because it means I can&#8217;t easily upgrade to newer versions of Lua (as I would have to port my patch across to the new codebase every time).<\/p>\n\n\n\n<p>So, from now on I am removing this patch, and the &#8220;&amp;&#8221; table-merge operator will no longer be available.<\/p>\n\n\n\n<p>Instead I have written a Lua function that does the same thing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function table_merge(a, b)\n    local result = {}\n    for k, v in pairs(a) do\n        result&#91;k] = v\n    end\n    for k, v in pairs(b) do\n        result&#91;k] = v\n    end\n    return result\nend<\/code><\/pre>\n\n\n\n<p>This function is available as &#8220;kts.table_merge&#8221;. Therefore the above &#8220;wand of destruction&#8221; code must now be written like this instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i_wand_of_destruction = kts.ItemType(\n  kts.table_merge(\n    basic_wand,\n    {\n      melee_damage = 1000,\n      melee_stun_time = rng_time_range(2, 3),\n      melee_tile_damage = 1000\n    }\n  }\n)<\/code><\/pre>\n\n\n\n<p>This does make the code a little bit uglier I suppose, but it is not a big problem and I think this is a small price to pay, in exchange for making it much easier to do Lua upgrades in the future.<\/p>\n\n\n\n<p>Of course, any mods that were using &#8220;&amp;&#8221;, must now be modified (no pun intended) to use &#8220;kts.table_merge&#8221; instead, but this shouldn&#8217;t be too difficult.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ENet change<\/h2>\n\n\n\n<p>I have also taken the opportunity to upgrade ENet from version 1.2.5 to the latest available version (on Windows this will be 1.3.17, as this is the latest VCPKG package available; on Linux it will be whatever is installed on your local system, which will hopefully be reasonably up to date).<\/p>\n\n\n\n<p>The main issue here is that ENet 1.3.x is incompatible with 1.2.x, so users running the older versions of Knights will not be able to connect to the new Knights server. They won&#8217;t even get any message telling them they need to upgrade; the connection will just fail completely.<\/p>\n\n\n\n<p>To mitigate this, I&#8217;ve arranged for the &#8220;Connect to Server&#8221; screen to show a message like the following when players try to connect:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.knightsgame.org.uk\/blog\/wp-content\/uploads\/2024\/05\/screenshot-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"658\" src=\"https:\/\/www.knightsgame.org.uk\/blog\/wp-content\/uploads\/2024\/05\/screenshot-4-1024x658.png\" alt=\"Screenshot showing error message asking the player to upgrade to the latest version of Knights.\" class=\"wp-image-703\" style=\"width:690px;height:auto\" srcset=\"https:\/\/www.knightsgame.org.uk\/blog\/wp-content\/uploads\/2024\/05\/screenshot-4-1024x658.png 1024w, https:\/\/www.knightsgame.org.uk\/blog\/wp-content\/uploads\/2024\/05\/screenshot-4-300x193.png 300w, https:\/\/www.knightsgame.org.uk\/blog\/wp-content\/uploads\/2024\/05\/screenshot-4-768x494.png 768w, https:\/\/www.knightsgame.org.uk\/blog\/wp-content\/uploads\/2024\/05\/screenshot-4.png 1052w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Hopefully this will be enough to let players know what they have to do, and it won&#8217;t be a huge issue.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In the next release of Knights I will be upgrading the versions of ENet (a networking library) and Lua that are used by Knights. I will also be removing some custom patches to Lua that were being used previously. This will have the following effects: Lua Change I have updated the version of Lua &hellip; <a href=\"https:\/\/www.knightsgame.org.uk\/blog\/2024\/05\/22\/lua-and-enet-upgrades\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Lua and ENet upgrades<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":5,"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knightsgame.org.uk\/blog\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}