Page 1 of 1

[CLOSED] Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Posted: 18 Feb 2023 21:15
by Jaume
Hi!

Since OpenTTD v1.13 the function AIGroup.CreateGroup() requires two parameters. Some AIs can crash because older versions of AIGroup.CreateGroup() function requires only one parameter.

In SuperSimpleAI v39 I fixed this with this class:

Code: Select all

/**
 * This file is part of SuperSimpleAI: An OpenTTD AI.
 *
 * Author: Jaume Sabater
 *
 * It's free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.
 *
 * You should have received a copy of the GNU General Public License
 * with it.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Define the MyAIGroup class which extends the AIGroup functions.
 */
class MyAIGroup /* extends AIGroup */
{
        /**
         * Create a new group.
         * @param vehicle_type The type of vehicle to create a group for.
         * @param parent_group_id The parent group id to create this group under,
         *         GROUP_INVALID for top-level (default and optional).
         * @return The GroupID of the new greup, or an invalid GroupID when it failed.
         *         Check the return value using AIGroup.IsValidGroup(). In test-mode 0
         *         is returned if it was successful; any other value indicates failure.
         */
        static function CreateGroup(vehicle_type, parent_group_id = AIGroup.GROUP_INVALID);
}

local ottd_v = AIController.GetVersion();
local ottd_major = (ottd_v & 0xF0000000) >> 28;
local ottd_minor = (ottd_v & 0x0F000000) >> 24;

if (ottd_major > 0 && ottd_minor > 12) {
        // Since OpenTTD 1.13 AIGroup.CreateGroup() requires two parameters.
        function MyAIGroup::CreateGroup(vehicle_type, parent_group_id = AIGroup.GROUP_INVALID)
        {
                return AIGroup.CreateGroup(vehicle_type, parent_group_id);
        }
} else {
        function MyAIGroup::CreateGroup(vehicle_type, parent_group_id = AIGroup.GROUP_INVALID)
        {
                return AIGroup.CreateGroup(vehicle_type);
        }
}
You can use it to fix your AI!

Re: Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Posted: 19 Feb 2023 01:14
by glx
AIGroup.CreateGroup() requires two parameters since API version 1.9.

Re: Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Posted: 19 Feb 2023 14:09
by frosch
This is not how it is supposed to work.

* AIGroup::CreateGroup was changed in 1.9. See the changelog: https://docs.openttd.org/ai-api/ai__changelog_8hpp.html
* OpenTTD provides an API compatibility layer. It provides the API matching the version requested in your info.nut file: https://github.com/OpenTTD/OpenTTD/blob ... at_1.2.nut
* SuperSimpleAI specifies API version 1.2 in its info.nut file.
* You can see the compatibility script getting loaded in the first line "1.2 API compatibility in effect." of the AI debug window, see attached screenshot.
* SuperSimpleAI v39 now crashes almost immediately after start, because it tries to call the AIGroup::CreateGroup with the 1.9 prototype, while the 1.2 API is active. See attached screenshot.

I can only guess that your installation is somehow broken, and OpenTTD can't find the compatibility scripts.

Re: Changes in OpenTTD 1.13 AIGroup.CreateGroup() can crash some AIs

Posted: 19 Feb 2023 19:50
by Jaume
Thank you.

I had problems with my libraries, now runs ok.